From 61d0c06cc64bb1263c249889ed921ea1987953ca Mon Sep 17 00:00:00 2001 From: Rudolf Polzer Date: Sun, 3 Oct 2010 20:23:14 +0200 Subject: [PATCH] option -randomsamples: makes -samples use adaptive random subsampling (only subsamples near edges, uses random distribution for luxels then) --- tools/quake3/q3map2/light.c | 6 ++++ tools/quake3/q3map2/light_ydnar.c | 50 ++++++++++++++++++++++++++++--- tools/quake3/q3map2/q3map2.h | 1 + 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/tools/quake3/q3map2/light.c b/tools/quake3/q3map2/light.c index c072be0..41bc1dc 100644 --- a/tools/quake3/q3map2/light.c +++ b/tools/quake3/q3map2/light.c @@ -2245,6 +2245,12 @@ int LightMain( int argc, char **argv ) i++; } + else if( !strcmp( argv[ i ], "-randomsamples" ) ) + { + lightRandomSamples = qtrue; + Sys_Printf( "Random sampling enabled\n", lightRandomSamples ); + } + else if( !strcmp( argv[ i ], "-samples" ) ) { lightSamples = atoi( argv[ i + 1 ] ); diff --git a/tools/quake3/q3map2/light_ydnar.c b/tools/quake3/q3map2/light_ydnar.c index 8ba5a9a..3c17d4b 100644 --- a/tools/quake3/q3map2/light_ydnar.c +++ b/tools/quake3/q3map2/light_ydnar.c @@ -1871,6 +1871,45 @@ static void SubsampleRawLuxel_r( rawLightmap_t *lm, trace_t *trace, vec3_t sampl lightLuxel[ 3 ] += 1.0f; } } +static void RandomSubsampleRawLuxel( rawLightmap_t *lm, trace_t *trace, vec3_t sampleOrigin, int x, int y, float bias, float *lightLuxel ) +{ + int b, samples, mapped; + int cluster; + vec3_t origin, normal; + vec3_t total; + + VectorClear( total ); + mapped = 0; + for(b = 0; b < lightSamples; ++b) + { + /* set origin */ + VectorCopy( sampleOrigin, origin ); + + /* calculate position */ + if( !SubmapRawLuxel( lm, x, y, (bias * (2 * Random() - 1)), (bias * (2 * Random() - 1)), &cluster, origin, normal ) ) + { + cluster = -1; + continue; + } + mapped++; + + trace->cluster = cluster; + VectorCopy( origin, trace->origin ); + VectorCopy( normal, trace->normal ); + + LightContributionToSample( trace ); + VectorAdd( total, trace->color, total ); + } + + /* add to luxel */ + if( mapped > 0 ) + { + /* average */ + lightLuxel[ 0 ] = total[ 0 ] / mapped; + lightLuxel[ 1 ] = total[ 1 ] / mapped; + lightLuxel[ 2 ] = total[ 2 ] / mapped; + } +} @@ -2099,7 +2138,7 @@ void IlluminateRawLightmap( int rawLightmapNum ) luxelFilterRadius = 1; /* allocate sampling flags storage */ - if(lightSamples > 1 && luxelFilterRadius == 0) + if((lightSamples > 1 || lightRandomSamples) && luxelFilterRadius == 0) { size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( unsigned char ); if(lm->superFlags == NULL) @@ -2152,7 +2191,7 @@ void IlluminateRawLightmap( int rawLightmapNum ) VectorAdd( deluxel, trace.directionContribution, deluxel ); /* check for evilness */ - if(trace.forceSubsampling > 1.0f && lightSamples > 1 && luxelFilterRadius == 0) + if(trace.forceSubsampling > 1.0f && (lightSamples > 1 || lightRandomSamples) && luxelFilterRadius == 0) { totalLighted++; *flag |= FLAG_FORCE_SUBSAMPLING; /* force */ @@ -2170,7 +2209,7 @@ void IlluminateRawLightmap( int rawLightmapNum ) /* secondary pass, adaptive supersampling (fixme: use a contrast function to determine if subsampling is necessary) */ /* 2003-09-27: changed it so filtering disamples supersampling, as it would waste time */ - if( lightSamples > 1 && luxelFilterRadius == 0 ) + if( (lightSamples > 1 || lightRandomSamples) && luxelFilterRadius == 0 ) { /* walk luxels */ for( y = 0; y < (lm->sh - 1); y++ ) @@ -2238,7 +2277,10 @@ void IlluminateRawLightmap( int rawLightmapNum ) //% continue; /* subsample it */ - SubsampleRawLuxel_r( lm, &trace, origin, sx, sy, 0.25f * lightSamplesSearchBoxSize, lightLuxel ); + if(lightRandomSamples) + RandomSubsampleRawLuxel( lm, &trace, origin, sx, sy, 0.5f, lightLuxel ); + else + SubsampleRawLuxel_r( lm, &trace, origin, sx, sy, 0.25f * lightSamplesSearchBoxSize, lightLuxel ); *flag |= FLAG_ALREADY_SUBSAMPLED; diff --git a/tools/quake3/q3map2/q3map2.h b/tools/quake3/q3map2/q3map2.h index 0294be7..b0e5f47 100644 --- a/tools/quake3/q3map2/q3map2.h +++ b/tools/quake3/q3map2/q3map2.h @@ -2197,6 +2197,7 @@ Q_EXTERN qboolean shade Q_ASSIGN( qfalse ); Q_EXTERN float shadeAngleDegrees Q_ASSIGN( 0.0f ); Q_EXTERN int superSample Q_ASSIGN( 0 ); Q_EXTERN int lightSamples Q_ASSIGN( 1 ); +Q_EXTERN qboolean lightRandomSamples Q_ASSIGN( qfalse ); Q_EXTERN int lightSamplesSearchBoxSize Q_ASSIGN( 1 ); Q_EXTERN qboolean filter Q_ASSIGN( qfalse ); Q_EXTERN qboolean dark Q_ASSIGN( qfalse ); -- 2.39.2