Debian Codegraph @

code/renderergl2/tr_light.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_light.c
23
24 #include "tr_local.h"
25
26 #define DLIGHT_AT_RADIUS 16
27 // at the edge of a dlight's influence, this amount of light will be added
28
29 #define DLIGHT_MINIMUM_RADIUS 16
30 // never calculate a range less than this to prevent huge light numbers
31
32
33 /*
34 ===============
35 R_TransformDlights
36
37 Transforms the origins of an array of dlights.
38 Used by both the front end (for DlightBmodel) and
39 the back end (before doing the lighting calculation)
40 ===============
41 */
42 void R_TransformDlights( int count, dlight_t *dl, orientationr_t *or) {
43 int i;
44 vec3_t temp;
45
46 for ( i = 0 ; i < count ; i++, dl++ ) {
47 VectorSubtract( dl->origin, or->origin, temp );
48 dl->transformed[0] = DotProduct( temp, or->axis[0] );
49 dl->transformed[1] = DotProduct( temp, or->axis[1] );
50 dl->transformed[2] = DotProduct( temp, or->axis[2] );
51 }
52 }
53
54 /*
55 =============
56 R_DlightBmodel
57
58 Determine which dynamic lights may effect this bmodel
59 =============
60 */
61 void R_DlightBmodel( bmodel_t *bmodel ) {
62 int i, j;
63 dlight_t *dl;
64 int mask;
65 msurface_t *surf;
66
67 // transform all the lights
68 R_TransformDlights( tr.refdef.num_dlights, tr.refdef.dlights, &tr.or );
69
70 mask = 0;
71 for ( i=0 ; i<tr.refdef.num_dlights ; i++ ) {
72 dl = &tr.refdef.dlights[i];
73
74 // see if the point is close enough to the bounds to matter
75 for ( j = 0 ; j < 3 ; j++ ) {
76 if ( dl->transformed[j] - bmodel->bounds[1][j] > dl->radius ) {
77 break;
78 }
79 if ( bmodel->bounds[0][j] - dl->transformed[j] > dl->radius ) {
80 break;
81 }
82 }
83 if ( j < 3 ) {
84 continue;
85 }
86
87 // we need to check this light
88 mask |= 1 << i;
89 }
90
91 tr.currentEntity->needDlights = (mask != 0);
92
93 // set the dlight bits in all the surfaces
94 for ( i = 0 ; i < bmodel->numSurfaces ; i++ ) {
95 surf = tr.world->surfaces + bmodel->firstSurface + i;
96
97 switch(*surf->data)
98 {
99 case SF_FACE:
100 case SF_GRID:
101 case SF_TRIANGLES:
102 ((srfBspSurface_t *)surf->data)->dlightBits = mask;
103 break;
104
105 default:
106 break;
107 }
108 }
109 }
110
111
112 /*
113 =============================================================================
114
115 LIGHT SAMPLING
116
117 =============================================================================
118 */
119
120 extern cvar_t *r_ambientScale;
121 extern cvar_t *r_directedScale;
122 extern cvar_t *r_debugLight;
123
124 /*
125 =================
126 R_SetupEntityLightingGrid
127
128 =================
129 */
130 static void R_SetupEntityLightingGrid( trRefEntity_t *ent, world_t *world ) {
131 vec3_t lightOrigin;
132 int pos[3];
133 int i, j;
134 byte *gridData;
135 float frac[3];
136 int gridStep[3];
137 vec3_t direction;
138 float totalFactor;
139
140 if ( ent->e.renderfx & RF_LIGHTING_ORIGIN ) {
141 // separate lightOrigins are needed so an object that is
142 // sinking into the ground can still be lit, and so
143 // multi-part models can be lit identically
144 VectorCopy( ent->e.lightingOrigin, lightOrigin );
145 } else {
146 VectorCopy( ent->e.origin, lightOrigin );
147 }
148
149 VectorSubtract( lightOrigin, world->lightGridOrigin, lightOrigin );
150 for ( i = 0 ; i < 3 ; i++ ) {
151 float v;
152
153 v = lightOrigin[i]*world->lightGridInverseSize[i];
154 pos[i] = floor( v );
155 frac[i] = v - pos[i];
156 if ( pos[i] < 0 ) {
157 pos[i] = 0;
158 } else if ( pos[i] > world->lightGridBounds[i] - 1 ) {
159 pos[i] = world->lightGridBounds[i] - 1;
160 }
161 }
162
163 VectorClear( ent->ambientLight );
164 VectorClear( ent->directedLight );
165 VectorClear( direction );
166
167 assert( world->lightGridData ); // NULL with -nolight maps
168
169 // trilerp the light value
170 gridStep[0] = 8;
171 gridStep[1] = 8 * world->lightGridBounds[0];
172 gridStep[2] = 8 * world->lightGridBounds[0] * world->lightGridBounds[1];
173 gridData = world->lightGridData + pos[0] * gridStep[0]
174 + pos[1] * gridStep[1] + pos[2] * gridStep[2];
175
176 totalFactor = 0;
177 for ( i = 0 ; i < 8 ; i++ ) {
178 float factor;
179 byte *data;
180 int lat, lng;
181 vec3_t normal;
182 #if idppc
183 float d0, d1, d2, d3, d4, d5;
184 #endif
185 factor = 1.0;
186 data = gridData;
187 for ( j = 0 ; j < 3 ; j++ ) {
188 if ( i & (1<<j) ) {
189 if ( pos[j] + 1 > world->lightGridBounds[j] - 1 ) {
190 break; // ignore values outside lightgrid
191 }
192 factor *= frac[j];
193 data += gridStep[j];
194 } else {
195 factor *= (1.0f - frac[j]);
196 }
197 }
198
199 if ( j != 3 ) {
200 continue;
201 }
202
203 if (world->lightGrid16)
204 {
205 uint16_t *data16 = world->lightGrid16 + (int)(data - world->lightGridData) / 8 * 6;
206 if (!(data16[0]+data16[1]+data16[2]+data16[3]+data16[4]+data16[5])) {
207 continue; // ignore samples in walls
208 }
209 }
210 else
211 {
212 if (!(data[0]+data[1]+data[2]+data[3]+data[4]+data[5]) ) {
213 continue; // ignore samples in walls
214 }
215 }
216 totalFactor += factor;
217 #if idppc
218 d0 = data[0]; d1 = data[1]; d2 = data[2];
219 d3 = data[3]; d4 = data[4]; d5 = data[5];
220
221 ent->ambientLight[0] += factor * d0;
222 ent->ambientLight[1] += factor * d1;
223 ent->ambientLight[2] += factor * d2;
224
225 ent->directedLight[0] += factor * d3;
226 ent->directedLight[1] += factor * d4;
227 ent->directedLight[2] += factor * d5;
228 #else
229 if (world->lightGrid16)
230 {
231 // FIXME: this is hideous
232 uint16_t *data16 = world->lightGrid16 + (int)(data - world->lightGridData) / 8 * 6;
233
234 ent->ambientLight[0] += factor * data16[0] / 257.0f;
235 ent->ambientLight[1] += factor * data16[1] / 257.0f;
236 ent->ambientLight[2] += factor * data16[2] / 257.0f;
237
238 ent->directedLight[0] += factor * data16[3] / 257.0f;
239 ent->directedLight[1] += factor * data16[4] / 257.0f;
240 ent->directedLight[2] += factor * data16[5] / 257.0f;
241 }
242 else
243 {
244 ent->ambientLight[0] += factor * data[0];
245 ent->ambientLight[1] += factor * data[1];
246 ent->ambientLight[2] += factor * data[2];
247
248 ent->directedLight[0] += factor * data[3];
249 ent->directedLight[1] += factor * data[4];
250 ent->directedLight[2] += factor * data[5];
251 }
252 #endif
253 lat = data[7];
254 lng = data[6];
255 lat *= (FUNCTABLE_SIZE/256);
256 lng *= (FUNCTABLE_SIZE/256);
257
258 // decode X as cos( lat ) * sin( long )
259 // decode Y as sin( lat ) * sin( long )
260 // decode Z as cos( long )
261
262 normal[0] = tr.sinTable[(lat+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK] * tr.sinTable[lng];
263 normal[1] = tr.sinTable[lat] * tr.sinTable[lng];
264 normal[2] = tr.sinTable[(lng+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK];
265
266 VectorMA( direction, factor, normal, direction );
267 }
268
269 if ( totalFactor > 0 && totalFactor < 0.99 ) {
270 totalFactor = 1.0f / totalFactor;
271 VectorScale( ent->ambientLight, totalFactor, ent->ambientLight );
272 VectorScale( ent->directedLight, totalFactor, ent->directedLight );
273 }
274
275 VectorScale( ent->ambientLight, r_ambientScale->value, ent->ambientLight );
276 VectorScale( ent->directedLight, r_directedScale->value, ent->directedLight );
277
278 VectorNormalize2( direction, ent->lightDir );
279 }
280
281
282 /*
283 ===============
284 LogLight
285 ===============
286 */
287 static void LogLight( trRefEntity_t *ent ) {
288 int max1, max2;
289
290 if ( !(ent->e.renderfx & RF_FIRST_PERSON ) ) {
291 return;
292 }
293
294 max1 = ent->ambientLight[0];
295 if ( ent->ambientLight[1] > max1 ) {
296 max1 = ent->ambientLight[1];
297 } else if ( ent->ambientLight[2] > max1 ) {
298 max1 = ent->ambientLight[2];
299 }
300
301 max2 = ent->directedLight[0];
302 if ( ent->directedLight[1] > max2 ) {
303 max2 = ent->directedLight[1];
304 } else if ( ent->directedLight[2] > max2 ) {
305 max2 = ent->directedLight[2];
306 }
307
308 ri.Printf( PRINT_ALL, "amb:%i dir:%i\n", max1, max2 );
309 }
310
311 /*
312 =================
313 R_SetupEntityLighting
314
315 Calculates all the lighting values that will be used
316 by the Calc_* functions
317 =================
318 */
319 void R_SetupEntityLighting( const trRefdef_t *refdef, trRefEntity_t *ent ) {
320 int i;
321 dlight_t *dl;
322 float power;
323 vec3_t dir;
324 float d;
325 vec3_t lightDir;
326 vec3_t lightOrigin;
327
328 // lighting calculations
329 if ( ent->lightingCalculated ) {
330 return;
331 }
332 ent->lightingCalculated = qtrue;
333
334 //
335 // trace a sample point down to find ambient light
336 //
337 if ( ent->e.renderfx & RF_LIGHTING_ORIGIN ) {
338 // separate lightOrigins are needed so an object that is
339 // sinking into the ground can still be lit, and so
340 // multi-part models can be lit identically
341 VectorCopy( ent->e.lightingOrigin, lightOrigin );
342 } else {
343 VectorCopy( ent->e.origin, lightOrigin );
344 }
345
346 // if NOWORLDMODEL, only use dynamic lights (menu system, etc)
347 if ( !(refdef->rdflags & RDF_NOWORLDMODEL )
348 && tr.world->lightGridData ) {
349 R_SetupEntityLightingGrid( ent, tr.world );
350 } else {
351 ent->ambientLight[0] = ent->ambientLight[1] =
352 ent->ambientLight[2] = tr.identityLight * 150;
353 ent->directedLight[0] = ent->directedLight[1] =
354 ent->directedLight[2] = tr.identityLight * 150;
355 VectorCopy( tr.sunDirection, ent->lightDir );
356 }
357
358 // bonus items and view weapons have a fixed minimum add
359 if ( 1 /* ent->e.renderfx & RF_MINLIGHT */ ) {
360 // give everything a minimum light add
361 ent->ambientLight[0] += tr.identityLight * 32;
362 ent->ambientLight[1] += tr.identityLight * 32;
363 ent->ambientLight[2] += tr.identityLight * 32;
364 }
365
366 //
367 // modify the light by dynamic lights
368 //
369 d = VectorLength( ent->directedLight );
370 VectorScale( ent->lightDir, d, lightDir );
371
372 for ( i = 0 ; i < refdef->num_dlights ; i++ ) {
373 dl = &refdef->dlights[i];
374 VectorSubtract( dl->origin, lightOrigin, dir );
375 d = VectorNormalize( dir );
376
377 power = DLIGHT_AT_RADIUS * ( dl->radius * dl->radius );
378 if ( d < DLIGHT_MINIMUM_RADIUS ) {
379 d = DLIGHT_MINIMUM_RADIUS;
380 }
381 d = power / ( d * d );
382
383 VectorMA( ent->directedLight, d, dl->color, ent->directedLight );
384 VectorMA( lightDir, d, dir, lightDir );
385 }
386
387 // clamp lights
388 // FIXME: old renderer clamps (ambient + NL * directed) per vertex
389 // check if that's worth implementing
390 {
391 float r, g, b, max;
392
393 r = ent->ambientLight[0];
394 g = ent->ambientLight[1];
395 b = ent->ambientLight[2];
396
397 max = MAX(MAX(r, g), b);
398
399 if (max > 255.0f)
400 {
401 max = 255.0f / max;
402 ent->ambientLight[0] *= max;
403 ent->ambientLight[1] *= max;
404 ent->ambientLight[2] *= max;
405 }
406
407 r = ent->directedLight[0];
408 g = ent->directedLight[1];
409 b = ent->directedLight[2];
410
411 max = MAX(MAX(r, g), b);
412
413 if (max > 255.0f)
414 {
415 max = 255.0f / max;
416 ent->directedLight[0] *= max;
417 ent->directedLight[1] *= max;
418 ent->directedLight[2] *= max;
419 }
420 }
421
422
423 if ( r_debugLight->integer ) {
424 LogLight( ent );
425 }
426
427 // save out the byte packet version
428 ((byte *)&ent->ambientLightInt)[0] = ri.ftol(ent->ambientLight[0]);
429 ((byte *)&ent->ambientLightInt)[1] = ri.ftol(ent->ambientLight[1]);
430 ((byte *)&ent->ambientLightInt)[2] = ri.ftol(ent->ambientLight[2]);
431 ((byte *)&ent->ambientLightInt)[3] = 0xff;
432
433 // transform the direction to local space
434 VectorNormalize( lightDir );
435 ent->modelLightDir[0] = DotProduct( lightDir, ent->e.axis[0] );
436 ent->modelLightDir[1] = DotProduct( lightDir, ent->e.axis[1] );
437 ent->modelLightDir[2] = DotProduct( lightDir, ent->e.axis[2] );
438
439 VectorCopy(lightDir, ent->lightDir);
440 }
441
442 /*
443 =================
444 R_LightForPoint
445 =================
446 */
447 int R_LightForPoint( vec3_t point, vec3_t ambientLight, vec3_t directedLight, vec3_t lightDir )
448 {
449 trRefEntity_t ent;
450
451 if ( tr.world->lightGridData == NULL )
452 return qfalse;
453
454 Com_Memset(&ent, 0, sizeof(ent));
455 VectorCopy( point, ent.e.origin );
456 R_SetupEntityLightingGrid( &ent, tr.world );
457 VectorCopy(ent.ambientLight, ambientLight);
458 VectorCopy(ent.directedLight, directedLight);
459 VectorCopy(ent.lightDir, lightDir);
460
461 return qtrue;
462 }
463
464
465 int R_LightDirForPoint( vec3_t point, vec3_t lightDir, vec3_t normal, world_t *world )
466 {
467 trRefEntity_t ent;
468
469 if ( world->lightGridData == NULL )
470 return qfalse;
471
472 Com_Memset(&ent, 0, sizeof(ent));
473 VectorCopy( point, ent.e.origin );
474 R_SetupEntityLightingGrid( &ent, world );
475
476 if (DotProduct(ent.lightDir, normal) > 0.2f)
477 VectorCopy(ent.lightDir, lightDir);
478 else
479 VectorCopy(normal, lightDir);
480
481 return qtrue;
482 }
483
484
485 int R_CubemapForPoint( vec3_t point )
486 {
487 int cubemapIndex = -1;
488
489 if (r_cubeMapping->integer && tr.numCubemaps)
490 {
491 int i;
492 vec_t shortest = (float)WORLD_SIZE * (float)WORLD_SIZE;
493
494 for (i = 0; i < tr.numCubemaps; i++)
495 {
496 vec3_t diff;
497 vec_t length;
498
499 VectorSubtract(point, tr.cubemaps[i].origin, diff);
500 length = DotProduct(diff, diff);
501
502 if (shortest > length)
503 {
504 shortest = length;
505 cubemapIndex = i;
506 }
507 }
508 }
509
510 return cubemapIndex + 1;
511 }