tr_mesh.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. // tr_mesh.c: triangle model functions
  19. #include "tr_local.h"
  20. static float ProjectRadius( float r, vec3_t location )
  21. {
  22. float pr;
  23. float dist;
  24. float c;
  25. vec3_t p;
  26. float projected[4];
  27. c = DotProduct( tr.viewParms.or.axis[0], tr.viewParms.or.origin );
  28. dist = DotProduct( tr.viewParms.or.axis[0], location ) - c;
  29. if ( dist <= 0 )
  30. return 0;
  31. p[0] = 0;
  32. p[1] = fabs( r );
  33. p[2] = -dist;
  34. projected[0] = p[0] * tr.viewParms.projectionMatrix[0] +
  35. p[1] * tr.viewParms.projectionMatrix[4] +
  36. p[2] * tr.viewParms.projectionMatrix[8] +
  37. tr.viewParms.projectionMatrix[12];
  38. projected[1] = p[0] * tr.viewParms.projectionMatrix[1] +
  39. p[1] * tr.viewParms.projectionMatrix[5] +
  40. p[2] * tr.viewParms.projectionMatrix[9] +
  41. tr.viewParms.projectionMatrix[13];
  42. projected[2] = p[0] * tr.viewParms.projectionMatrix[2] +
  43. p[1] * tr.viewParms.projectionMatrix[6] +
  44. p[2] * tr.viewParms.projectionMatrix[10] +
  45. tr.viewParms.projectionMatrix[14];
  46. projected[3] = p[0] * tr.viewParms.projectionMatrix[3] +
  47. p[1] * tr.viewParms.projectionMatrix[7] +
  48. p[2] * tr.viewParms.projectionMatrix[11] +
  49. tr.viewParms.projectionMatrix[15];
  50. pr = projected[1] / projected[3];
  51. if ( pr > 1.0f )
  52. pr = 1.0f;
  53. return pr;
  54. }
  55. /*
  56. =============
  57. R_CullModel
  58. =============
  59. */
  60. static int R_CullModel( md3Header_t *header, trRefEntity_t *ent ) {
  61. vec3_t bounds[2];
  62. md3Frame_t *oldFrame, *newFrame;
  63. int i;
  64. // compute frame pointers
  65. newFrame = ( md3Frame_t * ) ( ( byte * ) header + header->ofsFrames ) + ent->e.frame;
  66. oldFrame = ( md3Frame_t * ) ( ( byte * ) header + header->ofsFrames ) + ent->e.oldframe;
  67. // cull bounding sphere ONLY if this is not an upscaled entity
  68. if ( !ent->e.nonNormalizedAxes )
  69. {
  70. if ( ent->e.frame == ent->e.oldframe )
  71. {
  72. switch ( R_CullLocalPointAndRadius( newFrame->localOrigin, newFrame->radius ) )
  73. {
  74. case CULL_OUT:
  75. tr.pc.c_sphere_cull_md3_out++;
  76. return CULL_OUT;
  77. case CULL_IN:
  78. tr.pc.c_sphere_cull_md3_in++;
  79. return CULL_IN;
  80. case CULL_CLIP:
  81. tr.pc.c_sphere_cull_md3_clip++;
  82. break;
  83. }
  84. }
  85. else
  86. {
  87. int sphereCull, sphereCullB;
  88. sphereCull = R_CullLocalPointAndRadius( newFrame->localOrigin, newFrame->radius );
  89. if ( newFrame == oldFrame ) {
  90. sphereCullB = sphereCull;
  91. } else {
  92. sphereCullB = R_CullLocalPointAndRadius( oldFrame->localOrigin, oldFrame->radius );
  93. }
  94. if ( sphereCull == sphereCullB )
  95. {
  96. if ( sphereCull == CULL_OUT )
  97. {
  98. tr.pc.c_sphere_cull_md3_out++;
  99. return CULL_OUT;
  100. }
  101. else if ( sphereCull == CULL_IN )
  102. {
  103. tr.pc.c_sphere_cull_md3_in++;
  104. return CULL_IN;
  105. }
  106. else
  107. {
  108. tr.pc.c_sphere_cull_md3_clip++;
  109. }
  110. }
  111. }
  112. }
  113. // calculate a bounding box in the current coordinate system
  114. for (i = 0 ; i < 3 ; i++) {
  115. bounds[0][i] = oldFrame->bounds[0][i] < newFrame->bounds[0][i] ? oldFrame->bounds[0][i] : newFrame->bounds[0][i];
  116. bounds[1][i] = oldFrame->bounds[1][i] > newFrame->bounds[1][i] ? oldFrame->bounds[1][i] : newFrame->bounds[1][i];
  117. }
  118. switch ( R_CullLocalBox( bounds ) )
  119. {
  120. case CULL_IN:
  121. tr.pc.c_box_cull_md3_in++;
  122. return CULL_IN;
  123. case CULL_CLIP:
  124. tr.pc.c_box_cull_md3_clip++;
  125. return CULL_CLIP;
  126. case CULL_OUT:
  127. default:
  128. tr.pc.c_box_cull_md3_out++;
  129. return CULL_OUT;
  130. }
  131. }
  132. /*
  133. =================
  134. R_ComputeLOD
  135. =================
  136. */
  137. int R_ComputeLOD( trRefEntity_t *ent ) {
  138. float radius;
  139. float flod, lodscale;
  140. float projectedRadius;
  141. md3Frame_t *frame;
  142. int lod;
  143. if ( tr.currentModel->numLods < 2 )
  144. {
  145. // model has only 1 LOD level, skip computations and bias
  146. lod = 0;
  147. }
  148. else
  149. {
  150. // multiple LODs exist, so compute projected bounding sphere
  151. // and use that as a criteria for selecting LOD
  152. frame = ( md3Frame_t * ) ( ( ( unsigned char * ) tr.currentModel->md3[0] ) + tr.currentModel->md3[0]->ofsFrames );
  153. frame += ent->e.frame;
  154. radius = RadiusFromBounds( frame->bounds[0], frame->bounds[1] );
  155. if ( ( projectedRadius = ProjectRadius( radius, ent->e.origin ) ) != 0 )
  156. {
  157. lodscale = r_lodscale->value;
  158. if (lodscale > 20) lodscale = 20;
  159. flod = 1.0f - projectedRadius * lodscale;
  160. }
  161. else
  162. {
  163. // object intersects near view plane, e.g. view weapon
  164. flod = 0;
  165. }
  166. flod *= tr.currentModel->numLods;
  167. lod = myftol( flod );
  168. if ( lod < 0 )
  169. {
  170. lod = 0;
  171. }
  172. else if ( lod >= tr.currentModel->numLods )
  173. {
  174. lod = tr.currentModel->numLods - 1;
  175. }
  176. }
  177. lod += r_lodbias->integer;
  178. if ( lod >= tr.currentModel->numLods )
  179. lod = tr.currentModel->numLods - 1;
  180. if ( lod < 0 )
  181. lod = 0;
  182. return lod;
  183. }
  184. /*
  185. =================
  186. R_ComputeFogNum
  187. =================
  188. */
  189. int R_ComputeFogNum( md3Header_t *header, trRefEntity_t *ent ) {
  190. int i, j;
  191. fog_t *fog;
  192. md3Frame_t *md3Frame;
  193. vec3_t localOrigin;
  194. if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) {
  195. return 0;
  196. }
  197. // FIXME: non-normalized axis issues
  198. md3Frame = ( md3Frame_t * ) ( ( byte * ) header + header->ofsFrames ) + ent->e.frame;
  199. VectorAdd( ent->e.origin, md3Frame->localOrigin, localOrigin );
  200. for ( i = 1 ; i < tr.world->numfogs ; i++ ) {
  201. fog = &tr.world->fogs[i];
  202. for ( j = 0 ; j < 3 ; j++ ) {
  203. if ( localOrigin[j] - md3Frame->radius >= fog->bounds[1][j] ) {
  204. break;
  205. }
  206. if ( localOrigin[j] + md3Frame->radius <= fog->bounds[0][j] ) {
  207. break;
  208. }
  209. }
  210. if ( j == 3 ) {
  211. return i;
  212. }
  213. }
  214. return 0;
  215. }
  216. /*
  217. =================
  218. R_AddMD3Surfaces
  219. =================
  220. */
  221. void R_AddMD3Surfaces( trRefEntity_t *ent ) {
  222. int i;
  223. md3Header_t *header = 0;
  224. md3Surface_t *surface = 0;
  225. md3Shader_t *md3Shader = 0;
  226. shader_t *shader = 0;
  227. int cull;
  228. int lod;
  229. int fogNum;
  230. qboolean personalModel;
  231. // don't add third_person objects if not in a portal
  232. personalModel = (ent->e.renderfx & RF_THIRD_PERSON) && !tr.viewParms.isPortal;
  233. if ( ent->e.renderfx & RF_WRAP_FRAMES ) {
  234. ent->e.frame %= tr.currentModel->md3[0]->numFrames;
  235. ent->e.oldframe %= tr.currentModel->md3[0]->numFrames;
  236. }
  237. //
  238. // Validate the frames so there is no chance of a crash.
  239. // This will write directly into the entity structure, so
  240. // when the surfaces are rendered, they don't need to be
  241. // range checked again.
  242. //
  243. if ( (ent->e.frame >= tr.currentModel->md3[0]->numFrames)
  244. || (ent->e.frame < 0)
  245. || (ent->e.oldframe >= tr.currentModel->md3[0]->numFrames)
  246. || (ent->e.oldframe < 0) ) {
  247. ri.Printf( PRINT_DEVELOPER, "R_AddMD3Surfaces: no such frame %d to %d for '%s'\n",
  248. ent->e.oldframe, ent->e.frame,
  249. tr.currentModel->name );
  250. ent->e.frame = 0;
  251. ent->e.oldframe = 0;
  252. }
  253. //
  254. // compute LOD
  255. //
  256. lod = R_ComputeLOD( ent );
  257. header = tr.currentModel->md3[lod];
  258. //
  259. // cull the entire model if merged bounding box of both frames
  260. // is outside the view frustum.
  261. //
  262. cull = R_CullModel ( header, ent );
  263. if ( cull == CULL_OUT ) {
  264. return;
  265. }
  266. //
  267. // set up lighting now that we know we aren't culled
  268. //
  269. if ( !personalModel || r_shadows->integer > 1 ) {
  270. R_SetupEntityLighting( &tr.refdef, ent );
  271. }
  272. //
  273. // see if we are in a fog volume
  274. //
  275. fogNum = R_ComputeFogNum( header, ent );
  276. //
  277. // draw all surfaces
  278. //
  279. surface = (md3Surface_t *)( (byte *)header + header->ofsSurfaces );
  280. for ( i = 0 ; i < header->numSurfaces ; i++ ) {
  281. if ( ent->e.customShader ) {
  282. shader = R_GetShaderByHandle( ent->e.customShader );
  283. } else if ( ent->e.customSkin > 0 && ent->e.customSkin < tr.numSkins ) {
  284. skin_t *skin;
  285. int j;
  286. skin = R_GetSkinByHandle( ent->e.customSkin );
  287. // match the surface name to something in the skin file
  288. shader = tr.defaultShader;
  289. for ( j = 0 ; j < skin->numSurfaces ; j++ ) {
  290. // the names have both been lowercased
  291. if ( !strcmp( skin->surfaces[j]->name, surface->name ) ) {
  292. shader = skin->surfaces[j]->shader;
  293. break;
  294. }
  295. }
  296. if (shader == tr.defaultShader) {
  297. ri.Printf( PRINT_DEVELOPER, "WARNING: no shader for surface %s in skin %s\n", surface->name, skin->name);
  298. }
  299. else if (shader->defaultShader) {
  300. ri.Printf( PRINT_DEVELOPER, "WARNING: shader %s in skin %s not found\n", shader->name, skin->name);
  301. }
  302. } else if ( surface->numShaders <= 0 ) {
  303. shader = tr.defaultShader;
  304. } else {
  305. md3Shader = (md3Shader_t *) ( (byte *)surface + surface->ofsShaders );
  306. md3Shader += ent->e.skinNum % surface->numShaders;
  307. shader = tr.shaders[ md3Shader->shaderIndex ];
  308. }
  309. // we will add shadows even if the main object isn't visible in the view
  310. // stencil shadows can't do personal models unless I polyhedron clip
  311. if ( !personalModel
  312. && r_shadows->integer == 2
  313. && fogNum == 0
  314. && !(ent->e.renderfx & ( RF_NOSHADOW | RF_DEPTHHACK ) )
  315. && shader->sort == SS_OPAQUE ) {
  316. R_AddDrawSurf( (void *)surface, tr.shadowShader, 0, qfalse );
  317. }
  318. // projection shadows work fine with personal models
  319. if ( r_shadows->integer == 3
  320. && fogNum == 0
  321. && (ent->e.renderfx & RF_SHADOW_PLANE )
  322. && shader->sort == SS_OPAQUE ) {
  323. R_AddDrawSurf( (void *)surface, tr.projectionShadowShader, 0, qfalse );
  324. }
  325. // don't add third_person objects if not viewing through a portal
  326. if ( !personalModel ) {
  327. R_AddDrawSurf( (void *)surface, shader, fogNum, qfalse );
  328. }
  329. surface = (md3Surface_t *)( (byte *)surface + surface->ofsEnd );
  330. }
  331. }