Skip to content

Commit 1d7cedf

Browse files
authored
utils: pxrad: added lightprobelevel parameter, cleaned up a bit (#244)
1 parent 6ab97c4 commit 1d7cedf

File tree

3 files changed

+40
-58
lines changed

3 files changed

+40
-58
lines changed

utils/pxrad/ambientcube.cpp

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -211,40 +211,11 @@ bool R_GetDirectLightFromSurface( dface_t *surf, const vec3_t point, lightpoint_
211211
}
212212

213213

214-
215-
//-----------------------------------------------------------------------------
216-
// Finds ambient sky lights
217-
//-----------------------------------------------------------------------------
218-
static dworldlight_t *FindAmbientSkyLight( void )
219-
{
220-
static dworldlight_t *s_pCachedSkylight = NULL;
221-
222-
// Don't keep searching for the same light.
223-
if( !s_pCachedSkylight )
224-
{
225-
// find any ambient lights
226-
for( int iLight = 0; iLight < g_numworldlights; iLight++ )
227-
{
228-
dworldlight_t *wl = &g_dworldlights[iLight];
229-
230-
if( wl->emittype == emit_skylight )
231-
{
232-
s_pCachedSkylight = wl;
233-
break;
234-
}
235-
}
236-
}
237-
238-
return s_pCachedSkylight;
239-
}
240-
241-
242-
243214
//-----------------------------------------------------------------------------
244215
// Computes ambient lighting along a specified ray.
245216
// Ray represents a cone, tanTheta is the tan of the inner cone angle
246217
//-----------------------------------------------------------------------------
247-
static void CalcRayAmbientLighting( int threadnum, const vec3_t vStart, const vec3_t vEnd, dworldlight_t *pSkyLight, float tanTheta, vec3_t radcolor )
218+
static void CalcRayAmbientLighting( int threadnum, const vec3_t vStart, const vec3_t vEnd, vec3_t radcolor )
248219
{
249220
lightpoint_t info;
250221
vec3_t vDelta;
@@ -291,7 +262,7 @@ static void CalcRayAmbientLighting( int threadnum, const vec3_t vStart, const ve
291262
//Msg( "solid angle %f\n", scaleAvg );
292263
if( scaleAvg <= 0.0f )
293264
return;
294-
scaleAvg = 4.0f * M_PI / ((float)NUMVERTEXNORMALS * scaleAvg); //ratio of ray cone and face solid angles
265+
scaleAvg = 4.0f * M_PI / ((float)g_numskynormals[g_lightprobelevel] * scaleAvg); //ratio of ray cone and face solid angles
295266
scaleAvg = bound( 0.0f, scaleAvg, 1.0f );
296267
for (int i = 0; i < MAXLIGHTMAPS; i++ )
297268
if( (info.styles[i] == LS_NORMAL)||(info.styles[i] == LS_SKY)||(info.styles[i] == g_skystyle)) //only sun, sky and default style
@@ -305,39 +276,38 @@ static void CalcRayAmbientLighting( int threadnum, const vec3_t vStart, const ve
305276
static void ComputeAmbientFromSphericalSamples( int threadnum, const vec3_t p1, vec3_t lightBoxColor[6] )
306277
{
307278
// Figure out the color that rays hit when shot out from this position.
308-
float tanTheta = tan( VERTEXNORMAL_CONE_INNER_ANGLE );
309-
dworldlight_t *pSkyLight = FindAmbientSkyLight();
310-
vec3_t radcolor[NUMVERTEXNORMALS], p2;
279+
vec3_t p2;
280+
vec_t weight_sum[6];
311281

312-
for( int i = 0; i < NUMVERTEXNORMALS; i++ )
282+
for ( int j = 0; j < 6; j++ )
313283
{
314-
VectorMA( p1, (65536.0f * 1.74f), g_anorms[i], p2 );
315-
316-
// Now that we've got a ray, see what surface we've hit
317-
CalcRayAmbientLighting( threadnum, p1, p2, pSkyLight, tanTheta, radcolor[i] );
284+
VectorClear( lightBoxColor[j] );
285+
weight_sum[j] = 0.0f;
318286
}
319287

320-
// accumulate samples into radiant box
321-
for ( int j = 0; j < 6; j++ )
288+
for( int i = 0; i < g_numskynormals[g_lightprobelevel]; i++ )
322289
{
323-
float t = 0.0f;
290+
VectorMA( p1, (65536.0f * 1.74f), g_skynormals[g_lightprobelevel][i], p2 );
324291

325-
VectorClear( lightBoxColor[j] );
292+
vec3_t temp_color;
293+
CalcRayAmbientLighting( threadnum, p1, p2, temp_color);
326294

327-
for( int i = 0; i < NUMVERTEXNORMALS; i++ )
295+
for ( int j = 0; j < 6; j++ )
328296
{
329-
float c = DotProduct( g_anorms[i], g_BoxDirections[j] );
297+
float c = DotProduct( g_skynormals[g_lightprobelevel][i], g_BoxDirections[j] ) ;
330298

331299
if( c > 0.0f )
332300
{
333-
VectorMA( lightBoxColor[j], c, radcolor[i], lightBoxColor[j] );
334-
t += c;
301+
VectorMA( lightBoxColor[j], c , temp_color, lightBoxColor[j] );
302+
weight_sum[j] += c;
335303
}
336304
}
337-
338-
VectorScale( lightBoxColor[j], ( 1.0 / t ), lightBoxColor[j] );
339305
}
340306

307+
for ( int j = 0; j < 6; j++ )
308+
if( weight_sum[j] > 0.0f )
309+
VectorScale( lightBoxColor[j], 1.0f / weight_sum[j], lightBoxColor[j] );
310+
341311
// Now add direct light from the emit_surface lights. These go in the ambient cube because
342312
// there are a ton of them and they are often so dim that they get filtered out by r_worldlightmin.
343313
AddEmitSurfaceLights( threadnum, p1, lightBoxColor );
@@ -535,20 +505,23 @@ void ComputeAmbientForLeaf( int threadnum, int leafID, ambientlocallist_t *list
535505
int ySize = (g_dleafs[leafID].maxs[1] - g_dleafs[leafID].mins[1]) / 64;
536506
int zSize = (g_dleafs[leafID].maxs[2] - g_dleafs[leafID].mins[2]) / 64;
537507

538-
xSize = Q_max( xSize, 1 );
539-
ySize = Q_max( ySize, 1 );
540-
zSize = Q_max( zSize, 1 );
541-
508+
int xMin = xSize > 1 ? 2 : 1;
509+
int yMin = ySize > 1 ? 2 : 1;
510+
int zMin = zSize > 1 ? 2 : 1;
511+
512+
xSize = Q_max( xSize, xMin );
513+
ySize = Q_max( ySize, yMin );
514+
zSize = Q_max( zSize, zMin );
542515

543516
xSize = Q_min( xSize, MAX_LOCAL_SAMPLES );
544517
ySize = Q_min( ySize, MAX_LOCAL_SAMPLES );
545518
zSize = Q_min( zSize, MAX_LOCAL_SAMPLES );
546519

547520
while( xSize * ySize * zSize > MAX_LOCAL_SAMPLES ) //lazy way
548521
{
549-
xSize = Q_max( xSize - 1, 1 );
550-
ySize = Q_max( ySize - 1, 1 );
551-
zSize = Q_max( zSize - 1, 1 );
522+
xSize = Q_max( xSize - 1, xMin );
523+
ySize = Q_max( ySize - 1, yMin );
524+
zSize = Q_max( zSize - 1, zMin );
552525
}
553526

554527
vec3_t cube[6];

utils/pxrad/qrad.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ bool g_studiolegacy = false;
7474
vec_t g_scale = DEFAULT_GLOBAL_SCALE;
7575
rgbdata_t *g_skytextures[6];
7676
vec_t g_lightprobeepsilon = DEFAULT_LIGHTPROBE_EPSILON;
77+
int g_lightprobelevel = 4;
7778
bool g_vertexblur = false;
7879
uint g_numstudiobounce = DEFAULT_STUDIO_BOUNCE;
7980
int g_studiogipasscounter = 0;
@@ -2757,6 +2758,7 @@ static void PrintRadUsage( void )
27572758
Msg( " -worldspace : deluxe map in world space, not tangent space\n" );
27582759
Msg( " -studiolegacy : use legacy tree for studio models tracing instead of BVH\n" );
27592760
Msg( " -lightprobeepsilon #.#: set light probe importance threshold value. default is %f\n", DEFAULT_LIGHTPROBE_EPSILON );
2761+
Msg( " -lightprobelevel #: set number of rays for light probes baking, final number is 4^n. default is 4\n" );
27602762
Msg( " -studiobounce #: set number of studio model radiosity bounces. default is %d\n", DEFAULT_STUDIO_BOUNCE );
27612763
Msg( " -vertexblur : blur per-vertex lighting\n" );
27622764
Msg( " -noemissive : do not add emissive textures to the lightmap\n" );
@@ -2912,7 +2914,12 @@ int main( int argc, char **argv )
29122914
{
29132915
g_lightprobeepsilon = (float)atof( argv[i+1] );
29142916
i++;
2915-
}
2917+
}
2918+
else if( !Q_strcmp( argv[i], "-lightprobelevel" ))
2919+
{
2920+
g_lightprobelevel = atoi( argv[i+1] );
2921+
i++;
2922+
}
29162923
else if( !Q_strcmp( argv[i], "-studiobounce" ))
29172924
{
29182925
g_numstudiobounce = atoi( argv[i+1] );
@@ -3077,7 +3084,8 @@ int main( int argc, char **argv )
30773084
// keep it in acceptable range
30783085
g_blur = bound( 1.0, g_blur, 8.0 );
30793086
g_gamma = bound( 0.3, g_gamma, 1.0 );
3080-
g_skystyle = bound( 0, g_skystyle, 254 );
3087+
g_skystyle = bound( 0, g_skystyle, 254 );
3088+
g_lightprobelevel = bound( 1, g_lightprobelevel, SKYLEVELMAX );
30813089

30823090
RadWorld ();
30833091

utils/pxrad/qrad.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ extern bool g_studiolegacy;
429429
extern vec_t g_scale;
430430
extern rgbdata_t *g_skytextures[6];
431431
extern vec_t g_lightprobeepsilon;
432+
extern int g_lightprobelevel;
432433
extern directlight_t *g_skylights[256];
433434
extern int g_numskylights;
434435
extern uint g_numstudiobounce;

0 commit comments

Comments
 (0)