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