Debian Codegraph @

code/renderergl1/tr_flares.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22 // tr_flares.c
23
24 #include "tr_local.h"
25
26 /*
27 =============================================================================
28
29 LIGHT FLARES
30
31 A light flare is an effect that takes place inside the eye when bright light
32 sources are visible. The size of the flare relative to the screen is nearly
33 constant, irrespective of distance, but the intensity should be proportional to the
34 projected area of the light source.
35
36 A surface that has been flagged as having a light flare will calculate the depth
37 buffer value that its midpoint should have when the surface is added.
38
39 After all opaque surfaces have been rendered, the depth buffer is read back for
40 each flare in view. If the point has not been obscured by a closer surface, the
41 flare should be drawn.
42
43 Surfaces that have a repeated texture should never be flagged as flaring, because
44 there will only be a single flare added at the midpoint of the polygon.
45
46 To prevent abrupt popping, the intensity of the flare is interpolated up and
47 down as it changes visibility. This involves scene to scene state, unlike almost
48 all other aspects of the renderer, and is complicated by the fact that a single
49 frame may have multiple scenes.
50
51 RB_RenderFlares() will be called once per view (twice in a mirrored scene, potentially
52 up to five or more times in a frame with 3D status bar icons).
53
54 =============================================================================
55 */
56
57
58 // flare states maintain visibility over multiple frames for fading
59 // layers: view, mirror, menu
60 typedef struct flare_s {
61 struct flare_s *next; // for active chain
62
63 int addedFrame;
64
65 qboolean inPortal; // true if in a portal view of the scene
66 int frameSceneNum;
67 void *surface;
68 int fogNum;
69
70 int fadeTime;
71
72 qboolean visible; // state of last test
73 float drawIntensity; // may be non 0 even if !visible due to fading
74
75 int windowX, windowY;
76 float eyeZ;
77
78 vec3_t origin;
79 vec3_t color;
80 } flare_t;
81
82 #define MAX_FLARES 256
83
84 flare_t r_flareStructs[MAX_FLARES];
85 flare_t *r_activeFlares, *r_inactiveFlares;
86
87 int flareCoeff;
88
89 /*
90 ==================
91 R_SetFlareCoeff
92 ==================
93 */
94 static void R_SetFlareCoeff( void ) {
95
96 if(r_flareCoeff->value == 0.0f)
97 flareCoeff = atof(FLARE_STDCOEFF);
98 else
99 flareCoeff = r_flareCoeff->value;
100 }
101
102 /*
103 ==================
104 R_ClearFlares
105 ==================
106 */
107 void R_ClearFlares( void ) {
108 int i;
109
110 Com_Memset( r_flareStructs, 0, sizeof( r_flareStructs ) );
111 r_activeFlares = NULL;
112 r_inactiveFlares = NULL;
113
114 for ( i = 0 ; i < MAX_FLARES ; i++ ) {
115 r_flareStructs[i].next = r_inactiveFlares;
116 r_inactiveFlares = &r_flareStructs[i];
117 }
118
119 R_SetFlareCoeff();
120 }
121
122
123 /*
124 ==================
125 RB_AddFlare
126
127 This is called at surface tesselation time
128 ==================
129 */
130 void RB_AddFlare( void *surface, int fogNum, vec3_t point, vec3_t color, vec3_t normal ) {
131 int i;
132 flare_t *f;
133 vec3_t local;
134 float d = 1;
135 vec4_t eye, clip, normalized, window;
136
137 backEnd.pc.c_flareAdds++;
138
139 if(normal && (normal[0] || normal[1] || normal[2]))
140 {
141 VectorSubtract( backEnd.viewParms.or.origin, point, local );
142 VectorNormalizeFast(local);
143 d = DotProduct(local, normal);
144
145 // If the viewer is behind the flare don't add it.
146 if(d < 0)
147 return;
148 }
149
150 // if the point is off the screen, don't bother adding it
151 // calculate screen coordinates and depth
152 R_TransformModelToClip( point, backEnd.or.modelMatrix,
153 backEnd.viewParms.projectionMatrix, eye, clip );
154
155 // check to see if the point is completely off screen
156 for ( i = 0 ; i < 3 ; i++ ) {
157 if ( clip[i] >= clip[3] || clip[i] <= -clip[3] ) {
158 return;
159 }
160 }
161
162 R_TransformClipToWindow( clip, &backEnd.viewParms, normalized, window );
163
164 if ( window[0] < 0 || window[0] >= backEnd.viewParms.viewportWidth
165 || window[1] < 0 || window[1] >= backEnd.viewParms.viewportHeight ) {
166 return; // shouldn't happen, since we check the clip[] above, except for FP rounding
167 }
168
169 // see if a flare with a matching surface, scene, and view exists
170 for ( f = r_activeFlares ; f ; f = f->next ) {
171 if ( f->surface == surface && f->frameSceneNum == backEnd.viewParms.frameSceneNum
172 && f->inPortal == backEnd.viewParms.isPortal ) {
173 break;
174 }
175 }
176
177 // allocate a new one
178 if (!f ) {
179 if ( !r_inactiveFlares ) {
180 // the list is completely full
181 return;
182 }
183 f = r_inactiveFlares;
184 r_inactiveFlares = r_inactiveFlares->next;
185 f->next = r_activeFlares;
186 r_activeFlares = f;
187
188 f->surface = surface;
189 f->frameSceneNum = backEnd.viewParms.frameSceneNum;
190 f->inPortal = backEnd.viewParms.isPortal;
191 f->addedFrame = -1;
192 }
193
194 if ( f->addedFrame != backEnd.viewParms.frameCount - 1 ) {
195 f->visible = qfalse;
196 f->fadeTime = backEnd.refdef.time - 2000;
197 }
198
199 f->addedFrame = backEnd.viewParms.frameCount;
200 f->fogNum = fogNum;
201
202 VectorCopy(point, f->origin);
203 VectorCopy( color, f->color );
204
205 // fade the intensity of the flare down as the
206 // light surface turns away from the viewer
207 VectorScale( f->color, d, f->color );
208
209 // save info needed to test
210 f->windowX = backEnd.viewParms.viewportX + window[0];
211 f->windowY = backEnd.viewParms.viewportY + window[1];
212
213 f->eyeZ = eye[2];
214 }
215
216 /*
217 ==================
218 RB_AddDlightFlares
219 ==================
220 */
221 void RB_AddDlightFlares( void ) {
222 dlight_t *l;
223 int i, j, k;
224 fog_t *fog = NULL;
225
226 if ( !r_flares->integer ) {
227 return;
228 }
229
230 l = backEnd.refdef.dlights;
231
232 if(tr.world)
233 fog = tr.world->fogs;
234
235 for (i=0 ; i<backEnd.refdef.num_dlights ; i++, l++) {
236
237 if(fog)
238 {
239 // find which fog volume the light is in
240 for ( j = 1 ; j < tr.world->numfogs ; j++ ) {
241 fog = &tr.world->fogs[j];
242 for ( k = 0 ; k < 3 ; k++ ) {
243 if ( l->origin[k] < fog->bounds[0][k] || l->origin[k] > fog->bounds[1][k] ) {
244 break;
245 }
246 }
247 if ( k == 3 ) {
248 break;
249 }
250 }
251 if ( j == tr.world->numfogs ) {
252 j = 0;
253 }
254 }
255 else
256 j = 0;
257
258 RB_AddFlare( (void *)l, j, l->origin, l->color, NULL );
259 }
260 }
261
262 /*
263 ===============================================================================
264
265 FLARE BACK END
266
267 ===============================================================================
268 */
269
270 /*
271 ==================
272 RB_TestFlare
273 ==================
274 */
275 void RB_TestFlare( flare_t *f ) {
276 float depth;
277 qboolean visible;
278 float fade;
279 float screenZ;
280
281 backEnd.pc.c_flareTests++;
282
283 // doing a readpixels is as good as doing a glFinish(), so
284 // don't bother with another sync
285 glState.finishCalled = qfalse;
286
287 // read back the z buffer contents
288 qglReadPixels( f->windowX, f->windowY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth );
289
290 screenZ = backEnd.viewParms.projectionMatrix[14] /
291 ( ( 2*depth - 1 ) * backEnd.viewParms.projectionMatrix[11] - backEnd.viewParms.projectionMatrix[10] );
292
293 visible = ( -f->eyeZ - -screenZ ) < 24;
294
295 if ( visible ) {
296 if ( !f->visible ) {
297 f->visible = qtrue;
298 f->fadeTime = backEnd.refdef.time - 1;
299 }
300 fade = ( ( backEnd.refdef.time - f->fadeTime ) /1000.0f ) * r_flareFade->value;
301 } else {
302 if ( f->visible ) {
303 f->visible = qfalse;
304 f->fadeTime = backEnd.refdef.time - 1;
305 }
306 fade = 1.0f - ( ( backEnd.refdef.time - f->fadeTime ) / 1000.0f ) * r_flareFade->value;
307 }
308
309 if ( fade < 0 ) {
310 fade = 0;
311 }
312 if ( fade > 1 ) {
313 fade = 1;
314 }
315
316 f->drawIntensity = fade;
317 }
318
319
320 /*
321 ==================
322 RB_RenderFlare
323 ==================
324 */
325 void RB_RenderFlare( flare_t *f ) {
326 float size;
327 vec3_t color;
328 int iColor[3];
329 float distance, intensity, factor;
330 byte fogFactors[3] = {255, 255, 255};
331
332 backEnd.pc.c_flareRenders++;
333
334 // We don't want too big values anyways when dividing by distance.
335 if(f->eyeZ > -1.0f)
336 distance = 1.0f;
337 else
338 distance = -f->eyeZ;
339
340 // calculate the flare size..
341 size = backEnd.viewParms.viewportWidth * ( r_flareSize->value/640.0f + 8 / distance );
342
343 /*
344 * This is an alternative to intensity scaling. It changes the size of the flare on screen instead
345 * with growing distance. See in the description at the top why this is not the way to go.
346 // size will change ~ 1/r.
347 size = backEnd.viewParms.viewportWidth * (r_flareSize->value / (distance * -2.0f));
348 */
349
350 /*
351 * As flare sizes stay nearly constant with increasing distance we must decrease the intensity
352 * to achieve a reasonable visual result. The intensity is ~ (size^2 / distance^2) which can be
353 * got by considering the ratio of
354 * (flaresurface on screen) : (Surface of sphere defined by flare origin and distance from flare)
355 * An important requirement is:
356 * intensity <= 1 for all distances.
357 *
358 * The formula used here to compute the intensity is as follows:
359 * intensity = flareCoeff * size^2 / (distance + size*sqrt(flareCoeff))^2
360 * As you can see, the intensity will have a max. of 1 when the distance is 0.
361 * The coefficient flareCoeff will determine the falloff speed with increasing distance.
362 */
363
364 factor = distance + size * sqrt(flareCoeff);
365
366 intensity = flareCoeff * size * size / (factor * factor);
367
368 VectorScale(f->color, f->drawIntensity * intensity, color);
369
370 // Calculations for fogging
371 if(tr.world && f->fogNum > 0 && f->fogNum < tr.world->numfogs)
372 {
373 tess.numVertexes = 1;
374 VectorCopy(f->origin, tess.xyz[0]);
375 tess.fogNum = f->fogNum;
376
377 RB_CalcModulateColorsByFog(fogFactors);
378
379 // We don't need to render the flare if colors are 0 anyways.
380 if(!(fogFactors[0] || fogFactors[1] || fogFactors[2]))
381 return;
382 }
383
384 iColor[0] = color[0] * fogFactors[0];
385 iColor[1] = color[1] * fogFactors[1];
386 iColor[2] = color[2] * fogFactors[2];
387
388 RB_BeginSurface( tr.flareShader, f->fogNum );
389
390 // FIXME: use quadstamp?
391 tess.xyz[tess.numVertexes][0] = f->windowX - size;
392 tess.xyz[tess.numVertexes][1] = f->windowY - size;
393 tess.texCoords[tess.numVertexes][0][0] = 0;
394 tess.texCoords[tess.numVertexes][0][1] = 0;
395 tess.vertexColors[tess.numVertexes][0] = iColor[0];
396 tess.vertexColors[tess.numVertexes][1] = iColor[1];
397 tess.vertexColors[tess.numVertexes][2] = iColor[2];
398 tess.vertexColors[tess.numVertexes][3] = 255;
399 tess.numVertexes++;
400
401 tess.xyz[tess.numVertexes][0] = f->windowX - size;
402 tess.xyz[tess.numVertexes][1] = f->windowY + size;
403 tess.texCoords[tess.numVertexes][0][0] = 0;
404 tess.texCoords[tess.numVertexes][0][1] = 1;
405 tess.vertexColors[tess.numVertexes][0] = iColor[0];
406 tess.vertexColors[tess.numVertexes][1] = iColor[1];
407 tess.vertexColors[tess.numVertexes][2] = iColor[2];
408 tess.vertexColors[tess.numVertexes][3] = 255;
409 tess.numVertexes++;
410
411 tess.xyz[tess.numVertexes][0] = f->windowX + size;
412 tess.xyz[tess.numVertexes][1] = f->windowY + size;
413 tess.texCoords[tess.numVertexes][0][0] = 1;
414 tess.texCoords[tess.numVertexes][0][1] = 1;
415 tess.vertexColors[tess.numVertexes][0] = iColor[0];
416 tess.vertexColors[tess.numVertexes][1] = iColor[1];
417 tess.vertexColors[tess.numVertexes][2] = iColor[2];
418 tess.vertexColors[tess.numVertexes][3] = 255;
419 tess.numVertexes++;
420
421 tess.xyz[tess.numVertexes][0] = f->windowX + size;
422 tess.xyz[tess.numVertexes][1] = f->windowY - size;
423 tess.texCoords[tess.numVertexes][0][0] = 1;
424 tess.texCoords[tess.numVertexes][0][1] = 0;
425 tess.vertexColors[tess.numVertexes][0] = iColor[0];
426 tess.vertexColors[tess.numVertexes][1] = iColor[1];
427 tess.vertexColors[tess.numVertexes][2] = iColor[2];
428 tess.vertexColors[tess.numVertexes][3] = 255;
429 tess.numVertexes++;
430
431 tess.indexes[tess.numIndexes++] = 0;
432 tess.indexes[tess.numIndexes++] = 1;
433 tess.indexes[tess.numIndexes++] = 2;
434 tess.indexes[tess.numIndexes++] = 0;
435 tess.indexes[tess.numIndexes++] = 2;
436 tess.indexes[tess.numIndexes++] = 3;
437
438 RB_EndSurface();
439 }
440
441 /*
442 ==================
443 RB_RenderFlares
444
445 Because flares are simulating an occular effect, they should be drawn after
446 everything (all views) in the entire frame has been drawn.
447
448 Because of the way portals use the depth buffer to mark off areas, the
449 needed information would be lost after each view, so we are forced to draw
450 flares after each view.
451
452 The resulting artifact is that flares in mirrors or portals don't dim properly
453 when occluded by something in the main view, and portal flares that should
454 extend past the portal edge will be overwritten.
455 ==================
456 */
457 void RB_RenderFlares (void) {
458 flare_t *f;
459 flare_t **prev;
460 qboolean draw;
461
462 if ( !r_flares->integer ) {
463 return;
464 }
465
466 if(r_flareCoeff->modified)
467 {
468 R_SetFlareCoeff();
469 r_flareCoeff->modified = qfalse;
470 }
471
472 // Reset currentEntity to world so that any previously referenced entities
473 // don't have influence on the rendering of these flares (i.e. RF_ renderer flags).
474 backEnd.currentEntity = &tr.worldEntity;
475 backEnd.or = backEnd.viewParms.world;
476
477 // RB_AddDlightFlares();
478
479 // perform z buffer readback on each flare in this view
480 draw = qfalse;
481 prev = &r_activeFlares;
482 while ( ( f = *prev ) != NULL ) {
483 // throw out any flares that weren't added last frame
484 if ( f->addedFrame < backEnd.viewParms.frameCount - 1 ) {
485 *prev = f->next;
486 f->next = r_inactiveFlares;
487 r_inactiveFlares = f;
488 continue;
489 }
490
491 // don't draw any here that aren't from this scene / portal
492 f->drawIntensity = 0;
493 if ( f->frameSceneNum == backEnd.viewParms.frameSceneNum
494 && f->inPortal == backEnd.viewParms.isPortal ) {
495 RB_TestFlare( f );
496 if ( f->drawIntensity ) {
497 draw = qtrue;
498 } else {
499 // this flare has completely faded out, so remove it from the chain
500 *prev = f->next;
501 f->next = r_inactiveFlares;
502 r_inactiveFlares = f;
503 continue;
504 }
505 }
506
507 prev = &f->next;
508 }
509
510 if ( !draw ) {
511 return; // none visible
512 }
513
514 if ( backEnd.viewParms.isPortal ) {
515 qglDisable (GL_CLIP_PLANE0);
516 }
517
518 qglPushMatrix();
519 qglLoadIdentity();
520 qglMatrixMode( GL_PROJECTION );
521 qglPushMatrix();
522 qglLoadIdentity();
523 qglOrtho( backEnd.viewParms.viewportX, backEnd.viewParms.viewportX + backEnd.viewParms.viewportWidth,
524 backEnd.viewParms.viewportY, backEnd.viewParms.viewportY + backEnd.viewParms.viewportHeight,
525 -99999, 99999 );
526
527 for ( f = r_activeFlares ; f ; f = f->next ) {
528 if ( f->frameSceneNum == backEnd.viewParms.frameSceneNum
529 && f->inPortal == backEnd.viewParms.isPortal
530 && f->drawIntensity ) {
531 RB_RenderFlare( f );
532 }
533 }
534
535 qglPopMatrix();
536 qglMatrixMode( GL_MODELVIEW );
537 qglPopMatrix();
538 }
539