]> icculus.org git repositories - divverent/netradiant.git/blob - tools/quake3/q3map2/light_ydnar.c
fix deluxemap when lit from the wrong side as good as possible (by ignoring the delux...
[divverent/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 )
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         
1550         
1551         /* bail if this number exceeds the number of raw lightmaps */
1552         if( rawLightmapNum >= numRawLightmaps )
1553                 return;
1554         
1555         /* get lightmap */
1556         lm = &rawLightmaps[ rawLightmapNum ];
1557         
1558         /* setup trace */
1559         trace.testOcclusion = qtrue;
1560         trace.forceSunlight = qfalse;
1561         trace.recvShadows = lm->recvShadows;
1562         trace.numSurfaces = lm->numLightSurfaces;
1563         trace.surfaces = &lightSurfaces[ lm->firstLightSurface ];
1564         trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
1565         trace.testAll = qfalse;
1566         
1567         /* twosided lighting (may or may not be a good idea for lightmapped stuff) */
1568         trace.twoSided = qfalse;
1569         for( i = 0; i < trace.numSurfaces; i++ )
1570         {
1571                 /* get surface */
1572                 info = &surfaceInfos[ trace.surfaces[ i ] ];
1573                 
1574                 /* check twosidedness */
1575                 if( info->si->twoSided )
1576                 {
1577                         trace.twoSided = qtrue;
1578                         break;
1579                 }
1580         }
1581         
1582         /* gather dirt */
1583         for( y = 0; y < lm->sh; y++ )
1584         {
1585                 for( x = 0; x < lm->sw; x++ )
1586                 {
1587                         /* get luxel */
1588                         cluster = SUPER_CLUSTER( x, y );
1589                         origin = SUPER_ORIGIN( x, y );
1590                         normal = SUPER_NORMAL( x, y );
1591                         dirt = SUPER_DIRT( x, y );
1592                         
1593                         /* set default dirt */
1594                         *dirt = 0.0f;
1595                         
1596                         /* only look at mapped luxels */
1597                         if( *cluster < 0 )
1598                                 continue;
1599                         
1600                         /* copy to trace */
1601                         trace.cluster = *cluster;
1602                         VectorCopy( origin, trace.origin );
1603                         VectorCopy( normal, trace.normal );
1604                         
1605                         /* get dirt */
1606                         *dirt = DirtForSample( &trace );
1607                 }
1608         }
1609         
1610         /* testing no filtering */
1611         //%     return;
1612         
1613         /* filter dirt */
1614         for( y = 0; y < lm->sh; y++ )
1615         {
1616                 for( x = 0; x < lm->sw; x++ )
1617                 {
1618                         /* get luxel */
1619                         cluster = SUPER_CLUSTER( x, y );
1620                         dirt = SUPER_DIRT( x, y );
1621                         
1622                         /* filter dirt by adjacency to unmapped luxels */
1623                         average = *dirt;
1624                         samples = 1.0f;
1625                         for( sy = (y - 1); sy <= (y + 1); sy++ )
1626                         {
1627                                 if( sy < 0 || sy >= lm->sh )
1628                                         continue;
1629                                 
1630                                 for( sx = (x - 1); sx <= (x + 1); sx++ )
1631                                 {
1632                                         if( sx < 0 || sx >= lm->sw || (sx == x && sy == y) )
1633                                                 continue;
1634                                         
1635                                         /* get neighboring luxel */
1636                                         cluster = SUPER_CLUSTER( sx, sy );
1637                                         dirt2 = SUPER_DIRT( sx, sy );
1638                                         if( *cluster < 0 || *dirt2 <= 0.0f )
1639                                                 continue;
1640                                         
1641                                         /* add it */
1642                                         average += *dirt2;
1643                                         samples += 1.0f;
1644                                 }
1645                                 
1646                                 /* bail */
1647                                 if( samples <= 0.0f )
1648                                         break;
1649                         }
1650                         
1651                         /* bail */
1652                         if( samples <= 0.0f )
1653                                 continue;
1654                         
1655                         /* scale dirt */
1656                         *dirt = average / samples;
1657                 }
1658         }
1659 }
1660
1661
1662
1663 /*
1664 SubmapRawLuxel()
1665 calculates the pvs cluster, origin, normal of a sub-luxel
1666 */
1667
1668 static qboolean SubmapRawLuxel( rawLightmap_t *lm, int x, int y, float bx, float by, int *sampleCluster, vec3_t sampleOrigin, vec3_t sampleNormal )
1669 {
1670         int                     i, *cluster, *cluster2;
1671         float           *origin, *origin2, *normal;     //%     , *normal2;
1672         vec3_t          originVecs[ 2 ];                        //%     , normalVecs[ 2 ];
1673         
1674         
1675         /* calulate x vector */
1676         if( (x < (lm->sw - 1) && bx >= 0.0f) || (x == 0 && bx <= 0.0f) )
1677         {
1678                 cluster = SUPER_CLUSTER( x, y );
1679                 origin = SUPER_ORIGIN( x, y );
1680                 //%     normal = SUPER_NORMAL( x, y );
1681                 cluster2 = SUPER_CLUSTER( x + 1, y );
1682                 origin2 = *cluster2 < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x + 1, y );
1683                 //%     normal2 = *cluster2 < 0 ? SUPER_NORMAL( x, y ) : SUPER_NORMAL( x + 1, y );
1684         }
1685         else if( (x > 0 && bx <= 0.0f) || (x == (lm->sw - 1) && bx >= 0.0f) )
1686         {
1687                 cluster = SUPER_CLUSTER( x - 1, y );
1688                 origin = *cluster < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x - 1, y );
1689                 //%     normal = *cluster < 0 ? SUPER_NORMAL( x, y ) : SUPER_NORMAL( x - 1, y );
1690                 cluster2 = SUPER_CLUSTER( x, y );
1691                 origin2 = SUPER_ORIGIN( x, y );
1692                 //%     normal2 = SUPER_NORMAL( x, y );
1693         }
1694         else
1695                 Sys_Printf( "WARNING: Spurious lightmap S vector\n" );
1696         
1697         VectorSubtract( origin2, origin, originVecs[ 0 ] );
1698         //%     VectorSubtract( normal2, normal, normalVecs[ 0 ] );
1699         
1700         /* calulate y vector */
1701         if( (y < (lm->sh - 1) && bx >= 0.0f) || (y == 0 && bx <= 0.0f) )
1702         {
1703                 cluster = SUPER_CLUSTER( x, y );
1704                 origin = SUPER_ORIGIN( x, y );
1705                 //%     normal = SUPER_NORMAL( x, y );
1706                 cluster2 = SUPER_CLUSTER( x, y + 1 );
1707                 origin2 = *cluster2 < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x, y + 1 );
1708                 //%     normal2 = *cluster2 < 0 ? SUPER_NORMAL( x, y ) : SUPER_NORMAL( x, y + 1 );
1709         }
1710         else if( (y > 0 && bx <= 0.0f) || (y == (lm->sh - 1) && bx >= 0.0f) )
1711         {
1712                 cluster = SUPER_CLUSTER( x, y - 1 );
1713                 origin = *cluster < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x, y - 1 );
1714                 //%     normal = *cluster < 0 ? SUPER_NORMAL( x, y ) : SUPER_NORMAL( x, y - 1 );
1715                 cluster2 = SUPER_CLUSTER( x, y );
1716                 origin2 = SUPER_ORIGIN( x, y );
1717                 //%     normal2 = SUPER_NORMAL( x, y );
1718         }
1719         else
1720                 Sys_Printf( "WARNING: Spurious lightmap T vector\n" );
1721         
1722         VectorSubtract( origin2, origin, originVecs[ 1 ] );
1723         //%     VectorSubtract( normal2, normal, normalVecs[ 1 ] );
1724         
1725         /* calculate new origin */
1726         //%     VectorMA( origin, bx, originVecs[ 0 ], sampleOrigin );
1727         //%     VectorMA( sampleOrigin, by, originVecs[ 1 ], sampleOrigin );
1728         for( i = 0; i < 3; i++ )
1729                 sampleOrigin[ i ] = sampleOrigin[ i ] + (bx * originVecs[ 0 ][ i ]) + (by * originVecs[ 1 ][ i ]);
1730         
1731         /* get cluster */
1732         *sampleCluster = ClusterForPointExtFilter( sampleOrigin, (LUXEL_EPSILON * 2), lm->numLightClusters, lm->lightClusters );
1733         if( *sampleCluster < 0 )
1734                 return qfalse;
1735         
1736         /* calculate new normal */
1737         //%     VectorMA( normal, bx, normalVecs[ 0 ], sampleNormal );
1738         //%     VectorMA( sampleNormal, by, normalVecs[ 1 ], sampleNormal );
1739         //%     if( VectorNormalize( sampleNormal, sampleNormal ) <= 0.0f )
1740         //%             return qfalse;
1741         normal = SUPER_NORMAL( x, y );
1742         VectorCopy( normal, sampleNormal );
1743         
1744         /* return ok */
1745         return qtrue;
1746 }
1747
1748
1749 /*
1750 SubsampleRawLuxel_r()
1751 recursively subsamples a luxel until its color gradient is low enough or subsampling limit is reached
1752 */
1753
1754 static void SubsampleRawLuxel_r( rawLightmap_t *lm, trace_t *trace, vec3_t sampleOrigin, int x, int y, float bias, float *lightLuxel )
1755 {
1756         int                     b, samples, mapped, lighted;
1757         int                     cluster[ 4 ];
1758         vec4_t          luxel[ 4 ];
1759         vec3_t          origin[ 4 ], normal[ 4 ];
1760         float           biasDirs[ 4 ][ 2 ] = { { -1.0f, -1.0f }, { 1.0f, -1.0f }, { -1.0f, 1.0f }, { 1.0f, 1.0f } };
1761         vec3_t          color, total;
1762         
1763         
1764         /* limit check */
1765         if( lightLuxel[ 3 ] >= lightSamples )
1766                 return;
1767         
1768         /* setup */
1769         VectorClear( total );
1770         mapped = 0;
1771         lighted = 0;
1772         
1773         /* make 2x2 subsample stamp */
1774         for( b = 0; b < 4; b++ )
1775         {
1776                 /* set origin */
1777                 VectorCopy( sampleOrigin, origin[ b ] );
1778                 
1779                 /* calculate position */
1780                 if( !SubmapRawLuxel( lm, x, y, (bias * biasDirs[ b ][ 0 ]), (bias * biasDirs[ b ][ 1 ]), &cluster[ b ], origin[ b ], normal[ b ] ) )
1781                 {
1782                         cluster[ b ] = -1;
1783                         continue;
1784                 }
1785                 mapped++;
1786                 
1787                 /* increment sample count */
1788                 luxel[ b ][ 3 ] = lightLuxel[ 3 ] + 1.0f;
1789                 
1790                 /* setup trace */
1791                 trace->cluster = *cluster;
1792                 VectorCopy( origin[ b ], trace->origin );
1793                 VectorCopy( normal[ b ], trace->normal );
1794                 
1795                 /* sample light */
1796
1797                 LightContributionToSample( trace );
1798                 
1799                 /* add to totals (fixme: make contrast function) */
1800                 VectorCopy( trace->color, luxel[ b ] );
1801                 VectorAdd( total, trace->color, total );
1802                 if( (luxel[ b ][ 0 ] + luxel[ b ][ 1 ] + luxel[ b ][ 2 ]) > 0.0f )
1803                         lighted++;
1804         }
1805         
1806         /* subsample further? */
1807         if( (lightLuxel[ 3 ] + 1.0f) < lightSamples &&
1808                 (total[ 0 ] > 4.0f || total[ 1 ] > 4.0f || total[ 2 ] > 4.0f) &&
1809                 lighted != 0 && lighted != mapped )
1810         {
1811                 for( b = 0; b < 4; b++ )
1812                 {
1813                         if( cluster[ b ] < 0 )
1814                                 continue;
1815                         SubsampleRawLuxel_r( lm, trace, origin[ b ], x, y, (bias * 0.25f), luxel[ b ] );
1816                 }
1817         }
1818         
1819         /* average */
1820         //%     VectorClear( color );
1821         //%     samples = 0;
1822         VectorCopy( lightLuxel, color );
1823         samples = 1;
1824         for( b = 0; b < 4; b++ )
1825         {
1826                 if( cluster[ b ] < 0 )
1827                         continue;
1828                 VectorAdd( color, luxel[ b ], color );
1829                 samples++;
1830         }
1831         
1832         /* add to luxel */
1833         if( samples > 0 )
1834         {
1835                 /* average */
1836                 color[ 0 ] /= samples;
1837                 color[ 1 ] /= samples;
1838                 color[ 2 ] /= samples;
1839                 
1840                 /* add to color */
1841                 VectorCopy( color, lightLuxel );
1842                 lightLuxel[ 3 ] += 1.0f;
1843         }
1844 }
1845
1846
1847
1848 /*
1849 IlluminateRawLightmap()
1850 illuminates the luxels
1851 */
1852
1853 #define STACK_LL_SIZE                   (SUPER_LUXEL_SIZE * 64 * 64)
1854 #define LIGHT_LUXEL( x, y )             (lightLuxels + ((((y) * lm->sw) + (x)) * SUPER_LUXEL_SIZE))
1855
1856 void IlluminateRawLightmap( int rawLightmapNum )
1857 {
1858         int                                     i, t, x, y, sx, sy, size, llSize, luxelFilterRadius, lightmapNum;
1859         int                                     *cluster, *cluster2, mapped, lighted, totalLighted;
1860         rawLightmap_t           *lm;
1861         surfaceInfo_t           *info;
1862         qboolean                        filterColor, filterDir;
1863         float                           brightness;
1864         float                           *origin, *normal, *dirt, *luxel, *luxel2, *deluxel, *deluxel2;
1865         float                           *lightLuxels, *lightLuxel, samples, filterRadius, weight;
1866         vec3_t                          color, averageColor, averageDir, total, temp, temp2;
1867         float                           tests[ 4 ][ 2 ] = { { 0.0f, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } };
1868         trace_t                         trace;
1869         float                           stackLightLuxels[ STACK_LL_SIZE ];
1870         vec3_t                          flood;
1871         float                           *floodlight;
1872         
1873         
1874         /* bail if this number exceeds the number of raw lightmaps */
1875         if( rawLightmapNum >= numRawLightmaps )
1876                 return;
1877         
1878         /* get lightmap */
1879         lm = &rawLightmaps[ rawLightmapNum ];
1880         
1881         /* setup trace */
1882         trace.testOcclusion = !noTrace;
1883         trace.forceSunlight = qfalse;
1884         trace.recvShadows = lm->recvShadows;
1885         trace.numSurfaces = lm->numLightSurfaces;
1886         trace.surfaces = &lightSurfaces[ lm->firstLightSurface ];
1887         trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
1888         
1889         /* twosided lighting (may or may not be a good idea for lightmapped stuff) */
1890         trace.twoSided = qfalse;
1891         for( i = 0; i < trace.numSurfaces; i++ )
1892         {
1893                 /* get surface */
1894                 info = &surfaceInfos[ trace.surfaces[ i ] ];
1895                 
1896                 /* check twosidedness */
1897                 if( info->si->twoSided )
1898                 {
1899                         trace.twoSided = qtrue;
1900                         break;
1901                 }
1902         }
1903         
1904         /* create a culled light list for this raw lightmap */
1905         CreateTraceLightsForBounds( lm->mins, lm->maxs, lm->plane, lm->numLightClusters, lm->lightClusters, LIGHT_SURFACES, &trace );
1906         
1907         /* -----------------------------------------------------------------
1908            fill pass
1909            ----------------------------------------------------------------- */
1910         
1911         /* set counts */
1912         numLuxelsIlluminated += (lm->sw * lm->sh);
1913         
1914         /* test debugging state */
1915         if( debugSurfaces || debugAxis || debugCluster || debugOrigin || dirtDebug || normalmap )
1916         {
1917                 /* debug fill the luxels */
1918                 for( y = 0; y < lm->sh; y++ )
1919                 {
1920                         for( x = 0; x < lm->sw; x++ )
1921                         {
1922                                 /* get cluster */
1923                                 cluster = SUPER_CLUSTER( x, y );
1924
1925                                 /* only fill mapped luxels */
1926                                 if( *cluster < 0 )
1927                                         continue;
1928                                 
1929                                 /* get particulars */
1930                                 luxel = SUPER_LUXEL( 0, x, y );
1931                                 origin = SUPER_ORIGIN( x, y );
1932                                 normal = SUPER_NORMAL( x, y );
1933                                 
1934                                 /* color the luxel with raw lightmap num? */
1935                                 if( debugSurfaces )
1936                                         VectorCopy( debugColors[ rawLightmapNum % 12 ], luxel );
1937                                 
1938                                 /* color the luxel with lightmap axis? */
1939                                 else if( debugAxis )
1940                                 {
1941                                         luxel[ 0 ] = (lm->axis[ 0 ] + 1.0f) * 127.5f;
1942                                         luxel[ 1 ] = (lm->axis[ 1 ] + 1.0f) * 127.5f;
1943                                         luxel[ 2 ] = (lm->axis[ 2 ] + 1.0f) * 127.5f;
1944                                 }
1945                                 
1946                                 /* color the luxel with luxel cluster? */
1947                                 else if( debugCluster )
1948                                         VectorCopy( debugColors[ *cluster % 12 ], luxel );
1949                                 
1950                                 /* color the luxel with luxel origin? */
1951                                 else if( debugOrigin )
1952                                 {
1953                                         VectorSubtract( lm->maxs, lm->mins, temp );
1954                                         VectorScale( temp, (1.0f / 255.0f), temp );
1955                                         VectorSubtract( origin, lm->mins, temp2 );
1956                                         luxel[ 0 ] = lm->mins[ 0 ] + (temp[ 0 ] * temp2[ 0 ]);
1957                                         luxel[ 1 ] = lm->mins[ 1 ] + (temp[ 1 ] * temp2[ 1 ]);
1958                                         luxel[ 2 ] = lm->mins[ 2 ] + (temp[ 2 ] * temp2[ 2 ]);
1959                                 }
1960                                 
1961                                 /* color the luxel with the normal */
1962                                 else if( normalmap )
1963                                 {
1964                                         luxel[ 0 ] = (normal[ 0 ] + 1.0f) * 127.5f;
1965                                         luxel[ 1 ] = (normal[ 1 ] + 1.0f) * 127.5f;
1966                                         luxel[ 2 ] = (normal[ 2 ] + 1.0f) * 127.5f;
1967                                 }
1968                                 
1969                                 /* otherwise clear it */
1970                                 else
1971                                         VectorClear( luxel );
1972                                 
1973                                 /* add to counts */
1974                                 luxel[ 3 ] = 1.0f;
1975                         }
1976                 }
1977         }
1978         else
1979         {
1980                 /* allocate temporary per-light luxel storage */
1981                 llSize = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float );
1982                 if( llSize <= (STACK_LL_SIZE * sizeof( float )) )
1983                         lightLuxels = stackLightLuxels;
1984                 else
1985                         lightLuxels = safe_malloc( llSize );
1986                 
1987                 /* clear luxels */
1988                 //%     memset( lm->superLuxels[ 0 ], 0, llSize );
1989                 
1990                 /* set ambient color */
1991                 for( y = 0; y < lm->sh; y++ )
1992                 {
1993                         for( x = 0; x < lm->sw; x++ )
1994                         {
1995                                 /* get cluster */
1996                                 cluster = SUPER_CLUSTER( x, y );
1997                                 luxel = SUPER_LUXEL( 0, x, y );
1998                                 normal = SUPER_NORMAL( x, y );
1999                                 deluxel = SUPER_DELUXEL( x, y );
2000                                 
2001                                 /* blacken unmapped clusters */
2002                                 if( *cluster < 0 )
2003                                         VectorClear( luxel );
2004                                 
2005                                 /* set ambient */
2006                                 else
2007                                 {
2008                                         VectorCopy( ambientColor, luxel );
2009                                         if( deluxemap )
2010                                         {
2011                                                 brightness = ambientColor[ 0 ] * 0.3f + ambientColor[ 1 ] * 0.59f + ambientColor[ 2 ] * 0.11f;
2012                                                 brightness *= (1.0 / 255.0);
2013                                                 VectorScale( normal, brightness, deluxel );
2014                                         }
2015                                         luxel[ 3 ] = 1.0f;
2016                                 }
2017                         }
2018                 }
2019                 
2020                 /* clear styled lightmaps */
2021                 size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float );
2022                 for( lightmapNum = 1; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2023                 {
2024                         if( lm->superLuxels[ lightmapNum ] != NULL )
2025                                 memset( lm->superLuxels[ lightmapNum ], 0, size );
2026                 }
2027                 
2028                 /* debugging code */
2029                 //%     if( trace.numLights <= 0 )
2030                 //%             Sys_Printf( "Lightmap %9d: 0 lights, axis: %.2f, %.2f, %.2f\n", rawLightmapNum, lm->axis[ 0 ], lm->axis[ 1 ], lm->axis[ 2 ] );
2031                 
2032                 /* walk light list */
2033                 for( i = 0; i < trace.numLights; i++ )
2034                 {
2035                         /* setup trace */
2036                         trace.light = trace.lights[ i ];
2037                         
2038                         /* style check */
2039                         for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2040                         {
2041                                 if( lm->styles[ lightmapNum ] == trace.light->style ||
2042                                         lm->styles[ lightmapNum ] == LS_NONE )
2043                                         break;
2044                         }
2045                         
2046                         /* max of MAX_LIGHTMAPS (4) styles allowed to hit a surface/lightmap */
2047                         if( lightmapNum >= MAX_LIGHTMAPS )
2048                         {
2049                                 Sys_Printf( "WARNING: Hit per-surface style limit (%d)\n", MAX_LIGHTMAPS );
2050                                 continue;
2051                         }
2052                         
2053                         /* setup */
2054                         memset( lightLuxels, 0, llSize );
2055                         totalLighted = 0;
2056                         
2057                         /* initial pass, one sample per luxel */
2058                         for( y = 0; y < lm->sh; y++ )
2059                         {
2060                                 for( x = 0; x < lm->sw; x++ )
2061                                 {
2062                                         /* get cluster */
2063                                         cluster = SUPER_CLUSTER( x, y );
2064                                         if( *cluster < 0 )
2065                                                 continue;
2066                                         
2067                                         /* get particulars */
2068                                         lightLuxel = LIGHT_LUXEL( x, y );
2069                                         deluxel = SUPER_DELUXEL( x, y );
2070                                         origin = SUPER_ORIGIN( x, y );
2071                                         normal = SUPER_NORMAL( x, y );
2072
2073 #if 0
2074                                         ////////// 27's temp hack for testing edge clipping ////
2075                                         if( origin[0]==0 && origin[1]==0 && origin[2]==0 )
2076                                         {
2077                                                 lightLuxel[ 1 ] = 255;
2078                                                 lightLuxel[ 3 ] = 1.0f;
2079                                                 totalLighted++;
2080                                         }
2081                                         else
2082 #endif
2083                                         {
2084                                                 /* set contribution count */
2085                                                 lightLuxel[ 3 ] = 1.0f;
2086
2087                                                 /* setup trace */
2088                                                 trace.cluster = *cluster;
2089                                                 VectorCopy( origin, trace.origin );
2090                                                 VectorCopy( normal, trace.normal );
2091
2092                                                 /* get light for this sample */
2093                                                 LightContributionToSample( &trace );
2094                                                 VectorCopy( trace.color, lightLuxel );
2095
2096                                                 /* add to count */
2097                                                 if( trace.color[ 0 ] || trace.color[ 1 ] || trace.color[ 2 ] )
2098                                                         totalLighted++;
2099                                         }
2100                                         
2101                                         /* add to light direction map (fixme: use luxel normal as starting point for deluxel?) */
2102                                         if( deluxemap )
2103                                         {
2104                                                 if(DotProduct(normal, trace.direction) > 0) // do not take light from the back side
2105                                                 {
2106                                                         /* color to grayscale (photoshop rgb weighting) */
2107                                                         brightness = trace.color[ 0 ] * 0.3f + trace.color[ 1 ] * 0.59f + trace.color[ 2 ] * 0.11f;
2108                                                         brightness *= (1.0 / 255.0);
2109                                                         VectorScale( trace.direction, brightness, trace.direction );
2110                                                         VectorAdd( deluxel, trace.direction, deluxel );
2111                                                 }
2112                                         }
2113                                 }
2114                         }
2115                         
2116                         /* don't even bother with everything else if nothing was lit */
2117                         if( totalLighted == 0 )
2118                                 continue;
2119                         
2120                         /* determine filter radius */
2121                         filterRadius = lm->filterRadius > trace.light->filterRadius
2122                                 ? lm->filterRadius
2123                                 : trace.light->filterRadius;
2124                         if( filterRadius < 0.0f )
2125                                 filterRadius = 0.0f;
2126                         
2127                         /* set luxel filter radius */
2128                         luxelFilterRadius = superSample * filterRadius / lm->sampleSize;
2129                         if( luxelFilterRadius == 0 && (filterRadius > 0.0f || filter) )
2130                                 luxelFilterRadius = 1;
2131                         
2132                         /* secondary pass, adaptive supersampling (fixme: use a contrast function to determine if subsampling is necessary) */
2133                         /* 2003-09-27: changed it so filtering disamples supersampling, as it would waste time */
2134                         if( lightSamples > 1 && luxelFilterRadius == 0 )
2135                         {
2136                                 /* walk luxels */
2137                                 for( y = 0; y < (lm->sh - 1); y++ )
2138                                 {
2139                                         for( x = 0; x < (lm->sw - 1); x++ )
2140                                         {
2141                                                 /* setup */
2142                                                 mapped = 0;
2143                                                 lighted = 0;
2144                                                 VectorClear( total );
2145                                                 
2146                                                 /* test 2x2 stamp */
2147                                                 for( t = 0; t < 4; t++ )
2148                                                 {
2149                                                         /* set sample coords */
2150                                                         sx = x + tests[ t ][ 0 ];
2151                                                         sy = y + tests[ t ][ 1 ];
2152                                                         
2153                                                         /* get cluster */
2154                                                         cluster = SUPER_CLUSTER( sx, sy );
2155                                                         if( *cluster < 0 )
2156                                                                 continue;
2157                                                         mapped++;
2158                                                         
2159                                                         /* get luxel */
2160                                                         lightLuxel = LIGHT_LUXEL( sx, sy );
2161                                                         VectorAdd( total, lightLuxel, total );
2162                                                         if( (lightLuxel[ 0 ] + lightLuxel[ 1 ] + lightLuxel[ 2 ]) > 0.0f )
2163                                                                 lighted++;
2164                                                 }
2165                                                 
2166                                                 /* if total color is under a certain amount, then don't bother subsampling */
2167                                                 if( total[ 0 ] <= 4.0f && total[ 1 ] <= 4.0f && total[ 2 ] <= 4.0f )
2168                                                         continue;
2169                                                 
2170                                                 /* if all 4 pixels are either in shadow or light, then don't subsample */
2171                                                 if( lighted != 0 && lighted != mapped )
2172                                                 {
2173                                                         for( t = 0; t < 4; t++ )
2174                                                         {
2175                                                                 /* set sample coords */
2176                                                                 sx = x + tests[ t ][ 0 ];
2177                                                                 sy = y + tests[ t ][ 1 ];
2178                                                                 
2179                                                                 /* get luxel */
2180                                                                 cluster = SUPER_CLUSTER( sx, sy );
2181                                                                 if( *cluster < 0 )
2182                                                                         continue;
2183                                                                 lightLuxel = LIGHT_LUXEL( sx, sy );
2184                                                                 origin = SUPER_ORIGIN( sx, sy );
2185                                                                 
2186                                                                 /* only subsample shadowed luxels */
2187                                                                 //%     if( (lightLuxel[ 0 ] + lightLuxel[ 1 ] + lightLuxel[ 2 ]) <= 0.0f )
2188                                                                 //%             continue;
2189                                                                 
2190                                                                 /* subsample it */
2191                                                                 SubsampleRawLuxel_r( lm, &trace, origin, sx, sy, 0.25f, lightLuxel );
2192                                                                 
2193                                                                 /* debug code to colorize subsampled areas to yellow */
2194                                                                 //%     luxel = SUPER_LUXEL( lightmapNum, sx, sy );
2195                                                                 //%     VectorSet( luxel, 255, 204, 0 );
2196                                                         }
2197                                                 }
2198                                         }
2199                                 }
2200                         }
2201                         
2202                         /* tertiary pass, apply dirt map (ambient occlusion) */
2203                         if( 0 && dirty )
2204                         {
2205                                 /* walk luxels */
2206                                 for( y = 0; y < lm->sh; y++ )
2207                                 {
2208                                         for( x = 0; x < lm->sw; x++ )
2209                                         {
2210                                                 /* get cluster  */
2211                                                 cluster = SUPER_CLUSTER( x, y );
2212                                                 if( *cluster < 0 )
2213                                                         continue;
2214                                                 
2215                                                 /* get particulars */
2216                                                 lightLuxel = LIGHT_LUXEL( x, y );
2217                                                 dirt = SUPER_DIRT( x, y );
2218                                                 
2219                                                 /* scale light value */
2220                                                 VectorScale( lightLuxel, *dirt, lightLuxel );
2221                                         }
2222                                 }
2223                         }
2224                         
2225                         /* allocate sampling lightmap storage */
2226                         if( lm->superLuxels[ lightmapNum ] == NULL )
2227                         {
2228                                 /* allocate sampling lightmap storage */
2229                                 size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float );
2230                                 lm->superLuxels[ lightmapNum ] = safe_malloc( size );
2231                                 memset( lm->superLuxels[ lightmapNum ], 0, size );
2232                         }
2233                         
2234                         /* set style */
2235                         if( lightmapNum > 0 )
2236                         {
2237                                 lm->styles[ lightmapNum ] = trace.light->style;
2238                                 //%     Sys_Printf( "Surface %6d has lightstyle %d\n", rawLightmapNum, trace.light->style );
2239                         }
2240                         
2241                         /* copy to permanent luxels */
2242                         for( y = 0; y < lm->sh; y++ )
2243                         {
2244                                 for( x = 0; x < lm->sw; x++ )
2245                                 {
2246                                         /* get cluster and origin */
2247                                         cluster = SUPER_CLUSTER( x, y );
2248                                         if( *cluster < 0 )
2249                                                 continue;
2250                                         origin = SUPER_ORIGIN( x, y );
2251                                         
2252                                         /* filter? */
2253                                         if( luxelFilterRadius )
2254                                         {
2255                                                 /* setup */
2256                                                 VectorClear( averageColor );
2257                                                 samples = 0.0f;
2258                                                 
2259                                                 /* cheaper distance-based filtering */
2260                                                 for( sy = (y - luxelFilterRadius); sy <= (y + luxelFilterRadius); sy++ )
2261                                                 {
2262                                                         if( sy < 0 || sy >= lm->sh )
2263                                                                 continue;
2264                                                         
2265                                                         for( sx = (x - luxelFilterRadius); sx <= (x + luxelFilterRadius); sx++ )
2266                                                         {
2267                                                                 if( sx < 0 || sx >= lm->sw )
2268                                                                         continue;
2269                                                                 
2270                                                                 /* get particulars */
2271                                                                 cluster = SUPER_CLUSTER( sx, sy );
2272                                                                 if( *cluster < 0 )
2273                                                                         continue;
2274                                                                 lightLuxel = LIGHT_LUXEL( sx, sy );
2275                                                                 
2276                                                                 /* create weight */
2277                                                                 weight = (abs( sx - x ) == luxelFilterRadius ? 0.5f : 1.0f);
2278                                                                 weight *= (abs( sy - y ) == luxelFilterRadius ? 0.5f : 1.0f);
2279                                                                 
2280                                                                 /* scale luxel by filter weight */
2281                                                                 VectorScale( lightLuxel, weight, color );
2282                                                                 VectorAdd( averageColor, color, averageColor );
2283                                                                 samples += weight;
2284                                                         }
2285                                                 }
2286                                                 
2287                                                 /* any samples? */
2288                                                 if( samples <= 0.0f     )
2289                                                         continue;
2290                                                 
2291                                                 /* scale into luxel */
2292                                                 luxel = SUPER_LUXEL( lightmapNum, x, y );
2293                                                 luxel[ 3 ] = 1.0f;
2294                                                 
2295                                                 /* handle negative light */
2296                                                 if( trace.light->flags & LIGHT_NEGATIVE )
2297                                                 { 
2298                                                         luxel[ 0 ] -= averageColor[ 0 ] / samples;
2299                                                         luxel[ 1 ] -= averageColor[ 1 ] / samples;
2300                                                         luxel[ 2 ] -= averageColor[ 2 ] / samples;
2301                                                 }
2302                                                 
2303                                                 /* handle normal light */
2304                                                 else
2305                                                 { 
2306                                                         luxel[ 0 ] += averageColor[ 0 ] / samples;
2307                                                         luxel[ 1 ] += averageColor[ 1 ] / samples;
2308                                                         luxel[ 2 ] += averageColor[ 2 ] / samples;
2309                                                 }
2310                                         }
2311                                         
2312                                         /* single sample */
2313                                         else
2314                                         {
2315                                                 /* get particulars */
2316                                                 lightLuxel = LIGHT_LUXEL( x, y );
2317                                                 luxel = SUPER_LUXEL( lightmapNum, x, y );
2318                                                 
2319                                                 /* handle negative light */
2320                                                 if( trace.light->flags & LIGHT_NEGATIVE )
2321                                                         VectorScale( averageColor, -1.0f, averageColor );
2322
2323                                                 /* add color */
2324                                                 luxel[ 3 ] = 1.0f;
2325                                                 
2326                                                 /* handle negative light */
2327                                                 if( trace.light->flags & LIGHT_NEGATIVE )
2328                                                         VectorSubtract( luxel, lightLuxel, luxel );
2329                                                 
2330                                                 /* handle normal light */
2331                                                 else
2332                                                         VectorAdd( luxel, lightLuxel, luxel );
2333                                         }
2334                                 }
2335                         }
2336                 }
2337                 
2338                 /* free temporary luxels */
2339                 if( lightLuxels != stackLightLuxels )
2340                         free( lightLuxels );
2341         }
2342         
2343         /* free light list */
2344         FreeTraceLights( &trace );
2345         
2346         /*      -----------------------------------------------------------------
2347                 floodlight pass
2348                 ----------------------------------------------------------------- */
2349
2350         if( floodlighty )
2351         {
2352                 /* walk lightmaps */
2353                 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2354                 {
2355                         /* early out */
2356                         if( lm->superLuxels[ lightmapNum ] == NULL )
2357                                 continue;
2358
2359                         /* apply floodlight to each luxel */
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                                         luxel = SUPER_LUXEL( lightmapNum, x, y );
2371                                         floodlight = SUPER_FLOODLIGHT( x, y );
2372
2373                                         flood[0]=floodlightRGB[0]*floodlightIntensity;
2374                                         flood[1]=floodlightRGB[1]*floodlightIntensity;
2375                                         flood[2]=floodlightRGB[2]*floodlightIntensity;
2376
2377                                         /* scale light value */
2378                                         VectorScale( flood, *floodlight, flood );
2379                                         luxel[0]+=flood[0];
2380                                         luxel[1]+=flood[1];
2381                                         luxel[2]+=flood[2];
2382
2383                                         if (luxel[3]==0) luxel[3]=1;
2384
2385                                         brightness = flood[ 0 ] * 0.3f + flood[ 1 ] * 0.59f + flood[ 2 ] * 0.11f;
2386                                         brightness *= (1.0 / 255.0);
2387                                         VectorScale( normal, brightness, temp );
2388                                         VectorAdd( deluxel, temp, deluxel );
2389                                 }
2390                         }
2391                 }
2392         }
2393
2394         if (debugnormals)
2395         {
2396                 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2397                 {
2398                         /* early out */
2399                         if( lm->superLuxels[ lightmapNum ] == NULL )
2400                                 continue;
2401
2402                         for( y = 0; y < lm->sh; y++ )
2403                         {
2404                                 for( x = 0; x < lm->sw; x++ )
2405                                 {
2406                                         /* get cluster */
2407                                         cluster = SUPER_CLUSTER( x, y );
2408                                         //%     if( *cluster < 0 )
2409                                         //%             continue;
2410
2411                                         /* get particulars */
2412                                         luxel = SUPER_LUXEL( lightmapNum, x, y );
2413                                         normal = SUPER_NORMAL (  x, y );
2414
2415                                         luxel[0]=(normal[0]*127)+127;
2416                                         luxel[1]=(normal[1]*127)+127;
2417                                         luxel[2]=(normal[2]*127)+127;
2418                                 }
2419                         }
2420                 }
2421         }
2422
2423         /*      -----------------------------------------------------------------
2424                 dirt pass
2425                 ----------------------------------------------------------------- */
2426         
2427         if( dirty )
2428         {
2429                 /* walk lightmaps */
2430                 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2431                 {
2432                         /* early out */
2433                         if( lm->superLuxels[ lightmapNum ] == NULL )
2434                                 continue;
2435                         
2436                         /* apply dirt to each luxel */
2437                         for( y = 0; y < lm->sh; y++ )
2438                         {
2439                                 for( x = 0; x < lm->sw; x++ )
2440                                 {
2441                                         /* get cluster */
2442                                         cluster = SUPER_CLUSTER( x, y );
2443                                         //%     if( *cluster < 0 ) // TODO why not do this check? These pixels should be zero anyway
2444                                         //%             continue;
2445                                         
2446                                         /* get particulars */
2447                                         luxel = SUPER_LUXEL( lightmapNum, x, y );
2448                                         dirt = SUPER_DIRT( x, y );
2449                                         
2450                                         /* apply dirt */
2451                                         VectorScale( luxel, *dirt, luxel );
2452                                         
2453                                         /* debugging */
2454                                         if( dirtDebug )
2455                                                 VectorSet( luxel, *dirt * 255.0f, *dirt * 255.0f, *dirt * 255.0f );
2456                                 }
2457                         }
2458                 }
2459         }
2460         
2461         /* -----------------------------------------------------------------
2462            filter pass
2463            ----------------------------------------------------------------- */
2464         
2465         /* walk lightmaps */
2466         for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2467         {
2468                 /* early out */
2469                 if( lm->superLuxels[ lightmapNum ] == NULL )
2470                         continue;
2471                 
2472                 /* average occluded luxels from neighbors */
2473                 for( y = 0; y < lm->sh; y++ )
2474                 {
2475                         for( x = 0; x < lm->sw; x++ )
2476                         {
2477                                 /* get particulars */
2478                                 cluster = SUPER_CLUSTER( x, y );
2479                                 luxel = SUPER_LUXEL( lightmapNum, x, y );
2480                                 deluxel = SUPER_DELUXEL( x, y );
2481                                 normal = SUPER_NORMAL( x, y );
2482                                 
2483                                 /* determine if filtering is necessary */
2484                                 filterColor = qfalse;
2485                                 filterDir = qfalse;
2486                                 if( *cluster < 0 ||
2487                                         (lm->splotchFix && (luxel[ 0 ] <= ambientColor[ 0 ] || luxel[ 1 ] <= ambientColor[ 1 ] || luxel[ 2 ] <= ambientColor[ 2 ])) )
2488                                         filterColor = qtrue;
2489                                 if( deluxemap && lightmapNum == 0 && (*cluster < 0 || filter) )
2490                                         filterDir = qtrue;
2491                                 
2492                                 if( !filterColor && !filterDir )
2493                                         continue;
2494                                 
2495                                 /* choose seed amount */
2496                                 VectorClear( averageColor );
2497                                 VectorClear( averageDir );
2498                                 samples = 0.0f;
2499                                 
2500                                 /* walk 3x3 matrix */
2501                                 for( sy = (y - 1); sy <= (y + 1); sy++ )
2502                                 {
2503                                         if( sy < 0 || sy >= lm->sh )
2504                                                 continue;
2505                                         
2506                                         for( sx = (x - 1); sx <= (x + 1); sx++ )
2507                                         {
2508                                                 if( sx < 0 || sx >= lm->sw || (sx == x && sy == y) )
2509                                                         continue;
2510                                                 
2511                                                 /* get neighbor's particulars */
2512                                                 cluster2 = SUPER_CLUSTER( sx, sy );
2513                                                 luxel2 = SUPER_LUXEL( lightmapNum, sx, sy );
2514                                                 deluxel2 = SUPER_DELUXEL( sx, sy );
2515                                                 
2516                                                 /* ignore unmapped/unlit luxels */
2517                                                 if( *cluster2 < 0 || luxel2[ 3 ] == 0.0f ||
2518                                                         (lm->splotchFix && VectorCompare( luxel2, ambientColor )) )
2519                                                         continue;
2520                                                 
2521                                                 /* add its distinctiveness to our own */
2522                                                 VectorAdd( averageColor, luxel2, averageColor );
2523                                                 samples += luxel2[ 3 ];
2524                                                 if( filterDir )
2525                                                         VectorAdd( averageDir, deluxel2, averageDir );
2526                                         }
2527                                 }
2528                                 
2529                                 /* fall through */
2530                                 if( samples <= 0.0f )
2531                                         continue;
2532                                 
2533                                 /* dark lightmap seams */
2534                                 if( dark )
2535                                 {
2536                                         if( lightmapNum == 0 )
2537                                                 VectorMA( averageColor, 2.0f, ambientColor, averageColor );
2538                                         samples += 2.0f;
2539                                 }
2540                                 
2541                                 /* average it */
2542                                 if( filterColor )
2543                                 {
2544                                         VectorDivide( averageColor, samples, luxel );
2545                                         luxel[ 3 ] = 1.0f;
2546                                 }
2547                                 if( filterDir )
2548                                         VectorDivide( averageDir, samples, deluxel );
2549                                 
2550                                 /* set cluster to -3 */
2551                                 if( *cluster < 0 )
2552                                         *cluster = CLUSTER_FLOODED;
2553                         }
2554                 }
2555         }
2556
2557
2558 #if 0
2559         // audit pass
2560         for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2561         {
2562                 /* early out */
2563                 if( lm->superLuxels[ lightmapNum ] == NULL )
2564                         continue;
2565                 for( y = 0; y < lm->sh; y++ )
2566                         for( x = 0; x < lm->sw; x++ )
2567                         {
2568                                 /* get cluster */
2569                                 cluster = SUPER_CLUSTER( x, y );
2570                                 luxel = SUPER_LUXEL( lightmapNum, x, y );
2571                                 deluxel = SUPER_DELUXEL( x, y );
2572                                 if(!luxel || !deluxel || !cluster)
2573                                 {
2574                                         Sys_FPrintf(SYS_VRB, "WARNING: I got NULL'd.\n");
2575                                         continue;
2576                                 }
2577                                 else if(*cluster < 0)
2578                                 {
2579                                         // unmapped pixel
2580                                         // should have neither deluxemap nor lightmap
2581                                         if(deluxel[3])
2582                                                 Sys_FPrintf(SYS_VRB, "WARNING: I have written deluxe to an unmapped luxel. Sorry.\n");
2583                                 }
2584                                 else
2585                                 {
2586                                         // mapped pixel
2587                                         // should have both deluxemap and lightmap
2588                                         if(deluxel[3])
2589                                                 Sys_FPrintf(SYS_VRB, "WARNING: I forgot to write deluxe to a mapped luxel. Sorry.\n");
2590                                 }
2591                         }
2592         }
2593 #endif
2594 }
2595
2596
2597
2598 /*
2599 IlluminateVertexes()
2600 light the surface vertexes
2601 */
2602
2603 #define VERTEX_NUDGE    4.0f
2604
2605 void IlluminateVertexes( int num )
2606 {
2607         int                                     i, x, y, z, x1, y1, z1, sx, sy, radius, maxRadius, *cluster;
2608         int                                     lightmapNum, numAvg;
2609         float                           samples, *vertLuxel, *radVertLuxel, *luxel, dirt;
2610         vec3_t                          origin, temp, temp2, colors[ MAX_LIGHTMAPS ], avgColors[ MAX_LIGHTMAPS ];
2611         bspDrawSurface_t        *ds;
2612         surfaceInfo_t           *info;
2613         rawLightmap_t           *lm;
2614         bspDrawVert_t           *verts;
2615         trace_t                         trace;
2616         
2617         
2618         /* get surface, info, and raw lightmap */
2619         ds = &bspDrawSurfaces[ num ];
2620         info = &surfaceInfos[ num ];
2621         lm = info->lm;
2622         
2623         /* -----------------------------------------------------------------
2624            illuminate the vertexes
2625            ----------------------------------------------------------------- */
2626         
2627         /* calculate vertex lighting for surfaces without lightmaps */
2628         if( lm == NULL || cpmaHack )
2629         {
2630                 /* setup trace */
2631                 trace.testOcclusion = (cpmaHack && lm != NULL) ? qfalse : !noTrace;
2632                 trace.forceSunlight = info->si->forceSunlight;
2633                 trace.recvShadows = info->recvShadows;
2634                 trace.numSurfaces = 1;
2635                 trace.surfaces = &num;
2636                 trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
2637                 
2638                 /* twosided lighting */
2639                 trace.twoSided = info->si->twoSided;
2640                 
2641                 /* make light list for this surface */
2642                 CreateTraceLightsForSurface( num, &trace );
2643                 
2644                 /* setup */
2645                 verts = yDrawVerts + ds->firstVert;
2646                 numAvg = 0;
2647                 memset( avgColors, 0, sizeof( avgColors ) );
2648                 
2649                 /* walk the surface verts */
2650                 for( i = 0; i < ds->numVerts; i++ )
2651                 {
2652                         /* get vertex luxel */
2653                         radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2654                         
2655                         /* color the luxel with raw lightmap num? */
2656                         if( debugSurfaces )
2657                                 VectorCopy( debugColors[ num % 12 ], radVertLuxel );
2658                         
2659                         /* color the luxel with luxel origin? */
2660                         else if( debugOrigin )
2661                         {
2662                                 VectorSubtract( info->maxs, info->mins, temp );
2663                                 VectorScale( temp, (1.0f / 255.0f), temp );
2664                                 VectorSubtract( origin, lm->mins, temp2 );
2665                                 radVertLuxel[ 0 ] = info->mins[ 0 ] + (temp[ 0 ] * temp2[ 0 ]);
2666                                 radVertLuxel[ 1 ] = info->mins[ 1 ] + (temp[ 1 ] * temp2[ 1 ]);
2667                                 radVertLuxel[ 2 ] = info->mins[ 2 ] + (temp[ 2 ] * temp2[ 2 ]);
2668                         }
2669                         
2670                         /* color the luxel with the normal */
2671                         else if( normalmap )
2672                         {
2673                                 radVertLuxel[ 0 ] = (verts[ i ].normal[ 0 ] + 1.0f) * 127.5f;
2674                                 radVertLuxel[ 1 ] = (verts[ i ].normal[ 1 ] + 1.0f) * 127.5f;
2675                                 radVertLuxel[ 2 ] = (verts[ i ].normal[ 2 ] + 1.0f) * 127.5f;
2676                         }
2677                         
2678                         /* illuminate the vertex */
2679                         else
2680                         {
2681                                 /* clear vertex luxel */
2682                                 VectorSet( radVertLuxel, -1.0f, -1.0f, -1.0f );
2683                                 
2684                                 /* try at initial origin */
2685                                 trace.cluster = ClusterForPointExtFilter( verts[ i ].xyz, VERTEX_EPSILON, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ] );
2686                                 if( trace.cluster >= 0 )
2687                                 {
2688                                         /* setup trace */
2689                                         VectorCopy( verts[ i ].xyz, trace.origin );
2690                                         VectorCopy( verts[ i ].normal, trace.normal );
2691                                         
2692                                         /* r7 dirt */
2693                                         if( dirty )
2694                                                 dirt = DirtForSample( &trace );
2695                                         else
2696                                                 dirt = 1.0f;
2697
2698                                         /* trace */
2699                                         LightingAtSample( &trace, ds->vertexStyles, colors );
2700                                         
2701                                         /* store */
2702                                         for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2703                                         {
2704                                                 /* r7 dirt */
2705                                                 VectorScale( colors[ lightmapNum ], dirt, colors[ lightmapNum ] );
2706                                                 
2707                                                 /* store */
2708                                                 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2709                                                 VectorCopy( colors[ lightmapNum ], radVertLuxel );
2710                                                 VectorAdd( avgColors[ lightmapNum ], colors[ lightmapNum ], colors[ lightmapNum ] );
2711                                         }
2712                                 }
2713                                 
2714                                 /* is this sample bright enough? */
2715                                 radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2716                                 if( radVertLuxel[ 0 ] <= ambientColor[ 0 ] &&
2717                                         radVertLuxel[ 1 ] <= ambientColor[ 1 ] &&
2718                                         radVertLuxel[ 2 ] <= ambientColor[ 2 ] )
2719                                 {
2720                                         /* nudge the sample point around a bit */
2721                                         for( x = 0; x < 4; x++ )
2722                                         {
2723                                                 /* two's complement 0, 1, -1, 2, -2, etc */
2724                                                 x1 = ((x >> 1) ^ (x & 1 ? -1 : 0)) + (x & 1);
2725                                                 
2726                                                 for( y = 0; y < 4; y++ )
2727                                                 {
2728                                                         y1 = ((y >> 1) ^ (y & 1 ? -1 : 0)) + (y & 1);
2729                                                         
2730                                                         for( z = 0; z < 4; z++ )
2731                                                         {
2732                                                                 z1 = ((z >> 1) ^ (z & 1 ? -1 : 0)) + (z & 1);
2733                                                                 
2734                                                                 /* nudge origin */
2735                                                                 trace.origin[ 0 ] = verts[ i ].xyz[ 0 ] + (VERTEX_NUDGE * x1);
2736                                                                 trace.origin[ 1 ] = verts[ i ].xyz[ 1 ] + (VERTEX_NUDGE * y1);
2737                                                                 trace.origin[ 2 ] = verts[ i ].xyz[ 2 ] + (VERTEX_NUDGE * z1);
2738                                                                 
2739                                                                 /* try at nudged origin */
2740                                                                 trace.cluster = ClusterForPointExtFilter( origin, VERTEX_EPSILON, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ] );
2741                                                                 if( trace.cluster < 0 )
2742                                                                         continue;
2743                                                                                                                         
2744                                                                 /* trace */
2745                                                                 LightingAtSample( &trace, ds->vertexStyles, colors );
2746                                                                 
2747                                                                 /* store */
2748                                                                 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2749                                                                 {
2750                                                                         /* r7 dirt */
2751                                                                         VectorScale( colors[ lightmapNum ], dirt, colors[ lightmapNum ] );
2752                                                                         
2753                                                                         /* store */
2754                                                                         radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2755                                                                         VectorCopy( colors[ lightmapNum ], radVertLuxel );
2756                                                                 }
2757                                                                 
2758                                                                 /* bright enough? */
2759                                                                 radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2760                                                                 if( radVertLuxel[ 0 ] > ambientColor[ 0 ] ||
2761                                                                         radVertLuxel[ 1 ] > ambientColor[ 1 ] ||
2762                                                                         radVertLuxel[ 2 ] > ambientColor[ 2 ] )
2763                                                                         x = y = z = 1000;
2764                                                         }
2765                                                 }
2766                                         }
2767                                 }
2768                                 
2769                                 /* add to average? */
2770                                 radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2771                                 if( radVertLuxel[ 0 ] > ambientColor[ 0 ] ||
2772                                         radVertLuxel[ 1 ] > ambientColor[ 1 ] ||
2773                                         radVertLuxel[ 2 ] > ambientColor[ 2 ] )
2774                                 {
2775                                         numAvg++;
2776                                         for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2777                                         {
2778                                                 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2779                                                 VectorAdd( avgColors[ lightmapNum ], radVertLuxel, avgColors[ lightmapNum ] );
2780                                         }
2781                                 }
2782                         }
2783                         
2784                         /* another happy customer */
2785                         numVertsIlluminated++;
2786                 }
2787                 
2788                 /* set average color */
2789                 if( numAvg > 0 )
2790                 {
2791                         for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2792                                 VectorScale( avgColors[ lightmapNum ], (1.0f / numAvg), avgColors[ lightmapNum ] );
2793                 }
2794                 else
2795                 {
2796                         VectorCopy( ambientColor, avgColors[ 0 ] );
2797                 }
2798                 
2799                 /* clean up and store vertex color */
2800                 for( i = 0; i < ds->numVerts; i++ )
2801                 {
2802                         /* get vertex luxel */
2803                         radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i );
2804                         
2805                         /* store average in occluded vertexes */
2806                         if( radVertLuxel[ 0 ] < 0.0f )
2807                         {
2808                                 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2809                                 {
2810                                         radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2811                                         VectorCopy( avgColors[ lightmapNum ], radVertLuxel );
2812                                         
2813                                         /* debug code */
2814                                         //%     VectorSet( radVertLuxel, 255.0f, 0.0f, 0.0f );
2815                                 }
2816                         }
2817                         
2818                         /* store it */
2819                         for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2820                         {
2821                                 /* get luxels */
2822                                 vertLuxel = VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2823                                 radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2824                                 
2825                                 /* store */
2826                                 if( bouncing || bounce == 0 || !bounceOnly )
2827                                         VectorAdd( vertLuxel, radVertLuxel, vertLuxel );
2828                                 if( !info->si->noVertexLight )
2829                                         ColorToBytes( vertLuxel, verts[ i ].color[ lightmapNum ], info->si->vertexScale );
2830                         }
2831                 }
2832                 
2833                 /* free light list */
2834                 FreeTraceLights( &trace );
2835                 
2836                 /* return to sender */
2837                 return;
2838         }
2839         
2840         /* -----------------------------------------------------------------
2841            reconstitute vertex lighting from the luxels
2842            ----------------------------------------------------------------- */
2843         
2844         /* set styles from lightmap */
2845         for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2846                 ds->vertexStyles[ lightmapNum ] = lm->styles[ lightmapNum ];
2847         
2848         /* get max search radius */
2849         maxRadius = lm->sw;
2850         maxRadius = maxRadius > lm->sh ? maxRadius : lm->sh;
2851         
2852         /* walk the surface verts */
2853         verts = yDrawVerts + ds->firstVert;
2854         for( i = 0; i < ds->numVerts; i++ )
2855         {
2856                 /* do each lightmap */
2857                 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
2858                 {
2859                         /* early out */
2860                         if( lm->superLuxels[ lightmapNum ] == NULL )
2861                                 continue;
2862                         
2863                         /* get luxel coords */
2864                         x = verts[ i ].lightmap[ lightmapNum ][ 0 ];
2865                         y = verts[ i ].lightmap[ lightmapNum ][ 1 ];
2866                         if( x < 0 )
2867                                 x = 0;
2868                         else if( x >= lm->sw )
2869                                 x = lm->sw - 1;
2870                         if( y < 0 )
2871                                 y = 0;
2872                         else if( y >= lm->sh )
2873                                 y = lm->sh - 1;
2874                         
2875                         /* get vertex luxels */
2876                         vertLuxel = VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2877                         radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i );
2878                         
2879                         /* color the luxel with the normal? */
2880                         if( normalmap )
2881                         {
2882                                 radVertLuxel[ 0 ] = (verts[ i ].normal[ 0 ] + 1.0f) * 127.5f;
2883                                 radVertLuxel[ 1 ] = (verts[ i ].normal[ 1 ] + 1.0f) * 127.5f;
2884                                 radVertLuxel[ 2 ] = (verts[ i ].normal[ 2 ] + 1.0f) * 127.5f;
2885                         }
2886                         
2887                         /* color the luxel with surface num? */
2888                         else if( debugSurfaces )
2889                                 VectorCopy( debugColors[ num % 12 ], radVertLuxel );
2890                         
2891                         /* divine color from the superluxels */
2892                         else
2893                         {
2894                                 /* increasing radius */
2895                                 VectorClear( radVertLuxel );
2896                                 samples = 0.0f;
2897                                 for( radius = 0; radius < maxRadius && samples <= 0.0f; radius++ )
2898                                 {
2899                                         /* sample within radius */
2900                                         for( sy = (y - radius); sy <= (y + radius); sy++ )
2901                                         {
2902                                                 if( sy < 0 || sy >= lm->sh )
2903                                                         continue;
2904                                                 
2905                                                 for( sx = (x - radius); sx <= (x + radius); sx++ )
2906                                                 {
2907                                                         if( sx < 0 || sx >= lm->sw )
2908                                                                 continue;
2909                                                         
2910                                                         /* get luxel particulars */
2911                                                         luxel = SUPER_LUXEL( lightmapNum, sx, sy );
2912                                                         cluster = SUPER_CLUSTER( sx, sy );
2913                                                         if( *cluster < 0 )
2914                                                                 continue;
2915                                                         
2916                                                         /* testing: must be brigher than ambient color */
2917                                                         //%     if( luxel[ 0 ] <= ambientColor[ 0 ] || luxel[ 1 ] <= ambientColor[ 1 ] || luxel[ 2 ] <= ambientColor[ 2 ] )
2918                                                         //%             continue;
2919                                                         
2920                                                         /* add its distinctiveness to our own */
2921                                                         VectorAdd( radVertLuxel, luxel, radVertLuxel );
2922                                                         samples += luxel[ 3 ];
2923                                                 }
2924                                         }
2925                                 }
2926                                 
2927                                 /* any color? */
2928                                 if( samples > 0.0f )
2929                                         VectorDivide( radVertLuxel, samples, radVertLuxel );
2930                                 else
2931                                         VectorCopy( ambientColor, radVertLuxel );
2932                         }
2933                         
2934                         /* store into floating point storage */
2935                         VectorAdd( vertLuxel, radVertLuxel, vertLuxel );
2936                         numVertsIlluminated++;
2937                         
2938                         /* store into bytes (for vertex approximation) */
2939                         if( !info->si->noVertexLight )
2940                                 ColorToBytes( vertLuxel, verts[ i ].color[ lightmapNum ], 1.0f );
2941                 }
2942         }
2943 }
2944
2945
2946
2947 /* -------------------------------------------------------------------------------
2948
2949 light optimization (-fast)
2950
2951 creates a list of lights that will affect a surface and stores it in tw
2952 this is to optimize surface lighting by culling out as many of the
2953 lights in the world as possible from further calculation
2954
2955 ------------------------------------------------------------------------------- */
2956
2957 /*
2958 SetupBrushes()
2959 determines opaque brushes in the world and find sky shaders for sunlight calculations
2960 */
2961
2962 void SetupBrushes( void )
2963 {
2964         int                             i, j, b, compileFlags;
2965         qboolean                inside;
2966         bspBrush_t              *brush;
2967         bspBrushSide_t  *side;
2968         bspShader_t             *shader;
2969         shaderInfo_t    *si;
2970         
2971         
2972         /* note it */
2973         Sys_FPrintf( SYS_VRB, "--- SetupBrushes ---\n" );
2974         
2975         /* allocate */
2976         if( opaqueBrushes == NULL )
2977                 opaqueBrushes = safe_malloc( numBSPBrushes / 8 + 1 );
2978         
2979         /* clear */
2980         memset( opaqueBrushes, 0, numBSPBrushes / 8 + 1 );
2981         numOpaqueBrushes = 0;
2982         
2983         /* walk the list of worldspawn brushes */
2984         for( i = 0; i < bspModels[ 0 ].numBSPBrushes; i++ )
2985         {
2986                 /* get brush */
2987                 b = bspModels[ 0 ].firstBSPBrush + i;
2988                 brush = &bspBrushes[ b ];
2989                 
2990                 /* check all sides */
2991                 inside = qtrue;
2992                 compileFlags = 0;
2993                 for( j = 0; j < brush->numSides && inside; j++ )
2994                 {
2995                         /* do bsp shader calculations */
2996                         side = &bspBrushSides[ brush->firstSide + j ];
2997                         shader = &bspShaders[ side->shaderNum ];
2998                         
2999                         /* get shader info */
3000                         si = ShaderInfoForShader( shader->shader );
3001                         if( si == NULL )
3002                                 continue;
3003                         
3004                         /* or together compile flags */
3005                         compileFlags |= si->compileFlags;
3006                 }
3007                 
3008                 /* determine if this brush is opaque to light */
3009                 if( !(compileFlags & C_TRANSLUCENT) )
3010                 {
3011                         opaqueBrushes[ b >> 3 ] |= (1 << (b & 7));
3012                         numOpaqueBrushes++;
3013                         maxOpaqueBrush = i;
3014                 }
3015         }
3016         
3017         /* emit some statistics */
3018         Sys_FPrintf( SYS_VRB, "%9d opaque brushes\n", numOpaqueBrushes );
3019 }
3020
3021
3022
3023 /*
3024 ClusterVisible()
3025 determines if two clusters are visible to each other using the PVS
3026 */
3027
3028 qboolean ClusterVisible( int a, int b )
3029 {
3030         int                     portalClusters, leafBytes;
3031         byte            *pvs;
3032         
3033         
3034         /* dummy check */
3035         if( a < 0 || b < 0 )
3036                 return qfalse;
3037         
3038         /* early out */
3039         if( a == b )
3040                 return qtrue;
3041         
3042         /* not vised? */
3043         if( numBSPVisBytes <=8 )
3044                 return qtrue;
3045         
3046         /* get pvs data */
3047         portalClusters = ((int *) bspVisBytes)[ 0 ];
3048         leafBytes = ((int*) bspVisBytes)[ 1 ];
3049         pvs = bspVisBytes + VIS_HEADER_SIZE + (a * leafBytes);
3050         
3051         /* check */
3052         if( (pvs[ b >> 3 ] & (1 << (b & 7))) )
3053                 return qtrue;
3054         return qfalse;
3055 }
3056
3057
3058
3059 /*
3060 PointInLeafNum_r()
3061 borrowed from vlight.c
3062 */
3063
3064 int     PointInLeafNum_r( vec3_t point, int nodenum )
3065 {
3066         int                     leafnum;
3067         vec_t           dist;
3068         bspNode_t               *node;
3069         bspPlane_t      *plane;
3070         
3071         
3072         while( nodenum >= 0 )
3073         {
3074                 node = &bspNodes[ nodenum ];
3075                 plane = &bspPlanes[ node->planeNum ];
3076                 dist = DotProduct( point, plane->normal ) - plane->dist;
3077                 if( dist > 0.1 )
3078                         nodenum = node->children[ 0 ];
3079                 else if( dist < -0.1 )
3080                         nodenum = node->children[ 1 ];
3081                 else
3082                 {
3083                         leafnum = PointInLeafNum_r( point, node->children[ 0 ] );
3084                         if( bspLeafs[ leafnum ].cluster != -1 )
3085                                 return leafnum;
3086                         nodenum = node->children[ 1 ];
3087                 }
3088         }
3089         
3090         leafnum = -nodenum - 1;
3091         return leafnum;
3092 }
3093
3094
3095
3096 /*
3097 PointInLeafnum()
3098 borrowed from vlight.c
3099 */
3100
3101 int     PointInLeafNum( vec3_t point )
3102 {
3103         return PointInLeafNum_r( point, 0 );
3104 }
3105
3106
3107
3108 /*
3109 ClusterVisibleToPoint() - ydnar
3110 returns qtrue if point can "see" cluster
3111 */
3112
3113 qboolean ClusterVisibleToPoint( vec3_t point, int cluster )
3114 {
3115         int             pointCluster;
3116         
3117
3118         /* get leafNum for point */
3119         pointCluster = ClusterForPoint( point );
3120         if( pointCluster < 0 )
3121                 return qfalse;
3122         
3123         /* check pvs */
3124         return ClusterVisible( pointCluster, cluster );
3125 }
3126
3127
3128
3129 /*
3130 ClusterForPoint() - ydnar
3131 returns the pvs cluster for point
3132 */
3133
3134 int ClusterForPoint( vec3_t point )
3135 {
3136         int             leafNum;
3137         
3138
3139         /* get leafNum for point */
3140         leafNum = PointInLeafNum( point );
3141         if( leafNum < 0 )
3142                 return -1;
3143         
3144         /* return the cluster */
3145         return bspLeafs[ leafNum ].cluster;
3146 }
3147
3148
3149
3150 /*
3151 ClusterForPointExt() - ydnar
3152 also takes brushes into account for occlusion testing
3153 */
3154
3155 int ClusterForPointExt( vec3_t point, float epsilon )
3156 {
3157         int                             i, j, b, leafNum, cluster;
3158         float                   dot;
3159         qboolean                inside;
3160         int                             *brushes, numBSPBrushes;
3161         bspLeaf_t               *leaf;
3162         bspBrush_t              *brush;
3163         bspPlane_t              *plane;
3164         
3165         
3166         /* get leaf for point */
3167         leafNum = PointInLeafNum( point );
3168         if( leafNum < 0 )
3169                 return -1;
3170         leaf = &bspLeafs[ leafNum ];
3171         
3172         /* get the cluster */
3173         cluster = leaf->cluster;
3174         if( cluster < 0 )
3175                 return -1;
3176         
3177         /* transparent leaf, so check point against all brushes in the leaf */
3178         brushes = &bspLeafBrushes[ leaf->firstBSPLeafBrush ];
3179         numBSPBrushes = leaf->numBSPLeafBrushes;
3180         for( i = 0; i < numBSPBrushes; i++ )
3181         {
3182                 /* get parts */
3183                 b = brushes[ i ];
3184                 if( b > maxOpaqueBrush )
3185                         continue;
3186                 brush = &bspBrushes[ b ];
3187                 if( !(opaqueBrushes[ b >> 3 ] & (1 << (b & 7))) )
3188                         continue;
3189                 
3190                 /* check point against all planes */
3191                 inside = qtrue;
3192                 for( j = 0; j < brush->numSides && inside; j++ )
3193                 {
3194                         plane = &bspPlanes[ bspBrushSides[ brush->firstSide + j ].planeNum ];
3195                         dot = DotProduct( point, plane->normal );
3196                         dot -= plane->dist;
3197                         if( dot > epsilon )
3198                                 inside = qfalse;
3199                 }
3200                 
3201                 /* if inside, return bogus cluster */
3202                 if( inside )
3203                         return -1 - b;
3204         }
3205         
3206         /* if the point made it this far, it's not inside any opaque brushes */
3207         return cluster;
3208 }
3209
3210
3211
3212 /*
3213 ClusterForPointExtFilter() - ydnar
3214 adds cluster checking against a list of known valid clusters
3215 */
3216
3217 int ClusterForPointExtFilter( vec3_t point, float epsilon, int numClusters, int *clusters )
3218 {
3219         int             i, cluster;
3220         
3221         
3222         /* get cluster for point */
3223         cluster = ClusterForPointExt( point, epsilon );
3224         
3225         /* check if filtering is necessary */
3226         if( cluster < 0 || numClusters <= 0 || clusters == NULL )
3227                 return cluster;
3228         
3229         /* filter */
3230         for( i = 0; i < numClusters; i++ )
3231         {
3232                 if( cluster == clusters[ i ] || ClusterVisible( cluster, clusters[ i ] ) )
3233                         return cluster;
3234         }
3235         
3236         /* failed */
3237         return -1;
3238 }
3239
3240
3241
3242 /*
3243 ShaderForPointInLeaf() - ydnar
3244 checks a point against all brushes in a leaf, returning the shader of the brush
3245 also sets the cumulative surface and content flags for the brush hit
3246 */
3247
3248 int ShaderForPointInLeaf( vec3_t point, int leafNum, float epsilon, int wantContentFlags, int wantSurfaceFlags, int *contentFlags, int *surfaceFlags )
3249 {
3250         int                             i, j;
3251         float                   dot;
3252         qboolean                inside;
3253         int                             *brushes, numBSPBrushes;
3254         bspLeaf_t                       *leaf;
3255         bspBrush_t              *brush;
3256         bspBrushSide_t  *side;
3257         bspPlane_t              *plane;
3258         bspShader_t             *shader;
3259         int                             allSurfaceFlags, allContentFlags;
3260
3261         
3262         /* clear things out first */
3263         *surfaceFlags = 0;
3264         *contentFlags = 0;
3265         
3266         /* get leaf */
3267         if( leafNum < 0 )
3268                 return -1;
3269         leaf = &bspLeafs[ leafNum ];
3270         
3271         /* transparent leaf, so check point against all brushes in the leaf */
3272         brushes = &bspLeafBrushes[ leaf->firstBSPLeafBrush ];
3273         numBSPBrushes = leaf->numBSPLeafBrushes;
3274         for( i = 0; i < numBSPBrushes; i++ )
3275         {
3276                 /* get parts */
3277                 brush = &bspBrushes[ brushes[ i ] ];
3278                 
3279                 /* check point against all planes */
3280                 inside = qtrue;
3281                 allSurfaceFlags = 0;
3282                 allContentFlags = 0;
3283                 for( j = 0; j < brush->numSides && inside; j++ )
3284                 {
3285                         side = &bspBrushSides[ brush->firstSide + j ];
3286                         plane = &bspPlanes[ side->planeNum ];
3287                         dot = DotProduct( point, plane->normal );
3288                         dot -= plane->dist;
3289                         if( dot > epsilon )
3290                                 inside = qfalse;
3291                         else
3292                         {
3293                                 shader = &bspShaders[ side->shaderNum ];
3294                                 allSurfaceFlags |= shader->surfaceFlags;
3295                                 allContentFlags |= shader->contentFlags;
3296                         }
3297                 }
3298                 
3299                 /* handle if inside */
3300                 if( inside )
3301                 {
3302                         /* if there are desired flags, check for same and continue if they aren't matched */
3303                         if( wantContentFlags && !(wantContentFlags & allContentFlags) )
3304                                 continue;
3305                         if( wantSurfaceFlags && !(wantSurfaceFlags & allSurfaceFlags) )
3306                                 continue;
3307                         
3308                         /* store the cumulative flags and return the brush shader (which is mostly useless) */
3309                         *surfaceFlags = allSurfaceFlags;
3310                         *contentFlags = allContentFlags;
3311                         return brush->shaderNum;
3312                 }
3313         }
3314         
3315         /* if the point made it this far, it's not inside any brushes */
3316         return -1;
3317 }
3318
3319
3320
3321 /*
3322 ChopBounds()
3323 chops a bounding box by the plane defined by origin and normal
3324 returns qfalse if the bounds is entirely clipped away
3325
3326 this is not exactly the fastest way to do this...
3327 */
3328
3329 qboolean ChopBounds( vec3_t mins, vec3_t maxs, vec3_t origin, vec3_t normal )
3330 {
3331         /* FIXME: rewrite this so it doesn't use bloody brushes */
3332         return qtrue;
3333 }
3334
3335
3336
3337 /*
3338 SetupEnvelopes()
3339 calculates each light's effective envelope,
3340 taking into account brightness, type, and pvs.
3341 */
3342
3343 #define LIGHT_EPSILON   0.125f
3344 #define LIGHT_NUDGE             2.0f
3345
3346 void SetupEnvelopes( qboolean forGrid, qboolean fastFlag )
3347 {
3348         int                     i, x, y, z, x1, y1, z1;
3349         light_t         *light, *light2, **owner;
3350         bspLeaf_t       *leaf;
3351         vec3_t          origin, dir, mins, maxs, nullVector = { 0, 0, 0 };
3352         float           radius, intensity;
3353         light_t         *buckets[ 256 ];
3354         
3355         
3356         /* early out for weird cases where there are no lights */
3357         if( lights == NULL )
3358                 return;
3359         
3360         /* note it */
3361         Sys_FPrintf( SYS_VRB, "--- SetupEnvelopes%s ---\n", fastFlag ? " (fast)" : "" );
3362         
3363         /* count lights */
3364         numLights = 0;
3365         numCulledLights = 0;
3366         owner = &lights;
3367         while( *owner != NULL )
3368         {
3369                 /* get light */
3370                 light = *owner;
3371                 
3372                 /* handle negative lights */
3373                 if( light->photons < 0.0f || light->add < 0.0f )
3374                 {
3375                         light->photons *= -1.0f;
3376                         light->add *= -1.0f;
3377                         light->flags |= LIGHT_NEGATIVE;
3378                 }
3379                 
3380                 /* sunlight? */
3381                 if( light->type == EMIT_SUN )
3382                 {
3383                         /* special cased */
3384                         light->cluster = 0;
3385                         light->envelope = MAX_WORLD_COORD * 8.0f;
3386                         VectorSet( light->mins, MIN_WORLD_COORD * 8.0f, MIN_WORLD_COORD * 8.0f, MIN_WORLD_COORD * 8.0f );
3387                         VectorSet( light->maxs, MAX_WORLD_COORD * 8.0f, MAX_WORLD_COORD * 8.0f, MAX_WORLD_COORD * 8.0f );
3388                 }
3389                 
3390                 /* everything else */
3391                 else
3392                 {
3393                         /* get pvs cluster for light */
3394                         light->cluster = ClusterForPointExt( light->origin, LIGHT_EPSILON );
3395                         
3396                         /* invalid cluster? */
3397                         if( light->cluster < 0 )
3398                         {
3399                                 /* nudge the sample point around a bit */
3400                                 for( x = 0; x < 4; x++ )
3401                                 {
3402                                         /* two's complement 0, 1, -1, 2, -2, etc */
3403                                         x1 = ((x >> 1) ^ (x & 1 ? -1 : 0)) + (x & 1);
3404                                         
3405                                         for( y = 0; y < 4; y++ )
3406                                         {
3407                                                 y1 = ((y >> 1) ^ (y & 1 ? -1 : 0)) + (y & 1);
3408                                                 
3409                                                 for( z = 0; z < 4; z++ )
3410                                                 {
3411                                                         z1 = ((z >> 1) ^ (z & 1 ? -1 : 0)) + (z & 1);
3412                                                         
3413                                                         /* nudge origin */
3414                                                         origin[ 0 ] = light->origin[ 0 ] + (LIGHT_NUDGE * x1);
3415                                                         origin[ 1 ] = light->origin[ 1 ] + (LIGHT_NUDGE * y1);
3416                                                         origin[ 2 ] = light->origin[ 2 ] + (LIGHT_NUDGE * z1);
3417                                                         
3418                                                         /* try at nudged origin */
3419                                                         light->cluster = ClusterForPointExt( origin, LIGHT_EPSILON );
3420                                                         if( light->cluster < 0 )
3421                                                                 continue;
3422                                                                         
3423                                                         /* set origin */
3424                                                         VectorCopy( origin, light->origin );
3425                                                 }
3426                                         }
3427                                 }
3428                         }
3429                         
3430                         /* only calculate for lights in pvs and outside of opaque brushes */
3431                         if( light->cluster >= 0 )
3432                         {
3433                                 /* set light fast flag */
3434                                 if( fastFlag )
3435                                         light->flags |= LIGHT_FAST_TEMP;
3436                                 else
3437                                         light->flags &= ~LIGHT_FAST_TEMP;
3438                                 if( light->si && light->si->noFast )
3439                                         light->flags &= ~(LIGHT_FAST | LIGHT_FAST_TEMP);
3440                                 
3441                                 /* clear light envelope */
3442                                 light->envelope = 0;
3443                                 
3444                                 /* handle area lights */
3445                                 if( exactPointToPolygon && light->type == EMIT_AREA && light->w != NULL )
3446                                 {
3447                                         /* ugly hack to calculate extent for area lights, but only done once */
3448                                         VectorScale( light->normal, -1.0f, dir );
3449                                         for( radius = 100.0f; radius < 130000.0f && light->envelope == 0; radius += 10.0f )
3450                                         {
3451                                                 float   factor;
3452                                                 
3453                                                 VectorMA( light->origin, radius, light->normal, origin );
3454                                                 factor = PointToPolygonFormFactor( origin, dir, light->w );
3455                                                 if( factor < 0.0f )
3456                                                         factor *= -1.0f;
3457                                                 if( (factor * light->add) <= light->falloffTolerance )
3458                                                         light->envelope = radius;
3459                                         }
3460                                         
3461                                         /* check for fast mode */
3462                                         if( !(light->flags & LIGHT_FAST) && !(light->flags & LIGHT_FAST_TEMP) )
3463                                                 light->envelope = MAX_WORLD_COORD * 8.0f;
3464                                 }
3465                                 else
3466                                 {
3467                                         radius = 0.0f;
3468                                         intensity = light->photons;
3469                                 }
3470                                 
3471                                 /* other calcs */
3472                                 if( light->envelope <= 0.0f )
3473                                 {
3474                                         /* solve distance for non-distance lights */
3475                                         if( !(light->flags & LIGHT_ATTEN_DISTANCE) )
3476                                                 light->envelope = MAX_WORLD_COORD * 8.0f;
3477                                         
3478                                         /* solve distance for linear lights */
3479                                         else if( (light->flags & LIGHT_ATTEN_LINEAR ) )
3480                                                 //% light->envelope = ((intensity / light->falloffTolerance) * linearScale - 1 + radius) / light->fade;
3481                                                 light->envelope = ((intensity * linearScale) - light->falloffTolerance) / light->fade;
3482
3483                                                 /*
3484                                                 add = angle * light->photons * linearScale - (dist * light->fade);
3485                                                 T = (light->photons * linearScale) - (dist * light->fade);
3486                                                 T + (dist * light->fade) = (light->photons * linearScale);
3487                                                 dist * light->fade = (light->photons * linearScale) - T;
3488                                                 dist = ((light->photons * linearScale) - T) / light->fade;
3489                                                 */
3490                                         
3491                                         /* solve for inverse square falloff */
3492                                         else
3493                                                 light->envelope = sqrt( intensity / light->falloffTolerance ) + radius;
3494                                                 
3495                                                 /*
3496                                                 add = light->photons / (dist * dist);
3497                                                 T = light->photons / (dist * dist);
3498                                                 T * (dist * dist) = light->photons;
3499                                                 dist = sqrt( light->photons / T );
3500                                                 */
3501                                 }
3502                                 
3503                                 /* chop radius against pvs */
3504                                 {
3505                                         /* clear bounds */
3506                                         ClearBounds( mins, maxs );
3507                                         
3508                                         /* check all leaves */
3509                                         for( i = 0; i < numBSPLeafs; i++ )
3510                                         {
3511                                                 /* get test leaf */
3512                                                 leaf = &bspLeafs[ i ];
3513                                                 
3514                                                 /* in pvs? */
3515                                                 if( leaf->cluster < 0 )
3516                                                         continue;
3517                                                 if( ClusterVisible( light->cluster, leaf->cluster ) == qfalse ) /* ydnar: thanks Arnout for exposing my stupid error (this never failed before) */
3518                                                         continue;
3519                                                 
3520                                                 /* add this leafs bbox to the bounds */
3521                                                 VectorCopy( leaf->mins, origin );
3522                                                 AddPointToBounds( origin, mins, maxs );
3523                                                 VectorCopy( leaf->maxs, origin );
3524                                                 AddPointToBounds( origin, mins, maxs );
3525                                         }
3526                                         
3527                                         /* test to see if bounds encompass light */
3528                                         for( i = 0; i < 3; i++ )
3529                                         {
3530                                                 if( mins[ i ] > light->origin[ i ] || maxs[ i ] < light->origin[ i ] )
3531                                                 {
3532                                                         //% Sys_Printf( "WARNING: Light PVS bounds (%.0f, %.0f, %.0f) -> (%.0f, %.0f, %.0f)\ndo not encompass light %d (%f, %f, %f)\n",
3533                                                         //%     mins[ 0 ], mins[ 1 ], mins[ 2 ],
3534                                                         //%     maxs[ 0 ], maxs[ 1 ], maxs[ 2 ],
3535                                                         //%     numLights, light->origin[ 0 ], light->origin[ 1 ], light->origin[ 2 ] );
3536                                                         AddPointToBounds( light->origin, mins, maxs );
3537                                                 }
3538                                         }
3539                                         
3540                                         /* chop the bounds by a plane for area lights and spotlights */
3541                                         if( light->type == EMIT_AREA || light->type == EMIT_SPOT )
3542                                                 ChopBounds( mins, maxs, light->origin, light->normal );
3543                                         
3544                                         /* copy bounds */
3545                                         VectorCopy( mins, light->mins );
3546                                         VectorCopy( maxs, light->maxs );
3547                                         
3548                                         /* reflect bounds around light origin */
3549                                         //%     VectorMA( light->origin, -1.0f, origin, origin );
3550                                         VectorScale( light->origin, 2, origin );
3551                                         VectorSubtract( origin, maxs, origin );
3552                                         AddPointToBounds( origin, mins, maxs );
3553                                         //%     VectorMA( light->origin, -1.0f, mins, origin );
3554                                         VectorScale( light->origin, 2, origin );
3555                                         VectorSubtract( origin, mins, origin );
3556                                         AddPointToBounds( origin, mins, maxs );
3557                                          
3558                                         /* calculate spherical bounds */
3559                                         VectorSubtract( maxs, light->origin, dir );
3560                                         radius = (float) VectorLength( dir );
3561                                         
3562                                         /* if this radius is smaller than the envelope, then set the envelope to it */
3563                                         if( radius < light->envelope )
3564                                         {
3565                                                 light->envelope = radius;
3566                                                 //%     Sys_FPrintf( SYS_VRB, "PVS Cull (%d): culled\n", numLights );
3567                                         }
3568                                         //%     else
3569                                         //%             Sys_FPrintf( SYS_VRB, "PVS Cull (%d): failed (%8.0f > %8.0f)\n", numLights, radius, light->envelope );
3570                                 }
3571                                 
3572                                 /* add grid/surface only check */
3573                                 if( forGrid )
3574                                 {
3575                                         if( !(light->flags & LIGHT_GRID) )
3576                                                 light->envelope = 0.0f;
3577                                 }
3578                                 else
3579                                 {
3580                                         if( !(light->flags & LIGHT_SURFACES) )
3581                                                 light->envelope = 0.0f;
3582                                 }
3583                         }
3584                         
3585                         /* culled? */
3586                         if( light->cluster < 0 || light->envelope <= 0.0f )
3587                         {
3588                                 /* debug code */
3589                                 //%     Sys_Printf( "Culling light: Cluster: %d Envelope: %f\n", light->cluster, light->envelope );
3590                                 
3591                                 /* delete the light */
3592                                 numCulledLights++;
3593                                 *owner = light->next;
3594                                 if( light->w != NULL )
3595                                         free( light->w );
3596                                 free( light );
3597                                 continue;
3598                         }
3599                 }
3600                 
3601                 /* square envelope */
3602                 light->envelope2 = (light->envelope * light->envelope);
3603                 
3604                 /* increment light count */
3605                 numLights++;
3606                 
3607                 /* set next light */
3608                 owner = &((**owner).next);
3609         }
3610         
3611         /* bucket sort lights by style */
3612         memset( buckets, 0, sizeof( buckets ) );
3613         light2 = NULL;
3614         for( light = lights; light != NULL; light = light2 )
3615         {
3616                 /* get next light */
3617                 light2 = light->next;
3618                 
3619                 /* filter into correct bucket */
3620                 light->next = buckets[ light->style ];
3621                 buckets[ light->style ] = light;
3622                 
3623                 /* if any styled light is present, automatically set nocollapse */
3624                 if( light->style != LS_NORMAL )
3625                         noCollapse = qtrue;
3626         }
3627         
3628         /* filter back into light list */
3629         lights = NULL;
3630         for( i = 255; i >= 0; i-- )
3631         {
3632                 light2 = NULL;
3633                 for( light = buckets[ i ]; light != NULL; light = light2 )
3634                 {
3635                         light2 = light->next;
3636                         light->next = lights;
3637                         lights = light;
3638                 }
3639         }
3640         
3641         /* emit some statistics */
3642         Sys_Printf( "%9d total lights\n", numLights );
3643         Sys_Printf( "%9d culled lights\n", numCulledLights );
3644 }
3645
3646
3647
3648 /*
3649 CreateTraceLightsForBounds()
3650 creates a list of lights that affect the given bounding box and pvs clusters (bsp leaves)
3651 */
3652
3653 void CreateTraceLightsForBounds( vec3_t mins, vec3_t maxs, vec3_t normal, int numClusters, int *clusters, int flags, trace_t *trace )
3654 {
3655         int                     i;
3656         light_t         *light;
3657         vec3_t          origin, dir, nullVector = { 0.0f, 0.0f, 0.0f };
3658         float           radius, dist, length;
3659         
3660         
3661         /* potential pre-setup  */
3662         if( numLights == 0 )
3663                 SetupEnvelopes( qfalse, fast );
3664         
3665         /* debug code */
3666         //% 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 ] );
3667         
3668         /* allocate the light list */
3669         trace->lights = safe_malloc( sizeof( light_t* ) * (numLights + 1) );
3670         trace->numLights = 0;
3671         
3672         /* calculate spherical bounds */
3673         VectorAdd( mins, maxs, origin );
3674         VectorScale( origin, 0.5f, origin );
3675         VectorSubtract( maxs, origin, dir );
3676         radius = (float) VectorLength( dir );
3677         
3678         /* get length of normal vector */
3679         if( normal != NULL )
3680                 length = VectorLength( normal );
3681         else
3682         {
3683                 normal = nullVector;
3684                 length = 0;
3685         }
3686         
3687         /* test each light and see if it reaches the sphere */
3688         /* note: the attenuation code MUST match LightingAtSample() */
3689         for( light = lights; light; light = light->next )
3690         {
3691                 /* check zero sized envelope */
3692                 if( light->envelope <= 0 )
3693                 {
3694                         lightsEnvelopeCulled++;
3695                         continue;
3696                 }
3697                 
3698                 /* check flags */
3699                 if( !(light->flags & flags) )
3700                         continue;
3701                 
3702                 /* sunlight skips all this nonsense */
3703                 if( light->type != EMIT_SUN )
3704                 {
3705                         /* sun only? */
3706                         if( sunOnly )
3707                                 continue;
3708                         
3709                         /* check against pvs cluster */
3710                         if( numClusters > 0 && clusters != NULL )
3711                         {
3712                                 for( i = 0; i < numClusters; i++ )
3713                                 {
3714                                         if( ClusterVisible( light->cluster, clusters[ i ] ) )
3715                                                 break;
3716                                 }
3717                                 
3718                                 /* fixme! */
3719                                 if( i == numClusters )
3720                                 {
3721                                         lightsClusterCulled++;
3722                                         continue;
3723                                 }
3724                         }
3725                         
3726                         /* if the light's bounding sphere intersects with the bounding sphere then this light needs to be tested */
3727                         VectorSubtract( light->origin, origin, dir );
3728                         dist = VectorLength( dir );
3729                         dist -= light->envelope;
3730                         dist -= radius;
3731                         if( dist > 0 )
3732                         {
3733                                 lightsEnvelopeCulled++;
3734                                 continue;
3735                         }
3736                         
3737                         /* check bounding box against light's pvs envelope (note: this code never eliminated any lights, so disabling it) */
3738                         #if 0
3739                         skip = qfalse;
3740                         for( i = 0; i < 3; i++ )
3741                         {
3742                                 if( mins[ i ] > light->maxs[ i ] || maxs[ i ] < light->mins[ i ] )
3743                                         skip = qtrue;
3744                         }
3745                         if( skip )
3746                         {
3747                                 lightsBoundsCulled++;
3748                                 continue;
3749                         }
3750                         #endif
3751                 }
3752                 
3753                 /* planar surfaces (except twosided surfaces) have a couple more checks */
3754                 if( length > 0.0f && trace->twoSided == qfalse )
3755                 {
3756                         /* lights coplanar with a surface won't light it */
3757                         if( !(light->flags & LIGHT_TWOSIDED) && DotProduct( light->normal, normal ) > 0.999f )
3758                         {
3759                                 lightsPlaneCulled++;
3760                                 continue;
3761                         }
3762                         
3763                         /* check to see if light is behind the plane */
3764                         if( DotProduct( light->origin, normal ) - DotProduct( origin, normal ) < -1.0f )
3765                         {
3766                                 lightsPlaneCulled++;
3767                                 continue;
3768                         }
3769                 }
3770                 
3771                 /* add this light */
3772                 trace->lights[ trace->numLights++ ] = light;
3773         }
3774         
3775         /* make last night null */
3776         trace->lights[ trace->numLights ] = NULL;
3777 }
3778
3779
3780
3781 void FreeTraceLights( trace_t *trace )
3782 {
3783         if( trace->lights != NULL )
3784                 free( trace->lights );
3785 }
3786
3787
3788
3789 /*
3790 CreateTraceLightsForSurface()
3791 creates a list of lights that can potentially affect a drawsurface
3792 */
3793
3794 void CreateTraceLightsForSurface( int num, trace_t *trace )
3795 {
3796         int                                     i;
3797         vec3_t                          mins, maxs, normal;
3798         bspDrawVert_t           *dv;
3799         bspDrawSurface_t        *ds;
3800         surfaceInfo_t           *info;
3801         
3802         
3803         /* dummy check */
3804         if( num < 0 )
3805                 return;
3806         
3807         /* get drawsurface and info */
3808         ds = &bspDrawSurfaces[ num ];
3809         info = &surfaceInfos[ num ];
3810         
3811         /* get the mins/maxs for the dsurf */
3812         ClearBounds( mins, maxs );
3813         VectorCopy( bspDrawVerts[ ds->firstVert ].normal, normal );
3814         for( i = 0; i < ds->numVerts; i++ )
3815         {
3816                 dv = &yDrawVerts[ ds->firstVert + i ];
3817                 AddPointToBounds( dv->xyz, mins, maxs );
3818                 if( !VectorCompare( dv->normal, normal ) )
3819                         VectorClear( normal );
3820         }
3821         
3822         /* create the lights for the bounding box */
3823         CreateTraceLightsForBounds( mins, maxs, normal, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ], LIGHT_SURFACES, trace );
3824 }
3825
3826 /////////////////////////////////////////////////////////////
3827
3828 #define FLOODLIGHT_CONE_ANGLE                   88      /* degrees */
3829 #define FLOODLIGHT_NUM_ANGLE_STEPS              16
3830 #define FLOODLIGHT_NUM_ELEVATION_STEPS  4
3831 #define FLOODLIGHT_NUM_VECTORS                  (FLOODLIGHT_NUM_ANGLE_STEPS * FLOODLIGHT_NUM_ELEVATION_STEPS)
3832
3833 static vec3_t   floodVectors[ FLOODLIGHT_NUM_VECTORS ];
3834 static int              numFloodVectors = 0;
3835
3836 void SetupFloodLight( void )
3837 {
3838         int             i, j;
3839         float   angle, elevation, angleStep, elevationStep;
3840         const char      *value;
3841         double v1,v2,v3,v4,v5;
3842
3843         /* note it */
3844         Sys_FPrintf( SYS_VRB, "--- SetupFloodLight ---\n" );
3845
3846         /* calculate angular steps */
3847         angleStep = DEG2RAD( 360.0f / FLOODLIGHT_NUM_ANGLE_STEPS );
3848         elevationStep = DEG2RAD( FLOODLIGHT_CONE_ANGLE / FLOODLIGHT_NUM_ELEVATION_STEPS );
3849
3850         /* iterate angle */
3851         angle = 0.0f;
3852         for( i = 0, angle = 0.0f; i < FLOODLIGHT_NUM_ANGLE_STEPS; i++, angle += angleStep )
3853         {
3854                 /* iterate elevation */
3855                 for( j = 0, elevation = elevationStep * 0.5f; j < FLOODLIGHT_NUM_ELEVATION_STEPS; j++, elevation += elevationStep )
3856                 {
3857                         floodVectors[ numFloodVectors ][ 0 ] = sin( elevation ) * cos( angle );
3858                         floodVectors[ numFloodVectors ][ 1 ] = sin( elevation ) * sin( angle );
3859                         floodVectors[ numFloodVectors ][ 2 ] = cos( elevation );
3860                         numFloodVectors++;
3861                 }
3862         }
3863
3864         /* emit some statistics */
3865         Sys_FPrintf( SYS_VRB, "%9d numFloodVectors\n", numFloodVectors );
3866
3867       /* floodlight */
3868         value = ValueForKey( &entities[ 0 ], "_floodlight" );
3869
3870         if( value[ 0 ] != '\0' )
3871         {
3872                 v1=v2=v3=0;
3873                 v4=floodlightDistance;
3874                 v5=floodlightIntensity;
3875
3876                 sscanf( value, "%lf %lf %lf %lf %lf", &v1, &v2, &v3, &v4, &v5);
3877
3878                 floodlightRGB[0]=v1;
3879                 floodlightRGB[1]=v2;
3880                 floodlightRGB[2]=v3;
3881
3882                 if (VectorLength(floodlightRGB)==0)
3883                 {
3884                         VectorSet(floodlightRGB,240,240,255);
3885                 }
3886
3887                 if (v4<1) v4=1024;
3888                 if (v5<1) v5=128;
3889
3890                 floodlightDistance=v4;
3891                 floodlightIntensity=v5;
3892
3893                 floodlighty = qtrue;
3894                 Sys_Printf( "FloodLighting enabled via worldspawn _floodlight key.\n" );
3895         }
3896         else
3897         {
3898                 VectorSet(floodlightRGB,240,240,255);
3899                 //floodlighty = qtrue;
3900                 //Sys_Printf( "FloodLighting enabled via worldspawn _floodlight key.\n" );
3901         }
3902         VectorNormalize(floodlightRGB,floodlightRGB);
3903 }
3904
3905 //27 - lighttracer style ambient occlusion light hack.
3906 //Kudos to the dirtmapping author for most of this source.
3907 void FloodLightRawLightmap( int rawLightmapNum )
3908 {
3909         int                                     i, x, y, sx, sy, *cluster;
3910         float                           *origin, *normal, *floodlight, *floodlight2, average, samples;
3911         rawLightmap_t           *lm;
3912         surfaceInfo_t           *info;
3913         trace_t                         trace;
3914
3915         /* bail if this number exceeds the number of raw lightmaps */
3916         if( rawLightmapNum >= numRawLightmaps )
3917                 return;
3918
3919         /* get lightmap */
3920         lm = &rawLightmaps[ rawLightmapNum ];
3921
3922         memset(&trace,0,sizeof(trace_t));
3923         /* setup trace */
3924         trace.testOcclusion = qtrue;
3925         trace.forceSunlight = qfalse;
3926         trace.twoSided = qtrue;
3927         trace.recvShadows = lm->recvShadows;
3928         trace.numSurfaces = lm->numLightSurfaces;
3929         trace.surfaces = &lightSurfaces[ lm->firstLightSurface ];
3930         trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
3931         trace.testAll = qfalse;
3932         trace.distance = 1024;
3933
3934         /* twosided lighting (may or may not be a good idea for lightmapped stuff) */
3935         //trace.twoSided = qfalse;
3936         for( i = 0; i < trace.numSurfaces; i++ )
3937         {
3938                 /* get surface */
3939                 info = &surfaceInfos[ trace.surfaces[ i ] ];
3940
3941                 /* check twosidedness */
3942                 if( info->si->twoSided )
3943                 {
3944                         trace.twoSided = qtrue;
3945                         break;
3946                 }
3947         }
3948
3949         /* gather dirt */
3950         for( y = 0; y < lm->sh; y++ )
3951         {
3952                 for( x = 0; x < lm->sw; x++ )
3953                 {
3954                         /* get luxel */
3955                         cluster = SUPER_CLUSTER( x, y );
3956                         origin = SUPER_ORIGIN( x, y );
3957                         normal = SUPER_NORMAL( x, y );
3958                         floodlight = SUPER_FLOODLIGHT( x, y );
3959
3960                         /* set default dirt */
3961                         *floodlight = 0.0f;
3962
3963                         /* only look at mapped luxels */
3964                         if( *cluster < 0 )
3965                                 continue;
3966
3967                         /* copy to trace */
3968                         trace.cluster = *cluster;
3969                         VectorCopy( origin, trace.origin );
3970                         VectorCopy( normal, trace.normal );
3971
3972
3973
3974                         /* get dirt */
3975                         *floodlight = FloodLightForSample( &trace );
3976                 }
3977         }
3978
3979         /* testing no filtering */
3980         return;
3981
3982         /* filter "dirt" */
3983         for( y = 0; y < lm->sh; y++ )
3984         {
3985                 for( x = 0; x < lm->sw; x++ )
3986                 {
3987                         /* get luxel */
3988                         cluster = SUPER_CLUSTER( x, y );
3989                         floodlight = SUPER_FLOODLIGHT( x, y );
3990
3991                         /* filter dirt by adjacency to unmapped luxels */
3992                         average = *floodlight;
3993                         samples = 1.0f;
3994                         for( sy = (y - 1); sy <= (y + 1); sy++ )
3995                         {
3996                                 if( sy < 0 || sy >= lm->sh )
3997                                         continue;
3998
3999                                 for( sx = (x - 1); sx <= (x + 1); sx++ )
4000                                 {
4001                                         if( sx < 0 || sx >= lm->sw || (sx == x && sy == y) )
4002                                                 continue;
4003
4004                                         /* get neighboring luxel */
4005                                         cluster = SUPER_CLUSTER( sx, sy );
4006                                         floodlight2 = SUPER_FLOODLIGHT( sx, sy );
4007                                         if( *cluster < 0 || *floodlight2 <= 0.0f )
4008                                                 continue;
4009
4010                                         /* add it */
4011                                         average += *floodlight2;
4012                                         samples += 1.0f;
4013                                 }
4014
4015                                 /* bail */
4016                                 if( samples <= 0.0f )
4017                                         break;
4018                         }
4019
4020                         /* bail */
4021                         if( samples <= 0.0f )
4022                                 continue;
4023
4024                         /* scale dirt */
4025                         *floodlight = average / samples;
4026                 }
4027         }
4028 }
4029
4030 /*
4031 FloodLightForSample()
4032 calculates floodlight value for a given sample
4033 once again, kudos to the dirtmapping coder
4034 */
4035 float FloodLightForSample( trace_t *trace )
4036 {
4037         int             i;
4038         float   d;
4039         float   contribution;
4040         int     sub = 0;
4041         float   gatherLight, outLight;
4042         vec3_t  normal, worldUp, myUp, myRt, direction, displacement;
4043         float   dd;
4044         int     vecs = 0;
4045
4046         gatherLight=0;
4047         /* dummy check */
4048         //if( !dirty )
4049         //      return 1.0f;
4050         if( trace == NULL || trace->cluster < 0 )
4051                 return 0.0f;
4052
4053
4054         /* setup */
4055         dd = floodlightDistance;
4056         VectorCopy( trace->normal, normal );
4057
4058         /* check if the normal is aligned to the world-up */
4059         if( normal[ 0 ] == 0.0f && normal[ 1 ] == 0.0f )
4060         {
4061                 if( normal[ 2 ] == 1.0f )
4062                 {
4063                         VectorSet( myRt, 1.0f, 0.0f, 0.0f );
4064                         VectorSet( myUp, 0.0f, 1.0f, 0.0f );
4065                 }
4066                 else if( normal[ 2 ] == -1.0f )
4067                 {
4068                         VectorSet( myRt, -1.0f, 0.0f, 0.0f );
4069                         VectorSet( myUp,  0.0f, 1.0f, 0.0f );
4070                 }
4071         }
4072         else
4073         {
4074                 VectorSet( worldUp, 0.0f, 0.0f, 1.0f );
4075                 CrossProduct( normal, worldUp, myRt );
4076                 VectorNormalize( myRt, myRt );
4077                 CrossProduct( myRt, normal, myUp );
4078                 VectorNormalize( myUp, myUp );
4079         }
4080
4081         /* iterate through ordered vectors */
4082         for( i = 0; i < numFloodVectors; i++ )
4083         {
4084                 if (floodlight_lowquality==qtrue)
4085         {
4086                         if (rand()%10 != 0 ) continue;
4087                 }
4088
4089                 vecs++;
4090
4091                 /* transform vector into tangent space */
4092                 direction[ 0 ] = myRt[ 0 ] * floodVectors[ i ][ 0 ] + myUp[ 0 ] * floodVectors[ i ][ 1 ] + normal[ 0 ] * floodVectors[ i ][ 2 ];
4093                 direction[ 1 ] = myRt[ 1 ] * floodVectors[ i ][ 0 ] + myUp[ 1 ] * floodVectors[ i ][ 1 ] + normal[ 1 ] * floodVectors[ i ][ 2 ];
4094                 direction[ 2 ] = myRt[ 2 ] * floodVectors[ i ][ 0 ] + myUp[ 2 ] * floodVectors[ i ][ 1 ] + normal[ 2 ] * floodVectors[ i ][ 2 ];
4095
4096                 /* set endpoint */
4097                 VectorMA( trace->origin, dd, direction, trace->end );
4098
4099                 //VectorMA( trace->origin, 1, direction, trace->origin );
4100
4101                 SetupTrace( trace );
4102                 /* trace */
4103                 TraceLine( trace );
4104                 contribution=1;
4105
4106                 if (trace->compileFlags & C_SKY )
4107                 {
4108                         contribution=1.0f;
4109                 }
4110                 else if ( trace->opaque )
4111                 {
4112                         VectorSubtract( trace->hit, trace->origin, displacement );
4113                         d=VectorLength( displacement );
4114
4115                         // d=trace->distance;
4116                         //if (d>256) gatherDirt+=1;
4117                         contribution=d/dd;
4118                         if (contribution>1) contribution=1.0f;
4119
4120                         //gatherDirt += 1.0f - ooDepth * VectorLength( displacement );
4121                 }
4122
4123                 gatherLight+=contribution;
4124         }
4125
4126         /* early out */
4127         if( gatherLight <= 0.0f )
4128                 return 0.0f;
4129
4130         sub=vecs;
4131
4132         if (sub<1) sub=1;
4133         gatherLight/=(sub);
4134
4135         outLight=gatherLight;
4136         if( outLight > 1.0f )
4137                 outLight = 1.0f;
4138
4139         /* return to sender */
4140         return outLight;
4141 }
4142