1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
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.
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.
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
22 ----------------------------------------------------------------------------------
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."
27 ------------------------------------------------------------------------------- */
44 ydnar: moved to here 2001-02-04
47 void ColorToBytes( const float *color, byte *colorBytes, float scale )
55 /* ydnar: scaling necessary for simulating r_overbrightBits on external lightmaps */
59 /* make a local copy */
60 VectorScale( color, scale, sample );
63 gamma = 1.0f / lightmapGamma;
64 for( i = 0; i < 3; i++ )
66 /* handle negative light */
67 if( sample[ i ] < 0.0f )
74 sample[ i ] = pow( sample[ i ] / 255.0f, gamma ) * 255.0f;
77 if (lightmapExposure == 1)
79 /* clamp with color normalization */
81 if( sample[ 1 ] > max )
83 if( sample[ 2 ] > max )
86 VectorScale( sample, (255.0f / max), sample );
90 if (lightmapExposure==0)
92 lightmapExposure=1.0f;
94 inv=1.f/lightmapExposure;
98 if( sample[ 1 ] > max )
100 if( sample[ 2 ] > max )
103 dif = (1- exp(-max * inv) ) * 255;
121 /* compensate for ingame overbrighting/bitshifting */
122 VectorScale( sample, (1.0f / lightmapCompensate), sample );
125 colorBytes[ 0 ] = sample[ 0 ];
126 colorBytes[ 1 ] = sample[ 1 ];
127 colorBytes[ 2 ] = sample[ 2 ];
132 /* -------------------------------------------------------------------------------
134 this section deals with phong shading (normal interpolation across brush faces)
136 ------------------------------------------------------------------------------- */
140 smooths together coincident vertex normals across the bsp
143 #define MAX_SAMPLES 256
144 #define THETA_EPSILON 0.000001
145 #define EQUAL_NORMAL_EPSILON 0.01
147 void SmoothNormals( void )
149 int i, j, k, f, cs, numVerts, numVotes, fOld, start;
150 float shadeAngle, defaultShadeAngle, maxShadeAngle, dot, testAngle;
151 bspDrawSurface_t *ds;
155 vec3_t average, diff;
156 int indexes[ MAX_SAMPLES ];
157 vec3_t votes[ MAX_SAMPLES ];
160 /* allocate shade angle table */
161 shadeAngles = safe_malloc( numBSPDrawVerts * sizeof( float ) );
162 memset( shadeAngles, 0, numBSPDrawVerts * sizeof( float ) );
164 /* allocate smoothed table */
165 cs = (numBSPDrawVerts / 8) + 1;
166 smoothed = safe_malloc( cs );
167 memset( smoothed, 0, cs );
169 /* set default shade angle */
170 defaultShadeAngle = DEG2RAD( shadeAngleDegrees );
173 /* run through every surface and flag verts belonging to non-lightmapped surfaces
174 and set per-vertex smoothing angle */
175 for( i = 0; i < numBSPDrawSurfaces; i++ )
178 ds = &bspDrawSurfaces[ i ];
180 /* get shader for shade angle */
181 si = surfaceInfos[ i ].si;
182 if( si->shadeAngleDegrees )
183 shadeAngle = DEG2RAD( si->shadeAngleDegrees );
185 shadeAngle = defaultShadeAngle;
186 if( shadeAngle > maxShadeAngle )
187 maxShadeAngle = shadeAngle;
190 for( j = 0; j < ds->numVerts; j++ )
192 f = ds->firstVert + j;
193 shadeAngles[ f ] = shadeAngle;
194 if( ds->surfaceType == MST_TRIANGLE_SOUP )
195 smoothed[ f >> 3 ] |= (1 << (f & 7));
198 /* ydnar: optional force-to-trisoup */
199 if( trisoup && ds->surfaceType == MST_PLANAR )
201 ds->surfaceType = MST_TRIANGLE_SOUP;
202 ds->lightmapNum[ 0 ] = -3;
206 /* bail if no surfaces have a shade angle */
207 if( maxShadeAngle == 0 )
216 start = I_FloatTime();
218 /* go through the list of vertexes */
219 for( i = 0; i < numBSPDrawVerts; i++ )
222 f = 10 * i / numBSPDrawVerts;
226 Sys_Printf( "%i...", f );
229 /* already smoothed? */
230 if( smoothed[ i >> 3 ] & (1 << (i & 7)) )
234 VectorClear( average );
238 /* build a table of coincident vertexes */
239 for( j = i; j < numBSPDrawVerts && numVerts < MAX_SAMPLES; j++ )
241 /* already smoothed? */
242 if( smoothed[ j >> 3 ] & (1 << (j & 7)) )
246 if( VectorCompare( yDrawVerts[ i ].xyz, yDrawVerts[ j ].xyz ) == qfalse )
249 /* use smallest shade angle */
250 shadeAngle = (shadeAngles[ i ] < shadeAngles[ j ] ? shadeAngles[ i ] : shadeAngles[ j ]);
252 /* check shade angle */
253 dot = DotProduct( bspDrawVerts[ i ].normal, bspDrawVerts[ j ].normal );
256 else if( dot < -1.0 )
258 testAngle = acos( dot ) + THETA_EPSILON;
259 if( testAngle >= shadeAngle )
261 //Sys_Printf( "F(%3.3f >= %3.3f) ", RAD2DEG( testAngle ), RAD2DEG( shadeAngle ) );
264 //Sys_Printf( "P(%3.3f < %3.3f) ", RAD2DEG( testAngle ), RAD2DEG( shadeAngle ) );
266 /* add to the list */
267 indexes[ numVerts++ ] = j;
270 smoothed[ j >> 3 ] |= (1 << (j & 7));
272 /* see if this normal has already been voted */
273 for( k = 0; k < numVotes; k++ )
275 VectorSubtract( bspDrawVerts[ j ].normal, votes[ k ], diff );
276 if( fabs( diff[ 0 ] ) < EQUAL_NORMAL_EPSILON &&
277 fabs( diff[ 1 ] ) < EQUAL_NORMAL_EPSILON &&
278 fabs( diff[ 2 ] ) < EQUAL_NORMAL_EPSILON )
282 /* add a new vote? */
283 if( k == numVotes && numVotes < MAX_SAMPLES )
285 VectorAdd( average, bspDrawVerts[ j ].normal, average );
286 VectorCopy( bspDrawVerts[ j ].normal, votes[ numVotes ] );
291 /* don't average for less than 2 verts */
296 if( VectorNormalize( average, average ) > 0 )
299 for( j = 0; j < numVerts; j++ )
300 VectorCopy( average, yDrawVerts[ indexes[ j ] ].normal );
304 /* free the tables */
309 Sys_Printf( " (%i)\n", (int) (I_FloatTime() - start) );
314 /* -------------------------------------------------------------------------------
316 this section deals with phong shaded lightmap tracing
318 ------------------------------------------------------------------------------- */
320 /* 9th rewrite (recursive subdivision of a lightmap triangle) */
324 calculates the st tangent vectors for normalmapping
327 static qboolean CalcTangentVectors( int numVerts, bspDrawVert_t **dv, vec3_t *stv, vec3_t *ttv )
334 /* calculate barycentric basis for the triangle */
335 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 ]);
336 if( fabs( bb ) < 0.00000001f )
340 for( i = 0; i < numVerts; i++ )
342 /* calculate s tangent vector */
343 s = dv[ i ]->st[ 0 ] + 10.0f;
344 t = dv[ i ]->st[ 1 ];
345 bary[ 0 ] = ((dv[ 1 ]->st[ 0 ] - s) * (dv[ 2 ]->st[ 1 ] - t) - (dv[ 2 ]->st[ 0 ] - s) * (dv[ 1 ]->st[ 1 ] - t)) / bb;
346 bary[ 1 ] = ((dv[ 2 ]->st[ 0 ] - s) * (dv[ 0 ]->st[ 1 ] - t) - (dv[ 0 ]->st[ 0 ] - s) * (dv[ 2 ]->st[ 1 ] - t)) / bb;
347 bary[ 2 ] = ((dv[ 0 ]->st[ 0 ] - s) * (dv[ 1 ]->st[ 1 ] - t) - (dv[ 1 ]->st[ 0 ] - s) * (dv[ 0 ]->st[ 1 ] - t)) / bb;
349 stv[ i ][ 0 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 0 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 0 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 0 ];
350 stv[ i ][ 1 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 1 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 1 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 1 ];
351 stv[ i ][ 2 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 2 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 2 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 2 ];
353 VectorSubtract( stv[ i ], dv[ i ]->xyz, stv[ i ] );
354 VectorNormalize( stv[ i ], stv[ i ] );
356 /* calculate t tangent vector */
357 s = dv[ i ]->st[ 0 ];
358 t = dv[ i ]->st[ 1 ] + 10.0f;
359 bary[ 0 ] = ((dv[ 1 ]->st[ 0 ] - s) * (dv[ 2 ]->st[ 1 ] - t) - (dv[ 2 ]->st[ 0 ] - s) * (dv[ 1 ]->st[ 1 ] - t)) / bb;
360 bary[ 1 ] = ((dv[ 2 ]->st[ 0 ] - s) * (dv[ 0 ]->st[ 1 ] - t) - (dv[ 0 ]->st[ 0 ] - s) * (dv[ 2 ]->st[ 1 ] - t)) / bb;
361 bary[ 2 ] = ((dv[ 0 ]->st[ 0 ] - s) * (dv[ 1 ]->st[ 1 ] - t) - (dv[ 1 ]->st[ 0 ] - s) * (dv[ 0 ]->st[ 1 ] - t)) / bb;
363 ttv[ i ][ 0 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 0 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 0 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 0 ];
364 ttv[ i ][ 1 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 1 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 1 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 1 ];
365 ttv[ i ][ 2 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 2 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 2 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 2 ];
367 VectorSubtract( ttv[ i ], dv[ i ]->xyz, ttv[ i ] );
368 VectorNormalize( ttv[ i ], ttv[ i ] );
371 //% Sys_FPrintf( SYS_VRB, "%d S: (%f %f %f) T: (%f %f %f)\n", i,
372 //% stv[ i ][ 0 ], stv[ i ][ 1 ], stv[ i ][ 2 ], ttv[ i ][ 0 ], ttv[ i ][ 1 ], ttv[ i ][ 2 ] );
375 /* return to caller */
384 perterbs the normal by the shader's normalmap in tangent space
387 static void PerturbNormal( bspDrawVert_t *dv, shaderInfo_t *si, vec3_t pNormal, vec3_t stv[ 3 ], vec3_t ttv[ 3 ] )
394 VectorCopy( dv->normal, pNormal );
396 /* sample normalmap */
397 if( RadSampleImage( si->normalImage->pixels, si->normalImage->width, si->normalImage->height, dv->st, bump ) == qfalse )
400 /* remap sampled normal from [0,255] to [-1,-1] */
401 for( i = 0; i < 3; i++ )
402 bump[ i ] = (bump[ i ] - 127.0f) * (1.0f / 127.5f);
404 /* scale tangent vectors and add to original normal */
405 VectorMA( dv->normal, bump[ 0 ], stv[ 0 ], pNormal );
406 VectorMA( pNormal, bump[ 1 ], ttv[ 0 ], pNormal );
407 VectorMA( pNormal, bump[ 2 ], dv->normal, pNormal );
409 /* renormalize and return */
410 VectorNormalize( pNormal, pNormal );
417 maps a luxel for triangle bv at
421 #define BOGUS_NUDGE -99999.0f
423 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 ] )
425 int i, x, y, numClusters, *clusters, pointCluster, *cluster;
426 float *luxel, *origin, *normal, d, lightmapSampleOffset;
433 vec4_t sideplane, hostplane;
438 static float nudges[][ 2 ] =
440 //%{ 0, 0 }, /* try center first */
441 { -NUDGE, 0 }, /* left */
442 { NUDGE, 0 }, /* right */
443 { 0, NUDGE }, /* up */
444 { 0, -NUDGE }, /* down */
445 { -NUDGE, NUDGE }, /* left/up */
446 { NUDGE, -NUDGE }, /* right/down */
447 { NUDGE, NUDGE }, /* right/up */
448 { -NUDGE, -NUDGE }, /* left/down */
449 { BOGUS_NUDGE, BOGUS_NUDGE }
453 /* find luxel xy coords (fixme: subtract 0.5?) */
454 x = dv->lightmap[ 0 ][ 0 ];
455 y = dv->lightmap[ 0 ][ 1 ];
458 else if( x >= lm->sw )
462 else if( y >= lm->sh )
465 /* set shader and cluster list */
469 numClusters = info->numSurfaceClusters;
470 clusters = &surfaceClusters[ info->firstSurfaceCluster ];
479 /* get luxel, origin, cluster, and normal */
480 luxel = SUPER_LUXEL( 0, x, y );
481 origin = SUPER_ORIGIN( x, y );
482 normal = SUPER_NORMAL( x, y );
483 cluster = SUPER_CLUSTER( x, y );
485 /* don't attempt to remap occluded luxels for planar surfaces */
486 if( (*cluster) == CLUSTER_OCCLUDED && lm->plane != NULL )
489 /* only average the normal for premapped luxels */
490 else if( (*cluster) >= 0 )
492 /* do bumpmap calculations */
494 PerturbNormal( dv, si, pNormal, stv, ttv );
496 VectorCopy( dv->normal, pNormal );
498 /* add the additional normal data */
499 VectorAdd( normal, pNormal, normal );
504 /* otherwise, unmapped luxels (*cluster == CLUSTER_UNMAPPED) will have their full attributes calculated */
508 /* axial lightmap projection */
509 if( lm->vecs != NULL )
511 /* calculate an origin for the sample from the lightmap vectors */
512 VectorCopy( lm->origin, origin );
513 for( i = 0; i < 3; i++ )
515 /* add unless it's the axis, which is taken care of later */
516 if( i == lm->axisNum )
518 origin[ i ] += (x * lm->vecs[ 0 ][ i ]) + (y * lm->vecs[ 1 ][ i ]);
521 /* project the origin onto the plane */
522 d = DotProduct( origin, plane ) - plane[ 3 ];
523 d /= plane[ lm->axisNum ];
524 origin[ lm->axisNum ] -= d;
527 /* non axial lightmap projection (explicit xyz) */
529 VectorCopy( dv->xyz, origin );
531 //////////////////////
532 //27's test to make sure samples stay within the triangle boundaries
533 //1) Test the sample origin to see if it lays on the wrong side of any edge (x/y)
534 //2) if it does, nudge it onto the correct side.
536 if (worldverts!=NULL && lightmapTriangleCheck)
540 VectorCopy(worldverts[j],cverts[j]);
542 PlaneFromPoints(hostplane,cverts[0],cverts[1],cverts[2]);
548 //build plane using 2 edges and a normal
551 VectorCopy(cverts[next],temp);
552 VectorAdd(temp,hostplane,temp);
553 PlaneFromPoints(sideplane,cverts[i],cverts[ next ], temp);
555 //planetest sample point
556 e=DotProduct(origin,sideplane);
561 //VectorClear(origin);
562 //Move the sample point back inside triangle bounds
563 origin[0]-=sideplane[0]*(e+1);
564 origin[1]-=sideplane[1]*(e+1);
565 origin[2]-=sideplane[2]*(e+1);
574 ////////////////////////
576 /* planar surfaces have precalculated lightmap vectors for nudging */
577 if( lm->plane != NULL )
579 VectorCopy( lm->vecs[ 0 ], vecs[ 0 ] );
580 VectorCopy( lm->vecs[ 1 ], vecs[ 1 ] );
581 VectorCopy( lm->plane, vecs[ 2 ] );
584 /* non-planar surfaces must calculate them */
588 VectorCopy( plane, vecs[ 2 ] );
590 VectorCopy( dv->normal, vecs[ 2 ] );
591 MakeNormalVectors( vecs[ 2 ], vecs[ 0 ], vecs[ 1 ] );
594 /* push the origin off the surface a bit */
596 lightmapSampleOffset = si->lightmapSampleOffset;
598 lightmapSampleOffset = DEFAULT_LIGHTMAP_SAMPLE_OFFSET;
599 if( lm->axisNum < 0 )
600 VectorMA( origin, lightmapSampleOffset, vecs[ 2 ], origin );
601 else if( vecs[ 2 ][ lm->axisNum ] < 0.0f )
602 origin[ lm->axisNum ] -= lightmapSampleOffset;
604 origin[ lm->axisNum ] += lightmapSampleOffset;
606 VectorCopy(origin,origintwo);
607 if(lightmapExtraVisClusterNudge)
609 origintwo[0]+=vecs[2][0];
610 origintwo[1]+=vecs[2][1];
611 origintwo[2]+=vecs[2][2];
615 pointCluster = ClusterForPointExtFilter( origintwo, LUXEL_EPSILON, numClusters, clusters );
617 /* another retarded hack, storing nudge count in luxel[ 1 ] */
620 /* point in solid? (except in dark mode) */
621 if( pointCluster < 0 && dark == qfalse )
623 /* nudge the the location around */
625 while( nudge[ 0 ] > BOGUS_NUDGE && pointCluster < 0 )
627 /* nudge the vector around a bit */
628 for( i = 0; i < 3; i++ )
630 /* set nudged point*/
631 nudged[ i ] = origintwo[ i ] + (nudge[ 0 ] * vecs[ 0 ][ i ]) + (nudge[ 1 ] * vecs[ 1 ][ i ]);
635 /* get pvs cluster */
636 pointCluster = ClusterForPointExtFilter( nudged, LUXEL_EPSILON, numClusters, clusters ); //% + 0.625 );
637 if( pointCluster >= 0 )
638 VectorCopy( nudged, origin );
643 /* as a last resort, if still in solid, try drawvert origin offset by normal (except in dark mode) */
644 if( pointCluster < 0 && si != NULL && dark == qfalse )
646 VectorMA( dv->xyz, lightmapSampleOffset, dv->normal, nudged );
647 pointCluster = ClusterForPointExtFilter( nudged, LUXEL_EPSILON, numClusters, clusters );
648 if( pointCluster >= 0 )
649 VectorCopy( nudged, origin );
654 if( pointCluster < 0 )
656 (*cluster) = CLUSTER_OCCLUDED;
657 VectorClear( origin );
658 VectorClear( normal );
664 //% Sys_Printf( "%f %f %f\n", origin[ 0 ], origin[ 1 ], origin[ 2 ] );
666 /* do bumpmap calculations */
668 PerturbNormal( dv, si, pNormal, stv, ttv );
670 VectorCopy( dv->normal, pNormal );
672 /* store the cluster and normal */
673 (*cluster) = pointCluster;
674 VectorCopy( pNormal, normal );
676 /* store explicit mapping pass and implicit mapping pass */
691 recursively subdivides a triangle until its edges are shorter
692 than the distance between two luxels (thanks jc :)
695 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 ] )
697 bspDrawVert_t mid, *dv2[ 3 ];
701 /* map the vertexes */
703 MapSingleLuxel( lm, info, dv[ 0 ], plane, 1, stv, ttv );
704 MapSingleLuxel( lm, info, dv[ 1 ], plane, 1, stv, ttv );
705 MapSingleLuxel( lm, info, dv[ 2 ], plane, 1, stv, ttv );
711 float *a, *b, dx, dy, dist, maxDist;
714 /* find the longest edge and split it */
717 for( i = 0; i < 3; i++ )
720 a = dv[ i ]->lightmap[ 0 ];
721 b = dv[ (i + 1) % 3 ]->lightmap[ 0 ];
724 dx = a[ 0 ] - b[ 0 ];
725 dy = a[ 1 ] - b[ 1 ];
726 dist = (dx * dx) + (dy * dy); //% sqrt( (dx * dx) + (dy * dy) );
736 /* try to early out */
737 if( max < 0 || maxDist <= subdivideThreshold ) /* ydnar: was i < 0 instead of max < 0 (?) */
741 /* split the longest edge and map it */
742 LerpDrawVert( dv[ max ], dv[ (max + 1) % 3 ], &mid );
743 MapSingleLuxel( lm, info, &mid, plane, 1, stv, ttv, worldverts );
745 /* push the point up a little bit to account for fp creep (fixme: revisit this) */
746 //% VectorMA( mid.xyz, 2.0f, mid.normal, mid.xyz );
748 /* recurse to first triangle */
749 VectorCopy( dv, dv2 );
751 MapTriangle_r( lm, info, dv2, plane, stv, ttv, worldverts );
753 /* recurse to second triangle */
754 VectorCopy( dv, dv2 );
755 dv2[ (max + 1) % 3 ] = ∣
756 MapTriangle_r( lm, info, dv2, plane, stv, ttv, worldverts );
763 seed function for MapTriangle_r()
764 requires a cw ordered triangle
767 static qboolean MapTriangle( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t *dv[ 3 ], qboolean mapNonAxial )
771 vec3_t *stv, *ttv, stvStatic[ 3 ], ttvStatic[ 3 ];
772 vec3_t worldverts[ 3 ];
775 /* get plane if possible */
776 if( lm->plane != NULL )
778 VectorCopy( lm->plane, plane );
779 plane[ 3 ] = lm->plane[ 3 ];
782 /* otherwise make one from the points */
783 else if( PlaneFromPoints( plane, dv[ 0 ]->xyz, dv[ 1 ]->xyz, dv[ 2 ]->xyz ) == qfalse )
786 /* check to see if we need to calculate texture->world tangent vectors */
787 if( info->si->normalImage != NULL && CalcTangentVectors( 3, dv, stvStatic, ttvStatic ) )
798 VectorCopy( dv[ 0 ]->xyz, worldverts[ 0 ] );
799 VectorCopy( dv[ 1 ]->xyz, worldverts[ 1 ] );
800 VectorCopy( dv[ 2 ]->xyz, worldverts[ 2 ] );
802 /* map the vertexes */
803 MapSingleLuxel( lm, info, dv[ 0 ], plane, 1, stv, ttv, worldverts );
804 MapSingleLuxel( lm, info, dv[ 1 ], plane, 1, stv, ttv, worldverts );
805 MapSingleLuxel( lm, info, dv[ 2 ], plane, 1, stv, ttv, worldverts );
807 /* 2002-11-20: prefer axial triangle edges */
810 /* subdivide the triangle */
811 MapTriangle_r( lm, info, dv, plane, stv, ttv, worldverts );
815 for( i = 0; i < 3; i++ )
818 bspDrawVert_t *dv2[ 3 ];
822 a = dv[ i ]->lightmap[ 0 ];
823 b = dv[ (i + 1) % 3 ]->lightmap[ 0 ];
825 /* make degenerate triangles for mapping edges */
826 if( fabs( a[ 0 ] - b[ 0 ] ) < 0.01f || fabs( a[ 1 ] - b[ 1 ] ) < 0.01f )
829 dv2[ 1 ] = dv[ (i + 1) % 3 ];
830 dv2[ 2 ] = dv[ (i + 1) % 3 ];
832 /* map the degenerate triangle */
833 MapTriangle_r( lm, info, dv2, plane, stv, ttv, worldverts );
844 recursively subdivides a quad until its edges are shorter
845 than the distance between two luxels
848 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 ] )
850 bspDrawVert_t mid[ 2 ], *dv2[ 4 ];
857 float *a, *b, dx, dy, dist, maxDist;
860 /* find the longest edge and split it */
863 for( i = 0; i < 4; i++ )
866 a = dv[ i ]->lightmap[ 0 ];
867 b = dv[ (i + 1) % 4 ]->lightmap[ 0 ];
870 dx = a[ 0 ] - b[ 0 ];
871 dy = a[ 1 ] - b[ 1 ];
872 dist = (dx * dx) + (dy * dy); //% sqrt( (dx * dx) + (dy * dy) );
882 /* try to early out */
883 if( max < 0 || maxDist <= subdivideThreshold )
887 /* we only care about even/odd edges */
890 /* split the longest edges */
891 LerpDrawVert( dv[ max ], dv[ (max + 1) % 4 ], &mid[ 0 ] );
892 LerpDrawVert( dv[ max + 2 ], dv[ (max + 3) % 4 ], &mid[ 1 ] );
894 /* map the vertexes */
895 MapSingleLuxel( lm, info, &mid[ 0 ], plane, 1, stv, ttv, NULL );
896 MapSingleLuxel( lm, info, &mid[ 1 ], plane, 1, stv, ttv, NULL );
901 /* recurse to first quad */
903 dv2[ 1 ] = &mid[ 0 ];
904 dv2[ 2 ] = &mid[ 1 ];
906 MapQuad_r( lm, info, dv2, plane, stv, ttv );
908 /* recurse to second quad */
909 dv2[ 0 ] = &mid[ 0 ];
912 dv2[ 3 ] = &mid[ 1 ];
913 MapQuad_r( lm, info, dv2, plane, stv, ttv );
919 /* recurse to first quad */
922 dv2[ 2 ] = &mid[ 0 ];
923 dv2[ 3 ] = &mid[ 1 ];
924 MapQuad_r( lm, info, dv2, plane, stv, ttv );
926 /* recurse to second quad */
927 dv2[ 0 ] = &mid[ 1 ];
928 dv2[ 1 ] = &mid[ 0 ];
931 MapQuad_r( lm, info, dv2, plane, stv, ttv );
939 seed function for MapQuad_r()
940 requires a cw ordered triangle quad
943 #define QUAD_PLANAR_EPSILON 0.5f
945 static qboolean MapQuad( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t *dv[ 4 ] )
949 vec3_t *stv, *ttv, stvStatic[ 4 ], ttvStatic[ 4 ];
952 /* get plane if possible */
953 if( lm->plane != NULL )
955 VectorCopy( lm->plane, plane );
956 plane[ 3 ] = lm->plane[ 3 ];
959 /* otherwise make one from the points */
960 else if( PlaneFromPoints( plane, dv[ 0 ]->xyz, dv[ 1 ]->xyz, dv[ 2 ]->xyz ) == qfalse )
963 /* 4th point must fall on the plane */
964 dist = DotProduct( plane, dv[ 3 ]->xyz ) - plane[ 3 ];
965 if( fabs( dist ) > QUAD_PLANAR_EPSILON )
968 /* check to see if we need to calculate texture->world tangent vectors */
969 if( info->si->normalImage != NULL && CalcTangentVectors( 4, dv, stvStatic, ttvStatic ) )
980 /* map the vertexes */
981 MapSingleLuxel( lm, info, dv[ 0 ], plane, 1, stv, ttv, NULL );
982 MapSingleLuxel( lm, info, dv[ 1 ], plane, 1, stv, ttv, NULL );
983 MapSingleLuxel( lm, info, dv[ 2 ], plane, 1, stv, ttv, NULL );
984 MapSingleLuxel( lm, info, dv[ 3 ], plane, 1, stv, ttv, NULL );
986 /* subdivide the quad */
987 MapQuad_r( lm, info, dv, plane, stv, ttv );
995 maps the locations, normals, and pvs clusters for a raw lightmap
998 #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)
1000 void MapRawLightmap( int rawLightmapNum )
1002 int n, num, i, x, y, sx, sy, pw[ 5 ], r, *cluster, mapNonAxial;
1003 float *luxel, *origin, *normal, samples, radius, pass;
1005 bspDrawSurface_t *ds;
1006 surfaceInfo_t *info;
1007 mesh_t src, *subdivided, *mesh;
1008 bspDrawVert_t *verts, *dv[ 4 ], fake;
1011 /* bail if this number exceeds the number of raw lightmaps */
1012 if( rawLightmapNum >= numRawLightmaps )
1016 lm = &rawLightmaps[ rawLightmapNum ];
1018 /* -----------------------------------------------------------------
1019 map referenced surfaces onto the raw lightmap
1020 ----------------------------------------------------------------- */
1022 /* walk the list of surfaces on this raw lightmap */
1023 for( n = 0; n < lm->numLightSurfaces; n++ )
1025 /* with > 1 surface per raw lightmap, clear occluded */
1028 for( y = 0; y < lm->sh; y++ )
1030 for( x = 0; x < lm->sw; x++ )
1033 cluster = SUPER_CLUSTER( x, y );
1035 *cluster = CLUSTER_UNMAPPED;
1041 num = lightSurfaces[ lm->firstLightSurface + n ];
1042 ds = &bspDrawSurfaces[ num ];
1043 info = &surfaceInfos[ num ];
1045 /* bail if no lightmap to calculate */
1046 if( info->lm != lm )
1052 /* map the surface onto the lightmap origin/cluster/normal buffers */
1053 switch( ds->surfaceType )
1057 verts = yDrawVerts + ds->firstVert;
1059 /* map the triangles */
1060 for( mapNonAxial = 0; mapNonAxial < 2; mapNonAxial++ )
1062 for( i = 0; i < ds->numIndexes; i += 3 )
1064 dv[ 0 ] = &verts[ bspDrawIndexes[ ds->firstIndex + i ] ];
1065 dv[ 1 ] = &verts[ bspDrawIndexes[ ds->firstIndex + i + 1 ] ];
1066 dv[ 2 ] = &verts[ bspDrawIndexes[ ds->firstIndex + i + 2 ] ];
1067 MapTriangle( lm, info, dv, mapNonAxial );
1073 /* make a mesh from the drawsurf */
1074 src.width = ds->patchWidth;
1075 src.height = ds->patchHeight;
1076 src.verts = &yDrawVerts[ ds->firstVert ];
1077 //% subdivided = SubdivideMesh( src, 8, 512 );
1078 subdivided = SubdivideMesh2( src, info->patchIterations );
1080 /* fit it to the curve and remove colinear verts on rows/columns */
1081 PutMeshOnCurve( *subdivided );
1082 mesh = RemoveLinearMeshColumnsRows( subdivided );
1083 FreeMesh( subdivided );
1086 verts = mesh->verts;
1092 Sys_Printf( "Planar patch: [%1.3f %1.3f %1.3f] [%1.3f %1.3f %1.3f] [%1.3f %1.3f %1.3f]\n",
1093 lm->plane[ 0 ], lm->plane[ 1 ], lm->plane[ 2 ],
1094 lm->vecs[ 0 ][ 0 ], lm->vecs[ 0 ][ 1 ], lm->vecs[ 0 ][ 2 ],
1095 lm->vecs[ 1 ][ 0 ], lm->vecs[ 1 ][ 1 ], lm->vecs[ 1 ][ 2 ] );
1099 /* map the mesh quads */
1102 for( mapNonAxial = 0; mapNonAxial < 2; mapNonAxial++ )
1104 for( y = 0; y < (mesh->height - 1); y++ )
1106 for( x = 0; x < (mesh->width - 1); x++ )
1109 pw[ 0 ] = x + (y * mesh->width);
1110 pw[ 1 ] = x + ((y + 1) * mesh->width);
1111 pw[ 2 ] = x + 1 + ((y + 1) * mesh->width);
1112 pw[ 3 ] = x + 1 + (y * mesh->width);
1113 pw[ 4 ] = x + (y * mesh->width); /* same as pw[ 0 ] */
1118 /* get drawverts and map first triangle */
1119 dv[ 0 ] = &verts[ pw[ r + 0 ] ];
1120 dv[ 1 ] = &verts[ pw[ r + 1 ] ];
1121 dv[ 2 ] = &verts[ pw[ r + 2 ] ];
1122 MapTriangle( lm, info, dv, mapNonAxial );
1124 /* get drawverts and map second triangle */
1125 dv[ 0 ] = &verts[ pw[ r + 0 ] ];
1126 dv[ 1 ] = &verts[ pw[ r + 2 ] ];
1127 dv[ 2 ] = &verts[ pw[ r + 3 ] ];
1128 MapTriangle( lm, info, dv, mapNonAxial );
1135 for( y = 0; y < (mesh->height - 1); y++ )
1137 for( x = 0; x < (mesh->width - 1); x++ )
1140 pw[ 0 ] = x + (y * mesh->width);
1141 pw[ 1 ] = x + ((y + 1) * mesh->width);
1142 pw[ 2 ] = x + 1 + ((y + 1) * mesh->width);
1143 pw[ 3 ] = x + 1 + (y * mesh->width);
1149 /* attempt to map quad first */
1150 dv[ 0 ] = &verts[ pw[ r + 0 ] ];
1151 dv[ 1 ] = &verts[ pw[ r + 1 ] ];
1152 dv[ 2 ] = &verts[ pw[ r + 2 ] ];
1153 dv[ 3 ] = &verts[ pw[ r + 3 ] ];
1154 if( MapQuad( lm, info, dv ) )
1157 /* get drawverts and map first triangle */
1158 MapTriangle( lm, info, dv, mapNonAxial );
1160 /* get drawverts and map second triangle */
1161 dv[ 1 ] = &verts[ pw[ r + 2 ] ];
1162 dv[ 2 ] = &verts[ pw[ r + 3 ] ];
1163 MapTriangle( lm, info, dv, mapNonAxial );
1178 /* -----------------------------------------------------------------
1179 average and clean up luxel normals
1180 ----------------------------------------------------------------- */
1182 /* walk the luxels */
1183 for( y = 0; y < lm->sh; y++ )
1185 for( x = 0; x < lm->sw; x++ )
1188 luxel = SUPER_LUXEL( 0, x, y );
1189 normal = SUPER_NORMAL( x, y );
1190 cluster = SUPER_CLUSTER( x, y );
1192 /* only look at mapped luxels */
1196 /* the normal data could be the sum of multiple samples */
1197 if( luxel[ 3 ] > 1.0f )
1198 VectorNormalize( normal, normal );
1200 /* mark this luxel as having only one normal */
1205 /* non-planar surfaces stop here */
1206 if( lm->plane == NULL )
1209 /* -----------------------------------------------------------------
1210 map occluded or unuxed luxels
1211 ----------------------------------------------------------------- */
1213 /* walk the luxels */
1214 radius = floor( superSample / 2 );
1215 radius = radius > 0 ? radius : 1.0f;
1217 for( pass = 2.0f; pass <= radius; pass += 1.0f )
1219 for( y = 0; y < lm->sh; y++ )
1221 for( x = 0; x < lm->sw; x++ )
1224 luxel = SUPER_LUXEL( 0, x, y );
1225 normal = SUPER_NORMAL( x, y );
1226 cluster = SUPER_CLUSTER( x, y );
1228 /* only look at unmapped luxels */
1229 if( *cluster != CLUSTER_UNMAPPED )
1232 /* divine a normal and origin from neighboring luxels */
1233 VectorClear( fake.xyz );
1234 VectorClear( fake.normal );
1235 fake.lightmap[ 0 ][ 0 ] = x; //% 0.0001 + x;
1236 fake.lightmap[ 0 ][ 1 ] = y; //% 0.0001 + y;
1238 for( sy = (y - 1); sy <= (y + 1); sy++ )
1240 if( sy < 0 || sy >= lm->sh )
1243 for( sx = (x - 1); sx <= (x + 1); sx++ )
1245 if( sx < 0 || sx >= lm->sw || (sx == x && sy == y) )
1248 /* get neighboring luxel */
1249 luxel = SUPER_LUXEL( 0, sx, sy );
1250 origin = SUPER_ORIGIN( sx, sy );
1251 normal = SUPER_NORMAL( sx, sy );
1252 cluster = SUPER_CLUSTER( sx, sy );
1254 /* only consider luxels mapped in previous passes */
1255 if( *cluster < 0 || luxel[ 0 ] >= pass )
1258 /* add its distinctiveness to our own */
1259 VectorAdd( fake.xyz, origin, fake.xyz );
1260 VectorAdd( fake.normal, normal, fake.normal );
1261 samples += luxel[ 3 ];
1266 if( samples == 0.0f )
1270 VectorDivide( fake.xyz, samples, fake.xyz );
1271 //% VectorDivide( fake.normal, samples, fake.normal );
1272 if( VectorNormalize( fake.normal, fake.normal ) == 0.0f )
1275 /* map the fake vert */
1276 MapSingleLuxel( lm, NULL, &fake, lm->plane, pass, NULL, NULL, NULL );
1281 /* -----------------------------------------------------------------
1282 average and clean up luxel normals
1283 ----------------------------------------------------------------- */
1285 /* walk the luxels */
1286 for( y = 0; y < lm->sh; y++ )
1288 for( x = 0; x < lm->sw; x++ )
1291 luxel = SUPER_LUXEL( 0, x, y );
1292 normal = SUPER_NORMAL( x, y );
1293 cluster = SUPER_CLUSTER( x, y );
1295 /* only look at mapped luxels */
1299 /* the normal data could be the sum of multiple samples */
1300 if( luxel[ 3 ] > 1.0f )
1301 VectorNormalize( normal, normal );
1303 /* mark this luxel as having only one normal */
1311 for( y = 0; y < lm->sh; y++ )
1313 for( x = 0; x < lm->sw; x++ )
1318 cluster = SUPER_CLUSTER( x, y );
1319 origin = SUPER_ORIGIN( x, y );
1320 normal = SUPER_NORMAL( x, y );
1321 luxel = SUPER_LUXEL( x, y );
1326 /* check if within the bounding boxes of all surfaces referenced */
1327 ClearBounds( mins, maxs );
1328 for( n = 0; n < lm->numLightSurfaces; n++ )
1331 info = &surfaceInfos[ lightSurfaces[ lm->firstLightSurface + n ] ];
1332 TOL = info->sampleSize + 2;
1333 AddPointToBounds( info->mins, mins, maxs );
1334 AddPointToBounds( info->maxs, mins, maxs );
1335 if( origin[ 0 ] > (info->mins[ 0 ] - TOL) && origin[ 0 ] < (info->maxs[ 0 ] + TOL) &&
1336 origin[ 1 ] > (info->mins[ 1 ] - TOL) && origin[ 1 ] < (info->maxs[ 1 ] + TOL) &&
1337 origin[ 2 ] > (info->mins[ 2 ] - TOL) && origin[ 2 ] < (info->maxs[ 2 ] + TOL) )
1342 if( n < lm->numLightSurfaces )
1345 /* report bogus origin */
1346 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",
1347 rawLightmapNum, x, y, *cluster,
1348 origin[ 0 ], origin[ 1 ], origin[ 2 ],
1349 mins[ 0 ], mins[ 1 ], mins[ 2 ],
1350 maxs[ 0 ], maxs[ 1 ], maxs[ 2 ],
1361 sets up dirtmap (ambient occlusion)
1364 #define DIRT_CONE_ANGLE 88 /* degrees */
1365 #define DIRT_NUM_ANGLE_STEPS 16
1366 #define DIRT_NUM_ELEVATION_STEPS 3
1367 #define DIRT_NUM_VECTORS (DIRT_NUM_ANGLE_STEPS * DIRT_NUM_ELEVATION_STEPS)
1369 static vec3_t dirtVectors[ DIRT_NUM_VECTORS ];
1370 static int numDirtVectors = 0;
1372 void SetupDirt( void )
1375 float angle, elevation, angleStep, elevationStep;
1379 Sys_FPrintf( SYS_VRB, "--- SetupDirt ---\n" );
1381 /* calculate angular steps */
1382 angleStep = DEG2RAD( 360.0f / DIRT_NUM_ANGLE_STEPS );
1383 elevationStep = DEG2RAD( DIRT_CONE_ANGLE / DIRT_NUM_ELEVATION_STEPS );
1387 for( i = 0, angle = 0.0f; i < DIRT_NUM_ANGLE_STEPS; i++, angle += angleStep )
1389 /* iterate elevation */
1390 for( j = 0, elevation = elevationStep * 0.5f; j < DIRT_NUM_ELEVATION_STEPS; j++, elevation += elevationStep )
1392 dirtVectors[ numDirtVectors ][ 0 ] = sin( elevation ) * cos( angle );
1393 dirtVectors[ numDirtVectors ][ 1 ] = sin( elevation ) * sin( angle );
1394 dirtVectors[ numDirtVectors ][ 2 ] = cos( elevation );
1399 /* emit some statistics */
1400 Sys_FPrintf( SYS_VRB, "%9d dirtmap vectors\n", numDirtVectors );
1406 calculates dirt value for a given sample
1409 float DirtForSample( trace_t *trace )
1412 float gatherDirt, outDirt, angle, elevation, ooDepth;
1413 vec3_t normal, worldUp, myUp, myRt, temp, direction, displacement;
1419 if( trace == NULL || trace->cluster < 0 )
1424 ooDepth = 1.0f / dirtDepth;
1425 VectorCopy( trace->normal, normal );
1427 /* check if the normal is aligned to the world-up */
1428 if( normal[ 0 ] == 0.0f && normal[ 1 ] == 0.0f )
1430 if( normal[ 2 ] == 1.0f )
1432 VectorSet( myRt, 1.0f, 0.0f, 0.0f );
1433 VectorSet( myUp, 0.0f, 1.0f, 0.0f );
1435 else if( normal[ 2 ] == -1.0f )
1437 VectorSet( myRt, -1.0f, 0.0f, 0.0f );
1438 VectorSet( myUp, 0.0f, 1.0f, 0.0f );
1443 VectorSet( worldUp, 0.0f, 0.0f, 1.0f );
1444 CrossProduct( normal, worldUp, myRt );
1445 VectorNormalize( myRt, myRt );
1446 CrossProduct( myRt, normal, myUp );
1447 VectorNormalize( myUp, myUp );
1450 /* 1 = random mode, 0 (well everything else) = non-random mode */
1454 for( i = 0; i < numDirtVectors; i++ )
1456 /* get random vector */
1457 angle = Random() * DEG2RAD( 360.0f );
1458 elevation = Random() * DEG2RAD( DIRT_CONE_ANGLE );
1459 temp[ 0 ] = cos( angle ) * sin( elevation );
1460 temp[ 1 ] = sin( angle ) * sin( elevation );
1461 temp[ 2 ] = cos( elevation );
1463 /* transform into tangent space */
1464 direction[ 0 ] = myRt[ 0 ] * temp[ 0 ] + myUp[ 0 ] * temp[ 1 ] + normal[ 0 ] * temp[ 2 ];
1465 direction[ 1 ] = myRt[ 1 ] * temp[ 0 ] + myUp[ 1 ] * temp[ 1 ] + normal[ 1 ] * temp[ 2 ];
1466 direction[ 2 ] = myRt[ 2 ] * temp[ 0 ] + myUp[ 2 ] * temp[ 1 ] + normal[ 2 ] * temp[ 2 ];
1469 VectorMA( trace->origin, dirtDepth, direction, trace->end );
1470 SetupTrace( trace );
1476 VectorSubtract( trace->hit, trace->origin, displacement );
1477 gatherDirt += 1.0f - ooDepth * VectorLength( displacement );
1483 /* iterate through ordered vectors */
1484 for( i = 0; i < numDirtVectors; i++ )
1486 /* transform vector into tangent space */
1487 direction[ 0 ] = myRt[ 0 ] * dirtVectors[ i ][ 0 ] + myUp[ 0 ] * dirtVectors[ i ][ 1 ] + normal[ 0 ] * dirtVectors[ i ][ 2 ];
1488 direction[ 1 ] = myRt[ 1 ] * dirtVectors[ i ][ 0 ] + myUp[ 1 ] * dirtVectors[ i ][ 1 ] + normal[ 1 ] * dirtVectors[ i ][ 2 ];
1489 direction[ 2 ] = myRt[ 2 ] * dirtVectors[ i ][ 0 ] + myUp[ 2 ] * dirtVectors[ i ][ 1 ] + normal[ 2 ] * dirtVectors[ i ][ 2 ];
1492 VectorMA( trace->origin, dirtDepth, direction, trace->end );
1493 SetupTrace( trace );
1499 VectorSubtract( trace->hit, trace->origin, displacement );
1500 gatherDirt += 1.0f - ooDepth * VectorLength( displacement );
1506 VectorMA( trace->origin, dirtDepth, normal, trace->end );
1507 SetupTrace( trace );
1513 VectorSubtract( trace->hit, trace->origin, displacement );
1514 gatherDirt += 1.0f - ooDepth * VectorLength( displacement );
1518 if( gatherDirt <= 0.0f )
1521 /* apply gain (does this even do much? heh) */
1522 outDirt = pow( gatherDirt / (numDirtVectors + 1), dirtGain );
1523 if( outDirt > 1.0f )
1527 outDirt *= dirtScale;
1528 if( outDirt > 1.0f )
1531 /* return to sender */
1532 return 1.0f - outDirt;
1539 calculates dirty fraction for each luxel
1542 void DirtyRawLightmap( int rawLightmapNum )
1544 int i, x, y, sx, sy, *cluster;
1545 float *origin, *normal, *dirt, *dirt2, average, samples;
1547 surfaceInfo_t *info;
1551 /* bail if this number exceeds the number of raw lightmaps */
1552 if( rawLightmapNum >= numRawLightmaps )
1556 lm = &rawLightmaps[ rawLightmapNum ];
1559 trace.testOcclusion = qtrue;
1560 trace.forceSunlight = qfalse;
1561 trace.recvShadows = lm->recvShadows;
1562 trace.numSurfaces = lm->numLightSurfaces;
1563 trace.surfaces = &lightSurfaces[ lm->firstLightSurface ];
1564 trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
1565 trace.testAll = qfalse;
1567 /* twosided lighting (may or may not be a good idea for lightmapped stuff) */
1568 trace.twoSided = qfalse;
1569 for( i = 0; i < trace.numSurfaces; i++ )
1572 info = &surfaceInfos[ trace.surfaces[ i ] ];
1574 /* check twosidedness */
1575 if( info->si->twoSided )
1577 trace.twoSided = qtrue;
1583 for( y = 0; y < lm->sh; y++ )
1585 for( x = 0; x < lm->sw; x++ )
1588 cluster = SUPER_CLUSTER( x, y );
1589 origin = SUPER_ORIGIN( x, y );
1590 normal = SUPER_NORMAL( x, y );
1591 dirt = SUPER_DIRT( x, y );
1593 /* set default dirt */
1596 /* only look at mapped luxels */
1601 trace.cluster = *cluster;
1602 VectorCopy( origin, trace.origin );
1603 VectorCopy( normal, trace.normal );
1606 *dirt = DirtForSample( &trace );
1610 /* testing no filtering */
1614 for( y = 0; y < lm->sh; y++ )
1616 for( x = 0; x < lm->sw; x++ )
1619 cluster = SUPER_CLUSTER( x, y );
1620 dirt = SUPER_DIRT( x, y );
1622 /* filter dirt by adjacency to unmapped luxels */
1625 for( sy = (y - 1); sy <= (y + 1); sy++ )
1627 if( sy < 0 || sy >= lm->sh )
1630 for( sx = (x - 1); sx <= (x + 1); sx++ )
1632 if( sx < 0 || sx >= lm->sw || (sx == x && sy == y) )
1635 /* get neighboring luxel */
1636 cluster = SUPER_CLUSTER( sx, sy );
1637 dirt2 = SUPER_DIRT( sx, sy );
1638 if( *cluster < 0 || *dirt2 <= 0.0f )
1647 if( samples <= 0.0f )
1652 if( samples <= 0.0f )
1656 *dirt = average / samples;
1665 calculates the pvs cluster, origin, normal of a sub-luxel
1668 static qboolean SubmapRawLuxel( rawLightmap_t *lm, int x, int y, float bx, float by, int *sampleCluster, vec3_t sampleOrigin, vec3_t sampleNormal )
1670 int i, *cluster, *cluster2;
1671 float *origin, *origin2, *normal; //% , *normal2;
1672 vec3_t originVecs[ 2 ]; //% , normalVecs[ 2 ];
1675 /* calulate x vector */
1676 if( (x < (lm->sw - 1) && bx >= 0.0f) || (x == 0 && bx <= 0.0f) )
1678 cluster = SUPER_CLUSTER( x, y );
1679 origin = SUPER_ORIGIN( x, y );
1680 //% normal = SUPER_NORMAL( x, y );
1681 cluster2 = SUPER_CLUSTER( x + 1, y );
1682 origin2 = *cluster2 < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x + 1, y );
1683 //% normal2 = *cluster2 < 0 ? SUPER_NORMAL( x, y ) : SUPER_NORMAL( x + 1, y );
1685 else if( (x > 0 && bx <= 0.0f) || (x == (lm->sw - 1) && bx >= 0.0f) )
1687 cluster = SUPER_CLUSTER( x - 1, y );
1688 origin = *cluster < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x - 1, y );
1689 //% normal = *cluster < 0 ? SUPER_NORMAL( x, y ) : SUPER_NORMAL( x - 1, y );
1690 cluster2 = SUPER_CLUSTER( x, y );
1691 origin2 = SUPER_ORIGIN( x, y );
1692 //% normal2 = SUPER_NORMAL( x, y );
1695 Sys_Printf( "WARNING: Spurious lightmap S vector\n" );
1697 VectorSubtract( origin2, origin, originVecs[ 0 ] );
1698 //% VectorSubtract( normal2, normal, normalVecs[ 0 ] );
1700 /* calulate y vector */
1701 if( (y < (lm->sh - 1) && bx >= 0.0f) || (y == 0 && bx <= 0.0f) )
1703 cluster = SUPER_CLUSTER( x, y );
1704 origin = SUPER_ORIGIN( x, y );
1705 //% normal = SUPER_NORMAL( x, y );
1706 cluster2 = SUPER_CLUSTER( x, y + 1 );
1707 origin2 = *cluster2 < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x, y + 1 );
1708 //% normal2 = *cluster2 < 0 ? SUPER_NORMAL( x, y ) : SUPER_NORMAL( x, y + 1 );
1710 else if( (y > 0 && bx <= 0.0f) || (y == (lm->sh - 1) && bx >= 0.0f) )
1712 cluster = SUPER_CLUSTER( x, y - 1 );
1713 origin = *cluster < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x, y - 1 );
1714 //% normal = *cluster < 0 ? SUPER_NORMAL( x, y ) : SUPER_NORMAL( x, y - 1 );
1715 cluster2 = SUPER_CLUSTER( x, y );
1716 origin2 = SUPER_ORIGIN( x, y );
1717 //% normal2 = SUPER_NORMAL( x, y );
1720 Sys_Printf( "WARNING: Spurious lightmap T vector\n" );
1722 VectorSubtract( origin2, origin, originVecs[ 1 ] );
1723 //% VectorSubtract( normal2, normal, normalVecs[ 1 ] );
1725 /* calculate new origin */
1726 //% VectorMA( origin, bx, originVecs[ 0 ], sampleOrigin );
1727 //% VectorMA( sampleOrigin, by, originVecs[ 1 ], sampleOrigin );
1728 for( i = 0; i < 3; i++ )
1729 sampleOrigin[ i ] = sampleOrigin[ i ] + (bx * originVecs[ 0 ][ i ]) + (by * originVecs[ 1 ][ i ]);
1732 *sampleCluster = ClusterForPointExtFilter( sampleOrigin, (LUXEL_EPSILON * 2), lm->numLightClusters, lm->lightClusters );
1733 if( *sampleCluster < 0 )
1736 /* calculate new normal */
1737 //% VectorMA( normal, bx, normalVecs[ 0 ], sampleNormal );
1738 //% VectorMA( sampleNormal, by, normalVecs[ 1 ], sampleNormal );
1739 //% if( VectorNormalize( sampleNormal, sampleNormal ) <= 0.0f )
1741 normal = SUPER_NORMAL( x, y );
1742 VectorCopy( normal, sampleNormal );
1750 SubsampleRawLuxel_r()
1751 recursively subsamples a luxel until its color gradient is low enough or subsampling limit is reached
1754 static void SubsampleRawLuxel_r( rawLightmap_t *lm, trace_t *trace, vec3_t sampleOrigin, int x, int y, float bias, float *lightLuxel )
1756 int b, samples, mapped, lighted;
1759 vec3_t origin[ 4 ], normal[ 4 ];
1760 float biasDirs[ 4 ][ 2 ] = { { -1.0f, -1.0f }, { 1.0f, -1.0f }, { -1.0f, 1.0f }, { 1.0f, 1.0f } };
1761 vec3_t color, total;
1765 if( lightLuxel[ 3 ] >= lightSamples )
1769 VectorClear( total );
1773 /* make 2x2 subsample stamp */
1774 for( b = 0; b < 4; b++ )
1777 VectorCopy( sampleOrigin, origin[ b ] );
1779 /* calculate position */
1780 if( !SubmapRawLuxel( lm, x, y, (bias * biasDirs[ b ][ 0 ]), (bias * biasDirs[ b ][ 1 ]), &cluster[ b ], origin[ b ], normal[ b ] ) )
1787 /* increment sample count */
1788 luxel[ b ][ 3 ] = lightLuxel[ 3 ] + 1.0f;
1791 trace->cluster = *cluster;
1792 VectorCopy( origin[ b ], trace->origin );
1793 VectorCopy( normal[ b ], trace->normal );
1797 LightContributionToSample( trace );
1799 /* add to totals (fixme: make contrast function) */
1800 VectorCopy( trace->color, luxel[ b ] );
1801 VectorAdd( total, trace->color, total );
1802 if( (luxel[ b ][ 0 ] + luxel[ b ][ 1 ] + luxel[ b ][ 2 ]) > 0.0f )
1806 /* subsample further? */
1807 if( (lightLuxel[ 3 ] + 1.0f) < lightSamples &&
1808 (total[ 0 ] > 4.0f || total[ 1 ] > 4.0f || total[ 2 ] > 4.0f) &&
1809 lighted != 0 && lighted != mapped )
1811 for( b = 0; b < 4; b++ )
1813 if( cluster[ b ] < 0 )
1815 SubsampleRawLuxel_r( lm, trace, origin[ b ], x, y, (bias * 0.25f), luxel[ b ] );
1820 //% VectorClear( color );
1822 VectorCopy( lightLuxel, color );
1824 for( b = 0; b < 4; b++ )
1826 if( cluster[ b ] < 0 )
1828 VectorAdd( color, luxel[ b ], color );
1836 color[ 0 ] /= samples;
1837 color[ 1 ] /= samples;
1838 color[ 2 ] /= samples;
1841 VectorCopy( color, lightLuxel );
1842 lightLuxel[ 3 ] += 1.0f;
1849 IlluminateRawLightmap()
1850 illuminates the luxels
1853 #define STACK_LL_SIZE (SUPER_LUXEL_SIZE * 64 * 64)
1854 #define LIGHT_LUXEL( x, y ) (lightLuxels + ((((y) * lm->sw) + (x)) * SUPER_LUXEL_SIZE))
1856 void IlluminateRawLightmap( int rawLightmapNum )
1858 int i, t, x, y, sx, sy, size, llSize, luxelFilterRadius, lightmapNum;
1859 int *cluster, *cluster2, mapped, lighted, totalLighted;
1861 surfaceInfo_t *info;
1862 qboolean filterColor, filterDir;
1864 float *origin, *normal, *dirt, *luxel, *luxel2, *deluxel, *deluxel2;
1865 float *lightLuxels, *lightLuxel, samples, filterRadius, weight;
1866 vec3_t color, averageColor, averageDir, total, temp, temp2;
1867 float tests[ 4 ][ 2 ] = { { 0.0f, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } };
1869 float stackLightLuxels[ STACK_LL_SIZE ];
1874 /* bail if this number exceeds the number of raw lightmaps */
1875 if( rawLightmapNum >= numRawLightmaps )
1879 lm = &rawLightmaps[ rawLightmapNum ];
1882 trace.testOcclusion = !noTrace;
1883 trace.forceSunlight = qfalse;
1884 trace.recvShadows = lm->recvShadows;
1885 trace.numSurfaces = lm->numLightSurfaces;
1886 trace.surfaces = &lightSurfaces[ lm->firstLightSurface ];
1887 trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
1889 /* twosided lighting (may or may not be a good idea for lightmapped stuff) */
1890 trace.twoSided = qfalse;
1891 for( i = 0; i < trace.numSurfaces; i++ )
1894 info = &surfaceInfos[ trace.surfaces[ i ] ];
1896 /* check twosidedness */
1897 if( info->si->twoSided )
1899 trace.twoSided = qtrue;
1904 /* create a culled light list for this raw lightmap */
1905 CreateTraceLightsForBounds( lm->mins, lm->maxs, lm->plane, lm->numLightClusters, lm->lightClusters, LIGHT_SURFACES, &trace );
1907 /* -----------------------------------------------------------------
1909 ----------------------------------------------------------------- */
1912 numLuxelsIlluminated += (lm->sw * lm->sh);
1914 /* test debugging state */
1915 if( debugSurfaces || debugAxis || debugCluster || debugOrigin || dirtDebug || normalmap )
1917 /* debug fill the luxels */
1918 for( y = 0; y < lm->sh; y++ )
1920 for( x = 0; x < lm->sw; x++ )
1923 cluster = SUPER_CLUSTER( x, y );
1925 /* only fill mapped luxels */
1929 /* get particulars */
1930 luxel = SUPER_LUXEL( 0, x, y );
1931 origin = SUPER_ORIGIN( x, y );
1932 normal = SUPER_NORMAL( x, y );
1934 /* color the luxel with raw lightmap num? */
1936 VectorCopy( debugColors[ rawLightmapNum % 12 ], luxel );
1938 /* color the luxel with lightmap axis? */
1939 else if( debugAxis )
1941 luxel[ 0 ] = (lm->axis[ 0 ] + 1.0f) * 127.5f;
1942 luxel[ 1 ] = (lm->axis[ 1 ] + 1.0f) * 127.5f;
1943 luxel[ 2 ] = (lm->axis[ 2 ] + 1.0f) * 127.5f;
1946 /* color the luxel with luxel cluster? */
1947 else if( debugCluster )
1948 VectorCopy( debugColors[ *cluster % 12 ], luxel );
1950 /* color the luxel with luxel origin? */
1951 else if( debugOrigin )
1953 VectorSubtract( lm->maxs, lm->mins, temp );
1954 VectorScale( temp, (1.0f / 255.0f), temp );
1955 VectorSubtract( origin, lm->mins, temp2 );
1956 luxel[ 0 ] = lm->mins[ 0 ] + (temp[ 0 ] * temp2[ 0 ]);
1957 luxel[ 1 ] = lm->mins[ 1 ] + (temp[ 1 ] * temp2[ 1 ]);
1958 luxel[ 2 ] = lm->mins[ 2 ] + (temp[ 2 ] * temp2[ 2 ]);
1961 /* color the luxel with the normal */
1962 else if( normalmap )
1964 luxel[ 0 ] = (normal[ 0 ] + 1.0f) * 127.5f;
1965 luxel[ 1 ] = (normal[ 1 ] + 1.0f) * 127.5f;
1966 luxel[ 2 ] = (normal[ 2 ] + 1.0f) * 127.5f;
1969 /* otherwise clear it */
1971 VectorClear( luxel );
1980 /* allocate temporary per-light luxel storage */
1981 llSize = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float );
1982 if( llSize <= (STACK_LL_SIZE * sizeof( float )) )
1983 lightLuxels = stackLightLuxels;
1985 lightLuxels = safe_malloc( llSize );
1988 //% memset( lm->superLuxels[ 0 ], 0, llSize );
1990 /* set ambient color */
1991 for( y = 0; y < lm->sh; y++ )
1993 for( x = 0; x < lm->sw; x++ )
1996 cluster = SUPER_CLUSTER( x, y );
1997 luxel = SUPER_LUXEL( 0, x, y );
1998 normal = SUPER_NORMAL( x, y );
1999 deluxel = SUPER_DELUXEL( x, y );
2001 /* blacken unmapped clusters */
2003 VectorClear( luxel );
2008 VectorCopy( ambientColor, luxel );
2011 VectorScale( normal, 0.00390625f, deluxel );
2012 deluxel[3] = 0.00390625f;
2019 /* clear styled lightmaps */
2020 size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float );
2021 for( lightmapNum = 1; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2023 if( lm->superLuxels[ lightmapNum ] != NULL )
2024 memset( lm->superLuxels[ lightmapNum ], 0, size );
2027 /* debugging code */
2028 //% if( trace.numLights <= 0 )
2029 //% Sys_Printf( "Lightmap %9d: 0 lights, axis: %.2f, %.2f, %.2f\n", rawLightmapNum, lm->axis[ 0 ], lm->axis[ 1 ], lm->axis[ 2 ] );
2031 /* walk light list */
2032 for( i = 0; i < trace.numLights; i++ )
2035 trace.light = trace.lights[ i ];
2038 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2040 if( lm->styles[ lightmapNum ] == trace.light->style ||
2041 lm->styles[ lightmapNum ] == LS_NONE )
2045 /* max of MAX_LIGHTMAPS (4) styles allowed to hit a surface/lightmap */
2046 if( lightmapNum >= MAX_LIGHTMAPS )
2048 Sys_Printf( "WARNING: Hit per-surface style limit (%d)\n", MAX_LIGHTMAPS );
2053 memset( lightLuxels, 0, llSize );
2056 /* initial pass, one sample per luxel */
2057 for( y = 0; y < lm->sh; y++ )
2059 for( x = 0; x < lm->sw; x++ )
2062 cluster = SUPER_CLUSTER( x, y );
2066 /* get particulars */
2067 lightLuxel = LIGHT_LUXEL( x, y );
2068 deluxel = SUPER_DELUXEL( x, y );
2069 origin = SUPER_ORIGIN( x, y );
2070 normal = SUPER_NORMAL( x, y );
2073 ////////// 27's temp hack for testing edge clipping ////
2074 if( origin[0]==0 && origin[1]==0 && origin[2]==0 )
2076 lightLuxel[ 1 ] = 255;
2077 lightLuxel[ 3 ] = 1.0f;
2083 /* set contribution count */
2084 lightLuxel[ 3 ] = 1.0f;
2087 trace.cluster = *cluster;
2088 VectorCopy( origin, trace.origin );
2089 VectorCopy( normal, trace.normal );
2091 /* get light for this sample */
2092 LightContributionToSample( &trace );
2093 VectorCopy( trace.color, lightLuxel );
2096 if( trace.color[ 0 ] || trace.color[ 1 ] || trace.color[ 2 ] )
2100 /* add to light direction map (fixme: use luxel normal as starting point for deluxel?) */
2103 /* color to grayscale (photoshop rgb weighting) */
2104 brightness = trace.color[ 0 ] * 0.3f + trace.color[ 1 ] * 0.59f + trace.color[ 2 ] * 0.11f;
2105 brightness *= (1.0 / 255.0);
2106 VectorScale( trace.direction, brightness, temp );
2107 VectorAdd( deluxel, temp, deluxel );
2108 deluxel[3] += brightness;
2113 /* don't even bother with everything else if nothing was lit */
2114 if( totalLighted == 0 )
2117 /* determine filter radius */
2118 filterRadius = lm->filterRadius > trace.light->filterRadius
2120 : trace.light->filterRadius;
2121 if( filterRadius < 0.0f )
2122 filterRadius = 0.0f;
2124 /* set luxel filter radius */
2125 luxelFilterRadius = superSample * filterRadius / lm->sampleSize;
2126 if( luxelFilterRadius == 0 && (filterRadius > 0.0f || filter) )
2127 luxelFilterRadius = 1;
2129 /* secondary pass, adaptive supersampling (fixme: use a contrast function to determine if subsampling is necessary) */
2130 /* 2003-09-27: changed it so filtering disamples supersampling, as it would waste time */
2131 if( lightSamples > 1 && luxelFilterRadius == 0 )
2134 for( y = 0; y < (lm->sh - 1); y++ )
2136 for( x = 0; x < (lm->sw - 1); x++ )
2141 VectorClear( total );
2143 /* test 2x2 stamp */
2144 for( t = 0; t < 4; t++ )
2146 /* set sample coords */
2147 sx = x + tests[ t ][ 0 ];
2148 sy = y + tests[ t ][ 1 ];
2151 cluster = SUPER_CLUSTER( sx, sy );
2157 lightLuxel = LIGHT_LUXEL( sx, sy );
2158 VectorAdd( total, lightLuxel, total );
2159 if( (lightLuxel[ 0 ] + lightLuxel[ 1 ] + lightLuxel[ 2 ]) > 0.0f )
2163 /* if total color is under a certain amount, then don't bother subsampling */
2164 if( total[ 0 ] <= 4.0f && total[ 1 ] <= 4.0f && total[ 2 ] <= 4.0f )
2167 /* if all 4 pixels are either in shadow or light, then don't subsample */
2168 if( lighted != 0 && lighted != mapped )
2170 for( t = 0; t < 4; t++ )
2172 /* set sample coords */
2173 sx = x + tests[ t ][ 0 ];
2174 sy = y + tests[ t ][ 1 ];
2177 cluster = SUPER_CLUSTER( sx, sy );
2180 lightLuxel = LIGHT_LUXEL( sx, sy );
2181 origin = SUPER_ORIGIN( sx, sy );
2183 /* only subsample shadowed luxels */
2184 //% if( (lightLuxel[ 0 ] + lightLuxel[ 1 ] + lightLuxel[ 2 ]) <= 0.0f )
2188 SubsampleRawLuxel_r( lm, &trace, origin, sx, sy, 0.25f, lightLuxel );
2190 /* debug code to colorize subsampled areas to yellow */
2191 //% luxel = SUPER_LUXEL( lightmapNum, sx, sy );
2192 //% VectorSet( luxel, 255, 204, 0 );
2199 /* tertiary pass, apply dirt map (ambient occlusion) */
2203 for( y = 0; y < lm->sh; y++ )
2205 for( x = 0; x < lm->sw; x++ )
2208 cluster = SUPER_CLUSTER( x, y );
2212 /* get particulars */
2213 lightLuxel = LIGHT_LUXEL( x, y );
2214 dirt = SUPER_DIRT( x, y );
2216 /* scale light value */
2217 VectorScale( lightLuxel, *dirt, lightLuxel );
2222 /* allocate sampling lightmap storage */
2223 if( lm->superLuxels[ lightmapNum ] == NULL )
2225 /* allocate sampling lightmap storage */
2226 size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float );
2227 lm->superLuxels[ lightmapNum ] = safe_malloc( size );
2228 memset( lm->superLuxels[ lightmapNum ], 0, size );
2232 if( lightmapNum > 0 )
2234 lm->styles[ lightmapNum ] = trace.light->style;
2235 //% Sys_Printf( "Surface %6d has lightstyle %d\n", rawLightmapNum, trace.light->style );
2238 /* copy to permanent luxels */
2239 for( y = 0; y < lm->sh; y++ )
2241 for( x = 0; x < lm->sw; x++ )
2243 /* get cluster and origin */
2244 cluster = SUPER_CLUSTER( x, y );
2247 origin = SUPER_ORIGIN( x, y );
2250 if( luxelFilterRadius )
2253 VectorClear( averageColor );
2256 /* cheaper distance-based filtering */
2257 for( sy = (y - luxelFilterRadius); sy <= (y + luxelFilterRadius); sy++ )
2259 if( sy < 0 || sy >= lm->sh )
2262 for( sx = (x - luxelFilterRadius); sx <= (x + luxelFilterRadius); sx++ )
2264 if( sx < 0 || sx >= lm->sw )
2267 /* get particulars */
2268 cluster = SUPER_CLUSTER( sx, sy );
2271 lightLuxel = LIGHT_LUXEL( sx, sy );
2274 weight = (abs( sx - x ) == luxelFilterRadius ? 0.5f : 1.0f);
2275 weight *= (abs( sy - y ) == luxelFilterRadius ? 0.5f : 1.0f);
2277 /* scale luxel by filter weight */
2278 VectorScale( lightLuxel, weight, color );
2279 VectorAdd( averageColor, color, averageColor );
2285 if( samples <= 0.0f )
2288 /* scale into luxel */
2289 luxel = SUPER_LUXEL( lightmapNum, x, y );
2292 /* handle negative light */
2293 if( trace.light->flags & LIGHT_NEGATIVE )
2295 luxel[ 0 ] -= averageColor[ 0 ] / samples;
2296 luxel[ 1 ] -= averageColor[ 1 ] / samples;
2297 luxel[ 2 ] -= averageColor[ 2 ] / samples;
2300 /* handle normal light */
2303 luxel[ 0 ] += averageColor[ 0 ] / samples;
2304 luxel[ 1 ] += averageColor[ 1 ] / samples;
2305 luxel[ 2 ] += averageColor[ 2 ] / samples;
2312 /* get particulars */
2313 lightLuxel = LIGHT_LUXEL( x, y );
2314 luxel = SUPER_LUXEL( lightmapNum, x, y );
2316 /* handle negative light */
2317 if( trace.light->flags & LIGHT_NEGATIVE )
2318 VectorScale( averageColor, -1.0f, averageColor );
2323 /* handle negative light */
2324 if( trace.light->flags & LIGHT_NEGATIVE )
2325 VectorSubtract( luxel, lightLuxel, luxel );
2327 /* handle normal light */
2329 VectorAdd( luxel, lightLuxel, luxel );
2335 /* free temporary luxels */
2336 if( lightLuxels != stackLightLuxels )
2337 free( lightLuxels );
2340 /* free light list */
2341 FreeTraceLights( &trace );
2343 /* -----------------------------------------------------------------
2345 ----------------------------------------------------------------- */
2349 /* walk lightmaps */
2350 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2353 if( lm->superLuxels[ lightmapNum ] == NULL )
2356 /* apply floodlight to each luxel */
2357 for( y = 0; y < lm->sh; y++ )
2359 for( x = 0; x < lm->sw; x++ )
2362 cluster = SUPER_CLUSTER( x, y );
2366 /* get particulars */
2367 luxel = SUPER_LUXEL( lightmapNum, x, y );
2368 floodlight = SUPER_FLOODLIGHT( x, y );
2370 flood[0]=floodlightRGB[0]*floodlightIntensity;
2371 flood[1]=floodlightRGB[1]*floodlightIntensity;
2372 flood[2]=floodlightRGB[2]*floodlightIntensity;
2374 /* scale light value */
2375 VectorScale( flood, *floodlight, flood );
2380 if (luxel[3]==0) luxel[3]=1;
2388 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2391 if( lm->superLuxels[ lightmapNum ] == NULL )
2394 for( y = 0; y < lm->sh; y++ )
2396 for( x = 0; x < lm->sw; x++ )
2399 cluster = SUPER_CLUSTER( x, y );
2400 //% if( *cluster < 0 )
2403 /* get particulars */
2404 luxel = SUPER_LUXEL( lightmapNum, x, y );
2405 normal = SUPER_NORMAL ( x, y );
2407 luxel[0]=(normal[0]*127)+127;
2408 luxel[1]=(normal[1]*127)+127;
2409 luxel[2]=(normal[2]*127)+127;
2415 /* -----------------------------------------------------------------
2417 ----------------------------------------------------------------- */
2421 /* walk lightmaps */
2422 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2425 if( lm->superLuxels[ lightmapNum ] == NULL )
2428 /* apply dirt to each luxel */
2429 for( y = 0; y < lm->sh; y++ )
2431 for( x = 0; x < lm->sw; x++ )
2434 cluster = SUPER_CLUSTER( x, y );
2435 //% if( *cluster < 0 ) // TODO why not do this check? These pixels should be zero anyway
2438 /* get particulars */
2439 luxel = SUPER_LUXEL( lightmapNum, x, y );
2440 dirt = SUPER_DIRT( x, y );
2443 VectorScale( luxel, *dirt, luxel );
2447 VectorSet( luxel, *dirt * 255.0f, *dirt * 255.0f, *dirt * 255.0f );
2453 /* -----------------------------------------------------------------
2455 ----------------------------------------------------------------- */
2457 /* walk lightmaps */
2458 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2461 if( lm->superLuxels[ lightmapNum ] == NULL )
2464 /* average occluded luxels from neighbors */
2465 for( y = 0; y < lm->sh; y++ )
2467 for( x = 0; x < lm->sw; x++ )
2469 /* get particulars */
2470 cluster = SUPER_CLUSTER( x, y );
2471 luxel = SUPER_LUXEL( lightmapNum, x, y );
2472 deluxel = SUPER_DELUXEL( x, y );
2473 normal = SUPER_NORMAL( x, y );
2475 /* determine if filtering is necessary */
2476 filterColor = qfalse;
2479 (lm->splotchFix && (luxel[ 0 ] <= ambientColor[ 0 ] || luxel[ 1 ] <= ambientColor[ 1 ] || luxel[ 2 ] <= ambientColor[ 2 ])) )
2480 filterColor = qtrue;
2481 if( deluxemap && lightmapNum == 0 && (*cluster < 0 || filter) )
2484 if( !filterColor && !filterDir )
2487 /* choose seed amount */
2488 VectorClear( averageColor );
2489 VectorClear( averageDir );
2492 /* walk 3x3 matrix */
2493 for( sy = (y - 1); sy <= (y + 1); sy++ )
2495 if( sy < 0 || sy >= lm->sh )
2498 for( sx = (x - 1); sx <= (x + 1); sx++ )
2500 if( sx < 0 || sx >= lm->sw || (sx == x && sy == y) )
2503 /* get neighbor's particulars */
2504 cluster2 = SUPER_CLUSTER( sx, sy );
2505 luxel2 = SUPER_LUXEL( lightmapNum, sx, sy );
2506 deluxel2 = SUPER_DELUXEL( sx, sy );
2508 /* ignore unmapped/unlit luxels */
2509 if( *cluster2 < 0 || luxel2[ 3 ] == 0.0f ||
2510 (lm->splotchFix && VectorCompare( luxel2, ambientColor )) )
2513 /* add its distinctiveness to our own */
2514 VectorAdd( averageColor, luxel2, averageColor );
2515 samples += luxel2[ 3 ];
2517 VectorAdd( averageDir, deluxel2, averageDir );
2522 if( samples <= 0.0f )
2525 /* dark lightmap seams */
2528 if( lightmapNum == 0 )
2529 VectorMA( averageColor, 2.0f, ambientColor, averageColor );
2536 VectorDivide( averageColor, samples, luxel );
2540 VectorDivide( averageDir, samples, deluxel );
2542 /* set cluster to -3 */
2544 *cluster = CLUSTER_FLOODED;
2552 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2555 if( lm->superLuxels[ lightmapNum ] == NULL )
2557 for( y = 0; y < lm->sh; y++ )
2558 for( x = 0; x < lm->sw; x++ )
2561 cluster = SUPER_CLUSTER( x, y );
2562 luxel = SUPER_LUXEL( lightmapNum, x, y );
2563 deluxel = SUPER_DELUXEL( x, y );
2564 if(!luxel || !deluxel || !cluster)
2566 Sys_FPrintf(SYS_VRB, "WARNING: I got NULL'd.\n");
2569 else if(*cluster < 0)
2572 // should have neither deluxemap nor lightmap
2574 Sys_FPrintf(SYS_VRB, "WARNING: I have written deluxe to an unmapped luxel. Sorry.\n");
2579 // should have both deluxemap and lightmap
2581 Sys_FPrintf(SYS_VRB, "WARNING: I forgot to write deluxe to a mapped luxel. Sorry.\n");
2591 IlluminateVertexes()
2592 light the surface vertexes
2595 #define VERTEX_NUDGE 4.0f
2597 void IlluminateVertexes( int num )
2599 int i, x, y, z, x1, y1, z1, sx, sy, radius, maxRadius, *cluster;
2600 int lightmapNum, numAvg;
2601 float samples, *vertLuxel, *radVertLuxel, *luxel, dirt;
2602 vec3_t origin, temp, temp2, colors[ MAX_LIGHTMAPS ], avgColors[ MAX_LIGHTMAPS ];
2603 bspDrawSurface_t *ds;
2604 surfaceInfo_t *info;
2606 bspDrawVert_t *verts;
2610 /* get surface, info, and raw lightmap */
2611 ds = &bspDrawSurfaces[ num ];
2612 info = &surfaceInfos[ num ];
2615 /* -----------------------------------------------------------------
2616 illuminate the vertexes
2617 ----------------------------------------------------------------- */
2619 /* calculate vertex lighting for surfaces without lightmaps */
2620 if( lm == NULL || cpmaHack )
2623 trace.testOcclusion = (cpmaHack && lm != NULL) ? qfalse : !noTrace;
2624 trace.forceSunlight = info->si->forceSunlight;
2625 trace.recvShadows = info->recvShadows;
2626 trace.numSurfaces = 1;
2627 trace.surfaces = #
2628 trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
2630 /* twosided lighting */
2631 trace.twoSided = info->si->twoSided;
2633 /* make light list for this surface */
2634 CreateTraceLightsForSurface( num, &trace );
2637 verts = yDrawVerts + ds->firstVert;
2639 memset( avgColors, 0, sizeof( avgColors ) );
2641 /* walk the surface verts */
2642 for( i = 0; i < ds->numVerts; i++ )
2644 /* get vertex luxel */
2645 radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2647 /* color the luxel with raw lightmap num? */
2649 VectorCopy( debugColors[ num % 12 ], radVertLuxel );
2651 /* color the luxel with luxel origin? */
2652 else if( debugOrigin )
2654 VectorSubtract( info->maxs, info->mins, temp );
2655 VectorScale( temp, (1.0f / 255.0f), temp );
2656 VectorSubtract( origin, lm->mins, temp2 );
2657 radVertLuxel[ 0 ] = info->mins[ 0 ] + (temp[ 0 ] * temp2[ 0 ]);
2658 radVertLuxel[ 1 ] = info->mins[ 1 ] + (temp[ 1 ] * temp2[ 1 ]);
2659 radVertLuxel[ 2 ] = info->mins[ 2 ] + (temp[ 2 ] * temp2[ 2 ]);
2662 /* color the luxel with the normal */
2663 else if( normalmap )
2665 radVertLuxel[ 0 ] = (verts[ i ].normal[ 0 ] + 1.0f) * 127.5f;
2666 radVertLuxel[ 1 ] = (verts[ i ].normal[ 1 ] + 1.0f) * 127.5f;
2667 radVertLuxel[ 2 ] = (verts[ i ].normal[ 2 ] + 1.0f) * 127.5f;
2670 /* illuminate the vertex */
2673 /* clear vertex luxel */
2674 VectorSet( radVertLuxel, -1.0f, -1.0f, -1.0f );
2676 /* try at initial origin */
2677 trace.cluster = ClusterForPointExtFilter( verts[ i ].xyz, VERTEX_EPSILON, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ] );
2678 if( trace.cluster >= 0 )
2681 VectorCopy( verts[ i ].xyz, trace.origin );
2682 VectorCopy( verts[ i ].normal, trace.normal );
2686 dirt = DirtForSample( &trace );
2691 LightingAtSample( &trace, ds->vertexStyles, colors );
2694 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2697 VectorScale( colors[ lightmapNum ], dirt, colors[ lightmapNum ] );
2700 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2701 VectorCopy( colors[ lightmapNum ], radVertLuxel );
2702 VectorAdd( avgColors[ lightmapNum ], colors[ lightmapNum ], colors[ lightmapNum ] );
2706 /* is this sample bright enough? */
2707 radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2708 if( radVertLuxel[ 0 ] <= ambientColor[ 0 ] &&
2709 radVertLuxel[ 1 ] <= ambientColor[ 1 ] &&
2710 radVertLuxel[ 2 ] <= ambientColor[ 2 ] )
2712 /* nudge the sample point around a bit */
2713 for( x = 0; x < 4; x++ )
2715 /* two's complement 0, 1, -1, 2, -2, etc */
2716 x1 = ((x >> 1) ^ (x & 1 ? -1 : 0)) + (x & 1);
2718 for( y = 0; y < 4; y++ )
2720 y1 = ((y >> 1) ^ (y & 1 ? -1 : 0)) + (y & 1);
2722 for( z = 0; z < 4; z++ )
2724 z1 = ((z >> 1) ^ (z & 1 ? -1 : 0)) + (z & 1);
2727 trace.origin[ 0 ] = verts[ i ].xyz[ 0 ] + (VERTEX_NUDGE * x1);
2728 trace.origin[ 1 ] = verts[ i ].xyz[ 1 ] + (VERTEX_NUDGE * y1);
2729 trace.origin[ 2 ] = verts[ i ].xyz[ 2 ] + (VERTEX_NUDGE * z1);
2731 /* try at nudged origin */
2732 trace.cluster = ClusterForPointExtFilter( origin, VERTEX_EPSILON, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ] );
2733 if( trace.cluster < 0 )
2737 LightingAtSample( &trace, ds->vertexStyles, colors );
2740 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2743 VectorScale( colors[ lightmapNum ], dirt, colors[ lightmapNum ] );
2746 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2747 VectorCopy( colors[ lightmapNum ], radVertLuxel );
2750 /* bright enough? */
2751 radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2752 if( radVertLuxel[ 0 ] > ambientColor[ 0 ] ||
2753 radVertLuxel[ 1 ] > ambientColor[ 1 ] ||
2754 radVertLuxel[ 2 ] > ambientColor[ 2 ] )
2761 /* add to average? */
2762 radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2763 if( radVertLuxel[ 0 ] > ambientColor[ 0 ] ||
2764 radVertLuxel[ 1 ] > ambientColor[ 1 ] ||
2765 radVertLuxel[ 2 ] > ambientColor[ 2 ] )
2768 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2770 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2771 VectorAdd( avgColors[ lightmapNum ], radVertLuxel, avgColors[ lightmapNum ] );
2776 /* another happy customer */
2777 numVertsIlluminated++;
2780 /* set average color */
2783 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2784 VectorScale( avgColors[ lightmapNum ], (1.0f / numAvg), avgColors[ lightmapNum ] );
2788 VectorCopy( ambientColor, avgColors[ 0 ] );
2791 /* clean up and store vertex color */
2792 for( i = 0; i < ds->numVerts; i++ )
2794 /* get vertex luxel */
2795 radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2797 /* store average in occluded vertexes */
2798 if( radVertLuxel[ 0 ] < 0.0f )
2800 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2802 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2803 VectorCopy( avgColors[ lightmapNum ], radVertLuxel );
2806 //% VectorSet( radVertLuxel, 255.0f, 0.0f, 0.0f );
2811 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2814 vertLuxel = VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2815 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2818 if( bouncing || bounce == 0 || !bounceOnly )
2819 VectorAdd( vertLuxel, radVertLuxel, vertLuxel );
2820 if( !info->si->noVertexLight )
2821 ColorToBytes( vertLuxel, verts[ i ].color[ lightmapNum ], info->si->vertexScale );
2825 /* free light list */
2826 FreeTraceLights( &trace );
2828 /* return to sender */
2832 /* -----------------------------------------------------------------
2833 reconstitute vertex lighting from the luxels
2834 ----------------------------------------------------------------- */
2836 /* set styles from lightmap */
2837 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2838 ds->vertexStyles[ lightmapNum ] = lm->styles[ lightmapNum ];
2840 /* get max search radius */
2842 maxRadius = maxRadius > lm->sh ? maxRadius : lm->sh;
2844 /* walk the surface verts */
2845 verts = yDrawVerts + ds->firstVert;
2846 for( i = 0; i < ds->numVerts; i++ )
2848 /* do each lightmap */
2849 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2852 if( lm->superLuxels[ lightmapNum ] == NULL )
2855 /* get luxel coords */
2856 x = verts[ i ].lightmap[ lightmapNum ][ 0 ];
2857 y = verts[ i ].lightmap[ lightmapNum ][ 1 ];
2860 else if( x >= lm->sw )
2864 else if( y >= lm->sh )
2867 /* get vertex luxels */
2868 vertLuxel = VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2869 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2871 /* color the luxel with the normal? */
2874 radVertLuxel[ 0 ] = (verts[ i ].normal[ 0 ] + 1.0f) * 127.5f;
2875 radVertLuxel[ 1 ] = (verts[ i ].normal[ 1 ] + 1.0f) * 127.5f;
2876 radVertLuxel[ 2 ] = (verts[ i ].normal[ 2 ] + 1.0f) * 127.5f;
2879 /* color the luxel with surface num? */
2880 else if( debugSurfaces )
2881 VectorCopy( debugColors[ num % 12 ], radVertLuxel );
2883 /* divine color from the superluxels */
2886 /* increasing radius */
2887 VectorClear( radVertLuxel );
2889 for( radius = 0; radius < maxRadius && samples <= 0.0f; radius++ )
2891 /* sample within radius */
2892 for( sy = (y - radius); sy <= (y + radius); sy++ )
2894 if( sy < 0 || sy >= lm->sh )
2897 for( sx = (x - radius); sx <= (x + radius); sx++ )
2899 if( sx < 0 || sx >= lm->sw )
2902 /* get luxel particulars */
2903 luxel = SUPER_LUXEL( lightmapNum, sx, sy );
2904 cluster = SUPER_CLUSTER( sx, sy );
2908 /* testing: must be brigher than ambient color */
2909 //% if( luxel[ 0 ] <= ambientColor[ 0 ] || luxel[ 1 ] <= ambientColor[ 1 ] || luxel[ 2 ] <= ambientColor[ 2 ] )
2912 /* add its distinctiveness to our own */
2913 VectorAdd( radVertLuxel, luxel, radVertLuxel );
2914 samples += luxel[ 3 ];
2920 if( samples > 0.0f )
2921 VectorDivide( radVertLuxel, samples, radVertLuxel );
2923 VectorCopy( ambientColor, radVertLuxel );
2926 /* store into floating point storage */
2927 VectorAdd( vertLuxel, radVertLuxel, vertLuxel );
2928 numVertsIlluminated++;
2930 /* store into bytes (for vertex approximation) */
2931 if( !info->si->noVertexLight )
2932 ColorToBytes( vertLuxel, verts[ i ].color[ lightmapNum ], 1.0f );
2939 /* -------------------------------------------------------------------------------
2941 light optimization (-fast)
2943 creates a list of lights that will affect a surface and stores it in tw
2944 this is to optimize surface lighting by culling out as many of the
2945 lights in the world as possible from further calculation
2947 ------------------------------------------------------------------------------- */
2951 determines opaque brushes in the world and find sky shaders for sunlight calculations
2954 void SetupBrushes( void )
2956 int i, j, b, compileFlags;
2959 bspBrushSide_t *side;
2960 bspShader_t *shader;
2965 Sys_FPrintf( SYS_VRB, "--- SetupBrushes ---\n" );
2968 if( opaqueBrushes == NULL )
2969 opaqueBrushes = safe_malloc( numBSPBrushes / 8 + 1 );
2972 memset( opaqueBrushes, 0, numBSPBrushes / 8 + 1 );
2973 numOpaqueBrushes = 0;
2975 /* walk the list of worldspawn brushes */
2976 for( i = 0; i < bspModels[ 0 ].numBSPBrushes; i++ )
2979 b = bspModels[ 0 ].firstBSPBrush + i;
2980 brush = &bspBrushes[ b ];
2982 /* check all sides */
2985 for( j = 0; j < brush->numSides && inside; j++ )
2987 /* do bsp shader calculations */
2988 side = &bspBrushSides[ brush->firstSide + j ];
2989 shader = &bspShaders[ side->shaderNum ];
2991 /* get shader info */
2992 si = ShaderInfoForShader( shader->shader );
2996 /* or together compile flags */
2997 compileFlags |= si->compileFlags;
3000 /* determine if this brush is opaque to light */
3001 if( !(compileFlags & C_TRANSLUCENT) )
3003 opaqueBrushes[ b >> 3 ] |= (1 << (b & 7));
3009 /* emit some statistics */
3010 Sys_FPrintf( SYS_VRB, "%9d opaque brushes\n", numOpaqueBrushes );
3017 determines if two clusters are visible to each other using the PVS
3020 qboolean ClusterVisible( int a, int b )
3022 int portalClusters, leafBytes;
3027 if( a < 0 || b < 0 )
3035 if( numBSPVisBytes <=8 )
3039 portalClusters = ((int *) bspVisBytes)[ 0 ];
3040 leafBytes = ((int*) bspVisBytes)[ 1 ];
3041 pvs = bspVisBytes + VIS_HEADER_SIZE + (a * leafBytes);
3044 if( (pvs[ b >> 3 ] & (1 << (b & 7))) )
3053 borrowed from vlight.c
3056 int PointInLeafNum_r( vec3_t point, int nodenum )
3064 while( nodenum >= 0 )
3066 node = &bspNodes[ nodenum ];
3067 plane = &bspPlanes[ node->planeNum ];
3068 dist = DotProduct( point, plane->normal ) - plane->dist;
3070 nodenum = node->children[ 0 ];
3071 else if( dist < -0.1 )
3072 nodenum = node->children[ 1 ];
3075 leafnum = PointInLeafNum_r( point, node->children[ 0 ] );
3076 if( bspLeafs[ leafnum ].cluster != -1 )
3078 nodenum = node->children[ 1 ];
3082 leafnum = -nodenum - 1;
3090 borrowed from vlight.c
3093 int PointInLeafNum( vec3_t point )
3095 return PointInLeafNum_r( point, 0 );
3101 ClusterVisibleToPoint() - ydnar
3102 returns qtrue if point can "see" cluster
3105 qboolean ClusterVisibleToPoint( vec3_t point, int cluster )
3110 /* get leafNum for point */
3111 pointCluster = ClusterForPoint( point );
3112 if( pointCluster < 0 )
3116 return ClusterVisible( pointCluster, cluster );
3122 ClusterForPoint() - ydnar
3123 returns the pvs cluster for point
3126 int ClusterForPoint( vec3_t point )
3131 /* get leafNum for point */
3132 leafNum = PointInLeafNum( point );
3136 /* return the cluster */
3137 return bspLeafs[ leafNum ].cluster;
3143 ClusterForPointExt() - ydnar
3144 also takes brushes into account for occlusion testing
3147 int ClusterForPointExt( vec3_t point, float epsilon )
3149 int i, j, b, leafNum, cluster;
3152 int *brushes, numBSPBrushes;
3158 /* get leaf for point */
3159 leafNum = PointInLeafNum( point );
3162 leaf = &bspLeafs[ leafNum ];
3164 /* get the cluster */
3165 cluster = leaf->cluster;
3169 /* transparent leaf, so check point against all brushes in the leaf */
3170 brushes = &bspLeafBrushes[ leaf->firstBSPLeafBrush ];
3171 numBSPBrushes = leaf->numBSPLeafBrushes;
3172 for( i = 0; i < numBSPBrushes; i++ )
3176 if( b > maxOpaqueBrush )
3178 brush = &bspBrushes[ b ];
3179 if( !(opaqueBrushes[ b >> 3 ] & (1 << (b & 7))) )
3182 /* check point against all planes */
3184 for( j = 0; j < brush->numSides && inside; j++ )
3186 plane = &bspPlanes[ bspBrushSides[ brush->firstSide + j ].planeNum ];
3187 dot = DotProduct( point, plane->normal );
3193 /* if inside, return bogus cluster */
3198 /* if the point made it this far, it's not inside any opaque brushes */
3205 ClusterForPointExtFilter() - ydnar
3206 adds cluster checking against a list of known valid clusters
3209 int ClusterForPointExtFilter( vec3_t point, float epsilon, int numClusters, int *clusters )
3214 /* get cluster for point */
3215 cluster = ClusterForPointExt( point, epsilon );
3217 /* check if filtering is necessary */
3218 if( cluster < 0 || numClusters <= 0 || clusters == NULL )
3222 for( i = 0; i < numClusters; i++ )
3224 if( cluster == clusters[ i ] || ClusterVisible( cluster, clusters[ i ] ) )
3235 ShaderForPointInLeaf() - ydnar
3236 checks a point against all brushes in a leaf, returning the shader of the brush
3237 also sets the cumulative surface and content flags for the brush hit
3240 int ShaderForPointInLeaf( vec3_t point, int leafNum, float epsilon, int wantContentFlags, int wantSurfaceFlags, int *contentFlags, int *surfaceFlags )
3245 int *brushes, numBSPBrushes;
3248 bspBrushSide_t *side;
3250 bspShader_t *shader;
3251 int allSurfaceFlags, allContentFlags;
3254 /* clear things out first */
3261 leaf = &bspLeafs[ leafNum ];
3263 /* transparent leaf, so check point against all brushes in the leaf */
3264 brushes = &bspLeafBrushes[ leaf->firstBSPLeafBrush ];
3265 numBSPBrushes = leaf->numBSPLeafBrushes;
3266 for( i = 0; i < numBSPBrushes; i++ )
3269 brush = &bspBrushes[ brushes[ i ] ];
3271 /* check point against all planes */
3273 allSurfaceFlags = 0;
3274 allContentFlags = 0;
3275 for( j = 0; j < brush->numSides && inside; j++ )
3277 side = &bspBrushSides[ brush->firstSide + j ];
3278 plane = &bspPlanes[ side->planeNum ];
3279 dot = DotProduct( point, plane->normal );
3285 shader = &bspShaders[ side->shaderNum ];
3286 allSurfaceFlags |= shader->surfaceFlags;
3287 allContentFlags |= shader->contentFlags;
3291 /* handle if inside */
3294 /* if there are desired flags, check for same and continue if they aren't matched */
3295 if( wantContentFlags && !(wantContentFlags & allContentFlags) )
3297 if( wantSurfaceFlags && !(wantSurfaceFlags & allSurfaceFlags) )
3300 /* store the cumulative flags and return the brush shader (which is mostly useless) */
3301 *surfaceFlags = allSurfaceFlags;
3302 *contentFlags = allContentFlags;
3303 return brush->shaderNum;
3307 /* if the point made it this far, it's not inside any brushes */
3315 chops a bounding box by the plane defined by origin and normal
3316 returns qfalse if the bounds is entirely clipped away
3318 this is not exactly the fastest way to do this...
3321 qboolean ChopBounds( vec3_t mins, vec3_t maxs, vec3_t origin, vec3_t normal )
3323 /* FIXME: rewrite this so it doesn't use bloody brushes */
3331 calculates each light's effective envelope,
3332 taking into account brightness, type, and pvs.
3335 #define LIGHT_EPSILON 0.125f
3336 #define LIGHT_NUDGE 2.0f
3338 void SetupEnvelopes( qboolean forGrid, qboolean fastFlag )
3340 int i, x, y, z, x1, y1, z1;
3341 light_t *light, *light2, **owner;
3343 vec3_t origin, dir, mins, maxs, nullVector = { 0, 0, 0 };
3344 float radius, intensity;
3345 light_t *buckets[ 256 ];
3348 /* early out for weird cases where there are no lights */
3349 if( lights == NULL )
3353 Sys_FPrintf( SYS_VRB, "--- SetupEnvelopes%s ---\n", fastFlag ? " (fast)" : "" );
3357 numCulledLights = 0;
3359 while( *owner != NULL )
3364 /* handle negative lights */
3365 if( light->photons < 0.0f || light->add < 0.0f )
3367 light->photons *= -1.0f;
3368 light->add *= -1.0f;
3369 light->flags |= LIGHT_NEGATIVE;
3373 if( light->type == EMIT_SUN )
3377 light->envelope = MAX_WORLD_COORD * 8.0f;
3378 VectorSet( light->mins, MIN_WORLD_COORD * 8.0f, MIN_WORLD_COORD * 8.0f, MIN_WORLD_COORD * 8.0f );
3379 VectorSet( light->maxs, MAX_WORLD_COORD * 8.0f, MAX_WORLD_COORD * 8.0f, MAX_WORLD_COORD * 8.0f );
3382 /* everything else */
3385 /* get pvs cluster for light */
3386 light->cluster = ClusterForPointExt( light->origin, LIGHT_EPSILON );
3388 /* invalid cluster? */
3389 if( light->cluster < 0 )
3391 /* nudge the sample point around a bit */
3392 for( x = 0; x < 4; x++ )
3394 /* two's complement 0, 1, -1, 2, -2, etc */
3395 x1 = ((x >> 1) ^ (x & 1 ? -1 : 0)) + (x & 1);
3397 for( y = 0; y < 4; y++ )
3399 y1 = ((y >> 1) ^ (y & 1 ? -1 : 0)) + (y & 1);
3401 for( z = 0; z < 4; z++ )
3403 z1 = ((z >> 1) ^ (z & 1 ? -1 : 0)) + (z & 1);
3406 origin[ 0 ] = light->origin[ 0 ] + (LIGHT_NUDGE * x1);
3407 origin[ 1 ] = light->origin[ 1 ] + (LIGHT_NUDGE * y1);
3408 origin[ 2 ] = light->origin[ 2 ] + (LIGHT_NUDGE * z1);
3410 /* try at nudged origin */
3411 light->cluster = ClusterForPointExt( origin, LIGHT_EPSILON );
3412 if( light->cluster < 0 )
3416 VectorCopy( origin, light->origin );
3422 /* only calculate for lights in pvs and outside of opaque brushes */
3423 if( light->cluster >= 0 )
3425 /* set light fast flag */
3427 light->flags |= LIGHT_FAST_TEMP;
3429 light->flags &= ~LIGHT_FAST_TEMP;
3430 if( light->si && light->si->noFast )
3431 light->flags &= ~(LIGHT_FAST | LIGHT_FAST_TEMP);
3433 /* clear light envelope */
3434 light->envelope = 0;
3436 /* handle area lights */
3437 if( exactPointToPolygon && light->type == EMIT_AREA && light->w != NULL )
3439 /* ugly hack to calculate extent for area lights, but only done once */
3440 VectorScale( light->normal, -1.0f, dir );
3441 for( radius = 100.0f; radius < 130000.0f && light->envelope == 0; radius += 10.0f )
3445 VectorMA( light->origin, radius, light->normal, origin );
3446 factor = PointToPolygonFormFactor( origin, dir, light->w );
3449 if( (factor * light->add) <= light->falloffTolerance )
3450 light->envelope = radius;
3453 /* check for fast mode */
3454 if( !(light->flags & LIGHT_FAST) && !(light->flags & LIGHT_FAST_TEMP) )
3455 light->envelope = MAX_WORLD_COORD * 8.0f;
3460 intensity = light->photons;
3464 if( light->envelope <= 0.0f )
3466 /* solve distance for non-distance lights */
3467 if( !(light->flags & LIGHT_ATTEN_DISTANCE) )
3468 light->envelope = MAX_WORLD_COORD * 8.0f;
3470 /* solve distance for linear lights */
3471 else if( (light->flags & LIGHT_ATTEN_LINEAR ) )
3472 //% light->envelope = ((intensity / light->falloffTolerance) * linearScale - 1 + radius) / light->fade;
3473 light->envelope = ((intensity * linearScale) - light->falloffTolerance) / light->fade;
3476 add = angle * light->photons * linearScale - (dist * light->fade);
3477 T = (light->photons * linearScale) - (dist * light->fade);
3478 T + (dist * light->fade) = (light->photons * linearScale);
3479 dist * light->fade = (light->photons * linearScale) - T;
3480 dist = ((light->photons * linearScale) - T) / light->fade;
3483 /* solve for inverse square falloff */
3485 light->envelope = sqrt( intensity / light->falloffTolerance ) + radius;
3488 add = light->photons / (dist * dist);
3489 T = light->photons / (dist * dist);
3490 T * (dist * dist) = light->photons;
3491 dist = sqrt( light->photons / T );
3495 /* chop radius against pvs */
3498 ClearBounds( mins, maxs );
3500 /* check all leaves */
3501 for( i = 0; i < numBSPLeafs; i++ )
3504 leaf = &bspLeafs[ i ];
3507 if( leaf->cluster < 0 )
3509 if( ClusterVisible( light->cluster, leaf->cluster ) == qfalse ) /* ydnar: thanks Arnout for exposing my stupid error (this never failed before) */
3512 /* add this leafs bbox to the bounds */
3513 VectorCopy( leaf->mins, origin );
3514 AddPointToBounds( origin, mins, maxs );
3515 VectorCopy( leaf->maxs, origin );
3516 AddPointToBounds( origin, mins, maxs );
3519 /* test to see if bounds encompass light */
3520 for( i = 0; i < 3; i++ )
3522 if( mins[ i ] > light->origin[ i ] || maxs[ i ] < light->origin[ i ] )
3524 //% Sys_Printf( "WARNING: Light PVS bounds (%.0f, %.0f, %.0f) -> (%.0f, %.0f, %.0f)\ndo not encompass light %d (%f, %f, %f)\n",
3525 //% mins[ 0 ], mins[ 1 ], mins[ 2 ],
3526 //% maxs[ 0 ], maxs[ 1 ], maxs[ 2 ],
3527 //% numLights, light->origin[ 0 ], light->origin[ 1 ], light->origin[ 2 ] );
3528 AddPointToBounds( light->origin, mins, maxs );
3532 /* chop the bounds by a plane for area lights and spotlights */
3533 if( light->type == EMIT_AREA || light->type == EMIT_SPOT )
3534 ChopBounds( mins, maxs, light->origin, light->normal );
3537 VectorCopy( mins, light->mins );
3538 VectorCopy( maxs, light->maxs );
3540 /* reflect bounds around light origin */
3541 //% VectorMA( light->origin, -1.0f, origin, origin );
3542 VectorScale( light->origin, 2, origin );
3543 VectorSubtract( origin, maxs, origin );
3544 AddPointToBounds( origin, mins, maxs );
3545 //% VectorMA( light->origin, -1.0f, mins, origin );
3546 VectorScale( light->origin, 2, origin );
3547 VectorSubtract( origin, mins, origin );
3548 AddPointToBounds( origin, mins, maxs );
3550 /* calculate spherical bounds */
3551 VectorSubtract( maxs, light->origin, dir );
3552 radius = (float) VectorLength( dir );
3554 /* if this radius is smaller than the envelope, then set the envelope to it */
3555 if( radius < light->envelope )
3557 light->envelope = radius;
3558 //% Sys_FPrintf( SYS_VRB, "PVS Cull (%d): culled\n", numLights );
3561 //% Sys_FPrintf( SYS_VRB, "PVS Cull (%d): failed (%8.0f > %8.0f)\n", numLights, radius, light->envelope );
3564 /* add grid/surface only check */
3567 if( !(light->flags & LIGHT_GRID) )
3568 light->envelope = 0.0f;
3572 if( !(light->flags & LIGHT_SURFACES) )
3573 light->envelope = 0.0f;
3578 if( light->cluster < 0 || light->envelope <= 0.0f )
3581 //% Sys_Printf( "Culling light: Cluster: %d Envelope: %f\n", light->cluster, light->envelope );
3583 /* delete the light */
3585 *owner = light->next;
3586 if( light->w != NULL )
3593 /* square envelope */
3594 light->envelope2 = (light->envelope * light->envelope);
3596 /* increment light count */
3599 /* set next light */
3600 owner = &((**owner).next);
3603 /* bucket sort lights by style */
3604 memset( buckets, 0, sizeof( buckets ) );
3606 for( light = lights; light != NULL; light = light2 )
3608 /* get next light */
3609 light2 = light->next;
3611 /* filter into correct bucket */
3612 light->next = buckets[ light->style ];
3613 buckets[ light->style ] = light;
3615 /* if any styled light is present, automatically set nocollapse */
3616 if( light->style != LS_NORMAL )
3620 /* filter back into light list */
3622 for( i = 255; i >= 0; i-- )
3625 for( light = buckets[ i ]; light != NULL; light = light2 )
3627 light2 = light->next;
3628 light->next = lights;
3633 /* emit some statistics */
3634 Sys_Printf( "%9d total lights\n", numLights );
3635 Sys_Printf( "%9d culled lights\n", numCulledLights );
3641 CreateTraceLightsForBounds()
3642 creates a list of lights that affect the given bounding box and pvs clusters (bsp leaves)
3645 void CreateTraceLightsForBounds( vec3_t mins, vec3_t maxs, vec3_t normal, int numClusters, int *clusters, int flags, trace_t *trace )
3649 vec3_t origin, dir, nullVector = { 0.0f, 0.0f, 0.0f };
3650 float radius, dist, length;
3653 /* potential pre-setup */
3654 if( numLights == 0 )
3655 SetupEnvelopes( qfalse, fast );
3658 //% 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 ] );
3660 /* allocate the light list */
3661 trace->lights = safe_malloc( sizeof( light_t* ) * (numLights + 1) );
3662 trace->numLights = 0;
3664 /* calculate spherical bounds */
3665 VectorAdd( mins, maxs, origin );
3666 VectorScale( origin, 0.5f, origin );
3667 VectorSubtract( maxs, origin, dir );
3668 radius = (float) VectorLength( dir );
3670 /* get length of normal vector */
3671 if( normal != NULL )
3672 length = VectorLength( normal );
3675 normal = nullVector;
3679 /* test each light and see if it reaches the sphere */
3680 /* note: the attenuation code MUST match LightingAtSample() */
3681 for( light = lights; light; light = light->next )
3683 /* check zero sized envelope */
3684 if( light->envelope <= 0 )
3686 lightsEnvelopeCulled++;
3691 if( !(light->flags & flags) )
3694 /* sunlight skips all this nonsense */
3695 if( light->type != EMIT_SUN )
3701 /* check against pvs cluster */
3702 if( numClusters > 0 && clusters != NULL )
3704 for( i = 0; i < numClusters; i++ )
3706 if( ClusterVisible( light->cluster, clusters[ i ] ) )
3711 if( i == numClusters )
3713 lightsClusterCulled++;
3718 /* if the light's bounding sphere intersects with the bounding sphere then this light needs to be tested */
3719 VectorSubtract( light->origin, origin, dir );
3720 dist = VectorLength( dir );
3721 dist -= light->envelope;
3725 lightsEnvelopeCulled++;
3729 /* check bounding box against light's pvs envelope (note: this code never eliminated any lights, so disabling it) */
3732 for( i = 0; i < 3; i++ )
3734 if( mins[ i ] > light->maxs[ i ] || maxs[ i ] < light->mins[ i ] )
3739 lightsBoundsCulled++;
3745 /* planar surfaces (except twosided surfaces) have a couple more checks */
3746 if( length > 0.0f && trace->twoSided == qfalse )
3748 /* lights coplanar with a surface won't light it */
3749 if( !(light->flags & LIGHT_TWOSIDED) && DotProduct( light->normal, normal ) > 0.999f )
3751 lightsPlaneCulled++;
3755 /* check to see if light is behind the plane */
3756 if( DotProduct( light->origin, normal ) - DotProduct( origin, normal ) < -1.0f )
3758 lightsPlaneCulled++;
3763 /* add this light */
3764 trace->lights[ trace->numLights++ ] = light;
3767 /* make last night null */
3768 trace->lights[ trace->numLights ] = NULL;
3773 void FreeTraceLights( trace_t *trace )
3775 if( trace->lights != NULL )
3776 free( trace->lights );
3782 CreateTraceLightsForSurface()
3783 creates a list of lights that can potentially affect a drawsurface
3786 void CreateTraceLightsForSurface( int num, trace_t *trace )
3789 vec3_t mins, maxs, normal;
3791 bspDrawSurface_t *ds;
3792 surfaceInfo_t *info;
3799 /* get drawsurface and info */
3800 ds = &bspDrawSurfaces[ num ];
3801 info = &surfaceInfos[ num ];
3803 /* get the mins/maxs for the dsurf */
3804 ClearBounds( mins, maxs );
3805 VectorCopy( bspDrawVerts[ ds->firstVert ].normal, normal );
3806 for( i = 0; i < ds->numVerts; i++ )
3808 dv = &yDrawVerts[ ds->firstVert + i ];
3809 AddPointToBounds( dv->xyz, mins, maxs );
3810 if( !VectorCompare( dv->normal, normal ) )
3811 VectorClear( normal );
3814 /* create the lights for the bounding box */
3815 CreateTraceLightsForBounds( mins, maxs, normal, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ], LIGHT_SURFACES, trace );
3818 /////////////////////////////////////////////////////////////
3820 #define FLOODLIGHT_CONE_ANGLE 88 /* degrees */
3821 #define FLOODLIGHT_NUM_ANGLE_STEPS 16
3822 #define FLOODLIGHT_NUM_ELEVATION_STEPS 4
3823 #define FLOODLIGHT_NUM_VECTORS (FLOODLIGHT_NUM_ANGLE_STEPS * FLOODLIGHT_NUM_ELEVATION_STEPS)
3825 static vec3_t floodVectors[ FLOODLIGHT_NUM_VECTORS ];
3826 static int numFloodVectors = 0;
3828 void SetupFloodLight( void )
3831 float angle, elevation, angleStep, elevationStep;
3833 double v1,v2,v3,v4,v5;
3836 Sys_FPrintf( SYS_VRB, "--- SetupFloodLight ---\n" );
3838 /* calculate angular steps */
3839 angleStep = DEG2RAD( 360.0f / FLOODLIGHT_NUM_ANGLE_STEPS );
3840 elevationStep = DEG2RAD( FLOODLIGHT_CONE_ANGLE / FLOODLIGHT_NUM_ELEVATION_STEPS );
3844 for( i = 0, angle = 0.0f; i < FLOODLIGHT_NUM_ANGLE_STEPS; i++, angle += angleStep )
3846 /* iterate elevation */
3847 for( j = 0, elevation = elevationStep * 0.5f; j < FLOODLIGHT_NUM_ELEVATION_STEPS; j++, elevation += elevationStep )
3849 floodVectors[ numFloodVectors ][ 0 ] = sin( elevation ) * cos( angle );
3850 floodVectors[ numFloodVectors ][ 1 ] = sin( elevation ) * sin( angle );
3851 floodVectors[ numFloodVectors ][ 2 ] = cos( elevation );
3856 /* emit some statistics */
3857 Sys_FPrintf( SYS_VRB, "%9d numFloodVectors\n", numFloodVectors );
3860 value = ValueForKey( &entities[ 0 ], "_floodlight" );
3862 if( value[ 0 ] != '\0' )
3865 v4=floodlightDistance;
3866 v5=floodlightIntensity;
3868 sscanf( value, "%lf %lf %lf %lf %lf", &v1, &v2, &v3, &v4, &v5);
3870 floodlightRGB[0]=v1;
3871 floodlightRGB[1]=v2;
3872 floodlightRGB[2]=v3;
3874 if (VectorLength(floodlightRGB)==0)
3876 VectorSet(floodlightRGB,240,240,255);
3882 floodlightDistance=v4;
3883 floodlightIntensity=v5;
3885 floodlighty = qtrue;
3886 Sys_Printf( "FloodLighting enabled via worldspawn _floodlight key.\n" );
3890 VectorSet(floodlightRGB,240,240,255);
3891 //floodlighty = qtrue;
3892 //Sys_Printf( "FloodLighting enabled via worldspawn _floodlight key.\n" );
3894 VectorNormalize(floodlightRGB,floodlightRGB);
3897 //27 - lighttracer style ambient occlusion light hack.
3898 //Kudos to the dirtmapping author for most of this source.
3899 void FloodLightRawLightmap( int rawLightmapNum )
3901 int i, x, y, sx, sy, *cluster;
3902 float *origin, *normal, *floodlight, *floodlight2, average, samples;
3904 surfaceInfo_t *info;
3907 /* bail if this number exceeds the number of raw lightmaps */
3908 if( rawLightmapNum >= numRawLightmaps )
3912 lm = &rawLightmaps[ rawLightmapNum ];
3914 memset(&trace,0,sizeof(trace_t));
3916 trace.testOcclusion = qtrue;
3917 trace.forceSunlight = qfalse;
3918 trace.twoSided = qtrue;
3919 trace.recvShadows = lm->recvShadows;
3920 trace.numSurfaces = lm->numLightSurfaces;
3921 trace.surfaces = &lightSurfaces[ lm->firstLightSurface ];
3922 trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
3923 trace.testAll = qfalse;
3924 trace.distance = 1024;
3926 /* twosided lighting (may or may not be a good idea for lightmapped stuff) */
3927 //trace.twoSided = qfalse;
3928 for( i = 0; i < trace.numSurfaces; i++ )
3931 info = &surfaceInfos[ trace.surfaces[ i ] ];
3933 /* check twosidedness */
3934 if( info->si->twoSided )
3936 trace.twoSided = qtrue;
3942 for( y = 0; y < lm->sh; y++ )
3944 for( x = 0; x < lm->sw; x++ )
3947 cluster = SUPER_CLUSTER( x, y );
3948 origin = SUPER_ORIGIN( x, y );
3949 normal = SUPER_NORMAL( x, y );
3950 floodlight = SUPER_FLOODLIGHT( x, y );
3952 /* set default dirt */
3955 /* only look at mapped luxels */
3960 trace.cluster = *cluster;
3961 VectorCopy( origin, trace.origin );
3962 VectorCopy( normal, trace.normal );
3967 *floodlight = FloodLightForSample( &trace );
3971 /* testing no filtering */
3975 for( y = 0; y < lm->sh; y++ )
3977 for( x = 0; x < lm->sw; x++ )
3980 cluster = SUPER_CLUSTER( x, y );
3981 floodlight = SUPER_FLOODLIGHT( x, y );
3983 /* filter dirt by adjacency to unmapped luxels */
3984 average = *floodlight;
3986 for( sy = (y - 1); sy <= (y + 1); sy++ )
3988 if( sy < 0 || sy >= lm->sh )
3991 for( sx = (x - 1); sx <= (x + 1); sx++ )
3993 if( sx < 0 || sx >= lm->sw || (sx == x && sy == y) )
3996 /* get neighboring luxel */
3997 cluster = SUPER_CLUSTER( sx, sy );
3998 floodlight2 = SUPER_FLOODLIGHT( sx, sy );
3999 if( *cluster < 0 || *floodlight2 <= 0.0f )
4003 average += *floodlight2;
4008 if( samples <= 0.0f )
4013 if( samples <= 0.0f )
4017 *floodlight = average / samples;
4023 FloodLightForSample()
4024 calculates floodlight value for a given sample
4025 once again, kudos to the dirtmapping coder
4027 float FloodLightForSample( trace_t *trace )
4033 float gatherLight, outLight;
4034 vec3_t normal, worldUp, myUp, myRt, direction, displacement;
4042 if( trace == NULL || trace->cluster < 0 )
4047 dd = floodlightDistance;
4048 VectorCopy( trace->normal, normal );
4050 /* check if the normal is aligned to the world-up */
4051 if( normal[ 0 ] == 0.0f && normal[ 1 ] == 0.0f )
4053 if( normal[ 2 ] == 1.0f )
4055 VectorSet( myRt, 1.0f, 0.0f, 0.0f );
4056 VectorSet( myUp, 0.0f, 1.0f, 0.0f );
4058 else if( normal[ 2 ] == -1.0f )
4060 VectorSet( myRt, -1.0f, 0.0f, 0.0f );
4061 VectorSet( myUp, 0.0f, 1.0f, 0.0f );
4066 VectorSet( worldUp, 0.0f, 0.0f, 1.0f );
4067 CrossProduct( normal, worldUp, myRt );
4068 VectorNormalize( myRt, myRt );
4069 CrossProduct( myRt, normal, myUp );
4070 VectorNormalize( myUp, myUp );
4073 /* iterate through ordered vectors */
4074 for( i = 0; i < numFloodVectors; i++ )
4076 if (floodlight_lowquality==qtrue)
4078 if (rand()%10 != 0 ) continue;
4083 /* transform vector into tangent space */
4084 direction[ 0 ] = myRt[ 0 ] * floodVectors[ i ][ 0 ] + myUp[ 0 ] * floodVectors[ i ][ 1 ] + normal[ 0 ] * floodVectors[ i ][ 2 ];
4085 direction[ 1 ] = myRt[ 1 ] * floodVectors[ i ][ 0 ] + myUp[ 1 ] * floodVectors[ i ][ 1 ] + normal[ 1 ] * floodVectors[ i ][ 2 ];
4086 direction[ 2 ] = myRt[ 2 ] * floodVectors[ i ][ 0 ] + myUp[ 2 ] * floodVectors[ i ][ 1 ] + normal[ 2 ] * floodVectors[ i ][ 2 ];
4089 VectorMA( trace->origin, dd, direction, trace->end );
4091 //VectorMA( trace->origin, 1, direction, trace->origin );
4093 SetupTrace( trace );
4098 if (trace->compileFlags & C_SKY )
4102 else if ( trace->opaque )
4104 VectorSubtract( trace->hit, trace->origin, displacement );
4105 d=VectorLength( displacement );
4107 // d=trace->distance;
4108 //if (d>256) gatherDirt+=1;
4110 if (contribution>1) contribution=1.0f;
4112 //gatherDirt += 1.0f - ooDepth * VectorLength( displacement );
4115 gatherLight+=contribution;
4119 if( gatherLight <= 0.0f )
4127 outLight=gatherLight;
4128 if( outLight > 1.0f )
4131 /* return to sender */