]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/light.c
[q3map2] allow to set arbitrary default light subdivision value
[xonotic/netradiant.git] / tools / quake3 / q3map2 / light.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_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /*
42    CreateSunLight() - ydnar
43    this creates a sun light
44  */
45
46 static void CreateSunLight( sun_t *sun ){
47         int i;
48         float photons, d, angle, elevation, da, de;
49         vec3_t direction;
50         light_t     *light;
51
52
53         /* dummy check */
54         if ( sun == NULL ) {
55                 return;
56         }
57
58         /* fixup */
59         if ( sun->numSamples < 1 ) {
60                 sun->numSamples = 1;
61         }
62
63         /* set photons */
64         photons = sun->photons / sun->numSamples;
65
66         /* create the right number of suns */
67         for ( i = 0; i < sun->numSamples; i++ )
68         {
69                 /* calculate sun direction */
70                 if ( i == 0 ) {
71                         VectorCopy( sun->direction, direction );
72                 }
73                 else
74                 {
75                         /*
76                             sun->direction[ 0 ] = cos( angle ) * cos( elevation );
77                             sun->direction[ 1 ] = sin( angle ) * cos( elevation );
78                             sun->direction[ 2 ] = sin( elevation );
79
80                             xz_dist   = sqrt( x*x + z*z )
81                             latitude  = atan2( xz_dist, y ) * RADIANS
82                             longitude = atan2( x,       z ) * RADIANS
83                          */
84
85                         d = sqrt( sun->direction[ 0 ] * sun->direction[ 0 ] + sun->direction[ 1 ] * sun->direction[ 1 ] );
86                         angle = atan2( sun->direction[ 1 ], sun->direction[ 0 ] );
87                         elevation = atan2( sun->direction[ 2 ], d );
88
89                         /* jitter the angles (loop to keep random sample within sun->deviance steridians) */
90                         do
91                         {
92                                 da = ( Random() * 2.0f - 1.0f ) * sun->deviance;
93                                 de = ( Random() * 2.0f - 1.0f ) * sun->deviance;
94                         }
95                         while ( ( da * da + de * de ) > ( sun->deviance * sun->deviance ) );
96                         angle += da;
97                         elevation += de;
98
99                         /* debug code */
100                         //%     Sys_Printf( "%d: Angle: %3.4f Elevation: %3.3f\n", sun->numSamples, (angle / Q_PI * 180.0f), (elevation / Q_PI * 180.0f) );
101
102                         /* create new vector */
103                         direction[ 0 ] = cos( angle ) * cos( elevation );
104                         direction[ 1 ] = sin( angle ) * cos( elevation );
105                         direction[ 2 ] = sin( elevation );
106                 }
107
108                 /* create a light */
109                 numSunLights++;
110                 light = safe_malloc( sizeof( *light ) );
111                 memset( light, 0, sizeof( *light ) );
112                 light->next = lights;
113                 lights = light;
114
115                 /* initialize the light */
116                 light->flags = LIGHT_SUN_DEFAULT;
117                 light->type = EMIT_SUN;
118                 light->fade = 1.0f;
119                 light->falloffTolerance = falloffTolerance;
120                 light->filterRadius = sun->filterRadius / sun->numSamples;
121                 light->style = noStyles ? LS_NORMAL : sun->style;
122
123                 /* set the light's position out to infinity */
124                 VectorMA( vec3_origin, ( MAX_WORLD_COORD * 8.0f ), direction, light->origin );    /* MAX_WORLD_COORD * 2.0f */
125
126                 /* set the facing to be the inverse of the sun direction */
127                 VectorScale( direction, -1.0, light->normal );
128                 light->dist = DotProduct( light->origin, light->normal );
129
130                 /* set color and photons */
131                 VectorCopy( sun->color, light->color );
132                 light->photons = photons * skyScale;
133         }
134
135         /* another sun? */
136         if ( sun->next != NULL ) {
137                 CreateSunLight( sun->next );
138         }
139 }
140
141
142
143 /*
144    CreateSkyLights() - ydnar
145    simulates sky light with multiple suns
146  */
147
148 static void CreateSkyLights( vec3_t color, float value, int iterations, float filterRadius, int style ){
149         int i, j, numSuns;
150         int angleSteps, elevationSteps;
151         float angle, elevation;
152         float angleStep, elevationStep;
153         sun_t sun;
154
155
156         /* dummy check */
157         if ( value <= 0.0f || iterations < 2 ) {
158                 return;
159         }
160
161         /* basic sun setup */
162         VectorCopy( color, sun.color );
163         sun.deviance = 0.0f;
164         sun.filterRadius = filterRadius;
165         sun.numSamples = 1;
166         sun.style = noStyles ? LS_NORMAL : style;
167         sun.next = NULL;
168
169         /* setup */
170         elevationSteps = iterations - 1;
171         angleSteps = elevationSteps * 4;
172         angle = 0.0f;
173         elevationStep = DEG2RAD( 90.0f / iterations );  /* skip elevation 0 */
174         angleStep = DEG2RAD( 360.0f / angleSteps );
175
176         /* calc individual sun brightness */
177         numSuns = angleSteps * elevationSteps + 1;
178         sun.photons = value / numSuns;
179
180         /* iterate elevation */
181         elevation = elevationStep * 0.5f;
182         angle = 0.0f;
183         for ( i = 0, elevation = elevationStep * 0.5f; i < elevationSteps; i++ )
184         {
185                 /* iterate angle */
186                 for ( j = 0; j < angleSteps; j++ )
187                 {
188                         /* create sun */
189                         sun.direction[ 0 ] = cos( angle ) * cos( elevation );
190                         sun.direction[ 1 ] = sin( angle ) * cos( elevation );
191                         sun.direction[ 2 ] = sin( elevation );
192                         CreateSunLight( &sun );
193
194                         /* move */
195                         angle += angleStep;
196                 }
197
198                 /* move */
199                 elevation += elevationStep;
200                 angle += angleStep / elevationSteps;
201         }
202
203         /* create vertical sun */
204         VectorSet( sun.direction, 0.0f, 0.0f, 1.0f );
205         CreateSunLight( &sun );
206
207         /* short circuit */
208         return;
209 }
210
211
212
213 /*
214    CreateEntityLights()
215    creates lights from light entities
216  */
217
218 void CreateEntityLights( void ){
219         int i, j;
220         light_t         *light, *light2;
221         entity_t        *e, *e2;
222         const char      *name;
223         const char      *target;
224         vec3_t dest;
225         const char      *_color;
226         float intensity, scale, deviance, filterRadius;
227         int spawnflags, flags, numSamples;
228         qboolean junior;
229
230
231         /* go throught entity list and find lights */
232         for ( i = 0; i < numEntities; i++ )
233         {
234                 /* get entity */
235                 e = &entities[ i ];
236                 name = ValueForKey( e, "classname" );
237
238                 /* ydnar: check for lightJunior */
239                 if ( Q_strncasecmp( name, "lightJunior", 11 ) == 0 ) {
240                         junior = qtrue;
241                 }
242                 else if ( Q_strncasecmp( name, "light", 5 ) == 0 ) {
243                         junior = qfalse;
244                 }
245                 else{
246                         continue;
247                 }
248
249                 /* lights with target names (and therefore styles) are only parsed from BSP */
250                 target = ValueForKey( e, "targetname" );
251                 if ( target[ 0 ] != '\0' && i >= numBSPEntities ) {
252                         continue;
253                 }
254
255                 /* create a light */
256                 numPointLights++;
257                 light = safe_malloc( sizeof( *light ) );
258                 memset( light, 0, sizeof( *light ) );
259                 light->next = lights;
260                 lights = light;
261
262                 /* handle spawnflags */
263                 spawnflags = IntForKey( e, "spawnflags" );
264
265                 /* ydnar: quake 3+ light behavior */
266                 if ( wolfLight == qfalse ) {
267                         /* set default flags */
268                         flags = LIGHT_Q3A_DEFAULT;
269
270                         /* linear attenuation? */
271                         if ( spawnflags & 1 ) {
272                                 flags |= LIGHT_ATTEN_LINEAR;
273                                 flags &= ~LIGHT_ATTEN_ANGLE;
274                         }
275
276                         /* no angle attenuate? */
277                         if ( spawnflags & 2 ) {
278                                 flags &= ~LIGHT_ATTEN_ANGLE;
279                         }
280                 }
281
282                 /* ydnar: wolf light behavior */
283                 else
284                 {
285                         /* set default flags */
286                         flags = LIGHT_WOLF_DEFAULT;
287
288                         /* inverse distance squared attenuation? */
289                         if ( spawnflags & 1 ) {
290                                 flags &= ~LIGHT_ATTEN_LINEAR;
291                                 flags |= LIGHT_ATTEN_ANGLE;
292                         }
293
294                         /* angle attenuate? */
295                         if ( spawnflags & 2 ) {
296                                 flags |= LIGHT_ATTEN_ANGLE;
297                         }
298                 }
299
300                 /* other flags (borrowed from wolf) */
301
302                 /* wolf dark light? */
303                 if ( ( spawnflags & 4 ) || ( spawnflags & 8 ) ) {
304                         flags |= LIGHT_DARK;
305                 }
306
307                 /* nogrid? */
308                 if ( spawnflags & 16 ) {
309                         flags &= ~LIGHT_GRID;
310                 }
311
312                 /* junior? */
313                 if ( junior ) {
314                         flags |= LIGHT_GRID;
315                         flags &= ~LIGHT_SURFACES;
316                 }
317
318                 /* vortex: unnormalized? */
319                 if ( spawnflags & 32 ) {
320                         flags |= LIGHT_UNNORMALIZED;
321                 }
322
323                 /* vortex: distance atten? */
324                 if ( spawnflags & 64 ) {
325                         flags |= LIGHT_ATTEN_DISTANCE;
326                 }
327
328                 /* store the flags */
329                 light->flags = flags;
330
331                 /* ydnar: set fade key (from wolf) */
332                 light->fade = 1.0f;
333                 if ( light->flags & LIGHT_ATTEN_LINEAR ) {
334                         light->fade = FloatForKey( e, "fade" );
335                         if ( light->fade == 0.0f ) {
336                                 light->fade = 1.0f;
337                         }
338                 }
339
340                 /* ydnar: set angle scaling (from vlight) */
341                 light->angleScale = FloatForKey( e, "_anglescale" );
342                 if ( light->angleScale != 0.0f ) {
343                         light->flags |= LIGHT_ATTEN_ANGLE;
344                 }
345
346                 /* set origin */
347                 GetVectorForKey( e, "origin", light->origin );
348                 light->style = IntForKey( e, "_style" );
349                 if ( light->style == LS_NORMAL ) {
350                         light->style = IntForKey( e, "style" );
351                 }
352                 if ( light->style < LS_NORMAL || light->style >= LS_NONE ) {
353                         Error( "Invalid lightstyle (%d) on entity %d", light->style, i );
354                 }
355
356                 if ( light->style != LS_NORMAL ) {
357                         Sys_FPrintf( SYS_WRN, "WARNING: Styled light found targeting %s\n **", target );
358                 }
359
360                 /* set light intensity */
361                 intensity = FloatForKey( e, "_light" );
362                 if ( intensity == 0.0f ) {
363                         intensity = FloatForKey( e, "light" );
364                 }
365                 if ( intensity == 0.0f ) {
366                         intensity = 300.0f;
367                 }
368
369                 /* ydnar: set light scale (sof2) */
370                 scale = FloatForKey( e, "scale" );
371                 if ( scale == 0.0f ) {
372                         scale = 1.0f;
373                 }
374                 intensity *= scale;
375
376                 /* ydnar: get deviance and samples */
377                 deviance = FloatForKey( e, "_deviance" );
378                 if ( deviance == 0.0f ) {
379                         deviance = FloatForKey( e, "_deviation" );
380                 }
381                 if ( deviance == 0.0f ) {
382                         deviance = FloatForKey( e, "_jitter" );
383                 }
384                 numSamples = IntForKey( e, "_samples" );
385                 if ( deviance < 0.0f || numSamples < 1 ) {
386                         deviance = 0.0f;
387                         numSamples = 1;
388                 }
389                 intensity /= numSamples;
390
391                 /* ydnar: get filter radius */
392                 filterRadius = FloatForKey( e, "_filterradius" );
393                 if ( filterRadius == 0.0f ) {
394                         filterRadius = FloatForKey( e, "_filteradius" );
395                 }
396                 if ( filterRadius == 0.0f ) {
397                         filterRadius = FloatForKey( e, "_filter" );
398                 }
399                 if ( filterRadius < 0.0f ) {
400                         filterRadius = 0.0f;
401                 }
402                 light->filterRadius = filterRadius;
403
404                 /* set light color */
405                 _color = ValueForKey( e, "_color" );
406                 if ( _color && _color[ 0 ] ) {
407                         sscanf( _color, "%f %f %f", &light->color[ 0 ], &light->color[ 1 ], &light->color[ 2 ] );
408                         if ( colorsRGB ) {
409                                 light->color[0] = Image_LinearFloatFromsRGBFloat( light->color[0] );
410                                 light->color[1] = Image_LinearFloatFromsRGBFloat( light->color[1] );
411                                 light->color[2] = Image_LinearFloatFromsRGBFloat( light->color[2] );
412                         }
413                         if ( !( light->flags & LIGHT_UNNORMALIZED ) ) {
414                                 ColorNormalize( light->color, light->color );
415                         }
416                 }
417                 else{
418                         light->color[ 0 ] = light->color[ 1 ] = light->color[ 2 ] = 1.0f;
419                 }
420
421                 light->extraDist = FloatForKey( e, "_extradist" );
422                 if ( light->extraDist == 0.0f ) {
423                         light->extraDist = extraDist;
424                 }
425
426                 light->photons = intensity;
427
428                 light->type = EMIT_POINT;
429
430                 /* set falloff threshold */
431                 light->falloffTolerance = falloffTolerance / numSamples;
432
433                 /* lights with a target will be spotlights */
434                 target = ValueForKey( e, "target" );
435                 if ( target[ 0 ] ) {
436                         float radius;
437                         float dist;
438                         sun_t sun;
439                         const char  *_sun;
440
441
442                         /* get target */
443                         e2 = FindTargetEntity( target );
444                         if ( e2 == NULL ) {
445                                 Sys_FPrintf( SYS_WRN, "WARNING: light at (%i %i %i) has missing target\n",
446                                                         (int) light->origin[ 0 ], (int) light->origin[ 1 ], (int) light->origin[ 2 ] );
447                                 light->photons *= pointScale;
448                         }
449                         else
450                         {
451                                 /* not a point light */
452                                 numPointLights--;
453                                 numSpotLights++;
454
455                                 /* make a spotlight */
456                                 GetVectorForKey( e2, "origin", dest );
457                                 VectorSubtract( dest, light->origin, light->normal );
458                                 dist = VectorNormalize( light->normal, light->normal );
459                                 radius = FloatForKey( e, "radius" );
460                                 if ( !radius ) {
461                                         radius = 64;
462                                 }
463                                 if ( !dist ) {
464                                         dist = 64;
465                                 }
466                                 light->radiusByDist = ( radius + 16 ) / dist;
467                                 light->type = EMIT_SPOT;
468
469                                 /* ydnar: wolf mods: spotlights always use nonlinear + angle attenuation */
470                                 light->flags &= ~LIGHT_ATTEN_LINEAR;
471                                 light->flags |= LIGHT_ATTEN_ANGLE;
472                                 light->fade = 1.0f;
473
474                                 /* ydnar: is this a sun? */
475                                 _sun = ValueForKey( e, "_sun" );
476                                 if ( _sun[ 0 ] == '1' ) {
477                                         /* not a spot light */
478                                         numSpotLights--;
479
480                                         /* unlink this light */
481                                         lights = light->next;
482
483                                         /* make a sun */
484                                         VectorScale( light->normal, -1.0f, sun.direction );
485                                         VectorCopy( light->color, sun.color );
486                                         sun.photons = intensity;
487                                         sun.deviance = deviance / 180.0f * Q_PI;
488                                         sun.numSamples = numSamples;
489                                         sun.style = noStyles ? LS_NORMAL : light->style;
490                                         sun.next = NULL;
491
492                                         /* make a sun light */
493                                         CreateSunLight( &sun );
494
495                                         /* free original light */
496                                         free( light );
497                                         light = NULL;
498
499                                         /* skip the rest of this love story */
500                                         continue;
501                                 }
502                                 else
503                                 {
504                                         light->photons *= spotScale;
505                                 }
506                         }
507                 }
508                 else{
509                         light->photons *= pointScale;
510                 }
511
512                 /* jitter the light */
513                 for ( j = 1; j < numSamples; j++ )
514                 {
515                         /* create a light */
516                         light2 = safe_malloc( sizeof( *light ) );
517                         memcpy( light2, light, sizeof( *light ) );
518                         light2->next = lights;
519                         lights = light2;
520
521                         /* add to counts */
522                         if ( light->type == EMIT_SPOT ) {
523                                 numSpotLights++;
524                         }
525                         else{
526                                 numPointLights++;
527                         }
528
529                         /* jitter it */
530                         light2->origin[ 0 ] = light->origin[ 0 ] + ( Random() * 2.0f - 1.0f ) * deviance;
531                         light2->origin[ 1 ] = light->origin[ 1 ] + ( Random() * 2.0f - 1.0f ) * deviance;
532                         light2->origin[ 2 ] = light->origin[ 2 ] + ( Random() * 2.0f - 1.0f ) * deviance;
533                 }
534         }
535 }
536
537
538
539 /*
540    CreateSurfaceLights() - ydnar
541    this hijacks the radiosity code to generate surface lights for first pass
542  */
543
544 #define APPROX_BOUNCE   1.0f
545
546 void CreateSurfaceLights( void ){
547         int i;
548         bspDrawSurface_t    *ds;
549         surfaceInfo_t       *info;
550         shaderInfo_t        *si;
551         light_t             *light;
552         float subdivide;
553         vec3_t origin;
554         clipWork_t cw;
555         const char          *nss;
556
557
558         /* get sun shader supressor */
559         nss = ValueForKey( &entities[ 0 ], "_noshadersun" );
560
561         /* walk the list of surfaces */
562         for ( i = 0; i < numBSPDrawSurfaces; i++ )
563         {
564                 /* get surface and other bits */
565                 ds = &bspDrawSurfaces[ i ];
566                 info = &surfaceInfos[ i ];
567                 si = info->si;
568
569                 /* sunlight? */
570                 if ( si->sun != NULL && nss[ 0 ] != '1' ) {
571                         Sys_FPrintf( SYS_VRB, "Sun: %s\n", si->shader );
572                         CreateSunLight( si->sun );
573                         si->sun = NULL; /* FIXME: leak! */
574                 }
575
576                 /* sky light? */
577                 if ( si->skyLightValue > 0.0f ) {
578                         Sys_FPrintf( SYS_VRB, "Sky: %s\n", si->shader );
579                         CreateSkyLights( si->color, si->skyLightValue, si->skyLightIterations, si->lightFilterRadius, si->lightStyle );
580                         si->skyLightValue = 0.0f;   /* FIXME: hack! */
581                 }
582
583                 /* try to early out */
584                 if ( si->value <= 0 ) {
585                         continue;
586                 }
587
588                 /* autosprite shaders become point lights */
589                 if ( si->autosprite ) {
590                         /* create an average xyz */
591                         VectorAdd( info->mins, info->maxs, origin );
592                         VectorScale( origin, 0.5f, origin );
593
594                         /* create a light */
595                         light = safe_malloc( sizeof( *light ) );
596                         memset( light, 0, sizeof( *light ) );
597                         light->next = lights;
598                         lights = light;
599
600                         /* set it up */
601                         light->flags = LIGHT_Q3A_DEFAULT;
602                         light->type = EMIT_POINT;
603                         light->photons = si->value * pointScale;
604                         light->fade = 1.0f;
605                         light->si = si;
606                         VectorCopy( origin, light->origin );
607                         VectorCopy( si->color, light->color );
608                         light->falloffTolerance = falloffTolerance;
609                         light->style = si->lightStyle;
610
611                         /* add to point light count and continue */
612                         numPointLights++;
613                         continue;
614                 }
615
616                 /* get subdivision amount */
617                 if ( si->lightSubdivide > 0 ) {
618                         subdivide = si->lightSubdivide;
619                 }
620                 else{
621                         subdivide = defaultLightSubdivide;
622                 }
623
624                 /* switch on type */
625                 switch ( ds->surfaceType )
626                 {
627                 case MST_PLANAR:
628                 case MST_TRIANGLE_SOUP:
629                         RadLightForTriangles( i, 0, info->lm, si, APPROX_BOUNCE, subdivide, &cw );
630                         break;
631
632                 case MST_PATCH:
633                         RadLightForPatch( i, 0, info->lm, si, APPROX_BOUNCE, subdivide, &cw );
634                         break;
635
636                 default:
637                         break;
638                 }
639         }
640 }
641
642
643
644 /*
645    SetEntityOrigins()
646    find the offset values for inline models
647  */
648
649 void SetEntityOrigins( void ){
650         int i, j, k, f;
651         entity_t            *e;
652         vec3_t origin;
653         const char          *key;
654         int modelnum;
655         bspModel_t          *dm;
656         bspDrawSurface_t    *ds;
657
658
659         /* ydnar: copy drawverts into private storage for nefarious purposes */
660         yDrawVerts = safe_malloc( numBSPDrawVerts * sizeof( bspDrawVert_t ) );
661         memcpy( yDrawVerts, bspDrawVerts, numBSPDrawVerts * sizeof( bspDrawVert_t ) );
662
663         /* set the entity origins */
664         for ( i = 0; i < numEntities; i++ )
665         {
666                 /* get entity and model */
667                 e = &entities[ i ];
668                 key = ValueForKey( e, "model" );
669                 if ( key[ 0 ] != '*' ) {
670                         continue;
671                 }
672                 modelnum = atoi( key + 1 );
673                 dm = &bspModels[ modelnum ];
674
675                 /* get entity origin */
676                 key = ValueForKey( e, "origin" );
677                 if ( key[ 0 ] == '\0' ) {
678                         continue;
679                 }
680                 GetVectorForKey( e, "origin", origin );
681
682                 /* set origin for all surfaces for this model */
683                 for ( j = 0; j < dm->numBSPSurfaces; j++ )
684                 {
685                         /* get drawsurf */
686                         ds = &bspDrawSurfaces[ dm->firstBSPSurface + j ];
687
688                         /* set its verts */
689                         for ( k = 0; k < ds->numVerts; k++ )
690                         {
691                                 f = ds->firstVert + k;
692                                 VectorAdd( origin, bspDrawVerts[ f ].xyz, yDrawVerts[ f ].xyz );
693                         }
694                 }
695         }
696 }
697
698
699
700 /*
701    PointToPolygonFormFactor()
702    calculates the area over a point/normal hemisphere a winding covers
703    ydnar: fixme: there has to be a faster way to calculate this
704    without the expensive per-vert sqrts and transcendental functions
705    ydnar 2002-09-30: added -faster switch because only 19% deviance > 10%
706    between this and the approximation
707  */
708
709 #define ONE_OVER_2PI    0.159154942f    //% (1.0f / (2.0f * 3.141592657f))
710
711 float PointToPolygonFormFactor( const vec3_t point, const vec3_t normal, const winding_t *w ){
712         vec3_t triVector, triNormal;
713         int i, j;
714         vec3_t dirs[ MAX_POINTS_ON_WINDING ];
715         float total;
716         float dot, angle, facing;
717
718
719         /* this is expensive */
720         for ( i = 0; i < w->numpoints; i++ )
721         {
722                 VectorSubtract( w->p[ i ], point, dirs[ i ] );
723                 VectorNormalize( dirs[ i ], dirs[ i ] );
724         }
725
726         /* duplicate first vertex to avoid mod operation */
727         VectorCopy( dirs[ 0 ], dirs[ i ] );
728
729         /* calculcate relative area */
730         total = 0.0f;
731         for ( i = 0; i < w->numpoints; i++ )
732         {
733                 /* get a triangle */
734                 j = i + 1;
735                 dot = DotProduct( dirs[ i ], dirs[ j ] );
736
737                 /* roundoff can cause slight creep, which gives an IND from acos */
738                 if ( dot > 1.0f ) {
739                         dot = 1.0f;
740                 }
741                 else if ( dot < -1.0f ) {
742                         dot = -1.0f;
743                 }
744
745                 /* get the angle */
746                 angle = acos( dot );
747
748                 CrossProduct( dirs[ i ], dirs[ j ], triVector );
749                 if ( VectorNormalize( triVector, triNormal ) < 0.0001f ) {
750                         continue;
751                 }
752
753                 facing = DotProduct( normal, triNormal );
754                 total += facing * angle;
755
756                 /* ydnar: this was throwing too many errors with radiosity + crappy maps. ignoring it. */
757                 if ( total > 6.3f || total < -6.3f ) {
758                         return 0.0f;
759                 }
760         }
761
762         /* now in the range of 0 to 1 over the entire incoming hemisphere */
763         //%     total /= (2.0f * 3.141592657f);
764         total *= ONE_OVER_2PI;
765         return total;
766 }
767
768
769
770 /*
771    LightContributionTosample()
772    determines the amount of light reaching a sample (luxel or vertex) from a given light
773  */
774
775 int LightContributionToSample( trace_t *trace ){
776         light_t         *light;
777         float angle;
778         float add;
779         float dist;
780         float addDeluxe = 0.0f, addDeluxeBounceScale = 0.25f;
781         qboolean angledDeluxe = qtrue;
782         float colorBrightness;
783         qboolean doAddDeluxe = qtrue;
784
785         /* get light */
786         light = trace->light;
787
788         /* clear color */
789         trace->forceSubsampling = 0.0f; /* to make sure */
790         VectorClear( trace->color );
791         VectorClear( trace->colorNoShadow );
792         VectorClear( trace->directionContribution );
793
794         colorBrightness = RGBTOGRAY( light->color ) * ( 1.0f / 255.0f );
795
796         /* ydnar: early out */
797         if ( !( light->flags & LIGHT_SURFACES ) || light->envelope <= 0.0f ) {
798                 return 0;
799         }
800
801         /* do some culling checks */
802         if ( light->type != EMIT_SUN ) {
803                 /* MrE: if the light is behind the surface */
804                 if ( trace->twoSided == qfalse ) {
805                         if ( DotProduct( light->origin, trace->normal ) - DotProduct( trace->origin, trace->normal ) < 0.0f ) {
806                                 return 0;
807                         }
808                 }
809
810                 /* ydnar: test pvs */
811                 if ( !ClusterVisible( trace->cluster, light->cluster ) ) {
812                         return 0;
813                 }
814         }
815
816         /* exact point to polygon form factor */
817         if ( light->type == EMIT_AREA ) {
818                 float factor;
819                 float d;
820                 vec3_t pushedOrigin;
821
822                 /* project sample point into light plane */
823                 d = DotProduct( trace->origin, light->normal ) - light->dist;
824                 if ( d < 3.0f ) {
825                         /* sample point behind plane? */
826                         if ( !( light->flags & LIGHT_TWOSIDED ) && d < -1.0f ) {
827                                 return 0;
828                         }
829
830                         /* sample plane coincident? */
831                         if ( d > -3.0f && DotProduct( trace->normal, light->normal ) > 0.9f ) {
832                                 return 0;
833                         }
834                 }
835
836                 /* nudge the point so that it is clearly forward of the light */
837                 /* so that surfaces meeting a light emitter don't get black edges */
838                 if ( d > -8.0f && d < 8.0f ) {
839                         VectorMA( trace->origin, ( 8.0f - d ), light->normal, pushedOrigin );
840                 }
841                 else{
842                         VectorCopy( trace->origin, pushedOrigin );
843                 }
844
845                 /* get direction and distance */
846                 VectorCopy( light->origin, trace->end );
847                 dist = SetupTrace( trace );
848                 if ( dist >= light->envelope ) {
849                         return 0;
850                 }
851
852                 /* ptpff approximation */
853                 if ( faster ) {
854                         /* angle attenuation */
855                         angle = DotProduct( trace->normal, trace->direction );
856
857                         /* twosided lighting */
858                         if ( trace->twoSided && angle < 0 ) {
859                                 angle = -angle;
860
861                                 /* no deluxemap contribution from "other side" light */
862                                 doAddDeluxe = qfalse;
863                         }
864
865                         /* attenuate */
866                         angle *= -DotProduct( light->normal, trace->direction );
867                         if ( angle == 0.0f ) {
868                                 return 0;
869                         }
870                         else if ( angle < 0.0f &&
871                                           ( trace->twoSided || ( light->flags & LIGHT_TWOSIDED ) ) ) {
872                                 angle = -angle;
873
874                                 /* no deluxemap contribution from "other side" light */
875                                 doAddDeluxe = qfalse;
876                         }
877
878                         /* clamp the distance to prevent super hot spots */
879                         dist = sqrt( dist * dist + light->extraDist * light->extraDist );
880                         if ( dist < 16.0f ) {
881                                 dist = 16.0f;
882                         }
883
884                         add = light->photons / ( dist * dist ) * angle;
885
886                         if ( deluxemap ) {
887                                 if ( angledDeluxe ) {
888                                         addDeluxe = light->photons / ( dist * dist ) * angle;
889                                 }
890                                 else{
891                                         addDeluxe = light->photons / ( dist * dist );
892                                 }
893                         }
894                 }
895                 else
896                 {
897                         /* calculate the contribution */
898                         factor = PointToPolygonFormFactor( pushedOrigin, trace->normal, light->w );
899                         if ( factor == 0.0f ) {
900                                 return 0;
901                         }
902                         else if ( factor < 0.0f ) {
903                                 /* twosided lighting */
904                                 if ( trace->twoSided || ( light->flags & LIGHT_TWOSIDED ) ) {
905                                         factor = -factor;
906
907                                         /* push light origin to other side of the plane */
908                                         VectorMA( light->origin, -2.0f, light->normal, trace->end );
909                                         dist = SetupTrace( trace );
910                                         if ( dist >= light->envelope ) {
911                                                 return 0;
912                                         }
913
914                                         /* no deluxemap contribution from "other side" light */
915                                         doAddDeluxe = qfalse;
916                                 }
917                                 else{
918                                         return 0;
919                                 }
920                         }
921
922                         /* also don't deluxe if the direction is on the wrong side */
923                         if ( DotProduct( trace->normal, trace->direction ) < 0 ) {
924                                 /* no deluxemap contribution from "other side" light */
925                                 doAddDeluxe = qfalse;
926                         }
927
928                         /* ydnar: moved to here */
929                         add = factor * light->add;
930
931                         if ( deluxemap ) {
932                                 addDeluxe = add;
933                         }
934                 }
935         }
936
937         /* point/spot lights */
938         else if ( light->type == EMIT_POINT || light->type == EMIT_SPOT ) {
939                 /* get direction and distance */
940                 VectorCopy( light->origin, trace->end );
941                 dist = SetupTrace( trace );
942                 if ( dist >= light->envelope ) {
943                         return 0;
944                 }
945
946                 /* clamp the distance to prevent super hot spots */
947                 dist = sqrt( dist * dist + light->extraDist * light->extraDist );
948                 if ( dist < 16.0f ) {
949                         dist = 16.0f;
950                 }
951
952                 /* angle attenuation */
953                 if ( light->flags & LIGHT_ATTEN_ANGLE ) {
954                         /* standard Lambert attenuation */
955                         float dot = DotProduct( trace->normal, trace->direction );
956
957                         /* twosided lighting */
958                         if ( trace->twoSided && dot < 0 ) {
959                                 dot = -dot;
960
961                                 /* no deluxemap contribution from "other side" light */
962                                 doAddDeluxe = qfalse;
963                         }
964
965                         /* jal: optional half Lambert attenuation (http://developer.valvesoftware.com/wiki/Half_Lambert) */
966                         if ( lightAngleHL ) {
967                                 if ( dot > 0.001f ) { // skip coplanar
968                                         if ( dot > 1.0f ) {
969                                                 dot = 1.0f;
970                                         }
971                                         dot = ( dot * 0.5f ) + 0.5f;
972                                         dot *= dot;
973                                 }
974                                 else{
975                                         dot = 0;
976                                 }
977                         }
978
979                         angle = dot;
980                 }
981                 else{
982                         angle = 1.0f;
983                 }
984
985                 if ( light->angleScale != 0.0f ) {
986                         angle /= light->angleScale;
987                         if ( angle > 1.0f ) {
988                                 angle = 1.0f;
989                         }
990                 }
991
992                 /* attenuate */
993                 if ( light->flags & LIGHT_ATTEN_LINEAR ) {
994                         add = angle * light->photons * linearScale - ( dist * light->fade );
995                         if ( add < 0.0f ) {
996                                 add = 0.0f;
997                         }
998
999                         if ( deluxemap ) {
1000                                 if ( angledDeluxe ) {
1001                                         addDeluxe = angle * light->photons * linearScale - ( dist * light->fade );
1002                                 }
1003                                 else{
1004                                         addDeluxe = light->photons * linearScale - ( dist * light->fade );
1005                                 }
1006
1007                                 if ( addDeluxe < 0.0f ) {
1008                                         addDeluxe = 0.0f;
1009                                 }
1010                         }
1011                 }
1012                 else
1013                 {
1014                         add = ( light->photons / ( dist * dist ) ) * angle;
1015                         if ( add < 0.0f ) {
1016                                 add = 0.0f;
1017                         }
1018
1019                         if ( deluxemap ) {
1020                                 if ( angledDeluxe ) {
1021                                         addDeluxe = ( light->photons / ( dist * dist ) ) * angle;
1022                                 }
1023                                 else{
1024                                         addDeluxe = ( light->photons / ( dist * dist ) );
1025                                 }
1026                         }
1027
1028                         if ( addDeluxe < 0.0f ) {
1029                                 addDeluxe = 0.0f;
1030                         }
1031                 }
1032
1033                 /* handle spotlights */
1034                 if ( light->type == EMIT_SPOT ) {
1035                         float distByNormal, radiusAtDist, sampleRadius;
1036                         vec3_t pointAtDist, distToSample;
1037
1038                         /* do cone calculation */
1039                         distByNormal = -DotProduct( trace->displacement, light->normal );
1040                         if ( distByNormal < 0.0f ) {
1041                                 return 0;
1042                         }
1043                         VectorMA( light->origin, distByNormal, light->normal, pointAtDist );
1044                         radiusAtDist = light->radiusByDist * distByNormal;
1045                         VectorSubtract( trace->origin, pointAtDist, distToSample );
1046                         sampleRadius = VectorLength( distToSample );
1047
1048                         /* outside the cone */
1049                         if ( sampleRadius >= radiusAtDist ) {
1050                                 return 0;
1051                         }
1052
1053                         /* attenuate */
1054                         if ( sampleRadius > ( radiusAtDist - 32.0f ) ) {
1055                                 add *= ( ( radiusAtDist - sampleRadius ) / 32.0f );
1056                                 if ( add < 0.0f ) {
1057                                         add = 0.0f;
1058                                 }
1059
1060                                 addDeluxe *= ( ( radiusAtDist - sampleRadius ) / 32.0f );
1061
1062                                 if ( addDeluxe < 0.0f ) {
1063                                         addDeluxe = 0.0f;
1064                                 }
1065                         }
1066                 }
1067         }
1068
1069         /* ydnar: sunlight */
1070         else if ( light->type == EMIT_SUN ) {
1071                 /* get origin and direction */
1072                 VectorAdd( trace->origin, light->origin, trace->end );
1073                 dist = SetupTrace( trace );
1074
1075                 /* angle attenuation */
1076                 if ( light->flags & LIGHT_ATTEN_ANGLE ) {
1077                         /* standard Lambert attenuation */
1078                         float dot = DotProduct( trace->normal, trace->direction );
1079
1080                         /* twosided lighting */
1081                         if ( trace->twoSided && dot < 0 ) {
1082                                 dot = -dot;
1083
1084                                 /* no deluxemap contribution from "other side" light */
1085                                 doAddDeluxe = qfalse;
1086                         }
1087
1088                         /* jal: optional half Lambert attenuation (http://developer.valvesoftware.com/wiki/Half_Lambert) */
1089                         if ( lightAngleHL ) {
1090                                 if ( dot > 0.001f ) { // skip coplanar
1091                                         if ( dot > 1.0f ) {
1092                                                 dot = 1.0f;
1093                                         }
1094                                         dot = ( dot * 0.5f ) + 0.5f;
1095                                         dot *= dot;
1096                                 }
1097                                 else{
1098                                         dot = 0;
1099                                 }
1100                         }
1101
1102                         angle = dot;
1103                 }
1104                 else{
1105                         angle = 1.0f;
1106                 }
1107
1108                 /* attenuate */
1109                 add = light->photons * angle;
1110
1111                 if ( deluxemap ) {
1112                         if ( angledDeluxe ) {
1113                                 addDeluxe = light->photons * angle;
1114                         }
1115                         else{
1116                                 addDeluxe = light->photons;
1117                         }
1118
1119                         if ( addDeluxe < 0.0f ) {
1120                                 addDeluxe = 0.0f;
1121                         }
1122                 }
1123
1124                 if ( add <= 0.0f ) {
1125                         return 0;
1126                 }
1127
1128                 /* VorteX: set noShadow color */
1129                 VectorScale( light->color, add, trace->colorNoShadow );
1130
1131                 addDeluxe *= colorBrightness;
1132
1133                 if ( bouncing ) {
1134                         addDeluxe *= addDeluxeBounceScale;
1135                         if ( addDeluxe < 0.00390625f ) {
1136                                 addDeluxe = 0.00390625f;
1137                         }
1138                 }
1139
1140                 VectorScale( trace->direction, addDeluxe, trace->directionContribution );
1141
1142                 /* setup trace */
1143                 trace->testAll = qtrue;
1144                 VectorScale( light->color, add, trace->color );
1145
1146                 /* trace to point */
1147                 if ( trace->testOcclusion && !trace->forceSunlight ) {
1148                         /* trace */
1149                         TraceLine( trace );
1150                         trace->forceSubsampling *= add;
1151                         if ( !( trace->compileFlags & C_SKY ) || trace->opaque ) {
1152                                 VectorClear( trace->color );
1153                                 VectorClear( trace->directionContribution );
1154
1155                                 return -1;
1156                         }
1157                 }
1158
1159                 /* return to sender */
1160                 return 1;
1161         }
1162         else{
1163                 Error( "Light of undefined type!" );
1164         }
1165
1166         /* VorteX: set noShadow color */
1167         VectorScale( light->color, add, trace->colorNoShadow );
1168
1169         /* ydnar: changed to a variable number */
1170         if ( add <= 0.0f || ( add <= light->falloffTolerance && ( light->flags & LIGHT_FAST_ACTUAL ) ) ) {
1171                 return 0;
1172         }
1173
1174         addDeluxe *= colorBrightness;
1175
1176         /* hack land: scale down the radiosity contribution to light directionality.
1177            Deluxemaps fusion many light directions into one. In a rtl process all lights
1178            would contribute individually to the bump map, so several light sources together
1179            would make it more directional (example: a yellow and red lights received from
1180            opposing sides would light one side in red and the other in blue, adding
1181            the effect of 2 directions applied. In the deluxemapping case, this 2 lights would
1182            neutralize each other making it look like having no direction.
1183            Same thing happens with radiosity. In deluxemapping case the radiosity contribution
1184            is modifying the direction applied from directional lights, making it go closer and closer
1185            to the surface normal the bigger is the amount of radiosity received.
1186            So, for preserving the directional lights contributions, we scale down the radiosity
1187            contribution. It's a hack, but there's a reason behind it */
1188         if ( bouncing ) {
1189                 addDeluxe *= addDeluxeBounceScale;
1190                 /* better NOT increase it beyond the original value
1191                    if( addDeluxe < 0.00390625f )
1192                     addDeluxe = 0.00390625f;
1193                  */
1194         }
1195
1196         if ( doAddDeluxe ) {
1197                 VectorScale( trace->direction, addDeluxe, trace->directionContribution );
1198         }
1199
1200         /* setup trace */
1201         trace->testAll = qfalse;
1202         VectorScale( light->color, add, trace->color );
1203
1204         /* raytrace */
1205         TraceLine( trace );
1206         trace->forceSubsampling *= add;
1207         if ( trace->passSolid || trace->opaque ) {
1208                 VectorClear( trace->color );
1209                 VectorClear( trace->directionContribution );
1210
1211                 return -1;
1212         }
1213
1214         /* return to sender */
1215         return 1;
1216 }
1217
1218
1219
1220 /*
1221    LightingAtSample()
1222    determines the amount of light reaching a sample (luxel or vertex)
1223  */
1224
1225 void LightingAtSample( trace_t *trace, byte styles[ MAX_LIGHTMAPS ], vec3_t colors[ MAX_LIGHTMAPS ] ){
1226         int i, lightmapNum;
1227
1228
1229         /* clear colors */
1230         for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
1231                 VectorClear( colors[ lightmapNum ] );
1232
1233         /* ydnar: normalmap */
1234         if ( normalmap ) {
1235                 colors[ 0 ][ 0 ] = ( trace->normal[ 0 ] + 1.0f ) * 127.5f;
1236                 colors[ 0 ][ 1 ] = ( trace->normal[ 1 ] + 1.0f ) * 127.5f;
1237                 colors[ 0 ][ 2 ] = ( trace->normal[ 2 ] + 1.0f ) * 127.5f;
1238                 return;
1239         }
1240
1241         /* ydnar: don't bounce ambient all the time */
1242         if ( !bouncing ) {
1243                 VectorCopy( ambientColor, colors[ 0 ] );
1244         }
1245
1246         /* ydnar: trace to all the list of lights pre-stored in tw */
1247         for ( i = 0; i < trace->numLights && trace->lights[ i ] != NULL; i++ )
1248         {
1249                 /* set light */
1250                 trace->light = trace->lights[ i ];
1251
1252                 /* style check */
1253                 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
1254                 {
1255                         if ( styles[ lightmapNum ] == trace->light->style ||
1256                                  styles[ lightmapNum ] == LS_NONE ) {
1257                                 break;
1258                         }
1259                 }
1260
1261                 /* max of MAX_LIGHTMAPS (4) styles allowed to hit a sample */
1262                 if ( lightmapNum >= MAX_LIGHTMAPS ) {
1263                         continue;
1264                 }
1265
1266                 /* sample light */
1267                 LightContributionToSample( trace );
1268                 if ( trace->color[ 0 ] == 0.0f && trace->color[ 1 ] == 0.0f && trace->color[ 2 ] == 0.0f ) {
1269                         continue;
1270                 }
1271
1272                 /* handle negative light */
1273                 if ( trace->light->flags & LIGHT_NEGATIVE ) {
1274                         VectorScale( trace->color, -1.0f, trace->color );
1275                 }
1276
1277                 /* set style */
1278                 styles[ lightmapNum ] = trace->light->style;
1279
1280                 /* add it */
1281                 VectorAdd( colors[ lightmapNum ], trace->color, colors[ lightmapNum ] );
1282
1283                 /* cheap mode */
1284                 if ( cheap &&
1285                          colors[ 0 ][ 0 ] >= 255.0f &&
1286                          colors[ 0 ][ 1 ] >= 255.0f &&
1287                          colors[ 0 ][ 2 ] >= 255.0f ) {
1288                         break;
1289                 }
1290         }
1291 }
1292
1293
1294
1295 /*
1296    LightContributionToPoint()
1297    for a given light, how much light/color reaches a given point in space (with no facing)
1298    note: this is similar to LightContributionToSample() but optimized for omnidirectional sampling
1299  */
1300
1301 int LightContributionToPoint( trace_t *trace ){
1302         light_t     *light;
1303         float add, dist;
1304
1305
1306         /* get light */
1307         light = trace->light;
1308
1309         /* clear color */
1310         VectorClear( trace->color );
1311
1312         /* ydnar: early out */
1313         if ( !( light->flags & LIGHT_GRID ) || light->envelope <= 0.0f ) {
1314                 return qfalse;
1315         }
1316
1317         /* is this a sun? */
1318         if ( light->type != EMIT_SUN ) {
1319                 /* sun only? */
1320                 if ( sunOnly ) {
1321                         return qfalse;
1322                 }
1323
1324                 /* test pvs */
1325                 if ( !ClusterVisible( trace->cluster, light->cluster ) ) {
1326                         return qfalse;
1327                 }
1328         }
1329
1330         /* ydnar: check origin against light's pvs envelope */
1331         if ( trace->origin[ 0 ] > light->maxs[ 0 ] || trace->origin[ 0 ] < light->mins[ 0 ] ||
1332                  trace->origin[ 1 ] > light->maxs[ 1 ] || trace->origin[ 1 ] < light->mins[ 1 ] ||
1333                  trace->origin[ 2 ] > light->maxs[ 2 ] || trace->origin[ 2 ] < light->mins[ 2 ] ) {
1334                 gridBoundsCulled++;
1335                 return qfalse;
1336         }
1337
1338         /* set light origin */
1339         if ( light->type == EMIT_SUN ) {
1340                 VectorAdd( trace->origin, light->origin, trace->end );
1341         }
1342         else{
1343                 VectorCopy( light->origin, trace->end );
1344         }
1345
1346         /* set direction */
1347         dist = SetupTrace( trace );
1348
1349         /* test envelope */
1350         if ( dist > light->envelope ) {
1351                 gridEnvelopeCulled++;
1352                 return qfalse;
1353         }
1354
1355         /* ptpff approximation */
1356         if ( light->type == EMIT_AREA && faster ) {
1357                 /* clamp the distance to prevent super hot spots */
1358                 dist = sqrt( dist * dist + light->extraDist * light->extraDist );
1359                 if ( dist < 16.0f ) {
1360                         dist = 16.0f;
1361                 }
1362
1363                 /* attenuate */
1364                 add = light->photons / ( dist * dist );
1365         }
1366
1367         /* exact point to polygon form factor */
1368         else if ( light->type == EMIT_AREA ) {
1369                 float factor, d;
1370                 vec3_t pushedOrigin;
1371
1372
1373                 /* see if the point is behind the light */
1374                 d = DotProduct( trace->origin, light->normal ) - light->dist;
1375                 if ( !( light->flags & LIGHT_TWOSIDED ) && d < -1.0f ) {
1376                         return qfalse;
1377                 }
1378
1379                 /* nudge the point so that it is clearly forward of the light */
1380                 /* so that surfaces meeting a light emiter don't get black edges */
1381                 if ( d > -8.0f && d < 8.0f ) {
1382                         VectorMA( trace->origin, ( 8.0f - d ), light->normal, pushedOrigin );
1383                 }
1384                 else{
1385                         VectorCopy( trace->origin, pushedOrigin );
1386                 }
1387
1388                 /* calculate the contribution (ydnar 2002-10-21: [bug 642] bad normal calc) */
1389                 factor = PointToPolygonFormFactor( pushedOrigin, trace->direction, light->w );
1390                 if ( factor == 0.0f ) {
1391                         return qfalse;
1392                 }
1393                 else if ( factor < 0.0f ) {
1394                         if ( light->flags & LIGHT_TWOSIDED ) {
1395                                 factor = -factor;
1396                         }
1397                         else{
1398                                 return qfalse;
1399                         }
1400                 }
1401
1402                 /* ydnar: moved to here */
1403                 add = factor * light->add;
1404         }
1405
1406         /* point/spot lights */
1407         else if ( light->type == EMIT_POINT || light->type == EMIT_SPOT ) {
1408                 /* clamp the distance to prevent super hot spots */
1409                 dist = sqrt( dist * dist + light->extraDist * light->extraDist );
1410                 if ( dist < 16.0f ) {
1411                         dist = 16.0f;
1412                 }
1413
1414                 /* attenuate */
1415                 if ( light->flags & LIGHT_ATTEN_LINEAR ) {
1416                         add = light->photons * linearScale - ( dist * light->fade );
1417                         if ( add < 0.0f ) {
1418                                 add = 0.0f;
1419                         }
1420                 }
1421                 else{
1422                         add = light->photons / ( dist * dist );
1423                 }
1424
1425                 /* handle spotlights */
1426                 if ( light->type == EMIT_SPOT ) {
1427                         float distByNormal, radiusAtDist, sampleRadius;
1428                         vec3_t pointAtDist, distToSample;
1429
1430
1431                         /* do cone calculation */
1432                         distByNormal = -DotProduct( trace->displacement, light->normal );
1433                         if ( distByNormal < 0.0f ) {
1434                                 return qfalse;
1435                         }
1436                         VectorMA( light->origin, distByNormal, light->normal, pointAtDist );
1437                         radiusAtDist = light->radiusByDist * distByNormal;
1438                         VectorSubtract( trace->origin, pointAtDist, distToSample );
1439                         sampleRadius = VectorLength( distToSample );
1440
1441                         /* outside the cone */
1442                         if ( sampleRadius >= radiusAtDist ) {
1443                                 return qfalse;
1444                         }
1445
1446                         /* attenuate */
1447                         if ( sampleRadius > ( radiusAtDist - 32.0f ) ) {
1448                                 add *= ( ( radiusAtDist - sampleRadius ) / 32.0f );
1449                         }
1450                 }
1451         }
1452
1453         /* ydnar: sunlight */
1454         else if ( light->type == EMIT_SUN ) {
1455                 /* attenuate */
1456                 add = light->photons;
1457                 if ( add <= 0.0f ) {
1458                         return qfalse;
1459                 }
1460
1461                 /* setup trace */
1462                 trace->testAll = qtrue;
1463                 VectorScale( light->color, add, trace->color );
1464
1465                 /* trace to point */
1466                 if ( trace->testOcclusion && !trace->forceSunlight ) {
1467                         /* trace */
1468                         TraceLine( trace );
1469                         if ( !( trace->compileFlags & C_SKY ) || trace->opaque ) {
1470                                 VectorClear( trace->color );
1471                                 return -1;
1472                         }
1473                 }
1474
1475                 /* return to sender */
1476                 return qtrue;
1477         }
1478
1479         /* unknown light type */
1480         else{
1481                 return qfalse;
1482         }
1483
1484         /* ydnar: changed to a variable number */
1485         if ( add <= 0.0f || ( add <= light->falloffTolerance && ( light->flags & LIGHT_FAST_ACTUAL ) ) ) {
1486                 return qfalse;
1487         }
1488
1489         /* setup trace */
1490         trace->testAll = qfalse;
1491         VectorScale( light->color, add, trace->color );
1492
1493         /* trace */
1494         TraceLine( trace );
1495         if ( trace->passSolid ) {
1496                 VectorClear( trace->color );
1497                 return qfalse;
1498         }
1499
1500         /* we have a valid sample */
1501         return qtrue;
1502 }
1503
1504
1505
1506 /*
1507    TraceGrid()
1508    grid samples are for quickly determining the lighting
1509    of dynamically placed entities in the world
1510  */
1511
1512 #define MAX_CONTRIBUTIONS   32768
1513
1514 typedef struct
1515 {
1516         vec3_t dir;
1517         vec3_t color;
1518         vec3_t ambient;
1519         int style;
1520 }
1521 contribution_t;
1522
1523 void TraceGrid( int num ){
1524         int i, j, x, y, z, mod, numCon, numStyles;
1525         float d, step;
1526         vec3_t baseOrigin, cheapColor, color, thisdir;
1527         rawGridPoint_t          *gp;
1528         bspGridPoint_t          *bgp;
1529         contribution_t contributions[ MAX_CONTRIBUTIONS ];
1530         trace_t trace;
1531
1532         /* get grid points */
1533         gp = &rawGridPoints[ num ];
1534         bgp = &bspGridPoints[ num ];
1535
1536         /* get grid origin */
1537         mod = num;
1538         z = mod / ( gridBounds[ 0 ] * gridBounds[ 1 ] );
1539         mod -= z * ( gridBounds[ 0 ] * gridBounds[ 1 ] );
1540         y = mod / gridBounds[ 0 ];
1541         mod -= y * gridBounds[ 0 ];
1542         x = mod;
1543
1544         trace.origin[ 0 ] = gridMins[ 0 ] + x * gridSize[ 0 ];
1545         trace.origin[ 1 ] = gridMins[ 1 ] + y * gridSize[ 1 ];
1546         trace.origin[ 2 ] = gridMins[ 2 ] + z * gridSize[ 2 ];
1547
1548         /* set inhibit sphere */
1549         if ( gridSize[ 0 ] > gridSize[ 1 ] && gridSize[ 0 ] > gridSize[ 2 ] ) {
1550                 trace.inhibitRadius = gridSize[ 0 ] * 0.5f;
1551         }
1552         else if ( gridSize[ 1 ] > gridSize[ 0 ] && gridSize[ 1 ] > gridSize[ 2 ] ) {
1553                 trace.inhibitRadius = gridSize[ 1 ] * 0.5f;
1554         }
1555         else{
1556                 trace.inhibitRadius = gridSize[ 2 ] * 0.5f;
1557         }
1558
1559         /* find point cluster */
1560         trace.cluster = ClusterForPointExt( trace.origin, GRID_EPSILON );
1561         if ( trace.cluster < 0 ) {
1562                 /* try to nudge the origin around to find a valid point */
1563                 VectorCopy( trace.origin, baseOrigin );
1564                 for ( step = 0; ( step += 0.005 ) <= 1.0; )
1565                 {
1566                         VectorCopy( baseOrigin, trace.origin );
1567                         trace.origin[ 0 ] += step * ( Random() - 0.5 ) * gridSize[0];
1568                         trace.origin[ 1 ] += step * ( Random() - 0.5 ) * gridSize[1];
1569                         trace.origin[ 2 ] += step * ( Random() - 0.5 ) * gridSize[2];
1570
1571                         /* ydnar: changed to find cluster num */
1572                         trace.cluster = ClusterForPointExt( trace.origin, VERTEX_EPSILON );
1573                         if ( trace.cluster >= 0 ) {
1574                                 break;
1575                         }
1576                 }
1577
1578                 /* can't find a valid point at all */
1579                 if ( step > 1.0 ) {
1580                         return;
1581                 }
1582         }
1583
1584         /* setup trace */
1585         trace.testOcclusion = !noTrace;
1586         trace.forceSunlight = qfalse;
1587         trace.recvShadows = WORLDSPAWN_RECV_SHADOWS;
1588         trace.numSurfaces = 0;
1589         trace.surfaces = NULL;
1590         trace.numLights = 0;
1591         trace.lights = NULL;
1592
1593         /* clear */
1594         numCon = 0;
1595         VectorClear( cheapColor );
1596
1597         /* trace to all the lights, find the major light direction, and divide the
1598            total light between that along the direction and the remaining in the ambient */
1599         for ( trace.light = lights; trace.light != NULL; trace.light = trace.light->next )
1600         {
1601                 float addSize;
1602
1603
1604                 /* sample light */
1605                 if ( !LightContributionToPoint( &trace ) ) {
1606                         continue;
1607                 }
1608
1609                 /* handle negative light */
1610                 if ( trace.light->flags & LIGHT_NEGATIVE ) {
1611                         VectorScale( trace.color, -1.0f, trace.color );
1612                 }
1613
1614                 /* add a contribution */
1615                 VectorCopy( trace.color, contributions[ numCon ].color );
1616                 VectorCopy( trace.direction, contributions[ numCon ].dir );
1617                 VectorClear( contributions[ numCon ].ambient );
1618                 contributions[ numCon ].style = trace.light->style;
1619                 numCon++;
1620
1621                 /* push average direction around */
1622                 addSize = VectorLength( trace.color );
1623                 VectorMA( gp->dir, addSize, trace.direction, gp->dir );
1624
1625                 /* stop after a while */
1626                 if ( numCon >= ( MAX_CONTRIBUTIONS - 1 ) ) {
1627                         break;
1628                 }
1629
1630                 /* ydnar: cheap mode */
1631                 VectorAdd( cheapColor, trace.color, cheapColor );
1632                 if ( cheapgrid && cheapColor[ 0 ] >= 255.0f && cheapColor[ 1 ] >= 255.0f && cheapColor[ 2 ] >= 255.0f ) {
1633                         break;
1634                 }
1635         }
1636
1637         /////// Floodlighting for point //////////////////
1638         //do our floodlight ambient occlusion loop, and add a single contribution based on the brightest dir
1639         if ( floodlighty ) {
1640                 int k;
1641                 float addSize, f;
1642                 vec3_t dir = { 0, 0, 1 };
1643                 float ambientFrac = 0.25f;
1644
1645                 trace.testOcclusion = qtrue;
1646                 trace.forceSunlight = qfalse;
1647                 trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
1648                 trace.testAll = qtrue;
1649
1650                 for ( k = 0; k < 2; k++ )
1651                 {
1652                         if ( k == 0 ) { // upper hemisphere
1653                                 trace.normal[0] = 0;
1654                                 trace.normal[1] = 0;
1655                                 trace.normal[2] = 1;
1656                         }
1657                         else //lower hemisphere
1658                         {
1659                                 trace.normal[0] = 0;
1660                                 trace.normal[1] = 0;
1661                                 trace.normal[2] = -1;
1662                         }
1663
1664                         f = FloodLightForSample( &trace, floodlightDistance, floodlight_lowquality );
1665
1666                         /* add a fraction as pure ambient, half as top-down direction */
1667                         contributions[ numCon ].color[0] = floodlightRGB[0] * floodlightIntensity * f * ( 1.0f - ambientFrac );
1668                         contributions[ numCon ].color[1] = floodlightRGB[1] * floodlightIntensity * f * ( 1.0f - ambientFrac );
1669                         contributions[ numCon ].color[2] = floodlightRGB[2] * floodlightIntensity * f * ( 1.0f - ambientFrac );
1670
1671                         contributions[ numCon ].ambient[0] = floodlightRGB[0] * floodlightIntensity * f * ambientFrac;
1672                         contributions[ numCon ].ambient[1] = floodlightRGB[1] * floodlightIntensity * f * ambientFrac;
1673                         contributions[ numCon ].ambient[2] = floodlightRGB[2] * floodlightIntensity * f * ambientFrac;
1674
1675                         contributions[ numCon ].dir[0] = dir[0];
1676                         contributions[ numCon ].dir[1] = dir[1];
1677                         contributions[ numCon ].dir[2] = dir[2];
1678
1679                         contributions[ numCon ].style = 0;
1680
1681                         /* push average direction around */
1682                         addSize = VectorLength( contributions[ numCon ].color );
1683                         VectorMA( gp->dir, addSize, dir, gp->dir );
1684
1685                         numCon++;
1686                 }
1687         }
1688         /////////////////////
1689
1690         /* normalize to get primary light direction */
1691         VectorNormalize( gp->dir, thisdir );
1692
1693         /* now that we have identified the primary light direction,
1694            go back and separate all the light into directed and ambient */
1695
1696         numStyles = 1;
1697         for ( i = 0; i < numCon; i++ )
1698         {
1699                 /* get relative directed strength */
1700                 d = DotProduct( contributions[ i ].dir, thisdir );
1701                 /* we map 1 to gridDirectionality, and 0 to gridAmbientDirectionality */
1702                 d = gridAmbientDirectionality + d * ( gridDirectionality - gridAmbientDirectionality );
1703                 if ( d < 0.0f ) {
1704                         d = 0.0f;
1705                 }
1706
1707                 /* find appropriate style */
1708                 for ( j = 0; j < numStyles; j++ )
1709                 {
1710                         if ( gp->styles[ j ] == contributions[ i ].style ) {
1711                                 break;
1712                         }
1713                 }
1714
1715                 /* style not found? */
1716                 if ( j >= numStyles ) {
1717                         /* add a new style */
1718                         if ( numStyles < MAX_LIGHTMAPS ) {
1719                                 gp->styles[ numStyles ] = contributions[ i ].style;
1720                                 bgp->styles[ numStyles ] = contributions[ i ].style;
1721                                 numStyles++;
1722                                 //%     Sys_Printf( "(%d, %d) ", num, contributions[ i ].style );
1723                         }
1724
1725                         /* fallback */
1726                         else{
1727                                 j = 0;
1728                         }
1729                 }
1730
1731                 /* add the directed color */
1732                 VectorMA( gp->directed[ j ], d, contributions[ i ].color, gp->directed[ j ] );
1733
1734                 /* ambient light will be at 1/4 the value of directed light */
1735                 /* (ydnar: nuke this in favor of more dramatic lighting?) */
1736                 /* (PM: how about actually making it work? d=1 when it got here for single lights/sun :P */
1737 //              d = 0.25f;
1738                 /* (Hobbes: always setting it to .25 is hardly any better) */
1739                 d = 0.25f * ( 1.0f - d );
1740                 VectorMA( gp->ambient[ j ], d, contributions[ i ].color, gp->ambient[ j ] );
1741
1742                 VectorAdd( gp->ambient[ j ], contributions[ i ].ambient, gp->ambient[ j ] );
1743
1744 /*
1745  * div0:
1746  * the total light average = ambient value + 0.25 * sum of all directional values
1747  * we can also get the total light average as 0.25 * the sum of all contributions
1748  *
1749  * 0.25 * sum(contribution_i) == ambient + 0.25 * sum(d_i contribution_i)
1750  *
1751  * THIS YIELDS:
1752  * ambient == 0.25 * sum((1 - d_i) contribution_i)
1753  *
1754  * So, 0.25f * (1.0f - d) IS RIGHT. If you want to tune it, tune d BEFORE.
1755  */
1756         }
1757
1758
1759         /* store off sample */
1760         for ( i = 0; i < MAX_LIGHTMAPS; i++ )
1761         {
1762 #if 0
1763                 /* do some fudging to keep the ambient from being too low (2003-07-05: 0.25 -> 0.125) */
1764                 if ( !bouncing ) {
1765                         VectorMA( gp->ambient[ i ], 0.125f, gp->directed[ i ], gp->ambient[ i ] );
1766                 }
1767 #endif
1768
1769                 /* set minimum light and copy off to bytes */
1770                 VectorCopy( gp->ambient[ i ], color );
1771                 for ( j = 0; j < 3; j++ )
1772                         if ( color[ j ] < minGridLight[ j ] ) {
1773                                 color[ j ] = minGridLight[ j ];
1774                         }
1775
1776                 /* vortex: apply gridscale and gridambientscale here */
1777                 ColorToBytes( color, bgp->ambient[ i ], gridScale * gridAmbientScale );
1778                 ColorToBytes( gp->directed[ i ], bgp->directed[ i ], gridScale );
1779         }
1780
1781         /* debug code */
1782         #if 0
1783         //%     Sys_FPrintf( SYS_VRB, "%10d %10d %10d ", &gp->ambient[ 0 ][ 0 ], &gp->ambient[ 0 ][ 1 ], &gp->ambient[ 0 ][ 2 ] );
1784         Sys_FPrintf( SYS_VRB, "%9d Amb: (%03.1f %03.1f %03.1f) Dir: (%03.1f %03.1f %03.1f)\n",
1785                                  num,
1786                                  gp->ambient[ 0 ][ 0 ], gp->ambient[ 0 ][ 1 ], gp->ambient[ 0 ][ 2 ],
1787                                  gp->directed[ 0 ][ 0 ], gp->directed[ 0 ][ 1 ], gp->directed[ 0 ][ 2 ] );
1788         #endif
1789
1790         /* store direction */
1791         NormalToLatLong( thisdir, bgp->latLong );
1792 }
1793
1794
1795
1796 /*
1797    SetupGrid()
1798    calculates the size of the lightgrid and allocates memory
1799  */
1800
1801 void SetupGrid( void ){
1802         int i, j;
1803         vec3_t maxs, oldGridSize;
1804         const char  *value;
1805         char temp[ 64 ];
1806
1807
1808         /* don't do this if not grid lighting */
1809         if ( noGridLighting ) {
1810                 return;
1811         }
1812
1813         /* ydnar: set grid size */
1814         value = ValueForKey( &entities[ 0 ], "gridsize" );
1815         if ( value[ 0 ] != '\0' ) {
1816                 sscanf( value, "%f %f %f", &gridSize[ 0 ], &gridSize[ 1 ], &gridSize[ 2 ] );
1817         }
1818
1819         /* quantize it */
1820         VectorCopy( gridSize, oldGridSize );
1821         for ( i = 0; i < 3; i++ )
1822                 gridSize[ i ] = gridSize[ i ] >= 8.0f ? floor( gridSize[ i ] ) : 8.0f;
1823
1824         /* ydnar: increase gridSize until grid count is smaller than max allowed */
1825         numRawGridPoints = MAX_MAP_LIGHTGRID + 1;
1826         j = 0;
1827         while ( numRawGridPoints > MAX_MAP_LIGHTGRID )
1828         {
1829                 /* get world bounds */
1830                 for ( i = 0; i < 3; i++ )
1831                 {
1832                         gridMins[ i ] = gridSize[ i ] * ceil( bspModels[ 0 ].mins[ i ] / gridSize[ i ] );
1833                         maxs[ i ] = gridSize[ i ] * floor( bspModels[ 0 ].maxs[ i ] / gridSize[ i ] );
1834                         gridBounds[ i ] = ( maxs[ i ] - gridMins[ i ] ) / gridSize[ i ] + 1;
1835                 }
1836
1837                 /* set grid size */
1838                 numRawGridPoints = gridBounds[ 0 ] * gridBounds[ 1 ] * gridBounds[ 2 ];
1839
1840                 /* increase grid size a bit */
1841                 if ( numRawGridPoints > MAX_MAP_LIGHTGRID ) {
1842                         gridSize[ j++ % 3 ] += 16.0f;
1843                 }
1844         }
1845
1846         /* print it */
1847         Sys_Printf( "Grid size = { %1.0f, %1.0f, %1.0f }\n", gridSize[ 0 ], gridSize[ 1 ], gridSize[ 2 ] );
1848
1849         /* different? */
1850         if ( !VectorCompare( gridSize, oldGridSize ) ) {
1851                 sprintf( temp, "%.0f %.0f %.0f", gridSize[ 0 ], gridSize[ 1 ], gridSize[ 2 ] );
1852                 SetKeyValue( &entities[ 0 ], "gridsize", (const char*) temp );
1853                 Sys_FPrintf( SYS_VRB, "Storing adjusted grid size\n" );
1854         }
1855
1856         /* 2nd variable. fixme: is this silly? */
1857         numBSPGridPoints = numRawGridPoints;
1858
1859         /* allocate lightgrid */
1860         rawGridPoints = safe_malloc( numRawGridPoints * sizeof( *rawGridPoints ) );
1861         memset( rawGridPoints, 0, numRawGridPoints * sizeof( *rawGridPoints ) );
1862
1863         if ( bspGridPoints != NULL ) {
1864                 free( bspGridPoints );
1865         }
1866         bspGridPoints = safe_malloc( numBSPGridPoints * sizeof( *bspGridPoints ) );
1867         memset( bspGridPoints, 0, numBSPGridPoints * sizeof( *bspGridPoints ) );
1868
1869         /* clear lightgrid */
1870         for ( i = 0; i < numRawGridPoints; i++ )
1871         {
1872                 VectorCopy( ambientColor, rawGridPoints[ i ].ambient[ j ] );
1873                 rawGridPoints[ i ].styles[ 0 ] = LS_NORMAL;
1874                 bspGridPoints[ i ].styles[ 0 ] = LS_NORMAL;
1875                 for ( j = 1; j < MAX_LIGHTMAPS; j++ )
1876                 {
1877                         rawGridPoints[ i ].styles[ j ] = LS_NONE;
1878                         bspGridPoints[ i ].styles[ j ] = LS_NONE;
1879                 }
1880         }
1881
1882         /* note it */
1883         Sys_Printf( "%9d grid points\n", numRawGridPoints );
1884 }
1885
1886
1887
1888 /*
1889    LightWorld()
1890    does what it says...
1891  */
1892
1893 void LightWorld( const char *BSPFilePath, qboolean fastAllocate ){
1894         vec3_t color;
1895         float f;
1896         int b, bt;
1897         qboolean minVertex, minGrid;
1898         const char  *value;
1899
1900
1901         /* ydnar: smooth normals */
1902         if ( shade ) {
1903                 Sys_Printf( "--- SmoothNormals ---\n" );
1904                 SmoothNormals();
1905         }
1906
1907         /* determine the number of grid points */
1908         Sys_Printf( "--- SetupGrid ---\n" );
1909         SetupGrid();
1910
1911         /* find the optional minimum lighting values */
1912         GetVectorForKey( &entities[ 0 ], "_color", color );
1913         if ( VectorLength( color ) == 0.0f ) {
1914                 VectorSet( color, 1.0, 1.0, 1.0 );
1915         }
1916         if ( colorsRGB ) {
1917                 color[0] = Image_LinearFloatFromsRGBFloat( color[0] );
1918                 color[1] = Image_LinearFloatFromsRGBFloat( color[1] );
1919                 color[2] = Image_LinearFloatFromsRGBFloat( color[2] );
1920         }
1921
1922         /* ambient */
1923         f = FloatForKey( &entities[ 0 ], "_ambient" );
1924         if ( f == 0.0f ) {
1925                 f = FloatForKey( &entities[ 0 ], "ambient" );
1926         }
1927         VectorScale( color, f, ambientColor );
1928
1929         /* minvertexlight */
1930         minVertex = qfalse;
1931         value = ValueForKey( &entities[ 0 ], "_minvertexlight" );
1932         if ( value[ 0 ] != '\0' ) {
1933                 minVertex = qtrue;
1934                 f = atof( value );
1935                 VectorScale( color, f, minVertexLight );
1936         }
1937
1938         /* mingridlight */
1939         minGrid = qfalse;
1940         value = ValueForKey( &entities[ 0 ], "_mingridlight" );
1941         if ( value[ 0 ] != '\0' ) {
1942                 minGrid = qtrue;
1943                 f = atof( value );
1944                 VectorScale( color, f, minGridLight );
1945         }
1946
1947         /* minlight */
1948         value = ValueForKey( &entities[ 0 ], "_minlight" );
1949         if ( value[ 0 ] != '\0' ) {
1950                 f = atof( value );
1951                 VectorScale( color, f, minLight );
1952                 if ( minVertex == qfalse ) {
1953                         VectorScale( color, f, minVertexLight );
1954                 }
1955                 if ( minGrid == qfalse ) {
1956                         VectorScale( color, f, minGridLight );
1957                 }
1958         }
1959
1960         /* create world lights */
1961         Sys_FPrintf( SYS_VRB, "--- CreateLights ---\n" );
1962         CreateEntityLights();
1963         CreateSurfaceLights();
1964         Sys_Printf( "%9d point lights\n", numPointLights );
1965         Sys_Printf( "%9d spotlights\n", numSpotLights );
1966         Sys_Printf( "%9d diffuse (area) lights\n", numDiffuseLights );
1967         Sys_Printf( "%9d sun/sky lights\n", numSunLights );
1968
1969         /* calculate lightgrid */
1970         if ( !noGridLighting ) {
1971                 /* ydnar: set up light envelopes */
1972                 SetupEnvelopes( qtrue, fastgrid );
1973
1974                 Sys_Printf( "--- TraceGrid ---\n" );
1975                 inGrid = qtrue;
1976                 RunThreadsOnIndividual( numRawGridPoints, qtrue, TraceGrid );
1977                 inGrid = qfalse;
1978                 Sys_Printf( "%d x %d x %d = %d grid\n",
1979                                         gridBounds[ 0 ], gridBounds[ 1 ], gridBounds[ 2 ], numBSPGridPoints );
1980
1981                 /* ydnar: emit statistics on light culling */
1982                 Sys_FPrintf( SYS_VRB, "%9d grid points envelope culled\n", gridEnvelopeCulled );
1983                 Sys_FPrintf( SYS_VRB, "%9d grid points bounds culled\n", gridBoundsCulled );
1984         }
1985
1986         /* slight optimization to remove a sqrt */
1987         subdivideThreshold *= subdivideThreshold;
1988
1989         /* map the world luxels */
1990         Sys_Printf( "--- MapRawLightmap ---\n" );
1991         RunThreadsOnIndividual( numRawLightmaps, qtrue, MapRawLightmap );
1992         Sys_Printf( "%9d luxels\n", numLuxels );
1993         Sys_Printf( "%9d luxels mapped\n", numLuxelsMapped );
1994         Sys_Printf( "%9d luxels occluded\n", numLuxelsOccluded );
1995
1996         /* dirty them up */
1997         if ( dirty ) {
1998                 Sys_Printf( "--- DirtyRawLightmap ---\n" );
1999                 RunThreadsOnIndividual( numRawLightmaps, qtrue, DirtyRawLightmap );
2000         }
2001
2002         /* floodlight pass */
2003         FloodlightRawLightmaps();
2004
2005         /* ydnar: set up light envelopes */
2006         SetupEnvelopes( qfalse, fast );
2007
2008         /* light up my world */
2009         lightsPlaneCulled = 0;
2010         lightsEnvelopeCulled = 0;
2011         lightsBoundsCulled = 0;
2012         lightsClusterCulled = 0;
2013
2014         Sys_Printf( "--- IlluminateRawLightmap ---\n" );
2015         RunThreadsOnIndividual( numRawLightmaps, qtrue, IlluminateRawLightmap );
2016         Sys_Printf( "%9d luxels illuminated\n", numLuxelsIlluminated );
2017
2018         StitchSurfaceLightmaps();
2019
2020         Sys_Printf( "--- IlluminateVertexes ---\n" );
2021         RunThreadsOnIndividual( numBSPDrawSurfaces, qtrue, IlluminateVertexes );
2022         Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
2023
2024         /* ydnar: emit statistics on light culling */
2025         Sys_FPrintf( SYS_VRB, "%9d lights plane culled\n", lightsPlaneCulled );
2026         Sys_FPrintf( SYS_VRB, "%9d lights envelope culled\n", lightsEnvelopeCulled );
2027         Sys_FPrintf( SYS_VRB, "%9d lights bounds culled\n", lightsBoundsCulled );
2028         Sys_FPrintf( SYS_VRB, "%9d lights cluster culled\n", lightsClusterCulled );
2029
2030         /* radiosity */
2031         b = 1;
2032         bt = bounce;
2033         while ( bounce > 0 )
2034         {
2035                 /* store off the bsp between bounces */
2036                 StoreSurfaceLightmaps( fastAllocate );
2037                 UnparseEntities();
2038                 Sys_Printf( "Writing %s\n", BSPFilePath );
2039                 WriteBSPFile( BSPFilePath );
2040
2041                 /* note it */
2042                 Sys_Printf( "\n--- Radiosity (bounce %d of %d) ---\n", b, bt );
2043
2044                 /* flag bouncing */
2045                 bouncing = qtrue;
2046                 VectorClear( ambientColor );
2047                 floodlighty = qfalse;
2048
2049                 /* generate diffuse lights */
2050                 RadFreeLights();
2051                 RadCreateDiffuseLights();
2052
2053                 /* setup light envelopes */
2054                 SetupEnvelopes( qfalse, fastbounce );
2055                 if ( numLights == 0 ) {
2056                         Sys_Printf( "No diffuse light to calculate, ending radiosity.\n" );
2057                         return;
2058                 }
2059
2060                 /* add to lightgrid */
2061                 if ( bouncegrid ) {
2062                         gridEnvelopeCulled = 0;
2063                         gridBoundsCulled = 0;
2064
2065                         Sys_Printf( "--- BounceGrid ---\n" );
2066                         inGrid = qtrue;
2067                         RunThreadsOnIndividual( numRawGridPoints, qtrue, TraceGrid );
2068                         inGrid = qfalse;
2069                         Sys_FPrintf( SYS_VRB, "%9d grid points envelope culled\n", gridEnvelopeCulled );
2070                         Sys_FPrintf( SYS_VRB, "%9d grid points bounds culled\n", gridBoundsCulled );
2071                 }
2072
2073                 /* light up my world */
2074                 lightsPlaneCulled = 0;
2075                 lightsEnvelopeCulled = 0;
2076                 lightsBoundsCulled = 0;
2077                 lightsClusterCulled = 0;
2078
2079                 Sys_Printf( "--- IlluminateRawLightmap ---\n" );
2080                 RunThreadsOnIndividual( numRawLightmaps, qtrue, IlluminateRawLightmap );
2081                 Sys_Printf( "%9d luxels illuminated\n", numLuxelsIlluminated );
2082                 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
2083
2084                 StitchSurfaceLightmaps();
2085
2086                 Sys_Printf( "--- IlluminateVertexes ---\n" );
2087                 RunThreadsOnIndividual( numBSPDrawSurfaces, qtrue, IlluminateVertexes );
2088                 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
2089
2090                 /* ydnar: emit statistics on light culling */
2091                 Sys_FPrintf( SYS_VRB, "%9d lights plane culled\n", lightsPlaneCulled );
2092                 Sys_FPrintf( SYS_VRB, "%9d lights envelope culled\n", lightsEnvelopeCulled );
2093                 Sys_FPrintf( SYS_VRB, "%9d lights bounds culled\n", lightsBoundsCulled );
2094                 Sys_FPrintf( SYS_VRB, "%9d lights cluster culled\n", lightsClusterCulled );
2095
2096                 /* interate */
2097                 bounce--;
2098                 b++;
2099         }
2100         /* ydnar: store off lightmaps */
2101         StoreSurfaceLightmaps( fastAllocate );
2102 }
2103
2104
2105
2106 /*
2107    LightMain()
2108    main routine for light processing
2109  */
2110
2111 int LightMain( int argc, char **argv ){
2112         int i;
2113         float f;
2114         char BSPFilePath[ 1024 ];
2115         char surfaceFilePath[ 1024 ];
2116         BSPFilePath[0] = 0;
2117         surfaceFilePath[0] = 0;
2118         const char  *value;
2119         int lightmapMergeSize = 0;
2120         qboolean lightSamplesInsist = qfalse;
2121         qboolean fastAllocate = qfalse;
2122
2123         /* note it */
2124         Sys_Printf( "--- Light ---\n" );
2125         Sys_Printf( "--- ProcessGameSpecific ---\n" );
2126
2127         /* set standard game flags */
2128         wolfLight = game->wolfLight;
2129         if ( wolfLight == qtrue ) {
2130                 Sys_Printf( " lightning model: wolf\n" );
2131         }
2132         else{
2133                 Sys_Printf( " lightning model: quake3\n" );
2134         }
2135
2136         lmCustomSize = game->lightmapSize;
2137         Sys_Printf( " lightmap size: %d x %d pixels\n", lmCustomSize, lmCustomSize );
2138
2139         lightmapGamma = game->lightmapGamma;
2140         Sys_Printf( " lightning gamma: %f\n", lightmapGamma );
2141
2142         lightmapsRGB = game->lightmapsRGB;
2143         if ( lightmapsRGB ) {
2144                 Sys_Printf( " lightmap colorspace: sRGB\n" );
2145         }
2146         else{
2147                 Sys_Printf( " lightmap colorspace: linear\n" );
2148         }
2149
2150         texturesRGB = game->texturesRGB;
2151         if ( texturesRGB ) {
2152                 Sys_Printf( " texture colorspace: sRGB\n" );
2153         }
2154         else{
2155                 Sys_Printf( " texture colorspace: linear\n" );
2156         }
2157
2158         colorsRGB = game->colorsRGB;
2159         if ( colorsRGB ) {
2160                 Sys_Printf( " _color colorspace: sRGB\n" );
2161         }
2162         else{
2163                 Sys_Printf( " _color colorspace: linear\n" );
2164         }
2165
2166         lightmapCompensate = game->lightmapCompensate;
2167         Sys_Printf( " lightning compensation: %f\n", lightmapCompensate );
2168
2169         lightmapExposure = game->lightmapExposure;
2170         Sys_Printf( " lightning exposure: %f\n", lightmapExposure );
2171
2172         gridScale = game->gridScale;
2173         Sys_Printf( " lightgrid scale: %f\n", gridScale );
2174
2175         gridAmbientScale = game->gridAmbientScale;
2176         Sys_Printf( " lightgrid ambient scale: %f\n", gridAmbientScale );
2177
2178         lightAngleHL = game->lightAngleHL;
2179         if ( lightAngleHL ) {
2180                 Sys_Printf( " half lambert light angle attenuation enabled \n" );
2181         }
2182
2183         noStyles = game->noStyles;
2184         if ( noStyles == qtrue ) {
2185                 Sys_Printf( " shader lightstyles hack: disabled\n" );
2186         }
2187         else{
2188                 Sys_Printf( " shader lightstyles hack: enabled\n" );
2189         }
2190
2191         patchShadows = game->patchShadows;
2192         if ( patchShadows == qtrue ) {
2193                 Sys_Printf( " patch shadows: enabled\n" );
2194         }
2195         else{
2196                 Sys_Printf( " patch shadows: disabled\n" );
2197         }
2198
2199         deluxemap = game->deluxeMap;
2200         deluxemode = game->deluxeMode;
2201         if ( deluxemap == qtrue ) {
2202                 if ( deluxemode ) {
2203                         Sys_Printf( " deluxemapping: enabled with tangentspace deluxemaps\n" );
2204                 }
2205                 else{
2206                         Sys_Printf( " deluxemapping: enabled with modelspace deluxemaps\n" );
2207                 }
2208         }
2209         else{
2210                 Sys_Printf( " deluxemapping: disabled\n" );
2211         }
2212
2213         Sys_Printf( "--- ProcessCommandLine ---\n" );
2214
2215         /* process commandline arguments */
2216         for ( i = 1; i < ( argc - 1 ); i++ )
2217         {
2218                 /* lightsource scaling */
2219                 if ( !strcmp( argv[ i ], "-point" ) || !strcmp( argv[ i ], "-pointscale" ) ) {
2220                         f = atof( argv[ i + 1 ] );
2221                         pointScale *= f;
2222                         spotScale *= f;
2223                         Sys_Printf( "Spherical point (entity) light scaled by %f to %f\n", f, pointScale );
2224                         Sys_Printf( "Spot point (entity) light scaled by %f to %f\n", f, spotScale );
2225                         i++;
2226                 }
2227
2228                 else if ( !strcmp( argv[ i ], "-spherical" ) || !strcmp( argv[ i ], "-sphericalscale" ) ) {
2229                         f = atof( argv[ i + 1 ] );
2230                         pointScale *= f;
2231                         Sys_Printf( "Spherical point (entity) light scaled by %f to %f\n", f, pointScale );
2232                         i++;
2233                 }
2234
2235                 else if ( !strcmp( argv[ i ], "-spot" ) || !strcmp( argv[ i ], "-spotscale" ) ) {
2236                         f = atof( argv[ i + 1 ] );
2237                         spotScale *= f;
2238                         Sys_Printf( "Spot point (entity) light scaled by %f to %f\n", f, spotScale );
2239                         i++;
2240                 }
2241
2242                 else if ( !strcmp( argv[ i ], "-area" ) || !strcmp( argv[ i ], "-areascale" ) ) {
2243                         f = atof( argv[ i + 1 ] );
2244                         areaScale *= f;
2245                         Sys_Printf( "Area (shader) light scaled by %f to %f\n", f, areaScale );
2246                         i++;
2247                 }
2248
2249                 else if ( !strcmp( argv[ i ], "-sky" ) || !strcmp( argv[ i ], "-skyscale" ) ) {
2250                         f = atof( argv[ i + 1 ] );
2251                         skyScale *= f;
2252                         Sys_Printf( "Sky/sun light scaled by %f to %f\n", f, skyScale );
2253                         i++;
2254                 }
2255
2256                 else if ( !strcmp( argv[ i ], "-bouncescale" ) ) {
2257                         f = atof( argv[ i + 1 ] );
2258                         bounceScale *= f;
2259                         Sys_Printf( "Bounce (radiosity) light scaled by %f to %f\n", f, bounceScale );
2260                         i++;
2261                 }
2262
2263                 else if ( !strcmp( argv[ i ], "-scale" ) ) {
2264                         f = atof( argv[ i + 1 ] );
2265                         pointScale *= f;
2266                         spotScale *= f;
2267                         areaScale *= f;
2268                         skyScale *= f;
2269                         bounceScale *= f;
2270                         Sys_Printf( "All light scaled by %f\n", f );
2271                         i++;
2272                 }
2273
2274                 else if ( !strcmp( argv[ i ], "-gridscale" ) ) {
2275                         f = atof( argv[ i + 1 ] );
2276                         Sys_Printf( "Grid lightning scaled by %f\n", f );
2277                         gridScale *= f;
2278                         i++;
2279                 }
2280
2281                 else if ( !strcmp( argv[ i ], "-gridambientscale" ) ) {
2282                         f = atof( argv[ i + 1 ] );
2283                         Sys_Printf( "Grid ambient lightning scaled by %f\n", f );
2284                         gridAmbientScale *= f;
2285                         i++;
2286                 }
2287
2288                 else if ( !strcmp( argv[ i ], "-griddirectionality" ) ) {
2289                         f = atof( argv[ i + 1 ] );
2290                         if ( f > 1 ) {
2291                                 f = 1;
2292                         }
2293                         if ( f < gridAmbientDirectionality ) {
2294                                 gridAmbientDirectionality = f;
2295                         }
2296                         Sys_Printf( "Grid directionality is %f\n", f );
2297                         gridDirectionality = f;
2298                         i++;
2299                 }
2300
2301                 else if ( !strcmp( argv[ i ], "-gridambientdirectionality" ) ) {
2302                         f = atof( argv[ i + 1 ] );
2303                         if ( f < -1 ) {
2304                                 f = -1;
2305                         }
2306                         if ( f > gridDirectionality ) {
2307                                 gridDirectionality = f;
2308                         }
2309                         Sys_Printf( "Grid ambient directionality is %f\n", f );
2310                         gridAmbientDirectionality = f;
2311                         i++;
2312                 }
2313
2314                 else if ( !strcmp( argv[ i ], "-gamma" ) ) {
2315                         f = atof( argv[ i + 1 ] );
2316                         lightmapGamma = f;
2317                         Sys_Printf( "Lighting gamma set to %f\n", lightmapGamma );
2318                         i++;
2319                 }
2320
2321                 else if ( !strcmp( argv[ i ], "-sRGBlight" ) ) {
2322                         lightmapsRGB = qtrue;
2323                         Sys_Printf( "Lighting is in sRGB\n" );
2324                 }
2325
2326                 else if ( !strcmp( argv[ i ], "-nosRGBlight" ) ) {
2327                         lightmapsRGB = qfalse;
2328                         Sys_Printf( "Lighting is linear\n" );
2329                 }
2330
2331                 else if ( !strcmp( argv[ i ], "-sRGBtex" ) ) {
2332                         texturesRGB = qtrue;
2333                         Sys_Printf( "Textures are in sRGB\n" );
2334                 }
2335
2336                 else if ( !strcmp( argv[ i ], "-nosRGBtex" ) ) {
2337                         texturesRGB = qfalse;
2338                         Sys_Printf( "Textures are linear\n" );
2339                 }
2340
2341                 else if ( !strcmp( argv[ i ], "-sRGBcolor" ) ) {
2342                         colorsRGB = qtrue;
2343                         Sys_Printf( "Colors are in sRGB\n" );
2344                 }
2345
2346                 else if ( !strcmp( argv[ i ], "-nosRGBcolor" ) ) {
2347                         colorsRGB = qfalse;
2348                         Sys_Printf( "Colors are linear\n" );
2349                 }
2350
2351                 else if ( !strcmp( argv[ i ], "-sRGB" ) ) {
2352                         lightmapsRGB = qtrue;
2353                         Sys_Printf( "Lighting is in sRGB\n" );
2354                         texturesRGB = qtrue;
2355                         Sys_Printf( "Textures are in sRGB\n" );
2356                         colorsRGB = qtrue;
2357                         Sys_Printf( "Colors are in sRGB\n" );
2358                 }
2359
2360                 else if ( !strcmp( argv[ i ], "-nosRGB" ) ) {
2361                         lightmapsRGB = qfalse;
2362                         Sys_Printf( "Lighting is linear\n" );
2363                         texturesRGB = qfalse;
2364                         Sys_Printf( "Textures are linear\n" );
2365                         colorsRGB = qfalse;
2366                         Sys_Printf( "Colors are linear\n" );
2367                 }
2368
2369                 else if ( !strcmp( argv[ i ], "-exposure" ) ) {
2370                         f = atof( argv[ i + 1 ] );
2371                         lightmapExposure = f;
2372                         Sys_Printf( "Lighting exposure set to %f\n", lightmapExposure );
2373                         i++;
2374                 }
2375
2376                 else if ( !strcmp( argv[ i ], "-compensate" ) ) {
2377                         f = atof( argv[ i + 1 ] );
2378                         if ( f <= 0.0f ) {
2379                                 f = 1.0f;
2380                         }
2381                         lightmapCompensate = f;
2382                         Sys_Printf( "Lighting compensation set to 1/%f\n", lightmapCompensate );
2383                         i++;
2384                 }
2385
2386                 /* ydnar switches */
2387                 else if ( !strcmp( argv[ i ], "-bounce" ) ) {
2388                         bounce = atoi( argv[ i + 1 ] );
2389                         if ( bounce < 0 ) {
2390                                 bounce = 0;
2391                         }
2392                         else if ( bounce > 0 ) {
2393                                 Sys_Printf( "Radiosity enabled with %d bounce(s)\n", bounce );
2394                         }
2395                         i++;
2396                 }
2397
2398                 else if ( !strcmp( argv[ i ], "-supersample" ) || !strcmp( argv[ i ], "-super" ) ) {
2399                         superSample = atoi( argv[ i + 1 ] );
2400                         if ( superSample < 1 ) {
2401                                 superSample = 1;
2402                         }
2403                         else if ( superSample > 1 ) {
2404                                 Sys_Printf( "Ordered-grid supersampling enabled with %d sample(s) per lightmap texel\n", ( superSample * superSample ) );
2405                         }
2406                         i++;
2407                 }
2408
2409                 else if ( !strcmp( argv[ i ], "-randomsamples" ) ) {
2410                         lightRandomSamples = qtrue;
2411                         Sys_Printf( "Random sampling enabled\n", lightRandomSamples );
2412                 }
2413
2414                 else if ( !strcmp( argv[ i ], "-samples" ) ) {
2415                         if ( *argv[i + 1] == '+' ) {
2416                                 lightSamplesInsist = qtrue;
2417                         }
2418                         else{
2419                                 lightSamplesInsist = qfalse;
2420                         }
2421                         lightSamples = atoi( argv[ i + 1 ] );
2422                         if ( lightSamples < 1 ) {
2423                                 lightSamples = 1;
2424                         }
2425                         else if ( lightSamples > 1 ) {
2426                                 Sys_Printf( "Adaptive supersampling enabled with %d sample(s) per lightmap texel\n", lightSamples );
2427                         }
2428                         i++;
2429                 }
2430
2431                 else if ( !strcmp( argv[ i ], "-samplessearchboxsize" ) ) {
2432                         lightSamplesSearchBoxSize = atoi( argv[ i + 1 ] );
2433                         if ( lightSamplesSearchBoxSize <= 0 ) {
2434                                 lightSamplesSearchBoxSize = 1;
2435                         }
2436                         if ( lightSamplesSearchBoxSize > 4 ) {
2437                                 lightSamplesSearchBoxSize = 4; /* more makes no sense */
2438                         }
2439                         else if ( lightSamplesSearchBoxSize != 1 ) {
2440                                 Sys_Printf( "Adaptive supersampling uses %f times the normal search box size\n", lightSamplesSearchBoxSize );
2441                         }
2442                         i++;
2443                 }
2444
2445                 else if ( !strcmp( argv[ i ], "-filter" ) ) {
2446                         filter = qtrue;
2447                         Sys_Printf( "Lightmap filtering enabled\n" );
2448                 }
2449
2450                 else if ( !strcmp( argv[ i ], "-dark" ) ) {
2451                         dark = qtrue;
2452                         Sys_Printf( "Dark lightmap seams enabled\n" );
2453                 }
2454
2455                 else if ( !strcmp( argv[ i ], "-shadeangle" ) ) {
2456                         shadeAngleDegrees = atof( argv[ i + 1 ] );
2457                         if ( shadeAngleDegrees < 0.0f ) {
2458                                 shadeAngleDegrees = 0.0f;
2459                         }
2460                         else if ( shadeAngleDegrees > 0.0f ) {
2461                                 shade = qtrue;
2462                                 Sys_Printf( "Phong shading enabled with a breaking angle of %f degrees\n", shadeAngleDegrees );
2463                         }
2464                         i++;
2465                 }
2466
2467                 else if ( !strcmp( argv[ i ], "-thresh" ) ) {
2468                         subdivideThreshold = atof( argv[ i + 1 ] );
2469                         if ( subdivideThreshold < 0 ) {
2470                                 subdivideThreshold = DEFAULT_SUBDIVIDE_THRESHOLD;
2471                         }
2472                         else{
2473                                 Sys_Printf( "Subdivision threshold set at %.3f\n", subdivideThreshold );
2474                         }
2475                         i++;
2476                 }
2477
2478                 else if ( !strcmp( argv[ i ], "-approx" ) ) {
2479                         approximateTolerance = atoi( argv[ i + 1 ] );
2480                         if ( approximateTolerance < 0 ) {
2481                                 approximateTolerance = 0;
2482                         }
2483                         else if ( approximateTolerance > 0 ) {
2484                                 Sys_Printf( "Approximating lightmaps within a byte tolerance of %d\n", approximateTolerance );
2485                         }
2486                         i++;
2487                 }
2488                 else if ( !strcmp( argv[ i ], "-deluxe" ) || !strcmp( argv[ i ], "-deluxemap" ) ) {
2489                         deluxemap = qtrue;
2490                         Sys_Printf( "Generating deluxemaps for average light direction\n" );
2491                 }
2492                 else if ( !strcmp( argv[ i ], "-deluxemode" ) ) {
2493                         deluxemode = atoi( argv[ i + 1 ] );
2494                         if ( deluxemode == 0 || deluxemode > 1 || deluxemode < 0 ) {
2495                                 Sys_Printf( "Generating modelspace deluxemaps\n" );
2496                                 deluxemode = 0;
2497                         }
2498                         else{
2499                                 Sys_Printf( "Generating tangentspace deluxemaps\n" );
2500                         }
2501                         i++;
2502                 }
2503                 else if ( !strcmp( argv[ i ], "-nodeluxe" ) || !strcmp( argv[ i ], "-nodeluxemap" ) ) {
2504                         deluxemap = qfalse;
2505                         Sys_Printf( "Disabling generating of deluxemaps for average light direction\n" );
2506                 }
2507                 else if ( !strcmp( argv[ i ], "-external" ) ) {
2508                         externalLightmaps = qtrue;
2509                         Sys_Printf( "Storing all lightmaps externally\n" );
2510                 }
2511
2512                 else if ( !strcmp( argv[ i ], "-lightmapsize" ) ) {
2513                         lmCustomSize = atoi( argv[ i + 1 ] );
2514
2515                         /* must be a power of 2 and greater than 2 */
2516                         if ( ( ( lmCustomSize - 1 ) & lmCustomSize ) || lmCustomSize < 2 ) {
2517                                 Sys_FPrintf( SYS_WRN, "WARNING: Lightmap size must be a power of 2, greater or equal to 2 pixels.\n" );
2518                                 lmCustomSize = game->lightmapSize;
2519                         }
2520                         i++;
2521                         Sys_Printf( "Default lightmap size set to %d x %d pixels\n", lmCustomSize, lmCustomSize );
2522
2523                         /* enable external lightmaps */
2524                         if ( lmCustomSize != game->lightmapSize ) {
2525                                 externalLightmaps = qtrue;
2526                                 Sys_Printf( "Storing all lightmaps externally\n" );
2527                         }
2528                 }
2529
2530                 else if ( !strcmp( argv[ i ], "-rawlightmapsizelimit" ) ) {
2531                         lmLimitSize = atoi( argv[ i + 1 ] );
2532
2533                         i++;
2534                         Sys_Printf( "Raw lightmap size limit set to %d x %d pixels\n", lmLimitSize, lmLimitSize );
2535                 }
2536
2537                 else if ( !strcmp( argv[ i ], "-lightmapdir" ) ) {
2538                         lmCustomDir = argv[i + 1];
2539                         i++;
2540                         Sys_Printf( "Lightmap directory set to %s\n", lmCustomDir );
2541                         externalLightmaps = qtrue;
2542                         Sys_Printf( "Storing all lightmaps externally\n" );
2543                 }
2544
2545                 /* ydnar: add this to suppress warnings */
2546                 else if ( !strcmp( argv[ i ],  "-custinfoparms" ) ) {
2547                         Sys_Printf( "Custom info parms enabled\n" );
2548                         useCustomInfoParms = qtrue;
2549                 }
2550
2551                 else if ( !strcmp( argv[ i ], "-wolf" ) ) {
2552                         /* -game should already be set */
2553                         wolfLight = qtrue;
2554                         Sys_Printf( "Enabling Wolf lighting model (linear default)\n" );
2555                 }
2556
2557                 else if ( !strcmp( argv[ i ], "-q3" ) ) {
2558                         /* -game should already be set */
2559                         wolfLight = qfalse;
2560                         Sys_Printf( "Enabling Quake 3 lighting model (nonlinear default)\n" );
2561                 }
2562
2563                 else if ( !strcmp( argv[ i ], "-extradist" ) ) {
2564                         extraDist = atof( argv[ i + 1 ] );
2565                         if ( extraDist < 0 ) {
2566                                 extraDist = 0;
2567                         }
2568                         i++;
2569                         Sys_Printf( "Default extra radius set to %f units\n", extraDist );
2570                 }
2571
2572                 else if ( !strcmp( argv[ i ], "-sunonly" ) ) {
2573                         sunOnly = qtrue;
2574                         Sys_Printf( "Only computing sunlight\n" );
2575                 }
2576
2577                 else if ( !strcmp( argv[ i ], "-bounceonly" ) ) {
2578                         bounceOnly = qtrue;
2579                         Sys_Printf( "Storing bounced light (radiosity) only\n" );
2580                 }
2581
2582                 else if ( !strcmp( argv[ i ], "-nocollapse" ) ) {
2583                         noCollapse = qtrue;
2584                         Sys_Printf( "Identical lightmap collapsing disabled\n" );
2585                 }
2586
2587                 else if ( !strcmp( argv[ i ], "-nolightmapsearch" ) ) {
2588                         lightmapSearchBlockSize = 1;
2589                         Sys_Printf( "No lightmap searching - all lightmaps will be sequential\n" );
2590                 }
2591
2592                 else if ( !strcmp( argv[ i ], "-lightmapsearchpower" ) ) {
2593                         lightmapMergeSize = ( game->lightmapSize << atoi( argv[i + 1] ) );
2594                         ++i;
2595                         Sys_Printf( "Restricted lightmap searching enabled - optimize for lightmap merge power %d (size %d)\n", atoi( argv[i] ), lightmapMergeSize );
2596                 }
2597
2598                 else if ( !strcmp( argv[ i ], "-lightmapsearchblocksize" ) ) {
2599                         lightmapSearchBlockSize = atoi( argv[i + 1] );
2600                         ++i;
2601                         Sys_Printf( "Restricted lightmap searching enabled - block size set to %d\n", lightmapSearchBlockSize );
2602                 }
2603
2604                 else if ( !strcmp( argv[ i ], "-shade" ) ) {
2605                         shade = qtrue;
2606                         Sys_Printf( "Phong shading enabled\n" );
2607                 }
2608
2609                 else if ( !strcmp( argv[ i ], "-bouncegrid" ) ) {
2610                         bouncegrid = qtrue;
2611                         if ( bounce > 0 ) {
2612                                 Sys_Printf( "Grid lighting with radiosity enabled\n" );
2613                         }
2614                 }
2615
2616                 else if ( !strcmp( argv[ i ], "-smooth" ) ) {
2617                         lightSamples = EXTRA_SCALE;
2618                         Sys_Printf( "The -smooth argument is deprecated, use \"-samples 2\" instead\n" );
2619                 }
2620
2621                 else if ( !strcmp( argv[ i ], "-nofastpoint" ) ) {
2622                         fastpoint = qfalse;
2623                         Sys_Printf( "Automatic fast mode for point lights disabled\n" );
2624                 }
2625
2626                 else if ( !strcmp( argv[ i ], "-fast" ) ) {
2627                         fast = qtrue;
2628                         fastgrid = qtrue;
2629                         fastbounce = qtrue;
2630                         Sys_Printf( "Fast mode enabled for all area lights\n" );
2631                 }
2632
2633                 else if ( !strcmp( argv[ i ], "-faster" ) ) {
2634                         faster = qtrue;
2635                         fast = qtrue;
2636                         fastgrid = qtrue;
2637                         fastbounce = qtrue;
2638                         Sys_Printf( "Faster mode enabled\n" );
2639                 }
2640
2641                 else if ( !strcmp( argv[ i ], "-fastallocate" ) ) {
2642                         fastAllocate = qtrue;
2643                         Sys_Printf( "Fast allocation mode enabled\n" );
2644                 }
2645
2646                 else if ( !strcmp( argv[ i ], "-fastgrid" ) ) {
2647                         fastgrid = qtrue;
2648                         Sys_Printf( "Fast grid lighting enabled\n" );
2649                 }
2650
2651                 else if ( !strcmp( argv[ i ], "-fastbounce" ) ) {
2652                         fastbounce = qtrue;
2653                         Sys_Printf( "Fast bounce mode enabled\n" );
2654                 }
2655
2656                 else if ( !strcmp( argv[ i ], "-cheap" ) ) {
2657                         cheap = qtrue;
2658                         cheapgrid = qtrue;
2659                         Sys_Printf( "Cheap mode enabled\n" );
2660                 }
2661
2662                 else if ( !strcmp( argv[ i ], "-cheapgrid" ) ) {
2663                         cheapgrid = qtrue;
2664                         Sys_Printf( "Cheap grid mode enabled\n" );
2665                 }
2666
2667                 else if ( !strcmp( argv[ i ], "-normalmap" ) ) {
2668                         normalmap = qtrue;
2669                         Sys_Printf( "Storing normal map instead of lightmap\n" );
2670                 }
2671
2672                 else if ( !strcmp( argv[ i ], "-trisoup" ) ) {
2673                         trisoup = qtrue;
2674                         Sys_Printf( "Converting brush faces to triangle soup\n" );
2675                 }
2676
2677                 else if ( !strcmp( argv[ i ], "-debug" ) ) {
2678                         debug = qtrue;
2679                         Sys_Printf( "Lightmap debugging enabled\n" );
2680                 }
2681
2682                 else if ( !strcmp( argv[ i ], "-debugsurfaces" ) || !strcmp( argv[ i ], "-debugsurface" ) ) {
2683                         debugSurfaces = qtrue;
2684                         Sys_Printf( "Lightmap surface debugging enabled\n" );
2685                 }
2686
2687                 else if ( !strcmp( argv[ i ], "-debugunused" ) ) {
2688                         debugUnused = qtrue;
2689                         Sys_Printf( "Unused luxel debugging enabled\n" );
2690                 }
2691
2692                 else if ( !strcmp( argv[ i ], "-debugaxis" ) ) {
2693                         debugAxis = qtrue;
2694                         Sys_Printf( "Lightmap axis debugging enabled\n" );
2695                 }
2696
2697                 else if ( !strcmp( argv[ i ], "-debugcluster" ) ) {
2698                         debugCluster = qtrue;
2699                         Sys_Printf( "Luxel cluster debugging enabled\n" );
2700                 }
2701
2702                 else if ( !strcmp( argv[ i ], "-debugorigin" ) ) {
2703                         debugOrigin = qtrue;
2704                         Sys_Printf( "Luxel origin debugging enabled\n" );
2705                 }
2706
2707                 else if ( !strcmp( argv[ i ], "-debugdeluxe" ) ) {
2708                         deluxemap = qtrue;
2709                         debugDeluxemap = qtrue;
2710                         Sys_Printf( "Deluxemap debugging enabled\n" );
2711                 }
2712
2713                 else if ( !strcmp( argv[ i ], "-export" ) ) {
2714                         exportLightmaps = qtrue;
2715                         Sys_Printf( "Exporting lightmaps\n" );
2716                 }
2717
2718                 else if ( !strcmp( argv[ i ], "-notrace" ) ) {
2719                         noTrace = qtrue;
2720                         Sys_Printf( "Shadow occlusion disabled\n" );
2721                 }
2722                 else if ( !strcmp( argv[ i ], "-patchshadows" ) ) {
2723                         patchShadows = qtrue;
2724                         Sys_Printf( "Patch shadow casting enabled\n" );
2725                 }
2726                 else if ( !strcmp( argv[ i ], "-extra" ) ) {
2727                         superSample = EXTRA_SCALE;      /* ydnar */
2728                         Sys_Printf( "The -extra argument is deprecated, use \"-super 2\" instead\n" );
2729                 }
2730                 else if ( !strcmp( argv[ i ], "-extrawide" ) ) {
2731                         superSample = EXTRAWIDE_SCALE;  /* ydnar */
2732                         filter = qtrue;                 /* ydnar */
2733                         Sys_Printf( "The -extrawide argument is deprecated, use \"-filter [-super 2]\" instead\n" );
2734                 }
2735                 else if ( !strcmp( argv[ i ], "-samplesize" ) ) {
2736                         sampleSize = atoi( argv[ i + 1 ] );
2737                         if ( sampleSize < 1 ) {
2738                                 sampleSize = 1;
2739                         }
2740                         i++;
2741                         Sys_Printf( "Default lightmap sample size set to %dx%d units\n", sampleSize, sampleSize );
2742                 }
2743                 else if ( !strcmp( argv[ i ], "-minsamplesize" ) ) {
2744                         minSampleSize = atoi( argv[ i + 1 ] );
2745                         if ( minSampleSize < 1 ) {
2746                                 minSampleSize = 1;
2747                         }
2748                         i++;
2749                         Sys_Printf( "Minimum lightmap sample size set to %dx%d units\n", minSampleSize, minSampleSize );
2750                 }
2751                 else if ( !strcmp( argv[ i ],  "-samplescale" ) ) {
2752                         sampleScale = atoi( argv[ i + 1 ] );
2753                         i++;
2754                         Sys_Printf( "Lightmaps sample scale set to %d\n", sampleScale );
2755                 }
2756                 else if ( !strcmp( argv[ i ], "-novertex" ) ) {
2757                         noVertexLighting = qtrue;
2758                         Sys_Printf( "Disabling vertex lighting\n" );
2759                 }
2760                 else if ( !strcmp( argv[ i ], "-nogrid" ) ) {
2761                         noGridLighting = qtrue;
2762                         Sys_Printf( "Disabling grid lighting\n" );
2763                 }
2764                 else if ( !strcmp( argv[ i ], "-border" ) ) {
2765                         lightmapBorder = qtrue;
2766                         Sys_Printf( "Adding debug border to lightmaps\n" );
2767                 }
2768                 else if ( !strcmp( argv[ i ], "-nosurf" ) ) {
2769                         noSurfaces = qtrue;
2770                         Sys_Printf( "Not tracing against surfaces\n" );
2771                 }
2772                 else if ( !strcmp( argv[ i ], "-dump" ) ) {
2773                         dump = qtrue;
2774                         Sys_Printf( "Dumping radiosity lights into numbered prefabs\n" );
2775                 }
2776                 else if ( !strcmp( argv[ i ], "-lomem" ) ) {
2777                         loMem = qtrue;
2778                         Sys_Printf( "Enabling low-memory (potentially slower) lighting mode\n" );
2779                 }
2780                 else if ( !strcmp( argv[ i ], "-lightsubdiv" ) ) {
2781                         defaultLightSubdivide = atoi( argv[ i + 1 ] );
2782                         if ( defaultLightSubdivide < 1 ) {
2783                                 defaultLightSubdivide = 1;
2784                         }
2785                         i++;
2786                         Sys_Printf( "Default light subdivision set to %d\n", defaultLightSubdivide );
2787                 }
2788                 else if ( !strcmp( argv[ i ], "-lightanglehl" ) ) {
2789                         if ( ( atoi( argv[ i + 1 ] ) != 0 ) != lightAngleHL ) {
2790                                 lightAngleHL = ( atoi( argv[ i + 1 ] ) != 0 );
2791                                 if ( lightAngleHL ) {
2792                                         Sys_Printf( "Enabling half lambert light angle attenuation\n" );
2793                                 }
2794                                 else{
2795                                         Sys_Printf( "Disabling half lambert light angle attenuation\n" );
2796                                 }
2797                         }
2798                 }
2799                 else if ( !strcmp( argv[ i ], "-nostyle" ) || !strcmp( argv[ i ], "-nostyles" ) ) {
2800                         noStyles = qtrue;
2801                         Sys_Printf( "Disabling lightstyles\n" );
2802                 }
2803                 else if ( !strcmp( argv[ i ], "-style" ) || !strcmp( argv[ i ], "-styles" ) ) {
2804                         noStyles = qfalse;
2805                         Sys_Printf( "Enabling lightstyles\n" );
2806                 }
2807                 else if ( !strcmp( argv[ i ], "-cpma" ) ) {
2808                         cpmaHack = qtrue;
2809                         Sys_Printf( "Enabling Challenge Pro Mode Asstacular Vertex Lighting Mode (tm)\n" );
2810                 }
2811                 else if ( !strcmp( argv[ i ], "-floodlight" ) ) {
2812                         floodlighty = qtrue;
2813                         Sys_Printf( "FloodLighting enabled\n" );
2814                 }
2815                 else if ( !strcmp( argv[ i ], "-debugnormals" ) ) {
2816                         debugnormals = qtrue;
2817                         Sys_Printf( "DebugNormals enabled\n" );
2818                 }
2819                 else if ( !strcmp( argv[ i ], "-lowquality" ) ) {
2820                         floodlight_lowquality = qtrue;
2821                         Sys_Printf( "Low Quality FloodLighting enabled\n" );
2822                 }
2823
2824                 /* r7: dirtmapping */
2825                 else if ( !strcmp( argv[ i ], "-dirty" ) ) {
2826                         dirty = qtrue;
2827                         Sys_Printf( "Dirtmapping enabled\n" );
2828                 }
2829                 else if ( !strcmp( argv[ i ], "-dirtdebug" ) || !strcmp( argv[ i ], "-debugdirt" ) ) {
2830                         dirtDebug = qtrue;
2831                         Sys_Printf( "Dirtmap debugging enabled\n" );
2832                 }
2833                 else if ( !strcmp( argv[ i ], "-dirtmode" ) ) {
2834                         dirtMode = atoi( argv[ i + 1 ] );
2835                         if ( dirtMode != 0 && dirtMode != 1 ) {
2836                                 dirtMode = 0;
2837                         }
2838                         if ( dirtMode == 1 ) {
2839                                 Sys_Printf( "Enabling randomized dirtmapping\n" );
2840                         }
2841                         else{
2842                                 Sys_Printf( "Enabling ordered dir mapping\n" );
2843                         }
2844                         i++;
2845                 }
2846                 else if ( !strcmp( argv[ i ], "-dirtdepth" ) ) {
2847                         dirtDepth = atof( argv[ i + 1 ] );
2848                         if ( dirtDepth <= 0.0f ) {
2849                                 dirtDepth = 128.0f;
2850                         }
2851                         Sys_Printf( "Dirtmapping depth set to %.1f\n", dirtDepth );
2852                         i++;
2853                 }
2854                 else if ( !strcmp( argv[ i ], "-dirtscale" ) ) {
2855                         dirtScale = atof( argv[ i + 1 ] );
2856                         if ( dirtScale <= 0.0f ) {
2857                                 dirtScale = 1.0f;
2858                         }
2859                         Sys_Printf( "Dirtmapping scale set to %.1f\n", dirtScale );
2860                         i++;
2861                 }
2862                 else if ( !strcmp( argv[ i ], "-dirtgain" ) ) {
2863                         dirtGain = atof( argv[ i + 1 ] );
2864                         if ( dirtGain <= 0.0f ) {
2865                                 dirtGain = 1.0f;
2866                         }
2867                         Sys_Printf( "Dirtmapping gain set to %.1f\n", dirtGain );
2868                         i++;
2869                 }
2870                 else if ( !strcmp( argv[ i ], "-trianglecheck" ) ) {
2871                         lightmapTriangleCheck = qtrue;
2872                 }
2873                 else if ( !strcmp( argv[ i ], "-extravisnudge" ) ) {
2874                         lightmapExtraVisClusterNudge = qtrue;
2875                 }
2876                 else if ( !strcmp( argv[ i ], "-fill" ) ) {
2877                         lightmapFill = qtrue;
2878                         Sys_Printf( "Filling lightmap colors from surrounding pixels to improve JPEG compression\n" );
2879                 }
2880                 else if ( !strcmp( argv[ i ], "-bspfile" ) )
2881                 {
2882                         strcpy( BSPFilePath, argv[i + 1] );
2883                         i++;
2884                         Sys_Printf( "Use %s as bsp file\n", BSPFilePath );
2885                 }
2886                 else if ( !strcmp( argv[ i ], "-srffile" ) )
2887                 {
2888                         strcpy( surfaceFilePath, argv[i + 1] );
2889                         i++;
2890                         Sys_Printf( "Use %s as surface file\n", surfaceFilePath );
2891                 }
2892                 /* unhandled args */
2893                 else
2894                 {
2895                         Sys_FPrintf( SYS_WRN, "WARNING: Unknown argument \"%s\"\n", argv[ i ] );
2896                 }
2897
2898         }
2899
2900         /* fix up falloff tolerance for sRGB */
2901         if ( lightmapsRGB ) {
2902                 falloffTolerance = Image_LinearFloatFromsRGBFloat( falloffTolerance * ( 1.0 / 255.0 ) ) * 255.0;
2903         }
2904
2905         /* fix up samples count */
2906         if ( lightRandomSamples ) {
2907                 if ( !lightSamplesInsist ) {
2908                         /* approximately match -samples in quality */
2909                         switch ( lightSamples )
2910                         {
2911                         /* somewhat okay */
2912                         case 1:
2913                         case 2:
2914                                 lightSamples = 16;
2915                                 Sys_Printf( "Adaptive supersampling preset enabled with %d random sample(s) per lightmap texel\n", lightSamples );
2916                                 break;
2917
2918                         /* good */
2919                         case 3:
2920                                 lightSamples = 64;
2921                                 Sys_Printf( "Adaptive supersampling preset enabled with %d random sample(s) per lightmap texel\n", lightSamples );
2922                                 break;
2923
2924                         /* perfect */
2925                         case 4:
2926                                 lightSamples = 256;
2927                                 Sys_Printf( "Adaptive supersampling preset enabled with %d random sample(s) per lightmap texel\n", lightSamples );
2928                                 break;
2929
2930                         default: break;
2931                         }
2932                 }
2933         }
2934
2935         /* fix up lightmap search power */
2936         if ( lightmapMergeSize ) {
2937                 lightmapSearchBlockSize = ( lightmapMergeSize / lmCustomSize ) * ( lightmapMergeSize / lmCustomSize );
2938                 if ( lightmapSearchBlockSize < 1 ) {
2939                         lightmapSearchBlockSize = 1;
2940                 }
2941
2942                 Sys_Printf( "Restricted lightmap searching enabled - block size adjusted to %d\n", lightmapSearchBlockSize );
2943         }
2944
2945         strcpy( source, ExpandArg( argv[ i ] ) );
2946         StripExtension( source );
2947         DefaultExtension( source, ".map" );
2948
2949         if (!BSPFilePath[0]) {
2950                 strcpy( BSPFilePath, ExpandArg( argv[ i ] ) );
2951                 StripExtension( BSPFilePath );
2952                 DefaultExtension( BSPFilePath, ".bsp" );
2953         }
2954
2955         if (!surfaceFilePath[0]) {
2956                 strcpy( surfaceFilePath, ExpandArg( argv[ i ] ) );
2957                 StripExtension( surfaceFilePath );
2958                 DefaultExtension( surfaceFilePath, ".srf" );
2959         }
2960
2961         /* ydnar: set default sample size */
2962         SetDefaultSampleSize( sampleSize );
2963
2964         /* ydnar: handle shaders */
2965         BeginMapShaderFile( BSPFilePath );
2966         LoadShaderInfo();
2967
2968         /* note loading */
2969         Sys_Printf( "Loading %s\n", source );
2970
2971         /* ydnar: load surface file */
2972         LoadSurfaceExtraFile( surfaceFilePath );
2973
2974         /* load bsp file */
2975         LoadBSPFile( BSPFilePath );
2976
2977         /* parse bsp entities */
2978         ParseEntities();
2979
2980         /* inject command line parameters */
2981         InjectCommandLine( argv, 0, argc - 1 );
2982
2983         /* load map file */
2984         value = ValueForKey( &entities[ 0 ], "_keepLights" );
2985         if ( value[ 0 ] != '1' ) {
2986                 LoadMapFile( source, qtrue, qfalse );
2987         }
2988
2989         /* set the entity/model origins and init yDrawVerts */
2990         SetEntityOrigins();
2991
2992         /* ydnar: set up optimization */
2993         SetupBrushes();
2994         SetupDirt();
2995         SetupFloodLight();
2996         SetupSurfaceLightmaps();
2997
2998         /* initialize the surface facet tracing */
2999         SetupTraceNodes();
3000
3001         /* light the world */
3002         LightWorld( BSPFilePath, fastAllocate );
3003
3004         /* write out the bsp */
3005         UnparseEntities();
3006         Sys_Printf( "Writing %s\n", BSPFilePath );
3007         WriteBSPFile( BSPFilePath );
3008
3009         /* ydnar: export lightmaps */
3010         if ( exportLightmaps && !externalLightmaps ) {
3011                 ExportLightmaps();
3012         }
3013
3014         /* return to sender */
3015         return 0;
3016 }