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 );
342 if( light->style != LS_NORMAL ) {
343 Sys_FPrintf (SYS_WRN, "WARNING: Styled light found targeting %s\n **", target );
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 /////// Floodlighting for point //////////////////
1382 //do our floodlight ambient occlusion loop, and add a single contribution based on the brightest dir
1388 col[0]=col[1]=col[2]=floodlightIntensity;
1392 trace.testOcclusion = qtrue;
1393 trace.forceSunlight = qfalse;
1394 trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
1395 trace.testAll = qtrue;
1399 if (q==0) //upper hemisphere
1405 else //lower hemisphere
1412 f = FloodLightForSample(&trace);
1414 contributions[ numCon ].color[0]=col[0]*f;
1415 contributions[ numCon ].color[1]=col[1]*f;
1416 contributions[ numCon ].color[2]=col[2]*f;
1418 contributions[ numCon ].dir[0]=dir[0];
1419 contributions[ numCon ].dir[1]=dir[1];
1420 contributions[ numCon ].dir[2]=dir[2];
1422 contributions[ numCon ].style = 0;
1424 /* push average direction around */
1425 addSize = VectorLength( col );
1426 VectorMA( gp->dir, addSize, dir, gp->dir );
1429 /////////////////////
1431 /* normalize to get primary light direction */
1432 VectorNormalize( gp->dir, gp->dir );
1434 /* now that we have identified the primary light direction,
1435 go back and separate all the light into directed and ambient */
1437 for( i = 0; i < numCon; i++ )
1439 /* get relative directed strength */
1440 d = DotProduct( contributions[ i ].dir, gp->dir );
1444 /* find appropriate style */
1445 for( j = 0; j < numStyles; j++ )
1447 if( gp->styles[ j ] == contributions[ i ].style )
1451 /* style not found? */
1452 if( j >= numStyles )
1454 /* add a new style */
1455 if( numStyles < MAX_LIGHTMAPS )
1457 gp->styles[ numStyles ] = contributions[ i ].style;
1458 bgp->styles[ numStyles ] = contributions[ i ].style;
1460 //% Sys_Printf( "(%d, %d) ", num, contributions[ i ].style );
1468 /* add the directed color */
1469 VectorMA( gp->directed[ j ], d, contributions[ i ].color, gp->directed[ j ] );
1471 /* ambient light will be at 1/4 the value of directed light */
1472 /* (ydnar: nuke this in favor of more dramatic lighting?) */
1473 d = 0.25f * (1.0f - d);
1474 VectorMA( gp->ambient[ j ], d, contributions[ i ].color, gp->ambient[ j ] );
1478 /* store off sample */
1479 for( i = 0; i < MAX_LIGHTMAPS; i++ )
1481 /* do some fudging to keep the ambient from being too low (2003-07-05: 0.25 -> 0.125) */
1483 VectorMA( gp->ambient[ i ], 0.125f, gp->directed[ i ], gp->ambient[ i ] );
1485 /* set minimum light and copy off to bytes */
1486 VectorCopy( gp->ambient[ i ], color );
1487 for( j = 0; j < 3; j++ )
1488 if( color[ j ] < minGridLight[ j ] )
1489 color[ j ] = minGridLight[ j ];
1490 ColorToBytes( color, bgp->ambient[ i ], 1.0f );
1491 ColorToBytes( gp->directed[ i ], bgp->directed[ i ], 1.0f );
1496 //% Sys_FPrintf( SYS_VRB, "%10d %10d %10d ", &gp->ambient[ 0 ][ 0 ], &gp->ambient[ 0 ][ 1 ], &gp->ambient[ 0 ][ 2 ] );
1497 Sys_FPrintf( SYS_VRB, "%9d Amb: (%03.1f %03.1f %03.1f) Dir: (%03.1f %03.1f %03.1f)\n",
1499 gp->ambient[ 0 ][ 0 ], gp->ambient[ 0 ][ 1 ], gp->ambient[ 0 ][ 2 ],
1500 gp->directed[ 0 ][ 0 ], gp->directed[ 0 ][ 1 ], gp->directed[ 0 ][ 2 ] );
1503 /* store direction */
1505 NormalToLatLong( gp->dir, bgp->latLong );
1512 calculates the size of the lightgrid and allocates memory
1515 void SetupGrid( void )
1518 vec3_t maxs, oldGridSize;
1523 /* don't do this if not grid lighting */
1524 if( noGridLighting )
1527 /* ydnar: set grid size */
1528 value = ValueForKey( &entities[ 0 ], "gridsize" );
1529 if( value[ 0 ] != '\0' )
1530 sscanf( value, "%f %f %f", &gridSize[ 0 ], &gridSize[ 1 ], &gridSize[ 2 ] );
1533 VectorCopy( gridSize, oldGridSize );
1534 for( i = 0; i < 3; i++ )
1535 gridSize[ i ] = gridSize[ i ] >= 8.0f ? floor( gridSize[ i ] ) : 8.0f;
1537 /* ydnar: increase gridSize until grid count is smaller than max allowed */
1538 numRawGridPoints = MAX_MAP_LIGHTGRID + 1;
1540 while( numRawGridPoints > MAX_MAP_LIGHTGRID )
1542 /* get world bounds */
1543 for( i = 0; i < 3; i++ )
1545 gridMins[ i ] = gridSize[ i ] * ceil( bspModels[ 0 ].mins[ i ] / gridSize[ i ] );
1546 maxs[ i ] = gridSize[ i ] * floor( bspModels[ 0 ].maxs[ i ] / gridSize[ i ] );
1547 gridBounds[ i ] = (maxs[ i ] - gridMins[ i ]) / gridSize[ i ] + 1;
1551 numRawGridPoints = gridBounds[ 0 ] * gridBounds[ 1 ] * gridBounds[ 2 ];
1553 /* increase grid size a bit */
1554 if( numRawGridPoints > MAX_MAP_LIGHTGRID )
1555 gridSize[ j++ % 3 ] += 16.0f;
1559 Sys_Printf( "Grid size = { %1.0f, %1.0f, %1.0f }\n", gridSize[ 0 ], gridSize[ 1 ], gridSize[ 2 ] );
1562 if( !VectorCompare( gridSize, oldGridSize ) )
1564 sprintf( temp, "%.0f %.0f %.0f", gridSize[ 0 ], gridSize[ 1 ], gridSize[ 2 ] );
1565 SetKeyValue( &entities[ 0 ], "gridsize", (const char*) temp );
1566 Sys_FPrintf( SYS_VRB, "Storing adjusted grid size\n" );
1569 /* 2nd variable. fixme: is this silly? */
1570 numBSPGridPoints = numRawGridPoints;
1572 /* allocate lightgrid */
1573 rawGridPoints = safe_malloc( numRawGridPoints * sizeof( *rawGridPoints ) );
1574 memset( rawGridPoints, 0, numRawGridPoints * sizeof( *rawGridPoints ) );
1576 if( bspGridPoints != NULL )
1577 free( bspGridPoints );
1578 bspGridPoints = safe_malloc( numBSPGridPoints * sizeof( *bspGridPoints ) );
1579 memset( bspGridPoints, 0, numBSPGridPoints * sizeof( *bspGridPoints ) );
1581 /* clear lightgrid */
1582 for( i = 0; i < numRawGridPoints; i++ )
1584 VectorCopy( ambientColor, rawGridPoints[ i ].ambient[ j ] );
1585 rawGridPoints[ i ].styles[ 0 ] = LS_NORMAL;
1586 bspGridPoints[ i ].styles[ 0 ] = LS_NORMAL;
1587 for( j = 1; j < MAX_LIGHTMAPS; j++ )
1589 rawGridPoints[ i ].styles[ j ] = LS_NONE;
1590 bspGridPoints[ i ].styles[ j ] = LS_NONE;
1595 Sys_Printf( "%9d grid points\n", numRawGridPoints );
1602 does what it says...
1605 void LightWorld( void )
1610 qboolean minVertex, minGrid;
1614 /* ydnar: smooth normals */
1617 Sys_Printf( "--- SmoothNormals ---\n" );
1621 /* determine the number of grid points */
1622 Sys_Printf( "--- SetupGrid ---\n" );
1625 /* find the optional minimum lighting values */
1626 GetVectorForKey( &entities[ 0 ], "_color", color );
1627 if( VectorLength( color ) == 0.0f )
1628 VectorSet( color, 1.0, 1.0, 1.0 );
1631 f = FloatForKey( &entities[ 0 ], "_ambient" );
1633 f = FloatForKey( &entities[ 0 ], "ambient" );
1634 VectorScale( color, f, ambientColor );
1636 /* minvertexlight */
1638 value = ValueForKey( &entities[ 0 ], "_minvertexlight" );
1639 if( value[ 0 ] != '\0' )
1643 VectorScale( color, f, minVertexLight );
1648 value = ValueForKey( &entities[ 0 ], "_mingridlight" );
1649 if( value[ 0 ] != '\0' )
1653 VectorScale( color, f, minGridLight );
1657 value = ValueForKey( &entities[ 0 ], "_minlight" );
1658 if( value[ 0 ] != '\0' )
1661 VectorScale( color, f, minLight );
1662 if( minVertex == qfalse )
1663 VectorScale( color, f, minVertexLight );
1664 if( minGrid == qfalse )
1665 VectorScale( color, f, minGridLight );
1668 /* create world lights */
1669 Sys_FPrintf( SYS_VRB, "--- CreateLights ---\n" );
1670 CreateEntityLights();
1671 CreateSurfaceLights();
1672 Sys_Printf( "%9d point lights\n", numPointLights );
1673 Sys_Printf( "%9d spotlights\n", numSpotLights );
1674 Sys_Printf( "%9d diffuse (area) lights\n", numDiffuseLights );
1675 Sys_Printf( "%9d sun/sky lights\n", numSunLights );
1677 /* calculate lightgrid */
1678 if( !noGridLighting )
1680 /* ydnar: set up light envelopes */
1681 SetupEnvelopes( qtrue, fastgrid );
1683 Sys_Printf( "--- TraceGrid ---\n" );
1684 RunThreadsOnIndividual( numRawGridPoints, qtrue, TraceGrid );
1685 Sys_Printf( "%d x %d x %d = %d grid\n",
1686 gridBounds[ 0 ], gridBounds[ 1 ], gridBounds[ 2 ], numBSPGridPoints );
1688 /* ydnar: emit statistics on light culling */
1689 Sys_FPrintf( SYS_VRB, "%9d grid points envelope culled\n", gridEnvelopeCulled );
1690 Sys_FPrintf( SYS_VRB, "%9d grid points bounds culled\n", gridBoundsCulled );
1693 /* slight optimization to remove a sqrt */
1694 subdivideThreshold *= subdivideThreshold;
1696 /* map the world luxels */
1697 Sys_Printf( "--- MapRawLightmap ---\n" );
1698 RunThreadsOnIndividual( numRawLightmaps, qtrue, MapRawLightmap );
1699 Sys_Printf( "%9d luxels\n", numLuxels );
1700 Sys_Printf( "%9d luxels mapped\n", numLuxelsMapped );
1701 Sys_Printf( "%9d luxels occluded\n", numLuxelsOccluded );
1706 Sys_Printf( "--- DirtyRawLightmap ---\n" );
1711 RunThreadsOnIndividual( numRawLightmaps, qtrue, DirtyRawLightmap );
1714 /* floodlight them up */
1717 Sys_Printf( "--- FloodlightRawLightmap ---\n" );
1718 RunThreadsOnIndividual( numRawLightmaps, qtrue, FloodLightRawLightmap );
1721 /* ydnar: set up light envelopes */
1722 SetupEnvelopes( qfalse, fast );
1724 /* light up my world */
1725 lightsPlaneCulled = 0;
1726 lightsEnvelopeCulled = 0;
1727 lightsBoundsCulled = 0;
1728 lightsClusterCulled = 0;
1730 Sys_Printf( "--- IlluminateRawLightmap ---\n" );
1731 RunThreadsOnIndividual( numRawLightmaps, qtrue, IlluminateRawLightmap );
1732 Sys_Printf( "%9d luxels illuminated\n", numLuxelsIlluminated );
1734 StitchSurfaceLightmaps();
1736 Sys_Printf( "--- IlluminateVertexes ---\n" );
1737 RunThreadsOnIndividual( numBSPDrawSurfaces, qtrue, IlluminateVertexes );
1738 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
1740 /* ydnar: emit statistics on light culling */
1741 Sys_FPrintf( SYS_VRB, "%9d lights plane culled\n", lightsPlaneCulled );
1742 Sys_FPrintf( SYS_VRB, "%9d lights envelope culled\n", lightsEnvelopeCulled );
1743 Sys_FPrintf( SYS_VRB, "%9d lights bounds culled\n", lightsBoundsCulled );
1744 Sys_FPrintf( SYS_VRB, "%9d lights cluster culled\n", lightsClusterCulled );
1751 /* store off the bsp between bounces */
1752 StoreSurfaceLightmaps();
1753 Sys_Printf( "Writing %s\n", source );
1754 WriteBSPFile( source );
1757 Sys_Printf( "\n--- Radiosity (bounce %d of %d) ---\n", b, bt );
1761 VectorClear( ambientColor );
1762 floodlighty = qfalse;
1764 /* generate diffuse lights */
1766 RadCreateDiffuseLights();
1768 /* setup light envelopes */
1769 SetupEnvelopes( qfalse, fastbounce );
1770 if( numLights == 0 )
1772 Sys_Printf( "No diffuse light to calculate, ending radiosity.\n" );
1776 /* add to lightgrid */
1779 gridEnvelopeCulled = 0;
1780 gridBoundsCulled = 0;
1782 Sys_Printf( "--- BounceGrid ---\n" );
1783 RunThreadsOnIndividual( numRawGridPoints, qtrue, TraceGrid );
1784 Sys_FPrintf( SYS_VRB, "%9d grid points envelope culled\n", gridEnvelopeCulled );
1785 Sys_FPrintf( SYS_VRB, "%9d grid points bounds culled\n", gridBoundsCulled );
1788 /* light up my world */
1789 lightsPlaneCulled = 0;
1790 lightsEnvelopeCulled = 0;
1791 lightsBoundsCulled = 0;
1792 lightsClusterCulled = 0;
1794 Sys_Printf( "--- IlluminateRawLightmap ---\n" );
1795 RunThreadsOnIndividual( numRawLightmaps, qtrue, IlluminateRawLightmap );
1796 Sys_Printf( "%9d luxels illuminated\n", numLuxelsIlluminated );
1797 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
1799 StitchSurfaceLightmaps();
1801 Sys_Printf( "--- IlluminateVertexes ---\n" );
1802 RunThreadsOnIndividual( numBSPDrawSurfaces, qtrue, IlluminateVertexes );
1803 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
1805 /* ydnar: emit statistics on light culling */
1806 Sys_FPrintf( SYS_VRB, "%9d lights plane culled\n", lightsPlaneCulled );
1807 Sys_FPrintf( SYS_VRB, "%9d lights envelope culled\n", lightsEnvelopeCulled );
1808 Sys_FPrintf( SYS_VRB, "%9d lights bounds culled\n", lightsBoundsCulled );
1809 Sys_FPrintf( SYS_VRB, "%9d lights cluster culled\n", lightsClusterCulled );
1821 main routine for light processing
1824 int LightMain( int argc, char **argv )
1828 char mapSource[ 1024 ];
1833 Sys_Printf( "--- Light ---\n" );
1835 /* set standard game flags */
1836 wolfLight = game->wolfLight;
1837 lmCustomSize = game->lightmapSize;
1838 lightmapGamma = game->lightmapGamma;
1839 lightmapCompensate = game->lightmapCompensate;
1841 /* process commandline arguments */
1842 for( i = 1; i < (argc - 1); i++ )
1844 /* lightsource scaling */
1845 if( !strcmp( argv[ i ], "-point" ) || !strcmp( argv[ i ], "-pointscale" ) )
1847 f = atof( argv[ i + 1 ] );
1849 Sys_Printf( "Point (entity) light scaled by %f to %f\n", f, pointScale );
1853 else if( !strcmp( argv[ i ], "-area" ) || !strcmp( argv[ i ], "-areascale" ) )
1855 f = atof( argv[ i + 1 ] );
1857 Sys_Printf( "Area (shader) light scaled by %f to %f\n", f, areaScale );
1861 else if( !strcmp( argv[ i ], "-sky" ) || !strcmp( argv[ i ], "-skyscale" ) )
1863 f = atof( argv[ i + 1 ] );
1865 Sys_Printf( "Sky/sun light scaled by %f to %f\n", f, skyScale );
1869 else if( !strcmp( argv[ i ], "-bouncescale" ) )
1871 f = atof( argv[ i + 1 ] );
1873 Sys_Printf( "Bounce (radiosity) light scaled by %f to %f\n", f, bounceScale );
1877 else if( !strcmp( argv[ i ], "-scale" ) )
1879 f = atof( argv[ i + 1 ] );
1884 Sys_Printf( "All light scaled by %f\n", f );
1888 else if( !strcmp( argv[ i ], "-gamma" ) )
1890 f = atof( argv[ i + 1 ] );
1892 Sys_Printf( "Lighting gamma set to %f\n", lightmapGamma );
1896 else if( !strcmp( argv[ i ], "-exposure" ) )
1898 f = atof( argv[ i + 1 ] );
1899 lightmapExposure = f;
1900 Sys_Printf( "Lighting exposure set to %f\n", lightmapExposure );
1904 else if( !strcmp( argv[ i ], "-compensate" ) )
1906 f = atof( argv[ i + 1 ] );
1909 lightmapCompensate = f;
1910 Sys_Printf( "Lighting compensation set to 1/%f\n", lightmapCompensate );
1914 /* ydnar switches */
1915 else if( !strcmp( argv[ i ], "-bounce" ) )
1917 bounce = atoi( argv[ i + 1 ] );
1920 else if( bounce > 0 )
1921 Sys_Printf( "Radiosity enabled with %d bounce(s)\n", bounce );
1925 else if( !strcmp( argv[ i ], "-supersample" ) || !strcmp( argv[ i ], "-super" ) )
1927 superSample = atoi( argv[ i + 1 ] );
1928 if( superSample < 1 )
1930 else if( superSample > 1 )
1931 Sys_Printf( "Ordered-grid supersampling enabled with %d sample(s) per lightmap texel\n", (superSample * superSample) );
1935 else if( !strcmp( argv[ i ], "-samples" ) )
1937 lightSamples = atoi( argv[ i + 1 ] );
1938 if( lightSamples < 1 )
1940 else if( lightSamples > 1 )
1941 Sys_Printf( "Adaptive supersampling enabled with %d sample(s) per lightmap texel\n", lightSamples );
1945 else if( !strcmp( argv[ i ], "-filter" ) )
1948 Sys_Printf( "Lightmap filtering enabled\n" );
1951 else if( !strcmp( argv[ i ], "-dark" ) )
1954 Sys_Printf( "Dark lightmap seams enabled\n" );
1963 else if( !strcmp( argv[ i ], "-shadeangle" ) )
1965 shadeAngleDegrees = atof( argv[ i + 1 ] );
1966 if( shadeAngleDegrees < 0.0f )
1967 shadeAngleDegrees = 0.0f;
1968 else if( shadeAngleDegrees > 0.0f )
1971 Sys_Printf( "Phong shading enabled with a breaking angle of %f degrees\n", shadeAngleDegrees );
1976 else if( !strcmp( argv[ i ], "-thresh" ) )
1978 subdivideThreshold = atof( argv[ i + 1 ] );
1979 if( subdivideThreshold < 0 )
1980 subdivideThreshold = DEFAULT_SUBDIVIDE_THRESHOLD;
1982 Sys_Printf( "Subdivision threshold set at %.3f\n", subdivideThreshold );
1986 else if( !strcmp( argv[ i ], "-approx" ) )
1988 approximateTolerance = atoi( argv[ i + 1 ] );
1989 if( approximateTolerance < 0 )
1990 approximateTolerance = 0;
1991 else if( approximateTolerance > 0 )
1992 Sys_Printf( "Approximating lightmaps within a byte tolerance of %d\n", approximateTolerance );
1996 else if( !strcmp( argv[ i ], "-deluxe" ) || !strcmp( argv[ i ], "-deluxemap" ) )
1999 Sys_Printf( "Generating deluxemaps for average light direction\n" );
2002 else if( !strcmp( argv[ i ], "-external" ) )
2004 externalLightmaps = qtrue;
2005 Sys_Printf( "Storing all lightmaps externally\n" );
2008 else if( !strcmp( argv[ i ], "-lightmapsize" ) )
2010 lmCustomSize = atoi( argv[ i + 1 ] );
2012 /* must be a power of 2 and greater than 2 */
2013 if( ((lmCustomSize - 1) & lmCustomSize) || lmCustomSize < 2 )
2015 Sys_Printf( "WARNING: Lightmap size must be a power of 2, greater or equal to 2 pixels.\n" );
2016 lmCustomSize = game->lightmapSize;
2019 Sys_Printf( "Default lightmap size set to %d x %d pixels\n", lmCustomSize, lmCustomSize );
2021 /* enable external lightmaps */
2022 if( lmCustomSize != game->lightmapSize )
2024 externalLightmaps = qtrue;
2025 Sys_Printf( "Storing all lightmaps externally\n" );
2029 /* ydnar: add this to suppress warnings */
2030 else if( !strcmp( argv[ i ], "-custinfoparms") )
2032 Sys_Printf( "Custom info parms enabled\n" );
2033 useCustomInfoParms = qtrue;
2036 else if( !strcmp( argv[ i ], "-wolf" ) )
2038 /* -game should already be set */
2040 Sys_Printf( "Enabling Wolf lighting model (linear default)\n" );
2043 else if( !strcmp( argv[ i ], "-q3" ) )
2045 /* -game should already be set */
2047 Sys_Printf( "Enabling Quake 3 lighting model (nonlinear default)\n" );
2050 else if( !strcmp( argv[ i ], "-sunonly" ) )
2053 Sys_Printf( "Only computing sunlight\n" );
2056 else if( !strcmp( argv[ i ], "-bounceonly" ) )
2059 Sys_Printf( "Storing bounced light (radiosity) only\n" );
2062 else if( !strcmp( argv[ i ], "-nocollapse" ) )
2065 Sys_Printf( "Identical lightmap collapsing disabled\n" );
2068 else if( !strcmp( argv[ i ], "-shade" ) )
2071 Sys_Printf( "Phong shading enabled\n" );
2074 else if( !strcmp( argv[ i ], "-bouncegrid") )
2078 Sys_Printf( "Grid lighting with radiosity enabled\n" );
2081 else if( !strcmp( argv[ i ], "-smooth" ) )
2083 lightSamples = EXTRA_SCALE;
2084 Sys_Printf( "The -smooth argument is deprecated, use \"-samples 2\" instead\n" );
2087 else if( !strcmp( argv[ i ], "-fast" ) )
2092 Sys_Printf( "Fast mode enabled\n" );
2095 else if( !strcmp( argv[ i ], "-faster" ) )
2101 Sys_Printf( "Faster mode enabled\n" );
2104 else if( !strcmp( argv[ i ], "-fastgrid" ) )
2107 Sys_Printf( "Fast grid lighting enabled\n" );
2110 else if( !strcmp( argv[ i ], "-fastbounce" ) )
2113 Sys_Printf( "Fast bounce mode enabled\n" );
2116 else if( !strcmp( argv[ i ], "-cheap" ) )
2120 Sys_Printf( "Cheap mode enabled\n" );
2123 else if( !strcmp( argv[ i ], "-cheapgrid" ) )
2126 Sys_Printf( "Cheap grid mode enabled\n" );
2129 else if( !strcmp( argv[ i ], "-normalmap" ) )
2132 Sys_Printf( "Storing normal map instead of lightmap\n" );
2135 else if( !strcmp( argv[ i ], "-trisoup" ) )
2138 Sys_Printf( "Converting brush faces to triangle soup\n" );
2141 else if( !strcmp( argv[ i ], "-debug" ) )
2144 Sys_Printf( "Lightmap debugging enabled\n" );
2147 else if( !strcmp( argv[ i ], "-debugsurfaces" ) || !strcmp( argv[ i ], "-debugsurface" ) )
2149 debugSurfaces = qtrue;
2150 Sys_Printf( "Lightmap surface debugging enabled\n" );
2153 else if( !strcmp( argv[ i ], "-debugunused" ) )
2155 debugUnused = qtrue;
2156 Sys_Printf( "Unused luxel debugging enabled\n" );
2159 else if( !strcmp( argv[ i ], "-debugaxis" ) )
2162 Sys_Printf( "Lightmap axis debugging enabled\n" );
2165 else if( !strcmp( argv[ i ], "-debugcluster" ) )
2167 debugCluster = qtrue;
2168 Sys_Printf( "Luxel cluster debugging enabled\n" );
2171 else if( !strcmp( argv[ i ], "-debugorigin" ) )
2173 debugOrigin = qtrue;
2174 Sys_Printf( "Luxel origin debugging enabled\n" );
2177 else if( !strcmp( argv[ i ], "-debugdeluxe" ) )
2180 debugDeluxemap = qtrue;
2181 Sys_Printf( "Deluxemap debugging enabled\n" );
2184 else if( !strcmp( argv[ i ], "-export" ) )
2186 exportLightmaps = qtrue;
2187 Sys_Printf( "Exporting lightmaps\n" );
2190 else if( !strcmp(argv[ i ], "-notrace" ))
2193 Sys_Printf( "Shadow occlusion disabled\n" );
2195 else if( !strcmp(argv[ i ], "-patchshadows" ) )
2197 patchShadows = qtrue;
2198 Sys_Printf( "Patch shadow casting enabled\n" );
2200 else if( !strcmp( argv[ i ], "-extra" ) )
2202 superSample = EXTRA_SCALE; /* ydnar */
2203 Sys_Printf( "The -extra argument is deprecated, use \"-super 2\" instead\n" );
2205 else if( !strcmp( argv[ i ], "-extrawide" ) )
2207 superSample = EXTRAWIDE_SCALE; /* ydnar */
2208 filter = qtrue; /* ydnar */
2209 Sys_Printf( "The -extrawide argument is deprecated, use \"-filter [-super 2]\" instead\n");
2211 else if( !strcmp( argv[ i ], "-samplesize" ) )
2213 sampleSize = atoi( argv[ i + 1 ] );
2214 if( sampleSize < 1 )
2217 Sys_Printf( "Default lightmap sample size set to %dx%d units\n", sampleSize, sampleSize );
2219 else if( !strcmp( argv[ i ], "-minsamplesize" ) )
2221 minSampleSize = atoi( argv[ i + 1 ] );
2222 if( minSampleSize < 1 )
2225 Sys_Printf( "Minimum lightmap sample size set to %dx%d units\n", minSampleSize, minSampleSize );
2227 else if( !strcmp( argv[ i ], "-novertex" ) )
2229 noVertexLighting = qtrue;
2230 Sys_Printf( "Disabling vertex lighting\n" );
2232 else if( !strcmp( argv[ i ], "-nogrid" ) )
2234 noGridLighting = qtrue;
2235 Sys_Printf( "Disabling grid lighting\n" );
2237 else if( !strcmp( argv[ i ], "-border" ) )
2239 lightmapBorder = qtrue;
2240 Sys_Printf( "Adding debug border to lightmaps\n" );
2242 else if( !strcmp( argv[ i ], "-nosurf" ) )
2245 Sys_Printf( "Not tracing against surfaces\n" );
2247 else if( !strcmp( argv[ i ], "-dump" ) )
2250 Sys_Printf( "Dumping radiosity lights into numbered prefabs\n" );
2252 else if( !strcmp( argv[ i ], "-lomem" ) )
2255 Sys_Printf( "Enabling low-memory (potentially slower) lighting mode\n" );
2257 else if( !strcmp( argv[ i ], "-nostyle" ) || !strcmp( argv[ i ], "-nostyles" ) )
2260 Sys_Printf( "Disabling lightstyles\n" );
2262 else if( !strcmp( argv[ i ], "-cpma" ) )
2265 Sys_Printf( "Enabling Challenge Pro Mode Asstacular Vertex Lighting Mode (tm)\n" );
2267 else if( !strcmp( argv[ i ], "-floodlight" ) )
2269 floodlighty = qtrue;
2270 Sys_Printf( "FloodLighting enabled\n" );
2272 else if( !strcmp( argv[ i ], "-debugnormals" ) )
2274 debugnormals = qtrue;
2275 Sys_Printf( "DebugNormals enabled\n" );
2277 else if( !strcmp( argv[ i ], "-lowquality" ) )
2279 floodlight_lowquality = qtrue;
2280 Sys_Printf( "Low Quality FloodLighting enabled\n" );
2283 /* r7: dirtmapping */
2284 else if( !strcmp( argv[ i ], "-dirty" ) )
2287 Sys_Printf( "Dirtmapping enabled\n" );
2289 else if( !strcmp( argv[ i ], "-dirtdebug" ) || !strcmp( argv[ i ], "-debugdirt" ) )
2292 Sys_Printf( "Dirtmap debugging enabled\n" );
2294 else if( !strcmp( argv[ i ], "-dirtmode" ) )
2296 dirtMode = atoi( argv[ i + 1 ] );
2297 if( dirtMode != 0 && dirtMode != 1 )
2300 Sys_Printf( "Enabling randomized dirtmapping\n" );
2302 Sys_Printf( "Enabling ordered dir mapping\n" );
2305 else if( !strcmp( argv[ i ], "-dirtdepth" ) )
2307 dirtDepth = atof( argv[ i + 1 ] );
2308 if( dirtDepth <= 0.0f )
2310 Sys_Printf( "Dirtmapping depth set to %.1f\n", dirtDepth );
2313 else if( !strcmp( argv[ i ], "-dirtscale" ) )
2315 dirtScale = atof( argv[ i + 1 ] );
2316 if( dirtScale <= 0.0f )
2318 Sys_Printf( "Dirtmapping scale set to %.1f\n", dirtScale );
2321 else if( !strcmp( argv[ i ], "-dirtgain" ) )
2323 dirtGain = atof( argv[ i + 1 ] );
2324 if( dirtGain <= 0.0f )
2326 Sys_Printf( "Dirtmapping gain set to %.1f\n", dirtGain );
2329 else if( !strcmp( argv[ i ], "-trianglecheck" ) )
2331 lightmapTriangleCheck = qtrue;
2333 else if( !strcmp( argv[ i ], "-extravisnudge" ) )
2335 lightmapExtraVisClusterNudge = qtrue;
2337 /* unhandled args */
2340 Sys_Printf( "WARNING: Unknown argument \"%s\"\n", argv[ i ] );
2345 /* clean up map name */
2346 strcpy( source, ExpandArg( argv[ i ] ) );
2347 StripExtension( source );
2348 DefaultExtension( source, ".bsp" );
2349 strcpy( mapSource, ExpandArg( argv[ i ] ) );
2350 StripExtension( mapSource );
2351 DefaultExtension( mapSource, ".map" );
2353 /* ydnar: set default sample size */
2354 SetDefaultSampleSize( sampleSize );
2356 /* ydnar: handle shaders */
2357 BeginMapShaderFile( source );
2361 Sys_Printf( "Loading %s\n", source );
2363 /* ydnar: load surface file */
2364 LoadSurfaceExtraFile( source );
2367 LoadBSPFile( source );
2369 /* parse bsp entities */
2373 value = ValueForKey( &entities[ 0 ], "_keepLights" );
2374 if( value[ 0 ] != '1' )
2375 LoadMapFile( mapSource, qtrue );
2377 /* set the entity/model origins and init yDrawVerts */
2380 /* ydnar: set up optimization */
2384 SetupSurfaceLightmaps();
2386 /* initialize the surface facet tracing */
2389 /* light the world */
2392 /* ydnar: store off lightmaps */
2393 StoreSurfaceLightmaps();
2395 /* write out the bsp */
2397 Sys_Printf( "Writing %s\n", source );
2398 WriteBSPFile( source );
2400 /* ydnar: export lightmaps */
2401 if( exportLightmaps && !externalLightmaps )
2404 /* return to sender */