From ba44b0c0d5a1d4476680960d95bbe070c3eca38b Mon Sep 17 00:00:00 2001
From: pwarszawski
Date: Sat, 14 Nov 2026 14:30:51 +0100
Subject: [PATCH] Draw the glare of a FreeSpotLight after its point
The glare of a light point is drawn without the depth test, conditioned
on the occlusion query of the point. That query was issued only after
the glare and is kept on the TSubModel, which every instance of a model
shares, so the glare of one lamp used what the query saw for whichever
instance drew that submodel last (earlier in the frame or in the last
frame): glares showed through walls and blinked while the camera moved.
The point (with its query) is now drawn first and the glare after it,
so the conditional render of the opengl renderer uses the query of this
very point of this instance in this frame. The glare is drawn only when
the point was, which already held, as both need the viewer inside the
visibility cone. The vulkan renderer keeps using the results of the
last frame.
---
rendering/common/scenewalk.cpp | 83 +++++++++++++++++-----------------
rendering/common/scenewalk.h | 3 +-
2 files changed, 43 insertions(+), 43 deletions(-)
diff --git a/rendering/common/scenewalk.cpp b/rendering/common/scenewalk.cpp
index 63aa3ce47..ced5318af 100644
--- a/rendering/common/scenewalk.cpp
+++ b/rendering/common/scenewalk.cpp
@@ -3218,48 +3218,8 @@ void scene_walk::Render_Alpha(TSubModel *Submodel)
// a point light (FalloffAngle 360) is seen all around
auto const pointlight{Submodel->fCosFalloffAngle <= -1.f};
- if (Global.fLuminance < Submodel->fLight || Global.Overcast > 1.0f)
- {
- if (pointlight || (Submodel->fCosViewAngle > Submodel->fCosFalloffAngle))
- {
- // only bother if the viewer is inside the visibility cone
- // luminosity at night is at level of ~0.1, so the overall resulting transparency in clear conditions is ~0.5 at full 'brightness'
- auto glarelevel{std::clamp(std::max(0.6f - Global.fLuminance, // reduce the glare in bright daylight
- Global.Overcast - 1.f), // ensure some glare in rainy/foggy conditions
- 0.f, 1.f)};
- // view angle attenuation
- float const anglefactor{pointlight ? 1.f : std::clamp((Submodel->fCosViewAngle - Submodel->fCosFalloffAngle) / (Submodel->fCosHotspotAngle - Submodel->fCosFalloffAngle), 0.f, 1.f)};
-
- glarelevel *= anglefactor;
-
- if (glarelevel > 0.0f)
- {
- walk_glare(true);
-
- ::glPushMatrix();
- ::glLoadIdentity(); // macierz jedynkowa
- ::glTranslatef(lightcenter.x, lightcenter.y, lightcenter.z); // początek układu zostaje bez zmian
- ::glRotated(std::atan2(lightcenter.x, lightcenter.z) * 180.0 / M_PI, 0.0, 1.0, 0.0); // jedynie obracamy w pionie o kąt
-
- auto const lightcolor = glm::vec3(Submodel->DiffuseOverride.r < 0.f ? // -1 indicates no override
- Submodel->f4Diffuse :
- Submodel->DiffuseOverride);
-
- walk_program( walk_shader::billboard );
- walk_texture(0, m_glaretexture);
- model_ubs.param[0] = glm::vec4(glm::vec3(lightcolor), Submodel->fVisible * glarelevel);
-
- // main draw call
- // drawn where the light itself was seen in the last frame (an occlusion query of the opengl renderer)
- walk_glare_draw(Submodel, m_billboardgeometry);
-
- walk_glare(false);
-
- ::glPopMatrix();
- }
- }
- }
-
+ // the glare is drawn after the point, where the occlusion query of this very point (of this instance, in this frame) saw it
+ auto pointdrawn{false};
if (pointlight || (Submodel->fCosViewAngle > Submodel->fCosFalloffAngle))
{
// kąt większy niż maksymalny stożek swiatła
@@ -3316,11 +3276,50 @@ void scene_walk::Render_Alpha(TSubModel *Submodel)
walk_draw(Submodel->m_geometry.handle);
walk_light_query(Submodel, false);
+ pointdrawn = true;
// post-draw reset
model_ubs.emission = 0.0f;
model_ubs.fog_density = 1.0f / m_fogrange;
+ ::glPopMatrix();
+ }
+ }
+ // the point is drawn only inside its visibility cone, so the glare is too
+ if (pointdrawn && (Global.fLuminance < Submodel->fLight || Global.Overcast > 1.0f))
+ {
+ // luminosity at night is at level of ~0.1, so the overall resulting transparency in clear conditions is ~0.5 at full 'brightness'
+ auto glarelevel{std::clamp(std::max(0.6f - Global.fLuminance, // reduce the glare in bright daylight
+ Global.Overcast - 1.f), // ensure some glare in rainy/foggy conditions
+ 0.f, 1.f)};
+ // view angle attenuation
+ float const anglefactor{pointlight ? 1.f : std::clamp((Submodel->fCosViewAngle - Submodel->fCosFalloffAngle) / (Submodel->fCosHotspotAngle - Submodel->fCosFalloffAngle), 0.f, 1.f)};
+
+ glarelevel *= anglefactor;
+
+ if (glarelevel > 0.0f)
+ {
+ walk_glare(true);
+
+ ::glPushMatrix();
+ ::glLoadIdentity(); // macierz jedynkowa
+ ::glTranslatef(lightcenter.x, lightcenter.y, lightcenter.z); // początek układu zostaje bez zmian
+ ::glRotated(std::atan2(lightcenter.x, lightcenter.z) * 180.0 / M_PI, 0.0, 1.0, 0.0); // jedynie obracamy w pionie o kąt
+
+ auto const lightcolor = glm::vec3(Submodel->DiffuseOverride.r < 0.f ? // -1 indicates no override
+ Submodel->f4Diffuse :
+ Submodel->DiffuseOverride);
+
+ walk_program( walk_shader::billboard );
+ walk_texture(0, m_glaretexture);
+ model_ubs.param[0] = glm::vec4(glm::vec3(lightcolor), Submodel->fVisible * glarelevel);
+
+ // main draw call
+ // drawn where the point of the light was seen just above (an occlusion query of the opengl renderer)
+ walk_glare_draw(Submodel, m_billboardgeometry);
+
+ walk_glare(false);
+
::glPopMatrix();
}
}
diff --git a/rendering/common/scenewalk.h b/rendering/common/scenewalk.h
index 91a708c8d..ac22f7d99 100644
--- a/rendering/common/scenewalk.h
+++ b/rendering/common/scenewalk.h
@@ -266,7 +266,8 @@ class scene_walk
virtual void walk_timbers_expand(std::vector const &Origins, std::vector const &Ranges) {}
virtual void walk_cull_face(bool const Enabled) = 0;
// the glare of a lamp: drawn without the depth test, added to what is there; drawn only where the point of the light was
- // seen (walk_light_query() of the last frame, when the backend has it)
+ // seen (walk_light_query() of this point drawn just before it, when the backend has it: the opengl renderer uses
+ // the query at once, the vulkan renderer the result of the last frame)
virtual void walk_glare(bool const Enabled) = 0;
virtual void walk_glare_draw(TSubModel *Submodel, gfx::geometry_handle const &Geometry) = 0;
virtual void walk_light_query(TSubModel *Submodel, bool const Begin) = 0;