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 ){
48 float photons, d, angle, elevation, da, de;
59 if ( sun->numSamples < 1 ) {
64 photons = sun->photons / sun->numSamples;
66 /* create the right number of suns */
67 for ( i = 0; i < sun->numSamples; i++ )
69 /* calculate sun direction */
71 VectorCopy( sun->direction, direction );
76 sun->direction[ 0 ] = cos( angle ) * cos( elevation );
77 sun->direction[ 1 ] = sin( angle ) * cos( elevation );
78 sun->direction[ 2 ] = sin( elevation );
80 xz_dist = sqrt( x*x + z*z )
81 latitude = atan2( xz_dist, y ) * RADIANS
82 longitude = atan2( x, z ) * RADIANS
85 d = sqrt( sun->direction[ 0 ] * sun->direction[ 0 ] + sun->direction[ 1 ] * sun->direction[ 1 ] );
86 angle = atan2( sun->direction[ 1 ], sun->direction[ 0 ] );
87 elevation = atan2( sun->direction[ 2 ], d );
89 /* jitter the angles (loop to keep random sample within sun->deviance steridians) */
92 da = ( Random() * 2.0f - 1.0f ) * sun->deviance;
93 de = ( Random() * 2.0f - 1.0f ) * sun->deviance;
95 while ( ( da * da + de * de ) > ( sun->deviance * sun->deviance ) );
100 //% Sys_Printf( "%d: Angle: %3.4f Elevation: %3.3f\n", sun->numSamples, (angle / Q_PI * 180.0f), (elevation / Q_PI * 180.0f) );
102 /* create new vector */
103 direction[ 0 ] = cos( angle ) * cos( elevation );
104 direction[ 1 ] = sin( angle ) * cos( elevation );
105 direction[ 2 ] = sin( elevation );
110 light = safe_malloc0( sizeof( *light ) );
111 light->next = lights;
114 /* initialize the light */
115 light->flags = LIGHT_SUN_DEFAULT;
116 light->type = EMIT_SUN;
118 light->falloffTolerance = falloffTolerance;
119 light->filterRadius = sun->filterRadius / sun->numSamples;
120 light->style = noStyles ? LS_NORMAL : sun->style;
122 /* set the light's position out to infinity */
123 VectorMA( vec3_origin, ( MAX_WORLD_COORD * 8.0f ), direction, light->origin ); /* MAX_WORLD_COORD * 2.0f */
125 /* set the facing to be the inverse of the sun direction */
126 VectorScale( direction, -1.0, light->normal );
127 light->dist = DotProduct( light->origin, light->normal );
129 /* set color and photons */
130 VectorCopy( sun->color, light->color );
131 light->photons = photons * skyScale;
135 if ( sun->next != NULL ) {
136 CreateSunLight( sun->next );
143 CreateSkyLights() - ydnar
144 simulates sky light with multiple suns
147 static void CreateSkyLights( vec3_t color, float value, int iterations, float filterRadius, int style ){
149 int angleSteps, elevationSteps;
150 float angle, elevation;
151 float angleStep, elevationStep;
156 if ( value <= 0.0f || iterations < 2 ) {
160 /* basic sun setup */
161 VectorCopy( color, sun.color );
163 sun.filterRadius = filterRadius;
165 sun.style = noStyles ? LS_NORMAL : style;
169 elevationSteps = iterations - 1;
170 angleSteps = elevationSteps * 4;
172 elevationStep = DEG2RAD( 90.0f / iterations ); /* skip elevation 0 */
173 angleStep = DEG2RAD( 360.0f / angleSteps );
175 /* calc individual sun brightness */
176 numSuns = angleSteps * elevationSteps + 1;
177 sun.photons = value / numSuns;
179 /* iterate elevation */
180 elevation = elevationStep * 0.5f;
182 for ( i = 0, elevation = elevationStep * 0.5f; i < elevationSteps; i++ )
185 for ( j = 0; j < angleSteps; j++ )
188 sun.direction[ 0 ] = cos( angle ) * cos( elevation );
189 sun.direction[ 1 ] = sin( angle ) * cos( elevation );
190 sun.direction[ 2 ] = sin( elevation );
191 CreateSunLight( &sun );
198 elevation += elevationStep;
199 angle += angleStep / elevationSteps;
202 /* create vertical sun */
203 VectorSet( sun.direction, 0.0f, 0.0f, 1.0f );
204 CreateSunLight( &sun );
214 creates lights from light entities
217 void CreateEntityLights( void ){
219 light_t *light, *light2;
225 float intensity, scale, deviance, filterRadius;
226 int spawnflags, flags, numSamples;
230 /* go throught entity list and find lights */
231 for ( i = 0; i < numEntities; i++ )
235 name = ValueForKey( e, "classname" );
237 /* ydnar: check for lightJunior */
238 if ( Q_strncasecmp( name, "lightJunior", 11 ) == 0 ) {
241 else if ( Q_strncasecmp( name, "light", 5 ) == 0 ) {
248 /* lights with target names (and therefore styles) are only parsed from BSP */
249 target = ValueForKey( e, "targetname" );
250 if ( target[ 0 ] != '\0' && i >= numBSPEntities ) {
256 light = safe_malloc0( sizeof( *light ) );
257 light->next = lights;
260 /* handle spawnflags */
261 spawnflags = IntForKey( e, "spawnflags" );
263 /* ydnar: quake 3+ light behavior */
264 if ( wolfLight == qfalse ) {
265 /* set default flags */
266 flags = LIGHT_Q3A_DEFAULT;
268 /* linear attenuation? */
269 if ( spawnflags & 1 ) {
270 flags |= LIGHT_ATTEN_LINEAR;
271 flags &= ~LIGHT_ATTEN_ANGLE;
274 /* no angle attenuate? */
275 if ( spawnflags & 2 ) {
276 flags &= ~LIGHT_ATTEN_ANGLE;
280 /* ydnar: wolf light behavior */
283 /* set default flags */
284 flags = LIGHT_WOLF_DEFAULT;
286 /* inverse distance squared attenuation? */
287 if ( spawnflags & 1 ) {
288 flags &= ~LIGHT_ATTEN_LINEAR;
289 flags |= LIGHT_ATTEN_ANGLE;
292 /* angle attenuate? */
293 if ( spawnflags & 2 ) {
294 flags |= LIGHT_ATTEN_ANGLE;
298 /* other flags (borrowed from wolf) */
300 /* wolf dark light? */
301 if ( ( spawnflags & 4 ) || ( spawnflags & 8 ) ) {
306 if ( spawnflags & 16 ) {
307 flags &= ~LIGHT_GRID;
313 flags &= ~LIGHT_SURFACES;
316 /* vortex: unnormalized? */
317 if ( spawnflags & 32 ) {
318 flags |= LIGHT_UNNORMALIZED;
321 /* vortex: distance atten? */
322 if ( spawnflags & 64 ) {
323 flags |= LIGHT_ATTEN_DISTANCE;
326 /* store the flags */
327 light->flags = flags;
329 /* ydnar: set fade key (from wolf) */
331 if ( light->flags & LIGHT_ATTEN_LINEAR ) {
332 light->fade = FloatForKey( e, "fade" );
333 if ( light->fade == 0.0f ) {
338 /* ydnar: set angle scaling (from vlight) */
339 light->angleScale = FloatForKey( e, "_anglescale" );
340 if ( light->angleScale != 0.0f ) {
341 light->flags |= LIGHT_ATTEN_ANGLE;
345 GetVectorForKey( e, "origin", light->origin );
346 light->style = IntForKey( e, "_style" );
347 if ( light->style == LS_NORMAL ) {
348 light->style = IntForKey( e, "style" );
350 if ( light->style < LS_NORMAL || light->style >= LS_NONE ) {
351 Error( "Invalid lightstyle (%d) on entity %d", light->style, i );
354 if ( light->style != LS_NORMAL ) {
355 Sys_FPrintf( SYS_WRN, "WARNING: Styled light found targeting %s\n **", target );
358 /* set light intensity */
359 intensity = FloatForKey( e, "_light" );
360 if ( intensity == 0.0f ) {
361 intensity = FloatForKey( e, "light" );
363 if ( intensity == 0.0f ) {
367 /* ydnar: set light scale (sof2) */
368 scale = FloatForKey( e, "scale" );
369 if ( scale == 0.0f ) {
374 /* ydnar: get deviance and samples */
375 deviance = FloatForKey( e, "_deviance" );
376 if ( deviance == 0.0f ) {
377 deviance = FloatForKey( e, "_deviation" );
379 if ( deviance == 0.0f ) {
380 deviance = FloatForKey( e, "_jitter" );
382 numSamples = IntForKey( e, "_samples" );
383 if ( deviance < 0.0f || numSamples < 1 ) {
387 intensity /= numSamples;
389 /* ydnar: get filter radius */
390 filterRadius = FloatForKey( e, "_filterradius" );
391 if ( filterRadius == 0.0f ) {
392 filterRadius = FloatForKey( e, "_filteradius" );
394 if ( filterRadius == 0.0f ) {
395 filterRadius = FloatForKey( e, "_filter" );
397 if ( filterRadius < 0.0f ) {
400 light->filterRadius = filterRadius;
402 /* set light color */
403 _color = ValueForKey( e, "_color" );
404 if ( _color && _color[ 0 ] ) {
405 sscanf( _color, "%f %f %f", &light->color[ 0 ], &light->color[ 1 ], &light->color[ 2 ] );
407 light->color[0] = Image_LinearFloatFromsRGBFloat( light->color[0] );
408 light->color[1] = Image_LinearFloatFromsRGBFloat( light->color[1] );
409 light->color[2] = Image_LinearFloatFromsRGBFloat( light->color[2] );
411 if ( !( light->flags & LIGHT_UNNORMALIZED ) ) {
412 ColorNormalize( light->color, light->color );
416 light->color[ 0 ] = light->color[ 1 ] = light->color[ 2 ] = 1.0f;
419 light->extraDist = FloatForKey( e, "_extradist" );
420 if ( light->extraDist == 0.0f ) {
421 light->extraDist = extraDist;
424 light->photons = intensity;
426 light->type = EMIT_POINT;
428 /* set falloff threshold */
429 light->falloffTolerance = falloffTolerance / numSamples;
431 /* lights with a target will be spotlights */
432 target = ValueForKey( e, "target" );
441 e2 = FindTargetEntity( target );
443 Sys_FPrintf( SYS_WRN, "WARNING: light at (%i %i %i) has missing target\n",
444 (int) light->origin[ 0 ], (int) light->origin[ 1 ], (int) light->origin[ 2 ] );
445 light->photons *= pointScale;
449 /* not a point light */
453 /* make a spotlight */
454 GetVectorForKey( e2, "origin", dest );
455 VectorSubtract( dest, light->origin, light->normal );
456 dist = VectorNormalize( light->normal, light->normal );
457 radius = FloatForKey( e, "radius" );
464 light->radiusByDist = ( radius + 16 ) / dist;
465 light->type = EMIT_SPOT;
467 /* ydnar: wolf mods: spotlights always use nonlinear + angle attenuation */
468 light->flags &= ~LIGHT_ATTEN_LINEAR;
469 light->flags |= LIGHT_ATTEN_ANGLE;
472 /* ydnar: is this a sun? */
473 _sun = ValueForKey( e, "_sun" );
474 if ( _sun[ 0 ] == '1' ) {
475 /* not a spot light */
478 /* unlink this light */
479 lights = light->next;
482 VectorScale( light->normal, -1.0f, sun.direction );
483 VectorCopy( light->color, sun.color );
484 sun.photons = intensity;
485 sun.deviance = deviance / 180.0f * Q_PI;
486 sun.numSamples = numSamples;
487 sun.style = noStyles ? LS_NORMAL : light->style;
490 /* make a sun light */
491 CreateSunLight( &sun );
493 /* free original light */
497 /* skip the rest of this love story */
502 light->photons *= spotScale;
507 light->photons *= pointScale;
510 /* jitter the light */
511 for ( j = 1; j < numSamples; j++ )
514 light2 = safe_malloc( sizeof( *light ) );
515 memcpy( light2, light, sizeof( *light ) );
516 light2->next = lights;
520 if ( light->type == EMIT_SPOT ) {
528 light2->origin[ 0 ] = light->origin[ 0 ] + ( Random() * 2.0f - 1.0f ) * deviance;
529 light2->origin[ 1 ] = light->origin[ 1 ] + ( Random() * 2.0f - 1.0f ) * deviance;
530 light2->origin[ 2 ] = light->origin[ 2 ] + ( Random() * 2.0f - 1.0f ) * deviance;
538 CreateSurfaceLights() - ydnar
539 this hijacks the radiosity code to generate surface lights for first pass
542 #define APPROX_BOUNCE 1.0f
544 void CreateSurfaceLights( void ){
546 bspDrawSurface_t *ds;
556 /* get sun shader supressor */
557 nss = ValueForKey( &entities[ 0 ], "_noshadersun" );
559 /* walk the list of surfaces */
560 for ( i = 0; i < numBSPDrawSurfaces; i++ )
562 /* get surface and other bits */
563 ds = &bspDrawSurfaces[ i ];
564 info = &surfaceInfos[ i ];
568 if ( si->sun != NULL && nss[ 0 ] != '1' ) {
569 Sys_FPrintf( SYS_VRB, "Sun: %s\n", si->shader );
570 CreateSunLight( si->sun );
571 si->sun = NULL; /* FIXME: leak! */
575 if ( si->skyLightValue > 0.0f ) {
576 Sys_FPrintf( SYS_VRB, "Sky: %s\n", si->shader );
577 CreateSkyLights( si->color, si->skyLightValue, si->skyLightIterations, si->lightFilterRadius, si->lightStyle );
578 si->skyLightValue = 0.0f; /* FIXME: hack! */
581 /* try to early out */
582 if ( si->value <= 0 ) {
586 /* autosprite shaders become point lights */
587 if ( si->autosprite ) {
588 /* create an average xyz */
589 VectorAdd( info->mins, info->maxs, origin );
590 VectorScale( origin, 0.5f, origin );
593 light = safe_malloc0( sizeof( *light ) );
594 light->next = lights;
598 light->flags = LIGHT_Q3A_DEFAULT;
599 light->type = EMIT_POINT;
600 light->photons = si->value * pointScale;
603 VectorCopy( origin, light->origin );
604 VectorCopy( si->color, light->color );
605 light->falloffTolerance = falloffTolerance;
606 light->style = si->lightStyle;
608 /* add to point light count and continue */
613 /* get subdivision amount */
614 if ( si->lightSubdivide > 0 ) {
615 subdivide = si->lightSubdivide;
618 subdivide = defaultLightSubdivide;
622 switch ( ds->surfaceType )
625 case MST_TRIANGLE_SOUP:
626 RadLightForTriangles( i, 0, info->lm, si, APPROX_BOUNCE, subdivide, &cw );
630 RadLightForPatch( i, 0, info->lm, si, APPROX_BOUNCE, subdivide, &cw );
643 find the offset values for inline models
646 void SetEntityOrigins( void ){
653 bspDrawSurface_t *ds;
656 /* ydnar: copy drawverts into private storage for nefarious purposes */
657 yDrawVerts = safe_malloc( numBSPDrawVerts * sizeof( bspDrawVert_t ) );
658 memcpy( yDrawVerts, bspDrawVerts, numBSPDrawVerts * sizeof( bspDrawVert_t ) );
660 /* set the entity origins */
661 for ( i = 0; i < numEntities; i++ )
663 /* get entity and model */
665 key = ValueForKey( e, "model" );
666 if ( key[ 0 ] != '*' ) {
669 modelnum = atoi( key + 1 );
670 dm = &bspModels[ modelnum ];
672 /* get entity origin */
673 key = ValueForKey( e, "origin" );
674 if ( key[ 0 ] == '\0' ) {
677 GetVectorForKey( e, "origin", origin );
679 /* set origin for all surfaces for this model */
680 for ( j = 0; j < dm->numBSPSurfaces; j++ )
683 ds = &bspDrawSurfaces[ dm->firstBSPSurface + j ];
686 for ( k = 0; k < ds->numVerts; k++ )
688 f = ds->firstVert + k;
689 VectorAdd( origin, bspDrawVerts[ f ].xyz, yDrawVerts[ f ].xyz );
698 PointToPolygonFormFactor()
699 calculates the area over a point/normal hemisphere a winding covers
700 ydnar: fixme: there has to be a faster way to calculate this
701 without the expensive per-vert sqrts and transcendental functions
702 ydnar 2002-09-30: added -faster switch because only 19% deviance > 10%
703 between this and the approximation
706 #define ONE_OVER_2PI 0.159154942f //% (1.0f / (2.0f * 3.141592657f))
708 float PointToPolygonFormFactor( const vec3_t point, const vec3_t normal, const winding_t *w ){
709 vec3_t triVector, triNormal;
711 vec3_t dirs[ MAX_POINTS_ON_WINDING ];
713 float dot, angle, facing;
716 /* this is expensive */
717 for ( i = 0; i < w->numpoints; i++ )
719 VectorSubtract( w->p[ i ], point, dirs[ i ] );
720 VectorNormalize( dirs[ i ], dirs[ i ] );
723 /* duplicate first vertex to avoid mod operation */
724 VectorCopy( dirs[ 0 ], dirs[ i ] );
726 /* calculcate relative area */
728 for ( i = 0; i < w->numpoints; i++ )
732 dot = DotProduct( dirs[ i ], dirs[ j ] );
734 /* roundoff can cause slight creep, which gives an IND from acos */
738 else if ( dot < -1.0f ) {
745 CrossProduct( dirs[ i ], dirs[ j ], triVector );
746 if ( VectorNormalize( triVector, triNormal ) < 0.0001f ) {
750 facing = DotProduct( normal, triNormal );
751 total += facing * angle;
753 /* ydnar: this was throwing too many errors with radiosity + crappy maps. ignoring it. */
754 if ( total > 6.3f || total < -6.3f ) {
759 /* now in the range of 0 to 1 over the entire incoming hemisphere */
760 //% total /= (2.0f * 3.141592657f);
761 total *= ONE_OVER_2PI;
768 LightContributionTosample()
769 determines the amount of light reaching a sample (luxel or vertex) from a given light
772 int LightContributionToSample( trace_t *trace ){
777 float addDeluxe = 0.0f, addDeluxeBounceScale = 0.25f;
778 qboolean angledDeluxe = qtrue;
779 float colorBrightness;
780 qboolean doAddDeluxe = qtrue;
783 light = trace->light;
786 trace->forceSubsampling = 0.0f; /* to make sure */
787 VectorClear( trace->color );
788 VectorClear( trace->colorNoShadow );
789 VectorClear( trace->directionContribution );
791 colorBrightness = RGBTOGRAY( light->color ) * ( 1.0f / 255.0f );
793 /* ydnar: early out */
794 if ( !( light->flags & LIGHT_SURFACES ) || light->envelope <= 0.0f ) {
798 /* do some culling checks */
799 if ( light->type != EMIT_SUN ) {
800 /* MrE: if the light is behind the surface */
801 if ( trace->twoSided == qfalse ) {
802 if ( DotProduct( light->origin, trace->normal ) - DotProduct( trace->origin, trace->normal ) < 0.0f ) {
807 /* ydnar: test pvs */
808 if ( !ClusterVisible( trace->cluster, light->cluster ) ) {
813 /* exact point to polygon form factor */
814 if ( light->type == EMIT_AREA ) {
819 /* project sample point into light plane */
820 d = DotProduct( trace->origin, light->normal ) - light->dist;
822 /* sample point behind plane? */
823 if ( !( light->flags & LIGHT_TWOSIDED ) && d < -1.0f ) {
827 /* sample plane coincident? */
828 if ( d > -3.0f && DotProduct( trace->normal, light->normal ) > 0.9f ) {
833 /* nudge the point so that it is clearly forward of the light */
834 /* so that surfaces meeting a light emitter don't get black edges */
835 if ( d > -8.0f && d < 8.0f ) {
836 VectorMA( trace->origin, ( 8.0f - d ), light->normal, pushedOrigin );
839 VectorCopy( trace->origin, pushedOrigin );
842 /* get direction and distance */
843 VectorCopy( light->origin, trace->end );
844 dist = SetupTrace( trace );
845 if ( dist >= light->envelope ) {
849 /* ptpff approximation */
851 /* angle attenuation */
852 angle = DotProduct( trace->normal, trace->direction );
854 /* twosided lighting */
855 if ( trace->twoSided && angle < 0 ) {
858 /* no deluxemap contribution from "other side" light */
859 doAddDeluxe = qfalse;
863 angle *= -DotProduct( light->normal, trace->direction );
864 if ( angle == 0.0f ) {
867 else if ( angle < 0.0f &&
868 ( trace->twoSided || ( light->flags & LIGHT_TWOSIDED ) ) ) {
871 /* no deluxemap contribution from "other side" light */
872 doAddDeluxe = qfalse;
875 /* clamp the distance to prevent super hot spots */
876 dist = sqrt( dist * dist + light->extraDist * light->extraDist );
877 if ( dist < 16.0f ) {
881 add = light->photons / ( dist * dist ) * angle;
884 if ( angledDeluxe ) {
885 addDeluxe = light->photons / ( dist * dist ) * angle;
888 addDeluxe = light->photons / ( dist * dist );
894 /* calculate the contribution */
895 factor = PointToPolygonFormFactor( pushedOrigin, trace->normal, light->w );
896 if ( factor == 0.0f ) {
899 else if ( factor < 0.0f ) {
900 /* twosided lighting */
901 if ( trace->twoSided || ( light->flags & LIGHT_TWOSIDED ) ) {
904 /* push light origin to other side of the plane */
905 VectorMA( light->origin, -2.0f, light->normal, trace->end );
906 dist = SetupTrace( trace );
907 if ( dist >= light->envelope ) {
911 /* no deluxemap contribution from "other side" light */
912 doAddDeluxe = qfalse;
919 /* also don't deluxe if the direction is on the wrong side */
920 if ( DotProduct( trace->normal, trace->direction ) < 0 ) {
921 /* no deluxemap contribution from "other side" light */
922 doAddDeluxe = qfalse;
925 /* ydnar: moved to here */
926 add = factor * light->add;
934 /* point/spot lights */
935 else if ( light->type == EMIT_POINT || light->type == EMIT_SPOT ) {
936 /* get direction and distance */
937 VectorCopy( light->origin, trace->end );
938 dist = SetupTrace( trace );
939 if ( dist >= light->envelope ) {
943 /* clamp the distance to prevent super hot spots */
944 dist = sqrt( dist * dist + light->extraDist * light->extraDist );
945 if ( dist < 16.0f ) {
949 /* angle attenuation */
950 if ( light->flags & LIGHT_ATTEN_ANGLE ) {
951 /* standard Lambert attenuation */
952 float dot = DotProduct( trace->normal, trace->direction );
954 /* twosided lighting */
955 if ( trace->twoSided && dot < 0 ) {
958 /* no deluxemap contribution from "other side" light */
959 doAddDeluxe = qfalse;
962 /* jal: optional half Lambert attenuation (http://developer.valvesoftware.com/wiki/Half_Lambert) */
963 if ( lightAngleHL ) {
964 if ( dot > 0.001f ) { // skip coplanar
968 dot = ( dot * 0.5f ) + 0.5f;
982 if ( light->angleScale != 0.0f ) {
983 angle /= light->angleScale;
984 if ( angle > 1.0f ) {
990 if ( light->flags & LIGHT_ATTEN_LINEAR ) {
991 add = angle * light->photons * linearScale - ( dist * light->fade );
997 if ( angledDeluxe ) {
998 addDeluxe = angle * light->photons * linearScale - ( dist * light->fade );
1001 addDeluxe = light->photons * linearScale - ( dist * light->fade );
1004 if ( addDeluxe < 0.0f ) {
1011 add = ( light->photons / ( dist * dist ) ) * angle;
1017 if ( angledDeluxe ) {
1018 addDeluxe = ( light->photons / ( dist * dist ) ) * angle;
1021 addDeluxe = ( light->photons / ( dist * dist ) );
1025 if ( addDeluxe < 0.0f ) {
1030 /* handle spotlights */
1031 if ( light->type == EMIT_SPOT ) {
1032 float distByNormal, radiusAtDist, sampleRadius;
1033 vec3_t pointAtDist, distToSample;
1035 /* do cone calculation */
1036 distByNormal = -DotProduct( trace->displacement, light->normal );
1037 if ( distByNormal < 0.0f ) {
1040 VectorMA( light->origin, distByNormal, light->normal, pointAtDist );
1041 radiusAtDist = light->radiusByDist * distByNormal;
1042 VectorSubtract( trace->origin, pointAtDist, distToSample );
1043 sampleRadius = VectorLength( distToSample );
1045 /* outside the cone */
1046 if ( sampleRadius >= radiusAtDist ) {
1051 if ( sampleRadius > ( radiusAtDist - 32.0f ) ) {
1052 add *= ( ( radiusAtDist - sampleRadius ) / 32.0f );
1057 addDeluxe *= ( ( radiusAtDist - sampleRadius ) / 32.0f );
1059 if ( addDeluxe < 0.0f ) {
1066 /* ydnar: sunlight */
1067 else if ( light->type == EMIT_SUN ) {
1068 /* get origin and direction */
1069 VectorAdd( trace->origin, light->origin, trace->end );
1070 dist = SetupTrace( trace );
1072 /* angle attenuation */
1073 if ( light->flags & LIGHT_ATTEN_ANGLE ) {
1074 /* standard Lambert attenuation */
1075 float dot = DotProduct( trace->normal, trace->direction );
1077 /* twosided lighting */
1078 if ( trace->twoSided && dot < 0 ) {
1081 /* no deluxemap contribution from "other side" light */
1082 doAddDeluxe = qfalse;
1085 /* jal: optional half Lambert attenuation (http://developer.valvesoftware.com/wiki/Half_Lambert) */
1086 if ( lightAngleHL ) {
1087 if ( dot > 0.001f ) { // skip coplanar
1091 dot = ( dot * 0.5f ) + 0.5f;
1106 add = light->photons * angle;
1109 if ( angledDeluxe ) {
1110 addDeluxe = light->photons * angle;
1113 addDeluxe = light->photons;
1116 if ( addDeluxe < 0.0f ) {
1121 if ( add <= 0.0f ) {
1125 /* VorteX: set noShadow color */
1126 VectorScale( light->color, add, trace->colorNoShadow );
1128 addDeluxe *= colorBrightness;
1131 addDeluxe *= addDeluxeBounceScale;
1132 if ( addDeluxe < 0.00390625f ) {
1133 addDeluxe = 0.00390625f;
1137 VectorScale( trace->direction, addDeluxe, trace->directionContribution );
1140 trace->testAll = qtrue;
1141 VectorScale( light->color, add, trace->color );
1143 /* trace to point */
1144 if ( trace->testOcclusion && !trace->forceSunlight ) {
1147 trace->forceSubsampling *= add;
1148 if ( !( trace->compileFlags & C_SKY ) || trace->opaque ) {
1149 VectorClear( trace->color );
1150 VectorClear( trace->directionContribution );
1156 /* return to sender */
1160 Error( "Light of undefined type!" );
1163 /* VorteX: set noShadow color */
1164 VectorScale( light->color, add, trace->colorNoShadow );
1166 /* ydnar: changed to a variable number */
1167 if ( add <= 0.0f || ( add <= light->falloffTolerance && ( light->flags & LIGHT_FAST_ACTUAL ) ) ) {
1171 addDeluxe *= colorBrightness;
1173 /* hack land: scale down the radiosity contribution to light directionality.
1174 Deluxemaps fusion many light directions into one. In a rtl process all lights
1175 would contribute individually to the bump map, so several light sources together
1176 would make it more directional (example: a yellow and red lights received from
1177 opposing sides would light one side in red and the other in blue, adding
1178 the effect of 2 directions applied. In the deluxemapping case, this 2 lights would
1179 neutralize each other making it look like having no direction.
1180 Same thing happens with radiosity. In deluxemapping case the radiosity contribution
1181 is modifying the direction applied from directional lights, making it go closer and closer
1182 to the surface normal the bigger is the amount of radiosity received.
1183 So, for preserving the directional lights contributions, we scale down the radiosity
1184 contribution. It's a hack, but there's a reason behind it */
1186 addDeluxe *= addDeluxeBounceScale;
1187 /* better NOT increase it beyond the original value
1188 if( addDeluxe < 0.00390625f )
1189 addDeluxe = 0.00390625f;
1193 if ( doAddDeluxe ) {
1194 VectorScale( trace->direction, addDeluxe, trace->directionContribution );
1198 trace->testAll = qfalse;
1199 VectorScale( light->color, add, trace->color );
1203 trace->forceSubsampling *= add;
1204 if ( trace->passSolid || trace->opaque ) {
1205 VectorClear( trace->color );
1206 VectorClear( trace->directionContribution );
1211 /* return to sender */
1219 determines the amount of light reaching a sample (luxel or vertex)
1222 void LightingAtSample( trace_t *trace, byte styles[ MAX_LIGHTMAPS ], vec3_t colors[ MAX_LIGHTMAPS ] ){
1227 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
1228 VectorClear( colors[ lightmapNum ] );
1230 /* ydnar: normalmap */
1232 colors[ 0 ][ 0 ] = ( trace->normal[ 0 ] + 1.0f ) * 127.5f;
1233 colors[ 0 ][ 1 ] = ( trace->normal[ 1 ] + 1.0f ) * 127.5f;
1234 colors[ 0 ][ 2 ] = ( trace->normal[ 2 ] + 1.0f ) * 127.5f;
1238 /* ydnar: don't bounce ambient all the time */
1240 VectorCopy( ambientColor, colors[ 0 ] );
1243 /* ydnar: trace to all the list of lights pre-stored in tw */
1244 for ( i = 0; i < trace->numLights && trace->lights[ i ] != NULL; i++ )
1247 trace->light = trace->lights[ i ];
1250 for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ )
1252 if ( styles[ lightmapNum ] == trace->light->style ||
1253 styles[ lightmapNum ] == LS_NONE ) {
1258 /* max of MAX_LIGHTMAPS (4) styles allowed to hit a sample */
1259 if ( lightmapNum >= MAX_LIGHTMAPS ) {
1264 LightContributionToSample( trace );
1265 if ( trace->color[ 0 ] == 0.0f && trace->color[ 1 ] == 0.0f && trace->color[ 2 ] == 0.0f ) {
1269 /* handle negative light */
1270 if ( trace->light->flags & LIGHT_NEGATIVE ) {
1271 VectorScale( trace->color, -1.0f, trace->color );
1275 styles[ lightmapNum ] = trace->light->style;
1278 VectorAdd( colors[ lightmapNum ], trace->color, colors[ lightmapNum ] );
1282 colors[ 0 ][ 0 ] >= 255.0f &&
1283 colors[ 0 ][ 1 ] >= 255.0f &&
1284 colors[ 0 ][ 2 ] >= 255.0f ) {
1293 LightContributionToPoint()
1294 for a given light, how much light/color reaches a given point in space (with no facing)
1295 note: this is similar to LightContributionToSample() but optimized for omnidirectional sampling
1298 int LightContributionToPoint( trace_t *trace ){
1304 light = trace->light;
1307 VectorClear( trace->color );
1309 /* ydnar: early out */
1310 if ( !( light->flags & LIGHT_GRID ) || light->envelope <= 0.0f ) {
1314 /* is this a sun? */
1315 if ( light->type != EMIT_SUN ) {
1322 if ( !ClusterVisible( trace->cluster, light->cluster ) ) {
1327 /* ydnar: check origin against light's pvs envelope */
1328 if ( trace->origin[ 0 ] > light->maxs[ 0 ] || trace->origin[ 0 ] < light->mins[ 0 ] ||
1329 trace->origin[ 1 ] > light->maxs[ 1 ] || trace->origin[ 1 ] < light->mins[ 1 ] ||
1330 trace->origin[ 2 ] > light->maxs[ 2 ] || trace->origin[ 2 ] < light->mins[ 2 ] ) {
1335 /* set light origin */
1336 if ( light->type == EMIT_SUN ) {
1337 VectorAdd( trace->origin, light->origin, trace->end );
1340 VectorCopy( light->origin, trace->end );
1344 dist = SetupTrace( trace );
1347 if ( dist > light->envelope ) {
1348 gridEnvelopeCulled++;
1352 /* ptpff approximation */
1353 if ( light->type == EMIT_AREA && faster ) {
1354 /* clamp the distance to prevent super hot spots */
1355 dist = sqrt( dist * dist + light->extraDist * light->extraDist );
1356 if ( dist < 16.0f ) {
1361 add = light->photons / ( dist * dist );
1364 /* exact point to polygon form factor */
1365 else if ( light->type == EMIT_AREA ) {
1367 vec3_t pushedOrigin;
1370 /* see if the point is behind the light */
1371 d = DotProduct( trace->origin, light->normal ) - light->dist;
1372 if ( !( light->flags & LIGHT_TWOSIDED ) && d < -1.0f ) {
1376 /* nudge the point so that it is clearly forward of the light */
1377 /* so that surfaces meeting a light emiter don't get black edges */
1378 if ( d > -8.0f && d < 8.0f ) {
1379 VectorMA( trace->origin, ( 8.0f - d ), light->normal, pushedOrigin );
1382 VectorCopy( trace->origin, pushedOrigin );
1385 /* calculate the contribution (ydnar 2002-10-21: [bug 642] bad normal calc) */
1386 factor = PointToPolygonFormFactor( pushedOrigin, trace->direction, light->w );
1387 if ( factor == 0.0f ) {
1390 else if ( factor < 0.0f ) {
1391 if ( light->flags & LIGHT_TWOSIDED ) {
1399 /* ydnar: moved to here */
1400 add = factor * light->add;
1403 /* point/spot lights */
1404 else if ( light->type == EMIT_POINT || light->type == EMIT_SPOT ) {
1405 /* clamp the distance to prevent super hot spots */
1406 dist = sqrt( dist * dist + light->extraDist * light->extraDist );
1407 if ( dist < 16.0f ) {
1412 if ( light->flags & LIGHT_ATTEN_LINEAR ) {
1413 add = light->photons * linearScale - ( dist * light->fade );
1419 add = light->photons / ( dist * dist );
1422 /* handle spotlights */
1423 if ( light->type == EMIT_SPOT ) {
1424 float distByNormal, radiusAtDist, sampleRadius;
1425 vec3_t pointAtDist, distToSample;
1428 /* do cone calculation */
1429 distByNormal = -DotProduct( trace->displacement, light->normal );
1430 if ( distByNormal < 0.0f ) {
1433 VectorMA( light->origin, distByNormal, light->normal, pointAtDist );
1434 radiusAtDist = light->radiusByDist * distByNormal;
1435 VectorSubtract( trace->origin, pointAtDist, distToSample );
1436 sampleRadius = VectorLength( distToSample );
1438 /* outside the cone */
1439 if ( sampleRadius >= radiusAtDist ) {
1444 if ( sampleRadius > ( radiusAtDist - 32.0f ) ) {
1445 add *= ( ( radiusAtDist - sampleRadius ) / 32.0f );
1450 /* ydnar: sunlight */
1451 else if ( light->type == EMIT_SUN ) {
1453 add = light->photons;
1454 if ( add <= 0.0f ) {
1459 trace->testAll = qtrue;
1460 VectorScale( light->color, add, trace->color );
1462 /* trace to point */
1463 if ( trace->testOcclusion && !trace->forceSunlight ) {
1466 if ( !( trace->compileFlags & C_SKY ) || trace->opaque ) {
1467 VectorClear( trace->color );
1472 /* return to sender */
1476 /* unknown light type */
1481 /* ydnar: changed to a variable number */
1482 if ( add <= 0.0f || ( add <= light->falloffTolerance && ( light->flags & LIGHT_FAST_ACTUAL ) ) ) {
1487 trace->testAll = qfalse;
1488 VectorScale( light->color, add, trace->color );
1492 if ( trace->passSolid ) {
1493 VectorClear( trace->color );
1497 /* we have a valid sample */
1505 grid samples are for quickly determining the lighting
1506 of dynamically placed entities in the world
1509 #define MAX_CONTRIBUTIONS 32768
1520 void TraceGrid( int num ){
1521 int i, j, x, y, z, mod, numCon, numStyles;
1523 vec3_t baseOrigin, cheapColor, color, thisdir;
1525 bspGridPoint_t *bgp;
1526 contribution_t contributions[ MAX_CONTRIBUTIONS ];
1529 /* get grid points */
1530 gp = &rawGridPoints[ num ];
1531 bgp = &bspGridPoints[ num ];
1533 /* get grid origin */
1535 z = mod / ( gridBounds[ 0 ] * gridBounds[ 1 ] );
1536 mod -= z * ( gridBounds[ 0 ] * gridBounds[ 1 ] );
1537 y = mod / gridBounds[ 0 ];
1538 mod -= y * gridBounds[ 0 ];
1541 trace.origin[ 0 ] = gridMins[ 0 ] + x * gridSize[ 0 ];
1542 trace.origin[ 1 ] = gridMins[ 1 ] + y * gridSize[ 1 ];
1543 trace.origin[ 2 ] = gridMins[ 2 ] + z * gridSize[ 2 ];
1545 /* set inhibit sphere */
1546 if ( gridSize[ 0 ] > gridSize[ 1 ] && gridSize[ 0 ] > gridSize[ 2 ] ) {
1547 trace.inhibitRadius = gridSize[ 0 ] * 0.5f;
1549 else if ( gridSize[ 1 ] > gridSize[ 0 ] && gridSize[ 1 ] > gridSize[ 2 ] ) {
1550 trace.inhibitRadius = gridSize[ 1 ] * 0.5f;
1553 trace.inhibitRadius = gridSize[ 2 ] * 0.5f;
1556 /* find point cluster */
1557 trace.cluster = ClusterForPointExt( trace.origin, GRID_EPSILON );
1558 if ( trace.cluster < 0 ) {
1559 /* try to nudge the origin around to find a valid point */
1560 VectorCopy( trace.origin, baseOrigin );
1561 for ( step = 0; ( step += 0.005 ) <= 1.0; )
1563 VectorCopy( baseOrigin, trace.origin );
1564 trace.origin[ 0 ] += step * ( Random() - 0.5 ) * gridSize[0];
1565 trace.origin[ 1 ] += step * ( Random() - 0.5 ) * gridSize[1];
1566 trace.origin[ 2 ] += step * ( Random() - 0.5 ) * gridSize[2];
1568 /* ydnar: changed to find cluster num */
1569 trace.cluster = ClusterForPointExt( trace.origin, VERTEX_EPSILON );
1570 if ( trace.cluster >= 0 ) {
1575 /* can't find a valid point at all */
1582 trace.testOcclusion = !noTrace;
1583 trace.forceSunlight = qfalse;
1584 trace.recvShadows = WORLDSPAWN_RECV_SHADOWS;
1585 trace.numSurfaces = 0;
1586 trace.surfaces = NULL;
1587 trace.numLights = 0;
1588 trace.lights = NULL;
1592 VectorClear( cheapColor );
1594 /* trace to all the lights, find the major light direction, and divide the
1595 total light between that along the direction and the remaining in the ambient */
1596 for ( trace.light = lights; trace.light != NULL; trace.light = trace.light->next )
1602 if ( !LightContributionToPoint( &trace ) ) {
1606 /* handle negative light */
1607 if ( trace.light->flags & LIGHT_NEGATIVE ) {
1608 VectorScale( trace.color, -1.0f, trace.color );
1611 /* add a contribution */
1612 VectorCopy( trace.color, contributions[ numCon ].color );
1613 VectorCopy( trace.direction, contributions[ numCon ].dir );
1614 VectorClear( contributions[ numCon ].ambient );
1615 contributions[ numCon ].style = trace.light->style;
1618 /* push average direction around */
1619 addSize = VectorLength( trace.color );
1620 VectorMA( gp->dir, addSize, trace.direction, gp->dir );
1622 /* stop after a while */
1623 if ( numCon >= ( MAX_CONTRIBUTIONS - 1 ) ) {
1627 /* ydnar: cheap mode */
1628 VectorAdd( cheapColor, trace.color, cheapColor );
1629 if ( cheapgrid && cheapColor[ 0 ] >= 255.0f && cheapColor[ 1 ] >= 255.0f && cheapColor[ 2 ] >= 255.0f ) {
1634 /////// Floodlighting for point //////////////////
1635 //do our floodlight ambient occlusion loop, and add a single contribution based on the brightest dir
1636 if ( floodlighty ) {
1639 vec3_t dir = { 0, 0, 1 };
1640 float ambientFrac = 0.25f;
1642 trace.testOcclusion = qtrue;
1643 trace.forceSunlight = qfalse;
1644 trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS;
1645 trace.testAll = qtrue;
1647 for ( k = 0; k < 2; k++ )
1649 if ( k == 0 ) { // upper hemisphere
1650 trace.normal[0] = 0;
1651 trace.normal[1] = 0;
1652 trace.normal[2] = 1;
1654 else //lower hemisphere
1656 trace.normal[0] = 0;
1657 trace.normal[1] = 0;
1658 trace.normal[2] = -1;
1661 f = FloodLightForSample( &trace, floodlightDistance, floodlight_lowquality );
1663 /* add a fraction as pure ambient, half as top-down direction */
1664 contributions[ numCon ].color[0] = floodlightRGB[0] * floodlightIntensity * f * ( 1.0f - ambientFrac );
1665 contributions[ numCon ].color[1] = floodlightRGB[1] * floodlightIntensity * f * ( 1.0f - ambientFrac );
1666 contributions[ numCon ].color[2] = floodlightRGB[2] * floodlightIntensity * f * ( 1.0f - ambientFrac );
1668 contributions[ numCon ].ambient[0] = floodlightRGB[0] * floodlightIntensity * f * ambientFrac;
1669 contributions[ numCon ].ambient[1] = floodlightRGB[1] * floodlightIntensity * f * ambientFrac;
1670 contributions[ numCon ].ambient[2] = floodlightRGB[2] * floodlightIntensity * f * ambientFrac;
1672 contributions[ numCon ].dir[0] = dir[0];
1673 contributions[ numCon ].dir[1] = dir[1];
1674 contributions[ numCon ].dir[2] = dir[2];
1676 contributions[ numCon ].style = 0;
1678 /* push average direction around */
1679 addSize = VectorLength( contributions[ numCon ].color );
1680 VectorMA( gp->dir, addSize, dir, gp->dir );
1685 /////////////////////
1687 /* normalize to get primary light direction */
1688 VectorNormalize( gp->dir, thisdir );
1690 /* now that we have identified the primary light direction,
1691 go back and separate all the light into directed and ambient */
1694 for ( i = 0; i < numCon; i++ )
1696 /* get relative directed strength */
1697 d = DotProduct( contributions[ i ].dir, thisdir );
1698 /* we map 1 to gridDirectionality, and 0 to gridAmbientDirectionality */
1699 d = gridAmbientDirectionality + d * ( gridDirectionality - gridAmbientDirectionality );
1704 /* find appropriate style */
1705 for ( j = 0; j < numStyles; j++ )
1707 if ( gp->styles[ j ] == contributions[ i ].style ) {
1712 /* style not found? */
1713 if ( j >= numStyles ) {
1714 /* add a new style */
1715 if ( numStyles < MAX_LIGHTMAPS ) {
1716 gp->styles[ numStyles ] = contributions[ i ].style;
1717 bgp->styles[ numStyles ] = contributions[ i ].style;
1719 //% Sys_Printf( "(%d, %d) ", num, contributions[ i ].style );
1728 /* add the directed color */
1729 VectorMA( gp->directed[ j ], d, contributions[ i ].color, gp->directed[ j ] );
1731 /* ambient light will be at 1/4 the value of directed light */
1732 /* (ydnar: nuke this in favor of more dramatic lighting?) */
1733 /* (PM: how about actually making it work? d=1 when it got here for single lights/sun :P */
1735 /* (Hobbes: always setting it to .25 is hardly any better) */
1736 d = 0.25f * ( 1.0f - d );
1737 VectorMA( gp->ambient[ j ], d, contributions[ i ].color, gp->ambient[ j ] );
1739 VectorAdd( gp->ambient[ j ], contributions[ i ].ambient, gp->ambient[ j ] );
1743 * the total light average = ambient value + 0.25 * sum of all directional values
1744 * we can also get the total light average as 0.25 * the sum of all contributions
1746 * 0.25 * sum(contribution_i) == ambient + 0.25 * sum(d_i contribution_i)
1749 * ambient == 0.25 * sum((1 - d_i) contribution_i)
1751 * So, 0.25f * (1.0f - d) IS RIGHT. If you want to tune it, tune d BEFORE.
1756 /* store off sample */
1757 for ( i = 0; i < MAX_LIGHTMAPS; i++ )
1760 /* do some fudging to keep the ambient from being too low (2003-07-05: 0.25 -> 0.125) */
1762 VectorMA( gp->ambient[ i ], 0.125f, gp->directed[ i ], gp->ambient[ i ] );
1766 /* set minimum light and copy off to bytes */
1767 VectorCopy( gp->ambient[ i ], color );
1768 for ( j = 0; j < 3; j++ )
1769 if ( color[ j ] < minGridLight[ j ] ) {
1770 color[ j ] = minGridLight[ j ];
1773 /* vortex: apply gridscale and gridambientscale here */
1774 ColorToBytes( color, bgp->ambient[ i ], gridScale * gridAmbientScale );
1775 ColorToBytes( gp->directed[ i ], bgp->directed[ i ], gridScale );
1780 //% Sys_FPrintf( SYS_VRB, "%10d %10d %10d ", &gp->ambient[ 0 ][ 0 ], &gp->ambient[ 0 ][ 1 ], &gp->ambient[ 0 ][ 2 ] );
1781 Sys_FPrintf( SYS_VRB, "%9d Amb: (%03.1f %03.1f %03.1f) Dir: (%03.1f %03.1f %03.1f)\n",
1783 gp->ambient[ 0 ][ 0 ], gp->ambient[ 0 ][ 1 ], gp->ambient[ 0 ][ 2 ],
1784 gp->directed[ 0 ][ 0 ], gp->directed[ 0 ][ 1 ], gp->directed[ 0 ][ 2 ] );
1787 /* store direction */
1788 NormalToLatLong( thisdir, bgp->latLong );
1795 calculates the size of the lightgrid and allocates memory
1798 void SetupGrid( void ){
1800 vec3_t maxs, oldGridSize;
1805 /* don't do this if not grid lighting */
1806 if ( noGridLighting ) {
1810 /* ydnar: set grid size */
1811 value = ValueForKey( &entities[ 0 ], "gridsize" );
1812 if ( value[ 0 ] != '\0' ) {
1813 sscanf( value, "%f %f %f", &gridSize[ 0 ], &gridSize[ 1 ], &gridSize[ 2 ] );
1817 VectorCopy( gridSize, oldGridSize );
1818 for ( i = 0; i < 3; i++ )
1819 gridSize[ i ] = gridSize[ i ] >= 8.0f ? floor( gridSize[ i ] ) : 8.0f;
1821 /* ydnar: increase gridSize until grid count is smaller than max allowed */
1822 numRawGridPoints = MAX_MAP_LIGHTGRID + 1;
1824 while ( numRawGridPoints > MAX_MAP_LIGHTGRID )
1826 /* get world bounds */
1827 for ( i = 0; i < 3; i++ )
1829 gridMins[ i ] = gridSize[ i ] * ceil( bspModels[ 0 ].mins[ i ] / gridSize[ i ] );
1830 maxs[ i ] = gridSize[ i ] * floor( bspModels[ 0 ].maxs[ i ] / gridSize[ i ] );
1831 gridBounds[ i ] = ( maxs[ i ] - gridMins[ i ] ) / gridSize[ i ] + 1;
1835 numRawGridPoints = gridBounds[ 0 ] * gridBounds[ 1 ] * gridBounds[ 2 ];
1837 /* increase grid size a bit */
1838 if ( numRawGridPoints > MAX_MAP_LIGHTGRID ) {
1839 gridSize[ j++ % 3 ] += 16.0f;
1844 Sys_Printf( "Grid size = { %1.0f, %1.0f, %1.0f }\n", gridSize[ 0 ], gridSize[ 1 ], gridSize[ 2 ] );
1847 if ( !VectorCompare( gridSize, oldGridSize ) ) {
1848 sprintf( temp, "%.0f %.0f %.0f", gridSize[ 0 ], gridSize[ 1 ], gridSize[ 2 ] );
1849 SetKeyValue( &entities[ 0 ], "gridsize", (const char*) temp );
1850 Sys_FPrintf( SYS_VRB, "Storing adjusted grid size\n" );
1853 /* 2nd variable. fixme: is this silly? */
1854 numBSPGridPoints = numRawGridPoints;
1856 /* allocate lightgrid */
1857 rawGridPoints = safe_malloc0( numRawGridPoints * sizeof( *rawGridPoints ) );
1859 if ( bspGridPoints != NULL ) {
1860 free( bspGridPoints );
1862 bspGridPoints = safe_malloc0( numBSPGridPoints * sizeof( *bspGridPoints ) );
1864 /* clear lightgrid */
1865 for ( i = 0; i < numRawGridPoints; i++ )
1867 VectorCopy( ambientColor, rawGridPoints[ i ].ambient[ j ] );
1868 rawGridPoints[ i ].styles[ 0 ] = LS_NORMAL;
1869 bspGridPoints[ i ].styles[ 0 ] = LS_NORMAL;
1870 for ( j = 1; j < MAX_LIGHTMAPS; j++ )
1872 rawGridPoints[ i ].styles[ j ] = LS_NONE;
1873 bspGridPoints[ i ].styles[ j ] = LS_NONE;
1878 Sys_Printf( "%9d grid points\n", numRawGridPoints );
1885 does what it says...
1888 void LightWorld( const char *BSPFilePath, qboolean fastLightmapSearch, qboolean noBounceStore ){
1892 qboolean minVertex, minGrid;
1895 /* ydnar: smooth normals */
1897 Sys_Printf( "--- SmoothNormals ---\n" );
1901 /* determine the number of grid points */
1902 Sys_Printf( "--- SetupGrid ---\n" );
1905 /* find the optional minimum lighting values */
1906 GetVectorForKey( &entities[ 0 ], "_color", color );
1907 if ( VectorLength( color ) == 0.0f ) {
1908 VectorSet( color, 1.0, 1.0, 1.0 );
1912 color[0] = Image_LinearFloatFromsRGBFloat( color[0] );
1913 color[1] = Image_LinearFloatFromsRGBFloat( color[1] );
1914 color[2] = Image_LinearFloatFromsRGBFloat( color[2] );
1918 f = FloatForKey( &entities[ 0 ], "_ambient" );
1920 f = FloatForKey( &entities[ 0 ], "ambient" );
1922 VectorScale( color, f, ambientColor );
1924 /* minvertexlight */
1926 value = ValueForKey( &entities[ 0 ], "_minvertexlight" );
1927 if ( value[ 0 ] != '\0' ) {
1930 VectorScale( color, f, minVertexLight );
1935 value = ValueForKey( &entities[ 0 ], "_mingridlight" );
1936 if ( value[ 0 ] != '\0' ) {
1939 VectorScale( color, f, minGridLight );
1943 value = ValueForKey( &entities[ 0 ], "_minlight" );
1944 if ( value[ 0 ] != '\0' ) {
1946 VectorScale( color, f, minLight );
1947 if ( minVertex == qfalse ) {
1948 VectorScale( color, f, minVertexLight );
1950 if ( minGrid == qfalse ) {
1951 VectorScale( color, f, minGridLight );
1955 /* create world lights */
1956 Sys_FPrintf( SYS_VRB, "--- CreateLights ---\n" );
1957 CreateEntityLights();
1958 CreateSurfaceLights();
1959 Sys_Printf( "%9d point lights\n", numPointLights );
1960 Sys_Printf( "%9d spotlights\n", numSpotLights );
1961 Sys_Printf( "%9d diffuse (area) lights\n", numDiffuseLights );
1962 Sys_Printf( "%9d sun/sky lights\n", numSunLights );
1964 /* calculate lightgrid */
1965 if ( !noGridLighting ) {
1966 /* ydnar: set up light envelopes */
1967 SetupEnvelopes( qtrue, fastgrid );
1969 Sys_Printf( "--- TraceGrid ---\n" );
1971 RunThreadsOnIndividual( numRawGridPoints, qtrue, TraceGrid );
1973 Sys_Printf( "%d x %d x %d = %d grid\n",
1974 gridBounds[ 0 ], gridBounds[ 1 ], gridBounds[ 2 ], numBSPGridPoints );
1976 /* ydnar: emit statistics on light culling */
1977 Sys_FPrintf( SYS_VRB, "%9d grid points envelope culled\n", gridEnvelopeCulled );
1978 Sys_FPrintf( SYS_VRB, "%9d grid points bounds culled\n", gridBoundsCulled );
1981 /* slight optimization to remove a sqrt */
1982 subdivideThreshold *= subdivideThreshold;
1984 /* map the world luxels */
1985 Sys_Printf( "--- MapRawLightmap ---\n" );
1986 RunThreadsOnIndividual( numRawLightmaps, qtrue, MapRawLightmap );
1987 Sys_Printf( "%9d luxels\n", numLuxels );
1988 Sys_Printf( "%9d luxels mapped\n", numLuxelsMapped );
1989 Sys_Printf( "%9d luxels occluded\n", numLuxelsOccluded );
1993 Sys_Printf( "--- DirtyRawLightmap ---\n" );
1994 RunThreadsOnIndividual( numRawLightmaps, qtrue, DirtyRawLightmap );
1997 /* floodlight pass */
1998 FloodlightRawLightmaps();
2000 /* ydnar: set up light envelopes */
2001 SetupEnvelopes( qfalse, fast );
2003 /* light up my world */
2004 lightsPlaneCulled = 0;
2005 lightsEnvelopeCulled = 0;
2006 lightsBoundsCulled = 0;
2007 lightsClusterCulled = 0;
2009 Sys_Printf( "--- IlluminateRawLightmap ---\n" );
2010 RunThreadsOnIndividual( numRawLightmaps, qtrue, IlluminateRawLightmap );
2011 Sys_Printf( "%9d luxels illuminated\n", numLuxelsIlluminated );
2013 StitchSurfaceLightmaps();
2015 Sys_Printf( "--- IlluminateVertexes ---\n" );
2016 RunThreadsOnIndividual( numBSPDrawSurfaces, qtrue, IlluminateVertexes );
2017 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
2019 /* ydnar: emit statistics on light culling */
2020 Sys_FPrintf( SYS_VRB, "%9d lights plane culled\n", lightsPlaneCulled );
2021 Sys_FPrintf( SYS_VRB, "%9d lights envelope culled\n", lightsEnvelopeCulled );
2022 Sys_FPrintf( SYS_VRB, "%9d lights bounds culled\n", lightsBoundsCulled );
2023 Sys_FPrintf( SYS_VRB, "%9d lights cluster culled\n", lightsClusterCulled );
2029 while ( bounce > 0 )
2031 qboolean storeForReal = !noBounceStore;
2033 /* store off the bsp between bounces */
2034 StoreSurfaceLightmaps( fastLightmapSearch, storeForReal );
2037 if ( storeForReal ) {
2038 Sys_Printf( "Writing %s\n", BSPFilePath );
2039 WriteBSPFile( BSPFilePath );
2043 Sys_Printf( "\n--- Radiosity (bounce %d of %d) ---\n", b, bt );
2047 VectorClear( ambientColor );
2048 floodlighty = qfalse;
2050 /* generate diffuse lights */
2052 RadCreateDiffuseLights();
2054 /* setup light envelopes */
2055 SetupEnvelopes( qfalse, fastbounce );
2056 if ( numLights == 0 ) {
2057 Sys_Printf( "No diffuse light to calculate, ending radiosity.\n" );
2058 if ( noBounceStore ) {
2064 /* add to lightgrid */
2066 gridEnvelopeCulled = 0;
2067 gridBoundsCulled = 0;
2069 Sys_Printf( "--- BounceGrid ---\n" );
2071 RunThreadsOnIndividual( numRawGridPoints, qtrue, TraceGrid );
2073 Sys_FPrintf( SYS_VRB, "%9d grid points envelope culled\n", gridEnvelopeCulled );
2074 Sys_FPrintf( SYS_VRB, "%9d grid points bounds culled\n", gridBoundsCulled );
2077 /* light up my world */
2078 lightsPlaneCulled = 0;
2079 lightsEnvelopeCulled = 0;
2080 lightsBoundsCulled = 0;
2081 lightsClusterCulled = 0;
2083 Sys_Printf( "--- IlluminateRawLightmap ---\n" );
2084 RunThreadsOnIndividual( numRawLightmaps, qtrue, IlluminateRawLightmap );
2085 Sys_Printf( "%9d luxels illuminated\n", numLuxelsIlluminated );
2086 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
2088 StitchSurfaceLightmaps();
2090 Sys_Printf( "--- IlluminateVertexes ---\n" );
2091 RunThreadsOnIndividual( numBSPDrawSurfaces, qtrue, IlluminateVertexes );
2092 Sys_Printf( "%9d vertexes illuminated\n", numVertsIlluminated );
2094 /* ydnar: emit statistics on light culling */
2095 Sys_FPrintf( SYS_VRB, "%9d lights plane culled\n", lightsPlaneCulled );
2096 Sys_FPrintf( SYS_VRB, "%9d lights envelope culled\n", lightsEnvelopeCulled );
2097 Sys_FPrintf( SYS_VRB, "%9d lights bounds culled\n", lightsBoundsCulled );
2098 Sys_FPrintf( SYS_VRB, "%9d lights cluster culled\n", lightsClusterCulled );
2105 /* ydnar: store off lightmaps */
2106 StoreSurfaceLightmaps( fastLightmapSearch, qtrue );
2113 added by spoon to get back the changed surfaceflags
2117 void LoadSurfaceFlags( char *filename ) {
2120 for( i = 0; i < numBSPShaders; i++ ) {
2123 si = ShaderInfoForShader( bspShaders[i].shader );
2125 bspShaders[ i ].surfaceFlags = si->surfaceFlags;
2133 main routine for light processing
2136 int LightMain( int argc, char **argv ){
2139 char BSPFilePath[ 1024 ];
2140 char surfaceFilePath[ 1024 ];
2142 surfaceFilePath[0] = 0;
2144 int lightmapMergeSize = 0;
2145 qboolean lightSamplesInsist = qfalse;
2146 qboolean fastLightmapSearch = qfalse;
2147 qboolean noBounceStore = qfalse;
2150 Sys_Printf( "--- Light ---\n" );
2151 Sys_Printf( "--- ProcessGameSpecific ---\n" );
2153 /* set standard game flags */
2154 wolfLight = game->wolfLight;
2155 if ( wolfLight == qtrue ) {
2156 Sys_Printf( " lightning model: wolf\n" );
2159 Sys_Printf( " lightning model: quake3\n" );
2162 lmCustomSize = game->lightmapSize;
2163 Sys_Printf( " lightmap size: %d x %d pixels\n", lmCustomSize, lmCustomSize );
2165 lightmapGamma = game->lightmapGamma;
2166 Sys_Printf( " lightning gamma: %f\n", lightmapGamma );
2168 lightmapsRGB = game->lightmapsRGB;
2169 if ( lightmapsRGB ) {
2170 Sys_Printf( " lightmap colorspace: sRGB\n" );
2173 Sys_Printf( " lightmap colorspace: linear\n" );
2176 texturesRGB = game->texturesRGB;
2177 if ( texturesRGB ) {
2178 Sys_Printf( " texture colorspace: sRGB\n" );
2181 Sys_Printf( " texture colorspace: linear\n" );
2184 colorsRGB = game->colorsRGB;
2186 Sys_Printf( " _color colorspace: sRGB\n" );
2189 Sys_Printf( " _color colorspace: linear\n" );
2192 lightmapCompensate = game->lightmapCompensate;
2193 Sys_Printf( " lightning compensation: %f\n", lightmapCompensate );
2195 lightmapExposure = game->lightmapExposure;
2196 Sys_Printf( " lightning exposure: %f\n", lightmapExposure );
2198 gridScale = game->gridScale;
2199 Sys_Printf( " lightgrid scale: %f\n", gridScale );
2201 gridAmbientScale = game->gridAmbientScale;
2202 Sys_Printf( " lightgrid ambient scale: %f\n", gridAmbientScale );
2204 lightAngleHL = game->lightAngleHL;
2205 if ( lightAngleHL ) {
2206 Sys_Printf( " half lambert light angle attenuation enabled \n" );
2209 noStyles = game->noStyles;
2210 if ( noStyles == qtrue ) {
2211 Sys_Printf( " shader lightstyles hack: disabled\n" );
2214 Sys_Printf( " shader lightstyles hack: enabled\n" );
2217 patchShadows = game->patchShadows;
2218 if ( patchShadows == qtrue ) {
2219 Sys_Printf( " patch shadows: enabled\n" );
2222 Sys_Printf( " patch shadows: disabled\n" );
2225 deluxemap = game->deluxeMap;
2226 deluxemode = game->deluxeMode;
2227 if ( deluxemap == qtrue ) {
2229 Sys_Printf( " deluxemapping: enabled with tangentspace deluxemaps\n" );
2232 Sys_Printf( " deluxemapping: enabled with modelspace deluxemaps\n" );
2236 Sys_Printf( " deluxemapping: disabled\n" );
2239 Sys_Printf( "--- ProcessCommandLine ---\n" );
2241 /* process commandline arguments */
2242 for ( i = 1; i < ( argc - 1 ); i++ )
2244 /* lightsource scaling */
2245 if ( !strcmp( argv[ i ], "-point" ) || !strcmp( argv[ i ], "-pointscale" ) ) {
2246 f = atof( argv[ i + 1 ] );
2249 Sys_Printf( "Spherical point (entity) light scaled by %f to %f\n", f, pointScale );
2250 Sys_Printf( "Spot point (entity) light scaled by %f to %f\n", f, spotScale );
2254 else if ( !strcmp( argv[ i ], "-spherical" ) || !strcmp( argv[ i ], "-sphericalscale" ) ) {
2255 f = atof( argv[ i + 1 ] );
2257 Sys_Printf( "Spherical point (entity) light scaled by %f to %f\n", f, pointScale );
2261 else if ( !strcmp( argv[ i ], "-spot" ) || !strcmp( argv[ i ], "-spotscale" ) ) {
2262 f = atof( argv[ i + 1 ] );
2264 Sys_Printf( "Spot point (entity) light scaled by %f to %f\n", f, spotScale );
2268 else if ( !strcmp( argv[ i ], "-area" ) || !strcmp( argv[ i ], "-areascale" ) ) {
2269 f = atof( argv[ i + 1 ] );
2271 Sys_Printf( "Area (shader) light scaled by %f to %f\n", f, areaScale );
2275 else if ( !strcmp( argv[ i ], "-sky" ) || !strcmp( argv[ i ], "-skyscale" ) ) {
2276 f = atof( argv[ i + 1 ] );
2278 Sys_Printf( "Sky/sun light scaled by %f to %f\n", f, skyScale );
2282 else if ( !strcmp( argv[ i ], "-bouncescale" ) ) {
2283 f = atof( argv[ i + 1 ] );
2285 Sys_Printf( "Bounce (radiosity) light scaled by %f to %f\n", f, bounceScale );
2289 else if ( !strcmp( argv[ i ], "-scale" ) ) {
2290 f = atof( argv[ i + 1 ] );
2296 Sys_Printf( "All light scaled by %f\n", f );
2300 else if ( !strcmp( argv[ i ], "-gridscale" ) ) {
2301 f = atof( argv[ i + 1 ] );
2302 Sys_Printf( "Grid lightning scaled by %f\n", f );
2307 else if ( !strcmp( argv[ i ], "-gridambientscale" ) ) {
2308 f = atof( argv[ i + 1 ] );
2309 Sys_Printf( "Grid ambient lightning scaled by %f\n", f );
2310 gridAmbientScale *= f;
2314 else if ( !strcmp( argv[ i ], "-griddirectionality" ) ) {
2315 f = atof( argv[ i + 1 ] );
2319 if ( f < gridAmbientDirectionality ) {
2320 gridAmbientDirectionality = f;
2322 Sys_Printf( "Grid directionality is %f\n", f );
2323 gridDirectionality = f;
2327 else if ( !strcmp( argv[ i ], "-gridambientdirectionality" ) ) {
2328 f = atof( argv[ i + 1 ] );
2332 if ( f > gridDirectionality ) {
2333 gridDirectionality = f;
2335 Sys_Printf( "Grid ambient directionality is %f\n", f );
2336 gridAmbientDirectionality = f;
2340 else if ( !strcmp( argv[ i ], "-gamma" ) ) {
2341 f = atof( argv[ i + 1 ] );
2343 Sys_Printf( "Lighting gamma set to %f\n", lightmapGamma );
2347 else if ( !strcmp( argv[ i ], "-sRGBlight" ) ) {
2348 lightmapsRGB = qtrue;
2349 Sys_Printf( "Lighting is in sRGB\n" );
2352 else if ( !strcmp( argv[ i ], "-nosRGBlight" ) ) {
2353 lightmapsRGB = qfalse;
2354 Sys_Printf( "Lighting is linear\n" );
2357 else if ( !strcmp( argv[ i ], "-sRGBtex" ) ) {
2358 texturesRGB = qtrue;
2359 Sys_Printf( "Textures are in sRGB\n" );
2362 else if ( !strcmp( argv[ i ], "-nosRGBtex" ) ) {
2363 texturesRGB = qfalse;
2364 Sys_Printf( "Textures are linear\n" );
2367 else if ( !strcmp( argv[ i ], "-sRGBcolor" ) ) {
2369 Sys_Printf( "Colors are in sRGB\n" );
2372 else if ( !strcmp( argv[ i ], "-nosRGBcolor" ) ) {
2374 Sys_Printf( "Colors are linear\n" );
2377 else if ( !strcmp( argv[ i ], "-sRGB" ) ) {
2378 lightmapsRGB = qtrue;
2379 Sys_Printf( "Lighting is in sRGB\n" );
2380 texturesRGB = qtrue;
2381 Sys_Printf( "Textures are in sRGB\n" );
2383 Sys_Printf( "Colors are in sRGB\n" );
2386 else if ( !strcmp( argv[ i ], "-nosRGB" ) ) {
2387 lightmapsRGB = qfalse;
2388 Sys_Printf( "Lighting is linear\n" );
2389 texturesRGB = qfalse;
2390 Sys_Printf( "Textures are linear\n" );
2392 Sys_Printf( "Colors are linear\n" );
2395 else if ( !strcmp( argv[ i ], "-exposure" ) ) {
2396 f = atof( argv[ i + 1 ] );
2397 lightmapExposure = f;
2398 Sys_Printf( "Lighting exposure set to %f\n", lightmapExposure );
2402 else if ( !strcmp( argv[ i ], "-compensate" ) ) {
2403 f = atof( argv[ i + 1 ] );
2407 lightmapCompensate = f;
2408 Sys_Printf( "Lighting compensation set to 1/%f\n", lightmapCompensate );
2412 /* ydnar switches */
2413 else if ( !strcmp( argv[ i ], "-bounce" ) ) {
2414 bounce = atoi( argv[ i + 1 ] );
2418 else if ( bounce > 0 ) {
2419 Sys_Printf( "Radiosity enabled with %d bounce(s)\n", bounce );
2424 else if ( !strcmp( argv[ i ], "-supersample" ) || !strcmp( argv[ i ], "-super" ) ) {
2425 superSample = atoi( argv[ i + 1 ] );
2426 if ( superSample < 1 ) {
2429 else if ( superSample > 1 ) {
2430 Sys_Printf( "Ordered-grid supersampling enabled with %d sample(s) per lightmap texel\n", ( superSample * superSample ) );
2435 else if ( !strcmp( argv[ i ], "-randomsamples" ) ) {
2436 lightRandomSamples = qtrue;
2437 Sys_Printf( "Random sampling enabled\n", lightRandomSamples );
2440 else if ( !strcmp( argv[ i ], "-samples" ) ) {
2441 if ( *argv[i + 1] == '+' ) {
2442 lightSamplesInsist = qtrue;
2445 lightSamplesInsist = qfalse;
2447 lightSamples = atoi( argv[ i + 1 ] );
2448 if ( lightSamples < 1 ) {
2451 else if ( lightSamples > 1 ) {
2452 Sys_Printf( "Adaptive supersampling enabled with %d sample(s) per lightmap texel\n", lightSamples );
2457 else if ( !strcmp( argv[ i ], "-samplessearchboxsize" ) ) {
2458 lightSamplesSearchBoxSize = atoi( argv[ i + 1 ] );
2459 if ( lightSamplesSearchBoxSize <= 0 ) {
2460 lightSamplesSearchBoxSize = 1;
2462 if ( lightSamplesSearchBoxSize > 4 ) {
2463 lightSamplesSearchBoxSize = 4; /* more makes no sense */
2465 else if ( lightSamplesSearchBoxSize != 1 ) {
2466 Sys_Printf( "Adaptive supersampling uses %f times the normal search box size\n", lightSamplesSearchBoxSize );
2471 else if ( !strcmp( argv[ i ], "-filter" ) ) {
2473 Sys_Printf( "Lightmap filtering enabled\n" );
2476 else if ( !strcmp( argv[ i ], "-dark" ) ) {
2478 Sys_Printf( "Dark lightmap seams enabled\n" );
2481 else if ( !strcmp( argv[ i ], "-shadeangle" ) ) {
2482 shadeAngleDegrees = atof( argv[ i + 1 ] );
2483 if ( shadeAngleDegrees < 0.0f ) {
2484 shadeAngleDegrees = 0.0f;
2486 else if ( shadeAngleDegrees > 0.0f ) {
2488 Sys_Printf( "Phong shading enabled with a breaking angle of %f degrees\n", shadeAngleDegrees );
2493 else if ( !strcmp( argv[ i ], "-thresh" ) ) {
2494 subdivideThreshold = atof( argv[ i + 1 ] );
2495 if ( subdivideThreshold < 0 ) {
2496 subdivideThreshold = DEFAULT_SUBDIVIDE_THRESHOLD;
2499 Sys_Printf( "Subdivision threshold set at %.3f\n", subdivideThreshold );
2504 else if ( !strcmp( argv[ i ], "-approx" ) ) {
2505 approximateTolerance = atoi( argv[ i + 1 ] );
2506 if ( approximateTolerance < 0 ) {
2507 approximateTolerance = 0;
2509 else if ( approximateTolerance > 0 ) {
2510 Sys_Printf( "Approximating lightmaps within a byte tolerance of %d\n", approximateTolerance );
2515 else if ( !strcmp( argv[ i ], "-deluxe" ) || !strcmp( argv[ i ], "-deluxemap" ) ) {
2517 Sys_Printf( "Generating deluxemaps for average light direction\n" );
2519 else if ( !strcmp( argv[ i ], "-deluxemode" ) ) {
2520 deluxemode = atoi( argv[ i + 1 ] );
2521 if ( deluxemode == 0 || deluxemode > 1 || deluxemode < 0 ) {
2522 Sys_Printf( "Generating modelspace deluxemaps\n" );
2526 Sys_Printf( "Generating tangentspace deluxemaps\n" );
2530 else if ( !strcmp( argv[ i ], "-nodeluxe" ) || !strcmp( argv[ i ], "-nodeluxemap" ) ) {
2532 Sys_Printf( "Disabling generating of deluxemaps for average light direction\n" );
2534 else if ( !strcmp( argv[ i ], "-external" ) ) {
2535 externalLightmaps = qtrue;
2536 Sys_Printf( "Storing all lightmaps externally\n" );
2539 else if ( !strcmp( argv[ i ], "-lightmapsize" ) ) {
2540 lmCustomSize = atoi( argv[ i + 1 ] );
2542 /* must be a power of 2 and greater than 2 */
2543 if ( ( ( lmCustomSize - 1 ) & lmCustomSize ) || lmCustomSize < 2 ) {
2544 Sys_FPrintf( SYS_WRN, "WARNING: Lightmap size must be a power of 2, greater or equal to 2 pixels.\n" );
2545 lmCustomSize = game->lightmapSize;
2548 Sys_Printf( "Default lightmap size set to %d x %d pixels\n", lmCustomSize, lmCustomSize );
2550 /* enable external lightmaps */
2551 if ( lmCustomSize != game->lightmapSize ) {
2552 externalLightmaps = qtrue;
2553 Sys_Printf( "Storing all lightmaps externally\n" );
2557 else if ( !strcmp( argv[ i ], "-rawlightmapsizelimit" ) ) {
2558 lmLimitSize = atoi( argv[ i + 1 ] );
2561 Sys_Printf( "Raw lightmap size limit set to %d x %d pixels\n", lmLimitSize, lmLimitSize );
2564 else if ( !strcmp( argv[ i ], "-lightmapdir" ) ) {
2565 lmCustomDir = argv[i + 1];
2567 Sys_Printf( "Lightmap directory set to %s\n", lmCustomDir );
2568 externalLightmaps = qtrue;
2569 Sys_Printf( "Storing all lightmaps externally\n" );
2572 /* ydnar: add this to suppress warnings */
2573 else if ( !strcmp( argv[ i ], "-custinfoparms" ) ) {
2574 Sys_Printf( "Custom info parms enabled\n" );
2575 useCustomInfoParms = qtrue;
2578 else if ( !strcmp( argv[ i ], "-wolf" ) ) {
2579 /* -game should already be set */
2581 Sys_Printf( "Enabling Wolf lighting model (linear default)\n" );
2584 else if ( !strcmp( argv[ i ], "-q3" ) ) {
2585 /* -game should already be set */
2587 Sys_Printf( "Enabling Quake 3 lighting model (nonlinear default)\n" );
2590 else if ( !strcmp( argv[ i ], "-extradist" ) ) {
2591 extraDist = atof( argv[ i + 1 ] );
2592 if ( extraDist < 0 ) {
2596 Sys_Printf( "Default extra radius set to %f units\n", extraDist );
2599 else if ( !strcmp( argv[ i ], "-sunonly" ) ) {
2601 Sys_Printf( "Only computing sunlight\n" );
2604 else if ( !strcmp( argv[ i ], "-bounceonly" ) ) {
2606 Sys_Printf( "Storing bounced light (radiosity) only\n" );
2609 else if ( !strcmp( argv[ i ], "-nobouncestore" ) ) {
2610 noBounceStore = qtrue;
2611 Sys_Printf( "Do not store BSP, lightmap and shader files between bounces\n" );
2614 else if ( !strcmp( argv[ i ], "-nocollapse" ) ) {
2616 Sys_Printf( "Identical lightmap collapsing disabled\n" );
2619 else if ( !strcmp( argv[ i ], "-nolightmapsearch" ) ) {
2620 lightmapSearchBlockSize = 1;
2621 Sys_Printf( "No lightmap searching - all lightmaps will be sequential\n" );
2624 else if ( !strcmp( argv[ i ], "-lightmapsearchpower" ) ) {
2625 lightmapMergeSize = ( game->lightmapSize << atoi( argv[i + 1] ) );
2627 Sys_Printf( "Restricted lightmap searching enabled - optimize for lightmap merge power %d (size %d)\n", atoi( argv[i] ), lightmapMergeSize );
2630 else if ( !strcmp( argv[ i ], "-lightmapsearchblocksize" ) ) {
2631 lightmapSearchBlockSize = atoi( argv[i + 1] );
2633 Sys_Printf( "Restricted lightmap searching enabled - block size set to %d\n", lightmapSearchBlockSize );
2636 else if ( !strcmp( argv[ i ], "-shade" ) ) {
2638 Sys_Printf( "Phong shading enabled\n" );
2641 else if ( !strcmp( argv[ i ], "-bouncegrid" ) ) {
2644 Sys_Printf( "Grid lighting with radiosity enabled\n" );
2648 else if ( !strcmp( argv[ i ], "-smooth" ) ) {
2649 lightSamples = EXTRA_SCALE;
2650 Sys_Printf( "The -smooth argument is deprecated, use \"-samples 2\" instead\n" );
2653 else if ( !strcmp( argv[ i ], "-nofastpoint" ) ) {
2655 Sys_Printf( "Automatic fast mode for point lights disabled\n" );
2658 else if ( !strcmp( argv[ i ], "-fast" ) ) {
2662 Sys_Printf( "Fast mode enabled for all area lights\n" );
2665 else if ( !strcmp( argv[ i ], "-faster" ) ) {
2670 Sys_Printf( "Faster mode enabled\n" );
2673 else if ( !strcmp( argv[ i ], "-fastlightmapsearch" ) || !strcmp( argv[ i ], "-fastallocate") ) {
2674 fastLightmapSearch = qtrue;
2676 if ( !strcmp( argv[ i ], "-fastallocate" ) ) {
2677 Sys_Printf( "The -fastallocate argument is deprecated, use \"-fastlightmapsearch\" instead\n" );
2680 Sys_Printf( "Fast lightmap search enabled\n" );
2684 else if ( !strcmp( argv[ i ], "-fastgrid" ) ) {
2686 Sys_Printf( "Fast grid lighting enabled\n" );
2689 else if ( !strcmp( argv[ i ], "-fastbounce" ) ) {
2691 Sys_Printf( "Fast bounce mode enabled\n" );
2694 else if ( !strcmp( argv[ i ], "-cheap" ) ) {
2697 Sys_Printf( "Cheap mode enabled\n" );
2700 else if ( !strcmp( argv[ i ], "-cheapgrid" ) ) {
2702 Sys_Printf( "Cheap grid mode enabled\n" );
2705 else if ( !strcmp( argv[ i ], "-normalmap" ) ) {
2707 Sys_Printf( "Storing normal map instead of lightmap\n" );
2710 else if ( !strcmp( argv[ i ], "-trisoup" ) ) {
2712 Sys_Printf( "Converting brush faces to triangle soup\n" );
2715 else if ( !strcmp( argv[ i ], "-debug" ) ) {
2717 Sys_Printf( "Lightmap debugging enabled\n" );
2720 else if ( !strcmp( argv[ i ], "-debugsurfaces" ) || !strcmp( argv[ i ], "-debugsurface" ) ) {
2721 debugSurfaces = qtrue;
2722 Sys_Printf( "Lightmap surface debugging enabled\n" );
2725 else if ( !strcmp( argv[ i ], "-debugunused" ) ) {
2726 debugUnused = qtrue;
2727 Sys_Printf( "Unused luxel debugging enabled\n" );
2730 else if ( !strcmp( argv[ i ], "-debugaxis" ) ) {
2732 Sys_Printf( "Lightmap axis debugging enabled\n" );
2735 else if ( !strcmp( argv[ i ], "-debugcluster" ) ) {
2736 debugCluster = qtrue;
2737 Sys_Printf( "Luxel cluster debugging enabled\n" );
2740 else if ( !strcmp( argv[ i ], "-debugorigin" ) ) {
2741 debugOrigin = qtrue;
2742 Sys_Printf( "Luxel origin debugging enabled\n" );
2745 else if ( !strcmp( argv[ i ], "-debugdeluxe" ) ) {
2747 debugDeluxemap = qtrue;
2748 Sys_Printf( "Deluxemap debugging enabled\n" );
2751 else if ( !strcmp( argv[ i ], "-export" ) ) {
2752 exportLightmaps = qtrue;
2753 Sys_Printf( "Exporting lightmaps\n" );
2756 else if ( !strcmp( argv[ i ], "-notrace" ) ) {
2758 Sys_Printf( "Shadow occlusion disabled\n" );
2760 else if ( !strcmp( argv[ i ], "-patchshadows" ) ) {
2761 patchShadows = qtrue;
2762 Sys_Printf( "Patch shadow casting enabled\n" );
2764 else if ( !strcmp( argv[ i ], "-extra" ) ) {
2765 superSample = EXTRA_SCALE; /* ydnar */
2766 Sys_Printf( "The -extra argument is deprecated, use \"-super 2\" instead\n" );
2768 else if ( !strcmp( argv[ i ], "-extrawide" ) ) {
2769 superSample = EXTRAWIDE_SCALE; /* ydnar */
2770 filter = qtrue; /* ydnar */
2771 Sys_Printf( "The -extrawide argument is deprecated, use \"-filter [-super 2]\" instead\n" );
2773 else if ( !strcmp( argv[ i ], "-samplesize" ) ) {
2774 sampleSize = atoi( argv[ i + 1 ] );
2775 if ( sampleSize < 1 ) {
2779 Sys_Printf( "Default lightmap sample size set to %dx%d units\n", sampleSize, sampleSize );
2781 else if ( !strcmp( argv[ i ], "-minsamplesize" ) ) {
2782 minSampleSize = atoi( argv[ i + 1 ] );
2783 if ( minSampleSize < 1 ) {
2787 Sys_Printf( "Minimum lightmap sample size set to %dx%d units\n", minSampleSize, minSampleSize );
2789 else if ( !strcmp( argv[ i ], "-samplescale" ) ) {
2790 sampleScale = atoi( argv[ i + 1 ] );
2792 Sys_Printf( "Lightmaps sample scale set to %d\n", sampleScale );
2794 else if ( !strcmp( argv[ i ], "-novertex" ) ) {
2795 noVertexLighting = qtrue;
2796 Sys_Printf( "Disabling vertex lighting\n" );
2798 else if ( !strcmp( argv[ i ], "-nogrid" ) ) {
2799 noGridLighting = qtrue;
2800 Sys_Printf( "Disabling grid lighting\n" );
2802 else if ( !strcmp( argv[ i ], "-border" ) ) {
2803 lightmapBorder = qtrue;
2804 Sys_Printf( "Adding debug border to lightmaps\n" );
2806 else if ( !strcmp( argv[ i ], "-nosurf" ) ) {
2808 Sys_Printf( "Not tracing against surfaces\n" );
2810 else if ( !strcmp( argv[ i ], "-dump" ) ) {
2812 Sys_Printf( "Dumping radiosity lights into numbered prefabs\n" );
2814 else if ( !strcmp( argv[ i ], "-lomem" ) ) {
2816 Sys_Printf( "Enabling low-memory (potentially slower) lighting mode\n" );
2818 else if ( !strcmp( argv[ i ], "-lightsubdiv" ) ) {
2819 defaultLightSubdivide = atoi( argv[ i + 1 ] );
2820 if ( defaultLightSubdivide < 1 ) {
2821 defaultLightSubdivide = 1;
2824 Sys_Printf( "Default light subdivision set to %d\n", defaultLightSubdivide );
2826 else if ( !strcmp( argv[ i ], "-lightanglehl" ) ) {
2827 if ( ( atoi( argv[ i + 1 ] ) != 0 ) != lightAngleHL ) {
2828 lightAngleHL = ( atoi( argv[ i + 1 ] ) != 0 );
2829 if ( lightAngleHL ) {
2830 Sys_Printf( "Enabling half lambert light angle attenuation\n" );
2833 Sys_Printf( "Disabling half lambert light angle attenuation\n" );
2838 else if ( !strcmp( argv[ i ], "-nostyle" ) || !strcmp( argv[ i ], "-nostyles" ) ) {
2840 Sys_Printf( "Disabling lightstyles\n" );
2842 else if ( !strcmp( argv[ i ], "-style" ) || !strcmp( argv[ i ], "-styles" ) ) {
2844 Sys_Printf( "Enabling lightstyles\n" );
2846 else if ( !strcmp( argv[ i ], "-cpma" ) ) {
2848 Sys_Printf( "Enabling Challenge Pro Mode Asstacular Vertex Lighting Mode (tm)\n" );
2850 else if ( !strcmp( argv[ i ], "-floodlight" ) ) {
2851 floodlighty = qtrue;
2852 Sys_Printf( "FloodLighting enabled\n" );
2854 else if ( !strcmp( argv[ i ], "-debugnormals" ) ) {
2855 debugnormals = qtrue;
2856 Sys_Printf( "DebugNormals enabled\n" );
2858 else if ( !strcmp( argv[ i ], "-lowquality" ) ) {
2859 floodlight_lowquality = qtrue;
2860 Sys_Printf( "Low Quality FloodLighting enabled\n" );
2863 /* r7: dirtmapping */
2864 else if ( !strcmp( argv[ i ], "-dirty" ) ) {
2866 Sys_Printf( "Dirtmapping enabled\n" );
2868 else if ( !strcmp( argv[ i ], "-dirtdebug" ) || !strcmp( argv[ i ], "-debugdirt" ) ) {
2870 Sys_Printf( "Dirtmap debugging enabled\n" );
2872 else if ( !strcmp( argv[ i ], "-dirtmode" ) ) {
2873 dirtMode = atoi( argv[ i + 1 ] );
2874 if ( dirtMode != 0 && dirtMode != 1 ) {
2877 if ( dirtMode == 1 ) {
2878 Sys_Printf( "Enabling randomized dirtmapping\n" );
2881 Sys_Printf( "Enabling ordered dir mapping\n" );
2885 else if ( !strcmp( argv[ i ], "-dirtdepth" ) ) {
2886 dirtDepth = atof( argv[ i + 1 ] );
2887 if ( dirtDepth <= 0.0f ) {
2890 Sys_Printf( "Dirtmapping depth set to %.1f\n", dirtDepth );
2893 else if ( !strcmp( argv[ i ], "-dirtscale" ) ) {
2894 dirtScale = atof( argv[ i + 1 ] );
2895 if ( dirtScale <= 0.0f ) {
2898 Sys_Printf( "Dirtmapping scale set to %.1f\n", dirtScale );
2901 else if ( !strcmp( argv[ i ], "-dirtgain" ) ) {
2902 dirtGain = atof( argv[ i + 1 ] );
2903 if ( dirtGain <= 0.0f ) {
2906 Sys_Printf( "Dirtmapping gain set to %.1f\n", dirtGain );
2909 else if ( !strcmp( argv[ i ], "-trianglecheck" ) ) {
2910 lightmapTriangleCheck = qtrue;
2912 else if ( !strcmp( argv[ i ], "-extravisnudge" ) ) {
2913 lightmapExtraVisClusterNudge = qtrue;
2915 else if ( !strcmp( argv[ i ], "-fill" ) ) {
2916 lightmapFill = qtrue;
2917 Sys_Printf( "Filling lightmap colors from surrounding pixels to improve JPEG compression\n" );
2919 else if ( !strcmp( argv[ i ], "-bspfile" ) )
2921 strcpy( BSPFilePath, argv[i + 1] );
2923 Sys_Printf( "Use %s as bsp file\n", BSPFilePath );
2925 else if ( !strcmp( argv[ i ], "-srffile" ) )
2927 strcpy( surfaceFilePath, argv[i + 1] );
2929 Sys_Printf( "Use %s as surface file\n", surfaceFilePath );
2931 /* unhandled args */
2934 Sys_FPrintf( SYS_WRN, "WARNING: Unknown argument \"%s\"\n", argv[ i ] );
2939 /* fix up falloff tolerance for sRGB */
2940 if ( lightmapsRGB ) {
2941 falloffTolerance = Image_LinearFloatFromsRGBFloat( falloffTolerance * ( 1.0 / 255.0 ) ) * 255.0;
2944 /* fix up samples count */
2945 if ( lightRandomSamples ) {
2946 if ( !lightSamplesInsist ) {
2947 /* approximately match -samples in quality */
2948 switch ( lightSamples )
2954 Sys_Printf( "Adaptive supersampling preset enabled with %d random sample(s) per lightmap texel\n", lightSamples );
2960 Sys_Printf( "Adaptive supersampling preset enabled with %d random sample(s) per lightmap texel\n", lightSamples );
2966 Sys_Printf( "Adaptive supersampling preset enabled with %d random sample(s) per lightmap texel\n", lightSamples );
2974 /* fix up lightmap search power */
2975 if ( lightmapMergeSize ) {
2976 lightmapSearchBlockSize = ( lightmapMergeSize / lmCustomSize ) * ( lightmapMergeSize / lmCustomSize );
2977 if ( lightmapSearchBlockSize < 1 ) {
2978 lightmapSearchBlockSize = 1;
2981 Sys_Printf( "Restricted lightmap searching enabled - block size adjusted to %d\n", lightmapSearchBlockSize );
2984 strcpy( source, ExpandArg( argv[ i ] ) );
2985 StripExtension( source );
2986 DefaultExtension( source, ".map" );
2988 if (!BSPFilePath[0]) {
2989 strcpy( BSPFilePath, ExpandArg( argv[ i ] ) );
2990 StripExtension( BSPFilePath );
2991 DefaultExtension( BSPFilePath, ".bsp" );
2994 if (!surfaceFilePath[0]) {
2995 strcpy( surfaceFilePath, ExpandArg( argv[ i ] ) );
2996 StripExtension( surfaceFilePath );
2997 DefaultExtension( surfaceFilePath, ".srf" );
3000 /* ydnar: set default sample size */
3001 SetDefaultSampleSize( sampleSize );
3003 /* ydnar: handle shaders */
3004 BeginMapShaderFile( BSPFilePath );
3008 Sys_Printf( "Loading %s\n", source );
3010 /* ydnar: load surface file */
3011 LoadSurfaceExtraFile( surfaceFilePath );
3014 LoadBSPFile( BSPFilePath );
3016 /* parse bsp entities */
3019 /* inject command line parameters */
3020 InjectCommandLine( argv, 0, argc - 1 );
3023 value = ValueForKey( &entities[ 0 ], "_keepLights" );
3024 if ( value[ 0 ] != '1' ) {
3025 LoadMapFile( source, qtrue, qfalse );
3028 /* set the entity/model origins and init yDrawVerts */
3031 /* ydnar: set up optimization */
3035 SetupSurfaceLightmaps();
3037 /* initialize the surface facet tracing */
3040 /* light the world */
3041 LightWorld( BSPFilePath, fastLightmapSearch, noBounceStore );
3043 /* write out the bsp */
3045 Sys_Printf( "Writing %s\n", BSPFilePath );
3046 WriteBSPFile( BSPFilePath );
3048 /* ydnar: export lightmaps */
3049 if ( exportLightmaps && !externalLightmaps ) {
3053 /* return to sender */