Skip to content

Commit c7aba11

Browse files
committed
lights: Fix directional lights and add debug cvars to add a fake sun
Directional lights should affect all light tiles, so automatically add them to every tile since we don't populate the radius. Also add some cvars to allow adding a debug sun.
1 parent 421455d commit c7aba11

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

src/engine/renderer/glsl_source/lighttile_fp.glsl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ void main() {
130130
or use compute shaders with atomics so we can have a variable amount of lights for each tile. */
131131
for( uint i = uint( u_lightLayer ); i < uint( u_numLights ); i += uint( NUM_LIGHT_LAYERS ) ) {
132132
Light l = GetLight( i );
133+
// Directional lights (sun) have infinite extent, so always include them in tiles
134+
if ( l.type == 2 ) {
135+
pushIdxs( ( i / uint( NUM_LIGHT_LAYERS ) ) + 1u, lightCount, idxs );
136+
lightCount++;
137+
if( lightCount == lightsPerLayer ) {
138+
break;
139+
}
140+
continue;
141+
}
142+
133143
vec3 center = ( u_ModelMatrix * vec4( l.center, 1.0 ) ).xyz;
134144
float radius = max( 2.0 * l.radius, 2.0 * 32.0 ); // Avoid artifacts with weak light sources
135145

src/engine/renderer/tr_scene.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,39 @@ static void AddDebugProjectedLight()
522522
light->origin[ 2 ] = r_debugProjLightOriginZ.Get();
523523
}
524524

525+
// Debug sun light (directional) injection
526+
Cvar::Cvar<bool> r_debugSun( "r_debugSun", "inject a directional sun light each frame", Cvar::NONE, false );
527+
Cvar::Range<Cvar::Cvar<float>> r_debugSunYaw( "r_debugSunYaw", "debug sun yaw in degrees", Cvar::NONE, 45.0f, -360.0f, 360.0f );
528+
Cvar::Range<Cvar::Cvar<float>> r_debugSunPitch( "r_debugSunPitch", "debug sun pitch in degrees", Cvar::NONE, -60.0f, -89.0f, 89.0f );
529+
Cvar::Range<Cvar::Cvar<float>> r_debugSunIntensity( "r_debugSunIntensity", "debug sun intensity (scale)", Cvar::NONE, 1.0f, 0.0f, 100.0f );
530+
Cvar::Range<Cvar::Cvar<float>> r_debugSunR( "r_debugSunR", "debug sun color R", Cvar::NONE, 1.0f, 0.0f, 10.0f );
531+
Cvar::Range<Cvar::Cvar<float>> r_debugSunG( "r_debugSunG", "debug sun color G", Cvar::NONE, 1.0f, 0.0f, 10.0f );
532+
Cvar::Range<Cvar::Cvar<float>> r_debugSunB( "r_debugSunB", "debug sun color B", Cvar::NONE, 1.0f, 0.0f, 10.0f );
533+
534+
static void AddDebugSunLight()
535+
{
536+
if ( r_numLights + 1 >= MAX_REF_LIGHTS )
537+
{
538+
return;
539+
}
540+
refLight_t *sun = &backEndData[ tr.smpFrame ]->lights[ r_numLights++ ];
541+
*sun = {};
542+
sun->rlType = refLightType_t::RL_DIRECTIONAL;
543+
// Compute direction from yaw/pitch cvars (in degrees)
544+
float yaw = DEG2RAD( r_debugSunYaw.Get() );
545+
float pitch = DEG2RAD( r_debugSunPitch.Get() );
546+
// Right-handed: X forward, Y left, Z up. Direction vector components:
547+
sun->projTarget[ 0 ] = cosf( pitch ) * cosf( yaw );
548+
sun->projTarget[ 1 ] = cosf( pitch ) * sinf( yaw );
549+
sun->projTarget[ 2 ] = sinf( pitch );
550+
VectorNormalize( sun->projTarget );
551+
// Color and intensity
552+
sun->color[ 0 ] = r_debugSunR.Get();
553+
sun->color[ 1 ] = r_debugSunG.Get();
554+
sun->color[ 2 ] = r_debugSunB.Get();
555+
sun->scale = r_debugSunIntensity.Get();
556+
}
557+
525558
/*
526559
@@@@@@@@@@@@@@@@@@@@@
527560
RE_RenderScene
@@ -601,6 +634,10 @@ void RE_RenderScene( const refdef_t *fd )
601634
{
602635
AddDebugProjectedLight();
603636
}
637+
if ( r_debugSun.Get() )
638+
{
639+
AddDebugSunLight();
640+
}
604641

605642
// derived info
606643
if ( r_forceRendererTime.Get() >= 0 ) {

0 commit comments

Comments
 (0)