Debian Codegraph @

code/renderergl1/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 = bmodel->firstSurface + i;
96
97 if ( *surf->data == SF_FACE ) {
98 ((srfSurfaceFace_t *)surf->data)->dlightBits = mask;
99 } else if ( *surf->data == SF_GRID ) {
100 ((srfGridMesh_t *)surf->data)->dlightBits = mask;
101 } else if ( *surf->data == SF_TRIANGLES ) {
102 ((srfTriangles_t *)surf->data)->dlightBits = mask;
103 }
104 }
105 }
106
107
108 /*
109 =============================================================================
110
111 LIGHT SAMPLING
112
113 =============================================================================
114 */
115
116 extern cvar_t *r_ambientScale;
117 extern cvar_t *r_directedScale;
118 extern cvar_t *r_debugLight;
119
120 /*
121 =================
122 R_SetupEntityLightingGrid
123
124 =================
125 */
126 static void R_SetupEntityLightingGrid( trRefEntity_t *ent ) {
127 vec3_t lightOrigin;
128 int pos[3];
129 int i, j;
130 byte *gridData;
131 float frac[3];
132 int gridStep[3];
133 vec3_t direction;
134 float totalFactor;
135
136 if ( ent->e.renderfx & RF_LIGHTING_ORIGIN ) {
137 // separate lightOrigins are needed so an object that is
138 // sinking into the ground can still be lit, and so
139 // multi-part models can be lit identically
140 VectorCopy( ent->e.lightingOrigin, lightOrigin );
141 } else {
142 VectorCopy( ent->e.origin, lightOrigin );
143 }
144
145 VectorSubtract( lightOrigin, tr.world->lightGridOrigin, lightOrigin );
146 for ( i = 0 ; i < 3 ; i++ ) {
147 float v;
148
149 v = lightOrigin[i]*tr.world->lightGridInverseSize[i];
150 pos[i] = floor( v );
151 frac[i] = v - pos[i];
152 if ( pos[i] < 0 ) {
153 pos[i] = 0;
154 } else if ( pos[i] > tr.world->lightGridBounds[i] - 1 ) {
155 pos[i] = tr.world->lightGridBounds[i] - 1;
156 }
157 }
158
159 VectorClear( ent->ambientLight );
160 VectorClear( ent->directedLight );
161 VectorClear( direction );
162
163 assert( tr.world->lightGridData ); // NULL with -nolight maps
164
165 // trilerp the light value
166 gridStep[0] = 8;
167 gridStep[1] = 8 * tr.world->lightGridBounds[0];
168 gridStep[2] = 8 * tr.world->lightGridBounds[0] * tr.world->lightGridBounds[1];
169 gridData = tr.world->lightGridData + pos[0] * gridStep[0]
170 + pos[1] * gridStep[1] + pos[2] * gridStep[2];
171
172 totalFactor = 0;
173 for ( i = 0 ; i < 8 ; i++ ) {
174 float factor;
175 byte *data;
176 int lat, lng;
177 vec3_t normal;
178 #if idppc
179 float d0, d1, d2, d3, d4, d5;
180 #endif
181 factor = 1.0;
182 data = gridData;
183 for ( j = 0 ; j < 3 ; j++ ) {
184 if ( i & (1<<j) ) {
185 if ( pos[j] + 1 > tr.world->lightGridBounds[j] - 1 ) {
186 break; // ignore values outside lightgrid
187 }
188 factor *= frac[j];
189 data += gridStep[j];
190 } else {
191 factor *= (1.0f - frac[j]);
192 }
193 }
194
195 if ( j != 3 ) {
196 continue;
197 }
198 if ( !(data[0]+data[1]+data[2]) ) {
199 continue; // ignore samples in walls
200 }
201 totalFactor += factor;
202 #if idppc
203 d0 = data[0]; d1 = data[1]; d2 = data[2];
204 d3 = data[3]; d4 = data[4]; d5 = data[5];
205
206 ent->ambientLight[0] += factor * d0;
207 ent->ambientLight[1] += factor * d1;
208 ent->ambientLight[2] += factor * d2;
209
210 ent->directedLight[0] += factor * d3;
211 ent->directedLight[1] += factor * d4;
212 ent->directedLight[2] += factor * d5;
213 #else
214 ent->ambientLight[0] += factor * data[0];
215 ent->ambientLight[1] += factor * data[1];
216 ent->ambientLight[2] += factor * data[2];
217
218 ent->directedLight[0] += factor * data[3];
219 ent->directedLight[1] += factor * data[4];
220 ent->directedLight[2] += factor * data[5];
221 #endif
222 lat = data[7];
223 lng = data[6];
224 lat *= (FUNCTABLE_SIZE/256);
225 lng *= (FUNCTABLE_SIZE/256);
226
227 // decode X as cos( lat ) * sin( long )
228 // decode Y as sin( lat ) * sin( long )
229 // decode Z as cos( long )
230
231 normal[0] = tr.sinTable[(lat+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK] * tr.sinTable[lng];
232 normal[1] = tr.sinTable[lat] * tr.sinTable[lng];
233 normal[2] = tr.sinTable[(lng+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK];
234
235 VectorMA( direction, factor, normal, direction );
236 }
237
238 if ( totalFactor > 0 && totalFactor < 0.99 ) {
239 totalFactor = 1.0f / totalFactor;
240 VectorScale( ent->ambientLight, totalFactor, ent->ambientLight );
241 VectorScale( ent->directedLight, totalFactor, ent->directedLight );
242 }
243
244 VectorScale( ent->ambientLight, r_ambientScale->value, ent->ambientLight );
245 VectorScale( ent->directedLight, r_directedScale->value, ent->directedLight );
246
247 VectorNormalize2( direction, ent->lightDir );
248 }
249
250
251 /*
252 ===============
253 LogLight
254 ===============
255 */
256 static void LogLight( trRefEntity_t *ent ) {
257 int max1, max2;
258
259 if ( !(ent->e.renderfx & RF_FIRST_PERSON ) ) {
260 return;
261 }
262
263 max1 = ent->ambientLight[0];
264 if ( ent->ambientLight[1] > max1 ) {
265 max1 = ent->ambientLight[1];
266 } else if ( ent->ambientLight[2] > max1 ) {
267 max1 = ent->ambientLight[2];
268 }
269
270 max2 = ent->directedLight[0];
271 if ( ent->directedLight[1] > max2 ) {
272 max2 = ent->directedLight[1];
273 } else if ( ent->directedLight[2] > max2 ) {
274 max2 = ent->directedLight[2];
275 }
276
277 ri.Printf( PRINT_ALL, "amb:%i dir:%i\n", max1, max2 );
278 }
279
280 /*
281 =================
282 R_SetupEntityLighting
283
284 Calculates all the lighting values that will be used
285 by the Calc_* functions
286 =================
287 */
288 void R_SetupEntityLighting( const trRefdef_t *refdef, trRefEntity_t *ent ) {
289 int i;
290 dlight_t *dl;
291 float power;
292 vec3_t dir;
293 float d;
294 vec3_t lightDir;
295 vec3_t lightOrigin;
296
297 // lighting calculations
298 if ( ent->lightingCalculated ) {
299 return;
300 }
301 ent->lightingCalculated = qtrue;
302
303 //
304 // trace a sample point down to find ambient light
305 //
306 if ( ent->e.renderfx & RF_LIGHTING_ORIGIN ) {
307 // separate lightOrigins are needed so an object that is
308 // sinking into the ground can still be lit, and so
309 // multi-part models can be lit identically
310 VectorCopy( ent->e.lightingOrigin, lightOrigin );
311 } else {
312 VectorCopy( ent->e.origin, lightOrigin );
313 }
314
315 // if NOWORLDMODEL, only use dynamic lights (menu system, etc)
316 if ( !(refdef->rdflags & RDF_NOWORLDMODEL )
317 && tr.world->lightGridData ) {
318 R_SetupEntityLightingGrid( ent );
319 } else {
320 ent->ambientLight[0] = ent->ambientLight[1] =
321 ent->ambientLight[2] = tr.identityLight * 150;
322 ent->directedLight[0] = ent->directedLight[1] =
323 ent->directedLight[2] = tr.identityLight * 150;
324 VectorCopy( tr.sunDirection, ent->lightDir );
325 }
326
327 // bonus items and view weapons have a fixed minimum add
328 if ( 1 /* ent->e.renderfx & RF_MINLIGHT */ ) {
329 // give everything a minimum light add
330 ent->ambientLight[0] += tr.identityLight * 32;
331 ent->ambientLight[1] += tr.identityLight * 32;
332 ent->ambientLight[2] += tr.identityLight * 32;
333 }
334
335 //
336 // modify the light by dynamic lights
337 //
338 d = VectorLength( ent->directedLight );
339 VectorScale( ent->lightDir, d, lightDir );
340
341 for ( i = 0 ; i < refdef->num_dlights ; i++ ) {
342 dl = &refdef->dlights[i];
343 VectorSubtract( dl->origin, lightOrigin, dir );
344 d = VectorNormalize( dir );
345
346 power = DLIGHT_AT_RADIUS * ( dl->radius * dl->radius );
347 if ( d < DLIGHT_MINIMUM_RADIUS ) {
348 d = DLIGHT_MINIMUM_RADIUS;
349 }
350 d = power / ( d * d );
351
352 VectorMA( ent->directedLight, d, dl->color, ent->directedLight );
353 VectorMA( lightDir, d, dir, lightDir );
354 }
355
356 // clamp ambient
357 for ( i = 0 ; i < 3 ; i++ ) {
358 if ( ent->ambientLight[i] > tr.identityLightByte ) {
359 ent->ambientLight[i] = tr.identityLightByte;
360 }
361 }
362
363 if ( r_debugLight->integer ) {
364 LogLight( ent );
365 }
366
367 // save out the byte packet version
368 ((byte *)&ent->ambientLightInt)[0] = ri.ftol(ent->ambientLight[0]);
369 ((byte *)&ent->ambientLightInt)[1] = ri.ftol(ent->ambientLight[1]);
370 ((byte *)&ent->ambientLightInt)[2] = ri.ftol(ent->ambientLight[2]);
371 ((byte *)&ent->ambientLightInt)[3] = 0xff;
372
373 // transform the direction to local space
374 VectorNormalize( lightDir );
375 ent->lightDir[0] = DotProduct( lightDir, ent->e.axis[0] );
376 ent->lightDir[1] = DotProduct( lightDir, ent->e.axis[1] );
377 ent->lightDir[2] = DotProduct( lightDir, ent->e.axis[2] );
378 }
379
380 /*
381 =================
382 R_LightForPoint
383 =================
384 */
385 int R_LightForPoint( vec3_t point, vec3_t ambientLight, vec3_t directedLight, vec3_t lightDir )
386 {
387 trRefEntity_t ent;
388
389 if ( tr.world->lightGridData == NULL )
390 return qfalse;
391
392 Com_Memset(&ent, 0, sizeof(ent));
393 VectorCopy( point, ent.e.origin );
394 R_SetupEntityLightingGrid( &ent );
395 VectorCopy(ent.ambientLight, ambientLight);
396 VectorCopy(ent.directedLight, directedLight);
397 VectorCopy(ent.lightDir, lightDir);
398
399 return qtrue;
400 }