From 348a4cdc6ba1bbf43e1fd7c6217fd2b62a1d605c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 18 Dec 2022 14:23:18 +0000 Subject: First cut of view scope shadow mapper view --- gfx/gl/sceneRenderer.cpp | 2 +- gfx/gl/shaders/shadowDynamicPoint.vs | 1 + gfx/gl/shaders/shadowFixedPoint.vs | 1 + gfx/gl/shadowMapper.cpp | 36 +++++++++++++++++++++++++++++------- gfx/gl/shadowMapper.h | 3 ++- 5 files changed, 34 insertions(+), 9 deletions(-) (limited to 'gfx') diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index b3f2c47..1d7507a 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -115,7 +115,7 @@ SceneRenderer::setDirectionalLight( const glm::vec3 & colour, const glm::vec3 & direction, const SceneProvider & scene) const { if (colour.r > 0 || colour.g > 0 || colour.b > 0) { - const auto lvp = shadowMapper.update(scene, direction); + const auto lvp = shadowMapper.update(scene, direction, camera); glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); glViewport(0, 0, size.x, size.y); dirLight.use(); diff --git a/gfx/gl/shaders/shadowDynamicPoint.vs b/gfx/gl/shaders/shadowDynamicPoint.vs index 91d8bf7..6ea352f 100644 --- a/gfx/gl/shaders/shadowDynamicPoint.vs +++ b/gfx/gl/shaders/shadowDynamicPoint.vs @@ -9,4 +9,5 @@ void main() { gl_Position = viewProjection * model * vec4(position, 1.0); + gl_Position.z = max(gl_Position.z, -1); } diff --git a/gfx/gl/shaders/shadowFixedPoint.vs b/gfx/gl/shaders/shadowFixedPoint.vs index 75c04c4..246890c 100644 --- a/gfx/gl/shaders/shadowFixedPoint.vs +++ b/gfx/gl/shaders/shadowFixedPoint.vs @@ -8,4 +8,5 @@ void main() { gl_Position = viewProjection * vec4(position, 1.0); + gl_Position.z = max(gl_Position.z, -1); } diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index fd25dbe..b5f2ea5 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -1,11 +1,13 @@ #include "shadowMapper.h" +#include "camera.h" +#include "collections.hpp" #include "gfx/gl/shaders/vs-shadowDynamicPoint.h" #include "gfx/gl/shaders/vs-shadowFixedPoint.h" -#include "gfx/models/texture.h" #include "location.hpp" #include "maths.h" #include "sceneProvider.h" #include "sceneShader.h" +#include "sorting.hpp" #include #include #include @@ -18,7 +20,7 @@ ShadowMapper::ShadowMapper(const glm::ivec2 & s) : size {s} glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); - static constexpr glm::vec3 border {std::numeric_limits::infinity()}; + static constexpr glm::vec4 border {std::numeric_limits::infinity()}; glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, glm::value_ptr(border)); glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); @@ -32,12 +34,32 @@ ShadowMapper::ShadowMapper(const glm::ivec2 & s) : size {s} } glm::mat4x4 -ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir) const +ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir, const Camera & camera) const { - const glm::vec3 centre {0.F, 0.F, 0.F}; - const glm::vec3 range = glm::normalize(dir) * 1800.F; - const auto lightProjection = glm::ortho(-1200.F, 1200.F, -1200.F, 1200.F, 0.F, 3600.F); - const auto lightView = glm::lookAt(centre - range, centre, north); + constexpr auto BACK_OFF = 3600.f; + const glm::vec3 range = glm::normalize(dir) * BACK_OFF; + auto viewExtents = camera.extentsAtDist(1) + camera.extentsAtDist(1000); + for (auto & e : viewExtents) { + e = glm::round(e); + } + const auto extents_minmax = [&viewExtents](auto && comp) { + const auto mm = std::minmax_element(viewExtents.begin(), viewExtents.end(), comp); + return std::make_pair(comp.get(*mm.first), comp.get(*mm.second)); + }; + // Find camera view centre + const auto centre = [](const auto & x, const auto & y, const auto & z) { + return glm::vec3 {midpoint(x), midpoint(y), midpoint(z)}; + }(extents_minmax(CompareBy {0}), extents_minmax(CompareBy {1}), extents_minmax(CompareBy {2})); + + const auto lightView = glm::lookAt(centre, centre + range, up); + for (auto & e : viewExtents) { + e = lightView * glm::vec4(e, 1); + } + + const auto lightProjection = [](const auto & x, const auto & y, const auto & z) { + return glm::ortho(x.first, x.second, y.first, y.second, z.first, z.second); + }(extents_minmax(CompareBy {0}), extents_minmax(CompareBy {1}), extents_minmax(CompareBy {2})); + const auto lightViewProjection = lightProjection * lightView; fixedPoint.setViewProjection(lightViewProjection); dynamicPoint.setViewProjection(lightViewProjection); diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h index 6782d60..45a9536 100644 --- a/gfx/gl/shadowMapper.h +++ b/gfx/gl/shadowMapper.h @@ -5,12 +5,13 @@ #include class SceneProvider; +class Camera; class ShadowMapper { public: ShadowMapper(const glm::ivec2 & size); - glm::mat4x4 update(const SceneProvider &, const glm::vec3 & direction) const; + glm::mat4x4 update(const SceneProvider &, const glm::vec3 & direction, const Camera &) const; class FixedPoint : public Program { public: -- cgit v1.2.3