Debian Codegraph @

code/renderergl2/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 reletive 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 flareDepth;
280
281 FBO_t *oldFbo;
282
283 backEnd.pc.c_flareTests++;
284
285 // doing a readpixels is as good as doing a glFinish(), so
286 // don't bother with another sync
287 glState.finishCalled = qfalse;
288
289 // if we're doing multisample rendering, read from the correct FBO
290 oldFbo = glState.currentFBO;
291 if (tr.msaaResolveFbo)
292 {
293 FBO_Bind(tr.msaaResolveFbo);
294 }
295
296 // read back the z buffer contents
297 qglReadPixels( f->windowX, f->windowY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth );
298
299 // if we're doing multisample rendering, switch to the old FBO
300 if (tr.msaaResolveFbo)
301 {
302 FBO_Bind(oldFbo);
303 }
304
305 // Project flare origin with the CURRENT view (handles oblique near plane)
306 vec4_t eyePos, clipPos;
307 R_TransformModelToClip(f->origin,
308 backEnd.or.modelMatrix,
309 backEnd.viewParms.projectionMatrix,
310 eyePos, clipPos);
311 if (clipPos[3] <= 0.0f) {
312 visible = qfalse; // behind eye
313 } else {
314 // Treat near-equal depths as visible, tiny margin to avoid false occlusion from rounding
315 const float depthBias = 1e-5f; // ~256 / 2^24
316
317 float clipDepth = clipPos[2] / clipPos[3]; // [-1,1]
318 flareDepth = clipDepth * 0.5f + 0.5f; // 0..1
319 visible = (flareDepth - depthBias) <= depth; // in front (with margin)
320 }
321
322 if ( visible ) {
323 if ( !f->visible ) {
324 f->visible = qtrue;
325 f->fadeTime = backEnd.refdef.time - 1;
326 }
327 fade = ( ( backEnd.refdef.time - f->fadeTime ) /1000.0f ) * r_flareFade->value;
328 } else {
329 if ( f->visible ) {
330 f->visible = qfalse;
331 f->fadeTime = backEnd.refdef.time - 1;
332 }
333 fade = 1.0f - ( ( backEnd.refdef.time - f->fadeTime ) / 1000.0f ) * r_flareFade->value;
334 }
335
336 if ( fade < 0 ) {
337 fade = 0;
338 }
339 if ( fade > 1 ) {
340 fade = 1;
341 }
342
343 f->drawIntensity = fade;
344 }
345
346
347 /*
348 ==================
349 RB_RenderFlare
350 ==================
351 */
352 void RB_RenderFlare( flare_t *f ) {
353 float size;
354 vec3_t color;
355 int iColor[3];
356 float distance, intensity, factor;
357 byte fogFactors[3] = {255, 255, 255};
358
359 backEnd.pc.c_flareRenders++;
360
361 // We don't want too big values anyways when dividing by distance.
362 if(f->eyeZ > -1.0f)
363 distance = 1.0f;
364 else
365 distance = -f->eyeZ;
366
367 // calculate the flare size..
368 size = backEnd.viewParms.viewportWidth * ( r_flareSize->value/640.0f + 8 / distance );
369
370 /*
371 * This is an alternative to intensity scaling. It changes the size of the flare on screen instead
372 * with growing distance. See in the description at the top why this is not the way to go.
373 // size will change ~ 1/r.
374 size = backEnd.viewParms.viewportWidth * (r_flareSize->value / (distance * -2.0f));
375 */
376
377 /*
378 * As flare sizes stay nearly constant with increasing distance we must decrease the intensity
379 * to achieve a reasonable visual result. The intensity is ~ (size^2 / distance^2) which can be
380 * got by considering the ratio of
381 * (flaresurface on screen) : (Surface of sphere defined by flare origin and distance from flare)
382 * An important requirement is:
383 * intensity <= 1 for all distances.
384 *
385 * The formula used here to compute the intensity is as follows:
386 * intensity = flareCoeff * size^2 / (distance + size*sqrt(flareCoeff))^2
387 * As you can see, the intensity will have a max. of 1 when the distance is 0.
388 * The coefficient flareCoeff will determine the falloff speed with increasing distance.
389 */
390
391 factor = distance + size * sqrt(flareCoeff);
392
393 intensity = flareCoeff * size * size / (factor * factor);
394
395 VectorScale(f->color, f->drawIntensity * intensity, color);
396
397 // Calculations for fogging
398 if(tr.world && f->fogNum > 0 && f->fogNum < tr.world->numfogs)
399 {
400 tess.numVertexes = 1;
401 VectorCopy(f->origin, tess.xyz[0]);
402 tess.fogNum = f->fogNum;
403
404 RB_CalcModulateColorsByFog(fogFactors);
405
406 // We don't need to render the flare if colors are 0 anyways.
407 if(!(fogFactors[0] || fogFactors[1] || fogFactors[2]))
408 return;
409 }
410
411 iColor[0] = color[0] * fogFactors[0] * 257;
412 iColor[1] = color[1] * fogFactors[1] * 257;
413 iColor[2] = color[2] * fogFactors[2] * 257;
414
415 RB_BeginSurface( tr.flareShader, f->fogNum, 0 );
416
417 // FIXME: use quadstamp?
418 tess.xyz[tess.numVertexes][0] = f->windowX - size;
419 tess.xyz[tess.numVertexes][1] = f->windowY - size;
420 tess.texCoords[tess.numVertexes][0] = 0;
421 tess.texCoords[tess.numVertexes][1] = 0;
422 tess.color[tess.numVertexes][0] = iColor[0];
423 tess.color[tess.numVertexes][1] = iColor[1];
424 tess.color[tess.numVertexes][2] = iColor[2];
425 tess.color[tess.numVertexes][3] = 65535;
426 tess.numVertexes++;
427
428 tess.xyz[tess.numVertexes][0] = f->windowX - size;
429 tess.xyz[tess.numVertexes][1] = f->windowY + size;
430 tess.texCoords[tess.numVertexes][0] = 0;
431 tess.texCoords[tess.numVertexes][1] = 1;
432 tess.color[tess.numVertexes][0] = iColor[0];
433 tess.color[tess.numVertexes][1] = iColor[1];
434 tess.color[tess.numVertexes][2] = iColor[2];
435 tess.color[tess.numVertexes][3] = 65535;
436 tess.numVertexes++;
437
438 tess.xyz[tess.numVertexes][0] = f->windowX + size;
439 tess.xyz[tess.numVertexes][1] = f->windowY + size;
440 tess.texCoords[tess.numVertexes][0] = 1;
441 tess.texCoords[tess.numVertexes][1] = 1;
442 tess.color[tess.numVertexes][0] = iColor[0];
443 tess.color[tess.numVertexes][1] = iColor[1];
444 tess.color[tess.numVertexes][2] = iColor[2];
445 tess.color[tess.numVertexes][3] = 65535;
446 tess.numVertexes++;
447
448 tess.xyz[tess.numVertexes][0] = f->windowX + size;
449 tess.xyz[tess.numVertexes][1] = f->windowY - size;
450 tess.texCoords[tess.numVertexes][0] = 1;
451 tess.texCoords[tess.numVertexes][1] = 0;
452 tess.color[tess.numVertexes][0] = iColor[0];
453 tess.color[tess.numVertexes][1] = iColor[1];
454 tess.color[tess.numVertexes][2] = iColor[2];
455 tess.color[tess.numVertexes][3] = 65535;
456 tess.numVertexes++;
457
458 tess.indexes[tess.numIndexes++] = 0;
459 tess.indexes[tess.numIndexes++] = 1;
460 tess.indexes[tess.numIndexes++] = 2;
461 tess.indexes[tess.numIndexes++] = 0;
462 tess.indexes[tess.numIndexes++] = 2;
463 tess.indexes[tess.numIndexes++] = 3;
464
465 RB_EndSurface();
466 }
467
468 /*
469 ==================
470 RB_RenderFlares
471
472 Because flares are simulating an occular effect, they should be drawn after
473 everything (all views) in the entire frame has been drawn.
474
475 Because of the way portals use the depth buffer to mark off areas, the
476 needed information would be lost after each view, so we are forced to draw
477 flares after each view.
478
479 The resulting artifact is that flares in mirrors or portals don't dim properly
480 when occluded by something in the main view, and portal flares that should
481 extend past the portal edge will be overwritten.
482 ==================
483 */
484 void RB_RenderFlares (void) {
485 flare_t *f;
486 flare_t **prev;
487 qboolean draw;
488 mat4_t oldmodelview, oldprojection, matrix;
489
490 if ( !r_flares->integer ) {
491 return;
492 }
493
494 if ( r_flares->modified ) {
495 if ( qglesMajorVersion >= 1 && !glRefConfig.readDepth ) {
496 ri.Printf( PRINT_WARNING, "OpenGL ES needs GL_NV_read_depth to read depth to determine if flares are visible\n" );
497 ri.Cvar_Set( "r_flares", "0" );
498 }
499 r_flares->modified = qfalse;
500 }
501
502 if(r_flareCoeff->modified)
503 {
504 R_SetFlareCoeff();
505 r_flareCoeff->modified = qfalse;
506 }
507
508 // Reset currentEntity to world so that any previously referenced entities
509 // don't have influence on the rendering of these flares (i.e. RF_ renderer flags).
510 backEnd.currentEntity = &tr.worldEntity;
511 backEnd.or = backEnd.viewParms.world;
512
513 // RB_AddDlightFlares();
514
515 // perform z buffer readback on each flare in this view
516 draw = qfalse;
517 prev = &r_activeFlares;
518 while ( ( f = *prev ) != NULL ) {
519 // throw out any flares that weren't added last frame
520 if ( f->addedFrame < backEnd.viewParms.frameCount - 1 ) {
521 *prev = f->next;
522 f->next = r_inactiveFlares;
523 r_inactiveFlares = f;
524 continue;
525 }
526
527 // don't draw any here that aren't from this scene / portal
528 f->drawIntensity = 0;
529 if ( f->frameSceneNum == backEnd.viewParms.frameSceneNum
530 && f->inPortal == backEnd.viewParms.isPortal ) {
531 RB_TestFlare( f );
532 if ( f->drawIntensity ) {
533 draw = qtrue;
534 } else {
535 // this flare has completely faded out, so remove it from the chain
536 *prev = f->next;
537 f->next = r_inactiveFlares;
538 r_inactiveFlares = f;
539 continue;
540 }
541 }
542
543 prev = &f->next;
544 }
545
546 if ( !draw ) {
547 return; // none visible
548 }
549
550 Mat4Copy(glState.projection, oldprojection);
551 Mat4Copy(glState.modelview, oldmodelview);
552 Mat4Identity(matrix);
553 GL_SetModelviewMatrix(matrix);
554 Mat4Ortho( backEnd.viewParms.viewportX, backEnd.viewParms.viewportX + backEnd.viewParms.viewportWidth,
555 backEnd.viewParms.viewportY, backEnd.viewParms.viewportY + backEnd.viewParms.viewportHeight,
556 -99999, 99999, matrix );
557 GL_SetProjectionMatrix(matrix);
558
559 for ( f = r_activeFlares ; f ; f = f->next ) {
560 if ( f->frameSceneNum == backEnd.viewParms.frameSceneNum
561 && f->inPortal == backEnd.viewParms.isPortal
562 && f->drawIntensity ) {
563 RB_RenderFlare( f );
564 }
565 }
566
567 GL_SetProjectionMatrix(oldprojection);
568 GL_SetModelviewMatrix(oldmodelview);
569 }
570