r_model_dpm.c

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 1997-2001 Darkplaces
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018 See the GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 
00024 */
00025 
00026 #include "r_local.h"
00027 
00031 void R_ModelLoadDPMVertsForFrame (model_t *mod, int frame)
00032 {
00033     int i, j;
00034     mAliasMesh_t *mesh;
00035     mAliasVertex_t *outVertex;
00036     const mAliasBoneVertex_t *boneVertex;
00037     const mAliasBoneMatrix_t *boneMatrix;
00038 
00039     assert(mod);
00040     assert(frame >= 0);
00041     assert(frame < mod->alias.num_frames);
00042 
00043     boneMatrix = mod->alias.frames[frame].boneMatrix;
00044 
00045     for (i = 0, mesh = mod->alias.meshes; i < mod->alias.num_meshes; i++, mesh++) {
00046         /* the vertices might change - so free the memory */
00047         if (mesh->vertexes)
00048             Mem_Free(mesh->vertexes);
00049         boneVertex = mesh->bonesVertexes;
00050         mesh->vertexes = outVertex = Mem_PoolAlloc(sizeof(mAliasVertex_t) * mesh->num_verts, vid_modelPool, 0);
00051         for (j = 0; j < mesh->num_verts; j++, outVertex++, boneVertex++) {
00052             outVertex->point[0] = boneVertex->origin[0] * boneMatrix->matrix[0][0]
00053                  + boneVertex->origin[1] * boneMatrix->matrix[0][1]
00054                  + boneVertex->origin[2] * boneMatrix->matrix[0][2]
00055                  + boneVertex->influence * boneMatrix->matrix[0][3];
00056             outVertex->point[1] = boneVertex->origin[0] * boneMatrix->matrix[1][0]
00057                  + boneVertex->origin[1] * boneMatrix->matrix[1][1]
00058                  + boneVertex->origin[2] * boneMatrix->matrix[1][2]
00059                  + boneVertex->influence * boneMatrix->matrix[1][3];
00060             outVertex->point[2] = boneVertex->origin[0] * boneMatrix->matrix[2][0]
00061                  + boneVertex->origin[1] * boneMatrix->matrix[2][1]
00062                  + boneVertex->origin[2] * boneMatrix->matrix[2][2]
00063                  + boneVertex->influence * boneMatrix->matrix[2][3];
00064         }
00065     }
00066 }
00067 
00072 void R_ModLoadAliasDPMModel (model_t *mod, byte *buffer, int bufSize)
00073 {
00074     mAliasMesh_t *outMesh;
00075     mAliasFrame_t *outFrame;
00076     mAliasBone_t *outBones;
00077     mAliasBoneMatrix_t *outBoneMatrix;
00078     mAliasBoneVertex_t *outBoneVertexes;
00079     int32_t *outIndex, *index;
00080     const float *texcoords;
00081     int num;
00082     int i, j, k;
00083     dpmbone_t *bone;
00084     dpmbonepose_t *bonepose;
00085     dpmmesh_t *mesh;
00086     dpmbonevert_t *bonevert;
00087     dpmvertex_t *vert;
00088     dpmframe_t *frame;
00089     dpmheader_t *dpm;
00090 
00091     /* fixed values */
00092     mod->type = mod_alias_dpm;
00093 
00094     /* get the disk data */
00095     dpm = (dpmheader_t *) buffer;
00096 
00097     dpm->type = BigLong(dpm->type);
00098     if (dpm->type != 2)
00099         Com_Error(ERR_DROP, "%s has wrong type number (%i should be 2)", mod->name, dpm->type);
00100 
00101     mod->mins[0] = BigFloat(dpm->mins[0]);
00102     mod->mins[1] = BigFloat(dpm->mins[1]);
00103     mod->mins[2] = BigFloat(dpm->mins[2]);
00104     mod->maxs[0] = BigFloat(dpm->maxs[0]);
00105     mod->maxs[1] = BigFloat(dpm->maxs[1]);
00106     mod->maxs[2] = BigFloat(dpm->maxs[2]);
00107     /*mod->yawradius = BigFloat(dpm->yawradius);*/
00108     mod->radius = BigFloat(dpm->allradius);
00109 
00110     mod->alias.num_bones = BigLong(dpm->num_bones);
00111     mod->alias.num_meshes = BigLong(dpm->num_meshs);
00112     mod->alias.num_frames = BigLong(dpm->num_frames);
00113     dpm->ofs_bones = BigLong(dpm->ofs_bones);
00114     dpm->ofs_meshs = BigLong(dpm->ofs_meshs);
00115     dpm->ofs_frames = BigLong(dpm->ofs_frames);
00116     mod->alias.bones = outBones = Mem_PoolAlloc(sizeof(mAliasBone_t) * mod->alias.num_bones, vid_modelPool, 0);
00117     for (i = 0, bone = (dpmbone_t *)((byte *)dpm + dpm->ofs_bones); i < mod->alias.num_bones; i++, bone++, outBones++) {
00118         outBones->parent = BigLong(bone->parent);
00119         outBones->flags = BigLong(bone->flags);
00120     }
00121 
00122     mod->alias.meshes = outMesh = Mem_PoolAlloc(sizeof(mAliasMesh_t) * mod->alias.num_meshes, vid_modelPool, 0);
00123     for (i = 0, mesh = (dpmmesh_t *)((byte *)dpm + dpm->ofs_meshs); i < mod->alias.num_meshes; i++, mesh++, outMesh++) {
00124         /* offsets are only needed for loading the model */
00125         mesh->ofs_verts = BigLong(mesh->ofs_verts);
00126         mesh->ofs_texcoords = BigLong(mesh->ofs_texcoords);
00127         mesh->ofs_indices = BigLong(mesh->ofs_indices);
00128         mesh->ofs_groupids = BigLong(mesh->ofs_groupids);
00129 
00130         outMesh->num_verts = BigLong(mesh->num_verts);
00131         outMesh->num_tris = BigLong(mesh->num_tris);
00132 
00133         /* load skins */
00134         outMesh->skins = Mem_PoolAlloc(sizeof(mAliasSkin_t), vid_modelPool, 0);
00135         outMesh->skins[0].skin = R_AliasModelGetSkin(mod->name, mesh->shadername);
00136         Q_strncpyz(outMesh->skins[0].name, outMesh->skins[0].skin->name, sizeof(outMesh->skins[0].name));
00137 
00138         /* load bone verts */
00139         num = 0;
00140         for (j = 0, vert = (dpmvertex_t *)((byte *)dpm + mesh->ofs_verts); j < outMesh->num_verts; j++) {
00141             vert->numbones = BigLong(vert->numbones);
00142             for (k = 0, bonevert = (dpmbonevert_t *)(vert + 1); k < vert->numbones; k++, bonevert++) {
00143                 bonevert->origin[0] = BigFloat(bonevert->origin[0]);
00144                 bonevert->origin[1] = BigFloat(bonevert->origin[1]);
00145                 bonevert->origin[2] = BigFloat(bonevert->origin[2]);
00146                 bonevert->influence = BigFloat(bonevert->influence);
00147                 bonevert->normal[0] = BigFloat(bonevert->normal[0]);
00148                 bonevert->normal[1] = BigFloat(bonevert->normal[1]);
00149                 bonevert->normal[2] = BigFloat(bonevert->normal[2]);
00150                 bonevert->bonenum = BigLong(bonevert->bonenum);
00151             }
00152             num += vert->numbones; /* count the boneverts to know how much mem we have to allocate for them */
00153             vert = (dpmvertex_t *)bonevert;
00154         }
00155 
00156         outMesh->bonesVertexes = outBoneVertexes = Mem_PoolAlloc(sizeof(mAliasBoneVertex_t) * num, vid_modelPool, 0);
00157         for (j = 0, vert = (dpmvertex_t *)((byte *)dpm + mesh->ofs_verts); j < outMesh->num_verts; j++) {
00158             for (k = 0, bonevert = (dpmbonevert_t *)(vert + 1); k < vert->numbones; k++, bonevert++, outBoneVertexes++) {
00159                 VectorCopy(bonevert->origin, outBoneVertexes->origin);
00160                 outBoneVertexes->influence = bonevert->influence;
00161                 VectorCopy(bonevert->normal, outBoneVertexes->normal);
00162                 outBoneVertexes->bonenum = bonevert->bonenum;
00163             }
00164             vert = (dpmvertex_t *)bonevert;
00165         }
00166 
00167         /* load texcoords */
00168         outMesh->stcoords = Mem_PoolAlloc(sizeof(mAliasCoord_t) * outMesh->num_verts, vid_modelPool, 0);
00169         for (num = 0, texcoords = (float *)((byte *)dpm + mesh->ofs_texcoords); num < outMesh->num_verts; num++, texcoords += 2) {
00170             outMesh->stcoords[j][0] = BigFloat(texcoords[0]);
00171             outMesh->stcoords[j][1] = BigFloat(texcoords[1]);
00172         }
00173 
00174         /* load indexes for faster array draw access */
00175         outMesh->indexes = outIndex = Mem_PoolAlloc(sizeof(int32_t) * outMesh->num_tris * 3, vid_modelPool, 0);
00176         for (num = 0, index = (int32_t *)((byte *)dpm + mesh->ofs_indices); num < outMesh->num_tris; num++, index += 3) {
00177             outIndex[0] = BigLong(index[0]);
00178             outIndex[1] = BigLong(index[2]);
00179             outIndex[2] = BigLong(index[3]);
00180         }
00181 
00182 #if 0
00183         for (num = 0, index = (int32_t *)((byte *)dpm + mesh->ofs_groupids); num < outMesh->num_tris; num++, index++)
00184             index[0] = BigLong(index[0]);
00185 #endif
00186     }
00187 
00188     /* load the frames */
00189     mod->alias.frames = outFrame = Mem_PoolAlloc(sizeof(mAliasFrame_t) * mod->alias.num_frames, vid_modelPool, 0);
00190     for (i = 0, frame = (dpmframe_t *)((byte *)dpm + dpm->ofs_frames); i < mod->alias.num_frames; i++, frame++, outFrame++) {
00191         outFrame->mins[0] = BigFloat(frame->mins[0]);
00192         outFrame->mins[1] = BigFloat(frame->mins[1]);
00193         outFrame->mins[2] = BigFloat(frame->mins[2]);
00194         outFrame->maxs[0] = BigFloat(frame->maxs[0]);
00195         outFrame->maxs[1] = BigFloat(frame->maxs[1]);
00196         outFrame->maxs[2] = BigFloat(frame->maxs[2]);
00197         /*outFrame->yawradius = BigFloat(frame->yawradius);*/
00198         outFrame->radius = BigFloat(frame->allradius);
00199         outFrame->boneMatrix = outBoneMatrix = Mem_PoolAlloc(sizeof(mAliasBoneMatrix_t) * mod->alias.num_bones, vid_modelPool, 0);
00200         frame->ofs_bonepositions = BigLong(frame->ofs_bonepositions);
00201         /* load the bones matrix */
00202         for (j = 0, bonepose = (dpmbonepose_t *)((byte *)dpm + frame->ofs_bonepositions); j < mod->alias.num_bones; j++, bonepose++, outBoneMatrix++) {
00203             outBoneMatrix->matrix[0][0] = BigFloat(bonepose->matrix[0][0]);
00204             outBoneMatrix->matrix[0][1] = BigFloat(bonepose->matrix[0][1]);
00205             outBoneMatrix->matrix[0][2] = BigFloat(bonepose->matrix[0][2]);
00206             outBoneMatrix->matrix[0][3] = BigFloat(bonepose->matrix[0][3]);
00207             outBoneMatrix->matrix[1][0] = BigFloat(bonepose->matrix[1][0]);
00208             outBoneMatrix->matrix[1][1] = BigFloat(bonepose->matrix[1][1]);
00209             outBoneMatrix->matrix[1][2] = BigFloat(bonepose->matrix[1][2]);
00210             outBoneMatrix->matrix[1][3] = BigFloat(bonepose->matrix[1][3]);
00211             outBoneMatrix->matrix[2][0] = BigFloat(bonepose->matrix[2][0]);
00212             outBoneMatrix->matrix[2][1] = BigFloat(bonepose->matrix[2][1]);
00213             outBoneMatrix->matrix[2][2] = BigFloat(bonepose->matrix[2][2]);
00214             outBoneMatrix->matrix[2][3] = BigFloat(bonepose->matrix[2][3]);
00215         }
00216     }
00217 
00218     for (i = 0; i < mod->alias.num_frames; i++)
00219         R_ModelLoadDPMVertsForFrame(mod, i);
00220 
00221     for (i = 0; i < mod->alias.num_meshes; i++)
00222         R_ModLoadArrayData(&mod->alias, &mod->alias.meshes[i], qtrue);
00223 }

Generated by  doxygen 1.6.2