misc_model.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "qbsp.h"
  19. #include "aselib.h"
  20. #ifdef _WIN32
  21. #ifdef _TTIMOBUILD
  22. #include "pakstuff.h"
  23. #else
  24. #include "../libs/pakstuff.h"
  25. #endif
  26. #endif
  27. typedef struct {
  28. char modelName[1024];
  29. md3Header_t *header;
  30. } loadedModel_t;
  31. int c_triangleModels;
  32. int c_triangleSurfaces;
  33. int c_triangleVertexes;
  34. int c_triangleIndexes;
  35. #define MAX_LOADED_MODELS 1024
  36. loadedModel_t loadedModels[MAX_LOADED_MODELS];
  37. int numLoadedModels;
  38. /*
  39. =================
  40. R_LoadMD3
  41. =================
  42. */
  43. #define LL(x) x=LittleLong(x)
  44. md3Header_t *R_LoadMD3( const char *mod_name ) {
  45. int i, j;
  46. md3Header_t *md3;
  47. md3Frame_t *frame;
  48. md3Surface_t *surf;
  49. md3Triangle_t *tri;
  50. md3St_t *st;
  51. md3XyzNormal_t *xyz;
  52. int version;
  53. char filename[1024];
  54. int len;
  55. sprintf( filename, "%s%s", gamedir, mod_name );
  56. len = TryLoadFile( filename, (void **)&md3 );
  57. #ifdef _WIN32
  58. if ( len <= 0 ) {
  59. len = PakLoadAnyFile(filename, (void **)&md3);
  60. }
  61. #endif
  62. if ( len <= 0 ) {
  63. return NULL;
  64. }
  65. version = LittleLong (md3->version);
  66. if (version != MD3_VERSION) {
  67. _printf( "R_LoadMD3: %s has wrong version (%i should be %i)\n",
  68. mod_name, version, MD3_VERSION);
  69. return NULL;
  70. }
  71. LL(md3->ident);
  72. LL(md3->version);
  73. LL(md3->numFrames);
  74. LL(md3->numTags);
  75. LL(md3->numSurfaces);
  76. LL(md3->numSkins);
  77. LL(md3->ofsFrames);
  78. LL(md3->ofsTags);
  79. LL(md3->ofsSurfaces);
  80. LL(md3->ofsEnd);
  81. if ( md3->numFrames < 1 ) {
  82. _printf( "R_LoadMD3: %s has no frames\n", mod_name );
  83. return NULL;
  84. }
  85. // we don't need to swap tags in the renderer, they aren't used
  86. // swap all the frames
  87. frame = (md3Frame_t *) ( (byte *)md3 + md3->ofsFrames );
  88. for ( i = 0 ; i < md3->numFrames ; i++, frame++) {
  89. frame->radius = LittleFloat( frame->radius );
  90. for ( j = 0 ; j < 3 ; j++ ) {
  91. frame->bounds[0][j] = LittleFloat( frame->bounds[0][j] );
  92. frame->bounds[1][j] = LittleFloat( frame->bounds[1][j] );
  93. frame->localOrigin[j] = LittleFloat( frame->localOrigin[j] );
  94. }
  95. }
  96. // swap all the surfaces
  97. surf = (md3Surface_t *) ( (byte *)md3 + md3->ofsSurfaces );
  98. for ( i = 0 ; i < md3->numSurfaces ; i++) {
  99. LL(surf->ident);
  100. LL(surf->flags);
  101. LL(surf->numFrames);
  102. LL(surf->numShaders);
  103. LL(surf->numTriangles);
  104. LL(surf->ofsTriangles);
  105. LL(surf->numVerts);
  106. LL(surf->ofsShaders);
  107. LL(surf->ofsSt);
  108. LL(surf->ofsXyzNormals);
  109. LL(surf->ofsEnd);
  110. if ( surf->numVerts > SHADER_MAX_VERTEXES ) {
  111. Error ("R_LoadMD3: %s has more than %i verts on a surface (%i)",
  112. mod_name, SHADER_MAX_VERTEXES, surf->numVerts );
  113. }
  114. if ( surf->numTriangles*3 > SHADER_MAX_INDEXES ) {
  115. Error ("R_LoadMD3: %s has more than %i triangles on a surface (%i)",
  116. mod_name, SHADER_MAX_INDEXES / 3, surf->numTriangles );
  117. }
  118. // swap all the triangles
  119. tri = (md3Triangle_t *) ( (byte *)surf + surf->ofsTriangles );
  120. for ( j = 0 ; j < surf->numTriangles ; j++, tri++ ) {
  121. LL(tri->indexes[0]);
  122. LL(tri->indexes[1]);
  123. LL(tri->indexes[2]);
  124. }
  125. // swap all the ST
  126. st = (md3St_t *) ( (byte *)surf + surf->ofsSt );
  127. for ( j = 0 ; j < surf->numVerts ; j++, st++ ) {
  128. st->st[0] = LittleFloat( st->st[0] );
  129. st->st[1] = LittleFloat( st->st[1] );
  130. }
  131. // swap all the XyzNormals
  132. xyz = (md3XyzNormal_t *) ( (byte *)surf + surf->ofsXyzNormals );
  133. for ( j = 0 ; j < surf->numVerts * surf->numFrames ; j++, xyz++ )
  134. {
  135. xyz->xyz[0] = LittleShort( xyz->xyz[0] );
  136. xyz->xyz[1] = LittleShort( xyz->xyz[1] );
  137. xyz->xyz[2] = LittleShort( xyz->xyz[2] );
  138. xyz->normal = LittleShort( xyz->normal );
  139. }
  140. // find the next surface
  141. surf = (md3Surface_t *)( (byte *)surf + surf->ofsEnd );
  142. }
  143. return md3;
  144. }
  145. /*
  146. ================
  147. LoadModel
  148. ================
  149. */
  150. md3Header_t *LoadModel( const char *modelName ) {
  151. int i;
  152. loadedModel_t *lm;
  153. // see if we already have it loaded
  154. for ( i = 0, lm = loadedModels ; i < numLoadedModels ; i++, lm++ ) {
  155. if ( !strcmp( modelName, lm->modelName ) ) {
  156. return lm->header;
  157. }
  158. }
  159. // load it
  160. if ( numLoadedModels == MAX_LOADED_MODELS ) {
  161. Error( "MAX_LOADED_MODELS" );
  162. }
  163. numLoadedModels++;
  164. strcpy( lm->modelName, modelName );
  165. lm->header = R_LoadMD3( modelName );
  166. return lm->header;
  167. }
  168. /*
  169. ============
  170. InsertMD3Model
  171. Convert a model entity to raw geometry surfaces and insert it in the tree
  172. ============
  173. */
  174. void InsertMD3Model( const char *modelName, vec3_t origin, float angle, tree_t *tree ) {
  175. int i, j;
  176. md3Header_t *md3;
  177. md3Surface_t *surf;
  178. md3Shader_t *shader;
  179. md3Triangle_t *tri;
  180. md3St_t *st;
  181. md3XyzNormal_t *xyz;
  182. drawVert_t *outv;
  183. float lat, lng;
  184. float angleCos, angleSin;
  185. mapDrawSurface_t *out;
  186. vec3_t temp;
  187. angle = angle / 180 * Q_PI;
  188. angleCos = cos( angle );
  189. angleSin = sin( angle );
  190. // load the model
  191. md3 = LoadModel( modelName );
  192. if ( !md3 ) {
  193. return;
  194. }
  195. // each md3 surface will become a new bsp surface
  196. c_triangleModels++;
  197. c_triangleSurfaces += md3->numSurfaces;
  198. // expand, translate, and rotate the vertexes
  199. // swap all the surfaces
  200. surf = (md3Surface_t *) ( (byte *)md3 + md3->ofsSurfaces );
  201. for ( i = 0 ; i < md3->numSurfaces ; i++) {
  202. // allocate a surface
  203. out = AllocDrawSurf();
  204. out->miscModel = qtrue;
  205. shader = (md3Shader_t *) ( (byte *)surf + surf->ofsShaders );
  206. out->shaderInfo = ShaderInfoForShader( shader->name );
  207. out->numVerts = surf->numVerts;
  208. out->verts = malloc( out->numVerts * sizeof( out->verts[0] ) );
  209. out->numIndexes = surf->numTriangles * 3;
  210. out->indexes = malloc( out->numIndexes * sizeof( out->indexes[0] ) );
  211. out->lightmapNum = -1;
  212. out->fogNum = -1;
  213. // emit the indexes
  214. c_triangleIndexes += surf->numTriangles * 3;
  215. tri = (md3Triangle_t *) ( (byte *)surf + surf->ofsTriangles );
  216. for ( j = 0 ; j < surf->numTriangles ; j++, tri++ ) {
  217. out->indexes[j*3+0] = tri->indexes[0];
  218. out->indexes[j*3+1] = tri->indexes[1];
  219. out->indexes[j*3+2] = tri->indexes[2];
  220. }
  221. // emit the vertexes
  222. st = (md3St_t *) ( (byte *)surf + surf->ofsSt );
  223. xyz = (md3XyzNormal_t *) ( (byte *)surf + surf->ofsXyzNormals );
  224. c_triangleVertexes += surf->numVerts;
  225. for ( j = 0 ; j < surf->numVerts ; j++, st++, xyz++ ) {
  226. outv = &out->verts[ j ];
  227. outv->st[0] = st->st[0];
  228. outv->st[1] = st->st[1];
  229. outv->lightmap[0] = 0;
  230. outv->lightmap[1] = 0;
  231. // the colors will be set by the lighting pass
  232. outv->color[0] = 255;
  233. outv->color[1] = 255;
  234. outv->color[2] = 255;
  235. outv->color[3] = 255;
  236. outv->xyz[0] = origin[0] + MD3_XYZ_SCALE * ( xyz->xyz[0] * angleCos - xyz->xyz[1] * angleSin );
  237. outv->xyz[1] = origin[1] + MD3_XYZ_SCALE * ( xyz->xyz[0] * angleSin + xyz->xyz[1] * angleCos );
  238. outv->xyz[2] = origin[2] + MD3_XYZ_SCALE * ( xyz->xyz[2] );
  239. // decode the lat/lng normal to a 3 float normal
  240. lat = ( xyz->normal >> 8 ) & 0xff;
  241. lng = ( xyz->normal & 0xff );
  242. lat *= Q_PI/128;
  243. lng *= Q_PI/128;
  244. temp[0] = cos(lat) * sin(lng);
  245. temp[1] = sin(lat) * sin(lng);
  246. temp[2] = cos(lng);
  247. // rotate the normal
  248. outv->normal[0] = temp[0] * angleCos - temp[1] * angleSin;
  249. outv->normal[1] = temp[0] * angleSin + temp[1] * angleCos;
  250. outv->normal[2] = temp[2];
  251. }
  252. // find the next surface
  253. surf = (md3Surface_t *)( (byte *)surf + surf->ofsEnd );
  254. }
  255. }
  256. //==============================================================================
  257. /*
  258. ============
  259. InsertASEModel
  260. Convert a model entity to raw geometry surfaces and insert it in the tree
  261. ============
  262. */
  263. void InsertASEModel( const char *modelName, vec3_t origin, float angle, tree_t *tree ) {
  264. int i, j;
  265. drawVert_t *outv;
  266. float angleCos, angleSin;
  267. mapDrawSurface_t *out;
  268. int numSurfaces;
  269. const char *name;
  270. polyset_t *pset;
  271. int numFrames;
  272. char filename[1024];
  273. sprintf( filename, "%s%s", gamedir, modelName );
  274. angle = angle / 180 * Q_PI;
  275. angleCos = cos( angle );
  276. angleSin = sin( angle );
  277. // load the model
  278. ASE_Load( filename, qfalse, qfalse );
  279. // each ase surface will become a new bsp surface
  280. numSurfaces = ASE_GetNumSurfaces();
  281. c_triangleModels++;
  282. c_triangleSurfaces += numSurfaces;
  283. // expand, translate, and rotate the vertexes
  284. // swap all the surfaces
  285. for ( i = 0 ; i < numSurfaces ; i++) {
  286. name = ASE_GetSurfaceName( i );
  287. pset = ASE_GetSurfaceAnimation( i, &numFrames, -1, -1, -1 );
  288. if ( !name || !pset ) {
  289. continue;
  290. }
  291. // allocate a surface
  292. out = AllocDrawSurf();
  293. out->miscModel = qtrue;
  294. out->shaderInfo = ShaderInfoForShader( pset->materialname );
  295. out->numVerts = 3 * pset->numtriangles;
  296. out->verts = malloc( out->numVerts * sizeof( out->verts[0] ) );
  297. out->numIndexes = 3 * pset->numtriangles;
  298. out->indexes = malloc( out->numIndexes * sizeof( out->indexes[0] ) );
  299. out->lightmapNum = -1;
  300. out->fogNum = -1;
  301. // emit the indexes
  302. c_triangleIndexes += out->numIndexes;
  303. for ( j = 0 ; j < out->numIndexes ; j++ ) {
  304. out->indexes[j] = j;
  305. }
  306. // emit the vertexes
  307. c_triangleVertexes += out->numVerts;
  308. for ( j = 0 ; j < out->numVerts ; j++ ) {
  309. int index;
  310. triangle_t *tri;
  311. index = j % 3;
  312. tri = &pset->triangles[ j / 3 ];
  313. outv = &out->verts[ j ];
  314. outv->st[0] = tri->texcoords[index][0];
  315. outv->st[1] = tri->texcoords[index][1];
  316. outv->lightmap[0] = 0;
  317. outv->lightmap[1] = 0;
  318. // the colors will be set by the lighting pass
  319. outv->color[0] = 255;
  320. outv->color[1] = 255;
  321. outv->color[2] = 255;
  322. outv->color[3] = 255;
  323. outv->xyz[0] = origin[0] + tri->verts[index][0];
  324. outv->xyz[1] = origin[1] + tri->verts[index][1];
  325. outv->xyz[2] = origin[2] + tri->verts[index][2];
  326. // rotate the normal
  327. outv->normal[0] = tri->normals[index][0];
  328. outv->normal[1] = tri->normals[index][1];
  329. outv->normal[2] = tri->normals[index][2];
  330. }
  331. }
  332. }
  333. //==============================================================================
  334. /*
  335. =====================
  336. AddTriangleModels
  337. =====================
  338. */
  339. void AddTriangleModels( tree_t *tree ) {
  340. int entity_num;
  341. entity_t *entity;
  342. qprintf("----- AddTriangleModels -----\n");
  343. for ( entity_num=1 ; entity_num< num_entities ; entity_num++ ) {
  344. entity = &entities[entity_num];
  345. // convert misc_models into raw geometry
  346. if ( !Q_stricmp( "misc_model", ValueForKey( entity, "classname" ) ) ) {
  347. const char *model;
  348. vec3_t origin;
  349. float angle;
  350. // get the angle for rotation FIXME: support full matrix positioning
  351. angle = FloatForKey( entity, "angle" );
  352. GetVectorForKey( entity, "origin", origin );
  353. model = ValueForKey( entity, "model" );
  354. if ( !model[0] ) {
  355. _printf("WARNING: misc_model at %i %i %i without a model key\n", (int)origin[0],
  356. (int)origin[1], (int)origin[2] );
  357. continue;
  358. }
  359. if ( strstr( model, ".md3" ) || strstr( model, ".MD3" ) ) {
  360. InsertMD3Model( model, origin, angle, tree );
  361. continue;
  362. }
  363. if ( strstr( model, ".ase" ) || strstr( model, ".ASE" ) ) {
  364. InsertASEModel( model, origin, angle, tree );
  365. continue;
  366. }
  367. _printf( "Unknown misc_model type: %s\n", model );
  368. continue;
  369. }
  370. }
  371. qprintf( "%5i triangle models\n", c_triangleModels );
  372. qprintf( "%5i triangle surfaces\n", c_triangleSurfaces );
  373. qprintf( "%5i triangle vertexes\n", c_triangleVertexes );
  374. qprintf( "%5i triangle indexes\n", c_triangleIndexes );
  375. }