recursively subsamples a luxel until its color gradient is low enough or subsampling limit is reached
*/
-static void SubsampleRawLuxel_r( rawLightmap_t *lm, trace_t *trace, vec3_t sampleOrigin, int x, int y, float bias, float *lightLuxel )
+static void SubsampleRawLuxel_r( rawLightmap_t *lm, trace_t *trace, vec3_t sampleOrigin, int x, int y, float bias, float *lightLuxel, float *lightDeluxel )
{
int b, samples, mapped, lighted;
int cluster[ 4 ];
vec4_t luxel[ 4 ];
+ vec3_t deluxel[ 3 ];
vec3_t origin[ 4 ], normal[ 4 ];
float biasDirs[ 4 ][ 2 ] = { { -1.0f, -1.0f }, { 1.0f, -1.0f }, { -1.0f, 1.0f }, { 1.0f, 1.0f } };
- vec3_t color, total;
+ vec3_t color, direction, total;
/* limit check */
/* add to totals (fixme: make contrast function) */
VectorCopy( trace->color, luxel[ b ] );
+ if(lightDeluxel)
+ {
+ VectorCopy( trace->directionContribution, deluxel[ b ] );
+ }
VectorAdd( total, trace->color, total );
if( (luxel[ b ][ 0 ] + luxel[ b ][ 1 ] + luxel[ b ][ 2 ]) > 0.0f )
lighted++;
{
if( cluster[ b ] < 0 )
continue;
- SubsampleRawLuxel_r( lm, trace, origin[ b ], x, y, (bias * 0.5f), luxel[ b ] );
+ SubsampleRawLuxel_r( lm, trace, origin[ b ], x, y, (bias * 0.5f), luxel[ b ], lightDeluxel ? deluxel[ b ] : NULL );
}
}
//% VectorClear( color );
//% samples = 0;
VectorCopy( lightLuxel, color );
+ VectorCopy( lightDeluxel, direction );
samples = 1;
for( b = 0; b < 4; b++ )
{
if( cluster[ b ] < 0 )
continue;
VectorAdd( color, luxel[ b ], color );
+ if(lightDeluxel)
+ {
+ VectorAdd( direction, deluxel[ b ], direction );
+ }
samples++;
}
color[ 0 ] /= samples;
color[ 1 ] /= samples;
color[ 2 ] /= samples;
-
+
/* add to color */
VectorCopy( color, lightLuxel );
lightLuxel[ 3 ] += 1.0f;
+
+ if(lightDeluxel)
+ {
+ direction[ 0 ] /= samples;
+ direction[ 1 ] /= samples;
+ direction[ 2 ] /= samples;
+ VectorCopy( direction, lightDeluxel );
+ }
}
}
-static void RandomSubsampleRawLuxel( rawLightmap_t *lm, trace_t *trace, vec3_t sampleOrigin, int x, int y, float bias, float *lightLuxel )
+static void RandomSubsampleRawLuxel( rawLightmap_t *lm, trace_t *trace, vec3_t sampleOrigin, int x, int y, float bias, float *lightLuxel, float *lightDeluxel )
{
- int b, samples, mapped;
+ int b, mapped;
int cluster;
vec3_t origin, normal;
- vec3_t total;
+ vec3_t total, totaldirection;
VectorClear( total );
mapped = 0;
LightContributionToSample( trace );
VectorAdd( total, trace->color, total );
+ if(lightDeluxel)
+ {
+ VectorAdd( totaldirection, trace->directionContribution, totaldirection );
+ }
}
/* add to luxel */
lightLuxel[ 0 ] = total[ 0 ] / mapped;
lightLuxel[ 1 ] = total[ 1 ] / mapped;
lightLuxel[ 2 ] = total[ 2 ] / mapped;
+
+ if(lightDeluxel)
+ {
+ lightDeluxel[ 0 ] = totaldirection[ 0 ] / mapped;
+ lightDeluxel[ 1 ] = totaldirection[ 1 ] / mapped;
+ lightDeluxel[ 2 ] = totaldirection[ 2 ] / mapped;
+ }
}
}
#define STACK_LL_SIZE (SUPER_LUXEL_SIZE * 64 * 64)
#define LIGHT_LUXEL( x, y ) (lightLuxels + ((((y) * lm->sw) + (x)) * SUPER_LUXEL_SIZE))
+#define LIGHT_DELUXEL( x, y ) (lightDeluxels + ((((y) * lm->sw) + (x)) * SUPER_DELUXEL_SIZE))
void IlluminateRawLightmap( int rawLightmapNum )
{
- int i, t, x, y, sx, sy, size, llSize, luxelFilterRadius, lightmapNum;
+ int i, t, x, y, sx, sy, size, llSize, ldSize, luxelFilterRadius, lightmapNum;
int *cluster, *cluster2, mapped, lighted, totalLighted;
rawLightmap_t *lm;
surfaceInfo_t *info;
float brightness;
float *origin, *normal, *dirt, *luxel, *luxel2, *deluxel, *deluxel2;
unsigned char *flag;
- float *lightLuxels, *lightLuxel, samples, filterRadius, weight;
- vec3_t color, averageColor, averageDir, total, temp, temp2;
+ float *lightLuxels, *lightDeluxels, *lightLuxel, *lightDeluxel, samples, filterRadius, weight;
+ vec3_t color, direction, averageColor, averageDir, total, temp, temp2;
float tests[ 4 ][ 2 ] = { { 0.0f, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } };
trace_t trace;
float stackLightLuxels[ STACK_LL_SIZE ];
{
/* allocate temporary per-light luxel storage */
llSize = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float );
+ ldSize = lm->sw * lm->sh * SUPER_DELUXEL_SIZE * sizeof( float );
if( llSize <= (STACK_LL_SIZE * sizeof( float )) )
lightLuxels = stackLightLuxels;
else
lightLuxels = safe_malloc( llSize );
+ if(deluxemap)
+ lightDeluxels = safe_malloc( ldSize );
+ else
+ lightDeluxels = NULL;
/* clear luxels */
//% memset( lm->superLuxels[ 0 ], 0, llSize );
/* setup */
memset( lightLuxels, 0, llSize );
+ if(deluxemap)
+ memset( lightDeluxels, 0, ldSize );
totalLighted = 0;
/* determine filter radius */
/* get particulars */
lightLuxel = LIGHT_LUXEL( x, y );
- deluxel = SUPER_DELUXEL( x, y );
+ lightDeluxel = LIGHT_DELUXEL( x, y );
origin = SUPER_ORIGIN( x, y );
normal = SUPER_NORMAL( x, y );
flag = SUPER_FLAG( x, y );
/* add the contribution to the deluxemap */
if( deluxemap )
- VectorAdd( deluxel, trace.directionContribution, deluxel );
+ {
+ VectorCopy( trace.directionContribution, lightDeluxel );
+ }
/* check for evilness */
if(trace.forceSubsampling > 1.0f && (lightSamples > 1 || lightRandomSamples) && luxelFilterRadius == 0)
if(*flag & FLAG_ALREADY_SUBSAMPLED) // already subsampled
continue;
lightLuxel = LIGHT_LUXEL( sx, sy );
+ lightDeluxel = LIGHT_DELUXEL( sx, sy );
origin = SUPER_ORIGIN( sx, sy );
/* only subsample shadowed luxels */
/* subsample it */
if(lightRandomSamples)
- RandomSubsampleRawLuxel( lm, &trace, origin, sx, sy, 0.5f, lightLuxel );
+ RandomSubsampleRawLuxel( lm, &trace, origin, sx, sy, 0.5f, lightLuxel, deluxemap ? lightDeluxel : NULL );
else
- SubsampleRawLuxel_r( lm, &trace, origin, sx, sy, 0.25f * lightSamplesSearchBoxSize, lightLuxel );
+ SubsampleRawLuxel_r( lm, &trace, origin, sx, sy, 0.25f * lightSamplesSearchBoxSize, lightLuxel, deluxemap ? lightDeluxel : NULL );
*flag |= FLAG_ALREADY_SUBSAMPLED;
{
/* setup */
VectorClear( averageColor );
+ VectorClear( averageDir );
samples = 0.0f;
/* cheaper distance-based filtering */
if( *cluster < 0 )
continue;
lightLuxel = LIGHT_LUXEL( sx, sy );
+ lightDeluxel = LIGHT_DELUXEL( sx, sy );
/* create weight */
weight = (abs( sx - x ) == luxelFilterRadius ? 0.5f : 1.0f);
/* scale luxel by filter weight */
VectorScale( lightLuxel, weight, color );
VectorAdd( averageColor, color, averageColor );
+ if(deluxemap)
+ {
+ VectorScale( lightDeluxel, weight, direction );
+ VectorAdd( averageDir, direction, averageDir );
+ }
samples += weight;
}
}
luxel[ 1 ] += averageColor[ 1 ] / samples;
luxel[ 2 ] += averageColor[ 2 ] / samples;
}
+
+ if(deluxemap)
+ {
+ /* scale into luxel */
+ deluxel = SUPER_DELUXEL( x, y );
+ deluxel[ 0 ] += averageDir[ 0 ] / samples;
+ deluxel[ 1 ] += averageDir[ 1 ] / samples;
+ deluxel[ 2 ] += averageDir[ 2 ] / samples;
+ }
}
/* single sample */
{
/* get particulars */
lightLuxel = LIGHT_LUXEL( x, y );
+ lightDeluxel = LIGHT_DELUXEL( x, y );
luxel = SUPER_LUXEL( lightmapNum, x, y );
+ deluxel = SUPER_DELUXEL( x, y );
/* handle negative light */
if( trace.light->flags & LIGHT_NEGATIVE )
/* handle normal light */
else
VectorAdd( luxel, lightLuxel, luxel );
+
+ if(deluxemap)
+ {
+ VectorAdd( deluxel, lightDeluxel, deluxel );
+ }
}
}
}
/* free temporary luxels */
if( lightLuxels != stackLightLuxels )
free( lightLuxels );
+
+ if(deluxemap)
+ free( lightDeluxels );
}
/* free light list */