Skip to content

Commit 4fb5b96

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 7cd603a commit 4fb5b96

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,40 @@ static void AddDebugProjectedLight()
542542
light->origin[ 2 ] = r_debugProjLightOriginZ.Get();
543543
}
544544

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

625663
// derived info
626664
if ( r_forceRendererTime.Get() >= 0 ) {

0 commit comments

Comments
 (0)