Skip to content

Commit c94abc6

Browse files
committed
lights: Fix directional lights and add debug cvars to add a fake sun
Directional lights should affect all light tiles, so set a really high radius to ensure they are included in all light tiles. Also add some cvars to allow adding a debug sun.
1 parent 27c9062 commit c94abc6

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/engine/renderer/glsl_source/lighttile_fp.glsl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ 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+
133134
vec3 center = ( u_ModelMatrix * vec4( l.center, 1.0 ) ).xyz;
134135
float radius = max( 2.0 * l.radius, 2.0 * 32.0 ); // Avoid artifacts with weak light sources
135136

src/engine/renderer/tr_scene.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,38 @@ static void AddDebugProjectedLight()
540540
light->origin[ 2 ] = r_debugProjLightOriginZ.Get();
541541
}
542542

543+
// Debug sun light (directional) injection
544+
Cvar::Cvar<bool> r_debugSun( "r_debugSun", "inject a directional sun light each frame", Cvar::CHEAT, false );
545+
Cvar::Range<Cvar::Cvar<float>> r_debugSunYaw( "r_debugSunYaw", "debug sun yaw in degrees", Cvar::NONE, 45.0f, -360.0f, 360.0f );
546+
Cvar::Range<Cvar::Cvar<float>> r_debugSunPitch( "r_debugSunPitch", "debug sun pitch in degrees", Cvar::NONE, -60.0f, -89.0f, 89.0f );
547+
Cvar::Range<Cvar::Cvar<float>> r_debugSunR( "r_debugSunR", "debug sun color R", Cvar::NONE, 1.0f, 0.0f, 10.0f );
548+
Cvar::Range<Cvar::Cvar<float>> r_debugSunG( "r_debugSunG", "debug sun color G", Cvar::NONE, 1.0f, 0.0f, 10.0f );
549+
Cvar::Range<Cvar::Cvar<float>> r_debugSunB( "r_debugSunB", "debug sun color B", Cvar::NONE, 1.0f, 0.0f, 10.0f );
550+
551+
static void AddDebugSunLight()
552+
{
553+
if ( r_numLights >= MAX_REF_LIGHTS )
554+
{
555+
return;
556+
}
557+
refLight_t *sun = &backEndData[ tr.smpFrame ]->lights[ r_numLights++ ];
558+
*sun = {};
559+
sun->rlType = refLightType_t::RL_DIRECTIONAL;
560+
// Compute direction from yaw/pitch cvars (in degrees)
561+
float yaw = DEG2RAD( r_debugSunYaw.Get() );
562+
float pitch = DEG2RAD( r_debugSunPitch.Get() );
563+
// Right-handed: X forward, Y left, Z up. Direction vector components:
564+
sun->projTarget[ 0 ] = cosf( pitch ) * cosf( yaw );
565+
sun->projTarget[ 1 ] = cosf( pitch ) * sinf( yaw );
566+
sun->projTarget[ 2 ] = sinf( pitch );
567+
VectorNormalize( sun->projTarget );
568+
sun->color[ 0 ] = r_debugSunR.Get();
569+
sun->color[ 1 ] = r_debugSunG.Get();
570+
sun->color[ 2 ] = r_debugSunB.Get();
571+
// Max radius to ensure it is always included.
572+
sun->radius = std::numeric_limits<float>::max();
573+
}
574+
543575
/*
544576
@@@@@@@@@@@@@@@@@@@@@
545577
RE_RenderScene
@@ -619,6 +651,10 @@ void RE_RenderScene( const refdef_t *fd )
619651
{
620652
AddDebugProjectedLight();
621653
}
654+
if ( r_debugSun.Get() )
655+
{
656+
AddDebugSunLight();
657+
}
622658

623659
// derived info
624660
if ( r_forceRendererTime.Get() >= 0 ) {

0 commit comments

Comments
 (0)