Debian Codegraph @

code/renderergl2/tr_scene.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
23 #include "tr_local.h"
24
25 int r_firstSceneDrawSurf;
26
27 int r_numdlights;
28 int r_firstSceneDlight;
29
30 int r_numentities;
31 int r_firstSceneEntity;
32
33 int r_numpolys;
34 int r_firstScenePoly;
35
36 int r_numpolyverts;
37
38
39 /*
40 ====================
41 R_InitNextFrame
42
43 ====================
44 */
45 void R_InitNextFrame( void ) {
46 backEndData->commands.used = 0;
47
48 r_firstSceneDrawSurf = 0;
49
50 r_numdlights = 0;
51 r_firstSceneDlight = 0;
52
53 r_numentities = 0;
54 r_firstSceneEntity = 0;
55
56 r_numpolys = 0;
57 r_firstScenePoly = 0;
58
59 r_numpolyverts = 0;
60 }
61
62
63 /*
64 ====================
65 RE_ClearScene
66
67 ====================
68 */
69 void RE_ClearScene( void ) {
70 r_firstSceneDlight = r_numdlights;
71 r_firstSceneEntity = r_numentities;
72 r_firstScenePoly = r_numpolys;
73 }
74
75 /*
76 ===========================================================================
77
78 DISCRETE POLYS
79
80 ===========================================================================
81 */
82
83 /*
84 =====================
85 R_AddPolygonSurfaces
86
87 Adds all the scene's polys into this view's drawsurf list
88 =====================
89 */
90 void R_AddPolygonSurfaces( void ) {
91 int i;
92 shader_t *sh;
93 srfPoly_t *poly;
94 int fogMask;
95
96 tr.currentEntityNum = REFENTITYNUM_WORLD;
97 tr.shiftedEntityNum = tr.currentEntityNum << QSORT_REFENTITYNUM_SHIFT;
98 fogMask = -((tr.refdef.rdflags & RDF_NOFOG) == 0);
99
100 for ( i = 0, poly = tr.refdef.polys; i < tr.refdef.numPolys ; i++, poly++ ) {
101 sh = R_GetShaderByHandle( poly->hShader );
102 R_AddDrawSurf( ( void * )poly, sh, poly->fogIndex & fogMask, qfalse, qfalse, 0 /*cubeMap*/ );
103 }
104 }
105
106 /*
107 =====================
108 RE_AddPolyToScene
109
110 =====================
111 */
112 void RE_AddPolyToScene( qhandle_t hShader, int numVerts, const polyVert_t *verts, int numPolys ) {
113 srfPoly_t *poly;
114 int i, j;
115 int fogIndex;
116 fog_t *fog;
117 vec3_t bounds[2];
118
119 if ( !tr.registered ) {
120 return;
121 }
122
123 if ( !hShader ) {
124 // This isn't a useful warning, and an hShader of zero isn't a null shader, it's
125 // the default shader.
126 //ri.Printf( PRINT_WARNING, "WARNING: RE_AddPolyToScene: NULL poly shader\n");
127 //return;
128 }
129
130 for ( j = 0; j < numPolys; j++ ) {
131 if ( r_numpolyverts + numVerts > max_polyverts || r_numpolys >= max_polys ) {
132 /*
133 NOTE TTimo this was initially a PRINT_WARNING
134 but it happens a lot with high fighting scenes and particles
135 since we don't plan on changing the const and making for room for those effects
136 simply cut this message to developer only
137 */
138 ri.Printf( PRINT_DEVELOPER, "WARNING: RE_AddPolyToScene: r_max_polys or r_max_polyverts reached\n");
139 return;
140 }
141
142 poly = &backEndData->polys[r_numpolys];
143 poly->surfaceType = SF_POLY;
144 poly->hShader = hShader;
145 poly->numVerts = numVerts;
146 poly->verts = &backEndData->polyVerts[r_numpolyverts];
147
148 Com_Memcpy( poly->verts, &verts[numVerts*j], numVerts * sizeof( *verts ) );
149
150 if ( glConfig.hardwareType == GLHW_RAGEPRO ) {
151 poly->verts->modulate[0] = 255;
152 poly->verts->modulate[1] = 255;
153 poly->verts->modulate[2] = 255;
154 poly->verts->modulate[3] = 255;
155 }
156 // done.
157 r_numpolys++;
158 r_numpolyverts += numVerts;
159
160 // if no world is loaded
161 if ( tr.world == NULL ) {
162 fogIndex = 0;
163 }
164 // see if it is in a fog volume
165 else if ( tr.world->numfogs == 1 ) {
166 fogIndex = 0;
167 } else {
168 // find which fog volume the poly is in
169 VectorCopy( poly->verts[0].xyz, bounds[0] );
170 VectorCopy( poly->verts[0].xyz, bounds[1] );
171 for ( i = 1 ; i < poly->numVerts ; i++ ) {
172 AddPointToBounds( poly->verts[i].xyz, bounds[0], bounds[1] );
173 }
174 for ( fogIndex = 1 ; fogIndex < tr.world->numfogs ; fogIndex++ ) {
175 fog = &tr.world->fogs[fogIndex];
176 if ( bounds[1][0] >= fog->bounds[0][0]
177 && bounds[1][1] >= fog->bounds[0][1]
178 && bounds[1][2] >= fog->bounds[0][2]
179 && bounds[0][0] <= fog->bounds[1][0]
180 && bounds[0][1] <= fog->bounds[1][1]
181 && bounds[0][2] <= fog->bounds[1][2] ) {
182 break;
183 }
184 }
185 if ( fogIndex == tr.world->numfogs ) {
186 fogIndex = 0;
187 }
188 }
189 poly->fogIndex = fogIndex;
190 }
191 }
192
193
194 //=================================================================================
195
196
197 /*
198 =====================
199 RE_AddRefEntityToScene
200
201 =====================
202 */
203 void RE_AddRefEntityToScene( const refEntity_t *ent ) {
204 vec3_t cross;
205
206 if ( !tr.registered ) {
207 return;
208 }
209 if ( r_numentities >= MAX_REFENTITIES ) {
210 ri.Printf(PRINT_DEVELOPER, "RE_AddRefEntityToScene: Dropping refEntity, reached MAX_REFENTITIES\n");
211 return;
212 }
213 if ( Q_isnan(ent->origin[0]) || Q_isnan(ent->origin[1]) || Q_isnan(ent->origin[2]) ) {
214 static qboolean firstTime = qtrue;
215 if (firstTime) {
216 firstTime = qfalse;
217 ri.Printf( PRINT_WARNING, "RE_AddRefEntityToScene passed a refEntity which has an origin with a NaN component\n");
218 }
219 return;
220 }
221 if ( (int)ent->reType < 0 || ent->reType >= RT_MAX_REF_ENTITY_TYPE ) {
222 ri.Error( ERR_DROP, "RE_AddRefEntityToScene: bad reType %i", ent->reType );
223 }
224
225 backEndData->entities[r_numentities].e = *ent;
226 backEndData->entities[r_numentities].lightingCalculated = qfalse;
227
228 CrossProduct(ent->axis[0], ent->axis[1], cross);
229 backEndData->entities[r_numentities].mirrored = (DotProduct(ent->axis[2], cross) < 0.f);
230
231 r_numentities++;
232 }
233
234
235 /*
236 =====================
237 RE_AddDynamicLightToScene
238
239 =====================
240 */
241 void RE_AddDynamicLightToScene( const vec3_t org, float intensity, float r, float g, float b, int additive ) {
242 dlight_t *dl;
243
244 if ( !tr.registered ) {
245 return;
246 }
247 if ( r_numdlights >= MAX_DLIGHTS ) {
248 return;
249 }
250 if ( intensity <= 0 ) {
251 return;
252 }
253 // these cards don't have the correct blend mode
254 if ( glConfig.hardwareType == GLHW_RIVA128 || glConfig.hardwareType == GLHW_PERMEDIA2 ) {
255 return;
256 }
257 dl = &backEndData->dlights[r_numdlights++];
258 VectorCopy (org, dl->origin);
259 dl->radius = intensity;
260 dl->color[0] = r;
261 dl->color[1] = g;
262 dl->color[2] = b;
263 dl->additive = additive;
264 }
265
266 /*
267 =====================
268 RE_AddLightToScene
269
270 =====================
271 */
272 void RE_AddLightToScene( const vec3_t org, float intensity, float r, float g, float b ) {
273 RE_AddDynamicLightToScene( org, intensity, r, g, b, qfalse );
274 }
275
276 /*
277 =====================
278 RE_AddAdditiveLightToScene
279
280 =====================
281 */
282 void RE_AddAdditiveLightToScene( const vec3_t org, float intensity, float r, float g, float b ) {
283 RE_AddDynamicLightToScene( org, intensity, r, g, b, qtrue );
284 }
285
286
287 void RE_BeginScene(const refdef_t *fd)
288 {
289 Com_Memcpy( tr.refdef.text, fd->text, sizeof( tr.refdef.text ) );
290
291 tr.refdef.x = fd->x;
292 tr.refdef.y = fd->y;
293 tr.refdef.width = fd->width;
294 tr.refdef.height = fd->height;
295 tr.refdef.fov_x = fd->fov_x;
296 tr.refdef.fov_y = fd->fov_y;
297
298 VectorCopy( fd->vieworg, tr.refdef.vieworg );
299 VectorCopy( fd->viewaxis[0], tr.refdef.viewaxis[0] );
300 VectorCopy( fd->viewaxis[1], tr.refdef.viewaxis[1] );
301 VectorCopy( fd->viewaxis[2], tr.refdef.viewaxis[2] );
302
303 tr.refdef.time = fd->time;
304 tr.refdef.rdflags = fd->rdflags;
305
306 // copy the areamask data over and note if it has changed, which
307 // will force a reset of the visible leafs even if the view hasn't moved
308 tr.refdef.areamaskModified = qfalse;
309 if ( ! (tr.refdef.rdflags & RDF_NOWORLDMODEL) ) {
310 int areaDiff;
311 int i;
312
313 // compare the area bits
314 areaDiff = 0;
315 for (i = 0 ; i < MAX_MAP_AREA_BYTES/4 ; i++) {
316 areaDiff |= ((int *)tr.refdef.areamask)[i] ^ ((int *)fd->areamask)[i];
317 ((int *)tr.refdef.areamask)[i] = ((int *)fd->areamask)[i];
318 }
319
320 if ( areaDiff ) {
321 // a door just opened or something
322 tr.refdef.areamaskModified = qtrue;
323 }
324 }
325
326 tr.refdef.sunDir[3] = 0.0f;
327 tr.refdef.sunCol[3] = 1.0f;
328 tr.refdef.sunAmbCol[3] = 1.0f;
329
330 VectorCopy(tr.sunDirection, tr.refdef.sunDir);
331 if ( (tr.refdef.rdflags & RDF_NOWORLDMODEL) || !(r_depthPrepass->value) ){
332 VectorSet(tr.refdef.sunCol, 0, 0, 0);
333 VectorSet(tr.refdef.sunAmbCol, 0, 0, 0);
334 }
335 else
336 {
337 float scale = (1 << r_mapOverBrightBits->integer) / 255.0f;
338
339 if (r_forceSun->integer)
340 VectorScale(tr.sunLight, scale * r_forceSunLightScale->value, tr.refdef.sunCol);
341 else
342 VectorScale(tr.sunLight, scale, tr.refdef.sunCol);
343
344 if (r_sunlightMode->integer == 1)
345 {
346 tr.refdef.sunAmbCol[0] =
347 tr.refdef.sunAmbCol[1] =
348 tr.refdef.sunAmbCol[2] = r_forceSun->integer ? r_forceSunAmbientScale->value : tr.sunShadowScale;
349 }
350 else
351 {
352 if (r_forceSun->integer)
353 VectorScale(tr.sunLight, scale * r_forceSunAmbientScale->value, tr.refdef.sunAmbCol);
354 else
355 VectorScale(tr.sunLight, scale * tr.sunShadowScale, tr.refdef.sunAmbCol);
356 }
357 }
358
359 if (r_forceAutoExposure->integer)
360 {
361 tr.refdef.autoExposureMinMax[0] = r_forceAutoExposureMin->value;
362 tr.refdef.autoExposureMinMax[1] = r_forceAutoExposureMax->value;
363 }
364 else
365 {
366 tr.refdef.autoExposureMinMax[0] = tr.autoExposureMinMax[0];
367 tr.refdef.autoExposureMinMax[1] = tr.autoExposureMinMax[1];
368 }
369
370 if (r_forceToneMap->integer)
371 {
372 tr.refdef.toneMinAvgMaxLinear[0] = pow(2, r_forceToneMapMin->value);
373 tr.refdef.toneMinAvgMaxLinear[1] = pow(2, r_forceToneMapAvg->value);
374 tr.refdef.toneMinAvgMaxLinear[2] = pow(2, r_forceToneMapMax->value);
375 }
376 else
377 {
378 tr.refdef.toneMinAvgMaxLinear[0] = pow(2, tr.toneMinAvgMaxLevel[0]);
379 tr.refdef.toneMinAvgMaxLinear[1] = pow(2, tr.toneMinAvgMaxLevel[1]);
380 tr.refdef.toneMinAvgMaxLinear[2] = pow(2, tr.toneMinAvgMaxLevel[2]);
381 }
382
383 // Makro - copy exta info if present
384 if (fd->rdflags & RDF_EXTRA) {
385 const refdefex_t* extra = (const refdefex_t*) (fd+1);
386
387 tr.refdef.blurFactor = extra->blurFactor;
388
389 if (fd->rdflags & RDF_SUNLIGHT)
390 {
391 VectorCopy(extra->sunDir, tr.refdef.sunDir);
392 VectorCopy(extra->sunCol, tr.refdef.sunCol);
393 VectorCopy(extra->sunAmbCol, tr.refdef.sunAmbCol);
394 }
395 }
396 else
397 {
398 tr.refdef.blurFactor = 0.0f;
399 }
400
401 // derived info
402
403 tr.refdef.floatTime = tr.refdef.time * 0.001;
404
405 tr.refdef.numDrawSurfs = r_firstSceneDrawSurf;
406 tr.refdef.drawSurfs = backEndData->drawSurfs;
407
408 tr.refdef.num_entities = r_numentities - r_firstSceneEntity;
409 tr.refdef.entities = &backEndData->entities[r_firstSceneEntity];
410
411 tr.refdef.num_dlights = r_numdlights - r_firstSceneDlight;
412 tr.refdef.dlights = &backEndData->dlights[r_firstSceneDlight];
413
414 tr.refdef.numPolys = r_numpolys - r_firstScenePoly;
415 tr.refdef.polys = &backEndData->polys[r_firstScenePoly];
416
417 tr.refdef.num_pshadows = 0;
418 tr.refdef.pshadows = &backEndData->pshadows[0];
419
420 // turn off dynamic lighting globally by clearing all the
421 // dlights if it needs to be disabled or if vertex lighting is enabled
422 if ( r_dynamiclight->integer == 0 ||
423 r_vertexLight->integer == 1 ||
424 glConfig.hardwareType == GLHW_PERMEDIA2 ) {
425 tr.refdef.num_dlights = 0;
426 }
427
428 // a single frame may have multiple scenes draw inside it --
429 // a 3D game view, 3D status bar renderings, 3D menus, etc.
430 // They need to be distinguished by the light flare code, because
431 // the visibility state for a given surface may be different in
432 // each scene / view.
433 tr.frameSceneNum++;
434 tr.sceneCount++;
435 }
436
437
438 void RE_EndScene(void)
439 {
440 // the next scene rendered in this frame will tack on after this one
441 r_firstSceneDrawSurf = tr.refdef.numDrawSurfs;
442 r_firstSceneEntity = r_numentities;
443 r_firstSceneDlight = r_numdlights;
444 r_firstScenePoly = r_numpolys;
445 }
446
447 /*
448 @@@@@@@@@@@@@@@@@@@@@
449 RE_RenderScene
450
451 Draw a 3D view into a part of the window, then return
452 to 2D drawing.
453
454 Rendering a scene may require multiple views to be rendered
455 to handle mirrors,
456 @@@@@@@@@@@@@@@@@@@@@
457 */
458 void RE_RenderScene( const refdef_t *fd ) {
459 viewParms_t parms;
460 int startTime;
461
462 if ( !tr.registered ) {
463 return;
464 }
465 GLimp_LogComment( "====== RE_RenderScene =====\n" );
466
467 if ( r_norefresh->integer ) {
468 return;
469 }
470
471 startTime = ri.Milliseconds();
472
473 if (!tr.world && !( fd->rdflags & RDF_NOWORLDMODEL ) ) {
474 ri.Error (ERR_DROP, "R_RenderScene: NULL worldmodel");
475 }
476
477 RE_BeginScene(fd);
478
479 // SmileTheory: playing with shadow mapping
480 if (!( fd->rdflags & RDF_NOWORLDMODEL ) && tr.refdef.num_dlights && r_dlightMode->integer >= 2)
481 {
482 R_RenderDlightCubemaps(fd);
483 }
484
485 /* playing with more shadows */
486 if(glRefConfig.framebufferObject && !( fd->rdflags & RDF_NOWORLDMODEL ) && r_shadows->integer == 4)
487 {
488 R_RenderPshadowMaps(fd);
489 }
490
491 // playing with even more shadows
492 if(glRefConfig.framebufferObject && r_sunlightMode->integer && !( fd->rdflags & RDF_NOWORLDMODEL ) && (r_forceSun->integer || tr.sunShadows))
493 {
494 if (r_shadowCascadeZFar->integer != 0)
495 {
496 R_RenderSunShadowMaps(fd, 0);
497 R_RenderSunShadowMaps(fd, 1);
498 R_RenderSunShadowMaps(fd, 2);
499 }
500 else
501 {
502 Mat4Zero(tr.refdef.sunShadowMvp[0]);
503 Mat4Zero(tr.refdef.sunShadowMvp[1]);
504 Mat4Zero(tr.refdef.sunShadowMvp[2]);
505 }
506
507 // only rerender last cascade if sun has changed position
508 if (r_forceSun->integer == 2 || !VectorCompare(tr.refdef.sunDir, tr.lastCascadeSunDirection))
509 {
510 VectorCopy(tr.refdef.sunDir, tr.lastCascadeSunDirection);
511 R_RenderSunShadowMaps(fd, 3);
512 Mat4Copy(tr.refdef.sunShadowMvp[3], tr.lastCascadeSunMvp);
513 }
514 else
515 {
516 Mat4Copy(tr.lastCascadeSunMvp, tr.refdef.sunShadowMvp[3]);
517 }
518 }
519
520 // playing with cube maps
521 // this is where dynamic cubemaps would be rendered
522 if (0) //(glRefConfig.framebufferObject && !( fd->rdflags & RDF_NOWORLDMODEL ))
523 {
524 int i, j;
525
526 for (i = 0; i < tr.numCubemaps; i++)
527 {
528 for (j = 0; j < 6; j++)
529 {
530 R_RenderCubemapSide(i, j, qtrue);
531 }
532 }
533 }
534
535 // setup view parms for the initial view
536 //
537 // set up viewport
538 // The refdef takes 0-at-the-top y coordinates, so
539 // convert to GL's 0-at-the-bottom space
540 //
541 Com_Memset( &parms, 0, sizeof( parms ) );
542 parms.viewportX = tr.refdef.x;
543 parms.viewportY = glConfig.vidHeight - ( tr.refdef.y + tr.refdef.height );
544 parms.viewportWidth = tr.refdef.width;
545 parms.viewportHeight = tr.refdef.height;
546 parms.isPortal = qfalse;
547
548 parms.fovX = tr.refdef.fov_x;
549 parms.fovY = tr.refdef.fov_y;
550
551 parms.stereoFrame = tr.refdef.stereoFrame;
552
553 VectorCopy( fd->vieworg, parms.or.origin );
554 VectorCopy( fd->viewaxis[0], parms.or.axis[0] );
555 VectorCopy( fd->viewaxis[1], parms.or.axis[1] );
556 VectorCopy( fd->viewaxis[2], parms.or.axis[2] );
557
558 VectorCopy( fd->vieworg, parms.pvsOrigin );
559
560 if(!( fd->rdflags & RDF_NOWORLDMODEL ) && r_depthPrepass->value && ((r_forceSun->integer) || tr.sunShadows))
561 {
562 parms.flags = VPF_USESUNLIGHT;
563 }
564
565 R_RenderView( &parms );
566
567 if(!( fd->rdflags & RDF_NOWORLDMODEL ))
568 R_AddPostProcessCmd();
569
570 RE_EndScene();
571
572 tr.frontEndMsec += ri.Milliseconds() - startTime;
573 }