Debian Codegraph @

tools/quake3/q3map2/light_ydnar.c

raw symbols SCIP
1 /* -------------------------------------------------------------------------------
2
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6 This file is part of GtkRadiant.
7
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
22 ----------------------------------------------------------------------------------
23
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26
27 ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define LIGHT_YDNAR_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41
42 /*
43 ColorToBytes()
44 ydnar: moved to here 2001-02-04
45 */
46
47 void ColorToBytes( const float *color, byte *colorBytes, float scale ){
48 int i;
49 float max, gamma;
50 vec3_t sample;
51 float inv, dif;
52
53
54 /* ydnar: scaling necessary for simulating r_overbrightBits on external lightmaps */
55 if ( scale <= 0.0f ) {
56 scale = 1.0f;
57 }
58 /* globally */
59 scale *= lightmapBrightness;
60
61 /* make a local copy */
62 VectorScale( color, scale, sample );
63
64 /* muck with it */
65 gamma = 1.0f / lightmapGamma;
66 for ( i = 0; i < 3; i++ )
67 {
68 /* handle negative light */
69 if ( sample[ i ] < 0.0f ) {
70 sample[ i ] = 0.0f;
71 continue;
72 }
73
74 /* gamma */
75 sample[ i ] = pow( sample[ i ] / 255.0f, gamma ) * 255.0f;
76 }
77
78 if ( lightmapExposure == 0 ) {
79 /* clamp with color normalization */
80 max = sample[ 0 ];
81 if ( sample[ 1 ] > max ) {
82 max = sample[ 1 ];
83 }
84 if ( sample[ 2 ] > max ) {
85 max = sample[ 2 ];
86 }
87 if ( max > 255.0f ) {
88 VectorScale( sample, ( 255.0f / max ), sample );
89 }
90 }
91 else
92 {
93 inv = 1.f / lightmapExposure;
94 //Exposure
95
96 max = sample[ 0 ];
97 if ( sample[ 1 ] > max ) {
98 max = sample[ 1 ];
99 }
100 if ( sample[ 2 ] > max ) {
101 max = sample[ 2 ];
102 }
103
104 dif = ( 1 - exp( -max * inv ) ) * 255;
105
106 if ( max > 0 ) {
107 dif = dif / max;
108 }
109 else
110 {
111 dif = 0;
112 }
113
114 for ( i = 0; i < 3; i++ )
115 {
116 sample[i] *= dif;
117 }
118 }
119
120
121 /* compensate for ingame overbrighting/bitshifting */
122 VectorScale( sample, ( 1.0f / lightmapCompensate ), sample );
123
124 /* contrast */
125 if ( lightmapContrast != 1.0f ){
126 for ( i = 0; i < 3; i++ ){
127 sample[i] = lightmapContrast * ( sample[i] - 128 ) + 128;
128 if ( sample[i] < 0 ){
129 sample[i] = 0;
130 }
131 }
132 if ( ( sample[0] > 255 ) || ( sample[1] > 255 ) || ( sample[2] > 255 ) ) {
133 max = sample[0] > sample[1] ? sample[0] : sample[1];
134 max = max > sample[2] ? max : sample[2];
135 sample[0] = sample[0] * 255 / max;
136 sample[1] = sample[1] * 255 / max;
137 sample[2] = sample[2] * 255 / max;
138 }
139 }
140
141 /* sRGB lightmaps */
142 if ( lightmapsRGB ) {
143 sample[0] = floor( Image_sRGBFloatFromLinearFloat( sample[0] * ( 1.0 / 255.0 ) ) * 255.0 + 0.5 );
144 sample[1] = floor( Image_sRGBFloatFromLinearFloat( sample[1] * ( 1.0 / 255.0 ) ) * 255.0 + 0.5 );
145 sample[2] = floor( Image_sRGBFloatFromLinearFloat( sample[2] * ( 1.0 / 255.0 ) ) * 255.0 + 0.5 );
146 }
147
148 /* store it off */
149 colorBytes[ 0 ] = sample[ 0 ];
150 colorBytes[ 1 ] = sample[ 1 ];
151 colorBytes[ 2 ] = sample[ 2 ];
152 }
153
154 /*
155 * Same as ColorToBytes, but if the output color will never contain zero
156 * components. Used to avoid returning 0 0 0 due to an ioq3 issue. Reason
157 * to also map 0 0 1 to 1 1 1 is to ensure monotonicity in the color mapping
158 * to prevent banding-like artifacts on lightmaps.
159 */
160 void ColorToBytesNonZero( const float *color, byte *colorBytes, float scale) {
161 int i;
162 ColorToBytes(color, colorBytes, scale);
163 for (i = 0; i < 3; ++i)
164 if (colorBytes[i] == 0)
165 colorBytes[i] = 1;
166 }
167
168
169 /* -------------------------------------------------------------------------------
170
171 this section deals with phong shading (normal interpolation across brush faces)
172
173 ------------------------------------------------------------------------------- */
174
175 /*
176 SmoothNormals()
177 smooths together coincident vertex normals across the bsp
178 */
179
180 #define MAX_SAMPLES 256
181 #define THETA_EPSILON 0.000001
182 #define EQUAL_NORMAL_EPSILON 0.01
183
184 void SmoothNormals( void ){
185 int i, j, k, f, cs, numVerts, numVotes, fOld, start;
186 float shadeAngle, defaultShadeAngle, maxShadeAngle, dot, testAngle;
187 bspDrawSurface_t *ds;
188 shaderInfo_t *si;
189 float *shadeAngles;
190 byte *smoothed;
191 vec3_t average, diff;
192 int indexes[ MAX_SAMPLES ];
193 vec3_t votes[ MAX_SAMPLES ];
194
195
196 /* allocate shade angle table */
197 shadeAngles = safe_malloc0( numBSPDrawVerts * sizeof( float ) );
198
199 /* allocate smoothed table */
200 cs = ( numBSPDrawVerts / 8 ) + 1;
201 smoothed = safe_malloc0( cs );
202
203 /* set default shade angle */
204 defaultShadeAngle = DEG2RAD( shadeAngleDegrees );
205 maxShadeAngle = 0;
206
207 /* run through every surface and flag verts belonging to non-lightmapped surfaces
208 and set per-vertex smoothing angle */
209 for ( i = 0; i < numBSPDrawSurfaces; i++ )
210 {
211 /* get drawsurf */
212 ds = &bspDrawSurfaces[ i ];
213
214 /* get shader for shade angle */
215 si = surfaceInfos[ i ].si;
216 if ( si->shadeAngleDegrees ) {
217 shadeAngle = DEG2RAD( si->shadeAngleDegrees );
218 }
219 else{
220 shadeAngle = defaultShadeAngle;
221 }
222 if ( shadeAngle > maxShadeAngle ) {
223 maxShadeAngle = shadeAngle;
224 }
225
226 /* flag its verts */
227 for ( j = 0; j < ds->numVerts; j++ )
228 {
229 f = ds->firstVert + j;
230 shadeAngles[ f ] = shadeAngle;
231 if ( ds->surfaceType == MST_TRIANGLE_SOUP ) {
232 smoothed[ f >> 3 ] |= ( 1 << ( f & 7 ) );
233 }
234 }
235
236 /* ydnar: optional force-to-trisoup */
237 if ( trisoup && ds->surfaceType == MST_PLANAR ) {
238 ds->surfaceType = MST_TRIANGLE_SOUP;
239 ds->lightmapNum[ 0 ] = -3;
240 }
241 }
242
243 /* bail if no surfaces have a shade angle */
244 if ( maxShadeAngle == 0 ) {
245 free( shadeAngles );
246 free( smoothed );
247 return;
248 }
249
250 /* init pacifier */
251 fOld = -1;
252 start = I_FloatTime();
253
254 /* go through the list of vertexes */
255 for ( i = 0; i < numBSPDrawVerts; i++ )
256 {
257 /* print pacifier */
258 f = 10 * i / numBSPDrawVerts;
259 if ( f != fOld ) {
260 fOld = f;
261 Sys_Printf( "%i...", f );
262 }
263
264 /* already smoothed? */
265 if ( smoothed[ i >> 3 ] & ( 1 << ( i & 7 ) ) ) {
266 continue;
267 }
268
269 /* clear */
270 VectorClear( average );
271 numVerts = 0;
272 numVotes = 0;
273
274 /* build a table of coincident vertexes */
275 for ( j = i; j < numBSPDrawVerts && numVerts < MAX_SAMPLES; j++ )
276 {
277 /* already smoothed? */
278 if ( smoothed[ j >> 3 ] & ( 1 << ( j & 7 ) ) ) {
279 continue;
280 }
281
282 /* test vertexes */
283 if ( VectorCompare( yDrawVerts[ i ].xyz, yDrawVerts[ j ].xyz ) == qfalse ) {
284 continue;
285 }
286
287 /* use smallest shade angle */
288 shadeAngle = ( shadeAngles[ i ] < shadeAngles[ j ] ? shadeAngles[ i ] : shadeAngles[ j ] );
289
290 /* check shade angle */
291 dot = DotProduct( bspDrawVerts[ i ].normal, bspDrawVerts[ j ].normal );
292 if ( dot > 1.0 ) {
293 dot = 1.0;
294 }
295 else if ( dot < -1.0 ) {
296 dot = -1.0;
297 }
298 testAngle = acos( dot ) + THETA_EPSILON;
299 if ( testAngle >= shadeAngle ) {
300 //Sys_Printf( "F(%3.3f >= %3.3f) ", RAD2DEG( testAngle ), RAD2DEG( shadeAngle ) );
301 continue;
302 }
303 //Sys_Printf( "P(%3.3f < %3.3f) ", RAD2DEG( testAngle ), RAD2DEG( shadeAngle ) );
304
305 /* add to the list */
306 indexes[ numVerts++ ] = j;
307
308 /* flag vertex */
309 smoothed[ j >> 3 ] |= ( 1 << ( j & 7 ) );
310
311 /* see if this normal has already been voted */
312 for ( k = 0; k < numVotes; k++ )
313 {
314 VectorSubtract( bspDrawVerts[ j ].normal, votes[ k ], diff );
315 if ( fabs( diff[ 0 ] ) < EQUAL_NORMAL_EPSILON &&
316 fabs( diff[ 1 ] ) < EQUAL_NORMAL_EPSILON &&
317 fabs( diff[ 2 ] ) < EQUAL_NORMAL_EPSILON ) {
318 break;
319 }
320 }
321
322 /* add a new vote? */
323 if ( k == numVotes && numVotes < MAX_SAMPLES ) {
324 VectorAdd( average, bspDrawVerts[ j ].normal, average );
325 VectorCopy( bspDrawVerts[ j ].normal, votes[ numVotes ] );
326 numVotes++;
327 }
328 }
329
330 /* don't average for less than 2 verts */
331 if ( numVerts < 2 ) {
332 continue;
333 }
334
335 /* average normal */
336 if ( VectorNormalize( average, average ) > 0 ) {
337 /* smooth */
338 for ( j = 0; j < numVerts; j++ )
339 VectorCopy( average, yDrawVerts[ indexes[ j ] ].normal );
340 }
341 }
342
343 /* free the tables */
344 free( shadeAngles );
345 free( smoothed );
346
347 /* print time */
348 Sys_Printf( " (%i)\n", (int) ( I_FloatTime() - start ) );
349 }
350
351
352
353 /* -------------------------------------------------------------------------------
354
355 this section deals with phong shaded lightmap tracing
356
357 ------------------------------------------------------------------------------- */
358
359 /* 9th rewrite (recursive subdivision of a lightmap triangle) */
360
361 /*
362 CalcTangentVectors()
363 calculates the st tangent vectors for normalmapping
364 */
365
366 static qboolean CalcTangentVectors( int numVerts, bspDrawVert_t **dv, vec3_t *stv, vec3_t *ttv ){
367 int i;
368 float bb, s, t;
369 vec3_t bary;
370
371
372 /* calculate barycentric basis for the triangle */
373 bb = ( dv[ 1 ]->st[ 0 ] - dv[ 0 ]->st[ 0 ] ) * ( dv[ 2 ]->st[ 1 ] - dv[ 0 ]->st[ 1 ] ) - ( dv[ 2 ]->st[ 0 ] - dv[ 0 ]->st[ 0 ] ) * ( dv[ 1 ]->st[ 1 ] - dv[ 0 ]->st[ 1 ] );
374 if ( fabs( bb ) < 0.00000001f ) {
375 return qfalse;
376 }
377
378 /* do each vertex */
379 for ( i = 0; i < numVerts; i++ )
380 {
381 /* calculate s tangent vector */
382 s = dv[ i ]->st[ 0 ] + 10.0f;
383 t = dv[ i ]->st[ 1 ];
384 bary[ 0 ] = ( ( dv[ 1 ]->st[ 0 ] - s ) * ( dv[ 2 ]->st[ 1 ] - t ) - ( dv[ 2 ]->st[ 0 ] - s ) * ( dv[ 1 ]->st[ 1 ] - t ) ) / bb;
385 bary[ 1 ] = ( ( dv[ 2 ]->st[ 0 ] - s ) * ( dv[ 0 ]->st[ 1 ] - t ) - ( dv[ 0 ]->st[ 0 ] - s ) * ( dv[ 2 ]->st[ 1 ] - t ) ) / bb;
386 bary[ 2 ] = ( ( dv[ 0 ]->st[ 0 ] - s ) * ( dv[ 1 ]->st[ 1 ] - t ) - ( dv[ 1 ]->st[ 0 ] - s ) * ( dv[ 0 ]->st[ 1 ] - t ) ) / bb;
387
388 stv[ i ][ 0 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 0 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 0 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 0 ];
389 stv[ i ][ 1 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 1 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 1 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 1 ];
390 stv[ i ][ 2 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 2 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 2 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 2 ];
391
392 VectorSubtract( stv[ i ], dv[ i ]->xyz, stv[ i ] );
393 VectorNormalize( stv[ i ], stv[ i ] );
394
395 /* calculate t tangent vector */
396 s = dv[ i ]->st[ 0 ];
397 t = dv[ i ]->st[ 1 ] + 10.0f;
398 bary[ 0 ] = ( ( dv[ 1 ]->st[ 0 ] - s ) * ( dv[ 2 ]->st[ 1 ] - t ) - ( dv[ 2 ]->st[ 0 ] - s ) * ( dv[ 1 ]->st[ 1 ] - t ) ) / bb;
399 bary[ 1 ] = ( ( dv[ 2 ]->st[ 0 ] - s ) * ( dv[ 0 ]->st[ 1 ] - t ) - ( dv[ 0 ]->st[ 0 ] - s ) * ( dv[ 2 ]->st[ 1 ] - t ) ) / bb;
400 bary[ 2 ] = ( ( dv[ 0 ]->st[ 0 ] - s ) * ( dv[ 1 ]->st[ 1 ] - t ) - ( dv[ 1 ]->st[ 0 ] - s ) * ( dv[ 0 ]->st[ 1 ] - t ) ) / bb;
401
402 ttv[ i ][ 0 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 0 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 0 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 0 ];
403 ttv[ i ][ 1 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 1 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 1 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 1 ];
404 ttv[ i ][ 2 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 2 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 2 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 2 ];
405
406 VectorSubtract( ttv[ i ], dv[ i ]->xyz, ttv[ i ] );
407 VectorNormalize( ttv[ i ], ttv[ i ] );
408
409 /* debug code */
410 //% Sys_FPrintf( SYS_VRB, "%d S: (%f %f %f) T: (%f %f %f)\n", i,
411 //% stv[ i ][ 0 ], stv[ i ][ 1 ], stv[ i ][ 2 ], ttv[ i ][ 0 ], ttv[ i ][ 1 ], ttv[ i ][ 2 ] );
412 }
413
414 /* return to caller */
415 return qtrue;
416 }
417
418
419
420
421 /*
422 PerturbNormal()
423 perterbs the normal by the shader's normalmap in tangent space
424 */
425
426 static void PerturbNormal( bspDrawVert_t *dv, shaderInfo_t *si, vec3_t pNormal, vec3_t stv[ 3 ], vec3_t ttv[ 3 ] ){
427 int i;
428 vec4_t bump;
429
430
431 /* passthrough */
432 VectorCopy( dv->normal, pNormal );
433
434 /* sample normalmap */
435 if ( RadSampleImage( si->normalImage->pixels, si->normalImage->width, si->normalImage->height, dv->st, bump ) == qfalse ) {
436 return;
437 }
438
439 /* remap sampled normal from [0,255] to [-1,-1] */
440 for ( i = 0; i < 3; i++ )
441 bump[ i ] = ( bump[ i ] - 127.0f ) * ( 1.0f / 127.5f );
442
443 /* scale tangent vectors and add to original normal */
444 VectorMA( dv->normal, bump[ 0 ], stv[ 0 ], pNormal );
445 VectorMA( pNormal, bump[ 1 ], ttv[ 0 ], pNormal );
446 VectorMA( pNormal, bump[ 2 ], dv->normal, pNormal );
447
448 /* renormalize and return */
449 VectorNormalize( pNormal, pNormal );
450 }
451
452
453
454 /*
455 MapSingleLuxel()
456 maps a luxel for triangle bv at
457 */
458
459 #define NUDGE 0.5f
460 #define BOGUS_NUDGE -99999.0f
461
462 static int MapSingleLuxel( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t *dv, vec4_t plane, float pass, vec3_t stv[ 3 ], vec3_t ttv[ 3 ], vec3_t worldverts[ 3 ] ){
463 int i, x, y, numClusters, *clusters, pointCluster, *cluster;
464 float *luxel, *origin, *normal, d, lightmapSampleOffset;
465 shaderInfo_t *si;
466 vec3_t pNormal;
467 vec3_t vecs[ 3 ];
468 vec3_t nudged;
469 vec3_t cverts[ 3 ];
470 vec3_t temp;
471 vec4_t sideplane, hostplane;
472 vec3_t origintwo;
473 int j, next;
474 float e;
475 float *nudge;
476 static float nudges[][ 2 ] =
477 {
478 //%{ 0, 0 }, /* try center first */
479 { -NUDGE, 0 }, /* left */
480 { NUDGE, 0 }, /* right */
481 { 0, NUDGE }, /* up */
482 { 0, -NUDGE }, /* down */
483 { -NUDGE, NUDGE }, /* left/up */
484 { NUDGE, -NUDGE }, /* right/down */
485 { NUDGE, NUDGE }, /* right/up */
486 { -NUDGE, -NUDGE }, /* left/down */
487 { BOGUS_NUDGE, BOGUS_NUDGE }
488 };
489
490
491 /* find luxel xy coords (fixme: subtract 0.5?) */
492 x = dv->lightmap[ 0 ][ 0 ];
493 y = dv->lightmap[ 0 ][ 1 ];
494 if ( x < 0 ) {
495 x = 0;
496 }
497 else if ( x >= lm->sw ) {
498 x = lm->sw - 1;
499 }
500 if ( y < 0 ) {
501 y = 0;
502 }
503 else if ( y >= lm->sh ) {
504 y = lm->sh - 1;
505 }
506
507 /* set shader and cluster list */
508 if ( info != NULL ) {
509 si = info->si;
510 numClusters = info->numSurfaceClusters;
511 clusters = &surfaceClusters[ info->firstSurfaceCluster ];
512 }
513 else
514 {
515 si = NULL;
516 numClusters = 0;
517 clusters = NULL;
518 }
519
520 /* get luxel, origin, cluster, and normal */
521 luxel = SUPER_LUXEL( 0, x, y );
522 origin = SUPER_ORIGIN( x, y );
523 normal = SUPER_NORMAL( x, y );
524 cluster = SUPER_CLUSTER( x, y );
525
526 /* don't attempt to remap occluded luxels for planar surfaces */
527 if ( ( *cluster ) == CLUSTER_OCCLUDED && lm->plane != NULL ) {
528 return ( *cluster );
529 }
530
531 /* only average the normal for premapped luxels */
532 else if ( ( *cluster ) >= 0 ) {
533 /* do bumpmap calculations */
534 if ( stv != NULL ) {
535 PerturbNormal( dv, si, pNormal, stv, ttv );
536 }
537 else{
538 VectorCopy( dv->normal, pNormal );
539 }
540
541 /* add the additional normal data */
542 VectorAdd( normal, pNormal, normal );
543 luxel[ 3 ] += 1.0f;
544 return ( *cluster );
545 }
546
547 /* otherwise, unmapped luxels (*cluster == CLUSTER_UNMAPPED) will have their full attributes calculated */
548
549 /* get origin */
550
551 /* axial lightmap projection */
552 if ( lm->vecs != NULL ) {
553 /* calculate an origin for the sample from the lightmap vectors */
554 VectorCopy( lm->origin, origin );
555 for ( i = 0; i < 3; i++ )
556 {
557 /* add unless it's the axis, which is taken care of later */
558 if ( i == lm->axisNum ) {
559 continue;
560 }
561 origin[ i ] += ( x * lm->vecs[ 0 ][ i ] ) + ( y * lm->vecs[ 1 ][ i ] );
562 }
563
564 /* project the origin onto the plane */
565 d = DotProduct( origin, plane ) - plane[ 3 ];
566 // working around division by zero
567 vec_t c = plane[ lm->axisNum ];
568 d /= c ? c : 1;
569 origin[ lm->axisNum ] -= d;
570 }
571
572 /* non axial lightmap projection (explicit xyz) */
573 else{
574 VectorCopy( dv->xyz, origin );
575 }
576
577 //////////////////////
578 //27's test to make sure samples stay within the triangle boundaries
579 //1) Test the sample origin to see if it lays on the wrong side of any edge (x/y)
580 //2) if it does, nudge it onto the correct side.
581
582 if ( worldverts != NULL && lightmapTriangleCheck ) {
583 for ( j = 0; j < 3; j++ )
584 {
585 VectorCopy( worldverts[j],cverts[j] );
586 }
587 PlaneFromPoints( hostplane,cverts[0],cverts[1],cverts[2] );
588
589 for ( j = 0; j < 3; j++ )
590 {
591 for ( i = 0; i < 3; i++ )
592 {
593 //build plane using 2 edges and a normal
594 next = ( i + 1 ) % 3;
595
596 VectorCopy( cverts[next],temp );
597 VectorAdd( temp,hostplane,temp );
598 PlaneFromPoints( sideplane,cverts[i],cverts[ next ], temp );
599
600 //planetest sample point
601 e = DotProduct( origin,sideplane );
602 e = e - sideplane[3];
603 if ( e > 0 ) {
604 //we're bad.
605 //VectorClear(origin);
606 //Move the sample point back inside triangle bounds
607 origin[0] -= sideplane[0] * ( e + 1 );
608 origin[1] -= sideplane[1] * ( e + 1 );
609 origin[2] -= sideplane[2] * ( e + 1 );
610 #ifdef DEBUG_27_1
611 VectorClear( origin );
612 #endif
613 }
614 }
615 }
616 }
617
618 ////////////////////////
619
620 /* planar surfaces have precalculated lightmap vectors for nudging */
621 if ( lm->plane != NULL ) {
622 VectorCopy( lm->vecs[ 0 ], vecs[ 0 ] );
623 VectorCopy( lm->vecs[ 1 ], vecs[ 1 ] );
624 VectorCopy( lm->plane, vecs[ 2 ] );
625 }
626
627 /* non-planar surfaces must calculate them */
628 else
629 {
630 if ( plane != NULL ) {
631 VectorCopy( plane, vecs[ 2 ] );
632 }
633 else{
634 VectorCopy( dv->normal, vecs[ 2 ] );
635 }
636 MakeNormalVectors( vecs[ 2 ], vecs[ 0 ], vecs[ 1 ] );
637 }
638
639 /* push the origin off the surface a bit */
640 if ( si != NULL ) {
641 lightmapSampleOffset = si->lightmapSampleOffset;
642 }
643 else{
644 lightmapSampleOffset = DEFAULT_LIGHTMAP_SAMPLE_OFFSET;
645 }
646 if ( lm->axisNum < 0 ) {
647 VectorMA( origin, lightmapSampleOffset, vecs[ 2 ], origin );
648 }
649 else if ( vecs[ 2 ][ lm->axisNum ] < 0.0f ) {
650 origin[ lm->axisNum ] -= lightmapSampleOffset;
651 }
652 else{
653 origin[ lm->axisNum ] += lightmapSampleOffset;
654 }
655
656 VectorCopy( origin,origintwo );
657 if ( lightmapExtraVisClusterNudge ) {
658 origintwo[0] += vecs[2][0];
659 origintwo[1] += vecs[2][1];
660 origintwo[2] += vecs[2][2];
661 }
662
663 /* get cluster */
664 pointCluster = ClusterForPointExtFilter( origintwo, LUXEL_EPSILON, numClusters, clusters );
665
666 /* another retarded hack, storing nudge count in luxel[ 1 ] */
667 luxel[ 1 ] = 0.0f;
668
669 /* point in solid? (except in dark mode) */
670 if ( pointCluster < 0 && dark == qfalse ) {
671 /* nudge the the location around */
672 nudge = nudges[ 0 ];
673 while ( nudge[ 0 ] > BOGUS_NUDGE && pointCluster < 0 )
674 {
675 /* nudge the vector around a bit */
676 for ( i = 0; i < 3; i++ )
677 {
678 /* set nudged point*/
679 nudged[ i ] = origintwo[ i ] + ( nudge[ 0 ] * vecs[ 0 ][ i ] ) + ( nudge[ 1 ] * vecs[ 1 ][ i ] );
680 }
681 nudge += 2;
682
683 /* get pvs cluster */
684 pointCluster = ClusterForPointExtFilter( nudged, LUXEL_EPSILON, numClusters, clusters ); //% + 0.625 );
685 if ( pointCluster >= 0 ) {
686 VectorCopy( nudged, origin );
687 }
688 luxel[ 1 ] += 1.0f;
689 }
690 }
691
692 /* as a last resort, if still in solid, try drawvert origin offset by normal (except in dark mode) */
693 if ( pointCluster < 0 && si != NULL && dark == qfalse ) {
694 VectorMA( dv->xyz, lightmapSampleOffset, dv->normal, nudged );
695 pointCluster = ClusterForPointExtFilter( nudged, LUXEL_EPSILON, numClusters, clusters );
696 if ( pointCluster >= 0 ) {
697 VectorCopy( nudged, origin );
698 }
699 luxel[ 1 ] += 1.0f;
700 }
701
702 /* valid? */
703 if ( pointCluster < 0 ) {
704 ( *cluster ) = CLUSTER_OCCLUDED;
705 VectorClear( origin );
706 VectorClear( normal );
707 numLuxelsOccluded++;
708 return ( *cluster );
709 }
710
711 /* debug code */
712 //% Sys_Printf( "%f %f %f\n", origin[ 0 ], origin[ 1 ], origin[ 2 ] );
713
714 /* do bumpmap calculations */
715 if ( stv ) {
716 PerturbNormal( dv, si, pNormal, stv, ttv );
717 }
718 else{
719 VectorCopy( dv->normal, pNormal );
720 }
721
722 /* store the cluster and normal */
723 ( *cluster ) = pointCluster;
724 VectorCopy( pNormal, normal );
725
726 /* store explicit mapping pass and implicit mapping pass */
727 luxel[ 0 ] = pass;
728 luxel[ 3 ] = 1.0f;
729
730 /* add to count */
731 numLuxelsMapped++;
732
733 /* return ok */
734 return ( *cluster );
735 }
736
737
738
739 /*
740 MapTriangle_r()
741 recursively subdivides a triangle until its edges are shorter
742 than the distance between two luxels (thanks jc :)
743 */
744
745 static void MapTriangle_r( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t *dv[ 3 ], vec4_t plane, vec3_t stv[ 3 ], vec3_t ttv[ 3 ], vec3_t worldverts[ 3 ] ){
746 bspDrawVert_t mid, *dv2[ 3 ];
747 int max;
748
749
750 /* map the vertexes */
751 #if 0
752 MapSingleLuxel( lm, info, dv[ 0 ], plane, 1, stv, ttv );
753 MapSingleLuxel( lm, info, dv[ 1 ], plane, 1, stv, ttv );
754 MapSingleLuxel( lm, info, dv[ 2 ], plane, 1, stv, ttv );
755 #endif
756
757 /* subdivide calc */
758 {
759 int i;
760 float *a, *b, dx, dy, dist, maxDist;
761
762
763 /* find the longest edge and split it */
764 max = -1;
765 maxDist = 0;
766 for ( i = 0; i < 3; i++ )
767 {
768 /* get verts */
769 a = dv[ i ]->lightmap[ 0 ];
770 b = dv[ ( i + 1 ) % 3 ]->lightmap[ 0 ];
771
772 /* get dists */
773 dx = a[ 0 ] - b[ 0 ];
774 dy = a[ 1 ] - b[ 1 ];
775 dist = ( dx * dx ) + ( dy * dy ); //% sqrt( (dx * dx) + (dy * dy) );
776
777 /* longer? */
778 if ( dist > maxDist ) {
779 maxDist = dist;
780 max = i;
781 }
782 }
783
784 /* try to early out */
785 if ( max < 0 || maxDist <= subdivideThreshold ) { /* ydnar: was i < 0 instead of max < 0 (?) */
786 return;
787 }
788 }
789
790 /* split the longest edge and map it */
791 LerpDrawVert( dv[ max ], dv[ ( max + 1 ) % 3 ], &mid );
792 MapSingleLuxel( lm, info, &mid, plane, 1, stv, ttv, worldverts );
793
794 /* push the point up a little bit to account for fp creep (fixme: revisit this) */
795 //% VectorMA( mid.xyz, 2.0f, mid.normal, mid.xyz );
796
797 /* recurse to first triangle */
798 VectorCopy( dv, dv2 );
799 dv2[ max ] = &mid;
800 MapTriangle_r( lm, info, dv2, plane, stv, ttv, worldverts );
801
802 /* recurse to second triangle */
803 VectorCopy( dv, dv2 );
804 dv2[ ( max + 1 ) % 3 ] = &mid;
805 MapTriangle_r( lm, info, dv2, plane, stv, ttv, worldverts );
806 }
807
808
809
810 /*
811 MapTriangle()
812 seed function for MapTriangle_r()
813 requires a cw ordered triangle
814 */
815
816 static qboolean MapTriangle( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t *dv[ 3 ], qboolean mapNonAxial ){
817 int i;
818 vec4_t plane;
819 vec3_t *stv, *ttv, stvStatic[ 3 ], ttvStatic[ 3 ];
820 vec3_t worldverts[ 3 ];
821
822
823 /* get plane if possible */
824 if ( lm->plane != NULL ) {
825 VectorCopy( lm->plane, plane );
826 plane[ 3 ] = lm->plane[ 3 ];
827 }
828
829 /* otherwise make one from the points */
830 else if ( PlaneFromPoints( plane, dv[ 0 ]->xyz, dv[ 1 ]->xyz, dv[ 2 ]->xyz ) == qfalse ) {
831 return qfalse;
832 }
833
834 /* check to see if we need to calculate texture->world tangent vectors */
835 if ( info->si->normalImage != NULL && CalcTangentVectors( 3, dv, stvStatic, ttvStatic ) ) {
836 stv = stvStatic;
837 ttv = ttvStatic;
838 }
839 else
840 {
841 stv = NULL;
842 ttv = NULL;
843 }
844
845 VectorCopy( dv[ 0 ]->xyz, worldverts[ 0 ] );
846 VectorCopy( dv[ 1 ]->xyz, worldverts[ 1 ] );
847 VectorCopy( dv[ 2 ]->xyz, worldverts[ 2 ] );
848
849 /* map the vertexes */
850 MapSingleLuxel( lm, info, dv[ 0 ], plane, 1, stv, ttv, worldverts );
851 MapSingleLuxel( lm, info, dv[ 1 ], plane, 1, stv, ttv, worldverts );
852 MapSingleLuxel( lm, info, dv[ 2 ], plane, 1, stv, ttv, worldverts );
853
854 /* 2002-11-20: prefer axial triangle edges */
855 if ( mapNonAxial ) {
856 /* subdivide the triangle */
857 MapTriangle_r( lm, info, dv, plane, stv, ttv, worldverts );
858 return qtrue;
859 }
860
861 for ( i = 0; i < 3; i++ )
862 {
863 float *a, *b;
864 bspDrawVert_t *dv2[ 3 ];
865
866
867 /* get verts */
868 a = dv[ i ]->lightmap[ 0 ];
869 b = dv[ ( i + 1 ) % 3 ]->lightmap[ 0 ];
870
871 /* make degenerate triangles for mapping edges */
872 if ( fabs( a[ 0 ] - b[ 0 ] ) < 0.01f || fabs( a[ 1 ] - b[ 1 ] ) < 0.01f ) {
873 dv2[ 0 ] = dv[ i ];
874 dv2[ 1 ] = dv[ ( i + 1 ) % 3 ];
875 dv2[ 2 ] = dv[ ( i + 1 ) % 3 ];
876
877 /* map the degenerate triangle */
878 MapTriangle_r( lm, info, dv2, plane, stv, ttv, worldverts );
879 }
880 }
881
882 return qtrue;
883 }
884
885
886
887 /*
888 MapQuad_r()
889 recursively subdivides a quad until its edges are shorter
890 than the distance between two luxels
891 */
892
893 static void MapQuad_r( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t *dv[ 4 ], vec4_t plane, vec3_t stv[ 4 ], vec3_t ttv[ 4 ] ){
894 bspDrawVert_t mid[ 2 ], *dv2[ 4 ];
895 int max;
896
897
898 /* subdivide calc */
899 {
900 int i;
901 float *a, *b, dx, dy, dist, maxDist;
902
903
904 /* find the longest edge and split it */
905 max = -1;
906 maxDist = 0;
907 for ( i = 0; i < 4; i++ )
908 {
909 /* get verts */
910 a = dv[ i ]->lightmap[ 0 ];
911 b = dv[ ( i + 1 ) % 4 ]->lightmap[ 0 ];
912
913 /* get dists */
914 dx = a[ 0 ] - b[ 0 ];
915 dy = a[ 1 ] - b[ 1 ];
916 dist = ( dx * dx ) + ( dy * dy ); //% sqrt( (dx * dx) + (dy * dy) );
917
918 /* longer? */
919 if ( dist > maxDist ) {
920 maxDist = dist;
921 max = i;
922 }
923 }
924
925 /* try to early out */
926 if ( max < 0 || maxDist <= subdivideThreshold ) {
927 return;
928 }
929 }
930
931 /* we only care about even/odd edges */
932 max &= 1;
933
934 /* split the longest edges */
935 LerpDrawVert( dv[ max ], dv[ ( max + 1 ) % 4 ], &mid[ 0 ] );
936 LerpDrawVert( dv[ max + 2 ], dv[ ( max + 3 ) % 4 ], &mid[ 1 ] );
937
938 /* map the vertexes */
939 MapSingleLuxel( lm, info, &mid[ 0 ], plane, 1, stv, ttv, NULL );
940 MapSingleLuxel( lm, info, &mid[ 1 ], plane, 1, stv, ttv, NULL );
941
942 /* 0 and 2 */
943 if ( max == 0 ) {
944 /* recurse to first quad */
945 dv2[ 0 ] = dv[ 0 ];
946 dv2[ 1 ] = &mid[ 0 ];
947 dv2[ 2 ] = &mid[ 1 ];
948 dv2[ 3 ] = dv[ 3 ];
949 MapQuad_r( lm, info, dv2, plane, stv, ttv );
950
951 /* recurse to second quad */
952 dv2[ 0 ] = &mid[ 0 ];
953 dv2[ 1 ] = dv[ 1 ];
954 dv2[ 2 ] = dv[ 2 ];
955 dv2[ 3 ] = &mid[ 1 ];
956 MapQuad_r( lm, info, dv2, plane, stv, ttv );
957 }
958
959 /* 1 and 3 */
960 else
961 {
962 /* recurse to first quad */
963 dv2[ 0 ] = dv[ 0 ];
964 dv2[ 1 ] = dv[ 1 ];
965 dv2[ 2 ] = &mid[ 0 ];
966 dv2[ 3 ] = &mid[ 1 ];
967 MapQuad_r( lm, info, dv2, plane, stv, ttv );
968
969 /* recurse to second quad */
970 dv2[ 0 ] = &mid[ 1 ];
971 dv2[ 1 ] = &mid[ 0 ];
972 dv2[ 2 ] = dv[ 2 ];
973 dv2[ 3 ] = dv[ 3 ];
974 MapQuad_r( lm, info, dv2, plane, stv, ttv );
975 }
976 }
977
978
979
980 /*
981 MapQuad()
982 seed function for MapQuad_r()
983 requires a cw ordered triangle quad
984 */
985
986 #define QUAD_PLANAR_EPSILON 0.5f
987
988 static qboolean MapQuad( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t *dv[ 4 ] ){
989 float dist;
990 vec4_t plane;
991 vec3_t *stv, *ttv, stvStatic[ 4 ], ttvStatic[ 4 ];
992
993
994 /* get plane if possible */
995 if ( lm->plane != NULL ) {
996 VectorCopy( lm->plane, plane );
997 plane[ 3 ] = lm->plane[ 3 ];
998 }
999
1000 /* otherwise make one from the points */
1001 else if ( PlaneFromPoints( plane, dv[ 0 ]->xyz, dv[ 1 ]->xyz, dv[ 2 ]->xyz ) == qfalse ) {
1002 return qfalse;
1003 }
1004
1005 /* 4th point must fall on the plane */
1006 dist = DotProduct( plane, dv[ 3 ]->xyz ) - plane[ 3 ];
1007 if ( fabs( dist ) > QUAD_PLANAR_EPSILON ) {
1008 return qfalse;
1009 }
1010
1011 /* check to see if we need to calculate texture->world tangent vectors */
1012 if ( info->si->normalImage != NULL && CalcTangentVectors( 4, dv, stvStatic, ttvStatic ) ) {
1013 stv = stvStatic;
1014 ttv = ttvStatic;
1015 }
1016 else
1017 {
1018 stv = NULL;
1019 ttv = NULL;
1020 }
1021
1022 /* map the vertexes */
1023 MapSingleLuxel( lm, info, dv[ 0 ], plane, 1, stv, ttv, NULL );
1024 MapSingleLuxel( lm, info, dv[ 1 ], plane, 1, stv, ttv, NULL );
1025 MapSingleLuxel( lm, info, dv[ 2 ], plane, 1, stv, ttv, NULL );
1026 MapSingleLuxel( lm, info, dv[ 3 ], plane, 1, stv, ttv, NULL );
1027
1028 /* subdivide the quad */
1029 MapQuad_r( lm, info, dv, plane, stv, ttv );
1030 return qtrue;
1031 }
1032
1033
1034
1035 /*
1036 MapRawLightmap()
1037 maps the locations, normals, and pvs clusters for a raw lightmap
1038 */
1039
1040 #define VectorDivide( in, d, out ) VectorScale( in, ( 1.0f / ( d ) ), out ) //% (out)[ 0 ] = (in)[ 0 ] / (d), (out)[ 1 ] = (in)[ 1 ] / (d), (out)[ 2 ] = (in)[ 2 ] / (d)
1041
1042 void MapRawLightmap( int rawLightmapNum ){
1043 int n, num, i, x, y, sx, sy, pw[ 5 ], r, *cluster, mapNonAxial;
1044 float *luxel, *origin, *normal, samples, radius, pass;
1045 rawLightmap_t *lm;
1046 bspDrawSurface_t *ds;
1047 surfaceInfo_t *info;
1048 mesh_t src, *subdivided, *mesh;
1049 bspDrawVert_t *verts, *dv[ 4 ], fake;
1050
1051
1052 /* bail if this number exceeds the number of raw lightmaps */
1053 if ( rawLightmapNum >= numRawLightmaps ) {
1054 return;
1055 }
1056
1057 /* get lightmap */
1058 lm = &rawLightmaps[ rawLightmapNum ];
1059
1060 /* -----------------------------------------------------------------
1061 map referenced surfaces onto the raw lightmap
1062 ----------------------------------------------------------------- */
1063
1064 /* walk the list of surfaces on this raw lightmap */
1065 for ( n = 0; n < lm->numLightSurfaces; n++ )
1066 {
1067 /* with > 1 surface per raw lightmap, clear occluded */
1068 if ( n > 0 ) {
1069 for ( y = 0; y < lm->sh; y++ )
1070 {
1071 for ( x = 0; x < lm->sw; x++ )
1072 {
1073 /* get cluster */
1074 cluster = SUPER_CLUSTER( x, y );
1075 if ( *cluster < 0 ) {
1076 *cluster = CLUSTER_UNMAPPED;
1077 }
1078 }
1079 }
1080 }
1081
1082 /* get surface */
1083 num = lightSurfaces[ lm->firstLightSurface + n ];
1084 ds = &bspDrawSurfaces[ num ];
1085 info = &surfaceInfos[ num ];
1086
1087 /* bail if no lightmap to calculate */
1088 if ( info->lm != lm ) {
1089 Sys_Printf( "!" );
1090 continue;
1091 }
1092
1093 /* map the surface onto the lightmap origin/cluster/normal buffers */
1094 switch ( ds->surfaceType )
1095 {
1096 case MST_PLANAR:
1097 /* get verts */
1098 verts = yDrawVerts + ds->firstVert;
1099
1100 /* map the triangles */
1101 for ( mapNonAxial = 0; mapNonAxial < 2; mapNonAxial++ )
1102 {
1103 for ( i = 0; i < ds->numIndexes; i += 3 )
1104 {
1105 dv[ 0 ] = &verts[ bspDrawIndexes[ ds->firstIndex + i ] ];
1106 dv[ 1 ] = &verts[ bspDrawIndexes[ ds->firstIndex + i + 1 ] ];
1107 dv[ 2 ] = &verts[ bspDrawIndexes[ ds->firstIndex + i + 2 ] ];
1108 MapTriangle( lm, info, dv, mapNonAxial );
1109 }
1110 }
1111 break;
1112
1113 case MST_PATCH:
1114 /* make a mesh from the drawsurf */
1115 src.width = ds->patchWidth;
1116 src.height = ds->patchHeight;
1117 src.verts = &yDrawVerts[ ds->firstVert ];
1118 //% subdivided = SubdivideMesh( src, 8, 512 );
1119 subdivided = SubdivideMesh2( src, info->patchIterations );
1120
1121 /* fit it to the curve and remove colinear verts on rows/columns */
1122 PutMeshOnCurve( *subdivided );
1123 mesh = RemoveLinearMeshColumnsRows( subdivided );
1124 FreeMesh( subdivided );
1125
1126 /* get verts */
1127 verts = mesh->verts;
1128
1129 /* debug code */
1130 #if 0
1131 if ( lm->plane ) {
1132 Sys_Printf( "Planar patch: [%1.3f %1.3f %1.3f] [%1.3f %1.3f %1.3f] [%1.3f %1.3f %1.3f]\n",
1133 lm->plane[ 0 ], lm->plane[ 1 ], lm->plane[ 2 ],
1134 lm->vecs[ 0 ][ 0 ], lm->vecs[ 0 ][ 1 ], lm->vecs[ 0 ][ 2 ],
1135 lm->vecs[ 1 ][ 0 ], lm->vecs[ 1 ][ 1 ], lm->vecs[ 1 ][ 2 ] );
1136 }
1137 #endif
1138
1139 /* map the mesh quads */
1140 #if 0
1141
1142 for ( mapNonAxial = 0; mapNonAxial < 2; mapNonAxial++ )
1143 {
1144 for ( y = 0; y < ( mesh->height - 1 ); y++ )
1145 {
1146 for ( x = 0; x < ( mesh->width - 1 ); x++ )
1147 {
1148 /* set indexes */
1149 pw[ 0 ] = x + ( y * mesh->width );
1150 pw[ 1 ] = x + ( ( y + 1 ) * mesh->width );
1151 pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width );
1152 pw[ 3 ] = x + 1 + ( y * mesh->width );
1153 pw[ 4 ] = x + ( y * mesh->width ); /* same as pw[ 0 ] */
1154
1155 /* set radix */
1156 r = ( x + y ) & 1;
1157
1158 /* get drawverts and map first triangle */
1159 dv[ 0 ] = &verts[ pw[ r + 0 ] ];
1160 dv[ 1 ] = &verts[ pw[ r + 1 ] ];
1161 dv[ 2 ] = &verts[ pw[ r + 2 ] ];
1162 MapTriangle( lm, info, dv, mapNonAxial );
1163
1164 /* get drawverts and map second triangle */
1165 dv[ 0 ] = &verts[ pw[ r + 0 ] ];
1166 dv[ 1 ] = &verts[ pw[ r + 2 ] ];
1167 dv[ 2 ] = &verts[ pw[ r + 3 ] ];
1168 MapTriangle( lm, info, dv, mapNonAxial );
1169 }
1170 }
1171 }
1172
1173 #else
1174
1175 for ( y = 0; y < ( mesh->height - 1 ); y++ )
1176 {
1177 for ( x = 0; x < ( mesh->width - 1 ); x++ )
1178 {
1179 /* set indexes */
1180 pw[ 0 ] = x + ( y * mesh->width );
1181 pw[ 1 ] = x + ( ( y + 1 ) * mesh->width );
1182 pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width );
1183 pw[ 3 ] = x + 1 + ( y * mesh->width );
1184 pw[ 4 ] = pw[ 0 ];
1185
1186 /* set radix */
1187 r = ( x + y ) & 1;
1188
1189 /* attempt to map quad first */
1190 dv[ 0 ] = &verts[ pw[ r + 0 ] ];
1191 dv[ 1 ] = &verts[ pw[ r + 1 ] ];
1192 dv[ 2 ] = &verts[ pw[ r + 2 ] ];
1193 dv[ 3 ] = &verts[ pw[ r + 3 ] ];
1194 if ( MapQuad( lm, info, dv ) ) {
1195 continue;
1196 }
1197
1198 for ( mapNonAxial = 0; mapNonAxial < 2; mapNonAxial++ )
1199 {
1200 /* get drawverts and map first triangle */
1201 dv[ 1 ] = &verts[ pw[ r + 1 ] ];
1202 dv[ 2 ] = &verts[ pw[ r + 2 ] ];
1203 MapTriangle( lm, info, dv, mapNonAxial );
1204
1205 /* get drawverts and map second triangle */
1206 dv[ 1 ] = &verts[ pw[ r + 2 ] ];
1207 dv[ 2 ] = &verts[ pw[ r + 3 ] ];
1208 MapTriangle( lm, info, dv, mapNonAxial );
1209 }
1210 }
1211 }
1212
1213 #endif
1214
1215 /* free the mesh */
1216 FreeMesh( mesh );
1217 break;
1218
1219 default:
1220 break;
1221 }
1222 }
1223
1224 /* -----------------------------------------------------------------
1225 average and clean up luxel normals
1226 ----------------------------------------------------------------- */
1227
1228 /* walk the luxels */
1229 for ( y = 0; y < lm->sh; y++ )
1230 {
1231 for ( x = 0; x < lm->sw; x++ )
1232 {
1233 /* get luxel */
1234 luxel = SUPER_LUXEL( 0, x, y );
1235 normal = SUPER_NORMAL( x, y );
1236 cluster = SUPER_CLUSTER( x, y );
1237
1238 /* only look at mapped luxels */
1239 if ( *cluster < 0 ) {
1240 continue;
1241 }
1242
1243 /* the normal data could be the sum of multiple samples */
1244 if ( luxel[ 3 ] > 1.0f ) {
1245 VectorNormalize( normal, normal );
1246 }
1247
1248 /* mark this luxel as having only one normal */
1249 luxel[ 3 ] = 1.0f;
1250 }
1251 }
1252
1253 /* non-planar surfaces stop here */
1254 if ( lm->plane == NULL ) {
1255 return;
1256 }
1257
1258 /* -----------------------------------------------------------------
1259 map occluded or unuxed luxels
1260 ----------------------------------------------------------------- */
1261
1262 /* walk the luxels */
1263 /* FIXME: superSample is int, no need in floor() */
1264 radius = floor( superSample / 2 );
1265 radius = radius > 0 ? radius : 1.0f;
1266 radius += 1.0f;
1267 for ( pass = 2.0f; pass <= radius; pass += 1.0f )
1268 {
1269 for ( y = 0; y < lm->sh; y++ )
1270 {
1271 for ( x = 0; x < lm->sw; x++ )
1272 {
1273 /* get luxel */
1274 luxel = SUPER_LUXEL( 0, x, y );
1275 normal = SUPER_NORMAL( x, y );
1276 cluster = SUPER_CLUSTER( x, y );
1277
1278 /* only look at unmapped luxels */
1279 if ( *cluster != CLUSTER_UNMAPPED ) {
1280 continue;
1281 }
1282
1283 /* divine a normal and origin from neighboring luxels */
1284 VectorClear( fake.xyz );
1285 VectorClear( fake.normal );
1286 fake.lightmap[ 0 ][ 0 ] = x; //% 0.0001 + x;
1287 fake.lightmap[ 0 ][ 1 ] = y; //% 0.0001 + y;
1288 samples = 0.0f;
1289 for ( sy = ( y - 1 ); sy <= ( y + 1 ); sy++ )
1290 {
1291 if ( sy < 0 || sy >= lm->sh ) {
1292 continue;
1293 }
1294
1295 for ( sx = ( x - 1 ); sx <= ( x + 1 ); sx++ )
1296 {
1297 if ( sx < 0 || sx >= lm->sw || ( sx == x && sy == y ) ) {
1298 continue;
1299 }
1300
1301 /* get neighboring luxel */
1302 luxel = SUPER_LUXEL( 0, sx, sy );
1303 origin = SUPER_ORIGIN( sx, sy );
1304 normal = SUPER_NORMAL( sx, sy );
1305 cluster = SUPER_CLUSTER( sx, sy );
1306
1307 /* only consider luxels mapped in previous passes */
1308 if ( *cluster < 0 || luxel[ 0 ] >= pass ) {
1309 continue;
1310 }
1311
1312 /* add its distinctiveness to our own */
1313 VectorAdd( fake.xyz, origin, fake.xyz );
1314 VectorAdd( fake.normal, normal, fake.normal );
1315 samples += luxel[ 3 ];
1316 }
1317 }
1318
1319 /* any samples? */
1320 if ( samples == 0.0f ) {
1321 continue;
1322 }
1323
1324 /* average */
1325 VectorDivide( fake.xyz, samples, fake.xyz );
1326 //% VectorDivide( fake.normal, samples, fake.normal );
1327 if ( VectorNormalize( fake.normal, fake.normal ) == 0.0f ) {
1328 continue;
1329 }
1330
1331 /* map the fake vert */
1332 MapSingleLuxel( lm, NULL, &fake, lm->plane, pass, NULL, NULL, NULL );
1333 }
1334 }
1335 }
1336
1337 /* -----------------------------------------------------------------
1338 average and clean up luxel normals
1339 ----------------------------------------------------------------- */
1340
1341 /* walk the luxels */
1342 for ( y = 0; y < lm->sh; y++ )
1343 {
1344 for ( x = 0; x < lm->sw; x++ )
1345 {
1346 /* get luxel */
1347 luxel = SUPER_LUXEL( 0, x, y );
1348 normal = SUPER_NORMAL( x, y );
1349 cluster = SUPER_CLUSTER( x, y );
1350
1351 /* only look at mapped luxels */
1352 if ( *cluster < 0 ) {
1353 continue;
1354 }
1355
1356 /* the normal data could be the sum of multiple samples */
1357 if ( luxel[ 3 ] > 1.0f ) {
1358 VectorNormalize( normal, normal );
1359 }
1360
1361 /* mark this luxel as having only one normal */
1362 luxel[ 3 ] = 1.0f;
1363 }
1364 }
1365
1366 /* debug code */
1367 #if 0
1368 Sys_Printf( "\n" );
1369 for ( y = 0; y < lm->sh; y++ )
1370 {
1371 for ( x = 0; x < lm->sw; x++ )
1372 {
1373 vec3_t mins, maxs;
1374
1375
1376 cluster = SUPER_CLUSTER( x, y );
1377 origin = SUPER_ORIGIN( x, y );
1378 normal = SUPER_NORMAL( x, y );
1379 luxel = SUPER_LUXEL( x, y );
1380
1381 if ( *cluster < 0 ) {
1382 continue;
1383 }
1384
1385 /* check if within the bounding boxes of all surfaces referenced */
1386 ClearBounds( mins, maxs );
1387 for ( n = 0; n < lm->numLightSurfaces; n++ )
1388 {
1389 int TOL;
1390 info = &surfaceInfos[ lightSurfaces[ lm->firstLightSurface + n ] ];
1391 TOL = info->sampleSize + 2;
1392 AddPointToBounds( info->mins, mins, maxs );
1393 AddPointToBounds( info->maxs, mins, maxs );
1394 if ( origin[ 0 ] > ( info->mins[ 0 ] - TOL ) && origin[ 0 ] < ( info->maxs[ 0 ] + TOL ) &&
1395 origin[ 1 ] > ( info->mins[ 1 ] - TOL ) && origin[ 1 ] < ( info->maxs[ 1 ] + TOL ) &&
1396 origin[ 2 ] > ( info->mins[ 2 ] - TOL ) && origin[ 2 ] < ( info->maxs[ 2 ] + TOL ) ) {
1397 break;
1398 }
1399 }
1400
1401 /* inside? */
1402 if ( n < lm->numLightSurfaces ) {
1403 continue;
1404 }
1405
1406 /* report bogus origin */
1407 Sys_Printf( "%6d [%2d,%2d] (%4d): XYZ(%+4.1f %+4.1f %+4.1f) LO(%+4.1f %+4.1f %+4.1f) HI(%+4.1f %+4.1f %+4.1f) <%3.0f>\n",
1408 rawLightmapNum, x, y, *cluster,
1409 origin[ 0 ], origin[ 1 ], origin[ 2 ],
1410 mins[ 0 ], mins[ 1 ], mins[ 2 ],
1411 maxs[ 0 ], maxs[ 1 ], maxs[ 2 ],
1412 luxel[ 3 ] );
1413 }
1414 }
1415 #endif
1416 }
1417
1418
1419
1420 /*
1421 SetupDirt()
1422 sets up dirtmap (ambient occlusion)
1423 */
1424
1425 #define DIRT_CONE_ANGLE 88 /* degrees */
1426 #define DIRT_NUM_ANGLE_STEPS 16
1427 #define DIRT_NUM_ELEVATION_STEPS 3
1428 #define DIRT_NUM_VECTORS ( DIRT_NUM_ANGLE_STEPS * DIRT_NUM_ELEVATION_STEPS )
1429
1430 static vec3_t dirtVectors[ DIRT_NUM_VECTORS ];
1431 static int numDirtVectors = 0;
1432
1433 void SetupDirt( void ){
1434 int i, j;
1435 float angle, elevation, angleStep, elevationStep;
1436
1437
1438 /* note it */
1439 Sys_FPrintf( SYS_VRB, "--- SetupDirt ---\n" );
1440
1441 /* calculate angular steps */
1442 angleStep = DEG2RAD( 360.0f / DIRT_NUM_ANGLE_STEPS );
1443 elevationStep = DEG2RAD( DIRT_CONE_ANGLE / DIRT_NUM_ELEVATION_STEPS );
1444
1445 /* iterate angle */
1446 angle = 0.0f;
1447 for ( i = 0, angle = 0.0f; i < DIRT_NUM_ANGLE_STEPS; i++, angle += angleStep )
1448 {
1449 /* iterate elevation */
1450 for ( j = 0, elevation = elevationStep * 0.5f; j < DIRT_NUM_ELEVATION_STEPS; j++, elevation += elevationStep )
1451 {
1452 dirtVectors[ numDirtVectors ][ 0 ] = sin( elevation ) * cos( angle );
1453 dirtVectors[ numDirtVectors ][ 1 ] = sin( elevation ) * sin( angle );
1454 dirtVectors[ numDirtVectors ][ 2 ] = cos( elevation );
1455 numDirtVectors++;
1456 }
1457 }
1458
1459 /* emit some statistics */
1460 Sys_FPrintf( SYS_VRB, "%9d dirtmap vectors\n", numDirtVectors );
1461 }
1462
1463
1464 /*
1465 DirtForSample()
1466 calculates dirt value for a given sample
1467 */
1468
1469 float DirtForSample( trace_t *trace ){
1470 int i;
1471 float gatherDirt, outDirt, angle, elevation, ooDepth;
1472 vec3_t normal, worldUp, myUp, myRt, temp, direction, displacement;
1473
1474
1475 /* dummy check */
1476 if ( !dirty ) {
1477 return 1.0f;
1478 }
1479 if ( trace == NULL || trace->cluster < 0 ) {
1480 return 0.0f;
1481 }
1482
1483 /* setup */
1484 gatherDirt = 0.0f;
1485 ooDepth = 1.0f / dirtDepth;
1486 VectorCopy( trace->normal, normal );
1487
1488 /* check if the normal is aligned to the world-up */
1489 if ( normal[ 0 ] == 0.0f && normal[ 1 ] == 0.0f && ( normal[ 2 ] == 1.0f || normal[ 2 ] == -1.0f ) ) {
1490 if ( normal[ 2 ] == 1.0f ) {
1491 VectorSet( myRt, 1.0f, 0.0f, 0.0f );
1492 VectorSet( myUp, 0.0f, 1.0f, 0.0f );
1493 }
1494 else if ( normal[ 2 ] == -1.0f ) {
1495 VectorSet( myRt, -1.0f, 0.0f, 0.0f );
1496 VectorSet( myUp, 0.0f, 1.0f, 0.0f );
1497 }
1498 }
1499 else
1500 {
1501 VectorSet( worldUp, 0.0f, 0.0f, 1.0f );
1502 CrossProduct( normal, worldUp, myRt );
1503 VectorNormalize( myRt, myRt );
1504 CrossProduct( myRt, normal, myUp );
1505 VectorNormalize( myUp, myUp );
1506 }
1507
1508 /* 1 = random mode, 0 (well everything else) = non-random mode */
1509 if ( dirtMode == 1 ) {
1510 /* iterate */
1511 for ( i = 0; i < numDirtVectors; i++ )
1512 {
1513 /* get random vector */
1514 angle = Random() * DEG2RAD( 360.0f );
1515 elevation = Random() * DEG2RAD( DIRT_CONE_ANGLE );
1516 temp[ 0 ] = cos( angle ) * sin( elevation );
1517 temp[ 1 ] = sin( angle ) * sin( elevation );
1518 temp[ 2 ] = cos( elevation );
1519
1520 /* transform into tangent space */
1521 direction[ 0 ] = myRt[ 0 ] * temp[ 0 ] + myUp[ 0 ] * temp[ 1 ] + normal[ 0 ] * temp[ 2 ];
1522 direction[ 1 ] = myRt[ 1 ] * temp[ 0 ] + myUp[ 1 ] * temp[ 1 ] + normal[ 1 ] * temp[ 2 ];
1523 direction[ 2 ] = myRt[ 2 ] * temp[ 0 ] + myUp[ 2 ] * temp[ 1 ] + normal[ 2 ] * temp[ 2 ];
1524
1525 /* set endpoint */
1526 VectorMA( trace->origin, dirtDepth, direction, trace->end );
1527 SetupTrace( trace );
1528 VectorSet(trace->color, 1.0f, 1.0f, 1.0f);
1529
1530 /* trace */
1531 TraceLine( trace );
1532 if ( trace->opaque && !( trace->compileFlags & C_SKY ) ) {
1533 VectorSubtract( trace->hit, trace->origin, displacement );
1534 gatherDirt += 1.0f - ooDepth * VectorLength( displacement );
1535 }
1536 }
1537 }
1538 else
1539 {
1540 /* iterate through ordered vectors */
1541 for ( i = 0; i < numDirtVectors; i++ )
1542 {
1543 /* transform vector into tangent space */
1544 direction[ 0 ] = myRt[ 0 ] * dirtVectors[ i ][ 0 ] + myUp[ 0 ] * dirtVectors[ i ][ 1 ] + normal[ 0 ] * dirtVectors[ i ][ 2 ];
1545 direction[ 1 ] = myRt[ 1 ] * dirtVectors[ i ][ 0 ] + myUp[ 1 ] * dirtVectors[ i ][ 1 ] + normal[ 1 ] * dirtVectors[ i ][ 2 ];
1546 direction[ 2 ] = myRt[ 2 ] * dirtVectors[ i ][ 0 ] + myUp[ 2 ] * dirtVectors[ i ][ 1 ] + normal[ 2 ] * dirtVectors[ i ][ 2 ];
1547
1548 /* set endpoint */
1549 VectorMA( trace->origin, dirtDepth, direction, trace->end );
1550 SetupTrace( trace );
1551 VectorSet(trace->color, 1.0f, 1.0f, 1.0f);
1552
1553 /* trace */
1554 TraceLine( trace );
1555 if ( trace->opaque ) {
1556 VectorSubtract( trace->hit, trace->origin, displacement );
1557 gatherDirt += 1.0f - ooDepth * VectorLength( displacement );
1558 }
1559 }
1560 }
1561
1562 /* direct ray */
1563 VectorMA( trace->origin, dirtDepth, normal, trace->end );
1564 SetupTrace( trace );
1565 VectorSet(trace->color, 1.0f, 1.0f, 1.0f);
1566
1567 /* trace */
1568 TraceLine( trace );
1569 if ( trace->opaque ) {
1570 VectorSubtract( trace->hit, trace->origin, displacement );
1571 gatherDirt += 1.0f - ooDepth * VectorLength( displacement );
1572 }
1573
1574 /* early out */
1575 if ( gatherDirt <= 0.0f ) {
1576 return 1.0f;
1577 }
1578
1579 /* apply gain (does this even do much? heh) */
1580 outDirt = pow( gatherDirt / ( numDirtVectors + 1 ), dirtGain );
1581 if ( outDirt > 1.0f ) {
1582 outDirt = 1.0f;
1583 }
1584
1585 /* apply scale */
1586 outDirt *= dirtScale;
1587 if ( outDirt > 1.0f ) {
1588 outDirt = 1.0f;
1589 }
1590
1591 /* return to sender */
1592 return 1.0f - outDirt;
1593 }
1594
1595
1596
1597 /*
1598 DirtyRawLightmap()
1599 calculates dirty fraction for each luxel
1600 */
1601
1602 void DirtyRawLightmap( int rawLightmapNum ){
1603 int i, x, y, sx, sy, *cluster;
1604 float *origin, *normal, *dirt, *dirt2, average, samples;
1605 rawLightmap_t *lm;
1606 surfaceInfo_t *info;
1607 trace_t trace;
1608 qboolean noDirty;
1609
1610
1611 /* bail if this number exceeds the number of raw lightmaps */
1612 if ( rawLightmapNum >= numRawLightmaps ) {
1613 return;
1614 }
1615
1616 /* get lightmap */
1617 lm = &rawLightmaps[ rawLightmapNum ];
1618
1619 /* setup trace */
1620 trace.testOcclusion = qtrue;
1621 trace.forceSunlight = qfalse;
1622 trace.recvShadows = lm->recvShadows;
1623 trace.numSurfaces = lm->numLightSurfaces;
1624 trace.surfaces = &lightSurfaces[ lm->firstLightSurface ];
1625 trace.inhibitRadius = 0.0f;
1626 trace.testAll = qfalse;
1627
1628 /* twosided lighting (may or may not be a good idea for lightmapped stuff) */
1629 trace.twoSided = qfalse;
1630 for ( i = 0; i < trace.numSurfaces; i++ )
1631 {
1632 /* get surface */
1633 info = &surfaceInfos[ trace.surfaces[ i ] ];
1634
1635 /* check twosidedness */
1636 if ( info->si->twoSided ) {
1637 trace.twoSided = qtrue;
1638 break;
1639 }
1640 }
1641
1642 noDirty = qfalse;
1643 for ( i = 0; i < trace.numSurfaces; i++ )
1644 {
1645 /* get surface */
1646 info = &surfaceInfos[ trace.surfaces[ i ] ];
1647
1648 /* check twosidedness */
1649 if ( info->si->noDirty ) {
1650 noDirty = qtrue;
1651 break;
1652 }
1653 }
1654
1655 /* gather dirt */
1656 for ( y = 0; y < lm->sh; y++ )
1657 {
1658 for ( x = 0; x < lm->sw; x++ )
1659 {
1660 /* get luxel */
1661 cluster = SUPER_CLUSTER( x, y );
1662 origin = SUPER_ORIGIN( x, y );
1663 normal = SUPER_NORMAL( x, y );
1664 dirt = SUPER_DIRT( x, y );
1665
1666 /* set default dirt */
1667 *dirt = 0.0f;
1668
1669 /* only look at mapped luxels */
1670 if ( *cluster < 0 ) {
1671 continue;
1672 }
1673
1674 /* don't apply dirty on this surface */
1675 if ( noDirty ) {
1676 *dirt = 1.0f;
1677 continue;
1678 }
1679
1680 /* copy to trace */
1681 trace.cluster = *cluster;
1682 VectorCopy( origin, trace.origin );
1683 VectorCopy( normal, trace.normal );
1684
1685 /* get dirt */
1686 *dirt = DirtForSample( &trace );
1687 }
1688 }
1689
1690 /* testing no filtering */
1691 //% return;
1692
1693 /* filter dirt */
1694 for ( y = 0; y < lm->sh; y++ )
1695 {
1696 for ( x = 0; x < lm->sw; x++ )
1697 {
1698 /* get luxel */
1699 cluster = SUPER_CLUSTER( x, y );
1700 dirt = SUPER_DIRT( x, y );
1701
1702 /* filter dirt by adjacency to unmapped luxels */
1703 average = *dirt;
1704 samples = 1.0f;
1705 for ( sy = ( y - 1 ); sy <= ( y + 1 ); sy++ )
1706 {
1707 if ( sy < 0 || sy >= lm->sh ) {
1708 continue;
1709 }
1710
1711 for ( sx = ( x - 1 ); sx <= ( x + 1 ); sx++ )
1712 {
1713 if ( sx < 0 || sx >= lm->sw || ( sx == x && sy == y ) ) {
1714 continue;
1715 }
1716
1717 /* get neighboring luxel */
1718 cluster = SUPER_CLUSTER( sx, sy );
1719 dirt2 = SUPER_DIRT( sx, sy );
1720 if ( *cluster < 0 || *dirt2 <= 0.0f ) {
1721 continue;
1722 }
1723
1724 /* add it */
1725 average += *dirt2;
1726 samples += 1.0f;
1727 }
1728
1729 /* bail */
1730 if ( samples <= 0.0f ) {
1731 break;
1732 }
1733 }
1734
1735 /* bail */
1736 if ( samples <= 0.0f ) {
1737 continue;
1738 }
1739
1740 /* scale dirt */
1741 *dirt = average / samples;
1742 }
1743 }
1744 }
1745
1746
1747
1748 /*
1749 SubmapRawLuxel()
1750 calculates the pvs cluster, origin, normal of a sub-luxel
1751 */
1752
1753 static qboolean SubmapRawLuxel( rawLightmap_t *lm, int x, int y, float bx, float by, int *sampleCluster, vec3_t sampleOrigin, vec3_t sampleNormal ){
1754 int i, *cluster, *cluster2;
1755 float *origin, *origin2, *normal; //% , *normal2;
1756 vec3_t originVecs[ 2 ]; //% , normalVecs[ 2 ];
1757
1758 vec3_t origin_dummy = {};
1759
1760 /* calulate x vector */
1761 if ( ( x < ( lm->sw - 1 ) && bx >= 0.0f ) || ( x == 0 && bx <= 0.0f ) ) {
1762 cluster = SUPER_CLUSTER( x, y );
1763 origin = SUPER_ORIGIN( x, y );
1764 //% normal = SUPER_NORMAL( x, y );
1765 cluster2 = SUPER_CLUSTER( x + 1, y );
1766 origin2 = *cluster2 < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x + 1, y );
1767 //% normal2 = *cluster2 < 0 ? SUPER_NORMAL( x, y ) : SUPER_NORMAL( x + 1, y );
1768 }
1769 else if ( ( x > 0 && bx <= 0.0f ) || ( x == ( lm->sw - 1 ) && bx >= 0.0f ) ) {
1770 cluster = SUPER_CLUSTER( x - 1, y );
1771 origin = *cluster < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x - 1, y );
1772 //% normal = *cluster < 0 ? SUPER_NORMAL( x, y ) : SUPER_NORMAL( x - 1, y );
1773 cluster2 = SUPER_CLUSTER( x, y );
1774 origin2 = SUPER_ORIGIN( x, y );
1775 //% normal2 = SUPER_NORMAL( x, y );
1776 }
1777 else {
1778 Sys_FPrintf( SYS_WRN, "WARNING: Spurious lightmap S vector\n" );
1779 origin = origin_dummy;
1780 origin2 = origin_dummy;
1781 }
1782
1783 VectorSubtract( origin2, origin, originVecs[ 0 ] );
1784 //% VectorSubtract( normal2, normal, normalVecs[ 0 ] );
1785
1786 /* calulate y vector */
1787 if ( ( y < ( lm->sh - 1 ) && bx >= 0.0f ) || ( y == 0 && bx <= 0.0f ) ) {
1788 cluster = SUPER_CLUSTER( x, y );
1789 origin = SUPER_ORIGIN( x, y );
1790 //% normal = SUPER_NORMAL( x, y );
1791 cluster2 = SUPER_CLUSTER( x, y + 1 );
1792 origin2 = *cluster2 < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x, y + 1 );
1793 //% normal2 = *cluster2 < 0 ? SUPER_NORMAL( x, y ) : SUPER_NORMAL( x, y + 1 );
1794 }
1795 else if ( ( y > 0 && bx <= 0.0f ) || ( y == ( lm->sh - 1 ) && bx >= 0.0f ) ) {
1796 cluster = SUPER_CLUSTER( x, y - 1 );
1797 origin = *cluster < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x, y - 1 );
1798 //% normal = *cluster < 0 ? SUPER_NORMAL( x, y ) : SUPER_NORMAL( x, y - 1 );
1799 cluster2 = SUPER_CLUSTER( x, y );
1800 origin2 = SUPER_ORIGIN( x, y );
1801 //% normal2 = SUPER_NORMAL( x, y );
1802 }
1803 else{
1804 Sys_FPrintf( SYS_WRN, "WARNING: Spurious lightmap T vector\n" );
1805 }
1806
1807 VectorSubtract( origin2, origin, originVecs[ 1 ] );
1808
1809 /* calculate new origin */
1810 for ( i = 0; i < 3; i++ )
1811 sampleOrigin[ i ] = sampleOrigin[ i ] + ( bx * originVecs[ 0 ][ i ] ) + ( by * originVecs[ 1 ][ i ] );
1812
1813 /* get cluster */
1814 *sampleCluster = ClusterForPointExtFilter( sampleOrigin, ( LUXEL_EPSILON * 2 ), lm->numLightClusters, lm->lightClusters );
1815 if ( *sampleCluster < 0 ) {
1816 return qfalse;
1817 }
1818
1819 /* calculate new normal */
1820 normal = SUPER_NORMAL( x, y );
1821 VectorCopy( normal, sampleNormal );
1822
1823 /* return ok */
1824 return qtrue;
1825 }
1826
1827
1828 /*
1829 SubsampleRawLuxel_r()
1830 recursively subsamples a luxel until its color gradient is low enough or subsampling limit is reached
1831 */
1832
1833 static void SubsampleRawLuxel_r( rawLightmap_t *lm, trace_t *trace, vec3_t sampleOrigin, int x, int y, float bias, float *lightLuxel, float *lightDeluxel ){
1834 int b, samples, mapped, lighted;
1835 int cluster[ 4 ];
1836 vec4_t luxel[ 4 ];
1837 vec3_t deluxel[ 4 ];
1838 vec3_t origin[ 4 ], normal[ 4 ];
1839 float biasDirs[ 4 ][ 2 ] = { { -1.0f, -1.0f }, { 1.0f, -1.0f }, { -1.0f, 1.0f }, { 1.0f, 1.0f } };
1840 vec3_t color, direction = { 0, 0, 0 }, total;
1841
1842
1843 /* limit check */
1844 if ( lightLuxel[ 3 ] >= lightSamples ) {
1845 return;
1846 }
1847
1848 /* setup */
1849 VectorClear( total );
1850 mapped = 0;
1851 lighted = 0;
1852
1853 /* make 2x2 subsample stamp */
1854 for ( b = 0; b < 4; b++ )
1855 {
1856 /* set origin */
1857 VectorCopy( sampleOrigin, origin[ b ] );
1858
1859 /* calculate position */
1860 if ( !SubmapRawLuxel( lm, x, y, ( bias * biasDirs[ b ][ 0 ] ), ( bias * biasDirs[ b ][ 1 ] ), &cluster[ b ], origin[ b ], normal[ b ] ) ) {
1861 cluster[ b ] = -1;
1862 continue;
1863 }
1864 mapped++;
1865
1866 /* increment sample count */
1867 luxel[ b ][ 3 ] = lightLuxel[ 3 ] + 1.0f;
1868
1869 /* setup trace */
1870 trace->cluster = *cluster;
1871 VectorCopy( origin[ b ], trace->origin );
1872 VectorCopy( normal[ b ], trace->normal );
1873
1874 /* sample light */
1875
1876 LightContributionToSample( trace );
1877 if ( trace->forceSubsampling > 1.0f ) {
1878 /* alphashadow: we subsample as deep as we can */
1879 ++lighted;
1880 ++mapped;
1881 ++mapped;
1882 }
1883
1884 /* add to totals (fixme: make contrast function) */
1885 VectorCopy( trace->color, luxel[ b ] );
1886 if ( lightDeluxel ) {
1887 VectorCopy( trace->directionContribution, deluxel[ b ] );
1888 }
1889 VectorAdd( total, trace->color, total );
1890 if ( ( luxel[ b ][ 0 ] + luxel[ b ][ 1 ] + luxel[ b ][ 2 ] ) > 0.0f ) {
1891 lighted++;
1892 }
1893 }
1894
1895 /* subsample further? */
1896 if ( ( lightLuxel[ 3 ] + 1.0f ) < lightSamples &&
1897 ( total[ 0 ] > 4.0f || total[ 1 ] > 4.0f || total[ 2 ] > 4.0f ) &&
1898 lighted != 0 && lighted != mapped ) {
1899 for ( b = 0; b < 4; b++ )
1900 {
1901 if ( cluster[ b ] < 0 ) {
1902 continue;
1903 }
1904 SubsampleRawLuxel_r( lm, trace, origin[ b ], x, y, ( bias * 0.5f ), luxel[ b ], lightDeluxel ? deluxel[ b ] : NULL );
1905 }
1906 }
1907
1908 /* average */
1909 //% VectorClear( color );
1910 //% samples = 0;
1911 VectorCopy( lightLuxel, color );
1912 if ( lightDeluxel ) {
1913 VectorCopy( lightDeluxel, direction );
1914 }
1915 samples = 1;
1916 for ( b = 0; b < 4; b++ )
1917 {
1918 if ( cluster[ b ] < 0 ) {
1919 continue;
1920 }
1921 VectorAdd( color, luxel[ b ], color );
1922 if ( lightDeluxel ) {
1923 VectorAdd( direction, deluxel[ b ], direction );
1924 }
1925 samples++;
1926 }
1927
1928 /* add to luxel */
1929 if ( samples > 0 ) {
1930 /* average */
1931 color[ 0 ] /= samples;
1932 color[ 1 ] /= samples;
1933 color[ 2 ] /= samples;
1934
1935 /* add to color */
1936 VectorCopy( color, lightLuxel );
1937 lightLuxel[ 3 ] += 1.0f;
1938
1939 if ( lightDeluxel ) {
1940 direction[ 0 ] /= samples;
1941 direction[ 1 ] /= samples;
1942 direction[ 2 ] /= samples;
1943 VectorCopy( direction, lightDeluxel );
1944 }
1945 }
1946 }
1947
1948 /* A mostly Gaussian-like bounded random distribution (sigma is expected standard deviation) */
1949 static void GaussLikeRandom( float sigma, float *x, float *y ){
1950 float r;
1951 r = Random() * 2 * Q_PI;
1952 *x = sigma * 2.73861278752581783822 * cos( r );
1953 *y = sigma * 2.73861278752581783822 * sin( r );
1954 r = Random();
1955 r = 1 - sqrt( r );
1956 r = 1 - sqrt( r );
1957 *x *= r;
1958 *y *= r;
1959 }
1960 static void RandomSubsampleRawLuxel( rawLightmap_t *lm, trace_t *trace, vec3_t sampleOrigin, int x, int y, float bias, float *lightLuxel, float *lightDeluxel ){
1961 int b, mapped;
1962 int cluster;
1963 vec3_t origin, normal;
1964 vec3_t total, totaldirection;
1965 float dx, dy;
1966
1967 VectorClear( total );
1968 VectorClear( totaldirection );
1969 mapped = 0;
1970 for ( b = 0; b < lightSamples; ++b )
1971 {
1972 /* set origin */
1973 VectorCopy( sampleOrigin, origin );
1974 GaussLikeRandom( bias, &dx, &dy );
1975
1976 /* calculate position */
1977 if ( !SubmapRawLuxel( lm, x, y, dx, dy, &cluster, origin, normal ) ) {
1978 cluster = -1;
1979 continue;
1980 }
1981 mapped++;
1982
1983 trace->cluster = cluster;
1984 VectorCopy( origin, trace->origin );
1985 VectorCopy( normal, trace->normal );
1986
1987 LightContributionToSample( trace );
1988 VectorAdd( total, trace->color, total );
1989 if ( lightDeluxel ) {
1990 VectorAdd( totaldirection, trace->directionContribution, totaldirection );
1991 }
1992 }
1993
1994 /* add to luxel */
1995 if ( mapped > 0 ) {
1996 /* average */
1997 lightLuxel[ 0 ] = total[ 0 ] / mapped;
1998 lightLuxel[ 1 ] = total[ 1 ] / mapped;
1999 lightLuxel[ 2 ] = total[ 2 ] / mapped;
2000
2001 if ( lightDeluxel ) {
2002 lightDeluxel[ 0 ] = totaldirection[ 0 ] / mapped;
2003 lightDeluxel[ 1 ] = totaldirection[ 1 ] / mapped;
2004 lightDeluxel[ 2 ] = totaldirection[ 2 ] / mapped;
2005 }
2006 }
2007 }
2008
2009
2010
2011 /*
2012 IlluminateRawLightmap()
2013 illuminates the luxels
2014 */
2015
2016 #define STACK_LL_SIZE ( SUPER_LUXEL_SIZE * 64 * 64 )
2017 #define LIGHT_LUXEL( x, y ) ( lightLuxels + ( ( ( ( y ) * lm->sw ) + ( x ) ) * SUPER_LUXEL_SIZE ) )
2018 #define LIGHT_DELUXEL( x, y ) ( lightDeluxels + ( ( ( ( y ) * lm->sw ) + ( x ) ) * SUPER_DELUXEL_SIZE ) )
2019
2020 void IlluminateRawLightmap( int rawLightmapNum ){
2021 int i, t, x, y, sx, sy, size, luxelFilterRadius, lightmapNum;
2022 int *cluster, *cluster2, mapped, lighted, totalLighted;
2023 size_t llSize, ldSize;
2024 rawLightmap_t *lm;
2025 surfaceInfo_t *info;
2026 qboolean filterColor, filterDir;
2027 float brightness;
2028 float *origin, *normal, *dirt, *luxel, *luxel2, *deluxel, *deluxel2;
2029 unsigned char *flag;
2030 float *lightLuxels, *lightDeluxels, *lightLuxel, *lightDeluxel, samples, filterRadius, weight;
2031 vec3_t color, direction, averageColor, averageDir, total, temp, temp2;
2032 float tests[ 4 ][ 2 ] = { { 0.0f, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } };
2033 trace_t trace;
2034 float stackLightLuxels[ STACK_LL_SIZE ];
2035
2036
2037 /* bail if this number exceeds the number of raw lightmaps */
2038 if ( rawLightmapNum >= numRawLightmaps ) {
2039 return;
2040 }
2041
2042 /* get lightmap */
2043 lm = &rawLightmaps[ rawLightmapNum ];
2044
2045 /* setup trace */
2046 trace.testOcclusion = !noTrace;
2047 trace.forceSunlight = qfalse;
2048 trace.recvShadows = lm->recvShadows;
2049 trace.numSurfaces = lm->numLightSurfaces;
2050 trace.surfaces = &lightSurfaces[ lm->firstLightSurface ];
2051 trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
2052
2053 /* twosided lighting (may or may not be a good idea for lightmapped stuff) */
2054 trace.twoSided = qfalse;
2055 for ( i = 0; i < trace.numSurfaces; i++ )
2056 {
2057 /* get surface */
2058 info = &surfaceInfos[ trace.surfaces[ i ] ];
2059
2060 /* check twosidedness */
2061 if ( info->si->twoSided ) {
2062 trace.twoSided = qtrue;
2063 break;
2064 }
2065 }
2066
2067 /* create a culled light list for this raw lightmap */
2068 CreateTraceLightsForBounds( lm->mins, lm->maxs, lm->plane, lm->numLightClusters, lm->lightClusters, LIGHT_SURFACES, &trace );
2069
2070 /* -----------------------------------------------------------------
2071 fill pass
2072 ----------------------------------------------------------------- */
2073
2074 /* set counts */
2075 numLuxelsIlluminated += ( lm->sw * lm->sh );
2076
2077 /* test debugging state */
2078 if ( debugSurfaces || debugAxis || debugCluster || debugOrigin || dirtDebug || normalmap ) {
2079 /* debug fill the luxels */
2080 for ( y = 0; y < lm->sh; y++ )
2081 {
2082 for ( x = 0; x < lm->sw; x++ )
2083 {
2084 /* get cluster */
2085 cluster = SUPER_CLUSTER( x, y );
2086
2087 /* only fill mapped luxels */
2088 if ( *cluster < 0 ) {
2089 continue;
2090 }
2091
2092 /* get particulars */
2093 luxel = SUPER_LUXEL( 0, x, y );
2094 origin = SUPER_ORIGIN( x, y );
2095 normal = SUPER_NORMAL( x, y );
2096
2097 /* color the luxel with raw lightmap num? */
2098 if ( debugSurfaces ) {
2099 VectorCopy( debugColors[ rawLightmapNum % 12 ], luxel );
2100 }
2101
2102 /* color the luxel with lightmap axis? */
2103 else if ( debugAxis ) {
2104 luxel[ 0 ] = ( lm->axis[ 0 ] + 1.0f ) * 127.5f;
2105 luxel[ 1 ] = ( lm->axis[ 1 ] + 1.0f ) * 127.5f;
2106 luxel[ 2 ] = ( lm->axis[ 2 ] + 1.0f ) * 127.5f;
2107 }
2108
2109 /* color the luxel with luxel cluster? */
2110 else if ( debugCluster ) {
2111 VectorCopy( debugColors[ *cluster % 12 ], luxel );
2112 }
2113
2114 /* color the luxel with luxel origin? */
2115 else if ( debugOrigin ) {
2116 VectorSubtract( lm->maxs, lm->mins, temp );
2117 VectorScale( temp, ( 1.0f / 255.0f ), temp );
2118 VectorSubtract( origin, lm->mins, temp2 );
2119 luxel[ 0 ] = lm->mins[ 0 ] + ( temp[ 0 ] * temp2[ 0 ] );
2120 luxel[ 1 ] = lm->mins[ 1 ] + ( temp[ 1 ] * temp2[ 1 ] );
2121 luxel[ 2 ] = lm->mins[ 2 ] + ( temp[ 2 ] * temp2[ 2 ] );
2122 }
2123
2124 /* color the luxel with the normal */
2125 else if ( normalmap ) {
2126 luxel[ 0 ] = ( normal[ 0 ] + 1.0f ) * 127.5f;
2127 luxel[ 1 ] = ( normal[ 1 ] + 1.0f ) * 127.5f;
2128 luxel[ 2 ] = ( normal[ 2 ] + 1.0f ) * 127.5f;
2129 }
2130
2131 /* otherwise clear it */
2132 else{
2133 VectorClear( luxel );
2134 }
2135
2136 /* add to counts */
2137 luxel[ 3 ] = 1.0f;
2138 }
2139 }
2140 }
2141 else
2142 {
2143 /* allocate temporary per-light luxel storage */
2144 llSize = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float );
2145 ldSize = lm->sw * lm->sh * SUPER_DELUXEL_SIZE * sizeof( float );
2146 if ( llSize <= ( STACK_LL_SIZE * sizeof( float ) ) ) {
2147 lightLuxels = stackLightLuxels;
2148 }
2149 else{
2150 lightLuxels = safe_malloc( llSize );
2151 }
2152 if ( deluxemap ) {
2153 lightDeluxels = safe_malloc( ldSize );
2154 }
2155 else{
2156 lightDeluxels = NULL;
2157 }
2158
2159 /* clear luxels */
2160 //% memset( lm->superLuxels[ 0 ], 0, llSize );
2161
2162 /* set ambient color */
2163 for ( y = 0; y < lm->sh; y++ )
2164 {
2165 for ( x = 0; x < lm->sw; x++ )
2166 {
2167 /* get cluster */
2168 cluster = SUPER_CLUSTER( x, y );
2169 luxel = SUPER_LUXEL( 0, x, y );
2170 normal = SUPER_NORMAL( x, y );
2171 deluxel = SUPER_DELUXEL( x, y );
2172
2173 /* blacken unmapped clusters */
2174 if ( *cluster < 0 ) {
2175 VectorClear( luxel );
2176 }
2177
2178 /* set ambient */
2179 else
2180 {
2181 VectorCopy( ambientColor, luxel );
2182 if ( deluxemap ) {
2183 brightness = RGBTOGRAY( ambientColor ) * ( 1.0f / 255.0f );
2184
2185 // use AT LEAST this amount of contribution from ambient for the deluxemap, fixes points that receive ZERO light
2186 if ( brightness < 0.00390625f ) {
2187 brightness = 0.00390625f;
2188 }
2189
2190 VectorScale( normal, brightness, deluxel );
2191 }
2192 luxel[ 3 ] = 1.0f;
2193 }
2194 }
2195 }
2196
2197 /* clear styled lightmaps */
2198 size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float );
2199 for ( lightmapNum = 1; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2200 {
2201 if ( lm->superLuxels[ lightmapNum ] != NULL ) {
2202 memset( lm->superLuxels[ lightmapNum ], 0, size );
2203 }
2204 }
2205
2206 /* debugging code */
2207 //% if( trace.numLights <= 0 )
2208 //% Sys_Printf( "Lightmap %9d: 0 lights, axis: %.2f, %.2f, %.2f\n", rawLightmapNum, lm->axis[ 0 ], lm->axis[ 1 ], lm->axis[ 2 ] );
2209
2210 /* walk light list */
2211 for ( i = 0; i < trace.numLights; i++ )
2212 {
2213 /* setup trace */
2214 trace.light = trace.lights[ i ];
2215
2216 /* style check */
2217 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2218 {
2219 if ( lm->styles[ lightmapNum ] == trace.light->style ||
2220 lm->styles[ lightmapNum ] == LS_NONE ) {
2221 break;
2222 }
2223 }
2224
2225 /* max of MAX_LIGHTMAPS (4) styles allowed to hit a surface/lightmap */
2226 if ( lightmapNum >= MAX_LIGHTMAPS ) {
2227 Sys_FPrintf( SYS_WRN, "WARNING: Hit per-surface style limit (%d)\n", MAX_LIGHTMAPS );
2228 continue;
2229 }
2230
2231 /* setup */
2232 memset( lightLuxels, 0, llSize );
2233 if ( deluxemap ) {
2234 memset( lightDeluxels, 0, ldSize );
2235 }
2236 totalLighted = 0;
2237
2238 /* determine filter radius */
2239 filterRadius = lm->filterRadius > trace.light->filterRadius
2240 ? lm->filterRadius
2241 : trace.light->filterRadius;
2242 if ( filterRadius < 0.0f ) {
2243 filterRadius = 0.0f;
2244 }
2245
2246 /* set luxel filter radius */
2247 luxelFilterRadius = lm->sampleSize != 0 ? superSample * filterRadius / lm->sampleSize : 0;
2248 if ( luxelFilterRadius == 0 && ( filterRadius > 0.0f || filter ) ) {
2249 luxelFilterRadius = 1;
2250 }
2251
2252 /* allocate sampling flags storage */
2253 if ( lightSamples > 1 || lightRandomSamples ) {
2254 size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( unsigned char );
2255 if ( lm->superFlags == NULL ) {
2256 lm->superFlags = safe_malloc( size );
2257 }
2258 memset( (void *) lm->superFlags, 0, size );
2259 }
2260
2261 /* initial pass, one sample per luxel */
2262 for ( y = 0; y < lm->sh; y++ )
2263 {
2264 for ( x = 0; x < lm->sw; x++ )
2265 {
2266 /* get cluster */
2267 cluster = SUPER_CLUSTER( x, y );
2268 if ( *cluster < 0 ) {
2269 continue;
2270 }
2271
2272 /* get particulars */
2273 lightLuxel = LIGHT_LUXEL( x, y );
2274 lightDeluxel = LIGHT_DELUXEL( x, y );
2275 origin = SUPER_ORIGIN( x, y );
2276 normal = SUPER_NORMAL( x, y );
2277 flag = SUPER_FLAG( x, y );
2278
2279 /* set contribution count */
2280 lightLuxel[ 3 ] = 1.0f;
2281
2282 /* setup trace */
2283 trace.cluster = *cluster;
2284 VectorCopy( origin, trace.origin );
2285 VectorCopy( normal, trace.normal );
2286
2287 /* get light for this sample */
2288 LightContributionToSample( &trace );
2289 VectorCopy( trace.color, lightLuxel );
2290
2291 /* add the contribution to the deluxemap */
2292 if ( deluxemap ) {
2293 VectorCopy( trace.directionContribution, lightDeluxel );
2294 }
2295
2296 /* check for evilness */
2297 if ( trace.forceSubsampling > 1.0f && ( lightSamples > 1 || lightRandomSamples ) ) {
2298 totalLighted++;
2299 *flag |= FLAG_FORCE_SUBSAMPLING; /* force */
2300 }
2301 /* add to count */
2302 else if ( trace.color[ 0 ] || trace.color[ 1 ] || trace.color[ 2 ] ) {
2303 totalLighted++;
2304 }
2305 }
2306 }
2307
2308 /* don't even bother with everything else if nothing was lit */
2309 if ( totalLighted == 0 ) {
2310 continue;
2311 }
2312
2313 /* secondary pass, adaptive supersampling (fixme: use a contrast function to determine if subsampling is necessary) */
2314 /* 2003-09-27: changed it so filtering disamples supersampling, as it would waste time */
2315 if ( lightSamples > 1 || lightRandomSamples ) {
2316 /* walk luxels */
2317 for ( y = 0; y < ( lm->sh - 1 ); y++ )
2318 {
2319 for ( x = 0; x < ( lm->sw - 1 ); x++ )
2320 {
2321 /* setup */
2322 mapped = 0;
2323 lighted = 0;
2324 VectorClear( total );
2325
2326 /* test 2x2 stamp */
2327 for ( t = 0; t < 4; t++ )
2328 {
2329 /* set sample coords */
2330 sx = x + tests[ t ][ 0 ];
2331 sy = y + tests[ t ][ 1 ];
2332
2333 /* get cluster */
2334 cluster = SUPER_CLUSTER( sx, sy );
2335 if ( *cluster < 0 ) {
2336 continue;
2337 }
2338 mapped++;
2339
2340 /* get luxel */
2341 flag = SUPER_FLAG( sx, sy );
2342 if ( *flag & FLAG_FORCE_SUBSAMPLING ) {
2343 /* force a lighted/mapped discrepancy so we subsample */
2344 ++lighted;
2345 ++mapped;
2346 ++mapped;
2347 }
2348 lightLuxel = LIGHT_LUXEL( sx, sy );
2349 VectorAdd( total, lightLuxel, total );
2350 if ( ( lightLuxel[ 0 ] + lightLuxel[ 1 ] + lightLuxel[ 2 ] ) > 0.0f ) {
2351 lighted++;
2352 }
2353 }
2354
2355 /* if total color is under a certain amount, then don't bother subsampling */
2356 if ( total[ 0 ] <= 4.0f && total[ 1 ] <= 4.0f && total[ 2 ] <= 4.0f ) {
2357 continue;
2358 }
2359
2360 /* if all 4 pixels are either in shadow or light, then don't subsample */
2361 if ( lighted != 0 && lighted != mapped ) {
2362 for ( t = 0; t < 4; t++ )
2363 {
2364 /* set sample coords */
2365 sx = x + tests[ t ][ 0 ];
2366 sy = y + tests[ t ][ 1 ];
2367
2368 /* get luxel */
2369 cluster = SUPER_CLUSTER( sx, sy );
2370 if ( *cluster < 0 ) {
2371 continue;
2372 }
2373 flag = SUPER_FLAG( sx, sy );
2374 if ( *flag & FLAG_ALREADY_SUBSAMPLED ) { // already subsampled
2375 continue;
2376 }
2377 lightLuxel = LIGHT_LUXEL( sx, sy );
2378 lightDeluxel = LIGHT_DELUXEL( sx, sy );
2379 origin = SUPER_ORIGIN( sx, sy );
2380
2381 /* only subsample shadowed luxels */
2382 //% if( (lightLuxel[ 0 ] + lightLuxel[ 1 ] + lightLuxel[ 2 ]) <= 0.0f )
2383 //% continue;
2384
2385 /* subsample it */
2386 if ( lightRandomSamples ) {
2387 RandomSubsampleRawLuxel( lm, &trace, origin, sx, sy, 0.5f * lightSamplesSearchBoxSize, lightLuxel, deluxemap ? lightDeluxel : NULL );
2388 }
2389 else{
2390 SubsampleRawLuxel_r( lm, &trace, origin, sx, sy, 0.25f * lightSamplesSearchBoxSize, lightLuxel, deluxemap ? lightDeluxel : NULL );
2391 }
2392
2393 *flag |= FLAG_ALREADY_SUBSAMPLED;
2394
2395 /* debug code to colorize subsampled areas to yellow */
2396 //% luxel = SUPER_LUXEL( lightmapNum, sx, sy );
2397 //% VectorSet( luxel, 255, 204, 0 );
2398 }
2399 }
2400 }
2401 }
2402 }
2403
2404 /* tertiary pass, apply dirt map (ambient occlusion) */
2405 if ( 0 && dirty ) {
2406 /* walk luxels */
2407 for ( y = 0; y < lm->sh; y++ )
2408 {
2409 for ( x = 0; x < lm->sw; x++ )
2410 {
2411 /* get cluster */
2412 cluster = SUPER_CLUSTER( x, y );
2413 if ( *cluster < 0 ) {
2414 continue;
2415 }
2416
2417 /* get particulars */
2418 lightLuxel = LIGHT_LUXEL( x, y );
2419 dirt = SUPER_DIRT( x, y );
2420
2421 /* scale light value */
2422 VectorScale( lightLuxel, *dirt, lightLuxel );
2423 }
2424 }
2425 }
2426
2427 /* allocate sampling lightmap storage */
2428 if ( lm->superLuxels[ lightmapNum ] == NULL ) {
2429 /* allocate sampling lightmap storage */
2430 size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float );
2431 lm->superLuxels[ lightmapNum ] = safe_malloc0( size );
2432 }
2433
2434 /* set style */
2435 if ( lightmapNum > 0 ) {
2436 lm->styles[ lightmapNum ] = trace.light->style;
2437 //% Sys_Printf( "Surface %6d has lightstyle %d\n", rawLightmapNum, trace.light->style );
2438 }
2439
2440 /* copy to permanent luxels */
2441 for ( y = 0; y < lm->sh; y++ )
2442 {
2443 for ( x = 0; x < lm->sw; x++ )
2444 {
2445 /* get cluster and origin */
2446 cluster = SUPER_CLUSTER( x, y );
2447 if ( *cluster < 0 ) {
2448 continue;
2449 }
2450 origin = SUPER_ORIGIN( x, y );
2451
2452 /* filter? */
2453 if ( luxelFilterRadius ) {
2454 /* setup */
2455 VectorClear( averageColor );
2456 VectorClear( averageDir );
2457 samples = 0.0f;
2458
2459 /* cheaper distance-based filtering */
2460 for ( sy = ( y - luxelFilterRadius ); sy <= ( y + luxelFilterRadius ); sy++ )
2461 {
2462 if ( sy < 0 || sy >= lm->sh ) {
2463 continue;
2464 }
2465
2466 for ( sx = ( x - luxelFilterRadius ); sx <= ( x + luxelFilterRadius ); sx++ )
2467 {
2468 if ( sx < 0 || sx >= lm->sw ) {
2469 continue;
2470 }
2471
2472 /* get particulars */
2473 cluster = SUPER_CLUSTER( sx, sy );
2474 if ( *cluster < 0 ) {
2475 continue;
2476 }
2477 lightLuxel = LIGHT_LUXEL( sx, sy );
2478 lightDeluxel = LIGHT_DELUXEL( sx, sy );
2479
2480 /* create weight */
2481 weight = ( abs( sx - x ) == luxelFilterRadius ? 0.5f : 1.0f );
2482 weight *= ( abs( sy - y ) == luxelFilterRadius ? 0.5f : 1.0f );
2483
2484 /* scale luxel by filter weight */
2485 VectorScale( lightLuxel, weight, color );
2486 VectorAdd( averageColor, color, averageColor );
2487 if ( deluxemap ) {
2488 VectorScale( lightDeluxel, weight, direction );
2489 VectorAdd( averageDir, direction, averageDir );
2490 }
2491 samples += weight;
2492 }
2493 }
2494
2495 /* any samples? */
2496 if ( samples <= 0.0f ) {
2497 continue;
2498 }
2499
2500 /* scale into luxel */
2501 luxel = SUPER_LUXEL( lightmapNum, x, y );
2502 luxel[ 3 ] = 1.0f;
2503
2504 /* handle negative light */
2505 if ( trace.light->flags & LIGHT_NEGATIVE ) {
2506 luxel[ 0 ] -= averageColor[ 0 ] / samples;
2507 luxel[ 1 ] -= averageColor[ 1 ] / samples;
2508 luxel[ 2 ] -= averageColor[ 2 ] / samples;
2509 }
2510
2511 /* handle normal light */
2512 else
2513 {
2514 luxel[ 0 ] += averageColor[ 0 ] / samples;
2515 luxel[ 1 ] += averageColor[ 1 ] / samples;
2516 luxel[ 2 ] += averageColor[ 2 ] / samples;
2517 }
2518
2519 if ( deluxemap ) {
2520 /* scale into luxel */
2521 deluxel = SUPER_DELUXEL( x, y );
2522 deluxel[ 0 ] += averageDir[ 0 ] / samples;
2523 deluxel[ 1 ] += averageDir[ 1 ] / samples;
2524 deluxel[ 2 ] += averageDir[ 2 ] / samples;
2525 }
2526 }
2527
2528 /* single sample */
2529 else
2530 {
2531 /* get particulars */
2532 lightLuxel = LIGHT_LUXEL( x, y );
2533 lightDeluxel = LIGHT_DELUXEL( x, y );
2534 luxel = SUPER_LUXEL( lightmapNum, x, y );
2535 deluxel = SUPER_DELUXEL( x, y );
2536
2537 /* handle negative light */
2538 if ( trace.light->flags & LIGHT_NEGATIVE ) {
2539 VectorScale( averageColor, -1.0f, averageColor );
2540 }
2541
2542 /* add color */
2543 luxel[ 3 ] = 1.0f;
2544
2545 /* handle negative light */
2546 if ( trace.light->flags & LIGHT_NEGATIVE ) {
2547 VectorSubtract( luxel, lightLuxel, luxel );
2548 }
2549
2550 /* handle normal light */
2551 else{
2552 VectorAdd( luxel, lightLuxel, luxel );
2553 }
2554
2555 if ( deluxemap ) {
2556 VectorAdd( deluxel, lightDeluxel, deluxel );
2557 }
2558 }
2559 }
2560 }
2561 }
2562
2563 /* free temporary luxels */
2564 if ( lightLuxels != stackLightLuxels ) {
2565 free( lightLuxels );
2566 }
2567
2568 if ( deluxemap ) {
2569 free( lightDeluxels );
2570 }
2571 }
2572
2573 /* free light list */
2574 FreeTraceLights( &trace );
2575
2576 /* floodlight pass */
2577 if ( floodlighty ) {
2578 FloodlightIlluminateLightmap( lm );
2579 }
2580
2581 if ( debugnormals ) {
2582 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2583 {
2584 /* early out */
2585 if ( lm->superLuxels[ lightmapNum ] == NULL ) {
2586 continue;
2587 }
2588
2589 for ( y = 0; y < lm->sh; y++ )
2590 {
2591 for ( x = 0; x < lm->sw; x++ )
2592 {
2593 /* get cluster */
2594 cluster = SUPER_CLUSTER( x, y );
2595 //% if( *cluster < 0 )
2596 //% continue;
2597
2598 /* get particulars */
2599 luxel = SUPER_LUXEL( lightmapNum, x, y );
2600 normal = SUPER_NORMAL( x, y );
2601
2602 luxel[0] = ( normal[0] * 127 ) + 127;
2603 luxel[1] = ( normal[1] * 127 ) + 127;
2604 luxel[2] = ( normal[2] * 127 ) + 127;
2605 }
2606 }
2607 }
2608 }
2609
2610 /* -----------------------------------------------------------------
2611 dirt pass
2612 ----------------------------------------------------------------- */
2613
2614 if ( dirty ) {
2615 /* walk lightmaps */
2616 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2617 {
2618 /* early out */
2619 if ( lm->superLuxels[ lightmapNum ] == NULL ) {
2620 continue;
2621 }
2622
2623 /* apply dirt to each luxel */
2624 for ( y = 0; y < lm->sh; y++ )
2625 {
2626 for ( x = 0; x < lm->sw; x++ )
2627 {
2628 /* get cluster */
2629 cluster = SUPER_CLUSTER( x, y );
2630
2631 /* get particulars */
2632 luxel = SUPER_LUXEL( lightmapNum, x, y );
2633 dirt = SUPER_DIRT( x, y );
2634
2635 /* apply dirt */
2636 VectorScale( luxel, *dirt, luxel );
2637
2638 /* debugging */
2639 if ( dirtDebug ) {
2640 VectorSet( luxel, *dirt * 255.0f, *dirt * 255.0f, *dirt * 255.0f );
2641 }
2642 }
2643 }
2644 }
2645 }
2646
2647 /* -----------------------------------------------------------------
2648 filter pass
2649 ----------------------------------------------------------------- */
2650
2651 /* walk lightmaps */
2652 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2653 {
2654 /* early out */
2655 if ( lm->superLuxels[ lightmapNum ] == NULL ) {
2656 continue;
2657 }
2658
2659 /* average occluded luxels from neighbors */
2660 for ( y = 0; y < lm->sh; y++ )
2661 {
2662 for ( x = 0; x < lm->sw; x++ )
2663 {
2664 /* get particulars */
2665 cluster = SUPER_CLUSTER( x, y );
2666 luxel = SUPER_LUXEL( lightmapNum, x, y );
2667 deluxel = SUPER_DELUXEL( x, y );
2668 normal = SUPER_NORMAL( x, y );
2669
2670 /* determine if filtering is necessary */
2671 filterColor = qfalse;
2672 filterDir = qfalse;
2673 if ( *cluster < 0 ||
2674 ( lm->splotchFix && ( luxel[ 0 ] <= ambientColor[ 0 ] || luxel[ 1 ] <= ambientColor[ 1 ] || luxel[ 2 ] <= ambientColor[ 2 ] ) ) ) {
2675 filterColor = qtrue;
2676 }
2677
2678 if ( deluxemap && lightmapNum == 0 && ( *cluster < 0 || filter ) ) {
2679 filterDir = qtrue;
2680 }
2681
2682 if ( !filterColor && !filterDir ) {
2683 continue;
2684 }
2685
2686 /* choose seed amount */
2687 VectorClear( averageColor );
2688 VectorClear( averageDir );
2689 samples = 0.0f;
2690
2691 /* walk 3x3 matrix */
2692 for ( sy = ( y - 1 ); sy <= ( y + 1 ); sy++ )
2693 {
2694 if ( sy < 0 || sy >= lm->sh ) {
2695 continue;
2696 }
2697
2698 for ( sx = ( x - 1 ); sx <= ( x + 1 ); sx++ )
2699 {
2700 if ( sx < 0 || sx >= lm->sw || ( sx == x && sy == y ) ) {
2701 continue;
2702 }
2703
2704 /* get neighbor's particulars */
2705 cluster2 = SUPER_CLUSTER( sx, sy );
2706 luxel2 = SUPER_LUXEL( lightmapNum, sx, sy );
2707 deluxel2 = SUPER_DELUXEL( sx, sy );
2708
2709 /* ignore unmapped/unlit luxels */
2710 if ( *cluster2 < 0 || luxel2[ 3 ] == 0.0f ||
2711 ( lm->splotchFix && VectorCompare( luxel2, ambientColor ) ) ) {
2712 continue;
2713 }
2714
2715 /* add its distinctiveness to our own */
2716 VectorAdd( averageColor, luxel2, averageColor );
2717 samples += luxel2[ 3 ];
2718 if ( filterDir ) {
2719 VectorAdd( averageDir, deluxel2, averageDir );
2720 }
2721 }
2722 }
2723
2724 /* fall through */
2725 if ( samples <= 0.0f ) {
2726 continue;
2727 }
2728
2729 /* dark lightmap seams */
2730 if ( dark ) {
2731 if ( lightmapNum == 0 ) {
2732 VectorMA( averageColor, 2.0f, ambientColor, averageColor );
2733 }
2734 samples += 2.0f;
2735 }
2736
2737 /* average it */
2738 if ( filterColor ) {
2739 VectorDivide( averageColor, samples, luxel );
2740 luxel[ 3 ] = 1.0f;
2741 }
2742 if ( filterDir ) {
2743 VectorDivide( averageDir, samples, deluxel );
2744 }
2745
2746 /* set cluster to -3 */
2747 if ( *cluster < 0 ) {
2748 *cluster = CLUSTER_FLOODED;
2749 }
2750 }
2751 }
2752 }
2753 }
2754
2755
2756
2757 /*
2758 IlluminateVertexes()
2759 light the surface vertexes
2760 */
2761
2762 #define VERTEX_NUDGE 4.0f
2763
2764 void IlluminateVertexes( int num ){
2765 int i, x, y, z, x1, y1, z1, sx, sy, radius, maxRadius, *cluster;
2766 int lightmapNum, numAvg;
2767 float samples, *vertLuxel, *radVertLuxel, *luxel, dirt;
2768 vec3_t temp, temp2, colors[ MAX_LIGHTMAPS ], avgColors[ MAX_LIGHTMAPS ];
2769 bspDrawSurface_t *ds;
2770 surfaceInfo_t *info;
2771 rawLightmap_t *lm;
2772 bspDrawVert_t *verts;
2773 trace_t trace;
2774 float floodLightAmount;
2775 vec3_t floodColor;
2776
2777
2778 /* get surface, info, and raw lightmap */
2779 ds = &bspDrawSurfaces[ num ];
2780 info = &surfaceInfos[ num ];
2781 lm = info->lm;
2782
2783 /* -----------------------------------------------------------------
2784 illuminate the vertexes
2785 ----------------------------------------------------------------- */
2786
2787 /* calculate vertex lighting for surfaces without lightmaps */
2788 if ( lm == NULL || cpmaHack ) {
2789 /* setup trace */
2790 trace.testOcclusion = ( cpmaHack && lm != NULL ) ? qfalse : !noTrace;
2791 trace.forceSunlight = info->si->forceSunlight;
2792 trace.recvShadows = info->recvShadows;
2793 trace.numSurfaces = 1;
2794 trace.surfaces = &num;
2795 trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
2796
2797 /* twosided lighting */
2798 trace.twoSided = info->si->twoSided;
2799
2800 /* make light list for this surface */
2801 CreateTraceLightsForSurface( num, &trace );
2802
2803 /* setup */
2804 verts = yDrawVerts + ds->firstVert;
2805 numAvg = 0;
2806 memset( avgColors, 0, sizeof( avgColors ) );
2807
2808 /* walk the surface verts */
2809 for ( i = 0; i < ds->numVerts; i++ )
2810 {
2811 /* get vertex luxel */
2812 radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2813
2814 /* color the luxel with raw lightmap num? */
2815 if ( debugSurfaces ) {
2816 VectorCopy( debugColors[ num % 12 ], radVertLuxel );
2817 }
2818
2819 /* color the luxel with luxel origin? */
2820 else if ( debugOrigin ) {
2821 VectorSubtract( info->maxs, info->mins, temp );
2822 VectorScale( temp, ( 1.0f / 255.0f ), temp );
2823 VectorSubtract( verts[ i ].xyz, info->mins, temp2 );
2824 radVertLuxel[ 0 ] = info->mins[ 0 ] + ( temp[ 0 ] * temp2[ 0 ] );
2825 radVertLuxel[ 1 ] = info->mins[ 1 ] + ( temp[ 1 ] * temp2[ 1 ] );
2826 radVertLuxel[ 2 ] = info->mins[ 2 ] + ( temp[ 2 ] * temp2[ 2 ] );
2827 }
2828
2829 /* color the luxel with the normal */
2830 else if ( normalmap ) {
2831 radVertLuxel[ 0 ] = ( verts[ i ].normal[ 0 ] + 1.0f ) * 127.5f;
2832 radVertLuxel[ 1 ] = ( verts[ i ].normal[ 1 ] + 1.0f ) * 127.5f;
2833 radVertLuxel[ 2 ] = ( verts[ i ].normal[ 2 ] + 1.0f ) * 127.5f;
2834 }
2835
2836 else if ( info->si->noVertexLight ) {
2837 VectorSet( radVertLuxel, 127.5f, 127.5f, 127.5f );
2838 }
2839
2840 else if ( noVertexLighting > 0 ) {
2841 VectorSet( radVertLuxel, 127.5f * noVertexLighting, 127.5f * noVertexLighting, 127.5f * noVertexLighting );
2842 }
2843
2844 /* illuminate the vertex */
2845 else
2846 {
2847 /* clear vertex luxel */
2848 VectorSet( radVertLuxel, -1.0f, -1.0f, -1.0f );
2849
2850 /* try at initial origin */
2851 trace.cluster = ClusterForPointExtFilter( verts[ i ].xyz, VERTEX_EPSILON, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ] );
2852 if ( trace.cluster >= 0 ) {
2853 /* setup trace */
2854 VectorCopy( verts[ i ].xyz, trace.origin );
2855 VectorCopy( verts[ i ].normal, trace.normal );
2856
2857 /* r7 dirt */
2858 if ( dirty && !bouncing ) {
2859 dirt = DirtForSample( &trace );
2860 }
2861 else{
2862 dirt = 1.0f;
2863 }
2864
2865 /* jal: floodlight */
2866 floodLightAmount = 0.0f;
2867 VectorClear( floodColor );
2868 if ( floodlighty && !bouncing ) {
2869 floodLightAmount = floodlightIntensity * FloodLightForSample( &trace, floodlightDistance, floodlight_lowquality );
2870 VectorScale( floodlightRGB, floodLightAmount, floodColor );
2871 }
2872
2873 /* trace */
2874 LightingAtSample( &trace, ds->vertexStyles, colors );
2875
2876 /* store */
2877 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2878 {
2879 /* r7 dirt */
2880 VectorScale( colors[ lightmapNum ], dirt, colors[ lightmapNum ] );
2881
2882 /* jal: floodlight */
2883 VectorAdd( colors[ lightmapNum ], floodColor, colors[ lightmapNum ] );
2884
2885 /* store */
2886 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2887 VectorCopy( colors[ lightmapNum ], radVertLuxel );
2888 VectorAdd( avgColors[ lightmapNum ], colors[ lightmapNum ], colors[ lightmapNum ] );
2889 }
2890 }
2891
2892 /* is this sample bright enough? */
2893 radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2894 if ( radVertLuxel[ 0 ] <= ambientColor[ 0 ] &&
2895 radVertLuxel[ 1 ] <= ambientColor[ 1 ] &&
2896 radVertLuxel[ 2 ] <= ambientColor[ 2 ] ) {
2897 /* nudge the sample point around a bit */
2898 for ( x = 0; x < 5; x++ )
2899 {
2900 /* two's complement 0, 1, -1, 2, -2, etc */
2901 x1 = ( ( x >> 1 ) ^ ( x & 1 ? -1 : 0 ) ) + ( x & 1 );
2902
2903 for ( y = 0; y < 5; y++ )
2904 {
2905 y1 = ( ( y >> 1 ) ^ ( y & 1 ? -1 : 0 ) ) + ( y & 1 );
2906
2907 for ( z = 0; z < 5; z++ )
2908 {
2909 z1 = ( ( z >> 1 ) ^ ( z & 1 ? -1 : 0 ) ) + ( z & 1 );
2910
2911 /* nudge origin */
2912 trace.origin[ 0 ] = verts[ i ].xyz[ 0 ] + ( VERTEX_NUDGE * x1 );
2913 trace.origin[ 1 ] = verts[ i ].xyz[ 1 ] + ( VERTEX_NUDGE * y1 );
2914 trace.origin[ 2 ] = verts[ i ].xyz[ 2 ] + ( VERTEX_NUDGE * z1 );
2915
2916 /* try at nudged origin */
2917 trace.cluster = ClusterForPointExtFilter( trace.origin, VERTEX_EPSILON, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ] );
2918 if ( trace.cluster < 0 ) {
2919 continue;
2920 }
2921
2922 /* r7 dirt */
2923 if ( dirty && !bouncing ) {
2924 dirt = DirtForSample( &trace );
2925 }
2926 else{
2927 dirt = 1.0f;
2928 }
2929
2930 /* jal: floodlight */
2931 floodLightAmount = 0.0f;
2932 VectorClear( floodColor );
2933 if ( floodlighty && !bouncing ) {
2934 floodLightAmount = floodlightIntensity * FloodLightForSample( &trace, floodlightDistance, floodlight_lowquality );
2935 VectorScale( floodlightRGB, floodLightAmount, floodColor );
2936 }
2937
2938 /* trace */
2939 LightingAtSample( &trace, ds->vertexStyles, colors );
2940
2941 /* store */
2942 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2943 {
2944 /* r7 dirt */
2945 VectorScale( colors[ lightmapNum ], dirt, colors[ lightmapNum ] );
2946
2947 /* jal: floodlight */
2948 VectorAdd( colors[ lightmapNum ], floodColor, colors[ lightmapNum ] );
2949
2950 /* store */
2951 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2952 VectorCopy( colors[ lightmapNum ], radVertLuxel );
2953 }
2954
2955 /* bright enough? */
2956 radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2957 if ( radVertLuxel[ 0 ] > ambientColor[ 0 ] ||
2958 radVertLuxel[ 1 ] > ambientColor[ 1 ] ||
2959 radVertLuxel[ 2 ] > ambientColor[ 2 ] ) {
2960 x = y = z = 1000;
2961 }
2962 }
2963 }
2964 }
2965 }
2966
2967 /* add to average? */
2968 radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2969 if ( radVertLuxel[ 0 ] > ambientColor[ 0 ] ||
2970 radVertLuxel[ 1 ] > ambientColor[ 1 ] ||
2971 radVertLuxel[ 2 ] > ambientColor[ 2 ] ) {
2972 numAvg++;
2973 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2974 {
2975 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2976 VectorAdd( avgColors[ lightmapNum ], radVertLuxel, avgColors[ lightmapNum ] );
2977 }
2978 }
2979 }
2980
2981 /* another happy customer */
2982 numVertsIlluminated++;
2983 }
2984
2985 /* set average color */
2986 if ( numAvg > 0 ) {
2987 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2988 VectorScale( avgColors[ lightmapNum ], ( 1.0f / numAvg ), avgColors[ lightmapNum ] );
2989 }
2990 else
2991 {
2992 VectorCopy( ambientColor, avgColors[ 0 ] );
2993 }
2994
2995 /* clean up and store vertex color */
2996 for ( i = 0; i < ds->numVerts; i++ )
2997 {
2998 /* get vertex luxel */
2999 radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
3000
3001 /* store average in occluded vertexes */
3002 if ( radVertLuxel[ 0 ] < 0.0f ) {
3003 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
3004 {
3005 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
3006 VectorCopy( avgColors[ lightmapNum ], radVertLuxel );
3007
3008 /* debug code */
3009 //% VectorSet( radVertLuxel, 255.0f, 0.0f, 0.0f );
3010 }
3011 }
3012
3013 /* store it */
3014 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
3015 {
3016 /* get luxels */
3017 vertLuxel = VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
3018 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
3019
3020 /* store */
3021 if ( bouncing || bounce == 0 || !bounceOnly ) {
3022 VectorAdd( vertLuxel, radVertLuxel, vertLuxel );
3023 }
3024 if ( !info->si->noVertexLight ) {
3025 ColorToBytes( vertLuxel, verts[ i ].color[ lightmapNum ], info->si->vertexScale );
3026 }
3027 }
3028 }
3029
3030 /* free light list */
3031 FreeTraceLights( &trace );
3032
3033 /* return to sender */
3034 return;
3035 }
3036
3037 /* -----------------------------------------------------------------
3038 reconstitute vertex lighting from the luxels
3039 ----------------------------------------------------------------- */
3040
3041 /* set styles from lightmap */
3042 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
3043 ds->vertexStyles[ lightmapNum ] = lm->styles[ lightmapNum ];
3044
3045 /* get max search radius */
3046 maxRadius = lm->sw;
3047 maxRadius = maxRadius > lm->sh ? maxRadius : lm->sh;
3048
3049 /* walk the surface verts */
3050 verts = yDrawVerts + ds->firstVert;
3051 for ( i = 0; i < ds->numVerts; i++ )
3052 {
3053 /* do each lightmap */
3054 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
3055 {
3056 /* early out */
3057 if ( lm->superLuxels[ lightmapNum ] == NULL ) {
3058 continue;
3059 }
3060
3061 /* get luxel coords */
3062 x = verts[ i ].lightmap[ lightmapNum ][ 0 ];
3063 y = verts[ i ].lightmap[ lightmapNum ][ 1 ];
3064 if ( x < 0 ) {
3065 x = 0;
3066 }
3067 else if ( x >= lm->sw ) {
3068 x = lm->sw - 1;
3069 }
3070 if ( y < 0 ) {
3071 y = 0;
3072 }
3073 else if ( y >= lm->sh ) {
3074 y = lm->sh - 1;
3075 }
3076
3077 /* get vertex luxels */
3078 vertLuxel = VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
3079 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
3080
3081 /* color the luxel with the normal? */
3082 if ( normalmap ) {
3083 radVertLuxel[ 0 ] = ( verts[ i ].normal[ 0 ] + 1.0f ) * 127.5f;
3084 radVertLuxel[ 1 ] = ( verts[ i ].normal[ 1 ] + 1.0f ) * 127.5f;
3085 radVertLuxel[ 2 ] = ( verts[ i ].normal[ 2 ] + 1.0f ) * 127.5f;
3086 }
3087
3088 /* color the luxel with surface num? */
3089 else if ( debugSurfaces ) {
3090 VectorCopy( debugColors[ num % 12 ], radVertLuxel );
3091 }
3092
3093 else if ( info->si->noVertexLight ) {
3094 VectorSet( radVertLuxel, 127.5f, 127.5f, 127.5f );
3095 }
3096
3097 else if ( noVertexLighting > 0 ) {
3098 VectorSet( radVertLuxel, 127.5f * noVertexLighting, 127.5f * noVertexLighting, 127.5f * noVertexLighting );
3099 }
3100
3101 /* divine color from the superluxels */
3102 else
3103 {
3104 /* increasing radius */
3105 VectorClear( radVertLuxel );
3106 samples = 0.0f;
3107 for ( radius = 0; radius < maxRadius && samples <= 0.0f; radius++ )
3108 {
3109 /* sample within radius */
3110 for ( sy = ( y - radius ); sy <= ( y + radius ); sy++ )
3111 {
3112 if ( sy < 0 || sy >= lm->sh ) {
3113 continue;
3114 }
3115
3116 for ( sx = ( x - radius ); sx <= ( x + radius ); sx++ )
3117 {
3118 if ( sx < 0 || sx >= lm->sw ) {
3119 continue;
3120 }
3121
3122 /* get luxel particulars */
3123 luxel = SUPER_LUXEL( lightmapNum, sx, sy );
3124 cluster = SUPER_CLUSTER( sx, sy );
3125 if ( *cluster < 0 ) {
3126 continue;
3127 }
3128
3129 /* testing: must be brigher than ambient color */
3130 //% if( luxel[ 0 ] <= ambientColor[ 0 ] || luxel[ 1 ] <= ambientColor[ 1 ] || luxel[ 2 ] <= ambientColor[ 2 ] )
3131 //% continue;
3132
3133 /* add its distinctiveness to our own */
3134 VectorAdd( radVertLuxel, luxel, radVertLuxel );
3135 samples += luxel[ 3 ];
3136 }
3137 }
3138 }
3139
3140 /* any color? */
3141 if ( samples > 0.0f ) {
3142 VectorDivide( radVertLuxel, samples, radVertLuxel );
3143 }
3144 else{
3145 VectorCopy( ambientColor, radVertLuxel );
3146 }
3147 }
3148
3149 /* store into floating point storage */
3150 VectorAdd( vertLuxel, radVertLuxel, vertLuxel );
3151 numVertsIlluminated++;
3152
3153 /* store into bytes (for vertex approximation) */
3154 if ( !info->si->noVertexLight ) {
3155 ColorToBytes( vertLuxel, verts[ i ].color[ lightmapNum ], 1.0f );
3156 }
3157 }
3158 }
3159 }
3160
3161
3162
3163 /* -------------------------------------------------------------------------------
3164
3165 light optimization (-fast)
3166
3167 creates a list of lights that will affect a surface and stores it in tw
3168 this is to optimize surface lighting by culling out as many of the
3169 lights in the world as possible from further calculation
3170
3171 ------------------------------------------------------------------------------- */
3172
3173 /*
3174 SetupBrushes()
3175 determines opaque brushes in the world and find sky shaders for sunlight calculations
3176 */
3177
3178 void SetupBrushesFlags( unsigned int mask_any, unsigned int test_any, unsigned int mask_all, unsigned int test_all ){
3179 int i, j, b;
3180 unsigned int compileFlags, allCompileFlags;
3181 bspBrush_t *brush;
3182 bspBrushSide_t *side;
3183 bspShader_t *shader;
3184 shaderInfo_t *si;
3185
3186
3187 /* note it */
3188 Sys_FPrintf( SYS_VRB, "--- SetupBrushes ---\n" );
3189
3190 /* allocate */
3191 if ( opaqueBrushes == NULL ) {
3192 opaqueBrushes = safe_malloc( numBSPBrushes / 8 + 1 );
3193 }
3194
3195 /* clear */
3196 memset( opaqueBrushes, 0, numBSPBrushes / 8 + 1 );
3197 numOpaqueBrushes = 0;
3198
3199 /* walk the list of worldspawn brushes */
3200 for ( i = 0; i < bspModels[ 0 ].numBSPBrushes; i++ )
3201 {
3202 /* get brush */
3203 b = bspModels[ 0 ].firstBSPBrush + i;
3204 brush = &bspBrushes[ b ];
3205
3206 /* check all sides */
3207 compileFlags = 0;
3208 allCompileFlags = ~( 0u );
3209 for ( j = 0; j < brush->numSides; j++ )
3210 {
3211 /* do bsp shader calculations */
3212 side = &bspBrushSides[ brush->firstSide + j ];
3213 shader = &bspShaders[ side->shaderNum ];
3214
3215 /* get shader info */
3216 si = ShaderInfoForShaderNull( shader->shader );
3217 if ( si == NULL ) {
3218 continue;
3219 }
3220
3221 /* or together compile flags */
3222 compileFlags |= si->compileFlags;
3223 allCompileFlags &= si->compileFlags;
3224 }
3225
3226 /* determine if this brush is opaque to light */
3227 if ( ( compileFlags & mask_any ) == test_any && ( allCompileFlags & mask_all ) == test_all ) {
3228 opaqueBrushes[ b >> 3 ] |= ( 1 << ( b & 7 ) );
3229 numOpaqueBrushes++;
3230 maxOpaqueBrush = i;
3231 }
3232 }
3233
3234 /* emit some statistics */
3235 Sys_FPrintf( SYS_VRB, "%9d opaque brushes\n", numOpaqueBrushes );
3236 }
3237 void SetupBrushes( void ){
3238 SetupBrushesFlags( C_TRANSLUCENT, 0, 0, 0 );
3239 }
3240
3241
3242
3243 /*
3244 ClusterVisible()
3245 determines if two clusters are visible to each other using the PVS
3246 */
3247
3248 qboolean ClusterVisible( int a, int b ){
3249 int leafBytes;
3250 byte *pvs;
3251
3252
3253 /* dummy check */
3254 if ( a < 0 || b < 0 ) {
3255 return qfalse;
3256 }
3257
3258 /* early out */
3259 if ( a == b ) {
3260 return qtrue;
3261 }
3262
3263 /* not vised? */
3264 if ( numBSPVisBytes <= 8 ) {
3265 return qtrue;
3266 }
3267
3268 /* get pvs data */
3269 /* portalClusters = ((int *) bspVisBytes)[ 0 ]; */
3270 leafBytes = ( (int*) bspVisBytes )[ 1 ];
3271 pvs = bspVisBytes + VIS_HEADER_SIZE + ( a * leafBytes );
3272
3273 /* check */
3274 if ( ( pvs[ b >> 3 ] & ( 1 << ( b & 7 ) ) ) ) {
3275 return qtrue;
3276 }
3277 return qfalse;
3278 }
3279
3280
3281
3282 /*
3283 PointInLeafNum_r()
3284 borrowed from vlight.c
3285 */
3286
3287 int PointInLeafNum_r( vec3_t point, int nodenum ){
3288 int leafnum;
3289 vec_t dist;
3290 bspNode_t *node;
3291 bspPlane_t *plane;
3292
3293
3294 while ( nodenum >= 0 )
3295 {
3296 node = &bspNodes[ nodenum ];
3297 plane = &bspPlanes[ node->planeNum ];
3298 dist = DotProduct( point, plane->normal ) - plane->dist;
3299 if ( dist > 0.1 ) {
3300 nodenum = node->children[ 0 ];
3301 }
3302 else if ( dist < -0.1 ) {
3303 nodenum = node->children[ 1 ];
3304 }
3305 else
3306 {
3307 leafnum = PointInLeafNum_r( point, node->children[ 0 ] );
3308 if ( bspLeafs[ leafnum ].cluster != -1 ) {
3309 return leafnum;
3310 }
3311 nodenum = node->children[ 1 ];
3312 }
3313 }
3314
3315 leafnum = -nodenum - 1;
3316 return leafnum;
3317 }
3318
3319
3320
3321 /*
3322 PointInLeafnum()
3323 borrowed from vlight.c
3324 */
3325
3326 int PointInLeafNum( vec3_t point ){
3327 return PointInLeafNum_r( point, 0 );
3328 }
3329
3330
3331
3332 /*
3333 ClusterVisibleToPoint() - ydnar
3334 returns qtrue if point can "see" cluster
3335 */
3336
3337 qboolean ClusterVisibleToPoint( vec3_t point, int cluster ){
3338 int pointCluster;
3339
3340
3341 /* get leafNum for point */
3342 pointCluster = ClusterForPoint( point );
3343 if ( pointCluster < 0 ) {
3344 return qfalse;
3345 }
3346
3347 /* check pvs */
3348 return ClusterVisible( pointCluster, cluster );
3349 }
3350
3351
3352
3353 /*
3354 ClusterForPoint() - ydnar
3355 returns the pvs cluster for point
3356 */
3357
3358 int ClusterForPoint( vec3_t point ){
3359 int leafNum;
3360
3361
3362 /* get leafNum for point */
3363 leafNum = PointInLeafNum( point );
3364 if ( leafNum < 0 ) {
3365 return -1;
3366 }
3367
3368 /* return the cluster */
3369 return bspLeafs[ leafNum ].cluster;
3370 }
3371
3372
3373
3374 /*
3375 ClusterForPointExt() - ydnar
3376 also takes brushes into account for occlusion testing
3377 */
3378
3379 int ClusterForPointExt( vec3_t point, float epsilon ){
3380 int i, j, b, leafNum, cluster;
3381 float dot;
3382 qboolean inside;
3383 int *brushes, numBSPBrushes;
3384 bspLeaf_t *leaf;
3385 bspBrush_t *brush;
3386 bspPlane_t *plane;
3387
3388
3389 /* get leaf for point */
3390 leafNum = PointInLeafNum( point );
3391 if ( leafNum < 0 ) {
3392 return -1;
3393 }
3394 leaf = &bspLeafs[ leafNum ];
3395
3396 /* get the cluster */
3397 cluster = leaf->cluster;
3398 if ( cluster < 0 ) {
3399 return -1;
3400 }
3401
3402 /* transparent leaf, so check point against all brushes in the leaf */
3403 brushes = &bspLeafBrushes[ leaf->firstBSPLeafBrush ];
3404 numBSPBrushes = leaf->numBSPLeafBrushes;
3405 for ( i = 0; i < numBSPBrushes; i++ )
3406 {
3407 /* get parts */
3408 b = brushes[ i ];
3409 if ( b > maxOpaqueBrush ) {
3410 continue;
3411 }
3412 brush = &bspBrushes[ b ];
3413 if ( !( opaqueBrushes[ b >> 3 ] & ( 1 << ( b & 7 ) ) ) ) {
3414 continue;
3415 }
3416
3417 /* check point against all planes */
3418 inside = qtrue;
3419 for ( j = 0; j < brush->numSides && inside; j++ )
3420 {
3421 plane = &bspPlanes[ bspBrushSides[ brush->firstSide + j ].planeNum ];
3422 dot = DotProduct( point, plane->normal );
3423 dot -= plane->dist;
3424 if ( dot > epsilon ) {
3425 inside = qfalse;
3426 }
3427 }
3428
3429 /* if inside, return bogus cluster */
3430 if ( inside ) {
3431 return -1 - b;
3432 }
3433 }
3434
3435 /* if the point made it this far, it's not inside any opaque brushes */
3436 return cluster;
3437 }
3438
3439
3440
3441 /*
3442 ClusterForPointExtFilter() - ydnar
3443 adds cluster checking against a list of known valid clusters
3444 */
3445
3446 int ClusterForPointExtFilter( vec3_t point, float epsilon, int numClusters, int *clusters ){
3447 int i, cluster;
3448
3449
3450 /* get cluster for point */
3451 cluster = ClusterForPointExt( point, epsilon );
3452
3453 /* check if filtering is necessary */
3454 if ( cluster < 0 || numClusters <= 0 || clusters == NULL ) {
3455 return cluster;
3456 }
3457
3458 /* filter */
3459 for ( i = 0; i < numClusters; i++ )
3460 {
3461 if ( cluster == clusters[ i ] || ClusterVisible( cluster, clusters[ i ] ) ) {
3462 return cluster;
3463 }
3464 }
3465
3466 /* failed */
3467 return -1;
3468 }
3469
3470
3471
3472 /*
3473 ShaderForPointInLeaf() - ydnar
3474 checks a point against all brushes in a leaf, returning the shader of the brush
3475 also sets the cumulative surface and content flags for the brush hit
3476 */
3477
3478 int ShaderForPointInLeaf( vec3_t point, int leafNum, float epsilon, int wantContentFlags, int wantSurfaceFlags, int *contentFlags, int *surfaceFlags ){
3479 int i, j;
3480 float dot;
3481 qboolean inside;
3482 int *brushes, numBSPBrushes;
3483 bspLeaf_t *leaf;
3484 bspBrush_t *brush;
3485 bspBrushSide_t *side;
3486 bspPlane_t *plane;
3487 bspShader_t *shader;
3488 int allSurfaceFlags, allContentFlags;
3489
3490
3491 /* clear things out first */
3492 *surfaceFlags = 0;
3493 *contentFlags = 0;
3494
3495 /* get leaf */
3496 if ( leafNum < 0 ) {
3497 return -1;
3498 }
3499 leaf = &bspLeafs[ leafNum ];
3500
3501 /* transparent leaf, so check point against all brushes in the leaf */
3502 brushes = &bspLeafBrushes[ leaf->firstBSPLeafBrush ];
3503 numBSPBrushes = leaf->numBSPLeafBrushes;
3504 for ( i = 0; i < numBSPBrushes; i++ )
3505 {
3506 /* get parts */
3507 brush = &bspBrushes[ brushes[ i ] ];
3508
3509 /* check point against all planes */
3510 inside = qtrue;
3511 allSurfaceFlags = 0;
3512 allContentFlags = 0;
3513 for ( j = 0; j < brush->numSides && inside; j++ )
3514 {
3515 side = &bspBrushSides[ brush->firstSide + j ];
3516 plane = &bspPlanes[ side->planeNum ];
3517 dot = DotProduct( point, plane->normal );
3518 dot -= plane->dist;
3519 if ( dot > epsilon ) {
3520 inside = qfalse;
3521 }
3522 else
3523 {
3524 shader = &bspShaders[ side->shaderNum ];
3525 allSurfaceFlags |= shader->surfaceFlags;
3526 allContentFlags |= shader->contentFlags;
3527 }
3528 }
3529
3530 /* handle if inside */
3531 if ( inside ) {
3532 /* if there are desired flags, check for same and continue if they aren't matched */
3533 if ( wantContentFlags && !( wantContentFlags & allContentFlags ) ) {
3534 continue;
3535 }
3536 if ( wantSurfaceFlags && !( wantSurfaceFlags & allSurfaceFlags ) ) {
3537 continue;
3538 }
3539
3540 /* store the cumulative flags and return the brush shader (which is mostly useless) */
3541 *surfaceFlags = allSurfaceFlags;
3542 *contentFlags = allContentFlags;
3543 return brush->shaderNum;
3544 }
3545 }
3546
3547 /* if the point made it this far, it's not inside any brushes */
3548 return -1;
3549 }
3550
3551
3552
3553 /*
3554 ChopBounds()
3555 chops a bounding box by the plane defined by origin and normal
3556 returns qfalse if the bounds is entirely clipped away
3557
3558 this is not exactly the fastest way to do this...
3559 */
3560
3561 qboolean ChopBounds( vec3_t mins, vec3_t maxs, vec3_t origin, vec3_t normal ){
3562 /* FIXME: rewrite this so it doesn't use bloody brushes */
3563 return qtrue;
3564 }
3565
3566
3567
3568 /*
3569 SetupEnvelopes()
3570 calculates each light's effective envelope,
3571 taking into account brightness, type, and pvs.
3572 */
3573
3574 #define LIGHT_EPSILON 0.125f
3575 #define LIGHT_NUDGE 2.0f
3576
3577 void SetupEnvelopes( qboolean forGrid, qboolean fastFlag ){
3578 int i, x, y, z, x1, y1, z1;
3579 light_t *light, *light2, **owner;
3580 bspLeaf_t *leaf;
3581 vec3_t origin, dir, mins, maxs;
3582 float radius, intensity;
3583 light_t *buckets[ 256 ];
3584
3585
3586 /* early out for weird cases where there are no lights */
3587 if ( lights == NULL ) {
3588 return;
3589 }
3590
3591 /* note it */
3592 Sys_FPrintf( SYS_VRB, "--- SetupEnvelopes%s ---\n", fastFlag ? " (fast)" : "" );
3593
3594 /* count lights */
3595 numLights = 0;
3596 numCulledLights = 0;
3597 owner = &lights;
3598 while ( *owner != NULL )
3599 {
3600 /* get light */
3601 light = *owner;
3602
3603 /* handle negative lights */
3604 if ( light->photons < 0.0f || light->add < 0.0f ) {
3605 light->photons *= -1.0f;
3606 light->add *= -1.0f;
3607 light->flags |= LIGHT_NEGATIVE;
3608 }
3609
3610 /* sunlight? */
3611 if ( light->type == EMIT_SUN ) {
3612 /* special cased */
3613 light->cluster = 0;
3614 light->envelope = MAX_WORLD_COORD * 8.0f;
3615 VectorSet( light->mins, MIN_WORLD_COORD * 8.0f, MIN_WORLD_COORD * 8.0f, MIN_WORLD_COORD * 8.0f );
3616 VectorSet( light->maxs, MAX_WORLD_COORD * 8.0f, MAX_WORLD_COORD * 8.0f, MAX_WORLD_COORD * 8.0f );
3617 }
3618
3619 /* everything else */
3620 else
3621 {
3622 /* get pvs cluster for light */
3623 light->cluster = ClusterForPointExt( light->origin, LIGHT_EPSILON );
3624
3625 /* invalid cluster? */
3626 if ( light->cluster < 0 ) {
3627 /* nudge the sample point around a bit */
3628 for ( x = 0; x < 4; x++ )
3629 {
3630 /* two's complement 0, 1, -1, 2, -2, etc */
3631 x1 = ( ( x >> 1 ) ^ ( x & 1 ? -1 : 0 ) ) + ( x & 1 );
3632
3633 for ( y = 0; y < 4; y++ )
3634 {
3635 y1 = ( ( y >> 1 ) ^ ( y & 1 ? -1 : 0 ) ) + ( y & 1 );
3636
3637 for ( z = 0; z < 4; z++ )
3638 {
3639 z1 = ( ( z >> 1 ) ^ ( z & 1 ? -1 : 0 ) ) + ( z & 1 );
3640
3641 /* nudge origin */
3642 origin[ 0 ] = light->origin[ 0 ] + ( LIGHT_NUDGE * x1 );
3643 origin[ 1 ] = light->origin[ 1 ] + ( LIGHT_NUDGE * y1 );
3644 origin[ 2 ] = light->origin[ 2 ] + ( LIGHT_NUDGE * z1 );
3645
3646 /* try at nudged origin */
3647 light->cluster = ClusterForPointExt( origin, LIGHT_EPSILON );
3648 if ( light->cluster < 0 ) {
3649 continue;
3650 }
3651
3652 /* set origin */
3653 VectorCopy( origin, light->origin );
3654 }
3655 }
3656 }
3657 }
3658
3659 /* only calculate for lights in pvs and outside of opaque brushes */
3660 if ( light->cluster >= 0 ) {
3661 /* set light fast flag */
3662 if ( fastFlag ) {
3663 light->flags |= LIGHT_FAST_TEMP;
3664 }
3665 else{
3666 light->flags &= ~LIGHT_FAST_TEMP;
3667 }
3668 if ( fastpoint && ( light->type != EMIT_AREA ) ) {
3669 light->flags |= LIGHT_FAST_TEMP;
3670 }
3671 if ( light->si ) {
3672 if ( light->si->noFast ) {
3673 light->flags &= ~( LIGHT_FAST | LIGHT_FAST_TEMP );
3674 }
3675 if ( light->si->fast ) {
3676 light->flags |= LIGHT_FAST;
3677 }
3678 }
3679
3680 /* clear light envelope */
3681 light->envelope = 0;
3682
3683 /* handle area lights */
3684 if ( exactPointToPolygon && light->type == EMIT_AREA && light->w != NULL ) {
3685 light->envelope = MAX_WORLD_COORD * 8.0f;
3686
3687 /* check for fast mode */
3688 if ( ( light->flags & LIGHT_FAST ) || ( light->flags & LIGHT_FAST_TEMP ) ) {
3689 /* ugly hack to calculate extent for area lights, but only done once */
3690 VectorScale( light->normal, -1.0f, dir );
3691 for ( radius = 100.0f; radius < MAX_WORLD_COORD * 8.0f; radius += 10.0f )
3692 {
3693 float factor;
3694
3695 VectorMA( light->origin, radius, light->normal, origin );
3696 factor = PointToPolygonFormFactor( origin, dir, light->w );
3697 if ( factor < 0.0f ) {
3698 factor *= -1.0f;
3699 }
3700 if ( ( factor * light->add ) <= light->falloffTolerance ) {
3701 light->envelope = radius;
3702 break;
3703 }
3704 }
3705 }
3706
3707 intensity = light->photons; /* hopefully not used */
3708 }
3709 else
3710 {
3711 radius = 0.0f;
3712 intensity = light->photons;
3713 }
3714
3715 /* other calcs */
3716 if ( light->envelope <= 0.0f ) {
3717 /* solve distance for non-distance lights */
3718 if ( !( light->flags & LIGHT_ATTEN_DISTANCE ) ) {
3719 light->envelope = MAX_WORLD_COORD * 8.0f;
3720 }
3721
3722 else if ( ( light->flags & LIGHT_FAST ) || ( light->flags & LIGHT_FAST_TEMP ) ) {
3723 /* solve distance for linear lights */
3724 if ( ( light->flags & LIGHT_ATTEN_LINEAR ) ) {
3725 light->envelope = ( ( intensity * linearScale ) - light->falloffTolerance ) / light->fade;
3726 }
3727
3728 /*
3729 add = angle * light->photons * linearScale - (dist * light->fade);
3730 T = (light->photons * linearScale) - (dist * light->fade);
3731 T + (dist * light->fade) = (light->photons * linearScale);
3732 dist * light->fade = (light->photons * linearScale) - T;
3733 dist = ((light->photons * linearScale) - T) / light->fade;
3734 */
3735
3736 /* solve for inverse square falloff */
3737 else{
3738 light->envelope = sqrt( intensity / light->falloffTolerance ) + radius;
3739 }
3740
3741 /*
3742 add = light->photons / (dist * dist);
3743 T = light->photons / (dist * dist);
3744 T * (dist * dist) = light->photons;
3745 dist = sqrt( light->photons / T );
3746 */
3747 }
3748 else
3749 {
3750 /* solve distance for linear lights */
3751 if ( ( light->flags & LIGHT_ATTEN_LINEAR ) ) {
3752 light->envelope = ( intensity * linearScale ) / light->fade;
3753 }
3754
3755 /* can't cull these */
3756 else{
3757 light->envelope = MAX_WORLD_COORD * 8.0f;
3758 }
3759 }
3760 }
3761
3762 /* chop radius against pvs */
3763 {
3764 /* clear bounds */
3765 ClearBounds( mins, maxs );
3766
3767 /* check all leaves */
3768 for ( i = 0; i < numBSPLeafs; i++ )
3769 {
3770 /* get test leaf */
3771 leaf = &bspLeafs[ i ];
3772
3773 /* in pvs? */
3774 if ( leaf->cluster < 0 ) {
3775 continue;
3776 }
3777 if ( ClusterVisible( light->cluster, leaf->cluster ) == qfalse ) { /* ydnar: thanks Arnout for exposing my stupid error (this never failed before) */
3778 continue;
3779 }
3780
3781 /* add this leafs bbox to the bounds */
3782 VectorCopy( leaf->mins, origin );
3783 AddPointToBounds( origin, mins, maxs );
3784 VectorCopy( leaf->maxs, origin );
3785 AddPointToBounds( origin, mins, maxs );
3786 }
3787
3788 /* test to see if bounds encompass light */
3789 for ( i = 0; i < 3; i++ )
3790 {
3791 if ( mins[ i ] > light->origin[ i ] || maxs[ i ] < light->origin[ i ] ) {
3792 //% Sys_FPrintf( SYS_WRN, "WARNING: Light PVS bounds (%.0f, %.0f, %.0f) -> (%.0f, %.0f, %.0f)\ndo not encompass light %d (%f, %f, %f)\n",
3793 //% mins[ 0 ], mins[ 1 ], mins[ 2 ],
3794 //% maxs[ 0 ], maxs[ 1 ], maxs[ 2 ],
3795 //% numLights, light->origin[ 0 ], light->origin[ 1 ], light->origin[ 2 ] );
3796 AddPointToBounds( light->origin, mins, maxs );
3797 }
3798 }
3799
3800 /* chop the bounds by a plane for area lights and spotlights */
3801 if ( light->type == EMIT_AREA || light->type == EMIT_SPOT ) {
3802 ChopBounds( mins, maxs, light->origin, light->normal );
3803 }
3804
3805 /* copy bounds */
3806 VectorCopy( mins, light->mins );
3807 VectorCopy( maxs, light->maxs );
3808
3809 /* reflect bounds around light origin */
3810 //% VectorMA( light->origin, -1.0f, origin, origin );
3811 VectorScale( light->origin, 2, origin );
3812 VectorSubtract( origin, maxs, origin );
3813 AddPointToBounds( origin, mins, maxs );
3814 //% VectorMA( light->origin, -1.0f, mins, origin );
3815 VectorScale( light->origin, 2, origin );
3816 VectorSubtract( origin, mins, origin );
3817 AddPointToBounds( origin, mins, maxs );
3818
3819 /* calculate spherical bounds */
3820 VectorSubtract( maxs, light->origin, dir );
3821 radius = (float) VectorLength( dir );
3822
3823 /* if this radius is smaller than the envelope, then set the envelope to it */
3824 if ( radius < light->envelope ) {
3825 light->envelope = radius;
3826 //% Sys_FPrintf( SYS_VRB, "PVS Cull (%d): culled\n", numLights );
3827 }
3828 //% else
3829 //% Sys_FPrintf( SYS_VRB, "PVS Cull (%d): failed (%8.0f > %8.0f)\n", numLights, radius, light->envelope );
3830 }
3831
3832 /* add grid/surface only check */
3833 if ( forGrid ) {
3834 if ( !( light->flags & LIGHT_GRID ) ) {
3835 light->envelope = 0.0f;
3836 }
3837 }
3838 else
3839 {
3840 if ( !( light->flags & LIGHT_SURFACES ) ) {
3841 light->envelope = 0.0f;
3842 }
3843 }
3844 }
3845
3846 /* culled? */
3847 if ( light->cluster < 0 || light->envelope <= 0.0f ) {
3848 /* debug code */
3849 //% Sys_Printf( "Culling light: Cluster: %d Envelope: %f\n", light->cluster, light->envelope );
3850
3851 /* delete the light */
3852 numCulledLights++;
3853 *owner = light->next;
3854 if ( light->w != NULL ) {
3855 free( light->w );
3856 }
3857 free( light );
3858 continue;
3859 }
3860 }
3861
3862 /* square envelope */
3863 light->envelope2 = ( light->envelope * light->envelope );
3864
3865 /* increment light count */
3866 numLights++;
3867
3868 /* set next light */
3869 owner = &( ( **owner ).next );
3870 }
3871
3872 /* bucket sort lights by style */
3873 memset( buckets, 0, sizeof( buckets ) );
3874 light2 = NULL;
3875 for ( light = lights; light != NULL; light = light2 )
3876 {
3877 /* get next light */
3878 light2 = light->next;
3879
3880 /* filter into correct bucket */
3881 light->next = buckets[ light->style ];
3882 buckets[ light->style ] = light;
3883
3884 /* if any styled light is present, automatically set nocollapse */
3885 if ( light->style != LS_NORMAL ) {
3886 noCollapse = qtrue;
3887 }
3888 }
3889
3890 /* filter back into light list */
3891 lights = NULL;
3892 for ( i = 255; i >= 0; i-- )
3893 {
3894 light2 = NULL;
3895 for ( light = buckets[ i ]; light != NULL; light = light2 )
3896 {
3897 light2 = light->next;
3898 light->next = lights;
3899 lights = light;
3900 }
3901 }
3902
3903 /* emit some statistics */
3904 Sys_Printf( "%9d total lights\n", numLights );
3905 Sys_Printf( "%9d culled lights\n", numCulledLights );
3906 }
3907
3908
3909
3910 /*
3911 CreateTraceLightsForBounds()
3912 creates a list of lights that affect the given bounding box and pvs clusters (bsp leaves)
3913 */
3914
3915 void CreateTraceLightsForBounds( vec3_t mins, vec3_t maxs, vec3_t normal, int numClusters, int *clusters, int flags, trace_t *trace ){
3916 int i;
3917 light_t *light;
3918 vec3_t origin, dir, nullVector = { 0.0f, 0.0f, 0.0f };
3919 float radius, dist, length;
3920
3921
3922 /* potential pre-setup */
3923 if ( numLights == 0 ) {
3924 SetupEnvelopes( qfalse, fast );
3925 }
3926
3927 /* debug code */
3928 //% Sys_Printf( "CTWLFB: (%4.1f %4.1f %4.1f) (%4.1f %4.1f %4.1f)\n", mins[ 0 ], mins[ 1 ], mins[ 2 ], maxs[ 0 ], maxs[ 1 ], maxs[ 2 ] );
3929
3930 /* allocate the light list */
3931 trace->lights = safe_malloc( sizeof( light_t* ) * ( numLights + 1 ) );
3932 trace->numLights = 0;
3933
3934 /* calculate spherical bounds */
3935 VectorAdd( mins, maxs, origin );
3936 VectorScale( origin, 0.5f, origin );
3937 VectorSubtract( maxs, origin, dir );
3938 radius = (float) VectorLength( dir );
3939
3940 /* get length of normal vector */
3941 if ( normal != NULL ) {
3942 length = VectorLength( normal );
3943 }
3944 else
3945 {
3946 normal = nullVector;
3947 length = 0;
3948 }
3949
3950 /* test each light and see if it reaches the sphere */
3951 /* note: the attenuation code MUST match LightingAtSample() */
3952 for ( light = lights; light; light = light->next )
3953 {
3954 /* check zero sized envelope */
3955 if ( light->envelope <= 0 ) {
3956 lightsEnvelopeCulled++;
3957 continue;
3958 }
3959
3960 /* check flags */
3961 if ( !( light->flags & flags ) ) {
3962 continue;
3963 }
3964
3965 /* sunlight skips all this nonsense */
3966 if ( light->type != EMIT_SUN ) {
3967 /* sun only? */
3968 if ( sunOnly ) {
3969 continue;
3970 }
3971
3972 /* check against pvs cluster */
3973 if ( numClusters > 0 && clusters != NULL ) {
3974 for ( i = 0; i < numClusters; i++ )
3975 {
3976 if ( ClusterVisible( light->cluster, clusters[ i ] ) ) {
3977 break;
3978 }
3979 }
3980
3981 /* fixme! */
3982 if ( i == numClusters ) {
3983 lightsClusterCulled++;
3984 continue;
3985 }
3986 }
3987
3988 /* if the light's bounding sphere intersects with the bounding sphere then this light needs to be tested */
3989 VectorSubtract( light->origin, origin, dir );
3990 dist = VectorLength( dir );
3991 dist -= light->envelope;
3992 dist -= radius;
3993 if ( dist > 0 ) {
3994 lightsEnvelopeCulled++;
3995 continue;
3996 }
3997
3998 /* check bounding box against light's pvs envelope (note: this code never eliminated any lights, so disabling it) */
3999 #if 0
4000 skip = qfalse;
4001 for ( i = 0; i < 3; i++ )
4002 {
4003 if ( mins[ i ] > light->maxs[ i ] || maxs[ i ] < light->mins[ i ] ) {
4004 skip = qtrue;
4005 }
4006 }
4007 if ( skip ) {
4008 lightsBoundsCulled++;
4009 continue;
4010 }
4011 #endif
4012 }
4013
4014 /* planar surfaces (except twosided surfaces) have a couple more checks */
4015 if ( length > 0.0f && trace->twoSided == qfalse ) {
4016 /* lights coplanar with a surface won't light it */
4017 if ( !( light->flags & LIGHT_TWOSIDED ) && DotProduct( light->normal, normal ) > 0.999f ) {
4018 lightsPlaneCulled++;
4019 continue;
4020 }
4021
4022 /* check to see if light is behind the plane */
4023 if ( DotProduct( light->origin, normal ) - DotProduct( origin, normal ) < -1.0f ) {
4024 lightsPlaneCulled++;
4025 continue;
4026 }
4027 }
4028
4029 /* add this light */
4030 trace->lights[ trace->numLights++ ] = light;
4031 }
4032
4033 /* make last night null */
4034 trace->lights[ trace->numLights ] = NULL;
4035 }
4036
4037
4038
4039 void FreeTraceLights( trace_t *trace ){
4040 if ( trace->lights != NULL ) {
4041 free( trace->lights );
4042 }
4043 }
4044
4045
4046
4047 /*
4048 CreateTraceLightsForSurface()
4049 creates a list of lights that can potentially affect a drawsurface
4050 */
4051
4052 void CreateTraceLightsForSurface( int num, trace_t *trace ){
4053 int i;
4054 vec3_t mins, maxs, normal;
4055 bspDrawVert_t *dv;
4056 bspDrawSurface_t *ds;
4057 surfaceInfo_t *info;
4058
4059
4060 /* dummy check */
4061 if ( num < 0 ) {
4062 return;
4063 }
4064
4065 /* get drawsurface and info */
4066 ds = &bspDrawSurfaces[ num ];
4067 info = &surfaceInfos[ num ];
4068
4069 /* get the mins/maxs for the dsurf */
4070 ClearBounds( mins, maxs );
4071 VectorCopy( bspDrawVerts[ ds->firstVert ].normal, normal );
4072 for ( i = 0; i < ds->numVerts; i++ )
4073 {
4074 dv = &yDrawVerts[ ds->firstVert + i ];
4075 AddPointToBounds( dv->xyz, mins, maxs );
4076 if ( !VectorCompare( dv->normal, normal ) ) {
4077 VectorClear( normal );
4078 }
4079 }
4080
4081 /* create the lights for the bounding box */
4082 CreateTraceLightsForBounds( mins, maxs, normal, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ], LIGHT_SURFACES, trace );
4083 }
4084
4085 /////////////////////////////////////////////////////////////
4086
4087 #define FLOODLIGHT_CONE_ANGLE 88 /* degrees */
4088 #define FLOODLIGHT_NUM_ANGLE_STEPS 16
4089 #define FLOODLIGHT_NUM_ELEVATION_STEPS 4
4090 #define FLOODLIGHT_NUM_VECTORS ( FLOODLIGHT_NUM_ANGLE_STEPS * FLOODLIGHT_NUM_ELEVATION_STEPS )
4091
4092 static vec3_t floodVectors[ FLOODLIGHT_NUM_VECTORS ];
4093 static int numFloodVectors = 0;
4094
4095 void SetupFloodLight( void ){
4096 int i, j;
4097 float angle, elevation, angleStep, elevationStep;
4098 const char *value;
4099 double v1,v2,v3,v4,v5,v6;
4100
4101 /* note it */
4102 Sys_FPrintf( SYS_VRB, "--- SetupFloodLight ---\n" );
4103
4104 /* calculate angular steps */
4105 angleStep = DEG2RAD( 360.0f / FLOODLIGHT_NUM_ANGLE_STEPS );
4106 elevationStep = DEG2RAD( FLOODLIGHT_CONE_ANGLE / FLOODLIGHT_NUM_ELEVATION_STEPS );
4107
4108 /* iterate angle */
4109 angle = 0.0f;
4110 for ( i = 0, angle = 0.0f; i < FLOODLIGHT_NUM_ANGLE_STEPS; i++, angle += angleStep )
4111 {
4112 /* iterate elevation */
4113 for ( j = 0, elevation = elevationStep * 0.5f; j < FLOODLIGHT_NUM_ELEVATION_STEPS; j++, elevation += elevationStep )
4114 {
4115 floodVectors[ numFloodVectors ][ 0 ] = sin( elevation ) * cos( angle );
4116 floodVectors[ numFloodVectors ][ 1 ] = sin( elevation ) * sin( angle );
4117 floodVectors[ numFloodVectors ][ 2 ] = cos( elevation );
4118 numFloodVectors++;
4119 }
4120 }
4121
4122 /* emit some statistics */
4123 Sys_FPrintf( SYS_VRB, "%9d numFloodVectors\n", numFloodVectors );
4124
4125 /* floodlight */
4126 value = ValueForKey( &entities[ 0 ], "_floodlight" );
4127
4128 if ( value[ 0 ] != '\0' ) {
4129 v1 = v2 = v3 = 0;
4130 v4 = floodlightDistance;
4131 v5 = floodlightIntensity;
4132 v6 = floodlightDirectionScale;
4133
4134 sscanf( value, "%lf %lf %lf %lf %lf %lf", &v1, &v2, &v3, &v4, &v5, &v6 );
4135
4136 floodlightRGB[0] = v1;
4137 floodlightRGB[1] = v2;
4138 floodlightRGB[2] = v3;
4139
4140 if ( VectorLength( floodlightRGB ) == 0 ) {
4141 VectorSet( floodlightRGB,0.94,0.94,1.0 );
4142 }
4143
4144 if ( v4 < 1 ) {
4145 v4 = 1024;
4146 }
4147 if ( v5 < 1 ) {
4148 v5 = 128;
4149 }
4150 if ( v6 < 0 ) {
4151 v6 = 1;
4152 }
4153
4154 floodlightDistance = v4;
4155 floodlightIntensity = v5;
4156 floodlightDirectionScale = v6;
4157
4158 floodlighty = qtrue;
4159 Sys_Printf( "FloodLighting enabled via worldspawn _floodlight key.\n" );
4160 }
4161 else
4162 {
4163 VectorSet( floodlightRGB,0.94,0.94,1.0 );
4164 }
4165 if ( colorsRGB ) {
4166 floodlightRGB[0] = Image_LinearFloatFromsRGBFloat( floodlightRGB[0] );
4167 floodlightRGB[1] = Image_LinearFloatFromsRGBFloat( floodlightRGB[1] );
4168 floodlightRGB[2] = Image_LinearFloatFromsRGBFloat( floodlightRGB[2] );
4169 }
4170 ColorNormalize( floodlightRGB,floodlightRGB );
4171 }
4172
4173 /*
4174 FloodLightForSample()
4175 calculates floodlight value for a given sample
4176 once again, kudos to the dirtmapping coder
4177 */
4178
4179 float FloodLightForSample( trace_t *trace, float floodLightDistance, qboolean floodLightLowQuality ){
4180 int i;
4181 float d;
4182 float contribution;
4183 int sub = 0;
4184 float gatherLight, outLight;
4185 vec3_t normal, worldUp, myUp, myRt, direction, displacement;
4186 float dd;
4187 int vecs = 0;
4188
4189 gatherLight = 0;
4190 /* dummy check */
4191 //if( !dirty )
4192 // return 1.0f;
4193 if ( trace == NULL || trace->cluster < 0 ) {
4194 return 0.0f;
4195 }
4196
4197
4198 /* setup */
4199 dd = floodLightDistance;
4200 VectorCopy( trace->normal, normal );
4201
4202 /* check if the normal is aligned to the world-up */
4203 if ( normal[ 0 ] == 0.0f && normal[ 1 ] == 0.0f && ( normal[ 2 ] == 1.0f || normal[ 2 ] == -1.0f ) ) {
4204 if ( normal[ 2 ] == 1.0f ) {
4205 VectorSet( myRt, 1.0f, 0.0f, 0.0f );
4206 VectorSet( myUp, 0.0f, 1.0f, 0.0f );
4207 }
4208 else if ( normal[ 2 ] == -1.0f ) {
4209 VectorSet( myRt, -1.0f, 0.0f, 0.0f );
4210 VectorSet( myUp, 0.0f, 1.0f, 0.0f );
4211 }
4212 }
4213 else
4214 {
4215 VectorSet( worldUp, 0.0f, 0.0f, 1.0f );
4216 CrossProduct( normal, worldUp, myRt );
4217 VectorNormalize( myRt, myRt );
4218 CrossProduct( myRt, normal, myUp );
4219 VectorNormalize( myUp, myUp );
4220 }
4221
4222 /* vortex: optimise floodLightLowQuality a bit */
4223 if ( floodLightLowQuality == qtrue ) {
4224 /* iterate through ordered vectors */
4225 for ( i = 0; i < numFloodVectors; i++ )
4226 if ( rand() % 10 != 0 ) {
4227 continue;
4228 }
4229 }
4230 else
4231 {
4232 /* iterate through ordered vectors */
4233 for ( i = 0; i < numFloodVectors; i++ )
4234 {
4235 vecs++;
4236
4237 /* transform vector into tangent space */
4238 direction[ 0 ] = myRt[ 0 ] * floodVectors[ i ][ 0 ] + myUp[ 0 ] * floodVectors[ i ][ 1 ] + normal[ 0 ] * floodVectors[ i ][ 2 ];
4239 direction[ 1 ] = myRt[ 1 ] * floodVectors[ i ][ 0 ] + myUp[ 1 ] * floodVectors[ i ][ 1 ] + normal[ 1 ] * floodVectors[ i ][ 2 ];
4240 direction[ 2 ] = myRt[ 2 ] * floodVectors[ i ][ 0 ] + myUp[ 2 ] * floodVectors[ i ][ 1 ] + normal[ 2 ] * floodVectors[ i ][ 2 ];
4241
4242 /* set endpoint */
4243 VectorMA( trace->origin, dd, direction, trace->end );
4244
4245 //VectorMA( trace->origin, 1, direction, trace->origin );
4246
4247 SetupTrace( trace );
4248 VectorSet(trace->color, 1.0f, 1.0f, 1.0f);
4249 /* trace */
4250 TraceLine( trace );
4251 contribution = 1;
4252
4253 if ( trace->compileFlags & C_SKY || trace->compileFlags & C_TRANSLUCENT ) {
4254 contribution = 1.0f;
4255 }
4256 else if ( trace->opaque ) {
4257 VectorSubtract( trace->hit, trace->origin, displacement );
4258 d = VectorLength( displacement );
4259
4260 // d=trace->distance;
4261 //if (d>256) gatherDirt+=1;
4262 contribution = d / dd;
4263 if ( contribution > 1 ) {
4264 contribution = 1.0f;
4265 }
4266
4267 //gatherDirt += 1.0f - ooDepth * VectorLength( displacement );
4268 }
4269
4270 gatherLight += contribution;
4271 }
4272 }
4273
4274 /* early out */
4275 if ( gatherLight <= 0.0f ) {
4276 return 0.0f;
4277 }
4278
4279 sub = vecs;
4280
4281 if ( sub < 1 ) {
4282 sub = 1;
4283 }
4284 gatherLight /= ( sub );
4285
4286 outLight = gatherLight;
4287 if ( outLight > 1.0f ) {
4288 outLight = 1.0f;
4289 }
4290
4291 /* return to sender */
4292 return outLight;
4293 }
4294
4295 /*
4296 FloodLightRawLightmap
4297 lighttracer style ambient occlusion light hack.
4298 Kudos to the dirtmapping author for most of this source.
4299 VorteX: modified to floodlight up custom surfaces (q3map_floodLight)
4300 VorteX: fixed problems with deluxemapping
4301 */
4302
4303 // floodlight pass on a lightmap
4304 void FloodLightRawLightmapPass( rawLightmap_t *lm, vec3_t lmFloodLightRGB, float lmFloodLightIntensity, float lmFloodLightDistance, qboolean lmFloodLightLowQuality, float floodlightDirectionScale ){
4305 int i, x, y, *cluster;
4306 float *origin, *normal, *floodlight, floodLightAmount;
4307 surfaceInfo_t *info;
4308 trace_t trace;
4309 // int sx, sy;
4310 // float samples, average, *floodlight2;
4311
4312 memset( &trace,0,sizeof( trace_t ) );
4313
4314 /* setup trace */
4315 trace.testOcclusion = qtrue;
4316 trace.forceSunlight = qfalse;
4317 trace.twoSided = qtrue;
4318 trace.recvShadows = lm->recvShadows;
4319 trace.numSurfaces = lm->numLightSurfaces;
4320 trace.surfaces = &lightSurfaces[ lm->firstLightSurface ];
4321 trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
4322 trace.testAll = qfalse;
4323 trace.distance = 1024;
4324
4325 /* twosided lighting (may or may not be a good idea for lightmapped stuff) */
4326 //trace.twoSided = qfalse;
4327 for ( i = 0; i < trace.numSurfaces; i++ )
4328 {
4329 /* get surface */
4330 info = &surfaceInfos[ trace.surfaces[ i ] ];
4331
4332 /* check twosidedness */
4333 if ( info->si->twoSided ) {
4334 trace.twoSided = qtrue;
4335 break;
4336 }
4337 }
4338
4339 /* gather floodlight */
4340 for ( y = 0; y < lm->sh; y++ )
4341 {
4342 for ( x = 0; x < lm->sw; x++ )
4343 {
4344 /* get luxel */
4345 cluster = SUPER_CLUSTER( x, y );
4346 origin = SUPER_ORIGIN( x, y );
4347 normal = SUPER_NORMAL( x, y );
4348 floodlight = SUPER_FLOODLIGHT( x, y );
4349
4350 /* set default dirt */
4351 *floodlight = 0.0f;
4352
4353 /* only look at mapped luxels */
4354 if ( *cluster < 0 ) {
4355 continue;
4356 }
4357
4358 /* copy to trace */
4359 trace.cluster = *cluster;
4360 VectorCopy( origin, trace.origin );
4361 VectorCopy( normal, trace.normal );
4362
4363 /* get floodlight */
4364 floodLightAmount = FloodLightForSample( &trace, lmFloodLightDistance, lmFloodLightLowQuality ) * lmFloodLightIntensity;
4365
4366 /* add floodlight */
4367 floodlight[0] += lmFloodLightRGB[0] * floodLightAmount;
4368 floodlight[1] += lmFloodLightRGB[1] * floodLightAmount;
4369 floodlight[2] += lmFloodLightRGB[2] * floodLightAmount;
4370 floodlight[3] += floodlightDirectionScale;
4371 }
4372 }
4373
4374 /* testing no filtering */
4375 return;
4376
4377 #if 0
4378
4379 /* filter "dirt" */
4380 for ( y = 0; y < lm->sh; y++ )
4381 {
4382 for ( x = 0; x < lm->sw; x++ )
4383 {
4384 /* get luxel */
4385 cluster = SUPER_CLUSTER( x, y );
4386 floodlight = SUPER_FLOODLIGHT( x, y );
4387
4388 /* filter dirt by adjacency to unmapped luxels */
4389 average = *floodlight;
4390 samples = 1.0f;
4391 for ( sy = ( y - 1 ); sy <= ( y + 1 ); sy++ )
4392 {
4393 if ( sy < 0 || sy >= lm->sh ) {
4394 continue;
4395 }
4396
4397 for ( sx = ( x - 1 ); sx <= ( x + 1 ); sx++ )
4398 {
4399 if ( sx < 0 || sx >= lm->sw || ( sx == x && sy == y ) ) {
4400 continue;
4401 }
4402
4403 /* get neighboring luxel */
4404 cluster = SUPER_CLUSTER( sx, sy );
4405 floodlight2 = SUPER_FLOODLIGHT( sx, sy );
4406 if ( *cluster < 0 || *floodlight2 <= 0.0f ) {
4407 continue;
4408 }
4409
4410 /* add it */
4411 average += *floodlight2;
4412 samples += 1.0f;
4413 }
4414
4415 /* bail */
4416 if ( samples <= 0.0f ) {
4417 break;
4418 }
4419 }
4420
4421 /* bail */
4422 if ( samples <= 0.0f ) {
4423 continue;
4424 }
4425
4426 /* scale dirt */
4427 *floodlight = average / samples;
4428 }
4429 }
4430 #endif
4431 }
4432
4433 void FloodLightRawLightmap( int rawLightmapNum ){
4434 rawLightmap_t *lm;
4435
4436 /* bail if this number exceeds the number of raw lightmaps */
4437 if ( rawLightmapNum >= numRawLightmaps ) {
4438 return;
4439 }
4440 /* get lightmap */
4441 lm = &rawLightmaps[ rawLightmapNum ];
4442
4443 /* global pass */
4444 if ( floodlighty && floodlightIntensity ) {
4445 FloodLightRawLightmapPass( lm, floodlightRGB, floodlightIntensity, floodlightDistance, floodlight_lowquality, floodlightDirectionScale );
4446 }
4447
4448 /* custom pass */
4449 if ( lm->floodlightIntensity ) {
4450 FloodLightRawLightmapPass( lm, lm->floodlightRGB, lm->floodlightIntensity, lm->floodlightDistance, qfalse, lm->floodlightDirectionScale );
4451 numSurfacesFloodlighten += 1;
4452 }
4453 }
4454
4455 void FloodlightRawLightmaps(){
4456 Sys_Printf( "--- FloodlightRawLightmap ---\n" );
4457 numSurfacesFloodlighten = 0;
4458 RunThreadsOnIndividual( numRawLightmaps, qtrue, FloodLightRawLightmap );
4459 Sys_Printf( "%9d custom lightmaps floodlighted\n", numSurfacesFloodlighten );
4460 }
4461
4462 /*
4463 FloodLightIlluminate()
4464 illuminate floodlight into lightmap luxels
4465 */
4466
4467 void FloodlightIlluminateLightmap( rawLightmap_t *lm ){
4468 float *luxel, *floodlight, *deluxel, *normal;
4469 int *cluster;
4470 float brightness;
4471 int x, y, lightmapNum;
4472
4473 /* walk lightmaps */
4474 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
4475 {
4476 /* early out */
4477 if ( lm->superLuxels[ lightmapNum ] == NULL ) {
4478 continue;
4479 }
4480
4481 if( lm->styles[lightmapNum] != LS_NORMAL && lm->styles[lightmapNum] != LS_NONE ) // isStyleLight
4482 continue;
4483
4484 /* apply floodlight to each luxel */
4485 for ( y = 0; y < lm->sh; y++ )
4486 {
4487 for ( x = 0; x < lm->sw; x++ )
4488 {
4489 /* get floodlight */
4490 floodlight = SUPER_FLOODLIGHT( x, y );
4491 if ( !floodlight[0] && !floodlight[1] && !floodlight[2] ) {
4492 continue;
4493 }
4494
4495 /* get cluster */
4496 cluster = SUPER_CLUSTER( x, y );
4497
4498 /* only process mapped luxels */
4499 if ( *cluster < 0 ) {
4500 continue;
4501 }
4502
4503 /* get particulars */
4504 luxel = SUPER_LUXEL( lightmapNum, x, y );
4505 deluxel = SUPER_DELUXEL( x, y );
4506
4507 /* add to lightmap */
4508 luxel[0] += floodlight[0];
4509 luxel[1] += floodlight[1];
4510 luxel[2] += floodlight[2];
4511
4512 if ( luxel[3] == 0 ) {
4513 luxel[3] = 1;
4514 }
4515
4516 /* add to deluxemap */
4517 if ( deluxemap && floodlight[3] > 0 ) {
4518 vec3_t lightvector;
4519
4520 normal = SUPER_NORMAL( x, y );
4521 brightness = RGBTOGRAY( floodlight ) * ( 1.0f / 255.0f ) * floodlight[3];
4522
4523 // use AT LEAST this amount of contribution from ambient for the deluxemap, fixes points that receive ZERO light
4524 if ( brightness < 0.00390625f ) {
4525 brightness = 0.00390625f;
4526 }
4527
4528 VectorScale( normal, brightness, lightvector );
4529 VectorAdd( deluxel, lightvector, deluxel );
4530 }
4531 }
4532 }
4533 }
4534 }