Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

tr_animation.c

Go to the documentation of this file.
00001 /*
00002 ===========================================================================
00003 Copyright (C) 1999-2005 Id Software, Inc.
00004 
00005 This file is part of Quake III Arena source code.
00006 
00007 Quake III Arena source code is free software; you can redistribute it
00008 and/or modify it under the terms of the GNU General Public License as
00009 published by the Free Software Foundation; either version 2 of the License,
00010 or (at your option) any later version.
00011 
00012 Quake III Arena source code is distributed in the hope that it will be
00013 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Foobar; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 ===========================================================================
00021 */
00022 
00023 #include "tr_local.h"
00024 
00025 /*
00026 
00027 All bones should be an identity orientation to display the mesh exactly
00028 as it is specified.
00029 
00030 For all other frames, the bones represent the transformation from the 
00031 orientation of the bone in the base frame to the orientation in this
00032 frame.
00033 
00034 */
00035 
00036 /*
00037 ==============
00038 R_AddAnimSurfaces
00039 ==============
00040 */
00041 void R_AddAnimSurfaces( trRefEntity_t *ent ) {
00042     md4Header_t     *header;
00043     md4Surface_t    *surface;
00044     md4LOD_t        *lod;
00045     shader_t        *shader;
00046     int             i;
00047 
00048     header = tr.currentModel->md4;
00049     lod = (md4LOD_t *)( (byte *)header + header->ofsLODs );
00050 
00051     surface = (md4Surface_t *)( (byte *)lod + lod->ofsSurfaces );
00052     for ( i = 0 ; i < lod->numSurfaces ; i++ ) {
00053         shader = R_GetShaderByHandle( surface->shaderIndex );
00054         R_AddDrawSurf( (void *)surface, shader, 0 /*fogNum*/, qfalse );
00055         surface = (md4Surface_t *)( (byte *)surface + surface->ofsEnd );
00056     }
00057 }
00058 
00059 
00060 /*
00061 ==============
00062 RB_SurfaceAnim
00063 ==============
00064 */
00065 void RB_SurfaceAnim( md4Surface_t *surface ) {
00066     int             i, j, k;
00067     float           frontlerp, backlerp;
00068     int             *triangles;
00069     int             indexes;
00070     int             baseIndex, baseVertex;
00071     int             numVerts;
00072     md4Vertex_t     *v;
00073     md4Bone_t       bones[MD4_MAX_BONES];
00074     md4Bone_t       *bonePtr, *bone;
00075     md4Header_t     *header;
00076     md4Frame_t      *frame;
00077     md4Frame_t      *oldFrame;
00078     int             frameSize;
00079 
00080 
00081     if (  backEnd.currentEntity->e.oldframe == backEnd.currentEntity->e.frame ) {
00082         backlerp = 0;
00083         frontlerp = 1;
00084     } else  {
00085         backlerp = backEnd.currentEntity->e.backlerp;
00086         frontlerp = 1.0f - backlerp;
00087     }
00088     header = (md4Header_t *)((byte *)surface + surface->ofsHeader);
00089 
00090     frameSize = (int)( &((md4Frame_t *)0)->bones[ header->numBones ] );
00091 
00092     frame = (md4Frame_t *)((byte *)header + header->ofsFrames + 
00093         backEnd.currentEntity->e.frame * frameSize );
00094     oldFrame = (md4Frame_t *)((byte *)header + header->ofsFrames + 
00095         backEnd.currentEntity->e.oldframe * frameSize );
00096 
00097     RB_CheckOverflow( surface->numVerts, surface->numTriangles * 3 );
00098 
00099     triangles = (int *) ((byte *)surface + surface->ofsTriangles);
00100     indexes = surface->numTriangles * 3;
00101     baseIndex = tess.numIndexes;
00102     baseVertex = tess.numVertexes;
00103     for (j = 0 ; j < indexes ; j++) {
00104         tess.indexes[baseIndex + j] = baseIndex + triangles[j];
00105     }
00106     tess.numIndexes += indexes;
00107 
00108     //
00109     // lerp all the needed bones
00110     //
00111     if ( !backlerp ) {
00112         // no lerping needed
00113         bonePtr = frame->bones;
00114     } else {
00115         bonePtr = bones;
00116         for ( i = 0 ; i < header->numBones*12 ; i++ ) {
00117             ((float *)bonePtr)[i] = frontlerp * ((float *)frame->bones)[i]
00118                 + backlerp * ((float *)oldFrame->bones)[i];
00119         }
00120     }
00121 
00122     //
00123     // deform the vertexes by the lerped bones
00124     //
00125     numVerts = surface->numVerts;
00126     // FIXME
00127     // This makes TFC's skeletons work.  Shouldn't be necessary anymore, but left
00128     // in for reference.
00129     //v = (md4Vertex_t *) ((byte *)surface + surface->ofsVerts + 12);
00130     v = (md4Vertex_t *) ((byte *)surface + surface->ofsVerts);
00131     for ( j = 0; j < numVerts; j++ ) {
00132         vec3_t  tempVert, tempNormal;
00133         md4Weight_t *w;
00134 
00135         VectorClear( tempVert );
00136         VectorClear( tempNormal );
00137         w = v->weights;
00138         for ( k = 0 ; k < v->numWeights ; k++, w++ ) {
00139             bone = bonePtr + w->boneIndex;
00140 
00141             tempVert[0] += w->boneWeight * ( DotProduct( bone->matrix[0], w->offset ) + bone->matrix[0][3] );
00142             tempVert[1] += w->boneWeight * ( DotProduct( bone->matrix[1], w->offset ) + bone->matrix[1][3] );
00143             tempVert[2] += w->boneWeight * ( DotProduct( bone->matrix[2], w->offset ) + bone->matrix[2][3] );
00144 
00145             tempNormal[0] += w->boneWeight * DotProduct( bone->matrix[0], v->normal );
00146             tempNormal[1] += w->boneWeight * DotProduct( bone->matrix[1], v->normal );
00147             tempNormal[2] += w->boneWeight * DotProduct( bone->matrix[2], v->normal );
00148         }
00149 
00150         tess.xyz[baseVertex + j][0] = tempVert[0];
00151         tess.xyz[baseVertex + j][1] = tempVert[1];
00152         tess.xyz[baseVertex + j][2] = tempVert[2];
00153 
00154         tess.normal[baseVertex + j][0] = tempNormal[0];
00155         tess.normal[baseVertex + j][1] = tempNormal[1];
00156         tess.normal[baseVertex + j][2] = tempNormal[2];
00157 
00158         tess.texCoords[baseVertex + j][0][0] = v->texCoords[0];
00159         tess.texCoords[baseVertex + j][0][1] = v->texCoords[1];
00160 
00161         // FIXME
00162         // This makes TFC's skeletons work.  Shouldn't be necessary anymore, but left
00163         // in for reference.
00164         //v = (md4Vertex_t *)( ( byte * )&v->weights[v->numWeights] + 12 );
00165         v = (md4Vertex_t *)&v->weights[v->numWeights];
00166     }
00167 
00168     tess.numVertexes += surface->numVerts;
00169 }
00170 
00171 

Generated on Thu Aug 25 12:37:50 2005 for Quake III Arena by  doxygen 1.3.9.1