tr_render.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. /*
  24. back end scene + lights rendering functions
  25. */
  26. /*
  27. =================
  28. RB_DrawElementsImmediate
  29. Draws with immediate mode commands, which is going to be very slow.
  30. This should never happen if the vertex cache is operating properly.
  31. =================
  32. */
  33. void RB_DrawElementsImmediate( const srfTriangles_t *tri ) {
  34. backEnd.pc.c_drawElements++;
  35. backEnd.pc.c_drawIndexes += tri->numIndexes;
  36. backEnd.pc.c_drawVertexes += tri->numVerts;
  37. if ( tri->ambientSurface != NULL ) {
  38. if ( tri->indexes == tri->ambientSurface->indexes ) {
  39. backEnd.pc.c_drawRefIndexes += tri->numIndexes;
  40. }
  41. if ( tri->verts == tri->ambientSurface->verts ) {
  42. backEnd.pc.c_drawRefVertexes += tri->numVerts;
  43. }
  44. }
  45. qglBegin( GL_TRIANGLES );
  46. for ( int i = 0 ; i < tri->numIndexes ; i++ ) {
  47. qglTexCoord2fv( tri->verts[ tri->indexes[i] ].st.ToFloatPtr() );
  48. qglVertex3fv( tri->verts[ tri->indexes[i] ].xyz.ToFloatPtr() );
  49. }
  50. qglEnd();
  51. }
  52. /*
  53. ================
  54. RB_DrawElementsWithCounters
  55. ================
  56. */
  57. void RB_DrawElementsWithCounters( const srfTriangles_t *tri ) {
  58. backEnd.pc.c_drawElements++;
  59. backEnd.pc.c_drawIndexes += tri->numIndexes;
  60. backEnd.pc.c_drawVertexes += tri->numVerts;
  61. if ( tri->ambientSurface != NULL ) {
  62. if ( tri->indexes == tri->ambientSurface->indexes ) {
  63. backEnd.pc.c_drawRefIndexes += tri->numIndexes;
  64. }
  65. if ( tri->verts == tri->ambientSurface->verts ) {
  66. backEnd.pc.c_drawRefVertexes += tri->numVerts;
  67. }
  68. }
  69. if ( tri->indexCache && r_useIndexBuffers.GetBool() ) {
  70. qglDrawElements( GL_TRIANGLES,
  71. r_singleTriangle.GetBool() ? 3 : tri->numIndexes,
  72. GL_INDEX_TYPE,
  73. (int *)vertexCache.Position( tri->indexCache ) );
  74. backEnd.pc.c_vboIndexes += tri->numIndexes;
  75. } else {
  76. if ( r_useIndexBuffers.GetBool() ) {
  77. vertexCache.UnbindIndex();
  78. }
  79. qglDrawElements( GL_TRIANGLES,
  80. r_singleTriangle.GetBool() ? 3 : tri->numIndexes,
  81. GL_INDEX_TYPE,
  82. tri->indexes );
  83. }
  84. }
  85. /*
  86. ================
  87. RB_DrawShadowElementsWithCounters
  88. May not use all the indexes in the surface if caps are skipped
  89. ================
  90. */
  91. void RB_DrawShadowElementsWithCounters( const srfTriangles_t *tri, int numIndexes ) {
  92. backEnd.pc.c_shadowElements++;
  93. backEnd.pc.c_shadowIndexes += numIndexes;
  94. backEnd.pc.c_shadowVertexes += tri->numVerts;
  95. if ( tri->indexCache && r_useIndexBuffers.GetBool() ) {
  96. qglDrawElements( GL_TRIANGLES,
  97. r_singleTriangle.GetBool() ? 3 : numIndexes,
  98. GL_INDEX_TYPE,
  99. (int *)vertexCache.Position( tri->indexCache ) );
  100. backEnd.pc.c_vboIndexes += numIndexes;
  101. } else {
  102. if ( r_useIndexBuffers.GetBool() ) {
  103. vertexCache.UnbindIndex();
  104. }
  105. qglDrawElements( GL_TRIANGLES,
  106. r_singleTriangle.GetBool() ? 3 : numIndexes,
  107. GL_INDEX_TYPE,
  108. tri->indexes );
  109. }
  110. }
  111. /*
  112. ===============
  113. RB_RenderTriangleSurface
  114. Sets texcoord and vertex pointers
  115. ===============
  116. */
  117. void RB_RenderTriangleSurface( const srfTriangles_t *tri ) {
  118. if ( !tri->ambientCache ) {
  119. RB_DrawElementsImmediate( tri );
  120. return;
  121. }
  122. idDrawVert *ac = (idDrawVert *)vertexCache.Position( tri->ambientCache );
  123. qglVertexPointer( 3, GL_FLOAT, sizeof( idDrawVert ), ac->xyz.ToFloatPtr() );
  124. qglTexCoordPointer( 2, GL_FLOAT, sizeof( idDrawVert ), ac->st.ToFloatPtr() );
  125. RB_DrawElementsWithCounters( tri );
  126. }
  127. /*
  128. ===============
  129. RB_T_RenderTriangleSurface
  130. ===============
  131. */
  132. void RB_T_RenderTriangleSurface( const drawSurf_t *surf ) {
  133. RB_RenderTriangleSurface( surf->geo );
  134. }
  135. /*
  136. ===============
  137. RB_EnterWeaponDepthHack
  138. ===============
  139. */
  140. void RB_EnterWeaponDepthHack() {
  141. qglDepthRange( 0, 0.5 );
  142. float matrix[16];
  143. memcpy( matrix, backEnd.viewDef->projectionMatrix, sizeof( matrix ) );
  144. matrix[14] *= 0.25;
  145. qglMatrixMode(GL_PROJECTION);
  146. qglLoadMatrixf( matrix );
  147. qglMatrixMode(GL_MODELVIEW);
  148. }
  149. /*
  150. ===============
  151. RB_EnterModelDepthHack
  152. ===============
  153. */
  154. void RB_EnterModelDepthHack( float depth ) {
  155. qglDepthRange( 0.0f, 1.0f );
  156. float matrix[16];
  157. memcpy( matrix, backEnd.viewDef->projectionMatrix, sizeof( matrix ) );
  158. matrix[14] -= depth;
  159. qglMatrixMode(GL_PROJECTION);
  160. qglLoadMatrixf( matrix );
  161. qglMatrixMode(GL_MODELVIEW);
  162. }
  163. /*
  164. ===============
  165. RB_LeaveDepthHack
  166. ===============
  167. */
  168. void RB_LeaveDepthHack() {
  169. qglDepthRange( 0, 1 );
  170. qglMatrixMode(GL_PROJECTION);
  171. qglLoadMatrixf( backEnd.viewDef->projectionMatrix );
  172. qglMatrixMode(GL_MODELVIEW);
  173. }
  174. /*
  175. ====================
  176. RB_RenderDrawSurfListWithFunction
  177. The triangle functions can check backEnd.currentSpace != surf->space
  178. to see if they need to perform any new matrix setup. The modelview
  179. matrix will already have been loaded, and backEnd.currentSpace will
  180. be updated after the triangle function completes.
  181. ====================
  182. */
  183. void RB_RenderDrawSurfListWithFunction( drawSurf_t **drawSurfs, int numDrawSurfs,
  184. void (*triFunc_)( const drawSurf_t *) ) {
  185. int i;
  186. const drawSurf_t *drawSurf;
  187. backEnd.currentSpace = NULL;
  188. for (i = 0 ; i < numDrawSurfs ; i++ ) {
  189. drawSurf = drawSurfs[i];
  190. // change the matrix if needed
  191. if ( drawSurf->space != backEnd.currentSpace ) {
  192. qglLoadMatrixf( drawSurf->space->modelViewMatrix );
  193. }
  194. if ( drawSurf->space->weaponDepthHack ) {
  195. RB_EnterWeaponDepthHack();
  196. }
  197. if ( drawSurf->space->modelDepthHack != 0.0f ) {
  198. RB_EnterModelDepthHack( drawSurf->space->modelDepthHack );
  199. }
  200. // change the scissor if needed
  201. if ( r_useScissor.GetBool() && !backEnd.currentScissor.Equals( drawSurf->scissorRect ) ) {
  202. backEnd.currentScissor = drawSurf->scissorRect;
  203. qglScissor( backEnd.viewDef->viewport.x1 + backEnd.currentScissor.x1,
  204. backEnd.viewDef->viewport.y1 + backEnd.currentScissor.y1,
  205. backEnd.currentScissor.x2 + 1 - backEnd.currentScissor.x1,
  206. backEnd.currentScissor.y2 + 1 - backEnd.currentScissor.y1 );
  207. }
  208. // render it
  209. triFunc_( drawSurf );
  210. if ( drawSurf->space->weaponDepthHack || drawSurf->space->modelDepthHack != 0.0f ) {
  211. RB_LeaveDepthHack();
  212. }
  213. backEnd.currentSpace = drawSurf->space;
  214. }
  215. }
  216. /*
  217. ======================
  218. RB_RenderDrawSurfChainWithFunction
  219. ======================
  220. */
  221. void RB_RenderDrawSurfChainWithFunction( const drawSurf_t *drawSurfs,
  222. void (*triFunc_)( const drawSurf_t *) ) {
  223. const drawSurf_t *drawSurf;
  224. backEnd.currentSpace = NULL;
  225. for ( drawSurf = drawSurfs ; drawSurf ; drawSurf = drawSurf->nextOnLight ) {
  226. // change the matrix if needed
  227. if ( drawSurf->space != backEnd.currentSpace ) {
  228. qglLoadMatrixf( drawSurf->space->modelViewMatrix );
  229. }
  230. if ( drawSurf->space->weaponDepthHack ) {
  231. RB_EnterWeaponDepthHack();
  232. }
  233. if ( drawSurf->space->modelDepthHack ) {
  234. RB_EnterModelDepthHack( drawSurf->space->modelDepthHack );
  235. }
  236. // change the scissor if needed
  237. if ( r_useScissor.GetBool() && !backEnd.currentScissor.Equals( drawSurf->scissorRect ) ) {
  238. backEnd.currentScissor = drawSurf->scissorRect;
  239. qglScissor( backEnd.viewDef->viewport.x1 + backEnd.currentScissor.x1,
  240. backEnd.viewDef->viewport.y1 + backEnd.currentScissor.y1,
  241. backEnd.currentScissor.x2 + 1 - backEnd.currentScissor.x1,
  242. backEnd.currentScissor.y2 + 1 - backEnd.currentScissor.y1 );
  243. }
  244. // render it
  245. triFunc_( drawSurf );
  246. if ( drawSurf->space->weaponDepthHack || drawSurf->space->modelDepthHack != 0.0f ) {
  247. RB_LeaveDepthHack();
  248. }
  249. backEnd.currentSpace = drawSurf->space;
  250. }
  251. }
  252. /*
  253. ======================
  254. RB_GetShaderTextureMatrix
  255. ======================
  256. */
  257. void RB_GetShaderTextureMatrix( const float *shaderRegisters,
  258. const textureStage_t *texture, float matrix[16] ) {
  259. matrix[0] = shaderRegisters[ texture->matrix[0][0] ];
  260. matrix[4] = shaderRegisters[ texture->matrix[0][1] ];
  261. matrix[8] = 0;
  262. matrix[12] = shaderRegisters[ texture->matrix[0][2] ];
  263. // we attempt to keep scrolls from generating incredibly large texture values, but
  264. // center rotations and center scales can still generate offsets that need to be > 1
  265. if ( matrix[12] < -40 || matrix[12] > 40 ) {
  266. matrix[12] -= (int)matrix[12];
  267. }
  268. matrix[1] = shaderRegisters[ texture->matrix[1][0] ];
  269. matrix[5] = shaderRegisters[ texture->matrix[1][1] ];
  270. matrix[9] = 0;
  271. matrix[13] = shaderRegisters[ texture->matrix[1][2] ];
  272. if ( matrix[13] < -40 || matrix[13] > 40 ) {
  273. matrix[13] -= (int)matrix[13];
  274. }
  275. matrix[2] = 0;
  276. matrix[6] = 0;
  277. matrix[10] = 1;
  278. matrix[14] = 0;
  279. matrix[3] = 0;
  280. matrix[7] = 0;
  281. matrix[11] = 0;
  282. matrix[15] = 1;
  283. }
  284. /*
  285. ======================
  286. RB_LoadShaderTextureMatrix
  287. ======================
  288. */
  289. void RB_LoadShaderTextureMatrix( const float *shaderRegisters, const textureStage_t *texture ) {
  290. float matrix[16];
  291. RB_GetShaderTextureMatrix( shaderRegisters, texture, matrix );
  292. qglMatrixMode( GL_TEXTURE );
  293. qglLoadMatrixf( matrix );
  294. qglMatrixMode( GL_MODELVIEW );
  295. }
  296. /*
  297. ======================
  298. RB_BindVariableStageImage
  299. Handles generating a cinematic frame if needed
  300. ======================
  301. */
  302. void RB_BindVariableStageImage( const textureStage_t *texture, const float *shaderRegisters ) {
  303. if ( texture->cinematic ) {
  304. cinData_t cin;
  305. if ( r_skipDynamicTextures.GetBool() ) {
  306. globalImages->defaultImage->Bind();
  307. return;
  308. }
  309. // offset time by shaderParm[7] (FIXME: make the time offset a parameter of the shader?)
  310. // We make no attempt to optimize for multiple identical cinematics being in view, or
  311. // for cinematics going at a lower framerate than the renderer.
  312. cin = texture->cinematic->ImageForTime( (int)(1000 * ( backEnd.viewDef->floatTime + backEnd.viewDef->renderView.shaderParms[11] ) ) );
  313. if ( cin.image ) {
  314. globalImages->cinematicImage->UploadScratch( cin.image, cin.imageWidth, cin.imageHeight );
  315. } else {
  316. globalImages->blackImage->Bind();
  317. }
  318. } else {
  319. //FIXME: see why image is invalid
  320. if (texture->image) {
  321. texture->image->Bind();
  322. }
  323. }
  324. }
  325. /*
  326. ======================
  327. RB_BindStageTexture
  328. ======================
  329. */
  330. void RB_BindStageTexture( const float *shaderRegisters, const textureStage_t *texture, const drawSurf_t *surf ) {
  331. // image
  332. RB_BindVariableStageImage( texture, shaderRegisters );
  333. // texgens
  334. if ( texture->texgen == TG_DIFFUSE_CUBE ) {
  335. qglTexCoordPointer( 3, GL_FLOAT, sizeof( idDrawVert ), ((idDrawVert *)vertexCache.Position( surf->geo->ambientCache ))->normal.ToFloatPtr() );
  336. }
  337. if ( texture->texgen == TG_SKYBOX_CUBE || texture->texgen == TG_WOBBLESKY_CUBE ) {
  338. qglTexCoordPointer( 3, GL_FLOAT, 0, vertexCache.Position( surf->dynamicTexCoords ) );
  339. }
  340. if ( texture->texgen == TG_REFLECT_CUBE ) {
  341. qglEnable( GL_TEXTURE_GEN_S );
  342. qglEnable( GL_TEXTURE_GEN_T );
  343. qglEnable( GL_TEXTURE_GEN_R );
  344. qglTexGenf( GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT );
  345. qglTexGenf( GL_T, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT );
  346. qglTexGenf( GL_R, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP_EXT );
  347. qglEnableClientState( GL_NORMAL_ARRAY );
  348. qglNormalPointer( GL_FLOAT, sizeof( idDrawVert ), ((idDrawVert *)vertexCache.Position( surf->geo->ambientCache ))->normal.ToFloatPtr() );
  349. qglMatrixMode( GL_TEXTURE );
  350. float mat[16];
  351. R_TransposeGLMatrix( backEnd.viewDef->worldSpace.modelViewMatrix, mat );
  352. qglLoadMatrixf( mat );
  353. qglMatrixMode( GL_MODELVIEW );
  354. }
  355. // matrix
  356. if ( texture->hasMatrix ) {
  357. RB_LoadShaderTextureMatrix( shaderRegisters, texture );
  358. }
  359. }
  360. /*
  361. ======================
  362. RB_FinishStageTexture
  363. ======================
  364. */
  365. void RB_FinishStageTexture( const textureStage_t *texture, const drawSurf_t *surf ) {
  366. if ( texture->texgen == TG_DIFFUSE_CUBE || texture->texgen == TG_SKYBOX_CUBE
  367. || texture->texgen == TG_WOBBLESKY_CUBE ) {
  368. qglTexCoordPointer( 2, GL_FLOAT, sizeof( idDrawVert ),
  369. (void *)&(((idDrawVert *)vertexCache.Position( surf->geo->ambientCache ))->st) );
  370. }
  371. if ( texture->texgen == TG_REFLECT_CUBE ) {
  372. qglDisable( GL_TEXTURE_GEN_S );
  373. qglDisable( GL_TEXTURE_GEN_T );
  374. qglDisable( GL_TEXTURE_GEN_R );
  375. qglTexGenf( GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
  376. qglTexGenf( GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
  377. qglTexGenf( GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR );
  378. qglDisableClientState( GL_NORMAL_ARRAY );
  379. qglMatrixMode( GL_TEXTURE );
  380. qglLoadIdentity();
  381. qglMatrixMode( GL_MODELVIEW );
  382. }
  383. if ( texture->hasMatrix ) {
  384. qglMatrixMode( GL_TEXTURE );
  385. qglLoadIdentity();
  386. qglMatrixMode( GL_MODELVIEW );
  387. }
  388. }
  389. //=============================================================================================
  390. /*
  391. =================
  392. RB_DetermineLightScale
  393. Sets:
  394. backEnd.lightScale
  395. backEnd.overBright
  396. Find out how much we are going to need to overscale the lighting, so we
  397. can down modulate the pre-lighting passes.
  398. We only look at light calculations, but an argument could be made that
  399. we should also look at surface evaluations, which would let surfaces
  400. overbright past 1.0
  401. =================
  402. */
  403. void RB_DetermineLightScale( void ) {
  404. viewLight_t *vLight;
  405. const idMaterial *shader;
  406. float max;
  407. int i, j, numStages;
  408. const shaderStage_t *stage;
  409. // the light scale will be based on the largest color component of any surface
  410. // that will be drawn.
  411. // should we consider separating rgb scales?
  412. // if there are no lights, this will remain at 1.0, so GUI-only
  413. // rendering will not lose any bits of precision
  414. max = 1.0;
  415. for ( vLight = backEnd.viewDef->viewLights ; vLight ; vLight = vLight->next ) {
  416. // lights with no surfaces or shaderparms may still be present
  417. // for debug display
  418. if ( !vLight->localInteractions && !vLight->globalInteractions
  419. && !vLight->translucentInteractions ) {
  420. continue;
  421. }
  422. shader = vLight->lightShader;
  423. numStages = shader->GetNumStages();
  424. for ( i = 0 ; i < numStages ; i++ ) {
  425. stage = shader->GetStage( i );
  426. for ( j = 0 ; j < 3 ; j++ ) {
  427. float v = r_lightScale.GetFloat() * vLight->shaderRegisters[ stage->color.registers[j] ];
  428. if ( v > max ) {
  429. max = v;
  430. }
  431. }
  432. }
  433. }
  434. backEnd.pc.maxLightValue = max;
  435. if ( max <= tr.backEndRendererMaxLight ) {
  436. backEnd.lightScale = r_lightScale.GetFloat();
  437. backEnd.overBright = 1.0;
  438. } else {
  439. backEnd.lightScale = r_lightScale.GetFloat() * tr.backEndRendererMaxLight / max;
  440. backEnd.overBright = max / tr.backEndRendererMaxLight;
  441. }
  442. }
  443. /*
  444. =================
  445. RB_BeginDrawingView
  446. Any mirrored or portaled views have already been drawn, so prepare
  447. to actually render the visible surfaces for this view
  448. =================
  449. */
  450. void RB_BeginDrawingView (void) {
  451. // set the modelview matrix for the viewer
  452. qglMatrixMode(GL_PROJECTION);
  453. qglLoadMatrixf( backEnd.viewDef->projectionMatrix );
  454. qglMatrixMode(GL_MODELVIEW);
  455. // set the window clipping
  456. qglViewport( tr.viewportOffset[0] + backEnd.viewDef->viewport.x1,
  457. tr.viewportOffset[1] + backEnd.viewDef->viewport.y1,
  458. backEnd.viewDef->viewport.x2 + 1 - backEnd.viewDef->viewport.x1,
  459. backEnd.viewDef->viewport.y2 + 1 - backEnd.viewDef->viewport.y1 );
  460. // the scissor may be smaller than the viewport for subviews
  461. qglScissor( tr.viewportOffset[0] + backEnd.viewDef->viewport.x1 + backEnd.viewDef->scissor.x1,
  462. tr.viewportOffset[1] + backEnd.viewDef->viewport.y1 + backEnd.viewDef->scissor.y1,
  463. backEnd.viewDef->scissor.x2 + 1 - backEnd.viewDef->scissor.x1,
  464. backEnd.viewDef->scissor.y2 + 1 - backEnd.viewDef->scissor.y1 );
  465. backEnd.currentScissor = backEnd.viewDef->scissor;
  466. // ensures that depth writes are enabled for the depth clear
  467. GL_State( GLS_DEFAULT );
  468. // we don't have to clear the depth / stencil buffer for 2D rendering
  469. if ( backEnd.viewDef->viewEntitys ) {
  470. qglStencilMask( 0xff );
  471. // some cards may have 7 bit stencil buffers, so don't assume this
  472. // should be 128
  473. qglClearStencil( 1<<(glConfig.stencilBits-1) );
  474. qglClear( GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
  475. qglEnable( GL_DEPTH_TEST );
  476. } else {
  477. qglDisable( GL_DEPTH_TEST );
  478. qglDisable( GL_STENCIL_TEST );
  479. }
  480. backEnd.glState.faceCulling = -1; // force face culling to set next time
  481. GL_Cull( CT_FRONT_SIDED );
  482. }
  483. /*
  484. ==================
  485. R_SetDrawInteractions
  486. ==================
  487. */
  488. void R_SetDrawInteraction( const shaderStage_t *surfaceStage, const float *surfaceRegs,
  489. idImage **image, idVec4 matrix[2], float color[4] ) {
  490. *image = surfaceStage->texture.image;
  491. if ( surfaceStage->texture.hasMatrix ) {
  492. matrix[0][0] = surfaceRegs[surfaceStage->texture.matrix[0][0]];
  493. matrix[0][1] = surfaceRegs[surfaceStage->texture.matrix[0][1]];
  494. matrix[0][2] = 0;
  495. matrix[0][3] = surfaceRegs[surfaceStage->texture.matrix[0][2]];
  496. matrix[1][0] = surfaceRegs[surfaceStage->texture.matrix[1][0]];
  497. matrix[1][1] = surfaceRegs[surfaceStage->texture.matrix[1][1]];
  498. matrix[1][2] = 0;
  499. matrix[1][3] = surfaceRegs[surfaceStage->texture.matrix[1][2]];
  500. // we attempt to keep scrolls from generating incredibly large texture values, but
  501. // center rotations and center scales can still generate offsets that need to be > 1
  502. if ( matrix[0][3] < -40 || matrix[0][3] > 40 ) {
  503. matrix[0][3] -= (int)matrix[0][3];
  504. }
  505. if ( matrix[1][3] < -40 || matrix[1][3] > 40 ) {
  506. matrix[1][3] -= (int)matrix[1][3];
  507. }
  508. } else {
  509. matrix[0][0] = 1;
  510. matrix[0][1] = 0;
  511. matrix[0][2] = 0;
  512. matrix[0][3] = 0;
  513. matrix[1][0] = 0;
  514. matrix[1][1] = 1;
  515. matrix[1][2] = 0;
  516. matrix[1][3] = 0;
  517. }
  518. if ( color ) {
  519. for ( int i = 0 ; i < 4 ; i++ ) {
  520. color[i] = surfaceRegs[surfaceStage->color.registers[i]];
  521. // clamp here, so card with greater range don't look different.
  522. // we could perform overbrighting like we do for lights, but
  523. // it doesn't currently look worth it.
  524. if ( color[i] < 0 ) {
  525. color[i] = 0;
  526. } else if ( color[i] > 1.0 ) {
  527. color[i] = 1.0;
  528. }
  529. }
  530. }
  531. }
  532. /*
  533. =================
  534. RB_SubmittInteraction
  535. =================
  536. */
  537. static void RB_SubmittInteraction( drawInteraction_t *din, void (*DrawInteraction)(const drawInteraction_t *) ) {
  538. if ( !din->bumpImage ) {
  539. return;
  540. }
  541. if ( !din->diffuseImage || r_skipDiffuse.GetBool() ) {
  542. din->diffuseImage = globalImages->blackImage;
  543. }
  544. if ( !din->specularImage || r_skipSpecular.GetBool() || din->ambientLight ) {
  545. din->specularImage = globalImages->blackImage;
  546. }
  547. if ( !din->bumpImage || r_skipBump.GetBool() ) {
  548. din->bumpImage = globalImages->flatNormalMap;
  549. }
  550. // if we wouldn't draw anything, don't call the Draw function
  551. if (
  552. ( ( din->diffuseColor[0] > 0 ||
  553. din->diffuseColor[1] > 0 ||
  554. din->diffuseColor[2] > 0 ) && din->diffuseImage != globalImages->blackImage )
  555. || ( ( din->specularColor[0] > 0 ||
  556. din->specularColor[1] > 0 ||
  557. din->specularColor[2] > 0 ) && din->specularImage != globalImages->blackImage ) ) {
  558. DrawInteraction( din );
  559. }
  560. }
  561. /*
  562. =============
  563. RB_CreateSingleDrawInteractions
  564. This can be used by different draw_* backends to decompose a complex light / surface
  565. interaction into primitive interactions
  566. =============
  567. */
  568. void RB_CreateSingleDrawInteractions( const drawSurf_t *surf, void (*DrawInteraction)(const drawInteraction_t *) ) {
  569. const idMaterial *surfaceShader = surf->material;
  570. const float *surfaceRegs = surf->shaderRegisters;
  571. const viewLight_t *vLight = backEnd.vLight;
  572. const idMaterial *lightShader = vLight->lightShader;
  573. const float *lightRegs = vLight->shaderRegisters;
  574. drawInteraction_t inter;
  575. if ( r_skipInteractions.GetBool() || !surf->geo || !surf->geo->ambientCache ) {
  576. return;
  577. }
  578. if ( tr.logFile ) {
  579. RB_LogComment( "---------- RB_CreateSingleDrawInteractions %s on %s ----------\n", lightShader->GetName(), surfaceShader->GetName() );
  580. }
  581. // change the matrix and light projection vectors if needed
  582. if ( surf->space != backEnd.currentSpace ) {
  583. backEnd.currentSpace = surf->space;
  584. qglLoadMatrixf( surf->space->modelViewMatrix );
  585. }
  586. // change the scissor if needed
  587. if ( r_useScissor.GetBool() && !backEnd.currentScissor.Equals( surf->scissorRect ) ) {
  588. backEnd.currentScissor = surf->scissorRect;
  589. qglScissor( backEnd.viewDef->viewport.x1 + backEnd.currentScissor.x1,
  590. backEnd.viewDef->viewport.y1 + backEnd.currentScissor.y1,
  591. backEnd.currentScissor.x2 + 1 - backEnd.currentScissor.x1,
  592. backEnd.currentScissor.y2 + 1 - backEnd.currentScissor.y1 );
  593. }
  594. // hack depth range if needed
  595. if ( surf->space->weaponDepthHack ) {
  596. RB_EnterWeaponDepthHack();
  597. }
  598. if ( surf->space->modelDepthHack ) {
  599. RB_EnterModelDepthHack( surf->space->modelDepthHack );
  600. }
  601. inter.surf = surf;
  602. inter.lightFalloffImage = vLight->falloffImage;
  603. R_GlobalPointToLocal( surf->space->modelMatrix, vLight->globalLightOrigin, inter.localLightOrigin.ToVec3() );
  604. R_GlobalPointToLocal( surf->space->modelMatrix, backEnd.viewDef->renderView.vieworg, inter.localViewOrigin.ToVec3() );
  605. inter.localLightOrigin[3] = 0;
  606. inter.localViewOrigin[3] = 1;
  607. inter.ambientLight = lightShader->IsAmbientLight();
  608. // the base projections may be modified by texture matrix on light stages
  609. idPlane lightProject[4];
  610. for ( int i = 0 ; i < 4 ; i++ ) {
  611. R_GlobalPlaneToLocal( surf->space->modelMatrix, backEnd.vLight->lightProject[i], lightProject[i] );
  612. }
  613. for ( int lightStageNum = 0 ; lightStageNum < lightShader->GetNumStages() ; lightStageNum++ ) {
  614. const shaderStage_t *lightStage = lightShader->GetStage( lightStageNum );
  615. // ignore stages that fail the condition
  616. if ( !lightRegs[ lightStage->conditionRegister ] ) {
  617. continue;
  618. }
  619. inter.lightImage = lightStage->texture.image;
  620. memcpy( inter.lightProjection, lightProject, sizeof( inter.lightProjection ) );
  621. // now multiply the texgen by the light texture matrix
  622. if ( lightStage->texture.hasMatrix ) {
  623. RB_GetShaderTextureMatrix( lightRegs, &lightStage->texture, backEnd.lightTextureMatrix );
  624. RB_BakeTextureMatrixIntoTexgen( reinterpret_cast<class idPlane *>(inter.lightProjection), backEnd.lightTextureMatrix );
  625. }
  626. inter.bumpImage = NULL;
  627. inter.specularImage = NULL;
  628. inter.diffuseImage = NULL;
  629. inter.diffuseColor[0] = inter.diffuseColor[1] = inter.diffuseColor[2] = inter.diffuseColor[3] = 0;
  630. inter.specularColor[0] = inter.specularColor[1] = inter.specularColor[2] = inter.specularColor[3] = 0;
  631. float lightColor[4];
  632. // backEnd.lightScale is calculated so that lightColor[] will never exceed
  633. // tr.backEndRendererMaxLight
  634. lightColor[0] = backEnd.lightScale * lightRegs[ lightStage->color.registers[0] ];
  635. lightColor[1] = backEnd.lightScale * lightRegs[ lightStage->color.registers[1] ];
  636. lightColor[2] = backEnd.lightScale * lightRegs[ lightStage->color.registers[2] ];
  637. lightColor[3] = lightRegs[ lightStage->color.registers[3] ];
  638. // go through the individual stages
  639. for ( int surfaceStageNum = 0 ; surfaceStageNum < surfaceShader->GetNumStages() ; surfaceStageNum++ ) {
  640. const shaderStage_t *surfaceStage = surfaceShader->GetStage( surfaceStageNum );
  641. switch( surfaceStage->lighting ) {
  642. case SL_AMBIENT: {
  643. // ignore ambient stages while drawing interactions
  644. break;
  645. }
  646. case SL_BUMP: {
  647. // ignore stage that fails the condition
  648. if ( !surfaceRegs[ surfaceStage->conditionRegister ] ) {
  649. break;
  650. }
  651. // draw any previous interaction
  652. RB_SubmittInteraction( &inter, DrawInteraction );
  653. inter.diffuseImage = NULL;
  654. inter.specularImage = NULL;
  655. R_SetDrawInteraction( surfaceStage, surfaceRegs, &inter.bumpImage, inter.bumpMatrix, NULL );
  656. break;
  657. }
  658. case SL_DIFFUSE: {
  659. // ignore stage that fails the condition
  660. if ( !surfaceRegs[ surfaceStage->conditionRegister ] ) {
  661. break;
  662. }
  663. if ( inter.diffuseImage ) {
  664. RB_SubmittInteraction( &inter, DrawInteraction );
  665. }
  666. R_SetDrawInteraction( surfaceStage, surfaceRegs, &inter.diffuseImage,
  667. inter.diffuseMatrix, inter.diffuseColor.ToFloatPtr() );
  668. inter.diffuseColor[0] *= lightColor[0];
  669. inter.diffuseColor[1] *= lightColor[1];
  670. inter.diffuseColor[2] *= lightColor[2];
  671. inter.diffuseColor[3] *= lightColor[3];
  672. inter.vertexColor = surfaceStage->vertexColor;
  673. break;
  674. }
  675. case SL_SPECULAR: {
  676. // ignore stage that fails the condition
  677. if ( !surfaceRegs[ surfaceStage->conditionRegister ] ) {
  678. break;
  679. }
  680. if ( inter.specularImage ) {
  681. RB_SubmittInteraction( &inter, DrawInteraction );
  682. }
  683. R_SetDrawInteraction( surfaceStage, surfaceRegs, &inter.specularImage,
  684. inter.specularMatrix, inter.specularColor.ToFloatPtr() );
  685. inter.specularColor[0] *= lightColor[0];
  686. inter.specularColor[1] *= lightColor[1];
  687. inter.specularColor[2] *= lightColor[2];
  688. inter.specularColor[3] *= lightColor[3];
  689. inter.vertexColor = surfaceStage->vertexColor;
  690. break;
  691. }
  692. }
  693. }
  694. // draw the final interaction
  695. RB_SubmittInteraction( &inter, DrawInteraction );
  696. }
  697. // unhack depth range if needed
  698. if ( surf->space->weaponDepthHack || surf->space->modelDepthHack != 0.0f ) {
  699. RB_LeaveDepthHack();
  700. }
  701. }
  702. /*
  703. =============
  704. RB_DrawView
  705. =============
  706. */
  707. void RB_DrawView( const void *data ) {
  708. const drawSurfsCommand_t *cmd;
  709. cmd = (const drawSurfsCommand_t *)data;
  710. backEnd.viewDef = cmd->viewDef;
  711. // we will need to do a new copyTexSubImage of the screen
  712. // when a SS_POST_PROCESS material is used
  713. backEnd.currentRenderCopied = false;
  714. // if there aren't any drawsurfs, do nothing
  715. if ( !backEnd.viewDef->numDrawSurfs ) {
  716. return;
  717. }
  718. // skip render bypasses everything that has models, assuming
  719. // them to be 3D views, but leaves 2D rendering visible
  720. if ( r_skipRender.GetBool() && backEnd.viewDef->viewEntitys ) {
  721. return;
  722. }
  723. // skip render context sets the wgl context to NULL,
  724. // which should factor out the API cost, under the assumption
  725. // that all gl calls just return if the context isn't valid
  726. if ( r_skipRenderContext.GetBool() && backEnd.viewDef->viewEntitys ) {
  727. GLimp_DeactivateContext();
  728. }
  729. backEnd.pc.c_surfaces += backEnd.viewDef->numDrawSurfs;
  730. RB_ShowOverdraw();
  731. // render the scene, jumping to the hardware specific interaction renderers
  732. RB_STD_DrawView();
  733. // restore the context for 2D drawing if we were stubbing it out
  734. if ( r_skipRenderContext.GetBool() && backEnd.viewDef->viewEntitys ) {
  735. GLimp_ActivateContext();
  736. RB_SetDefaultGLState();
  737. }
  738. }