Model_md3.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "tr_local.h"
  23. #include "Model_local.h"
  24. #include "Model_md3.h"
  25. /***********************************************************************
  26. idMD3Mesh
  27. ***********************************************************************/
  28. #define LL(x) x=LittleLong(x)
  29. /*
  30. =================
  31. idRenderModelMD3::InitFromFile
  32. =================
  33. */
  34. void idRenderModelMD3::InitFromFile( const char *fileName ) {
  35. int i, j;
  36. md3Header_t *pinmodel;
  37. md3Frame_t *frame;
  38. md3Surface_t *surf;
  39. md3Shader_t *shader;
  40. md3Triangle_t *tri;
  41. md3St_t *st;
  42. md3XyzNormal_t *xyz;
  43. md3Tag_t *tag;
  44. void *buffer;
  45. int version;
  46. int size;
  47. name = fileName;
  48. size = fileSystem->ReadFile( fileName, &buffer, NULL );
  49. if (!size || size<0 ) {
  50. return;
  51. }
  52. pinmodel = (md3Header_t *)buffer;
  53. version = LittleLong (pinmodel->version);
  54. if (version != MD3_VERSION) {
  55. fileSystem->FreeFile( buffer );
  56. common->Warning( "InitFromFile: %s has wrong version (%i should be %i)",
  57. fileName, version, MD3_VERSION);
  58. return;
  59. }
  60. size = LittleLong(pinmodel->ofsEnd);
  61. dataSize += size;
  62. md3 = (md3Header_t *)Mem_Alloc( size );
  63. memcpy (md3, buffer, LittleLong(pinmodel->ofsEnd) );
  64. LL(md3->ident);
  65. LL(md3->version);
  66. LL(md3->numFrames);
  67. LL(md3->numTags);
  68. LL(md3->numSurfaces);
  69. LL(md3->ofsFrames);
  70. LL(md3->ofsTags);
  71. LL(md3->ofsSurfaces);
  72. LL(md3->ofsEnd);
  73. if ( md3->numFrames < 1 ) {
  74. common->Warning( "InitFromFile: %s has no frames", fileName );
  75. fileSystem->FreeFile( buffer );
  76. return;
  77. }
  78. // swap all the frames
  79. frame = (md3Frame_t *) ( (byte *)md3 + md3->ofsFrames );
  80. for ( i = 0 ; i < md3->numFrames ; i++, frame++) {
  81. frame->radius = LittleFloat( frame->radius );
  82. for ( j = 0 ; j < 3 ; j++ ) {
  83. frame->bounds[0][j] = LittleFloat( frame->bounds[0][j] );
  84. frame->bounds[1][j] = LittleFloat( frame->bounds[1][j] );
  85. frame->localOrigin[j] = LittleFloat( frame->localOrigin[j] );
  86. }
  87. }
  88. // swap all the tags
  89. tag = (md3Tag_t *) ( (byte *)md3 + md3->ofsTags );
  90. for ( i = 0 ; i < md3->numTags * md3->numFrames ; i++, tag++) {
  91. for ( j = 0 ; j < 3 ; j++ ) {
  92. tag->origin[j] = LittleFloat( tag->origin[j] );
  93. tag->axis[0][j] = LittleFloat( tag->axis[0][j] );
  94. tag->axis[1][j] = LittleFloat( tag->axis[1][j] );
  95. tag->axis[2][j] = LittleFloat( tag->axis[2][j] );
  96. }
  97. }
  98. // swap all the surfaces
  99. surf = (md3Surface_t *) ( (byte *)md3 + md3->ofsSurfaces );
  100. for ( i = 0 ; i < md3->numSurfaces ; i++) {
  101. LL(surf->ident);
  102. LL(surf->flags);
  103. LL(surf->numFrames);
  104. LL(surf->numShaders);
  105. LL(surf->numTriangles);
  106. LL(surf->ofsTriangles);
  107. LL(surf->numVerts);
  108. LL(surf->ofsShaders);
  109. LL(surf->ofsSt);
  110. LL(surf->ofsXyzNormals);
  111. LL(surf->ofsEnd);
  112. if ( surf->numVerts > SHADER_MAX_VERTEXES ) {
  113. common->Error( "InitFromFile: %s has more than %i verts on a surface (%i)",
  114. fileName, SHADER_MAX_VERTEXES, surf->numVerts );
  115. }
  116. if ( surf->numTriangles*3 > SHADER_MAX_INDEXES ) {
  117. common->Error( "InitFromFile: %s has more than %i triangles on a surface (%i)",
  118. fileName, SHADER_MAX_INDEXES / 3, surf->numTriangles );
  119. }
  120. // change to surface identifier
  121. surf->ident = 0; //SF_MD3;
  122. // lowercase the surface name so skin compares are faster
  123. int slen = (int)strlen( surf->name );
  124. for( j = 0; j < slen; j++ ) {
  125. surf->name[j] = tolower( surf->name[j] );
  126. }
  127. // strip off a trailing _1 or _2
  128. // this is a crutch for q3data being a mess
  129. j = strlen( surf->name );
  130. if ( j > 2 && surf->name[j-2] == '_' ) {
  131. surf->name[j-2] = 0;
  132. }
  133. // register the shaders
  134. shader = (md3Shader_t *) ( (byte *)surf + surf->ofsShaders );
  135. for ( j = 0 ; j < surf->numShaders ; j++, shader++ ) {
  136. const idMaterial *sh;
  137. sh = declManager->FindMaterial( shader->name );
  138. shader->shader = sh;
  139. }
  140. // swap all the triangles
  141. tri = (md3Triangle_t *) ( (byte *)surf + surf->ofsTriangles );
  142. for ( j = 0 ; j < surf->numTriangles ; j++, tri++ ) {
  143. LL(tri->indexes[0]);
  144. LL(tri->indexes[1]);
  145. LL(tri->indexes[2]);
  146. }
  147. // swap all the ST
  148. st = (md3St_t *) ( (byte *)surf + surf->ofsSt );
  149. for ( j = 0 ; j < surf->numVerts ; j++, st++ ) {
  150. st->st[0] = LittleFloat( st->st[0] );
  151. st->st[1] = LittleFloat( st->st[1] );
  152. }
  153. // swap all the XyzNormals
  154. xyz = (md3XyzNormal_t *) ( (byte *)surf + surf->ofsXyzNormals );
  155. for ( j = 0 ; j < surf->numVerts * surf->numFrames ; j++, xyz++ )
  156. {
  157. xyz->xyz[0] = LittleShort( xyz->xyz[0] );
  158. xyz->xyz[1] = LittleShort( xyz->xyz[1] );
  159. xyz->xyz[2] = LittleShort( xyz->xyz[2] );
  160. xyz->normal = LittleShort( xyz->normal );
  161. }
  162. // find the next surface
  163. surf = (md3Surface_t *)( (byte *)surf + surf->ofsEnd );
  164. }
  165. fileSystem->FreeFile( buffer );
  166. }
  167. /*
  168. =================
  169. idRenderModelMD3::IsDynamicModel
  170. =================
  171. */
  172. dynamicModel_t idRenderModelMD3::IsDynamicModel() const {
  173. return DM_CACHED;
  174. }
  175. /*
  176. =================
  177. idRenderModelMD3::LerpMeshVertexes
  178. =================
  179. */
  180. void idRenderModelMD3::LerpMeshVertexes ( srfTriangles_t *tri, const struct md3Surface_s *surf, const float backlerp, const int frame, const int oldframe ) const {
  181. short *oldXyz, *newXyz;
  182. float oldXyzScale, newXyzScale;
  183. int vertNum;
  184. int numVerts;
  185. newXyz = (short *)((byte *)surf + surf->ofsXyzNormals) + (frame * surf->numVerts * 4);
  186. newXyzScale = MD3_XYZ_SCALE * (1.0 - backlerp);
  187. numVerts = surf->numVerts;
  188. if ( backlerp == 0 ) {
  189. //
  190. // just copy the vertexes
  191. //
  192. for (vertNum=0 ; vertNum < numVerts ; vertNum++, newXyz += 4 ) {
  193. idDrawVert *outvert = &tri->verts[tri->numVerts];
  194. outvert->xyz.x = newXyz[0] * newXyzScale;
  195. outvert->xyz.y = newXyz[1] * newXyzScale;
  196. outvert->xyz.z = newXyz[2] * newXyzScale;
  197. tri->numVerts++;
  198. }
  199. } else {
  200. //
  201. // interpolate and copy the vertexes
  202. //
  203. oldXyz = (short *)((byte *)surf + surf->ofsXyzNormals) + (oldframe * surf->numVerts * 4);
  204. oldXyzScale = MD3_XYZ_SCALE * backlerp;
  205. for (vertNum=0 ; vertNum < numVerts ; vertNum++, oldXyz += 4, newXyz += 4 ) {
  206. idDrawVert *outvert = &tri->verts[tri->numVerts];
  207. // interpolate the xyz
  208. outvert->xyz.x = oldXyz[0] * oldXyzScale + newXyz[0] * newXyzScale;
  209. outvert->xyz.y = oldXyz[1] * oldXyzScale + newXyz[1] * newXyzScale;
  210. outvert->xyz.z = oldXyz[2] * oldXyzScale + newXyz[2] * newXyzScale;
  211. tri->numVerts++;
  212. }
  213. }
  214. }
  215. /*
  216. =============
  217. idRenderModelMD3::InstantiateDynamicModel
  218. =============
  219. */
  220. idRenderModel *idRenderModelMD3::InstantiateDynamicModel( const struct renderEntity_s *ent, const struct viewDef_s *view, idRenderModel *cachedModel ) {
  221. int i, j;
  222. float backlerp;
  223. int * triangles;
  224. float * texCoords;
  225. int indexes;
  226. int numVerts;
  227. md3Surface_t * surface;
  228. int frame, oldframe;
  229. idRenderModelStatic *staticModel;
  230. if ( cachedModel ) {
  231. delete cachedModel;
  232. cachedModel = NULL;
  233. }
  234. staticModel = new idRenderModelStatic;
  235. staticModel->bounds.Clear();
  236. surface = (md3Surface_t *) ((byte *)md3 + md3->ofsSurfaces);
  237. // TODO: these need set by an entity
  238. frame = ent->shaderParms[SHADERPARM_MD3_FRAME]; // probably want to keep frames < 1000 or so
  239. oldframe = ent->shaderParms[SHADERPARM_MD3_LASTFRAME];
  240. backlerp = ent->shaderParms[SHADERPARM_MD3_BACKLERP];
  241. for( i = 0; i < md3->numSurfaces; i++ ) {
  242. srfTriangles_t *tri = R_AllocStaticTriSurf();
  243. R_AllocStaticTriSurfVerts( tri, surface->numVerts );
  244. R_AllocStaticTriSurfIndexes( tri, surface->numTriangles * 3 );
  245. tri->bounds.Clear();
  246. modelSurface_t surf;
  247. surf.geometry = tri;
  248. md3Shader_t* shaders = (md3Shader_t *) ((byte *)surface + surface->ofsShaders);
  249. surf.shader = shaders->shader;
  250. LerpMeshVertexes( tri, surface, backlerp, frame, oldframe );
  251. triangles = (int *) ((byte *)surface + surface->ofsTriangles);
  252. indexes = surface->numTriangles * 3;
  253. for (j = 0 ; j < indexes ; j++) {
  254. tri->indexes[j] = triangles[j];
  255. }
  256. tri->numIndexes += indexes;
  257. texCoords = (float *) ((byte *)surface + surface->ofsSt);
  258. numVerts = surface->numVerts;
  259. for ( j = 0; j < numVerts; j++ ) {
  260. idDrawVert *stri = &tri->verts[j];
  261. stri->st[0] = texCoords[j*2+0];
  262. stri->st[1] = texCoords[j*2+1];
  263. }
  264. R_BoundTriSurf( tri );
  265. staticModel->AddSurface( surf );
  266. staticModel->bounds.AddPoint( surf.geometry->bounds[0] );
  267. staticModel->bounds.AddPoint( surf.geometry->bounds[1] );
  268. // find the next surface
  269. surface = (md3Surface_t *)( (byte *)surface + surface->ofsEnd );
  270. }
  271. return staticModel;
  272. }
  273. /*
  274. =====================
  275. idRenderModelMD3::Bounds
  276. =====================
  277. */
  278. idBounds idRenderModelMD3::Bounds(const struct renderEntity_s *ent) const {
  279. idBounds ret;
  280. ret.Clear();
  281. if (!ent || !md3) {
  282. // just give it the editor bounds
  283. ret.AddPoint(idVec3(-10,-10,-10));
  284. ret.AddPoint(idVec3( 10, 10, 10));
  285. return ret;
  286. }
  287. md3Frame_t *frame = (md3Frame_t *)( (byte *)md3 + md3->ofsFrames );
  288. ret.AddPoint( frame->bounds[0] );
  289. ret.AddPoint( frame->bounds[1] );
  290. return ret;
  291. }