tr_flares.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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_flares.c
  19. #include "tr_local.h"
  20. /*
  21. =============================================================================
  22. LIGHT FLARES
  23. A light flare is an effect that takes place inside the eye when bright light
  24. sources are visible. The size of the flare reletive to the screen is nearly
  25. constant, irrespective of distance, but the intensity should be proportional to the
  26. projected area of the light source.
  27. A surface that has been flagged as having a light flare will calculate the depth
  28. buffer value that it's midpoint should have when the surface is added.
  29. After all opaque surfaces have been rendered, the depth buffer is read back for
  30. each flare in view. If the point has not been obscured by a closer surface, the
  31. flare should be drawn.
  32. Surfaces that have a repeated texture should never be flagged as flaring, because
  33. there will only be a single flare added at the midpoint of the polygon.
  34. To prevent abrupt popping, the intensity of the flare is interpolated up and
  35. down as it changes visibility. This involves scene to scene state, unlike almost
  36. all other aspects of the renderer, and is complicated by the fact that a single
  37. frame may have multiple scenes.
  38. RB_RenderFlares() will be called once per view (twice in a mirrored scene, potentially
  39. up to five or more times in a frame with 3D status bar icons).
  40. =============================================================================
  41. */
  42. // flare states maintain visibility over multiple frames for fading
  43. // layers: view, mirror, menu
  44. typedef struct flare_s {
  45. struct flare_s *next; // for active chain
  46. int addedFrame;
  47. qboolean inPortal; // true if in a portal view of the scene
  48. int frameSceneNum;
  49. void *surface;
  50. int fogNum;
  51. int fadeTime;
  52. qboolean visible; // state of last test
  53. float drawIntensity; // may be non 0 even if !visible due to fading
  54. int windowX, windowY;
  55. float eyeZ;
  56. vec3_t color;
  57. } flare_t;
  58. #define MAX_FLARES 128
  59. flare_t r_flareStructs[MAX_FLARES];
  60. flare_t *r_activeFlares, *r_inactiveFlares;
  61. /*
  62. ==================
  63. R_ClearFlares
  64. ==================
  65. */
  66. void R_ClearFlares( void ) {
  67. int i;
  68. Com_Memset( r_flareStructs, 0, sizeof( r_flareStructs ) );
  69. r_activeFlares = NULL;
  70. r_inactiveFlares = NULL;
  71. for ( i = 0 ; i < MAX_FLARES ; i++ ) {
  72. r_flareStructs[i].next = r_inactiveFlares;
  73. r_inactiveFlares = &r_flareStructs[i];
  74. }
  75. }
  76. /*
  77. ==================
  78. RB_AddFlare
  79. This is called at surface tesselation time
  80. ==================
  81. */
  82. void RB_AddFlare( void *surface, int fogNum, vec3_t point, vec3_t color, vec3_t normal ) {
  83. int i;
  84. flare_t *f, *oldest;
  85. vec3_t local;
  86. float d;
  87. vec4_t eye, clip, normalized, window;
  88. backEnd.pc.c_flareAdds++;
  89. // if the point is off the screen, don't bother adding it
  90. // calculate screen coordinates and depth
  91. R_TransformModelToClip( point, backEnd.or.modelMatrix,
  92. backEnd.viewParms.projectionMatrix, eye, clip );
  93. // check to see if the point is completely off screen
  94. for ( i = 0 ; i < 3 ; i++ ) {
  95. if ( clip[i] >= clip[3] || clip[i] <= -clip[3] ) {
  96. return;
  97. }
  98. }
  99. R_TransformClipToWindow( clip, &backEnd.viewParms, normalized, window );
  100. if ( window[0] < 0 || window[0] >= backEnd.viewParms.viewportWidth
  101. || window[1] < 0 || window[1] >= backEnd.viewParms.viewportHeight ) {
  102. return; // shouldn't happen, since we check the clip[] above, except for FP rounding
  103. }
  104. // see if a flare with a matching surface, scene, and view exists
  105. oldest = r_flareStructs;
  106. for ( f = r_activeFlares ; f ; f = f->next ) {
  107. if ( f->surface == surface && f->frameSceneNum == backEnd.viewParms.frameSceneNum
  108. && f->inPortal == backEnd.viewParms.isPortal ) {
  109. break;
  110. }
  111. }
  112. // allocate a new one
  113. if (!f ) {
  114. if ( !r_inactiveFlares ) {
  115. // the list is completely full
  116. return;
  117. }
  118. f = r_inactiveFlares;
  119. r_inactiveFlares = r_inactiveFlares->next;
  120. f->next = r_activeFlares;
  121. r_activeFlares = f;
  122. f->surface = surface;
  123. f->frameSceneNum = backEnd.viewParms.frameSceneNum;
  124. f->inPortal = backEnd.viewParms.isPortal;
  125. f->addedFrame = -1;
  126. }
  127. if ( f->addedFrame != backEnd.viewParms.frameCount - 1 ) {
  128. f->visible = qfalse;
  129. f->fadeTime = backEnd.refdef.time - 2000;
  130. }
  131. f->addedFrame = backEnd.viewParms.frameCount;
  132. f->fogNum = fogNum;
  133. VectorCopy( color, f->color );
  134. // fade the intensity of the flare down as the
  135. // light surface turns away from the viewer
  136. if ( normal ) {
  137. VectorSubtract( backEnd.viewParms.or.origin, point, local );
  138. VectorNormalizeFast( local );
  139. d = DotProduct( local, normal );
  140. VectorScale( f->color, d, f->color );
  141. }
  142. // save info needed to test
  143. f->windowX = backEnd.viewParms.viewportX + window[0];
  144. f->windowY = backEnd.viewParms.viewportY + window[1];
  145. f->eyeZ = eye[2];
  146. }
  147. /*
  148. ==================
  149. RB_AddDlightFlares
  150. ==================
  151. */
  152. void RB_AddDlightFlares( void ) {
  153. dlight_t *l;
  154. int i, j, k;
  155. fog_t *fog;
  156. if ( !r_flares->integer ) {
  157. return;
  158. }
  159. l = backEnd.refdef.dlights;
  160. fog = tr.world->fogs;
  161. for (i=0 ; i<backEnd.refdef.num_dlights ; i++, l++) {
  162. // find which fog volume the light is in
  163. for ( j = 1 ; j < tr.world->numfogs ; j++ ) {
  164. fog = &tr.world->fogs[j];
  165. for ( k = 0 ; k < 3 ; k++ ) {
  166. if ( l->origin[k] < fog->bounds[0][k] || l->origin[k] > fog->bounds[1][k] ) {
  167. break;
  168. }
  169. }
  170. if ( k == 3 ) {
  171. break;
  172. }
  173. }
  174. if ( j == tr.world->numfogs ) {
  175. j = 0;
  176. }
  177. RB_AddFlare( (void *)l, j, l->origin, l->color, NULL );
  178. }
  179. }
  180. /*
  181. ===============================================================================
  182. FLARE BACK END
  183. ===============================================================================
  184. */
  185. /*
  186. ==================
  187. RB_TestFlare
  188. ==================
  189. */
  190. void RB_TestFlare( flare_t *f ) {
  191. float depth;
  192. qboolean visible;
  193. float fade;
  194. float screenZ;
  195. backEnd.pc.c_flareTests++;
  196. // doing a readpixels is as good as doing a glFinish(), so
  197. // don't bother with another sync
  198. glState.finishCalled = qfalse;
  199. // read back the z buffer contents
  200. qglReadPixels( f->windowX, f->windowY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth );
  201. screenZ = backEnd.viewParms.projectionMatrix[14] /
  202. ( ( 2*depth - 1 ) * backEnd.viewParms.projectionMatrix[11] - backEnd.viewParms.projectionMatrix[10] );
  203. visible = ( -f->eyeZ - -screenZ ) < 24;
  204. if ( visible ) {
  205. if ( !f->visible ) {
  206. f->visible = qtrue;
  207. f->fadeTime = backEnd.refdef.time - 1;
  208. }
  209. fade = ( ( backEnd.refdef.time - f->fadeTime ) /1000.0f ) * r_flareFade->value;
  210. } else {
  211. if ( f->visible ) {
  212. f->visible = qfalse;
  213. f->fadeTime = backEnd.refdef.time - 1;
  214. }
  215. fade = 1.0f - ( ( backEnd.refdef.time - f->fadeTime ) / 1000.0f ) * r_flareFade->value;
  216. }
  217. if ( fade < 0 ) {
  218. fade = 0;
  219. }
  220. if ( fade > 1 ) {
  221. fade = 1;
  222. }
  223. f->drawIntensity = fade;
  224. }
  225. /*
  226. ==================
  227. RB_RenderFlare
  228. ==================
  229. */
  230. void RB_RenderFlare( flare_t *f ) {
  231. float size;
  232. vec3_t color;
  233. int iColor[3];
  234. backEnd.pc.c_flareRenders++;
  235. VectorScale( f->color, f->drawIntensity*tr.identityLight, color );
  236. iColor[0] = color[0] * 255;
  237. iColor[1] = color[1] * 255;
  238. iColor[2] = color[2] * 255;
  239. size = backEnd.viewParms.viewportWidth * ( r_flareSize->value/640.0f + 8 / -f->eyeZ );
  240. RB_BeginSurface( tr.flareShader, f->fogNum );
  241. // FIXME: use quadstamp?
  242. tess.xyz[tess.numVertexes][0] = f->windowX - size;
  243. tess.xyz[tess.numVertexes][1] = f->windowY - size;
  244. tess.texCoords[tess.numVertexes][0][0] = 0;
  245. tess.texCoords[tess.numVertexes][0][1] = 0;
  246. tess.vertexColors[tess.numVertexes][0] = iColor[0];
  247. tess.vertexColors[tess.numVertexes][1] = iColor[1];
  248. tess.vertexColors[tess.numVertexes][2] = iColor[2];
  249. tess.vertexColors[tess.numVertexes][3] = 255;
  250. tess.numVertexes++;
  251. tess.xyz[tess.numVertexes][0] = f->windowX - size;
  252. tess.xyz[tess.numVertexes][1] = f->windowY + size;
  253. tess.texCoords[tess.numVertexes][0][0] = 0;
  254. tess.texCoords[tess.numVertexes][0][1] = 1;
  255. tess.vertexColors[tess.numVertexes][0] = iColor[0];
  256. tess.vertexColors[tess.numVertexes][1] = iColor[1];
  257. tess.vertexColors[tess.numVertexes][2] = iColor[2];
  258. tess.vertexColors[tess.numVertexes][3] = 255;
  259. tess.numVertexes++;
  260. tess.xyz[tess.numVertexes][0] = f->windowX + size;
  261. tess.xyz[tess.numVertexes][1] = f->windowY + size;
  262. tess.texCoords[tess.numVertexes][0][0] = 1;
  263. tess.texCoords[tess.numVertexes][0][1] = 1;
  264. tess.vertexColors[tess.numVertexes][0] = iColor[0];
  265. tess.vertexColors[tess.numVertexes][1] = iColor[1];
  266. tess.vertexColors[tess.numVertexes][2] = iColor[2];
  267. tess.vertexColors[tess.numVertexes][3] = 255;
  268. tess.numVertexes++;
  269. tess.xyz[tess.numVertexes][0] = f->windowX + size;
  270. tess.xyz[tess.numVertexes][1] = f->windowY - size;
  271. tess.texCoords[tess.numVertexes][0][0] = 1;
  272. tess.texCoords[tess.numVertexes][0][1] = 0;
  273. tess.vertexColors[tess.numVertexes][0] = iColor[0];
  274. tess.vertexColors[tess.numVertexes][1] = iColor[1];
  275. tess.vertexColors[tess.numVertexes][2] = iColor[2];
  276. tess.vertexColors[tess.numVertexes][3] = 255;
  277. tess.numVertexes++;
  278. tess.indexes[tess.numIndexes++] = 0;
  279. tess.indexes[tess.numIndexes++] = 1;
  280. tess.indexes[tess.numIndexes++] = 2;
  281. tess.indexes[tess.numIndexes++] = 0;
  282. tess.indexes[tess.numIndexes++] = 2;
  283. tess.indexes[tess.numIndexes++] = 3;
  284. RB_EndSurface();
  285. }
  286. /*
  287. ==================
  288. RB_RenderFlares
  289. Because flares are simulating an occular effect, they should be drawn after
  290. everything (all views) in the entire frame has been drawn.
  291. Because of the way portals use the depth buffer to mark off areas, the
  292. needed information would be lost after each view, so we are forced to draw
  293. flares after each view.
  294. The resulting artifact is that flares in mirrors or portals don't dim properly
  295. when occluded by something in the main view, and portal flares that should
  296. extend past the portal edge will be overwritten.
  297. ==================
  298. */
  299. void RB_RenderFlares (void) {
  300. flare_t *f;
  301. flare_t **prev;
  302. qboolean draw;
  303. if ( !r_flares->integer ) {
  304. return;
  305. }
  306. // RB_AddDlightFlares();
  307. // perform z buffer readback on each flare in this view
  308. draw = qfalse;
  309. prev = &r_activeFlares;
  310. while ( ( f = *prev ) != NULL ) {
  311. // throw out any flares that weren't added last frame
  312. if ( f->addedFrame < backEnd.viewParms.frameCount - 1 ) {
  313. *prev = f->next;
  314. f->next = r_inactiveFlares;
  315. r_inactiveFlares = f;
  316. continue;
  317. }
  318. // don't draw any here that aren't from this scene / portal
  319. f->drawIntensity = 0;
  320. if ( f->frameSceneNum == backEnd.viewParms.frameSceneNum
  321. && f->inPortal == backEnd.viewParms.isPortal ) {
  322. RB_TestFlare( f );
  323. if ( f->drawIntensity ) {
  324. draw = qtrue;
  325. } else {
  326. // this flare has completely faded out, so remove it from the chain
  327. *prev = f->next;
  328. f->next = r_inactiveFlares;
  329. r_inactiveFlares = f;
  330. continue;
  331. }
  332. }
  333. prev = &f->next;
  334. }
  335. if ( !draw ) {
  336. return; // none visible
  337. }
  338. if ( backEnd.viewParms.isPortal ) {
  339. qglDisable (GL_CLIP_PLANE0);
  340. }
  341. qglPushMatrix();
  342. qglLoadIdentity();
  343. qglMatrixMode( GL_PROJECTION );
  344. qglPushMatrix();
  345. qglLoadIdentity();
  346. qglOrtho( backEnd.viewParms.viewportX, backEnd.viewParms.viewportX + backEnd.viewParms.viewportWidth,
  347. backEnd.viewParms.viewportY, backEnd.viewParms.viewportY + backEnd.viewParms.viewportHeight,
  348. -99999, 99999 );
  349. for ( f = r_activeFlares ; f ; f = f->next ) {
  350. if ( f->frameSceneNum == backEnd.viewParms.frameSceneNum
  351. && f->inPortal == backEnd.viewParms.isPortal
  352. && f->drawIntensity ) {
  353. RB_RenderFlare( f );
  354. }
  355. }
  356. qglPopMatrix();
  357. qglMatrixMode( GL_MODELVIEW );
  358. qglPopMatrix();
  359. }