2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ----------------------------------------------------------------------------------
23 This code has been altered significantly from its original form, to support
24 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26 ------------------------------------------------------------------------------- */
41 CreateSunLight() - ydnar
42 this creates a sun light
45 static void CreateSunLight( sun_t *sun )
48 float photons, d, angle, elevation, da, de;
58 if( sun->numSamples < 1 )
62 photons = sun->photons / sun->numSamples;
64 /* create the right number of suns */
65 for( i = 0; i < sun->numSamples; i++ )
67 /* calculate sun direction */
69 VectorCopy( sun->direction, direction );
73 sun->direction[ 0 ] = cos( angle ) * cos( elevation );
74 sun->direction[ 1 ] = sin( angle ) * cos( elevation );
75 sun->direction[ 2 ] = sin( elevation );
77 xz_dist = sqrt( x*x + z*z )
78 latitude = atan2( xz_dist, y ) * RADIANS
79 longitude = atan2( x, z ) * RADIANS
82 d = sqrt( sun->direction[ 0 ] * sun->direction[ 0 ] + sun->direction[ 1 ] * sun->direction[ 1 ] );
83 angle = atan2( sun->direction[ 1 ], sun->direction[ 0 ] );
84 elevation = atan2( sun->direction[ 2 ], d );
86 /* jitter the angles (loop to keep random sample within sun->deviance steridians) */
89 da = (Random() * 2.0f - 1.0f) * sun->deviance;
90 de = (Random() * 2.0f - 1.0f) * sun->deviance;
92 while( (da * da + de * de) > (sun->deviance * sun->deviance) );
97 //% Sys_Printf( "%d: Angle: %3.4f Elevation: %3.3f\n", sun->numSamples, (angle / Q_PI * 180.0f), (elevation / Q_PI * 180.0f) );
99 /* create new vector */
100 direction[ 0 ] = cos( angle ) * cos( elevation );
101 direction[ 1 ] = sin( angle ) * cos( elevation );
102 direction[ 2 ] = sin( elevation );
107 light = safe_malloc( sizeof( *light ) );
108 memset( light, 0, sizeof( *light ) );
109 light->next = lights;
112 /* initialize the light */
113 light->flags = LIGHT_SUN_DEFAULT;
114 light->type = EMIT_SUN;
116 light->falloffTolerance = falloffTolerance;
117 light->filterRadius = sun->filterRadius / sun->numSamples;
119 /* set the light's position out to infinity */
120 VectorMA( vec3_origin, (MAX_WORLD_COORD * 8.0f), direction, light->origin ); /* MAX_WORLD_COORD * 2.0f */
122 /* set the facing to be the inverse of the sun direction */
123 VectorScale( direction, -1.0, light->normal );
124 light->dist = DotProduct( light->origin, light->normal );
126 /* set color and photons */
127 VectorCopy( sun->color, light->color );
128 light->photons = photons * skyScale;
132 if( sun->next != NULL )
133 CreateSunLight( sun->next );
139 CreateSkyLights() - ydnar
140 simulates sky light with multiple suns
143 static void CreateSkyLights( vec3_t color, float value, int iterations, float filterRadius )
145 int c, i, j, k, numSuns;
152 if( value <= 0.0f || iterations < 2 )
155 /* calculate some stuff */
156 step = 2.0f / (iterations - 1);
159 /* basic sun setup */
160 VectorCopy( color, sun.color );
162 sun.filterRadius = filterRadius;
168 for( c = 0; c < 2; c++ )
170 for( k = 0, in[ 2 ] = start; k < iterations; k++, in[ 2 ] += step )
172 /* don't create sky light below the horizon */
173 if( in[ 2 ] <= 0.0f )
176 for( j = 0, in[ 1 ] = start; j < iterations; j++, in[ 1 ] += step )
178 for( i = 0, in[ 0 ] = start; i < iterations; i++, in[ 0 ] += step )
180 if( VectorNormalize( in, sun.direction ) )
182 if( c > 0 && numSuns > 0 )
184 sun.photons = value / numSuns;
185 CreateSunLight( &sun );
200 creates lights from light entities
203 void CreateEntityLights( void )
206 light_t *light, *light2;
212 float intensity, scale, deviance, filterRadius;
213 int spawnflags, flags, numSamples;
217 /* go throught entity list and find lights */
218 for( i = 0; i < numEntities; i++ )
222 name = ValueForKey( e, "classname" );
224 /* ydnar: check for lightJunior */
225 if( Q_strncasecmp( name, "lightJunior", 11 ) == 0 )
227 else if( Q_strncasecmp( name, "light", 5 ) == 0 )
232 /* lights with target names (and therefore styles) are only parsed from BSP */
233 target = ValueForKey( e, "targetname" );
234 if( target[ 0 ] != '\0' && i >= numBSPEntities )
239 light = safe_malloc( sizeof( *light ) );
240 memset( light, 0, sizeof( *light ) );
241 light->next = lights;
244 /* handle spawnflags */
245 spawnflags = IntForKey( e, "spawnflags" );
247 /* ydnar: quake 3+ light behavior */
248 if( game->wolfLight == qfalse )
250 /* set default flags */
251 flags = LIGHT_Q3A_DEFAULT;
253 /* linear attenuation? */
256 flags |= LIGHT_ATTEN_LINEAR;
257 flags &= ~LIGHT_ATTEN_ANGLE;
260 /* no angle attenuate? */
262 flags &= ~LIGHT_ATTEN_ANGLE;
265 /* ydnar: wolf light behavior */
268 /* set default flags */
269 flags = LIGHT_WOLF_DEFAULT;
271 /* inverse distance squared attenuation? */
274 flags &= ~LIGHT_ATTEN_LINEAR;
275 flags |= LIGHT_ATTEN_ANGLE;
278 /* angle attenuate? */
280 flags |= LIGHT_ATTEN_ANGLE;
283 /* other flags (borrowed from wolf) */
285 /* wolf dark light? */
286 if( (spawnflags & 4) || (spawnflags & 8) )
290 if( spawnflags & 16 )
291 flags &= ~LIGHT_GRID;
297 flags &= ~LIGHT_SURFACES;
300 /* store the flags */
301 light->flags = flags;
303 /* ydnar: set fade key (from wolf) */
305 if( light->flags & LIGHT_ATTEN_LINEAR )
307 light->fade = FloatForKey( e, "fade" );
308 if( light->fade == 0.0f )
312 /* ydnar: set angle scaling (from vlight) */
313 light->angleScale = FloatForKey( e, "_anglescale" );
314 if( light->angleScale != 0.0f )
315 light->flags |= LIGHT_ATTEN_ANGLE;
318 GetVectorForKey( e, "origin", light->origin);
319 light->style = IntForKey( e, "_style" );
320 if( light->style == 0 )
321 light->style = IntForKey( e, "style" );
322 if( light->style < LS_NORMAL || light->style >= LS_NONE )
323 Error( "Invalid lightstyle (%d) on entity %d", light->style, i );
325 /* set light intensity */
326 intensity = FloatForKey( e, "_light" );
327 if( intensity == 0.0f )
328 intensity = FloatForKey( e, "light" );
329 if( intensity == 0.0f)
332 /* ydnar: set light scale (sof2) */
333 scale = FloatForKey( e, "scale" );
338 /* ydnar: get deviance and samples */
339 deviance = FloatForKey( e, "_deviance" );
340 if( deviance == 0.0f )
341 deviance = FloatForKey( e, "_deviation" );
342 if( deviance == 0.0f )
343 deviance = FloatForKey( e, "_jitter" );
344 numSamples = IntForKey( e, "_samples" );
345 if( deviance < 0.0f || numSamples < 1 )
350 intensity /= numSamples;
352 /* ydnar: get filter radius */
353 filterRadius = FloatForKey( e, "_filterradius" );
354 if( filterRadius == 0.0f )
355 filterRadius = FloatForKey( e, "_filteradius" );
356 if( filterRadius == 0.0f )
357 filterRadius = FloatForKey( e, "_filter" );
358 if( filterRadius < 0.0f )
360 light->filterRadius = filterRadius;
362 /* set light color */
363 _color = ValueForKey( e, "_color" );
364 if( _color && _color[ 0 ] )
366 sscanf( _color, "%f %f %f", &light->color[ 0 ], &light->color[ 1 ], &light->color[ 2 ] );
367 ColorNormalize( light->color, light->color );
370 light->color[ 0 ] = light->color[ 1 ] = light->color[ 2 ] = 1.0f;
372 intensity = intensity * pointScale;
373 light->photons = intensity;
375 light->type = EMIT_POINT;
377 /* set falloff threshold */
378 light->falloffTolerance = falloffTolerance / numSamples;
380 /* lights with a target will be spotlights */
381 target = ValueForKey( e, "target" );
391 e2 = FindTargetEntity( target );
394 Sys_Printf( "WARNING: light at (%i %i %i) has missing target\n",
395 (int) light->origin[ 0 ], (int) light->origin[ 1 ], (int) light->origin[ 2 ] );
399 /* not a point light */
403 /* make a spotlight */
404 GetVectorForKey( e2, "origin", dest );
405 VectorSubtract( dest, light->origin, light->normal );
406 dist = VectorNormalize( light->normal, light->normal );
407 radius = FloatForKey( e, "radius" );
412 light->radiusByDist = (radius + 16) / dist;
413 light->type = EMIT_SPOT;
415 /* ydnar: wolf mods: spotlights always use nonlinear + angle attenuation */
416 light->flags &= ~LIGHT_ATTEN_LINEAR;
417 light->flags |= LIGHT_ATTEN_ANGLE;
420 /* ydnar: is this a sun? */
421 _sun = ValueForKey( e, "_sun" );
422 if( _sun[ 0 ] == '1' )
424 /* not a spot light */
427 /* unlink this light */
428 lights = light->next;
431 VectorScale( light->normal, -1.0f, sun.direction );
432 VectorCopy( light->color, sun.color );
433 sun.photons = (intensity / pointScale);
434 sun.deviance = deviance / 180.0f * Q_PI;
435 sun.numSamples = numSamples;
438 /* make a sun light */
439 CreateSunLight( &sun );
441 /* free original light */
445 /* skip the rest of this love story */
451 /* jitter the light */
452 for( j = 1; j < numSamples; j++ )
455 light2 = safe_malloc( sizeof( *light ) );
456 memcpy( light2, light, sizeof( *light ) );
457 light2->next = lights;
461 if( light->type == EMIT_SPOT )
467 light2->origin[ 0 ] = light->origin[ 0 ] + (Random() * 2.0f - 1.0f) * deviance;
468 light2->origin[ 1 ] = light->origin[ 1 ] + (Random() * 2.0f - 1.0f) * deviance;
469 light2->origin[ 2 ] = light->origin[ 2 ] + (Random() * 2.0f - 1.0f) * deviance;
477 CreateSurfaceLights() - ydnar
478 this hijacks the radiosity code to generate surface lights for first pass
481 #define APPROX_BOUNCE 1.0f
483 void CreateSurfaceLights( void )
486 bspDrawSurface_t *ds;
496 /* get sun shader supressor */
497 nss = ValueForKey( &entities[ 0 ], "_noshadersun" );
499 /* walk the list of surfaces */
500 for( i = 0; i < numBSPDrawSurfaces; i++ )
502 /* get surface and other bits */
503 ds = &bspDrawSurfaces[ i ];
504 info = &surfaceInfos[ i ];
508 if( si->sun != NULL && nss[ 0 ] != '1' )
510 Sys_FPrintf( SYS_VRB, "Sun: %s\n", si->shader );
511 CreateSunLight( si->sun );
512 si->sun = NULL; /* FIXME: leak! */
516 if( si->skyLightValue > 0.0f )
518 Sys_FPrintf( SYS_VRB, "Sky: %s\n", si->shader );
519 CreateSkyLights( si->color, si->skyLightValue, si->skyLightIterations, si->lightFilterRadius );
520 si->skyLightValue = 0.0f; /* FIXME: hack! */
523 /* try to early out */
527 /* autosprite shaders become point lights */
530 /* create an average xyz */
531 VectorAdd( info->mins, info->maxs, origin );
532 VectorScale( origin, 0.5f, origin );
535 light = safe_malloc( sizeof( *light ) );
536 memset( light, 0, sizeof( *light ) );
537 light->next = lights;
541 light->flags = LIGHT_Q3A_DEFAULT;
542 light->type = EMIT_POINT;
543 light->photons = si->value * pointScale;
546 VectorCopy( origin, light->origin );
547 VectorCopy( si->color, light->color );
548 light->falloffTolerance = falloffTolerance;
549 light->style = light->style;
551 /* add to point light count and continue */
556 /* get subdivision amount */
557 if( si->lightSubdivide > 0 )
558 subdivide = si->lightSubdivide;
560 subdivide = defaultLightSubdivide;
563 switch( ds->surfaceType )
566 case MST_TRIANGLE_SOUP:
567 RadLightForTriangles( i, 0, info->lm, si, APPROX_BOUNCE, subdivide, &cw );
571 RadLightForPatch( i, 0, info->lm, si, APPROX_BOUNCE, subdivide, &cw );
584 find the offset values for inline models
587 void SetEntityOrigins( void )
595 bspDrawSurface_t *ds;
598 /* ydnar: copy drawverts into private storage for nefarious purposes */
599 yDrawVerts = safe_malloc( numBSPDrawVerts * sizeof( bspDrawVert_t ) );
600 memcpy( yDrawVerts, bspDrawVerts, numBSPDrawVerts * sizeof( bspDrawVert_t ) );
602 /* set the entity origins */
603 for( i = 0; i < numEntities; i++ )
605 /* get entity and model */
607 key = ValueForKey( e, "model" );
608 if( key[ 0 ] != '*' )
610 modelnum = atoi( key + 1 );
611 dm = &bspModels[ modelnum ];
613 /* get entity origin */
614 key = ValueForKey( e, "origin" );
615 if( key[ 0 ] == '\0' )
617 GetVectorForKey( e, "origin", origin );
619 /* set origin for all surfaces for this model */
620 for( j = 0; j < dm->numBSPSurfaces; j++ )
623 ds = &bspDrawSurfaces[ dm->firstBSPSurface + j ];
626 for( k = 0; k < ds->numVerts; k++ )
628 f = ds->firstVert + k;
629 VectorAdd( origin, bspDrawVerts[ f ].xyz, yDrawVerts[ f ].xyz );
638 PointToPolygonFormFactor()
639 calculates the area over a point/normal hemisphere a winding covers
640 ydnar: fixme: there has to be a faster way to calculate this
641 without the expensive per-vert sqrts and transcendental functions
642 ydnar 2002-09-30: added -faster switch because only 19% deviance > 10%
643 between this and the approximation
646 #define ONE_OVER_2PI 0.159154942f //% (1.0f / (2.0f * 3.141592657f))
648 float PointToPolygonFormFactor( const vec3_t point, const vec3_t normal, const winding_t *w )
650 vec3_t triVector, triNormal;
652 vec3_t dirs[ MAX_POINTS_ON_WINDING ];
654 float dot, angle, facing;
657 /* this is expensive */
658 for( i = 0; i < w->numpoints; i++ )
660 VectorSubtract( w->p[ i ], point, dirs[ i ] );
661 VectorNormalize( dirs[ i ], dirs[ i ] );
664 /* duplicate first vertex to avoid mod operation */
665 VectorCopy( dirs[ 0 ], dirs[ i ] );
667 /* calculcate relative area */
669 for( i = 0; i < w->numpoints; i++ )
673 dot = DotProduct( dirs[ i ], dirs[ j ] );
675 /* roundoff can cause slight creep, which gives an IND from acos */
678 else if( dot < -1.0f )
684 CrossProduct( dirs[ i ], dirs[ j ], triVector );
685 if( VectorNormalize( triVector, triNormal ) < 0.0001f )
688 facing = DotProduct( normal, triNormal );
689 total += facing * angle;
691 /* ydnar: this was throwing too many errors with radiosity + crappy maps. ignoring it. */
692 if( total > 6.3f || total < -6.3f )
696 /* now in the range of 0 to 1 over the entire incoming hemisphere */
697 //% total /= (2.0f * 3.141592657f);
698 total *= ONE_OVER_2PI;
705 LightContributionTosample()
706 determines the amount of light reaching a sample (luxel or vertex) from a given light
709 int LightContributionToSample( trace_t *trace )
718 light = trace->light;
721 VectorClear( trace->color );
723 /* ydnar: early out */
724 if( !(light->flags & LIGHT_SURFACES) || light->envelope <= 0.0f )
727 /* do some culling checks */
728 if( light->type != EMIT_SUN )
730 /* MrE: if the light is behind the surface */
731 if( trace->twoSided == qfalse )
732 if( DotProduct( light->origin, trace->normal ) - DotProduct( trace->origin, trace->normal ) < 0.0f )
735 /* ydnar: test pvs */
736 if( !ClusterVisible( trace->cluster, light->cluster ) )
740 /* ptpff approximation */
741 if( light->type == EMIT_AREA && faster )
743 /* get direction and distance */
744 VectorCopy( light->origin, trace->end );
745 dist = SetupTrace( trace );
746 if( dist >= light->envelope )
749 /* clamp the distance to prevent super hot spots */
753 /* angle attenuation */
754 angle = DotProduct( trace->normal, trace->direction );
756 /* twosided lighting */
757 if( trace->twoSided )
758 angle = fabs( angle );
761 angle *= -DotProduct( light->normal, trace->direction );
764 add = light->photons / (dist * dist) * angle;
767 /* exact point to polygon form factor */
768 else if( light->type == EMIT_AREA )
775 /* project sample point into light plane */
776 d = DotProduct( trace->origin, light->normal ) - light->dist;
777 //% if( !(light->flags & LIGHT_TWOSIDED) && d < -1.0f )
781 /* sample point behind plane? */
782 if( !(light->flags & LIGHT_TWOSIDED) && d < -1.0f )
785 /* sample plane coincident? */
786 if( d > -3.0f && DotProduct( trace->normal, light->normal ) > 0.9f )
790 /* nudge the point so that it is clearly forward of the light */
791 /* so that surfaces meeting a light emiter don't get black edges */
792 if( d > -8.0f && d < 8.0f )
793 VectorMA( trace->origin, (8.0f - d), light->normal, pushedOrigin );
795 VectorCopy( trace->origin, pushedOrigin );
797 /* get direction and distance */
798 VectorCopy( light->origin, trace->end );
799 dist = SetupTrace( trace );
800 if( dist >= light->envelope )
803 /* calculate the contribution */
804 factor = PointToPolygonFormFactor( pushedOrigin, trace->normal, light->w );
807 else if( factor < 0.0f )
809 /* twosided lighting */
810 if( trace->twoSided || (light->flags & LIGHT_TWOSIDED) )
814 /* push light origin to other side of the plane */
815 VectorMA( light->origin, -2.0f, light->normal, trace->end );
816 dist = SetupTrace( trace );
817 if( dist >= light->envelope )
824 /* ydnar: moved to here */
825 add = factor * light->add;
828 /* point/spot lights */
829 else if( light->type == EMIT_POINT || light->type == EMIT_SPOT )
831 /* get direction and distance */
832 VectorCopy( light->origin, trace->end );
833 dist = SetupTrace( trace );
834 if( dist >= light->envelope )
837 /* clamp the distance to prevent super hot spots */
841 /* angle attenuation */
842 angle = (light->flags & LIGHT_ATTEN_ANGLE) ? DotProduct( trace->normal, trace->direction ) : 1.0f;
843 if( light->angleScale != 0.0f )
845 angle /= light->angleScale;
850 /* twosided lighting */
851 if( trace->twoSided )
852 angle = fabs( angle );
855 if( light->flags & LIGHT_ATTEN_LINEAR )
857 add = angle * light->photons * linearScale - (dist * light->fade);
862 add = light->photons / (dist * dist) * angle;
864 /* handle spotlights */
865 if( light->type == EMIT_SPOT )
867 float distByNormal, radiusAtDist, sampleRadius;
868 vec3_t pointAtDist, distToSample;
871 /* do cone calculation */
872 distByNormal = -DotProduct( trace->displacement, light->normal );
873 if( distByNormal < 0.0f )
875 VectorMA( light->origin, distByNormal, light->normal, pointAtDist );
876 radiusAtDist = light->radiusByDist * distByNormal;
877 VectorSubtract( trace->origin, pointAtDist, distToSample );
878 sampleRadius = VectorLength( distToSample );
880 /* outside the cone */
881 if( sampleRadius >= radiusAtDist )
885 if( sampleRadius > (radiusAtDist - 32.0f) )
886 add *= ((radiusAtDist - sampleRadius) / 32.0f);
890 /* ydnar: sunlight */
891 else if( light->type == EMIT_SUN )
893 /* get origin and direction */
894 VectorAdd( trace->origin, light->origin, trace->end );
895 dist = SetupTrace( trace );
897 /* angle attenuation */
898 angle = (light->flags & LIGHT_ATTEN_ANGLE)
899 ? DotProduct( trace->normal, trace->direction )
902 /* twosided lighting */
903 if( trace->twoSided )
904 angle = fabs( angle );
907 add = light->photons * angle;
912 trace->testAll = qtrue;
913 VectorScale( light->color, add, trace->color );
916 if( trace->testOcclusion && !trace->forceSunlight )
920 if( !(trace->compileFlags & C_SKY) || trace->opaque )
922 VectorClear( trace->color );
927 /* return to sender */
931 /* ydnar: changed to a variable number */
932 if( add <= 0.0f || (add <= light->falloffTolerance && (light->flags & LIGHT_FAST_ACTUAL)) )
936 trace->testAll = qfalse;
937 VectorScale( light->color, add, trace->color );
941 if( trace->passSolid || trace->opaque )
943 VectorClear( trace->color );
947 /* return to sender */
955 determines the amount of light reaching a sample (luxel or vertex)
958 void LightingAtSample( trace_t *trace, byte styles[ MAX_LIGHTMAPS ], vec3_t colors[ MAX_LIGHTMAPS ] )
964 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
965 VectorClear( colors[ lightmapNum ] );
967 /* ydnar: normalmap */
970 colors[ 0 ][ 0 ] = (trace->normal[ 0 ] + 1.0f) * 127.5f;
971 colors[ 0 ][ 1 ] = (trace->normal[ 1 ] + 1.0f) * 127.5f;
972 colors[ 0 ][ 2 ] = (trace->normal[ 2 ] + 1.0f) * 127.5f;
976 /* ydnar: don't bounce ambient all the time */
978 VectorCopy( ambientColor, colors[ 0 ] );
980 /* ydnar: trace to all the list of lights pre-stored in tw */
981 for( i = 0; i < trace->numLights && trace->lights[ i ] != NULL; i++ )
984 trace->light = trace->lights[ i ];
987 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
989 if( styles[ lightmapNum ] == trace->light->style ||
990 styles[ lightmapNum ] == LS_NONE )
994 /* max of MAX_LIGHTMAPS (4) styles allowed to hit a sample */
995 if( lightmapNum >= MAX_LIGHTMAPS )
999 LightContributionToSample( trace );
1000 if( trace->color[ 0 ] == 0.0f && trace->color[ 1 ] == 0.0f && trace->color[ 2 ] == 0.0f )
1003 /* handle negative light */
1004 if( trace->light->flags & LIGHT_NEGATIVE )
1005 VectorScale( trace->color, -1.0f, trace->color );
1008 styles[ lightmapNum ] = trace->light->style;
1011 VectorAdd( colors[ lightmapNum ], trace->color, colors[ lightmapNum ] );
1015 colors[ 0 ][ 0 ] >= 255.0f &&
1016 colors[ 0 ][ 1 ] >= 255.0f &&
1017 colors[ 0 ][ 2 ] >= 255.0f )
1025 LightContributionToPoint()
1026 for a given light, how much light/color reaches a given point in space (with no facing)
1027 note: this is similar to LightContributionToSample() but optimized for omnidirectional sampling
1030 int LightContributionToPoint( trace_t *trace )
1037 light = trace->light;
1040 VectorClear( trace->color );
1042 /* ydnar: early out */
1043 if( !(light->flags & LIGHT_GRID) || light->envelope <= 0.0f )
1046 /* is this a sun? */
1047 if( light->type != EMIT_SUN )
1054 if( !ClusterVisible( trace->cluster, light->cluster ) )
1058 /* ydnar: check origin against light's pvs envelope */
1059 if( trace->origin[ 0 ] > light->maxs[ 0 ] || trace->origin[ 0 ] < light->mins[ 0 ] ||
1060 trace->origin[ 1 ] > light->maxs[ 1 ] || trace->origin[ 1 ] < light->mins[ 1 ] ||
1061 trace->origin[ 2 ] > light->maxs[ 2 ] || trace->origin[ 2 ] < light->mins[ 2 ] )
1067 /* set light origin */
1068 if( light->type == EMIT_SUN )
1069 VectorAdd( trace->origin, light->origin, trace->end );
1071 VectorCopy( light->origin, trace->end );
1074 dist = SetupTrace( trace );
1077 if( dist > light->envelope )
1079 gridEnvelopeCulled++;
1083 /* ptpff approximation */
1084 if( light->type == EMIT_AREA && faster )
1086 /* clamp the distance to prevent super hot spots */
1091 add = light->photons / (dist * dist);
1094 /* exact point to polygon form factor */
1095 else if( light->type == EMIT_AREA )
1098 vec3_t pushedOrigin;
1101 /* see if the point is behind the light */
1102 d = DotProduct( trace->origin, light->normal ) - light->dist;
1103 if( !(light->flags & LIGHT_TWOSIDED) && d < -1.0f )
1106 /* nudge the point so that it is clearly forward of the light */
1107 /* so that surfaces meeting a light emiter don't get black edges */
1108 if( d > -8.0f && d < 8.0f )
1109 VectorMA( trace->origin, (8.0f - d), light->normal, pushedOrigin );
1111 VectorCopy( trace->origin, pushedOrigin );
1113 /* calculate the contribution (ydnar 2002-10-21: [bug 642] bad normal calc) */
1114 factor = PointToPolygonFormFactor( pushedOrigin, trace->direction, light->w );
1115 if( factor == 0.0f )
1117 else if( factor < 0.0f )
1119 if( light->flags & LIGHT_TWOSIDED )
1125 /* ydnar: moved to here */
1126 add = factor * light->add;
1129 /* point/spot lights */
1130 else if( light->type == EMIT_POINT || light->type == EMIT_SPOT )
1132 /* clamp the distance to prevent super hot spots */
1137 if( light->flags & LIGHT_ATTEN_LINEAR )
1139 add = light->photons * linearScale - (dist * light->fade);
1144 add = light->photons / (dist * dist);
1146 /* handle spotlights */
1147 if( light->type == EMIT_SPOT )
1149 float distByNormal, radiusAtDist, sampleRadius;
1150 vec3_t pointAtDist, distToSample;
1153 /* do cone calculation */
1154 distByNormal = -DotProduct( trace->displacement, light->normal );
1155 if( distByNormal < 0.0f )
1157 VectorMA( light->origin, distByNormal, light->normal, pointAtDist );
1158 radiusAtDist = light->radiusByDist * distByNormal;
1159 VectorSubtract( trace->origin, pointAtDist, distToSample );
1160 sampleRadius = VectorLength( distToSample );
1162 /* outside the cone */
1163 if( sampleRadius >= radiusAtDist )
1167 if( sampleRadius > (radiusAtDist - 32.0f) )
1168 add *= ((radiusAtDist - sampleRadius) / 32.0f);
1172 /* ydnar: sunlight */
1173 else if( light->type == EMIT_SUN )
1176 add = light->photons;
1181 trace->testAll = qtrue;
1182 VectorScale( light->color, add, trace->color );
1184 /* trace to point */
1185 if( trace->testOcclusion && !trace->forceSunlight )
1189 if( !(trace->compileFlags & C_SKY) || trace->opaque )
1191 VectorClear( trace->color );
1196 /* return to sender */
1200 /* unknown light type */
1204 /* ydnar: changed to a variable number */
1205 if( add <= 0.0f || (add <= light->falloffTolerance && (light->flags & LIGHT_FAST_ACTUAL)) )
1209 trace->testAll = qfalse;
1210 VectorScale( light->color, add, trace->color );
1214 if( trace->passSolid )
1216 VectorClear( trace->color );
1220 /* we have a valid sample */
1228 grid samples are for quickly determining the lighting
1229 of dynamically placed entities in the world
1232 #define MAX_CONTRIBUTIONS 1024
1242 void TraceGrid( int num )
1244 int i, j, x, y, z, mod, step, numCon, numStyles;
1246 vec3_t baseOrigin, cheapColor, color;
1248 bspGridPoint_t *bgp;
1249 contribution_t contributions[ MAX_CONTRIBUTIONS ];
1253 /* get grid points */
1254 gp = &rawGridPoints[ num ];
1255 bgp = &bspGridPoints[ num ];
1257 /* get grid origin */
1259 z = mod / (gridBounds[ 0 ] * gridBounds[ 1 ]);
1260 mod -= z * (gridBounds[ 0 ] * gridBounds[ 1 ]);
1261 y = mod / gridBounds[ 0 ];
1262 mod -= y * gridBounds[ 0 ];
1265 trace.origin[ 0 ] = gridMins[ 0 ] + x * gridSize[ 0 ];
1266 trace.origin[ 1 ] = gridMins[ 1 ] + y * gridSize[ 1 ];
1267 trace.origin[ 2 ] = gridMins[ 2 ] + z * gridSize[ 2 ];
1269 /* set inhibit sphere */
1270 if( gridSize[ 0 ] > gridSize[ 1 ] && gridSize[ 0 ] > gridSize[ 2 ] )
1271 trace.inhibitRadius = gridSize[ 0 ] * 0.5f;
1272 else if( gridSize[ 1 ] > gridSize[ 0 ] && gridSize[ 1 ] > gridSize[ 2 ] )
1273 trace.inhibitRadius = gridSize[ 1 ] * 0.5f;
1275 trace.inhibitRadius = gridSize[ 2 ] * 0.5f;
1277 /* find point cluster */
1278 trace.cluster = ClusterForPointExt( trace.origin, GRID_EPSILON );
1279 if( trace.cluster < 0 )
1281 /* try to nudge the origin around to find a valid point */
1282 VectorCopy( trace.origin, baseOrigin );
1283 for( step = 9; step <= 18; step += 9 )
1285 for( i = 0; i < 8; i++ )
1287 VectorCopy( baseOrigin, trace.origin );
1289 trace.origin[ 0 ] += step;
1291 trace.origin[ 0 ] -= step;
1294 trace.origin[ 1 ] += step;
1296 trace.origin[ 1 ] -= step;
1299 trace.origin[ 2 ] += step;
1301 trace.origin[ 2 ] -= step;
1303 /* ydnar: changed to find cluster num */
1304 trace.cluster = ClusterForPointExt( trace.origin, VERTEX_EPSILON );
1305 if( trace.cluster >= 0 )
1313 /* can't find a valid point at all */
1319 trace.testOcclusion = !noTrace;
1320 trace.forceSunlight = qfalse;
1321 trace.recvShadows = WORLDSPAWN_RECV_SHADOWS;
1322 trace.numSurfaces = 0;
1323 trace.surfaces = NULL;
1324 trace.numLights = 0;
1325 trace.lights = NULL;
1329 VectorClear( cheapColor );
1331 /* trace to all the lights, find the major light direction, and divide the
1332 total light between that along the direction and the remaining in the ambient */
1333 for( trace.light = lights; trace.light != NULL; trace.light = trace.light->next )
1339 if( !LightContributionToPoint( &trace ) )
1342 /* handle negative light */
1343 if( trace.light->flags & LIGHT_NEGATIVE )
1344 VectorScale( trace.color, -1.0f, trace.color );
1346 /* add a contribution */
1347 VectorCopy( trace.color, contributions[ numCon ].color );
1348 VectorCopy( trace.direction, contributions[ numCon ].dir );
1349 contributions[ numCon ].style = trace.light->style;
1352 /* push average direction around */
1353 addSize = VectorLength( trace.color );
1354 VectorMA( gp->dir, addSize, trace.direction, gp->dir );
1356 /* stop after a while */
1357 if( numCon >= (MAX_CONTRIBUTIONS - 1) )
1360 /* ydnar: cheap mode */
1361 VectorAdd( cheapColor, trace.color, cheapColor );
1362 if( cheapgrid && cheapColor[ 0 ] >= 255.0f && cheapColor[ 1 ] >= 255.0f && cheapColor[ 2 ] >= 255.0f )
1366 /* normalize to get primary light direction */
1367 VectorNormalize( gp->dir, gp->dir );
1369 /* now that we have identified the primary light direction,
1370 go back and separate all the light into directed and ambient */
1372 for( i = 0; i < numCon; i++ )
1374 /* get relative directed strength */
1375 d = DotProduct( contributions[ i ].dir, gp->dir );
1379 /* find appropriate style */
1380 for( j = 0; j < numStyles; j++ )
1382 if( gp->styles[ j ] == contributions[ i ].style )
1386 /* style not found? */
1387 if( j >= numStyles )
1389 /* add a new style */
1390 if( numStyles < MAX_LIGHTMAPS )
1392 gp->styles[ numStyles ] = contributions[ i ].style;
1393 bgp->styles[ numStyles ] = contributions[ i ].style;
1395 //% Sys_Printf( "(%d, %d) ", num, contributions[ i ].style );
1403 /* add the directed color */
1404 VectorMA( gp->directed[ j ], d, contributions[ i ].color, gp->directed[ j ] );
1406 /* ambient light will be at 1/4 the value of directed light */
1407 /* (ydnar: nuke this in favor of more dramatic lighting?) */
1408 d = 0.25f * (1.0f - d);
1409 VectorMA( gp->ambient[ j ], d, contributions[ i ].color, gp->ambient[ j ] );
1413 /* store off sample */
1414 for( i = 0; i < MAX_LIGHTMAPS; i++ )
1416 /* do some fudging to keep the ambient from being too low (2003-07-05: 0.25 -> 0.125) */
1418 VectorMA( gp->ambient[ i ], 0.125f, gp->directed[ i ], gp->ambient[ i ] );
1420 /* set minimum light and copy off to bytes */
1421 VectorCopy( gp->ambient[ i ], color );
1422 for( j = 0; j < 3; j++ )
1423 if( color[ j ] < minGridLight[ j ] )
1424 color[ j ] = minGridLight[ j ];
1425 ColorToBytes( color, bgp->ambient[ i ], 1.0f );
1426 ColorToBytes( gp->directed[ i ], bgp->directed[ i ], 1.0f );
1431 //% Sys_FPrintf( SYS_VRB, "%10d %10d %10d ", &gp->ambient[ 0 ][ 0 ], &gp->ambient[ 0 ][ 1 ], &gp->ambient[ 0 ][ 2 ] );
1432 Sys_FPrintf( SYS_VRB, "%9d Amb: (%03.1f %03.1f %03.1f) Dir: (%03.1f %03.1f %03.1f)\n",
1434 gp->ambient[ 0 ][ 0 ], gp->ambient[ 0 ][ 1 ], gp->ambient[ 0 ][ 2 ],
1435 gp->directed[ 0 ][ 0 ], gp->directed[ 0 ][ 1 ], gp->directed[ 0 ][ 2 ] );
1438 /* store direction */
1439 NormalToLatLong( gp->dir, bgp->latLong );
1446 calculates the size of the lightgrid and allocates memory
1449 void SetupGrid( void )
1452 vec3_t maxs, oldGridSize;
1457 /* don't do this if not grid lighting */
1458 if( noGridLighting )
1461 /* ydnar: set grid size */
1462 value = ValueForKey( &entities[ 0 ], "gridsize" );
1463 if( value[ 0 ] != '\0' )
1464 sscanf( value, "%f %f %f", &gridSize[ 0 ], &gridSize[ 1 ], &gridSize[ 2 ] );
1467 VectorCopy( gridSize, oldGridSize );
1468 for( i = 0; i < 3; i++ )
1469 gridSize[ i ] = gridSize[ i ] >= 8.0f ? floor( gridSize[ i ] ) : 8.0f;
1471 /* ydnar: increase gridSize until grid count is smaller than max allowed */
1472 numRawGridPoints = MAX_MAP_LIGHTGRID + 1;
1474 while( numRawGridPoints > MAX_MAP_LIGHTGRID )
1476 /* get world bounds */
1477 for( i = 0; i < 3; i++ )
1479 gridMins[ i ] = gridSize[ i ] * ceil( bspModels[ 0 ].mins[ i ] / gridSize[ i ] );
1480 maxs[ i ] = gridSize[ i ] * floor( bspModels[ 0 ].maxs[ i ] / gridSize[ i ] );
1481 gridBounds[ i ] = (maxs[ i ] - gridMins[ i ]) / gridSize[ i ] + 1;
1485 numRawGridPoints = gridBounds[ 0 ] * gridBounds[ 1 ] * gridBounds[ 2 ];
1487 /* increase grid size a bit */
1488 if( numRawGridPoints > MAX_MAP_LIGHTGRID )
1489 gridSize[ j++ % 3 ] += 16.0f;
1493 Sys_Printf( "Grid size = { %1.0f, %1.0f, %1.0f }\n", gridSize[ 0 ], gridSize[ 1 ], gridSize[ 2 ] );
1496 if( !VectorCompare( gridSize, oldGridSize ) )
1498 sprintf( temp, "%.0f %.0f %.0f", gridSize[ 0 ], gridSize[ 1 ], gridSize[ 2 ] );
1499 SetKeyValue( &entities[ 0 ], "gridsize", (const char*) temp );
1500 Sys_FPrintf( SYS_VRB, "Storing adjusted grid size\n" );
1503 /* 2nd variable. fixme: is this silly? */
1504 numBSPGridPoints = numRawGridPoints;
1506 /* allocate lightgrid */
1507 rawGridPoints = safe_malloc( numRawGridPoints * sizeof( *rawGridPoints ) );
1508 memset( rawGridPoints, 0, numRawGridPoints * sizeof( *rawGridPoints ) );
1510 if( bspGridPoints != NULL )
1511 free( bspGridPoints );
1512 bspGridPoints = safe_malloc( numBSPGridPoints * sizeof( *bspGridPoints ) );
1513 memset( bspGridPoints, 0, numBSPGridPoints * sizeof( *bspGridPoints ) );
1515 /* clear lightgrid */
1516 for( i = 0; i < numRawGridPoints; i++ )
1518 VectorCopy( ambientColor, rawGridPoints[ i ].ambient[ j ] );
1519 rawGridPoints[ i ].styles[ 0 ] = LS_NORMAL;
1520 bspGridPoints[ i ].styles[ 0 ] = LS_NORMAL;
1521 for( j = 1; j < MAX_LIGHTMAPS; j++ )
1523 rawGridPoints[ i ].styles[ j ] = LS_NONE;
1524 bspGridPoints[ i ].styles[ j ] = LS_NONE;
1529 Sys_Printf( "%9d grid points\n", numRawGridPoints );
1536 does what it says...
1539 void LightWorld( void )
1544 qboolean minVertex, minGrid;
1548 /* ydnar: smooth normals */
1551 Sys_Printf( "--- SmoothNormals ---\n" );
1555 /* determine the number of grid points */
1556 Sys_Printf( "--- SetupGrid ---\n" );
1559 /* find the optional minimum lighting values */
1560 GetVectorForKey( &entities[ 0 ], "_color", color );
1561 if( VectorLength( color ) == 0.0f )
1562 VectorSet( color, 1.0, 1.0, 1.0 );
1565 f = FloatForKey( &entities[ 0 ], "_ambient" );
1567 f = FloatForKey( &entities[ 0 ], "ambient" );
1568 VectorScale( color, f, ambientColor );
1570 /* minvertexlight */
1572 value = ValueForKey( &entities[ 0 ], "_minvertexlight" );
1573 if( value[ 0 ] != '\0' )
1577 VectorScale( color, f, minVertexLight );
1582 value = ValueForKey( &entities[ 0 ], "_mingridlight" );
1583 if( value[ 0 ] != '\0' )
1587 VectorScale( color, f, minGridLight );
1591 value = ValueForKey( &entities[ 0 ], "_minlight" );
1592 if( value[ 0 ] != '\0' )
1595 VectorScale( color, f, minLight );
1596 if( minVertex == qfalse )
1597 VectorScale( color, f, minVertexLight );
1598 if( minGrid == qfalse )
1599 VectorScale( color, f, minGridLight );
1602 /* create world lights */
1603 Sys_FPrintf( SYS_VRB, "--- CreateLights ---\n" );
1604 CreateEntityLights();
1605 CreateSurfaceLights();
1606 Sys_Printf( "%9d point lights\n", numPointLights );
1607 Sys_Printf( "%9d spotlights\n", numSpotLights );
1608 Sys_Printf( "%9d diffuse (area) lights\n", numDiffuseLights );
1609 Sys_Printf( "%9d sun/sky lights\n", numSunLights );
1611 /* calculate lightgrid */
1612 if( !noGridLighting )
1614 /* ydnar: set up light envelopes */
1615 SetupEnvelopes( qtrue, fastgrid );
1617 Sys_Printf( "--- TraceGrid ---\n" );
1618 RunThreadsOnIndividual( numRawGridPoints, qtrue, TraceGrid );
1619 Sys_Printf( "%d x %d x %d = %d grid\n",
1620 gridBounds[ 0 ], gridBounds[ 1 ], gridBounds[ 2 ], numBSPGridPoints );
1622 /* ydnar: emit statistics on light culling */
1623 Sys_FPrintf( SYS_VRB, "%9d grid points envelope culled\n", gridEnvelopeCulled );
1624 Sys_FPrintf( SYS_VRB, "%9d grid points bounds culled\n", gridBoundsCulled );
1627 /* slight optimization to remove a sqrt */
1628 subdivideThreshold *= subdivideThreshold;
1630 /* map the world luxels */
1631 Sys_Printf( "--- MapRawLightmap ---\n" );
1632 RunThreadsOnIndividual( numRawLightmaps, qtrue, MapRawLightmap );
1633 Sys_Printf( "%9d luxels\n", numLuxels );
1634 Sys_Printf( "%9d luxels mapped\n", numLuxelsMapped );
1635 Sys_Printf( "%9d luxels occluded\n", numLuxelsOccluded );
1637 /* ydnar: set up light envelopes */
1638 SetupEnvelopes( qfalse, fast );
1640 /* light up my world */
1641 lightsPlaneCulled = 0;
1642 lightsEnvelopeCulled = 0;
1643 lightsBoundsCulled = 0;
1644 lightsClusterCulled = 0;
1646 Sys_Printf( "--- IlluminateRawLightmap ---\n" );
1647 RunThreadsOnIndividual( numRawLightmaps, qtrue, IlluminateRawLightmap );
1648 Sys_Printf( "%9d luxels illuminated\n", numLuxelsIlluminated );
1650 StitchSurfaceLightmaps();
1652 Sys_Printf( "--- IlluminateVertexes ---\n" );
1653 RunThreadsOnIndividual( numBSPDrawSurfaces, qtrue, IlluminateVertexes );
1654 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
1656 /* ydnar: emit statistics on light culling */
1657 Sys_FPrintf( SYS_VRB, "%9d lights plane culled\n", lightsPlaneCulled );
1658 Sys_FPrintf( SYS_VRB, "%9d lights envelope culled\n", lightsEnvelopeCulled );
1659 Sys_FPrintf( SYS_VRB, "%9d lights bounds culled\n", lightsBoundsCulled );
1660 Sys_FPrintf( SYS_VRB, "%9d lights cluster culled\n", lightsClusterCulled );
1667 /* store off the bsp between bounces */
1668 StoreSurfaceLightmaps();
1669 Sys_Printf( "Writing %s\n", source );
1670 WriteBSPFile( source );
1673 Sys_Printf( "\n--- Radiosity (bounce %d of %d) ---\n", b, bt );
1677 VectorClear( ambientColor );
1679 /* generate diffuse lights */
1681 RadCreateDiffuseLights();
1683 /* setup light envelopes */
1684 SetupEnvelopes( qfalse, fastbounce );
1685 if( numLights == 0 )
1687 Sys_Printf( "No diffuse light to calculate, ending radiosity.\n" );
1691 /* add to lightgrid */
1694 gridEnvelopeCulled = 0;
1695 gridBoundsCulled = 0;
1697 Sys_Printf( "--- BounceGrid ---\n" );
1698 RunThreadsOnIndividual( numRawGridPoints, qtrue, TraceGrid );
1699 Sys_FPrintf( SYS_VRB, "%9d grid points envelope culled\n", gridEnvelopeCulled );
1700 Sys_FPrintf( SYS_VRB, "%9d grid points bounds culled\n", gridBoundsCulled );
1703 /* light up my world */
1704 lightsPlaneCulled = 0;
1705 lightsEnvelopeCulled = 0;
1706 lightsBoundsCulled = 0;
1707 lightsClusterCulled = 0;
1709 Sys_Printf( "--- IlluminateRawLightmap ---\n" );
1710 RunThreadsOnIndividual( numRawLightmaps, qtrue, IlluminateRawLightmap );
1711 Sys_Printf( "%9d luxels illuminated\n", numLuxelsIlluminated );
1712 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
1714 StitchSurfaceLightmaps();
1716 Sys_Printf( "--- IlluminateVertexes ---\n" );
1717 RunThreadsOnIndividual( numBSPDrawSurfaces, qtrue, IlluminateVertexes );
1718 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
1720 /* ydnar: emit statistics on light culling */
1721 Sys_FPrintf( SYS_VRB, "%9d lights plane culled\n", lightsPlaneCulled );
1722 Sys_FPrintf( SYS_VRB, "%9d lights envelope culled\n", lightsEnvelopeCulled );
1723 Sys_FPrintf( SYS_VRB, "%9d lights bounds culled\n", lightsBoundsCulled );
1724 Sys_FPrintf( SYS_VRB, "%9d lights cluster culled\n", lightsClusterCulled );
1736 main routine for light processing
1739 int LightMain( int argc, char **argv )
1743 char mapSource[ 1024 ];
1748 Sys_Printf( "--- Light ---\n" );
1750 /* process commandline arguments */
1751 for( i = 1; i < (argc - 1); i++ )
1753 /* lightsource scaling */
1754 if( !strcmp( argv[ i ], "-point" ) || !strcmp( argv[ i ], "-pointscale" ) )
1756 f = atof( argv[ i + 1 ] );
1758 Sys_Printf( "Point (entity) light scaled by %f to %f\n", f, pointScale );
1762 else if( !strcmp( argv[ i ], "-area" ) || !strcmp( argv[ i ], "-areascale" ) )
1764 f = atof( argv[ i + 1 ] );
1766 Sys_Printf( "Area (shader) light scaled by %f to %f\n", f, areaScale );
1770 else if( !strcmp( argv[ i ], "-sky" ) || !strcmp( argv[ i ], "-skyscale" ) )
1772 f = atof( argv[ i + 1 ] );
1774 Sys_Printf( "Sky/sun light scaled by %f to %f\n", f, skyScale );
1778 else if( !strcmp( argv[ i ], "-bouncescale" ) )
1780 f = atof( argv[ i + 1 ] );
1782 Sys_Printf( "Bounce (radiosity) light scaled by %f to %f\n", f, bounceScale );
1786 else if( !strcmp( argv[ i ], "-scale" ) )
1788 f = atof( argv[ i + 1 ] );
1793 Sys_Printf( "All light scaled by %f\n", f );
1797 /* ydnar switches */
1798 else if( !strcmp( argv[ i ], "-bounce" ) )
1800 bounce = atoi( argv[ i + 1 ] );
1803 else if( bounce > 0 )
1804 Sys_Printf( "Radiosity enabled with %d bounce(s)\n", bounce );
1808 else if( !strcmp( argv[ i ], "-supersample" ) || !strcmp( argv[ i ], "-super" ) )
1810 superSample = atoi( argv[ i + 1 ] );
1811 if( superSample < 1 )
1813 else if( superSample > 1 )
1814 Sys_Printf( "Ordered-grid supersampling enabled with %d sample(s) per lightmap texel\n", (superSample * superSample) );
1818 else if( !strcmp( argv[ i ], "-samples" ) )
1820 lightSamples = atoi( argv[ i + 1 ] );
1821 if( lightSamples < 1 )
1823 else if( lightSamples > 1 )
1824 Sys_Printf( "Adaptive supersampling enabled with %d sample(s) per lightmap texel\n", lightSamples );
1828 else if( !strcmp( argv[ i ], "-filter" ) )
1831 Sys_Printf( "Lightmap filtering enabled\n" );
1834 else if( !strcmp( argv[ i ], "-shadeangle" ) )
1836 shadeAngleDegrees = atof( argv[ i + 1 ] );
1837 if( shadeAngleDegrees < 0.0f )
1838 shadeAngleDegrees = 0.0f;
1839 else if( shadeAngleDegrees > 0.0f )
1842 Sys_Printf( "Phong shading enabled with a breaking angle of %f degrees\n", shadeAngleDegrees );
1847 else if( !strcmp( argv[ i ], "-thresh" ) )
1849 subdivideThreshold = atof( argv[ i + 1 ] );
1850 if( subdivideThreshold < 0 )
1851 subdivideThreshold = DEFAULT_SUBDIVIDE_THRESHOLD;
1853 Sys_Printf( "Subdivision threshold set at %.3f\n", subdivideThreshold );
1857 else if( !strcmp( argv[ i ], "-approx" ) )
1859 approximateTolerance = atoi( argv[ i + 1 ] );
1860 if( approximateTolerance < 0 )
1861 approximateTolerance = 0;
1862 else if( approximateTolerance > 0 )
1863 Sys_Printf( "Approximating lightmaps within a byte tolerance of %d\n", approximateTolerance );
1867 else if( !strcmp( argv[ i ], "-deluxe" ) || !strcmp( argv[ i ], "-deluxemap" ) )
1870 Sys_Printf( "Generating deluxemaps for average light direction\n" );
1873 else if( !strcmp( argv[ i ], "-external" ) )
1875 externalLightmaps = qtrue;
1876 Sys_Printf( "Storing all lightmaps externally\n" );
1879 else if( !strcmp( argv[ i ], "-lightmapsize" ) )
1881 lmCustomSize = atoi( argv[ i + 1 ] );
1883 /* must be a power of 2 and greater than 2 */
1884 if( ((lmCustomSize - 1) & lmCustomSize) || lmCustomSize < 2 )
1886 Sys_Printf( "WARNING: Lightmap size must be a power of 2, greater or equal to 2 pixels.\n" );
1887 lmCustomSize = LIGHTMAP_WIDTH;
1890 Sys_Printf( "Default lightmap size set to %d x %d pixels\n", lmCustomSize, lmCustomSize );
1892 /* enable external lightmaps */
1893 if( lmCustomSize != LIGHTMAP_WIDTH )
1895 externalLightmaps = qtrue;
1896 Sys_Printf( "Storing all lightmaps externally\n" );
1900 /* ydnar: add this to suppress warnings */
1901 else if( !strcmp( argv[ i ], "-custinfoparms") )
1903 Sys_Printf( "Custom info parms enabled\n" );
1904 useCustomInfoParms = qtrue;
1907 else if( !strcmp( argv[ i ], "-wolf" ) )
1909 /* -game should already be set */
1910 game->wolfLight = qtrue;
1911 Sys_Printf( "Enabling Wolf lighting model\n" );
1914 else if( !strcmp( argv[ i ], "-sunonly" ) )
1917 Sys_Printf( "Only computing sunlight\n" );
1920 else if( !strcmp( argv[ i ], "-bounceonly" ) )
1923 Sys_Printf( "Storing bounced light (radiosity) only\n" );
1926 else if( !strcmp( argv[ i ], "-nocollapse" ) )
1929 Sys_Printf( "Identical lightmap collapsing disabled\n" );
1932 else if( !strcmp( argv[ i ], "-shade" ) )
1935 Sys_Printf( "Phong shading enabled\n" );
1938 else if( !strcmp( argv[ i ], "-bouncegrid") )
1942 Sys_Printf( "Grid lighting with radiosity enabled\n" );
1945 else if( !strcmp( argv[ i ], "-smooth" ) )
1948 lightSamples = EXTRA_SCALE;
1949 Sys_Printf( "The -smooth argument is deprecated, use \"-samples 2\" instead\n" );
1952 else if( !strcmp( argv[ i ], "-fast" ) )
1957 Sys_Printf( "Fast mode enabled\n" );
1960 else if( !strcmp( argv[ i ], "-faster" ) )
1966 Sys_Printf( "Faster mode enabled\n" );
1969 else if( !strcmp( argv[ i ], "-fastgrid" ) )
1972 Sys_Printf( "Fast grid lighting enabled\n" );
1975 else if( !strcmp( argv[ i ], "-fastbounce" ) )
1978 Sys_Printf( "Fast bounce mode enabled\n" );
1981 else if( !strcmp( argv[ i ], "-cheap" ) )
1985 Sys_Printf( "Cheap mode enabled\n" );
1988 else if( !strcmp( argv[ i ], "-cheapgrid" ) )
1991 Sys_Printf( "Cheap grid mode enabled\n" );
1994 else if( !strcmp( argv[ i ], "-normalmap" ) )
1997 Sys_Printf( "Storing normal map instead of lightmap\n" );
2000 else if( !strcmp( argv[ i ], "-trisoup" ) )
2003 Sys_Printf( "Converting brush faces to triangle soup\n" );
2006 else if( !strcmp( argv[ i ], "-debug" ) )
2009 Sys_Printf( "Lightmap debugging enabled\n" );
2012 else if( !strcmp( argv[ i ], "-debugsurfaces" ) || !strcmp( argv[ i ], "-debugsurface" ) )
2014 debugSurfaces = qtrue;
2015 Sys_Printf( "Lightmap surface debugging enabled\n" );
2018 else if( !strcmp( argv[ i ], "-debugunused" ) )
2020 debugUnused = qtrue;
2021 Sys_Printf( "Unused luxel debugging enabled\n" );
2024 else if( !strcmp( argv[ i ], "-debugaxis" ) )
2027 Sys_Printf( "Lightmap axis debugging enabled\n" );
2030 else if( !strcmp( argv[ i ], "-debugcluster" ) )
2032 debugCluster = qtrue;
2033 Sys_Printf( "Luxel cluster debugging enabled\n" );
2036 else if( !strcmp( argv[ i ], "-debugorigin" ) )
2038 debugOrigin = qtrue;
2039 Sys_Printf( "Luxel origin debugging enabled\n" );
2042 else if( !strcmp( argv[ i ], "-debugdeluxe" ) )
2045 debugDeluxemap = qtrue;
2046 Sys_Printf( "Deluxemap debugging enabled\n" );
2049 else if( !strcmp( argv[ i ], "-export" ) )
2051 exportLightmaps = qtrue;
2052 Sys_Printf( "Exporting lightmaps\n" );
2055 else if( !strcmp(argv[ i ], "-notrace" ))
2058 Sys_Printf( "Shadow occlusion disabled\n" );
2060 else if( !strcmp(argv[ i ], "-patchshadows" ) )
2062 patchShadows = qtrue;
2063 Sys_Printf( "Patch shadow casting enabled\n" );
2065 else if( !strcmp( argv[ i ], "-extra" ) )
2068 superSample = EXTRA_SCALE; /* ydnar */
2069 Sys_Printf( "The -extra argument is deprecated, use \"-super 2\" instead\n" );
2071 else if( !strcmp( argv[ i ], "-extrawide" ) )
2075 superSample = EXTRAWIDE_SCALE; /* ydnar */
2076 filter = qtrue; /* ydnar */
2077 Sys_Printf( "The -extrawide argument is deprecated, use \"-filter [-super 2]\" instead\n");
2079 else if( !strcmp( argv[ i ], "-samplesize" ) )
2081 sampleSize = atoi( argv[ i + 1 ] );
2082 if( sampleSize < 1 )
2085 Sys_Printf( "Default lightmap sample size set to %dx%d units\n", sampleSize, sampleSize );
2087 else if( !strcmp( argv[ i ], "-novertex" ) )
2089 noVertexLighting = qtrue;
2090 Sys_Printf( "Disabling vertex lighting\n" );
2092 else if( !strcmp( argv[ i ], "-nogrid" ) )
2094 noGridLighting = qtrue;
2095 Sys_Printf( "Disabling grid lighting\n" );
2097 else if( !strcmp( argv[ i ], "-border" ) )
2099 lightmapBorder = qtrue;
2100 Sys_Printf( "Adding debug border to lightmaps\n" );
2102 else if( !strcmp( argv[ i ], "-nosurf" ) )
2105 Sys_Printf( "Not tracing against surfaces\n" );
2107 else if( !strcmp( argv[ i ], "-dump" ) )
2110 Sys_Printf( "Dumping radiosity lights into numbered prefabs\n" );
2112 else if( !strcmp( argv[ i ], "-lomem" ) )
2115 Sys_Printf( "Enabling low-memory (potentially slower) lighting mode\n" );
2119 Sys_Printf( "WARNING: Unknown option \"%s\"\n", argv[ i ] );
2122 /* clean up map name */
2123 strcpy( source, ExpandArg( argv[ i ] ) );
2124 StripExtension( source );
2125 DefaultExtension( source, ".bsp" );
2126 strcpy( mapSource, ExpandArg( argv[ i ] ) );
2127 StripExtension( mapSource );
2128 DefaultExtension( mapSource, ".map" );
2130 /* ydnar: set default sample size */
2131 SetDefaultSampleSize( sampleSize );
2133 /* ydnar: handle shaders */
2134 BeginMapShaderFile( source );
2138 Sys_Printf( "Loading %s\n", source );
2140 /* ydnar: load surface file */
2141 LoadSurfaceExtraFile( source );
2144 LoadBSPFile( source );
2146 /* parse bsp entities */
2150 value = ValueForKey( &entities[ 0 ], "_keepLights" );
2151 if( value[ 0 ] != '1' )
2152 LoadMapFile( mapSource, qtrue );
2154 /* set the entity/model origins and init yDrawVerts */
2157 /* ydnar: set up optimization */
2159 SetupSurfaceLightmaps();
2161 /* initialize the surface facet tracing */
2164 /* light the world */
2167 /* ydnar: store off lightmaps */
2168 StoreSurfaceLightmaps();
2170 /* write out the bsp */
2172 Sys_Printf( "Writing %s\n", source );
2173 WriteBSPFile( source );
2175 /* ydnar: export lightmaps */
2176 if( exportLightmaps && !externalLightmaps )
2179 /* return to sender */