1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 ----------------------------------------------------------------------------------
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
27 ------------------------------------------------------------------------------- */
42 CreateSunLight() - ydnar
43 this creates a sun light
46 static void CreateSunLight( sun_t *sun )
49 float photons, d, angle, elevation, da, de;
59 if( sun->numSamples < 1 )
63 photons = sun->photons / sun->numSamples;
65 /* create the right number of suns */
66 for( i = 0; i < sun->numSamples; i++ )
68 /* calculate sun direction */
70 VectorCopy( sun->direction, direction );
74 sun->direction[ 0 ] = cos( angle ) * cos( elevation );
75 sun->direction[ 1 ] = sin( angle ) * cos( elevation );
76 sun->direction[ 2 ] = sin( elevation );
78 xz_dist = sqrt( x*x + z*z )
79 latitude = atan2( xz_dist, y ) * RADIANS
80 longitude = atan2( x, z ) * RADIANS
83 d = sqrt( sun->direction[ 0 ] * sun->direction[ 0 ] + sun->direction[ 1 ] * sun->direction[ 1 ] );
84 angle = atan2( sun->direction[ 1 ], sun->direction[ 0 ] );
85 elevation = atan2( sun->direction[ 2 ], d );
87 /* jitter the angles (loop to keep random sample within sun->deviance steridians) */
90 da = (Random() * 2.0f - 1.0f) * sun->deviance;
91 de = (Random() * 2.0f - 1.0f) * sun->deviance;
93 while( (da * da + de * de) > (sun->deviance * sun->deviance) );
98 //% Sys_Printf( "%d: Angle: %3.4f Elevation: %3.3f\n", sun->numSamples, (angle / Q_PI * 180.0f), (elevation / Q_PI * 180.0f) );
100 /* create new vector */
101 direction[ 0 ] = cos( angle ) * cos( elevation );
102 direction[ 1 ] = sin( angle ) * cos( elevation );
103 direction[ 2 ] = sin( elevation );
108 light = safe_malloc( sizeof( *light ) );
109 memset( light, 0, sizeof( *light ) );
110 light->next = lights;
113 /* initialize the light */
114 light->flags = LIGHT_SUN_DEFAULT;
115 light->type = EMIT_SUN;
117 light->falloffTolerance = falloffTolerance;
118 light->filterRadius = sun->filterRadius / sun->numSamples;
119 light->style = noStyles ? LS_NORMAL : sun->style;
121 /* set the light's position out to infinity */
122 VectorMA( vec3_origin, (MAX_WORLD_COORD * 8.0f), direction, light->origin ); /* MAX_WORLD_COORD * 2.0f */
124 /* set the facing to be the inverse of the sun direction */
125 VectorScale( direction, -1.0, light->normal );
126 light->dist = DotProduct( light->origin, light->normal );
128 /* set color and photons */
129 VectorCopy( sun->color, light->color );
130 light->photons = photons * skyScale;
134 if( sun->next != NULL )
135 CreateSunLight( sun->next );
141 CreateSkyLights() - ydnar
142 simulates sky light with multiple suns
145 static void CreateSkyLights( vec3_t color, float value, int iterations, float filterRadius, int style )
148 int angleSteps, elevationSteps;
149 float angle, elevation;
150 float angleStep, elevationStep;
156 if( value <= 0.0f || iterations < 2 )
159 /* calculate some stuff */
160 step = 2.0f / (iterations - 1);
163 /* basic sun setup */
164 VectorCopy( color, sun.color );
166 sun.filterRadius = filterRadius;
168 sun.style = noStyles ? LS_NORMAL : style;
172 elevationSteps = iterations - 1;
173 angleSteps = elevationSteps * 4;
175 elevationStep = DEG2RAD( 90.0f / iterations ); /* skip elevation 0 */
176 angleStep = DEG2RAD( 360.0f / angleSteps );
178 /* calc individual sun brightness */
179 numSuns = angleSteps * elevationSteps + 1;
180 sun.photons = value / numSuns;
182 /* iterate elevation */
183 elevation = elevationStep * 0.5f;
185 for( i = 0, elevation = elevationStep * 0.5f; i < elevationSteps; i++ )
188 for( j = 0; j < angleSteps; j++ )
191 sun.direction[ 0 ] = cos( angle ) * cos( elevation );
192 sun.direction[ 1 ] = sin( angle ) * cos( elevation );
193 sun.direction[ 2 ] = sin( elevation );
194 CreateSunLight( &sun );
201 elevation += elevationStep;
202 angle += angleStep / elevationSteps;
205 /* create vertical sun */
206 VectorSet( sun.direction, 0.0f, 0.0f, 1.0f );
207 CreateSunLight( &sun );
217 creates lights from light entities
220 void CreateEntityLights( void )
223 light_t *light, *light2;
229 float intensity, scale, deviance, filterRadius;
230 int spawnflags, flags, numSamples;
234 /* go throught entity list and find lights */
235 for( i = 0; i < numEntities; i++ )
239 name = ValueForKey( e, "classname" );
241 /* ydnar: check for lightJunior */
242 if( Q_strncasecmp( name, "lightJunior", 11 ) == 0 )
244 else if( Q_strncasecmp( name, "light", 5 ) == 0 )
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 )
256 light = safe_malloc( sizeof( *light ) );
257 memset( light, 0, sizeof( *light ) );
258 light->next = lights;
261 /* handle spawnflags */
262 spawnflags = IntForKey( e, "spawnflags" );
264 /* ydnar: quake 3+ light behavior */
265 if( wolfLight == qfalse )
267 /* set default flags */
268 flags = LIGHT_Q3A_DEFAULT;
270 /* linear attenuation? */
273 flags |= LIGHT_ATTEN_LINEAR;
274 flags &= ~LIGHT_ATTEN_ANGLE;
277 /* no angle attenuate? */
279 flags &= ~LIGHT_ATTEN_ANGLE;
282 /* ydnar: wolf light behavior */
285 /* set default flags */
286 flags = LIGHT_WOLF_DEFAULT;
288 /* inverse distance squared attenuation? */
291 flags &= ~LIGHT_ATTEN_LINEAR;
292 flags |= LIGHT_ATTEN_ANGLE;
295 /* angle attenuate? */
297 flags |= LIGHT_ATTEN_ANGLE;
300 /* other flags (borrowed from wolf) */
302 /* wolf dark light? */
303 if( (spawnflags & 4) || (spawnflags & 8) )
307 if( spawnflags & 16 )
308 flags &= ~LIGHT_GRID;
314 flags &= ~LIGHT_SURFACES;
317 /* store the flags */
318 light->flags = flags;
320 /* ydnar: set fade key (from wolf) */
322 if( light->flags & LIGHT_ATTEN_LINEAR )
324 light->fade = FloatForKey( e, "fade" );
325 if( light->fade == 0.0f )
329 /* ydnar: set angle scaling (from vlight) */
330 light->angleScale = FloatForKey( e, "_anglescale" );
331 if( light->angleScale != 0.0f )
332 light->flags |= LIGHT_ATTEN_ANGLE;
335 GetVectorForKey( e, "origin", light->origin);
336 light->style = IntForKey( e, "_style" );
337 if( light->style == LS_NORMAL )
338 light->style = IntForKey( e, "style" );
339 if( light->style < LS_NORMAL || light->style >= LS_NONE )
340 Error( "Invalid lightstyle (%d) on entity %d", light->style, i );
344 light->style = LS_NORMAL;
346 /* set light intensity */
347 intensity = FloatForKey( e, "_light" );
348 if( intensity == 0.0f )
349 intensity = FloatForKey( e, "light" );
350 if( intensity == 0.0f)
353 /* ydnar: set light scale (sof2) */
354 scale = FloatForKey( e, "scale" );
359 /* ydnar: get deviance and samples */
360 deviance = FloatForKey( e, "_deviance" );
361 if( deviance == 0.0f )
362 deviance = FloatForKey( e, "_deviation" );
363 if( deviance == 0.0f )
364 deviance = FloatForKey( e, "_jitter" );
365 numSamples = IntForKey( e, "_samples" );
366 if( deviance < 0.0f || numSamples < 1 )
371 intensity /= numSamples;
373 /* ydnar: get filter radius */
374 filterRadius = FloatForKey( e, "_filterradius" );
375 if( filterRadius == 0.0f )
376 filterRadius = FloatForKey( e, "_filteradius" );
377 if( filterRadius == 0.0f )
378 filterRadius = FloatForKey( e, "_filter" );
379 if( filterRadius < 0.0f )
381 light->filterRadius = filterRadius;
383 /* set light color */
384 _color = ValueForKey( e, "_color" );
385 if( _color && _color[ 0 ] )
387 sscanf( _color, "%f %f %f", &light->color[ 0 ], &light->color[ 1 ], &light->color[ 2 ] );
388 ColorNormalize( light->color, light->color );
391 light->color[ 0 ] = light->color[ 1 ] = light->color[ 2 ] = 1.0f;
393 intensity = intensity * pointScale;
394 light->photons = intensity;
396 light->type = EMIT_POINT;
398 /* set falloff threshold */
399 light->falloffTolerance = falloffTolerance / numSamples;
401 /* lights with a target will be spotlights */
402 target = ValueForKey( e, "target" );
412 e2 = FindTargetEntity( target );
415 Sys_Printf( "WARNING: light at (%i %i %i) has missing target\n",
416 (int) light->origin[ 0 ], (int) light->origin[ 1 ], (int) light->origin[ 2 ] );
420 /* not a point light */
424 /* make a spotlight */
425 GetVectorForKey( e2, "origin", dest );
426 VectorSubtract( dest, light->origin, light->normal );
427 dist = VectorNormalize( light->normal, light->normal );
428 radius = FloatForKey( e, "radius" );
433 light->radiusByDist = (radius + 16) / dist;
434 light->type = EMIT_SPOT;
436 /* ydnar: wolf mods: spotlights always use nonlinear + angle attenuation */
437 light->flags &= ~LIGHT_ATTEN_LINEAR;
438 light->flags |= LIGHT_ATTEN_ANGLE;
441 /* ydnar: is this a sun? */
442 _sun = ValueForKey( e, "_sun" );
443 if( _sun[ 0 ] == '1' )
445 /* not a spot light */
448 /* unlink this light */
449 lights = light->next;
452 VectorScale( light->normal, -1.0f, sun.direction );
453 VectorCopy( light->color, sun.color );
454 sun.photons = (intensity / pointScale);
455 sun.deviance = deviance / 180.0f * Q_PI;
456 sun.numSamples = numSamples;
457 sun.style = noStyles ? LS_NORMAL : light->style;
460 /* make a sun light */
461 CreateSunLight( &sun );
463 /* free original light */
467 /* skip the rest of this love story */
473 /* jitter the light */
474 for( j = 1; j < numSamples; j++ )
477 light2 = safe_malloc( sizeof( *light ) );
478 memcpy( light2, light, sizeof( *light ) );
479 light2->next = lights;
483 if( light->type == EMIT_SPOT )
489 light2->origin[ 0 ] = light->origin[ 0 ] + (Random() * 2.0f - 1.0f) * deviance;
490 light2->origin[ 1 ] = light->origin[ 1 ] + (Random() * 2.0f - 1.0f) * deviance;
491 light2->origin[ 2 ] = light->origin[ 2 ] + (Random() * 2.0f - 1.0f) * deviance;
499 CreateSurfaceLights() - ydnar
500 this hijacks the radiosity code to generate surface lights for first pass
503 #define APPROX_BOUNCE 1.0f
505 void CreateSurfaceLights( void )
508 bspDrawSurface_t *ds;
518 /* get sun shader supressor */
519 nss = ValueForKey( &entities[ 0 ], "_noshadersun" );
521 /* walk the list of surfaces */
522 for( i = 0; i < numBSPDrawSurfaces; i++ )
524 /* get surface and other bits */
525 ds = &bspDrawSurfaces[ i ];
526 info = &surfaceInfos[ i ];
530 if( si->sun != NULL && nss[ 0 ] != '1' )
532 Sys_FPrintf( SYS_VRB, "Sun: %s\n", si->shader );
533 CreateSunLight( si->sun );
534 si->sun = NULL; /* FIXME: leak! */
538 if( si->skyLightValue > 0.0f )
540 Sys_FPrintf( SYS_VRB, "Sky: %s\n", si->shader );
541 CreateSkyLights( si->color, si->skyLightValue, si->skyLightIterations, si->lightFilterRadius, si->lightStyle );
542 si->skyLightValue = 0.0f; /* FIXME: hack! */
545 /* try to early out */
549 /* autosprite shaders become point lights */
552 /* create an average xyz */
553 VectorAdd( info->mins, info->maxs, origin );
554 VectorScale( origin, 0.5f, origin );
557 light = safe_malloc( sizeof( *light ) );
558 memset( light, 0, sizeof( *light ) );
559 light->next = lights;
563 light->flags = LIGHT_Q3A_DEFAULT;
564 light->type = EMIT_POINT;
565 light->photons = si->value * pointScale;
568 VectorCopy( origin, light->origin );
569 VectorCopy( si->color, light->color );
570 light->falloffTolerance = falloffTolerance;
571 light->style = si->lightStyle;
573 /* add to point light count and continue */
578 /* get subdivision amount */
579 if( si->lightSubdivide > 0 )
580 subdivide = si->lightSubdivide;
582 subdivide = defaultLightSubdivide;
585 switch( ds->surfaceType )
588 case MST_TRIANGLE_SOUP:
589 RadLightForTriangles( i, 0, info->lm, si, APPROX_BOUNCE, subdivide, &cw );
593 RadLightForPatch( i, 0, info->lm, si, APPROX_BOUNCE, subdivide, &cw );
606 find the offset values for inline models
609 void SetEntityOrigins( void )
617 bspDrawSurface_t *ds;
620 /* ydnar: copy drawverts into private storage for nefarious purposes */
621 yDrawVerts = safe_malloc( numBSPDrawVerts * sizeof( bspDrawVert_t ) );
622 memcpy( yDrawVerts, bspDrawVerts, numBSPDrawVerts * sizeof( bspDrawVert_t ) );
624 /* set the entity origins */
625 for( i = 0; i < numEntities; i++ )
627 /* get entity and model */
629 key = ValueForKey( e, "model" );
630 if( key[ 0 ] != '*' )
632 modelnum = atoi( key + 1 );
633 dm = &bspModels[ modelnum ];
635 /* get entity origin */
636 key = ValueForKey( e, "origin" );
637 if( key[ 0 ] == '\0' )
639 GetVectorForKey( e, "origin", origin );
641 /* set origin for all surfaces for this model */
642 for( j = 0; j < dm->numBSPSurfaces; j++ )
645 ds = &bspDrawSurfaces[ dm->firstBSPSurface + j ];
648 for( k = 0; k < ds->numVerts; k++ )
650 f = ds->firstVert + k;
651 VectorAdd( origin, bspDrawVerts[ f ].xyz, yDrawVerts[ f ].xyz );
660 PointToPolygonFormFactor()
661 calculates the area over a point/normal hemisphere a winding covers
662 ydnar: fixme: there has to be a faster way to calculate this
663 without the expensive per-vert sqrts and transcendental functions
664 ydnar 2002-09-30: added -faster switch because only 19% deviance > 10%
665 between this and the approximation
668 #define ONE_OVER_2PI 0.159154942f //% (1.0f / (2.0f * 3.141592657f))
670 float PointToPolygonFormFactor( const vec3_t point, const vec3_t normal, const winding_t *w )
672 vec3_t triVector, triNormal;
674 vec3_t dirs[ MAX_POINTS_ON_WINDING ];
676 float dot, angle, facing;
679 /* this is expensive */
680 for( i = 0; i < w->numpoints; i++ )
682 VectorSubtract( w->p[ i ], point, dirs[ i ] );
683 VectorNormalize( dirs[ i ], dirs[ i ] );
686 /* duplicate first vertex to avoid mod operation */
687 VectorCopy( dirs[ 0 ], dirs[ i ] );
689 /* calculcate relative area */
691 for( i = 0; i < w->numpoints; i++ )
695 dot = DotProduct( dirs[ i ], dirs[ j ] );
697 /* roundoff can cause slight creep, which gives an IND from acos */
700 else if( dot < -1.0f )
706 CrossProduct( dirs[ i ], dirs[ j ], triVector );
707 if( VectorNormalize( triVector, triNormal ) < 0.0001f )
710 facing = DotProduct( normal, triNormal );
711 total += facing * angle;
713 /* ydnar: this was throwing too many errors with radiosity + crappy maps. ignoring it. */
714 if( total > 6.3f || total < -6.3f )
718 /* now in the range of 0 to 1 over the entire incoming hemisphere */
719 //% total /= (2.0f * 3.141592657f);
720 total *= ONE_OVER_2PI;
727 LightContributionTosample()
728 determines the amount of light reaching a sample (luxel or vertex) from a given light
731 int LightContributionToSample( trace_t *trace )
740 light = trace->light;
743 VectorClear( trace->color );
745 /* ydnar: early out */
746 if( !(light->flags & LIGHT_SURFACES) || light->envelope <= 0.0f )
749 /* do some culling checks */
750 if( light->type != EMIT_SUN )
752 /* MrE: if the light is behind the surface */
753 if( trace->twoSided == qfalse )
754 if( DotProduct( light->origin, trace->normal ) - DotProduct( trace->origin, trace->normal ) < 0.0f )
757 /* ydnar: test pvs */
758 if( !ClusterVisible( trace->cluster, light->cluster ) )
762 /* exact point to polygon form factor */
763 if( light->type == EMIT_AREA )
770 /* project sample point into light plane */
771 d = DotProduct( trace->origin, light->normal ) - light->dist;
774 /* sample point behind plane? */
775 if( !(light->flags & LIGHT_TWOSIDED) && d < -1.0f )
778 /* sample plane coincident? */
779 if( d > -3.0f && DotProduct( trace->normal, light->normal ) > 0.9f )
783 /* nudge the point so that it is clearly forward of the light */
784 /* so that surfaces meeting a light emiter don't get black edges */
785 if( d > -8.0f && d < 8.0f )
786 VectorMA( trace->origin, (8.0f - d), light->normal, pushedOrigin );
788 VectorCopy( trace->origin, pushedOrigin );
790 /* get direction and distance */
791 VectorCopy( light->origin, trace->end );
792 dist = SetupTrace( trace );
793 if( dist >= light->envelope )
796 /* ptpff approximation */
799 /* angle attenuation */
800 angle = DotProduct( trace->normal, trace->direction );
802 /* twosided lighting */
803 if( trace->twoSided )
804 angle = fabs( angle );
807 angle *= -DotProduct( light->normal, trace->direction );
810 else if( angle < 0.0f &&
811 (trace->twoSided || (light->flags & LIGHT_TWOSIDED)) )
813 add = light->photons / (dist * dist) * angle;
817 /* calculate the contribution */
818 factor = PointToPolygonFormFactor( pushedOrigin, trace->normal, light->w );
821 else if( factor < 0.0f )
823 /* twosided lighting */
824 if( trace->twoSided || (light->flags & LIGHT_TWOSIDED) )
828 /* push light origin to other side of the plane */
829 VectorMA( light->origin, -2.0f, light->normal, trace->end );
830 dist = SetupTrace( trace );
831 if( dist >= light->envelope )
838 /* ydnar: moved to here */
839 add = factor * light->add;
843 /* point/spot lights */
844 else if( light->type == EMIT_POINT || light->type == EMIT_SPOT )
846 /* get direction and distance */
847 VectorCopy( light->origin, trace->end );
848 dist = SetupTrace( trace );
849 if( dist >= light->envelope )
852 /* clamp the distance to prevent super hot spots */
856 /* angle attenuation */
857 angle = (light->flags & LIGHT_ATTEN_ANGLE) ? DotProduct( trace->normal, trace->direction ) : 1.0f;
858 if( light->angleScale != 0.0f )
860 angle /= light->angleScale;
865 /* twosided lighting */
866 if( trace->twoSided )
867 angle = fabs( angle );
870 if( light->flags & LIGHT_ATTEN_LINEAR )
872 add = angle * light->photons * linearScale - (dist * light->fade);
877 add = light->photons / (dist * dist) * angle;
879 /* handle spotlights */
880 if( light->type == EMIT_SPOT )
882 float distByNormal, radiusAtDist, sampleRadius;
883 vec3_t pointAtDist, distToSample;
886 /* do cone calculation */
887 distByNormal = -DotProduct( trace->displacement, light->normal );
888 if( distByNormal < 0.0f )
890 VectorMA( light->origin, distByNormal, light->normal, pointAtDist );
891 radiusAtDist = light->radiusByDist * distByNormal;
892 VectorSubtract( trace->origin, pointAtDist, distToSample );
893 sampleRadius = VectorLength( distToSample );
895 /* outside the cone */
896 if( sampleRadius >= radiusAtDist )
900 if( sampleRadius > (radiusAtDist - 32.0f) )
901 add *= ((radiusAtDist - sampleRadius) / 32.0f);
905 /* ydnar: sunlight */
906 else if( light->type == EMIT_SUN )
908 /* get origin and direction */
909 VectorAdd( trace->origin, light->origin, trace->end );
910 dist = SetupTrace( trace );
912 /* angle attenuation */
913 angle = (light->flags & LIGHT_ATTEN_ANGLE)
914 ? DotProduct( trace->normal, trace->direction )
917 /* twosided lighting */
918 if( trace->twoSided )
919 angle = fabs( angle );
922 add = light->photons * angle;
927 trace->testAll = qtrue;
928 VectorScale( light->color, add, trace->color );
931 if( trace->testOcclusion && !trace->forceSunlight )
935 if( !(trace->compileFlags & C_SKY) || trace->opaque )
937 VectorClear( trace->color );
942 /* return to sender */
946 /* ydnar: changed to a variable number */
947 if( add <= 0.0f || (add <= light->falloffTolerance && (light->flags & LIGHT_FAST_ACTUAL)) )
951 trace->testAll = qfalse;
952 VectorScale( light->color, add, trace->color );
956 if( trace->passSolid || trace->opaque )
958 VectorClear( trace->color );
962 /* return to sender */
970 determines the amount of light reaching a sample (luxel or vertex)
973 void LightingAtSample( trace_t *trace, byte styles[ MAX_LIGHTMAPS ], vec3_t colors[ MAX_LIGHTMAPS ] )
979 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
980 VectorClear( colors[ lightmapNum ] );
982 /* ydnar: normalmap */
985 colors[ 0 ][ 0 ] = (trace->normal[ 0 ] + 1.0f) * 127.5f;
986 colors[ 0 ][ 1 ] = (trace->normal[ 1 ] + 1.0f) * 127.5f;
987 colors[ 0 ][ 2 ] = (trace->normal[ 2 ] + 1.0f) * 127.5f;
991 /* ydnar: don't bounce ambient all the time */
993 VectorCopy( ambientColor, colors[ 0 ] );
995 /* ydnar: trace to all the list of lights pre-stored in tw */
996 for( i = 0; i < trace->numLights && trace->lights[ i ] != NULL; i++ )
999 trace->light = trace->lights[ i ];
1002 for( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
1004 if( styles[ lightmapNum ] == trace->light->style ||
1005 styles[ lightmapNum ] == LS_NONE )
1009 /* max of MAX_LIGHTMAPS (4) styles allowed to hit a sample */
1010 if( lightmapNum >= MAX_LIGHTMAPS )
1014 LightContributionToSample( trace );
1015 if( trace->color[ 0 ] == 0.0f && trace->color[ 1 ] == 0.0f && trace->color[ 2 ] == 0.0f )
1018 /* handle negative light */
1019 if( trace->light->flags & LIGHT_NEGATIVE )
1020 VectorScale( trace->color, -1.0f, trace->color );
1023 styles[ lightmapNum ] = trace->light->style;
1026 VectorAdd( colors[ lightmapNum ], trace->color, colors[ lightmapNum ] );
1030 colors[ 0 ][ 0 ] >= 255.0f &&
1031 colors[ 0 ][ 1 ] >= 255.0f &&
1032 colors[ 0 ][ 2 ] >= 255.0f )
1040 LightContributionToPoint()
1041 for a given light, how much light/color reaches a given point in space (with no facing)
1042 note: this is similar to LightContributionToSample() but optimized for omnidirectional sampling
1045 int LightContributionToPoint( trace_t *trace )
1052 light = trace->light;
1055 VectorClear( trace->color );
1057 /* ydnar: early out */
1058 if( !(light->flags & LIGHT_GRID) || light->envelope <= 0.0f )
1061 /* is this a sun? */
1062 if( light->type != EMIT_SUN )
1069 if( !ClusterVisible( trace->cluster, light->cluster ) )
1073 /* ydnar: check origin against light's pvs envelope */
1074 if( trace->origin[ 0 ] > light->maxs[ 0 ] || trace->origin[ 0 ] < light->mins[ 0 ] ||
1075 trace->origin[ 1 ] > light->maxs[ 1 ] || trace->origin[ 1 ] < light->mins[ 1 ] ||
1076 trace->origin[ 2 ] > light->maxs[ 2 ] || trace->origin[ 2 ] < light->mins[ 2 ] )
1082 /* set light origin */
1083 if( light->type == EMIT_SUN )
1084 VectorAdd( trace->origin, light->origin, trace->end );
1086 VectorCopy( light->origin, trace->end );
1089 dist = SetupTrace( trace );
1092 if( dist > light->envelope )
1094 gridEnvelopeCulled++;
1098 /* ptpff approximation */
1099 if( light->type == EMIT_AREA && faster )
1101 /* clamp the distance to prevent super hot spots */
1106 add = light->photons / (dist * dist);
1109 /* exact point to polygon form factor */
1110 else if( light->type == EMIT_AREA )
1113 vec3_t pushedOrigin;
1116 /* see if the point is behind the light */
1117 d = DotProduct( trace->origin, light->normal ) - light->dist;
1118 if( !(light->flags & LIGHT_TWOSIDED) && d < -1.0f )
1121 /* nudge the point so that it is clearly forward of the light */
1122 /* so that surfaces meeting a light emiter don't get black edges */
1123 if( d > -8.0f && d < 8.0f )
1124 VectorMA( trace->origin, (8.0f - d), light->normal, pushedOrigin );
1126 VectorCopy( trace->origin, pushedOrigin );
1128 /* calculate the contribution (ydnar 2002-10-21: [bug 642] bad normal calc) */
1129 factor = PointToPolygonFormFactor( pushedOrigin, trace->direction, light->w );
1130 if( factor == 0.0f )
1132 else if( factor < 0.0f )
1134 if( light->flags & LIGHT_TWOSIDED )
1140 /* ydnar: moved to here */
1141 add = factor * light->add;
1144 /* point/spot lights */
1145 else if( light->type == EMIT_POINT || light->type == EMIT_SPOT )
1147 /* clamp the distance to prevent super hot spots */
1152 if( light->flags & LIGHT_ATTEN_LINEAR )
1154 add = light->photons * linearScale - (dist * light->fade);
1159 add = light->photons / (dist * dist);
1161 /* handle spotlights */
1162 if( light->type == EMIT_SPOT )
1164 float distByNormal, radiusAtDist, sampleRadius;
1165 vec3_t pointAtDist, distToSample;
1168 /* do cone calculation */
1169 distByNormal = -DotProduct( trace->displacement, light->normal );
1170 if( distByNormal < 0.0f )
1172 VectorMA( light->origin, distByNormal, light->normal, pointAtDist );
1173 radiusAtDist = light->radiusByDist * distByNormal;
1174 VectorSubtract( trace->origin, pointAtDist, distToSample );
1175 sampleRadius = VectorLength( distToSample );
1177 /* outside the cone */
1178 if( sampleRadius >= radiusAtDist )
1182 if( sampleRadius > (radiusAtDist - 32.0f) )
1183 add *= ((radiusAtDist - sampleRadius) / 32.0f);
1187 /* ydnar: sunlight */
1188 else if( light->type == EMIT_SUN )
1191 add = light->photons;
1196 trace->testAll = qtrue;
1197 VectorScale( light->color, add, trace->color );
1199 /* trace to point */
1200 if( trace->testOcclusion && !trace->forceSunlight )
1204 if( !(trace->compileFlags & C_SKY) || trace->opaque )
1206 VectorClear( trace->color );
1211 /* return to sender */
1215 /* unknown light type */
1219 /* ydnar: changed to a variable number */
1220 if( add <= 0.0f || (add <= light->falloffTolerance && (light->flags & LIGHT_FAST_ACTUAL)) )
1224 trace->testAll = qfalse;
1225 VectorScale( light->color, add, trace->color );
1229 if( trace->passSolid )
1231 VectorClear( trace->color );
1235 /* we have a valid sample */
1243 grid samples are for quickly determining the lighting
1244 of dynamically placed entities in the world
1247 #define MAX_CONTRIBUTIONS 1024
1257 void TraceGrid( int num )
1259 int i, j, x, y, z, mod, step, numCon, numStyles;
1261 vec3_t baseOrigin, cheapColor, color;
1263 bspGridPoint_t *bgp;
1264 contribution_t contributions[ MAX_CONTRIBUTIONS ];
1268 /* get grid points */
1269 gp = &rawGridPoints[ num ];
1270 bgp = &bspGridPoints[ num ];
1272 /* get grid origin */
1274 z = mod / (gridBounds[ 0 ] * gridBounds[ 1 ]);
1275 mod -= z * (gridBounds[ 0 ] * gridBounds[ 1 ]);
1276 y = mod / gridBounds[ 0 ];
1277 mod -= y * gridBounds[ 0 ];
1280 trace.origin[ 0 ] = gridMins[ 0 ] + x * gridSize[ 0 ];
1281 trace.origin[ 1 ] = gridMins[ 1 ] + y * gridSize[ 1 ];
1282 trace.origin[ 2 ] = gridMins[ 2 ] + z * gridSize[ 2 ];
1284 /* set inhibit sphere */
1285 if( gridSize[ 0 ] > gridSize[ 1 ] && gridSize[ 0 ] > gridSize[ 2 ] )
1286 trace.inhibitRadius = gridSize[ 0 ] * 0.5f;
1287 else if( gridSize[ 1 ] > gridSize[ 0 ] && gridSize[ 1 ] > gridSize[ 2 ] )
1288 trace.inhibitRadius = gridSize[ 1 ] * 0.5f;
1290 trace.inhibitRadius = gridSize[ 2 ] * 0.5f;
1292 /* find point cluster */
1293 trace.cluster = ClusterForPointExt( trace.origin, GRID_EPSILON );
1294 if( trace.cluster < 0 )
1296 /* try to nudge the origin around to find a valid point */
1297 VectorCopy( trace.origin, baseOrigin );
1298 for( step = 9; step <= 18; step += 9 )
1300 for( i = 0; i < 8; i++ )
1302 VectorCopy( baseOrigin, trace.origin );
1304 trace.origin[ 0 ] += step;
1306 trace.origin[ 0 ] -= step;
1309 trace.origin[ 1 ] += step;
1311 trace.origin[ 1 ] -= step;
1314 trace.origin[ 2 ] += step;
1316 trace.origin[ 2 ] -= step;
1318 /* ydnar: changed to find cluster num */
1319 trace.cluster = ClusterForPointExt( trace.origin, VERTEX_EPSILON );
1320 if( trace.cluster >= 0 )
1328 /* can't find a valid point at all */
1334 trace.testOcclusion = !noTrace;
1335 trace.forceSunlight = qfalse;
1336 trace.recvShadows = WORLDSPAWN_RECV_SHADOWS;
1337 trace.numSurfaces = 0;
1338 trace.surfaces = NULL;
1339 trace.numLights = 0;
1340 trace.lights = NULL;
1344 VectorClear( cheapColor );
1346 /* trace to all the lights, find the major light direction, and divide the
1347 total light between that along the direction and the remaining in the ambient */
1348 for( trace.light = lights; trace.light != NULL; trace.light = trace.light->next )
1354 if( !LightContributionToPoint( &trace ) )
1357 /* handle negative light */
1358 if( trace.light->flags & LIGHT_NEGATIVE )
1359 VectorScale( trace.color, -1.0f, trace.color );
1361 /* add a contribution */
1362 VectorCopy( trace.color, contributions[ numCon ].color );
1363 VectorCopy( trace.direction, contributions[ numCon ].dir );
1364 contributions[ numCon ].style = trace.light->style;
1367 /* push average direction around */
1368 addSize = VectorLength( trace.color );
1369 VectorMA( gp->dir, addSize, trace.direction, gp->dir );
1371 /* stop after a while */
1372 if( numCon >= (MAX_CONTRIBUTIONS - 1) )
1375 /* ydnar: cheap mode */
1376 VectorAdd( cheapColor, trace.color, cheapColor );
1377 if( cheapgrid && cheapColor[ 0 ] >= 255.0f && cheapColor[ 1 ] >= 255.0f && cheapColor[ 2 ] >= 255.0f )
1381 /* normalize to get primary light direction */
1382 VectorNormalize( gp->dir, gp->dir );
1384 /* now that we have identified the primary light direction,
1385 go back and separate all the light into directed and ambient */
1387 for( i = 0; i < numCon; i++ )
1389 /* get relative directed strength */
1390 d = DotProduct( contributions[ i ].dir, gp->dir );
1394 /* find appropriate style */
1395 for( j = 0; j < numStyles; j++ )
1397 if( gp->styles[ j ] == contributions[ i ].style )
1401 /* style not found? */
1402 if( j >= numStyles )
1404 /* add a new style */
1405 if( numStyles < MAX_LIGHTMAPS )
1407 gp->styles[ numStyles ] = contributions[ i ].style;
1408 bgp->styles[ numStyles ] = contributions[ i ].style;
1410 //% Sys_Printf( "(%d, %d) ", num, contributions[ i ].style );
1418 /* add the directed color */
1419 VectorMA( gp->directed[ j ], d, contributions[ i ].color, gp->directed[ j ] );
1421 /* ambient light will be at 1/4 the value of directed light */
1422 /* (ydnar: nuke this in favor of more dramatic lighting?) */
1423 d = 0.25f * (1.0f - d);
1424 VectorMA( gp->ambient[ j ], d, contributions[ i ].color, gp->ambient[ j ] );
1428 /* store off sample */
1429 for( i = 0; i < MAX_LIGHTMAPS; i++ )
1431 /* do some fudging to keep the ambient from being too low (2003-07-05: 0.25 -> 0.125) */
1433 VectorMA( gp->ambient[ i ], 0.125f, gp->directed[ i ], gp->ambient[ i ] );
1435 /* set minimum light and copy off to bytes */
1436 VectorCopy( gp->ambient[ i ], color );
1437 for( j = 0; j < 3; j++ )
1438 if( color[ j ] < minGridLight[ j ] )
1439 color[ j ] = minGridLight[ j ];
1440 ColorToBytes( color, bgp->ambient[ i ], 1.0f );
1441 ColorToBytes( gp->directed[ i ], bgp->directed[ i ], 1.0f );
1446 //% Sys_FPrintf( SYS_VRB, "%10d %10d %10d ", &gp->ambient[ 0 ][ 0 ], &gp->ambient[ 0 ][ 1 ], &gp->ambient[ 0 ][ 2 ] );
1447 Sys_FPrintf( SYS_VRB, "%9d Amb: (%03.1f %03.1f %03.1f) Dir: (%03.1f %03.1f %03.1f)\n",
1449 gp->ambient[ 0 ][ 0 ], gp->ambient[ 0 ][ 1 ], gp->ambient[ 0 ][ 2 ],
1450 gp->directed[ 0 ][ 0 ], gp->directed[ 0 ][ 1 ], gp->directed[ 0 ][ 2 ] );
1453 /* store direction */
1455 NormalToLatLong( gp->dir, bgp->latLong );
1462 calculates the size of the lightgrid and allocates memory
1465 void SetupGrid( void )
1468 vec3_t maxs, oldGridSize;
1473 /* don't do this if not grid lighting */
1474 if( noGridLighting )
1477 /* ydnar: set grid size */
1478 value = ValueForKey( &entities[ 0 ], "gridsize" );
1479 if( value[ 0 ] != '\0' )
1480 sscanf( value, "%f %f %f", &gridSize[ 0 ], &gridSize[ 1 ], &gridSize[ 2 ] );
1483 VectorCopy( gridSize, oldGridSize );
1484 for( i = 0; i < 3; i++ )
1485 gridSize[ i ] = gridSize[ i ] >= 8.0f ? floor( gridSize[ i ] ) : 8.0f;
1487 /* ydnar: increase gridSize until grid count is smaller than max allowed */
1488 numRawGridPoints = MAX_MAP_LIGHTGRID + 1;
1490 while( numRawGridPoints > MAX_MAP_LIGHTGRID )
1492 /* get world bounds */
1493 for( i = 0; i < 3; i++ )
1495 gridMins[ i ] = gridSize[ i ] * ceil( bspModels[ 0 ].mins[ i ] / gridSize[ i ] );
1496 maxs[ i ] = gridSize[ i ] * floor( bspModels[ 0 ].maxs[ i ] / gridSize[ i ] );
1497 gridBounds[ i ] = (maxs[ i ] - gridMins[ i ]) / gridSize[ i ] + 1;
1501 numRawGridPoints = gridBounds[ 0 ] * gridBounds[ 1 ] * gridBounds[ 2 ];
1503 /* increase grid size a bit */
1504 if( numRawGridPoints > MAX_MAP_LIGHTGRID )
1505 gridSize[ j++ % 3 ] += 16.0f;
1509 Sys_Printf( "Grid size = { %1.0f, %1.0f, %1.0f }\n", gridSize[ 0 ], gridSize[ 1 ], gridSize[ 2 ] );
1512 if( !VectorCompare( gridSize, oldGridSize ) )
1514 sprintf( temp, "%.0f %.0f %.0f", gridSize[ 0 ], gridSize[ 1 ], gridSize[ 2 ] );
1515 SetKeyValue( &entities[ 0 ], "gridsize", (const char*) temp );
1516 Sys_FPrintf( SYS_VRB, "Storing adjusted grid size\n" );
1519 /* 2nd variable. fixme: is this silly? */
1520 numBSPGridPoints = numRawGridPoints;
1522 /* allocate lightgrid */
1523 rawGridPoints = safe_malloc( numRawGridPoints * sizeof( *rawGridPoints ) );
1524 memset( rawGridPoints, 0, numRawGridPoints * sizeof( *rawGridPoints ) );
1526 if( bspGridPoints != NULL )
1527 free( bspGridPoints );
1528 bspGridPoints = safe_malloc( numBSPGridPoints * sizeof( *bspGridPoints ) );
1529 memset( bspGridPoints, 0, numBSPGridPoints * sizeof( *bspGridPoints ) );
1531 /* clear lightgrid */
1532 for( i = 0; i < numRawGridPoints; i++ )
1534 VectorCopy( ambientColor, rawGridPoints[ i ].ambient[ j ] );
1535 rawGridPoints[ i ].styles[ 0 ] = LS_NORMAL;
1536 bspGridPoints[ i ].styles[ 0 ] = LS_NORMAL;
1537 for( j = 1; j < MAX_LIGHTMAPS; j++ )
1539 rawGridPoints[ i ].styles[ j ] = LS_NONE;
1540 bspGridPoints[ i ].styles[ j ] = LS_NONE;
1545 Sys_Printf( "%9d grid points\n", numRawGridPoints );
1552 does what it says...
1555 void LightWorld( void )
1560 qboolean minVertex, minGrid;
1564 /* ydnar: smooth normals */
1567 Sys_Printf( "--- SmoothNormals ---\n" );
1571 /* determine the number of grid points */
1572 Sys_Printf( "--- SetupGrid ---\n" );
1575 /* find the optional minimum lighting values */
1576 GetVectorForKey( &entities[ 0 ], "_color", color );
1577 if( VectorLength( color ) == 0.0f )
1578 VectorSet( color, 1.0, 1.0, 1.0 );
1581 f = FloatForKey( &entities[ 0 ], "_ambient" );
1583 f = FloatForKey( &entities[ 0 ], "ambient" );
1584 VectorScale( color, f, ambientColor );
1586 /* minvertexlight */
1588 value = ValueForKey( &entities[ 0 ], "_minvertexlight" );
1589 if( value[ 0 ] != '\0' )
1593 VectorScale( color, f, minVertexLight );
1598 value = ValueForKey( &entities[ 0 ], "_mingridlight" );
1599 if( value[ 0 ] != '\0' )
1603 VectorScale( color, f, minGridLight );
1607 value = ValueForKey( &entities[ 0 ], "_minlight" );
1608 if( value[ 0 ] != '\0' )
1611 VectorScale( color, f, minLight );
1612 if( minVertex == qfalse )
1613 VectorScale( color, f, minVertexLight );
1614 if( minGrid == qfalse )
1615 VectorScale( color, f, minGridLight );
1618 /* create world lights */
1619 Sys_FPrintf( SYS_VRB, "--- CreateLights ---\n" );
1620 CreateEntityLights();
1621 CreateSurfaceLights();
1622 Sys_Printf( "%9d point lights\n", numPointLights );
1623 Sys_Printf( "%9d spotlights\n", numSpotLights );
1624 Sys_Printf( "%9d diffuse (area) lights\n", numDiffuseLights );
1625 Sys_Printf( "%9d sun/sky lights\n", numSunLights );
1627 /* calculate lightgrid */
1628 if( !noGridLighting )
1630 /* ydnar: set up light envelopes */
1631 SetupEnvelopes( qtrue, fastgrid );
1633 Sys_Printf( "--- TraceGrid ---\n" );
1634 RunThreadsOnIndividual( numRawGridPoints, qtrue, TraceGrid );
1635 Sys_Printf( "%d x %d x %d = %d grid\n",
1636 gridBounds[ 0 ], gridBounds[ 1 ], gridBounds[ 2 ], numBSPGridPoints );
1638 /* ydnar: emit statistics on light culling */
1639 Sys_FPrintf( SYS_VRB, "%9d grid points envelope culled\n", gridEnvelopeCulled );
1640 Sys_FPrintf( SYS_VRB, "%9d grid points bounds culled\n", gridBoundsCulled );
1643 /* slight optimization to remove a sqrt */
1644 subdivideThreshold *= subdivideThreshold;
1646 /* map the world luxels */
1647 Sys_Printf( "--- MapRawLightmap ---\n" );
1648 RunThreadsOnIndividual( numRawLightmaps, qtrue, MapRawLightmap );
1649 Sys_Printf( "%9d luxels\n", numLuxels );
1650 Sys_Printf( "%9d luxels mapped\n", numLuxelsMapped );
1651 Sys_Printf( "%9d luxels occluded\n", numLuxelsOccluded );
1656 Sys_Printf( "--- DirtyRawLightmap ---\n" );
1661 RunThreadsOnIndividual( numRawLightmaps, qtrue, DirtyRawLightmap );
1665 /* ydnar: set up light envelopes */
1666 SetupEnvelopes( qfalse, fast );
1668 /* light up my world */
1669 lightsPlaneCulled = 0;
1670 lightsEnvelopeCulled = 0;
1671 lightsBoundsCulled = 0;
1672 lightsClusterCulled = 0;
1674 Sys_Printf( "--- IlluminateRawLightmap ---\n" );
1675 RunThreadsOnIndividual( numRawLightmaps, qtrue, IlluminateRawLightmap );
1676 Sys_Printf( "%9d luxels illuminated\n", numLuxelsIlluminated );
1678 StitchSurfaceLightmaps();
1680 Sys_Printf( "--- IlluminateVertexes ---\n" );
1681 RunThreadsOnIndividual( numBSPDrawSurfaces, qtrue, IlluminateVertexes );
1682 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
1684 /* ydnar: emit statistics on light culling */
1685 Sys_FPrintf( SYS_VRB, "%9d lights plane culled\n", lightsPlaneCulled );
1686 Sys_FPrintf( SYS_VRB, "%9d lights envelope culled\n", lightsEnvelopeCulled );
1687 Sys_FPrintf( SYS_VRB, "%9d lights bounds culled\n", lightsBoundsCulled );
1688 Sys_FPrintf( SYS_VRB, "%9d lights cluster culled\n", lightsClusterCulled );
1695 /* store off the bsp between bounces */
1696 StoreSurfaceLightmaps();
1697 Sys_Printf( "Writing %s\n", source );
1698 WriteBSPFile( source );
1701 Sys_Printf( "\n--- Radiosity (bounce %d of %d) ---\n", b, bt );
1705 VectorClear( ambientColor );
1707 /* generate diffuse lights */
1709 RadCreateDiffuseLights();
1711 /* setup light envelopes */
1712 SetupEnvelopes( qfalse, fastbounce );
1713 if( numLights == 0 )
1715 Sys_Printf( "No diffuse light to calculate, ending radiosity.\n" );
1719 /* add to lightgrid */
1722 gridEnvelopeCulled = 0;
1723 gridBoundsCulled = 0;
1725 Sys_Printf( "--- BounceGrid ---\n" );
1726 RunThreadsOnIndividual( numRawGridPoints, qtrue, TraceGrid );
1727 Sys_FPrintf( SYS_VRB, "%9d grid points envelope culled\n", gridEnvelopeCulled );
1728 Sys_FPrintf( SYS_VRB, "%9d grid points bounds culled\n", gridBoundsCulled );
1731 /* light up my world */
1732 lightsPlaneCulled = 0;
1733 lightsEnvelopeCulled = 0;
1734 lightsBoundsCulled = 0;
1735 lightsClusterCulled = 0;
1737 Sys_Printf( "--- IlluminateRawLightmap ---\n" );
1738 RunThreadsOnIndividual( numRawLightmaps, qtrue, IlluminateRawLightmap );
1739 Sys_Printf( "%9d luxels illuminated\n", numLuxelsIlluminated );
1740 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
1742 StitchSurfaceLightmaps();
1744 Sys_Printf( "--- IlluminateVertexes ---\n" );
1745 RunThreadsOnIndividual( numBSPDrawSurfaces, qtrue, IlluminateVertexes );
1746 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
1748 /* ydnar: emit statistics on light culling */
1749 Sys_FPrintf( SYS_VRB, "%9d lights plane culled\n", lightsPlaneCulled );
1750 Sys_FPrintf( SYS_VRB, "%9d lights envelope culled\n", lightsEnvelopeCulled );
1751 Sys_FPrintf( SYS_VRB, "%9d lights bounds culled\n", lightsBoundsCulled );
1752 Sys_FPrintf( SYS_VRB, "%9d lights cluster culled\n", lightsClusterCulled );
1764 main routine for light processing
1767 int LightMain( int argc, char **argv )
1771 char mapSource[ 1024 ];
1776 Sys_Printf( "--- Light ---\n" );
1778 /* set standard game flags */
1779 wolfLight = game->wolfLight;
1780 lmCustomSize = game->lightmapSize;
1781 lightmapGamma = game->lightmapGamma;
1782 lightmapCompensate = game->lightmapCompensate;
1784 /* process commandline arguments */
1785 for( i = 1; i < (argc - 1); i++ )
1787 /* lightsource scaling */
1788 if( !strcmp( argv[ i ], "-point" ) || !strcmp( argv[ i ], "-pointscale" ) )
1790 f = atof( argv[ i + 1 ] );
1792 Sys_Printf( "Point (entity) light scaled by %f to %f\n", f, pointScale );
1796 else if( !strcmp( argv[ i ], "-area" ) || !strcmp( argv[ i ], "-areascale" ) )
1798 f = atof( argv[ i + 1 ] );
1800 Sys_Printf( "Area (shader) light scaled by %f to %f\n", f, areaScale );
1804 else if( !strcmp( argv[ i ], "-sky" ) || !strcmp( argv[ i ], "-skyscale" ) )
1806 f = atof( argv[ i + 1 ] );
1808 Sys_Printf( "Sky/sun light scaled by %f to %f\n", f, skyScale );
1812 else if( !strcmp( argv[ i ], "-bouncescale" ) )
1814 f = atof( argv[ i + 1 ] );
1816 Sys_Printf( "Bounce (radiosity) light scaled by %f to %f\n", f, bounceScale );
1820 else if( !strcmp( argv[ i ], "-scale" ) )
1822 f = atof( argv[ i + 1 ] );
1827 Sys_Printf( "All light scaled by %f\n", f );
1831 else if( !strcmp( argv[ i ], "-gamma" ) )
1833 f = atof( argv[ i + 1 ] );
1835 Sys_Printf( "Lighting gamma set to %f\n", lightmapGamma );
1839 else if( !strcmp( argv[ i ], "-compensate" ) )
1841 f = atof( argv[ i + 1 ] );
1844 lightmapCompensate = f;
1845 Sys_Printf( "Lighting compensation set to 1/%f\n", lightmapCompensate );
1849 /* ydnar switches */
1850 else if( !strcmp( argv[ i ], "-bounce" ) )
1852 bounce = atoi( argv[ i + 1 ] );
1855 else if( bounce > 0 )
1856 Sys_Printf( "Radiosity enabled with %d bounce(s)\n", bounce );
1860 else if( !strcmp( argv[ i ], "-supersample" ) || !strcmp( argv[ i ], "-super" ) )
1862 superSample = atoi( argv[ i + 1 ] );
1863 if( superSample < 1 )
1865 else if( superSample > 1 )
1866 Sys_Printf( "Ordered-grid supersampling enabled with %d sample(s) per lightmap texel\n", (superSample * superSample) );
1870 else if( !strcmp( argv[ i ], "-samples" ) )
1872 lightSamples = atoi( argv[ i + 1 ] );
1873 if( lightSamples < 1 )
1875 else if( lightSamples > 1 )
1876 Sys_Printf( "Adaptive supersampling enabled with %d sample(s) per lightmap texel\n", lightSamples );
1880 else if( !strcmp( argv[ i ], "-filter" ) )
1883 Sys_Printf( "Lightmap filtering enabled\n" );
1886 else if( !strcmp( argv[ i ], "-dark" ) )
1889 Sys_Printf( "Dark lightmap seams enabled\n" );
1898 else if( !strcmp( argv[ i ], "-shadeangle" ) )
1900 shadeAngleDegrees = atof( argv[ i + 1 ] );
1901 if( shadeAngleDegrees < 0.0f )
1902 shadeAngleDegrees = 0.0f;
1903 else if( shadeAngleDegrees > 0.0f )
1906 Sys_Printf( "Phong shading enabled with a breaking angle of %f degrees\n", shadeAngleDegrees );
1911 else if( !strcmp( argv[ i ], "-thresh" ) )
1913 subdivideThreshold = atof( argv[ i + 1 ] );
1914 if( subdivideThreshold < 0 )
1915 subdivideThreshold = DEFAULT_SUBDIVIDE_THRESHOLD;
1917 Sys_Printf( "Subdivision threshold set at %.3f\n", subdivideThreshold );
1921 else if( !strcmp( argv[ i ], "-approx" ) )
1923 approximateTolerance = atoi( argv[ i + 1 ] );
1924 if( approximateTolerance < 0 )
1925 approximateTolerance = 0;
1926 else if( approximateTolerance > 0 )
1927 Sys_Printf( "Approximating lightmaps within a byte tolerance of %d\n", approximateTolerance );
1931 else if( !strcmp( argv[ i ], "-deluxe" ) || !strcmp( argv[ i ], "-deluxemap" ) )
1934 Sys_Printf( "Generating deluxemaps for average light direction\n" );
1937 else if( !strcmp( argv[ i ], "-external" ) )
1939 externalLightmaps = qtrue;
1940 Sys_Printf( "Storing all lightmaps externally\n" );
1943 else if( !strcmp( argv[ i ], "-lightmapsize" ) )
1945 lmCustomSize = atoi( argv[ i + 1 ] );
1947 /* must be a power of 2 and greater than 2 */
1948 if( ((lmCustomSize - 1) & lmCustomSize) || lmCustomSize < 2 )
1950 Sys_Printf( "WARNING: Lightmap size must be a power of 2, greater or equal to 2 pixels.\n" );
1951 lmCustomSize = game->lightmapSize;
1954 Sys_Printf( "Default lightmap size set to %d x %d pixels\n", lmCustomSize, lmCustomSize );
1956 /* enable external lightmaps */
1957 if( lmCustomSize != game->lightmapSize )
1959 externalLightmaps = qtrue;
1960 Sys_Printf( "Storing all lightmaps externally\n" );
1964 /* ydnar: add this to suppress warnings */
1965 else if( !strcmp( argv[ i ], "-custinfoparms") )
1967 Sys_Printf( "Custom info parms enabled\n" );
1968 useCustomInfoParms = qtrue;
1971 else if( !strcmp( argv[ i ], "-wolf" ) )
1973 /* -game should already be set */
1975 Sys_Printf( "Enabling Wolf lighting model (linear default)\n" );
1978 else if( !strcmp( argv[ i ], "-q3" ) )
1980 /* -game should already be set */
1982 Sys_Printf( "Enabling Quake 3 lighting model (nonlinear default)\n" );
1985 else if( !strcmp( argv[ i ], "-sunonly" ) )
1988 Sys_Printf( "Only computing sunlight\n" );
1991 else if( !strcmp( argv[ i ], "-bounceonly" ) )
1994 Sys_Printf( "Storing bounced light (radiosity) only\n" );
1997 else if( !strcmp( argv[ i ], "-nocollapse" ) )
2000 Sys_Printf( "Identical lightmap collapsing disabled\n" );
2003 else if( !strcmp( argv[ i ], "-shade" ) )
2006 Sys_Printf( "Phong shading enabled\n" );
2009 else if( !strcmp( argv[ i ], "-bouncegrid") )
2013 Sys_Printf( "Grid lighting with radiosity enabled\n" );
2016 else if( !strcmp( argv[ i ], "-smooth" ) )
2018 lightSamples = EXTRA_SCALE;
2019 Sys_Printf( "The -smooth argument is deprecated, use \"-samples 2\" instead\n" );
2022 else if( !strcmp( argv[ i ], "-fast" ) )
2027 Sys_Printf( "Fast mode enabled\n" );
2030 else if( !strcmp( argv[ i ], "-faster" ) )
2036 Sys_Printf( "Faster mode enabled\n" );
2039 else if( !strcmp( argv[ i ], "-fastgrid" ) )
2042 Sys_Printf( "Fast grid lighting enabled\n" );
2045 else if( !strcmp( argv[ i ], "-fastbounce" ) )
2048 Sys_Printf( "Fast bounce mode enabled\n" );
2051 else if( !strcmp( argv[ i ], "-cheap" ) )
2055 Sys_Printf( "Cheap mode enabled\n" );
2058 else if( !strcmp( argv[ i ], "-cheapgrid" ) )
2061 Sys_Printf( "Cheap grid mode enabled\n" );
2064 else if( !strcmp( argv[ i ], "-normalmap" ) )
2067 Sys_Printf( "Storing normal map instead of lightmap\n" );
2070 else if( !strcmp( argv[ i ], "-trisoup" ) )
2073 Sys_Printf( "Converting brush faces to triangle soup\n" );
2076 else if( !strcmp( argv[ i ], "-debug" ) )
2079 Sys_Printf( "Lightmap debugging enabled\n" );
2082 else if( !strcmp( argv[ i ], "-debugsurfaces" ) || !strcmp( argv[ i ], "-debugsurface" ) )
2084 debugSurfaces = qtrue;
2085 Sys_Printf( "Lightmap surface debugging enabled\n" );
2088 else if( !strcmp( argv[ i ], "-debugunused" ) )
2090 debugUnused = qtrue;
2091 Sys_Printf( "Unused luxel debugging enabled\n" );
2094 else if( !strcmp( argv[ i ], "-debugaxis" ) )
2097 Sys_Printf( "Lightmap axis debugging enabled\n" );
2100 else if( !strcmp( argv[ i ], "-debugcluster" ) )
2102 debugCluster = qtrue;
2103 Sys_Printf( "Luxel cluster debugging enabled\n" );
2106 else if( !strcmp( argv[ i ], "-debugorigin" ) )
2108 debugOrigin = qtrue;
2109 Sys_Printf( "Luxel origin debugging enabled\n" );
2112 else if( !strcmp( argv[ i ], "-debugdeluxe" ) )
2115 debugDeluxemap = qtrue;
2116 Sys_Printf( "Deluxemap debugging enabled\n" );
2119 else if( !strcmp( argv[ i ], "-export" ) )
2121 exportLightmaps = qtrue;
2122 Sys_Printf( "Exporting lightmaps\n" );
2125 else if( !strcmp(argv[ i ], "-notrace" ))
2128 Sys_Printf( "Shadow occlusion disabled\n" );
2130 else if( !strcmp(argv[ i ], "-patchshadows" ) )
2132 patchShadows = qtrue;
2133 Sys_Printf( "Patch shadow casting enabled\n" );
2135 else if( !strcmp( argv[ i ], "-extra" ) )
2137 superSample = EXTRA_SCALE; /* ydnar */
2138 Sys_Printf( "The -extra argument is deprecated, use \"-super 2\" instead\n" );
2140 else if( !strcmp( argv[ i ], "-extrawide" ) )
2142 superSample = EXTRAWIDE_SCALE; /* ydnar */
2143 filter = qtrue; /* ydnar */
2144 Sys_Printf( "The -extrawide argument is deprecated, use \"-filter [-super 2]\" instead\n");
2146 else if( !strcmp( argv[ i ], "-samplesize" ) )
2148 sampleSize = atoi( argv[ i + 1 ] );
2149 if( sampleSize < 1 )
2152 Sys_Printf( "Default lightmap sample size set to %dx%d units\n", sampleSize, sampleSize );
2154 else if( !strcmp( argv[ i ], "-novertex" ) )
2156 noVertexLighting = qtrue;
2157 Sys_Printf( "Disabling vertex lighting\n" );
2159 else if( !strcmp( argv[ i ], "-nogrid" ) )
2161 noGridLighting = qtrue;
2162 Sys_Printf( "Disabling grid lighting\n" );
2164 else if( !strcmp( argv[ i ], "-border" ) )
2166 lightmapBorder = qtrue;
2167 Sys_Printf( "Adding debug border to lightmaps\n" );
2169 else if( !strcmp( argv[ i ], "-nosurf" ) )
2172 Sys_Printf( "Not tracing against surfaces\n" );
2174 else if( !strcmp( argv[ i ], "-dump" ) )
2177 Sys_Printf( "Dumping radiosity lights into numbered prefabs\n" );
2179 else if( !strcmp( argv[ i ], "-lomem" ) )
2182 Sys_Printf( "Enabling low-memory (potentially slower) lighting mode\n" );
2184 else if( !strcmp( argv[ i ], "-nostyle" ) || !strcmp( argv[ i ], "-nostyles" ) )
2187 Sys_Printf( "Disabling lightstyles\n" );
2189 else if( !strcmp( argv[ i ], "-cpma" ) )
2192 Sys_Printf( "Enabling Challenge Pro Mode Asstacular Vertex Lighting Mode (tm)\n" );
2195 /* r7: dirtmapping */
2196 else if( !strcmp( argv[ i ], "-dirty" ) )
2199 Sys_Printf( "Dirtmapping enabled\n" );
2201 else if( !strcmp( argv[ i ], "-dirtdebug" ) || !strcmp( argv[ i ], "-debugdirt" ) )
2204 Sys_Printf( "Dirtmap debugging enabled\n" );
2206 else if( !strcmp( argv[ i ], "-dirtmode" ) )
2208 dirtMode = atoi( argv[ i + 1 ] );
2209 if( dirtMode != 0 && dirtMode != 1 )
2212 Sys_Printf( "Enabling randomized dirtmapping\n" );
2214 Sys_Printf( "Enabling ordered dir mapping\n" );
2216 else if( !strcmp( argv[ i ], "-dirtdepth" ) )
2218 dirtDepth = atof( argv[ i + 1 ] );
2219 if( dirtDepth <= 0.0f )
2221 Sys_Printf( "Dirtmapping depth set to %.1f\n", dirtDepth );
2223 else if( !strcmp( argv[ i ], "-dirtscale" ) )
2225 dirtScale = atof( argv[ i + 1 ] );
2226 if( dirtScale <= 0.0f )
2228 Sys_Printf( "Dirtmapping scale set to %.1f\n", dirtScale );
2230 else if( !strcmp( argv[ i ], "-dirtgain" ) )
2232 dirtGain = atof( argv[ i + 1 ] );
2233 if( dirtGain <= 0.0f )
2235 Sys_Printf( "Dirtmapping gain set to %.1f\n", dirtGain );
2238 /* unhandled args */
2240 Sys_Printf( "WARNING: Unknown argument \"%s\"\n", argv[ i ] );
2244 /* clean up map name */
2245 strcpy( source, ExpandArg( argv[ i ] ) );
2246 StripExtension( source );
2247 DefaultExtension( source, ".bsp" );
2248 strcpy( mapSource, ExpandArg( argv[ i ] ) );
2249 StripExtension( mapSource );
2250 DefaultExtension( mapSource, ".map" );
2252 /* ydnar: set default sample size */
2253 SetDefaultSampleSize( sampleSize );
2255 /* ydnar: handle shaders */
2256 BeginMapShaderFile( source );
2260 Sys_Printf( "Loading %s\n", source );
2262 /* ydnar: load surface file */
2263 LoadSurfaceExtraFile( source );
2266 LoadBSPFile( source );
2268 /* parse bsp entities */
2272 value = ValueForKey( &entities[ 0 ], "_keepLights" );
2273 if( value[ 0 ] != '1' )
2274 LoadMapFile( mapSource, qtrue );
2276 /* set the entity/model origins and init yDrawVerts */
2279 /* ydnar: set up optimization */
2282 SetupSurfaceLightmaps();
2284 /* initialize the surface facet tracing */
2287 /* light the world */
2290 /* ydnar: store off lightmaps */
2291 StoreSurfaceLightmaps();
2293 /* write out the bsp */
2295 Sys_Printf( "Writing %s\n", source );
2296 WriteBSPFile( source );
2298 /* ydnar: export lightmaps */
2299 if( exportLightmaps && !externalLightmaps )
2302 /* return to sender */