@@ -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
305276static 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 ];
0 commit comments