From cd4f2dce3994bee30faae3e3af88ab489fc7959c Mon Sep 17 00:00:00 2001 From: Joakim Kilby Date: Tue, 17 Oct 2023 10:11:42 +0200 Subject: [PATCH] support multiple shadowers (up to 10) --- .../shaders/globalrenderer_vs.glsl | 3 ++- .../shaders/localrenderer_vs.glsl | 9 +++++--- .../globebrowsing/shaders/renderer_fs.glsl | 21 ++++++++++++------- modules/globebrowsing/src/renderableglobe.cpp | 18 ++++++++++------ 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/modules/globebrowsing/shaders/globalrenderer_vs.glsl b/modules/globebrowsing/shaders/globalrenderer_vs.glsl index f1aed26245..3a932024aa 100644 --- a/modules/globebrowsing/shaders/globalrenderer_vs.glsl +++ b/modules/globebrowsing/shaders/globalrenderer_vs.glsl @@ -100,7 +100,8 @@ vec3 getLevelWeights(float distToVertexOnEllipsoid) { ); } -out vec4 position_lightspace; +const int n_depthmaps = 10; +out vec4 positions_lightspace[n_depthmaps]; void main() { PositionNormalPair pair = globalInterpolation(in_uv); diff --git a/modules/globebrowsing/shaders/localrenderer_vs.glsl b/modules/globebrowsing/shaders/localrenderer_vs.glsl index 4898c8eb81..b9bbc4ca7c 100644 --- a/modules/globebrowsing/shaders/localrenderer_vs.glsl +++ b/modules/globebrowsing/shaders/localrenderer_vs.glsl @@ -67,9 +67,10 @@ uniform float chunkMinHeight; uniform float distanceScaleFactor; uniform int chunkLevel; +const int n_depthmaps = 10; uniform dmat4 inv_vp; -uniform dmat4 light_vp; -out vec4 position_lightspace; +uniform dmat4 light_vps[n_depthmaps]; +out vec4 positions_lightspace[n_depthmaps]; vec3 bilinearInterpolation(vec2 uv) { @@ -131,5 +132,7 @@ void main() { shadowCoords = vec4(shadowMatrix * dvec4(p, 1.0)); #endif // SHADOW_MAPPING_ENABLED - position_lightspace = vec4(light_vp * (inv_vp * dvec4(p, 1.0))); + for (int idx = 0; idx < n_depthmaps; ++idx) { + positions_lightspace[idx] = vec4(light_vps[idx] * (inv_vp * dvec4(p, 1.0))); + } } diff --git a/modules/globebrowsing/shaders/renderer_fs.glsl b/modules/globebrowsing/shaders/renderer_fs.glsl index a944fc69fa..7b7882bde3 100644 --- a/modules/globebrowsing/shaders/renderer_fs.glsl +++ b/modules/globebrowsing/shaders/renderer_fs.glsl @@ -154,8 +154,9 @@ in vec3 positionWorldSpace; uniform float opacity; -in vec4 position_lightspace; -uniform sampler2D light_depth_map; +const int n_depthmaps = 10; +in vec4 positions_lightspace[n_depthmaps]; +uniform sampler2D light_depth_maps[n_depthmaps]; Fragment getFragment() { Fragment frag; @@ -294,12 +295,18 @@ Fragment getFragment() { frag.color.xyz *= shadow < 0.99 ? clamp(shadow + 0.3, 0.0, 1.0) : shadow; #endif - vec3 coords = position_lightspace.xyz / position_lightspace.w; - coords = coords * 0.5 + 0.5; - float sampled_depth = texture(light_depth_map, coords.xy).r; - float current_depth = coords.z; + bool shadowed = false; + for (int idx = 0; idx < n_depthmaps; ++idx) { + vec3 coords = positions_lightspace[idx].xyz / positions_lightspace[idx].w; + coords = coords * 0.5 + 0.5; + float sampled_depth = texture(light_depth_maps[idx], coords.xy).r; + float current_depth = coords.z; + if (current_depth > sampled_depth) { + shadowed = true; + } + } - if (current_depth > sampled_depth) { + if (shadowed) { frag.color.xyz *= 0.1; } diff --git a/modules/globebrowsing/src/renderableglobe.cpp b/modules/globebrowsing/src/renderableglobe.cpp index 6d727fe2c6..6306f61414 100644 --- a/modules/globebrowsing/src/renderableglobe.cpp +++ b/modules/globebrowsing/src/renderableglobe.cpp @@ -1643,16 +1643,22 @@ void RenderableGlobe::renderChunkLocally(const Chunk& chunk, const RenderData& d program.setUniform("shadowMapTexture", shadowMapUnit); } - if (depthMapData.size() > 0) { - _localRenderer.program->setUniform("light_vp", depthMapData[0].viewProjecion); - _localRenderer.program->setUniform("inv_vp", glm::inverse(data.camera.combinedViewMatrix())); - ghoul::opengl::TextureUnit depthmapUnit; - depthmapUnit.activate(); + std::vector light_vps; + std::vector depthmaps; + for (const RenderableModel::DepthMapData& data : depthMapData) { + light_vps.push_back(data.viewProjecion); + ghoul::opengl::TextureUnit unit; + depthmaps.push_back(unit); + unit.activate(); glBindTexture(GL_TEXTURE_2D, depthMapData[0].depthMap); - _localRenderer.program->setUniform("light_depth_map", depthmapUnit); } + _localRenderer.program->setUniform("inv_vp", glm::inverse(data.camera.combinedViewMatrix())); + _localRenderer.program->setUniform("light_depth_maps", depthmaps); + GLint loc = glGetUniformLocation(*_localRenderer.program, "light_vps"); + glUniformMatrix4dv(loc, light_vps.size(), GL_FALSE, glm::value_ptr(light_vps.front())); + glEnable(GL_DEPTH_TEST); if (!renderGeomOnly) { glEnable(GL_CULL_FACE);