tr_main.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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_main.c -- main control flow for each frame
  19. #include "tr_local.h"
  20. trGlobals_t tr;
  21. static float s_flipMatrix[16] = {
  22. // convert from our coordinate system (looking down X)
  23. // to OpenGL's coordinate system (looking down -Z)
  24. 0, 0, -1, 0,
  25. -1, 0, 0, 0,
  26. 0, 1, 0, 0,
  27. 0, 0, 0, 1
  28. };
  29. refimport_t ri;
  30. // entities that will have procedurally generated surfaces will just
  31. // point at this for their sorting surface
  32. surfaceType_t entitySurface = SF_ENTITY;
  33. /*
  34. =================
  35. R_CullLocalBox
  36. Returns CULL_IN, CULL_CLIP, or CULL_OUT
  37. =================
  38. */
  39. int R_CullLocalBox (vec3_t bounds[2]) {
  40. int i, j;
  41. vec3_t transformed[8];
  42. float dists[8];
  43. vec3_t v;
  44. cplane_t *frust;
  45. int anyBack;
  46. int front, back;
  47. if ( r_nocull->integer ) {
  48. return CULL_CLIP;
  49. }
  50. // transform into world space
  51. for (i = 0 ; i < 8 ; i++) {
  52. v[0] = bounds[i&1][0];
  53. v[1] = bounds[(i>>1)&1][1];
  54. v[2] = bounds[(i>>2)&1][2];
  55. VectorCopy( tr.or.origin, transformed[i] );
  56. VectorMA( transformed[i], v[0], tr.or.axis[0], transformed[i] );
  57. VectorMA( transformed[i], v[1], tr.or.axis[1], transformed[i] );
  58. VectorMA( transformed[i], v[2], tr.or.axis[2], transformed[i] );
  59. }
  60. // check against frustum planes
  61. anyBack = 0;
  62. for (i = 0 ; i < 4 ; i++) {
  63. frust = &tr.viewParms.frustum[i];
  64. front = back = 0;
  65. for (j = 0 ; j < 8 ; j++) {
  66. dists[j] = DotProduct(transformed[j], frust->normal);
  67. if ( dists[j] > frust->dist ) {
  68. front = 1;
  69. if ( back ) {
  70. break; // a point is in front
  71. }
  72. } else {
  73. back = 1;
  74. }
  75. }
  76. if ( !front ) {
  77. // all points were behind one of the planes
  78. return CULL_OUT;
  79. }
  80. anyBack |= back;
  81. }
  82. if ( !anyBack ) {
  83. return CULL_IN; // completely inside frustum
  84. }
  85. return CULL_CLIP; // partially clipped
  86. }
  87. /*
  88. ** R_CullLocalPointAndRadius
  89. */
  90. int R_CullLocalPointAndRadius( vec3_t pt, float radius )
  91. {
  92. vec3_t transformed;
  93. R_LocalPointToWorld( pt, transformed );
  94. return R_CullPointAndRadius( transformed, radius );
  95. }
  96. /*
  97. ** R_CullPointAndRadius
  98. */
  99. int R_CullPointAndRadius( vec3_t pt, float radius )
  100. {
  101. int i;
  102. float dist;
  103. cplane_t *frust;
  104. qboolean mightBeClipped = qfalse;
  105. if ( r_nocull->integer ) {
  106. return CULL_CLIP;
  107. }
  108. // check against frustum planes
  109. for (i = 0 ; i < 4 ; i++)
  110. {
  111. frust = &tr.viewParms.frustum[i];
  112. dist = DotProduct( pt, frust->normal) - frust->dist;
  113. if ( dist < -radius )
  114. {
  115. return CULL_OUT;
  116. }
  117. else if ( dist <= radius )
  118. {
  119. mightBeClipped = qtrue;
  120. }
  121. }
  122. if ( mightBeClipped )
  123. {
  124. return CULL_CLIP;
  125. }
  126. return CULL_IN; // completely inside frustum
  127. }
  128. /*
  129. =================
  130. R_LocalNormalToWorld
  131. =================
  132. */
  133. void R_LocalNormalToWorld (vec3_t local, vec3_t world) {
  134. world[0] = local[0] * tr.or.axis[0][0] + local[1] * tr.or.axis[1][0] + local[2] * tr.or.axis[2][0];
  135. world[1] = local[0] * tr.or.axis[0][1] + local[1] * tr.or.axis[1][1] + local[2] * tr.or.axis[2][1];
  136. world[2] = local[0] * tr.or.axis[0][2] + local[1] * tr.or.axis[1][2] + local[2] * tr.or.axis[2][2];
  137. }
  138. /*
  139. =================
  140. R_LocalPointToWorld
  141. =================
  142. */
  143. void R_LocalPointToWorld (vec3_t local, vec3_t world) {
  144. world[0] = local[0] * tr.or.axis[0][0] + local[1] * tr.or.axis[1][0] + local[2] * tr.or.axis[2][0] + tr.or.origin[0];
  145. world[1] = local[0] * tr.or.axis[0][1] + local[1] * tr.or.axis[1][1] + local[2] * tr.or.axis[2][1] + tr.or.origin[1];
  146. world[2] = local[0] * tr.or.axis[0][2] + local[1] * tr.or.axis[1][2] + local[2] * tr.or.axis[2][2] + tr.or.origin[2];
  147. }
  148. /*
  149. =================
  150. R_WorldToLocal
  151. =================
  152. */
  153. void R_WorldToLocal (vec3_t world, vec3_t local) {
  154. local[0] = DotProduct(world, tr.or.axis[0]);
  155. local[1] = DotProduct(world, tr.or.axis[1]);
  156. local[2] = DotProduct(world, tr.or.axis[2]);
  157. }
  158. /*
  159. ==========================
  160. R_TransformModelToClip
  161. ==========================
  162. */
  163. void R_TransformModelToClip( const vec3_t src, const float *modelMatrix, const float *projectionMatrix,
  164. vec4_t eye, vec4_t dst ) {
  165. int i;
  166. for ( i = 0 ; i < 4 ; i++ ) {
  167. eye[i] =
  168. src[0] * modelMatrix[ i + 0 * 4 ] +
  169. src[1] * modelMatrix[ i + 1 * 4 ] +
  170. src[2] * modelMatrix[ i + 2 * 4 ] +
  171. 1 * modelMatrix[ i + 3 * 4 ];
  172. }
  173. for ( i = 0 ; i < 4 ; i++ ) {
  174. dst[i] =
  175. eye[0] * projectionMatrix[ i + 0 * 4 ] +
  176. eye[1] * projectionMatrix[ i + 1 * 4 ] +
  177. eye[2] * projectionMatrix[ i + 2 * 4 ] +
  178. eye[3] * projectionMatrix[ i + 3 * 4 ];
  179. }
  180. }
  181. /*
  182. ==========================
  183. R_TransformClipToWindow
  184. ==========================
  185. */
  186. void R_TransformClipToWindow( const vec4_t clip, const viewParms_t *view, vec4_t normalized, vec4_t window ) {
  187. normalized[0] = clip[0] / clip[3];
  188. normalized[1] = clip[1] / clip[3];
  189. normalized[2] = ( clip[2] + clip[3] ) / ( 2 * clip[3] );
  190. window[0] = 0.5f * ( 1.0f + normalized[0] ) * view->viewportWidth;
  191. window[1] = 0.5f * ( 1.0f + normalized[1] ) * view->viewportHeight;
  192. window[2] = normalized[2];
  193. window[0] = (int) ( window[0] + 0.5 );
  194. window[1] = (int) ( window[1] + 0.5 );
  195. }
  196. /*
  197. ==========================
  198. myGlMultMatrix
  199. ==========================
  200. */
  201. void myGlMultMatrix( const float *a, const float *b, float *out ) {
  202. int i, j;
  203. for ( i = 0 ; i < 4 ; i++ ) {
  204. for ( j = 0 ; j < 4 ; j++ ) {
  205. out[ i * 4 + j ] =
  206. a [ i * 4 + 0 ] * b [ 0 * 4 + j ]
  207. + a [ i * 4 + 1 ] * b [ 1 * 4 + j ]
  208. + a [ i * 4 + 2 ] * b [ 2 * 4 + j ]
  209. + a [ i * 4 + 3 ] * b [ 3 * 4 + j ];
  210. }
  211. }
  212. }
  213. /*
  214. =================
  215. R_RotateForEntity
  216. Generates an orientation for an entity and viewParms
  217. Does NOT produce any GL calls
  218. Called by both the front end and the back end
  219. =================
  220. */
  221. void R_RotateForEntity( const trRefEntity_t *ent, const viewParms_t *viewParms,
  222. orientationr_t *or ) {
  223. float glMatrix[16];
  224. vec3_t delta;
  225. float axisLength;
  226. if ( ent->e.reType != RT_MODEL ) {
  227. *or = viewParms->world;
  228. return;
  229. }
  230. VectorCopy( ent->e.origin, or->origin );
  231. VectorCopy( ent->e.axis[0], or->axis[0] );
  232. VectorCopy( ent->e.axis[1], or->axis[1] );
  233. VectorCopy( ent->e.axis[2], or->axis[2] );
  234. glMatrix[0] = or->axis[0][0];
  235. glMatrix[4] = or->axis[1][0];
  236. glMatrix[8] = or->axis[2][0];
  237. glMatrix[12] = or->origin[0];
  238. glMatrix[1] = or->axis[0][1];
  239. glMatrix[5] = or->axis[1][1];
  240. glMatrix[9] = or->axis[2][1];
  241. glMatrix[13] = or->origin[1];
  242. glMatrix[2] = or->axis[0][2];
  243. glMatrix[6] = or->axis[1][2];
  244. glMatrix[10] = or->axis[2][2];
  245. glMatrix[14] = or->origin[2];
  246. glMatrix[3] = 0;
  247. glMatrix[7] = 0;
  248. glMatrix[11] = 0;
  249. glMatrix[15] = 1;
  250. myGlMultMatrix( glMatrix, viewParms->world.modelMatrix, or->modelMatrix );
  251. // calculate the viewer origin in the model's space
  252. // needed for fog, specular, and environment mapping
  253. VectorSubtract( viewParms->or.origin, or->origin, delta );
  254. // compensate for scale in the axes if necessary
  255. if ( ent->e.nonNormalizedAxes ) {
  256. axisLength = VectorLength( ent->e.axis[0] );
  257. if ( !axisLength ) {
  258. axisLength = 0;
  259. } else {
  260. axisLength = 1.0f / axisLength;
  261. }
  262. } else {
  263. axisLength = 1.0f;
  264. }
  265. or->viewOrigin[0] = DotProduct( delta, or->axis[0] ) * axisLength;
  266. or->viewOrigin[1] = DotProduct( delta, or->axis[1] ) * axisLength;
  267. or->viewOrigin[2] = DotProduct( delta, or->axis[2] ) * axisLength;
  268. }
  269. /*
  270. =================
  271. R_RotateForViewer
  272. Sets up the modelview matrix for a given viewParm
  273. =================
  274. */
  275. void R_RotateForViewer (void)
  276. {
  277. float viewerMatrix[16];
  278. vec3_t origin;
  279. Com_Memset (&tr.or, 0, sizeof(tr.or));
  280. tr.or.axis[0][0] = 1;
  281. tr.or.axis[1][1] = 1;
  282. tr.or.axis[2][2] = 1;
  283. VectorCopy (tr.viewParms.or.origin, tr.or.viewOrigin);
  284. // transform by the camera placement
  285. VectorCopy( tr.viewParms.or.origin, origin );
  286. viewerMatrix[0] = tr.viewParms.or.axis[0][0];
  287. viewerMatrix[4] = tr.viewParms.or.axis[0][1];
  288. viewerMatrix[8] = tr.viewParms.or.axis[0][2];
  289. viewerMatrix[12] = -origin[0] * viewerMatrix[0] + -origin[1] * viewerMatrix[4] + -origin[2] * viewerMatrix[8];
  290. viewerMatrix[1] = tr.viewParms.or.axis[1][0];
  291. viewerMatrix[5] = tr.viewParms.or.axis[1][1];
  292. viewerMatrix[9] = tr.viewParms.or.axis[1][2];
  293. viewerMatrix[13] = -origin[0] * viewerMatrix[1] + -origin[1] * viewerMatrix[5] + -origin[2] * viewerMatrix[9];
  294. viewerMatrix[2] = tr.viewParms.or.axis[2][0];
  295. viewerMatrix[6] = tr.viewParms.or.axis[2][1];
  296. viewerMatrix[10] = tr.viewParms.or.axis[2][2];
  297. viewerMatrix[14] = -origin[0] * viewerMatrix[2] + -origin[1] * viewerMatrix[6] + -origin[2] * viewerMatrix[10];
  298. viewerMatrix[3] = 0;
  299. viewerMatrix[7] = 0;
  300. viewerMatrix[11] = 0;
  301. viewerMatrix[15] = 1;
  302. // convert from our coordinate system (looking down X)
  303. // to OpenGL's coordinate system (looking down -Z)
  304. myGlMultMatrix( viewerMatrix, s_flipMatrix, tr.or.modelMatrix );
  305. tr.viewParms.world = tr.or;
  306. }
  307. /*
  308. ** SetFarClip
  309. */
  310. static void SetFarClip( void )
  311. {
  312. float farthestCornerDistance = 0;
  313. int i;
  314. // if not rendering the world (icons, menus, etc)
  315. // set a 2k far clip plane
  316. if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) {
  317. tr.viewParms.zFar = 2048;
  318. return;
  319. }
  320. //
  321. // set far clipping planes dynamically
  322. //
  323. farthestCornerDistance = 0;
  324. for ( i = 0; i < 8; i++ )
  325. {
  326. vec3_t v;
  327. vec3_t vecTo;
  328. float distance;
  329. if ( i & 1 )
  330. {
  331. v[0] = tr.viewParms.visBounds[0][0];
  332. }
  333. else
  334. {
  335. v[0] = tr.viewParms.visBounds[1][0];
  336. }
  337. if ( i & 2 )
  338. {
  339. v[1] = tr.viewParms.visBounds[0][1];
  340. }
  341. else
  342. {
  343. v[1] = tr.viewParms.visBounds[1][1];
  344. }
  345. if ( i & 4 )
  346. {
  347. v[2] = tr.viewParms.visBounds[0][2];
  348. }
  349. else
  350. {
  351. v[2] = tr.viewParms.visBounds[1][2];
  352. }
  353. VectorSubtract( v, tr.viewParms.or.origin, vecTo );
  354. distance = vecTo[0] * vecTo[0] + vecTo[1] * vecTo[1] + vecTo[2] * vecTo[2];
  355. if ( distance > farthestCornerDistance )
  356. {
  357. farthestCornerDistance = distance;
  358. }
  359. }
  360. tr.viewParms.zFar = sqrt( farthestCornerDistance );
  361. }
  362. /*
  363. ===============
  364. R_SetupProjection
  365. ===============
  366. */
  367. void R_SetupProjection( void ) {
  368. float xmin, xmax, ymin, ymax;
  369. float width, height, depth;
  370. float zNear, zFar;
  371. // dynamically compute far clip plane distance
  372. SetFarClip();
  373. //
  374. // set up projection matrix
  375. //
  376. zNear = r_znear->value;
  377. zFar = tr.viewParms.zFar;
  378. ymax = zNear * tan( tr.refdef.fov_y * M_PI / 360.0f );
  379. ymin = -ymax;
  380. xmax = zNear * tan( tr.refdef.fov_x * M_PI / 360.0f );
  381. xmin = -xmax;
  382. width = xmax - xmin;
  383. height = ymax - ymin;
  384. depth = zFar - zNear;
  385. tr.viewParms.projectionMatrix[0] = 2 * zNear / width;
  386. tr.viewParms.projectionMatrix[4] = 0;
  387. tr.viewParms.projectionMatrix[8] = ( xmax + xmin ) / width; // normally 0
  388. tr.viewParms.projectionMatrix[12] = 0;
  389. tr.viewParms.projectionMatrix[1] = 0;
  390. tr.viewParms.projectionMatrix[5] = 2 * zNear / height;
  391. tr.viewParms.projectionMatrix[9] = ( ymax + ymin ) / height; // normally 0
  392. tr.viewParms.projectionMatrix[13] = 0;
  393. tr.viewParms.projectionMatrix[2] = 0;
  394. tr.viewParms.projectionMatrix[6] = 0;
  395. tr.viewParms.projectionMatrix[10] = -( zFar + zNear ) / depth;
  396. tr.viewParms.projectionMatrix[14] = -2 * zFar * zNear / depth;
  397. tr.viewParms.projectionMatrix[3] = 0;
  398. tr.viewParms.projectionMatrix[7] = 0;
  399. tr.viewParms.projectionMatrix[11] = -1;
  400. tr.viewParms.projectionMatrix[15] = 0;
  401. }
  402. /*
  403. =================
  404. R_SetupFrustum
  405. Setup that culling frustum planes for the current view
  406. =================
  407. */
  408. void R_SetupFrustum (void) {
  409. int i;
  410. float xs, xc;
  411. float ang;
  412. ang = tr.viewParms.fovX / 180 * M_PI * 0.5f;
  413. xs = sin( ang );
  414. xc = cos( ang );
  415. VectorScale( tr.viewParms.or.axis[0], xs, tr.viewParms.frustum[0].normal );
  416. VectorMA( tr.viewParms.frustum[0].normal, xc, tr.viewParms.or.axis[1], tr.viewParms.frustum[0].normal );
  417. VectorScale( tr.viewParms.or.axis[0], xs, tr.viewParms.frustum[1].normal );
  418. VectorMA( tr.viewParms.frustum[1].normal, -xc, tr.viewParms.or.axis[1], tr.viewParms.frustum[1].normal );
  419. ang = tr.viewParms.fovY / 180 * M_PI * 0.5f;
  420. xs = sin( ang );
  421. xc = cos( ang );
  422. VectorScale( tr.viewParms.or.axis[0], xs, tr.viewParms.frustum[2].normal );
  423. VectorMA( tr.viewParms.frustum[2].normal, xc, tr.viewParms.or.axis[2], tr.viewParms.frustum[2].normal );
  424. VectorScale( tr.viewParms.or.axis[0], xs, tr.viewParms.frustum[3].normal );
  425. VectorMA( tr.viewParms.frustum[3].normal, -xc, tr.viewParms.or.axis[2], tr.viewParms.frustum[3].normal );
  426. for (i=0 ; i<4 ; i++) {
  427. tr.viewParms.frustum[i].type = PLANE_NON_AXIAL;
  428. tr.viewParms.frustum[i].dist = DotProduct (tr.viewParms.or.origin, tr.viewParms.frustum[i].normal);
  429. SetPlaneSignbits( &tr.viewParms.frustum[i] );
  430. }
  431. }
  432. /*
  433. =================
  434. R_MirrorPoint
  435. =================
  436. */
  437. void R_MirrorPoint (vec3_t in, orientation_t *surface, orientation_t *camera, vec3_t out) {
  438. int i;
  439. vec3_t local;
  440. vec3_t transformed;
  441. float d;
  442. VectorSubtract( in, surface->origin, local );
  443. VectorClear( transformed );
  444. for ( i = 0 ; i < 3 ; i++ ) {
  445. d = DotProduct(local, surface->axis[i]);
  446. VectorMA( transformed, d, camera->axis[i], transformed );
  447. }
  448. VectorAdd( transformed, camera->origin, out );
  449. }
  450. void R_MirrorVector (vec3_t in, orientation_t *surface, orientation_t *camera, vec3_t out) {
  451. int i;
  452. float d;
  453. VectorClear( out );
  454. for ( i = 0 ; i < 3 ; i++ ) {
  455. d = DotProduct(in, surface->axis[i]);
  456. VectorMA( out, d, camera->axis[i], out );
  457. }
  458. }
  459. /*
  460. =============
  461. R_PlaneForSurface
  462. =============
  463. */
  464. void R_PlaneForSurface (surfaceType_t *surfType, cplane_t *plane) {
  465. srfTriangles_t *tri;
  466. srfPoly_t *poly;
  467. drawVert_t *v1, *v2, *v3;
  468. vec4_t plane4;
  469. if (!surfType) {
  470. Com_Memset (plane, 0, sizeof(*plane));
  471. plane->normal[0] = 1;
  472. return;
  473. }
  474. switch (*surfType) {
  475. case SF_FACE:
  476. *plane = ((srfSurfaceFace_t *)surfType)->plane;
  477. return;
  478. case SF_TRIANGLES:
  479. tri = (srfTriangles_t *)surfType;
  480. v1 = tri->verts + tri->indexes[0];
  481. v2 = tri->verts + tri->indexes[1];
  482. v3 = tri->verts + tri->indexes[2];
  483. PlaneFromPoints( plane4, v1->xyz, v2->xyz, v3->xyz );
  484. VectorCopy( plane4, plane->normal );
  485. plane->dist = plane4[3];
  486. return;
  487. case SF_POLY:
  488. poly = (srfPoly_t *)surfType;
  489. PlaneFromPoints( plane4, poly->verts[0].xyz, poly->verts[1].xyz, poly->verts[2].xyz );
  490. VectorCopy( plane4, plane->normal );
  491. plane->dist = plane4[3];
  492. return;
  493. default:
  494. Com_Memset (plane, 0, sizeof(*plane));
  495. plane->normal[0] = 1;
  496. return;
  497. }
  498. }
  499. /*
  500. =================
  501. R_GetPortalOrientation
  502. entityNum is the entity that the portal surface is a part of, which may
  503. be moving and rotating.
  504. Returns qtrue if it should be mirrored
  505. =================
  506. */
  507. qboolean R_GetPortalOrientations( drawSurf_t *drawSurf, int entityNum,
  508. orientation_t *surface, orientation_t *camera,
  509. vec3_t pvsOrigin, qboolean *mirror ) {
  510. int i;
  511. cplane_t originalPlane, plane;
  512. trRefEntity_t *e;
  513. float d;
  514. vec3_t transformed;
  515. // create plane axis for the portal we are seeing
  516. R_PlaneForSurface( drawSurf->surface, &originalPlane );
  517. // rotate the plane if necessary
  518. if ( entityNum != ENTITYNUM_WORLD ) {
  519. tr.currentEntityNum = entityNum;
  520. tr.currentEntity = &tr.refdef.entities[entityNum];
  521. // get the orientation of the entity
  522. R_RotateForEntity( tr.currentEntity, &tr.viewParms, &tr.or );
  523. // rotate the plane, but keep the non-rotated version for matching
  524. // against the portalSurface entities
  525. R_LocalNormalToWorld( originalPlane.normal, plane.normal );
  526. plane.dist = originalPlane.dist + DotProduct( plane.normal, tr.or.origin );
  527. // translate the original plane
  528. originalPlane.dist = originalPlane.dist + DotProduct( originalPlane.normal, tr.or.origin );
  529. } else {
  530. plane = originalPlane;
  531. }
  532. VectorCopy( plane.normal, surface->axis[0] );
  533. PerpendicularVector( surface->axis[1], surface->axis[0] );
  534. CrossProduct( surface->axis[0], surface->axis[1], surface->axis[2] );
  535. // locate the portal entity closest to this plane.
  536. // origin will be the origin of the portal, origin2 will be
  537. // the origin of the camera
  538. for ( i = 0 ; i < tr.refdef.num_entities ; i++ ) {
  539. e = &tr.refdef.entities[i];
  540. if ( e->e.reType != RT_PORTALSURFACE ) {
  541. continue;
  542. }
  543. d = DotProduct( e->e.origin, originalPlane.normal ) - originalPlane.dist;
  544. if ( d > 64 || d < -64) {
  545. continue;
  546. }
  547. // get the pvsOrigin from the entity
  548. VectorCopy( e->e.oldorigin, pvsOrigin );
  549. // if the entity is just a mirror, don't use as a camera point
  550. if ( e->e.oldorigin[0] == e->e.origin[0] &&
  551. e->e.oldorigin[1] == e->e.origin[1] &&
  552. e->e.oldorigin[2] == e->e.origin[2] ) {
  553. VectorScale( plane.normal, plane.dist, surface->origin );
  554. VectorCopy( surface->origin, camera->origin );
  555. VectorSubtract( vec3_origin, surface->axis[0], camera->axis[0] );
  556. VectorCopy( surface->axis[1], camera->axis[1] );
  557. VectorCopy( surface->axis[2], camera->axis[2] );
  558. *mirror = qtrue;
  559. return qtrue;
  560. }
  561. // project the origin onto the surface plane to get
  562. // an origin point we can rotate around
  563. d = DotProduct( e->e.origin, plane.normal ) - plane.dist;
  564. VectorMA( e->e.origin, -d, surface->axis[0], surface->origin );
  565. // now get the camera origin and orientation
  566. VectorCopy( e->e.oldorigin, camera->origin );
  567. AxisCopy( e->e.axis, camera->axis );
  568. VectorSubtract( vec3_origin, camera->axis[0], camera->axis[0] );
  569. VectorSubtract( vec3_origin, camera->axis[1], camera->axis[1] );
  570. // optionally rotate
  571. if ( e->e.oldframe ) {
  572. // if a speed is specified
  573. if ( e->e.frame ) {
  574. // continuous rotate
  575. d = (tr.refdef.time/1000.0f) * e->e.frame;
  576. VectorCopy( camera->axis[1], transformed );
  577. RotatePointAroundVector( camera->axis[1], camera->axis[0], transformed, d );
  578. CrossProduct( camera->axis[0], camera->axis[1], camera->axis[2] );
  579. } else {
  580. // bobbing rotate, with skinNum being the rotation offset
  581. d = sin( tr.refdef.time * 0.003f );
  582. d = e->e.skinNum + d * 4;
  583. VectorCopy( camera->axis[1], transformed );
  584. RotatePointAroundVector( camera->axis[1], camera->axis[0], transformed, d );
  585. CrossProduct( camera->axis[0], camera->axis[1], camera->axis[2] );
  586. }
  587. }
  588. else if ( e->e.skinNum ) {
  589. d = e->e.skinNum;
  590. VectorCopy( camera->axis[1], transformed );
  591. RotatePointAroundVector( camera->axis[1], camera->axis[0], transformed, d );
  592. CrossProduct( camera->axis[0], camera->axis[1], camera->axis[2] );
  593. }
  594. *mirror = qfalse;
  595. return qtrue;
  596. }
  597. // if we didn't locate a portal entity, don't render anything.
  598. // We don't want to just treat it as a mirror, because without a
  599. // portal entity the server won't have communicated a proper entity set
  600. // in the snapshot
  601. // unfortunately, with local movement prediction it is easily possible
  602. // to see a surface before the server has communicated the matching
  603. // portal surface entity, so we don't want to print anything here...
  604. //ri.Printf( PRINT_ALL, "Portal surface without a portal entity\n" );
  605. return qfalse;
  606. }
  607. static qboolean IsMirror( const drawSurf_t *drawSurf, int entityNum )
  608. {
  609. int i;
  610. cplane_t originalPlane, plane;
  611. trRefEntity_t *e;
  612. float d;
  613. // create plane axis for the portal we are seeing
  614. R_PlaneForSurface( drawSurf->surface, &originalPlane );
  615. // rotate the plane if necessary
  616. if ( entityNum != ENTITYNUM_WORLD )
  617. {
  618. tr.currentEntityNum = entityNum;
  619. tr.currentEntity = &tr.refdef.entities[entityNum];
  620. // get the orientation of the entity
  621. R_RotateForEntity( tr.currentEntity, &tr.viewParms, &tr.or );
  622. // rotate the plane, but keep the non-rotated version for matching
  623. // against the portalSurface entities
  624. R_LocalNormalToWorld( originalPlane.normal, plane.normal );
  625. plane.dist = originalPlane.dist + DotProduct( plane.normal, tr.or.origin );
  626. // translate the original plane
  627. originalPlane.dist = originalPlane.dist + DotProduct( originalPlane.normal, tr.or.origin );
  628. }
  629. else
  630. {
  631. plane = originalPlane;
  632. }
  633. // locate the portal entity closest to this plane.
  634. // origin will be the origin of the portal, origin2 will be
  635. // the origin of the camera
  636. for ( i = 0 ; i < tr.refdef.num_entities ; i++ )
  637. {
  638. e = &tr.refdef.entities[i];
  639. if ( e->e.reType != RT_PORTALSURFACE ) {
  640. continue;
  641. }
  642. d = DotProduct( e->e.origin, originalPlane.normal ) - originalPlane.dist;
  643. if ( d > 64 || d < -64) {
  644. continue;
  645. }
  646. // if the entity is just a mirror, don't use as a camera point
  647. if ( e->e.oldorigin[0] == e->e.origin[0] &&
  648. e->e.oldorigin[1] == e->e.origin[1] &&
  649. e->e.oldorigin[2] == e->e.origin[2] )
  650. {
  651. return qtrue;
  652. }
  653. return qfalse;
  654. }
  655. return qfalse;
  656. }
  657. /*
  658. ** SurfIsOffscreen
  659. **
  660. ** Determines if a surface is completely offscreen.
  661. */
  662. static qboolean SurfIsOffscreen( const drawSurf_t *drawSurf, vec4_t clipDest[128] ) {
  663. float shortest = 100000000;
  664. int entityNum;
  665. int numTriangles;
  666. shader_t *shader;
  667. int fogNum;
  668. int dlighted;
  669. vec4_t clip, eye;
  670. int i;
  671. unsigned int pointOr = 0;
  672. unsigned int pointAnd = (unsigned int)~0;
  673. if ( glConfig.smpActive ) { // FIXME! we can't do RB_BeginSurface/RB_EndSurface stuff with smp!
  674. return qfalse;
  675. }
  676. R_RotateForViewer();
  677. R_DecomposeSort( drawSurf->sort, &entityNum, &shader, &fogNum, &dlighted );
  678. RB_BeginSurface( shader, fogNum );
  679. rb_surfaceTable[ *drawSurf->surface ]( drawSurf->surface );
  680. assert( tess.numVertexes < 128 );
  681. for ( i = 0; i < tess.numVertexes; i++ )
  682. {
  683. int j;
  684. unsigned int pointFlags = 0;
  685. R_TransformModelToClip( tess.xyz[i], tr.or.modelMatrix, tr.viewParms.projectionMatrix, eye, clip );
  686. for ( j = 0; j < 3; j++ )
  687. {
  688. if ( clip[j] >= clip[3] )
  689. {
  690. pointFlags |= (1 << (j*2));
  691. }
  692. else if ( clip[j] <= -clip[3] )
  693. {
  694. pointFlags |= ( 1 << (j*2+1));
  695. }
  696. }
  697. pointAnd &= pointFlags;
  698. pointOr |= pointFlags;
  699. }
  700. // trivially reject
  701. if ( pointAnd )
  702. {
  703. return qtrue;
  704. }
  705. // determine if this surface is backfaced and also determine the distance
  706. // to the nearest vertex so we can cull based on portal range. Culling
  707. // based on vertex distance isn't 100% correct (we should be checking for
  708. // range to the surface), but it's good enough for the types of portals
  709. // we have in the game right now.
  710. numTriangles = tess.numIndexes / 3;
  711. for ( i = 0; i < tess.numIndexes; i += 3 )
  712. {
  713. vec3_t normal;
  714. float dot;
  715. float len;
  716. VectorSubtract( tess.xyz[tess.indexes[i]], tr.viewParms.or.origin, normal );
  717. len = VectorLengthSquared( normal ); // lose the sqrt
  718. if ( len < shortest )
  719. {
  720. shortest = len;
  721. }
  722. if ( ( dot = DotProduct( normal, tess.normal[tess.indexes[i]] ) ) >= 0 )
  723. {
  724. numTriangles--;
  725. }
  726. }
  727. if ( !numTriangles )
  728. {
  729. return qtrue;
  730. }
  731. // mirrors can early out at this point, since we don't do a fade over distance
  732. // with them (although we could)
  733. if ( IsMirror( drawSurf, entityNum ) )
  734. {
  735. return qfalse;
  736. }
  737. if ( shortest > (tess.shader->portalRange*tess.shader->portalRange) )
  738. {
  739. return qtrue;
  740. }
  741. return qfalse;
  742. }
  743. /*
  744. ========================
  745. R_MirrorViewBySurface
  746. Returns qtrue if another view has been rendered
  747. ========================
  748. */
  749. qboolean R_MirrorViewBySurface (drawSurf_t *drawSurf, int entityNum) {
  750. vec4_t clipDest[128];
  751. viewParms_t newParms;
  752. viewParms_t oldParms;
  753. orientation_t surface, camera;
  754. // don't recursively mirror
  755. if (tr.viewParms.isPortal) {
  756. ri.Printf( PRINT_DEVELOPER, "WARNING: recursive mirror/portal found\n" );
  757. return qfalse;
  758. }
  759. if ( r_noportals->integer || (r_fastsky->integer == 1) ) {
  760. return qfalse;
  761. }
  762. // trivially reject portal/mirror
  763. if ( SurfIsOffscreen( drawSurf, clipDest ) ) {
  764. return qfalse;
  765. }
  766. // save old viewParms so we can return to it after the mirror view
  767. oldParms = tr.viewParms;
  768. newParms = tr.viewParms;
  769. newParms.isPortal = qtrue;
  770. if ( !R_GetPortalOrientations( drawSurf, entityNum, &surface, &camera,
  771. newParms.pvsOrigin, &newParms.isMirror ) ) {
  772. return qfalse; // bad portal, no portalentity
  773. }
  774. R_MirrorPoint (oldParms.or.origin, &surface, &camera, newParms.or.origin );
  775. VectorSubtract( vec3_origin, camera.axis[0], newParms.portalPlane.normal );
  776. newParms.portalPlane.dist = DotProduct( camera.origin, newParms.portalPlane.normal );
  777. R_MirrorVector (oldParms.or.axis[0], &surface, &camera, newParms.or.axis[0]);
  778. R_MirrorVector (oldParms.or.axis[1], &surface, &camera, newParms.or.axis[1]);
  779. R_MirrorVector (oldParms.or.axis[2], &surface, &camera, newParms.or.axis[2]);
  780. // OPTIMIZE: restrict the viewport on the mirrored view
  781. // render the mirror view
  782. R_RenderView (&newParms);
  783. tr.viewParms = oldParms;
  784. return qtrue;
  785. }
  786. /*
  787. =================
  788. R_SpriteFogNum
  789. See if a sprite is inside a fog volume
  790. =================
  791. */
  792. int R_SpriteFogNum( trRefEntity_t *ent ) {
  793. int i, j;
  794. fog_t *fog;
  795. if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) {
  796. return 0;
  797. }
  798. for ( i = 1 ; i < tr.world->numfogs ; i++ ) {
  799. fog = &tr.world->fogs[i];
  800. for ( j = 0 ; j < 3 ; j++ ) {
  801. if ( ent->e.origin[j] - ent->e.radius >= fog->bounds[1][j] ) {
  802. break;
  803. }
  804. if ( ent->e.origin[j] + ent->e.radius <= fog->bounds[0][j] ) {
  805. break;
  806. }
  807. }
  808. if ( j == 3 ) {
  809. return i;
  810. }
  811. }
  812. return 0;
  813. }
  814. /*
  815. ==========================================================================================
  816. DRAWSURF SORTING
  817. ==========================================================================================
  818. */
  819. /*
  820. =================
  821. qsort replacement
  822. =================
  823. */
  824. #define SWAP_DRAW_SURF(a,b) temp=((int *)a)[0];((int *)a)[0]=((int *)b)[0];((int *)b)[0]=temp; temp=((int *)a)[1];((int *)a)[1]=((int *)b)[1];((int *)b)[1]=temp;
  825. /* this parameter defines the cutoff between using quick sort and
  826. insertion sort for arrays; arrays with lengths shorter or equal to the
  827. below value use insertion sort */
  828. #define CUTOFF 8 /* testing shows that this is good value */
  829. static void shortsort( drawSurf_t *lo, drawSurf_t *hi ) {
  830. drawSurf_t *p, *max;
  831. int temp;
  832. while (hi > lo) {
  833. max = lo;
  834. for (p = lo + 1; p <= hi; p++ ) {
  835. if ( p->sort > max->sort ) {
  836. max = p;
  837. }
  838. }
  839. SWAP_DRAW_SURF(max, hi);
  840. hi--;
  841. }
  842. }
  843. /* sort the array between lo and hi (inclusive)
  844. FIXME: this was lifted and modified from the microsoft lib source...
  845. */
  846. void qsortFast (
  847. void *base,
  848. unsigned num,
  849. unsigned width
  850. )
  851. {
  852. char *lo, *hi; /* ends of sub-array currently sorting */
  853. char *mid; /* points to middle of subarray */
  854. char *loguy, *higuy; /* traveling pointers for partition step */
  855. unsigned size; /* size of the sub-array */
  856. char *lostk[30], *histk[30];
  857. int stkptr; /* stack for saving sub-array to be processed */
  858. int temp;
  859. if ( sizeof(drawSurf_t) != 8 ) {
  860. ri.Error( ERR_DROP, "change SWAP_DRAW_SURF macro" );
  861. }
  862. /* Note: the number of stack entries required is no more than
  863. 1 + log2(size), so 30 is sufficient for any array */
  864. if (num < 2 || width == 0)
  865. return; /* nothing to do */
  866. stkptr = 0; /* initialize stack */
  867. lo = base;
  868. hi = (char *)base + width * (num-1); /* initialize limits */
  869. /* this entry point is for pseudo-recursion calling: setting
  870. lo and hi and jumping to here is like recursion, but stkptr is
  871. prserved, locals aren't, so we preserve stuff on the stack */
  872. recurse:
  873. size = (hi - lo) / width + 1; /* number of el's to sort */
  874. /* below a certain size, it is faster to use a O(n^2) sorting method */
  875. if (size <= CUTOFF) {
  876. shortsort((drawSurf_t *)lo, (drawSurf_t *)hi);
  877. }
  878. else {
  879. /* First we pick a partititioning element. The efficiency of the
  880. algorithm demands that we find one that is approximately the
  881. median of the values, but also that we select one fast. Using
  882. the first one produces bad performace if the array is already
  883. sorted, so we use the middle one, which would require a very
  884. wierdly arranged array for worst case performance. Testing shows
  885. that a median-of-three algorithm does not, in general, increase
  886. performance. */
  887. mid = lo + (size / 2) * width; /* find middle element */
  888. SWAP_DRAW_SURF(mid, lo); /* swap it to beginning of array */
  889. /* We now wish to partition the array into three pieces, one
  890. consisiting of elements <= partition element, one of elements
  891. equal to the parition element, and one of element >= to it. This
  892. is done below; comments indicate conditions established at every
  893. step. */
  894. loguy = lo;
  895. higuy = hi + width;
  896. /* Note that higuy decreases and loguy increases on every iteration,
  897. so loop must terminate. */
  898. for (;;) {
  899. /* lo <= loguy < hi, lo < higuy <= hi + 1,
  900. A[i] <= A[lo] for lo <= i <= loguy,
  901. A[i] >= A[lo] for higuy <= i <= hi */
  902. do {
  903. loguy += width;
  904. } while (loguy <= hi &&
  905. ( ((drawSurf_t *)loguy)->sort <= ((drawSurf_t *)lo)->sort ) );
  906. /* lo < loguy <= hi+1, A[i] <= A[lo] for lo <= i < loguy,
  907. either loguy > hi or A[loguy] > A[lo] */
  908. do {
  909. higuy -= width;
  910. } while (higuy > lo &&
  911. ( ((drawSurf_t *)higuy)->sort >= ((drawSurf_t *)lo)->sort ) );
  912. /* lo-1 <= higuy <= hi, A[i] >= A[lo] for higuy < i <= hi,
  913. either higuy <= lo or A[higuy] < A[lo] */
  914. if (higuy < loguy)
  915. break;
  916. /* if loguy > hi or higuy <= lo, then we would have exited, so
  917. A[loguy] > A[lo], A[higuy] < A[lo],
  918. loguy < hi, highy > lo */
  919. SWAP_DRAW_SURF(loguy, higuy);
  920. /* A[loguy] < A[lo], A[higuy] > A[lo]; so condition at top
  921. of loop is re-established */
  922. }
  923. /* A[i] >= A[lo] for higuy < i <= hi,
  924. A[i] <= A[lo] for lo <= i < loguy,
  925. higuy < loguy, lo <= higuy <= hi
  926. implying:
  927. A[i] >= A[lo] for loguy <= i <= hi,
  928. A[i] <= A[lo] for lo <= i <= higuy,
  929. A[i] = A[lo] for higuy < i < loguy */
  930. SWAP_DRAW_SURF(lo, higuy); /* put partition element in place */
  931. /* OK, now we have the following:
  932. A[i] >= A[higuy] for loguy <= i <= hi,
  933. A[i] <= A[higuy] for lo <= i < higuy
  934. A[i] = A[lo] for higuy <= i < loguy */
  935. /* We've finished the partition, now we want to sort the subarrays
  936. [lo, higuy-1] and [loguy, hi].
  937. We do the smaller one first to minimize stack usage.
  938. We only sort arrays of length 2 or more.*/
  939. if ( higuy - 1 - lo >= hi - loguy ) {
  940. if (lo + width < higuy) {
  941. lostk[stkptr] = lo;
  942. histk[stkptr] = higuy - width;
  943. ++stkptr;
  944. } /* save big recursion for later */
  945. if (loguy < hi) {
  946. lo = loguy;
  947. goto recurse; /* do small recursion */
  948. }
  949. }
  950. else {
  951. if (loguy < hi) {
  952. lostk[stkptr] = loguy;
  953. histk[stkptr] = hi;
  954. ++stkptr; /* save big recursion for later */
  955. }
  956. if (lo + width < higuy) {
  957. hi = higuy - width;
  958. goto recurse; /* do small recursion */
  959. }
  960. }
  961. }
  962. /* We have sorted the array, except for any pending sorts on the stack.
  963. Check if there are any, and do them. */
  964. --stkptr;
  965. if (stkptr >= 0) {
  966. lo = lostk[stkptr];
  967. hi = histk[stkptr];
  968. goto recurse; /* pop subarray from stack */
  969. }
  970. else
  971. return; /* all subarrays done */
  972. }
  973. //==========================================================================================
  974. /*
  975. =================
  976. R_AddDrawSurf
  977. =================
  978. */
  979. void R_AddDrawSurf( surfaceType_t *surface, shader_t *shader,
  980. int fogIndex, int dlightMap ) {
  981. int index;
  982. // instead of checking for overflow, we just mask the index
  983. // so it wraps around
  984. index = tr.refdef.numDrawSurfs & DRAWSURF_MASK;
  985. // the sort data is packed into a single 32 bit value so it can be
  986. // compared quickly during the qsorting process
  987. tr.refdef.drawSurfs[index].sort = (shader->sortedIndex << QSORT_SHADERNUM_SHIFT)
  988. | tr.shiftedEntityNum | ( fogIndex << QSORT_FOGNUM_SHIFT ) | (int)dlightMap;
  989. tr.refdef.drawSurfs[index].surface = surface;
  990. tr.refdef.numDrawSurfs++;
  991. }
  992. /*
  993. =================
  994. R_DecomposeSort
  995. =================
  996. */
  997. void R_DecomposeSort( unsigned sort, int *entityNum, shader_t **shader,
  998. int *fogNum, int *dlightMap ) {
  999. *fogNum = ( sort >> QSORT_FOGNUM_SHIFT ) & 31;
  1000. *shader = tr.sortedShaders[ ( sort >> QSORT_SHADERNUM_SHIFT ) & (MAX_SHADERS-1) ];
  1001. *entityNum = ( sort >> QSORT_ENTITYNUM_SHIFT ) & 1023;
  1002. *dlightMap = sort & 3;
  1003. }
  1004. /*
  1005. =================
  1006. R_SortDrawSurfs
  1007. =================
  1008. */
  1009. void R_SortDrawSurfs( drawSurf_t *drawSurfs, int numDrawSurfs ) {
  1010. shader_t *shader;
  1011. int fogNum;
  1012. int entityNum;
  1013. int dlighted;
  1014. int i;
  1015. // it is possible for some views to not have any surfaces
  1016. if ( numDrawSurfs < 1 ) {
  1017. // we still need to add it for hyperspace cases
  1018. R_AddDrawSurfCmd( drawSurfs, numDrawSurfs );
  1019. return;
  1020. }
  1021. // if we overflowed MAX_DRAWSURFS, the drawsurfs
  1022. // wrapped around in the buffer and we will be missing
  1023. // the first surfaces, not the last ones
  1024. if ( numDrawSurfs > MAX_DRAWSURFS ) {
  1025. numDrawSurfs = MAX_DRAWSURFS;
  1026. }
  1027. // sort the drawsurfs by sort type, then orientation, then shader
  1028. qsortFast (drawSurfs, numDrawSurfs, sizeof(drawSurf_t) );
  1029. // check for any pass through drawing, which
  1030. // may cause another view to be rendered first
  1031. for ( i = 0 ; i < numDrawSurfs ; i++ ) {
  1032. R_DecomposeSort( (drawSurfs+i)->sort, &entityNum, &shader, &fogNum, &dlighted );
  1033. if ( shader->sort > SS_PORTAL ) {
  1034. break;
  1035. }
  1036. // no shader should ever have this sort type
  1037. if ( shader->sort == SS_BAD ) {
  1038. ri.Error (ERR_DROP, "Shader '%s'with sort == SS_BAD", shader->name );
  1039. }
  1040. // if the mirror was completely clipped away, we may need to check another surface
  1041. if ( R_MirrorViewBySurface( (drawSurfs+i), entityNum) ) {
  1042. // this is a debug option to see exactly what is being mirrored
  1043. if ( r_portalOnly->integer ) {
  1044. return;
  1045. }
  1046. break; // only one mirror view at a time
  1047. }
  1048. }
  1049. R_AddDrawSurfCmd( drawSurfs, numDrawSurfs );
  1050. }
  1051. /*
  1052. =============
  1053. R_AddEntitySurfaces
  1054. =============
  1055. */
  1056. void R_AddEntitySurfaces (void) {
  1057. trRefEntity_t *ent;
  1058. shader_t *shader;
  1059. if ( !r_drawentities->integer ) {
  1060. return;
  1061. }
  1062. for ( tr.currentEntityNum = 0;
  1063. tr.currentEntityNum < tr.refdef.num_entities;
  1064. tr.currentEntityNum++ ) {
  1065. ent = tr.currentEntity = &tr.refdef.entities[tr.currentEntityNum];
  1066. ent->needDlights = qfalse;
  1067. // preshift the value we are going to OR into the drawsurf sort
  1068. tr.shiftedEntityNum = tr.currentEntityNum << QSORT_ENTITYNUM_SHIFT;
  1069. //
  1070. // the weapon model must be handled special --
  1071. // we don't want the hacked weapon position showing in
  1072. // mirrors, because the true body position will already be drawn
  1073. //
  1074. if ( (ent->e.renderfx & RF_FIRST_PERSON) && tr.viewParms.isPortal) {
  1075. continue;
  1076. }
  1077. // simple generated models, like sprites and beams, are not culled
  1078. switch ( ent->e.reType ) {
  1079. case RT_PORTALSURFACE:
  1080. break; // don't draw anything
  1081. case RT_SPRITE:
  1082. case RT_BEAM:
  1083. case RT_LIGHTNING:
  1084. case RT_RAIL_CORE:
  1085. case RT_RAIL_RINGS:
  1086. // self blood sprites, talk balloons, etc should not be drawn in the primary
  1087. // view. We can't just do this check for all entities, because md3
  1088. // entities may still want to cast shadows from them
  1089. if ( (ent->e.renderfx & RF_THIRD_PERSON) && !tr.viewParms.isPortal) {
  1090. continue;
  1091. }
  1092. shader = R_GetShaderByHandle( ent->e.customShader );
  1093. R_AddDrawSurf( &entitySurface, shader, R_SpriteFogNum( ent ), 0 );
  1094. break;
  1095. case RT_MODEL:
  1096. // we must set up parts of tr.or for model culling
  1097. R_RotateForEntity( ent, &tr.viewParms, &tr.or );
  1098. tr.currentModel = R_GetModelByHandle( ent->e.hModel );
  1099. if (!tr.currentModel) {
  1100. R_AddDrawSurf( &entitySurface, tr.defaultShader, 0, 0 );
  1101. } else {
  1102. switch ( tr.currentModel->type ) {
  1103. case MOD_MESH:
  1104. R_AddMD3Surfaces( ent );
  1105. break;
  1106. case MOD_MD4:
  1107. R_AddAnimSurfaces( ent );
  1108. break;
  1109. case MOD_BRUSH:
  1110. R_AddBrushModelSurfaces( ent );
  1111. break;
  1112. case MOD_BAD: // null model axis
  1113. if ( (ent->e.renderfx & RF_THIRD_PERSON) && !tr.viewParms.isPortal) {
  1114. break;
  1115. }
  1116. shader = R_GetShaderByHandle( ent->e.customShader );
  1117. R_AddDrawSurf( &entitySurface, tr.defaultShader, 0, 0 );
  1118. break;
  1119. default:
  1120. ri.Error( ERR_DROP, "R_AddEntitySurfaces: Bad modeltype" );
  1121. break;
  1122. }
  1123. }
  1124. break;
  1125. default:
  1126. ri.Error( ERR_DROP, "R_AddEntitySurfaces: Bad reType" );
  1127. }
  1128. }
  1129. }
  1130. /*
  1131. ====================
  1132. R_GenerateDrawSurfs
  1133. ====================
  1134. */
  1135. void R_GenerateDrawSurfs( void ) {
  1136. R_AddWorldSurfaces ();
  1137. R_AddPolygonSurfaces();
  1138. // set the projection matrix with the minimum zfar
  1139. // now that we have the world bounded
  1140. // this needs to be done before entities are
  1141. // added, because they use the projection
  1142. // matrix for lod calculation
  1143. R_SetupProjection ();
  1144. R_AddEntitySurfaces ();
  1145. }
  1146. /*
  1147. ================
  1148. R_DebugPolygon
  1149. ================
  1150. */
  1151. void R_DebugPolygon( int color, int numPoints, float *points ) {
  1152. int i;
  1153. GL_State( GLS_DEPTHMASK_TRUE | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE );
  1154. // draw solid shade
  1155. qglColor3f( color&1, (color>>1)&1, (color>>2)&1 );
  1156. qglBegin( GL_POLYGON );
  1157. for ( i = 0 ; i < numPoints ; i++ ) {
  1158. qglVertex3fv( points + i * 3 );
  1159. }
  1160. qglEnd();
  1161. // draw wireframe outline
  1162. GL_State( GLS_POLYMODE_LINE | GLS_DEPTHMASK_TRUE | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE );
  1163. qglDepthRange( 0, 0 );
  1164. qglColor3f( 1, 1, 1 );
  1165. qglBegin( GL_POLYGON );
  1166. for ( i = 0 ; i < numPoints ; i++ ) {
  1167. qglVertex3fv( points + i * 3 );
  1168. }
  1169. qglEnd();
  1170. qglDepthRange( 0, 1 );
  1171. }
  1172. /*
  1173. ====================
  1174. R_DebugGraphics
  1175. Visualization aid for movement clipping debugging
  1176. ====================
  1177. */
  1178. void R_DebugGraphics( void ) {
  1179. if ( !r_debugSurface->integer ) {
  1180. return;
  1181. }
  1182. // the render thread can't make callbacks to the main thread
  1183. R_SyncRenderThread();
  1184. GL_Bind( tr.whiteImage);
  1185. GL_Cull( CT_FRONT_SIDED );
  1186. ri.CM_DrawDebugSurface( R_DebugPolygon );
  1187. }
  1188. /*
  1189. ================
  1190. R_RenderView
  1191. A view may be either the actual camera view,
  1192. or a mirror / remote location
  1193. ================
  1194. */
  1195. void R_RenderView (viewParms_t *parms) {
  1196. int firstDrawSurf;
  1197. if ( parms->viewportWidth <= 0 || parms->viewportHeight <= 0 ) {
  1198. return;
  1199. }
  1200. tr.viewCount++;
  1201. tr.viewParms = *parms;
  1202. tr.viewParms.frameSceneNum = tr.frameSceneNum;
  1203. tr.viewParms.frameCount = tr.frameCount;
  1204. firstDrawSurf = tr.refdef.numDrawSurfs;
  1205. tr.viewCount++;
  1206. // set viewParms.world
  1207. R_RotateForViewer ();
  1208. R_SetupFrustum ();
  1209. R_GenerateDrawSurfs();
  1210. R_SortDrawSurfs( tr.refdef.drawSurfs + firstDrawSurf, tr.refdef.numDrawSurfs - firstDrawSurf );
  1211. // draw main system development information (surface outlines, etc)
  1212. R_DebugGraphics();
  1213. }