From aaff59af171545cc16632881aefb19449a7c491a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 28 Dec 2022 19:29:53 +0000 Subject: Generate a single shadow band and region definition --- gfx/gl/sceneRenderer.cpp | 10 +++++----- gfx/gl/sceneRenderer.h | 3 ++- gfx/gl/shadowMapper.cpp | 46 +++++++++++++++++++++++++++++++++------------- gfx/gl/shadowMapper.h | 6 +++++- 4 files changed, 45 insertions(+), 20 deletions(-) (limited to 'gfx') diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp index 0fcf471..b13da35 100644 --- a/gfx/gl/sceneRenderer.cpp +++ b/gfx/gl/sceneRenderer.cpp @@ -119,7 +119,7 @@ SceneRenderer::setDirectionalLight( glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); glViewport(0, 0, size.x, size.y); dirLight.use(); - dirLight.setDirectionalLight(colour, direction, lvp); + dirLight.setDirectionalLight(colour, direction, lvp.projections, lvp.regions); glBindVertexArray(displayVAO); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glBindVertexArray(0); @@ -136,14 +136,14 @@ SceneRenderer::DirectionalLightProgram::DirectionalLightProgram() : } void -SceneRenderer::DirectionalLightProgram::setDirectionalLight( - const glm::vec3 & c, const glm::vec3 & d, const std::span lvp) const +SceneRenderer::DirectionalLightProgram::setDirectionalLight(const glm::vec3 & c, const glm::vec3 & d, + const std::span lvp, const std::span shadowMapRegions) const { - constexpr glm::vec4 shadowMapRegions {0.5F, 0.5F, 0.5F, 0.5F}; glUniform3fv(colourLoc, 1, glm::value_ptr(c)); const auto nd = glm::normalize(d); glUniform3fv(directionLoc, 1, glm::value_ptr(nd)); glUniform1ui(lightViewProjectionCountLoc, static_cast(lvp.size())); glUniformMatrix4fv(lightViewProjectionLoc, static_cast(lvp.size()), GL_FALSE, glm::value_ptr(lvp.front())); - glUniform4fv(lightViewShadowMapRegionLoc, 1, glm::value_ptr(shadowMapRegions)); + glUniform4fv(lightViewShadowMapRegionLoc, static_cast(shadowMapRegions.size()), + glm::value_ptr(shadowMapRegions.front())); } diff --git a/gfx/gl/sceneRenderer.h b/gfx/gl/sceneRenderer.h index a345b4b..28acc3b 100644 --- a/gfx/gl/sceneRenderer.h +++ b/gfx/gl/sceneRenderer.h @@ -35,7 +35,8 @@ private: DirectionalLightProgram(); using Program::use; - void setDirectionalLight(const glm::vec3 &, const glm::vec3 &, const std::span) const; + void setDirectionalLight(const glm::vec3 &, const glm::vec3 &, const std::span, + const std::span) const; private: RequiredUniformLocation directionLoc, colourLoc, lightViewProjectionLoc, lightViewProjectionCountLoc, diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp index 9c8a4c9..b41a53b 100644 --- a/gfx/gl/shadowMapper.cpp +++ b/gfx/gl/shadowMapper.cpp @@ -33,7 +33,17 @@ ShadowMapper::ShadowMapper(const glm::ivec2 & s) : size {s} glBindFramebuffer(GL_FRAMEBUFFER, 0); } -std::array +constexpr std::array viewports {{ + {31, 31, 0, 0}, +}}; +constexpr std::array shadowMapRegions { + {{0.5F, 0.5F, 0.5F, 0.5F}}, +}; +constexpr std::array shadowBands {1.F, 1000.F}; +static_assert(viewports.size() == shadowMapRegions.size()); +static_assert(shadowBands.size() == shadowMapRegions.size() + 1); + +ShadowMapper::Definitions<1> ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir, const Camera & camera) const { glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); @@ -42,29 +52,39 @@ ShadowMapper::update(const SceneProvider & scene, const glm::vec3 & dir, const C glEnable(GL_DEPTH_TEST); glCullFace(GL_FRONT); - auto viewExtents = camera.extentsAtDist(1) + camera.extentsAtDist(1000); - 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)); + auto bandViewExtents = shadowBands * [&camera](auto distance) { + return camera.extentsAtDist(distance); }; + const std::span viewExtents {bandViewExtents.front().begin(), bandViewExtents.back().end()}; const auto lightView = glm::lookAt(camera.getPosition(), camera.getPosition() + dir, up); for (auto & e : viewExtents) { e = lightView * glm::vec4(e, 1); } + Definitions<1> out; + for (std::size_t band = 0; band < viewports.size(); ++band) { + const auto extents_minmax = [extents = viewExtents.subspan(band * 4, 8)](auto && comp) { + const auto mm = std::minmax_element(extents.begin(), extents.end(), comp); + return std::make_pair(comp.get(*mm.first), comp.get(*mm.second)); + }; + + const auto lightProjection = [](const auto & x, const auto & y, const auto & z) { + return glm::ortho(x.first, x.second, y.first, y.second, -z.second, -z.first); + }(extents_minmax(CompareBy {0}), extents_minmax(CompareBy {1}), extents_minmax(CompareBy {2})); - const auto lightProjection = [](const auto & x, const auto & y, const auto & z) { - return glm::ortho(x.first, x.second, y.first, y.second, -z.second, -z.first); - }(extents_minmax(CompareBy {0}), extents_minmax(CompareBy {1}), extents_minmax(CompareBy {2})); + out.projections[band] = lightProjection * lightView; + fixedPoint.setViewProjection(out.projections[band]); + dynamicPoint.setViewProjection(out.projections[band]); + out.regions[band] = shadowMapRegions[band]; - const auto lightViewProjection = lightProjection * lightView; - fixedPoint.setViewProjection(lightViewProjection); - dynamicPoint.setViewProjection(lightViewProjection); + const auto & viewport = viewports[band]; + glViewport(size.x >> viewport.x, size.y >> viewport.y, size.x >> viewport.z, size.y >> viewport.w); + scene.shadows(*this); + }; - scene.shadows(*this); glCullFace(GL_BACK); - return {lightViewProjection}; + return out; } ShadowMapper::FixedPoint::FixedPoint() : Program {shadowFixedPoint_vs}, viewProjectionLoc {*this, "viewProjection"} { } diff --git a/gfx/gl/shadowMapper.h b/gfx/gl/shadowMapper.h index 2b9bc85..d5f091e 100644 --- a/gfx/gl/shadowMapper.h +++ b/gfx/gl/shadowMapper.h @@ -11,7 +11,11 @@ class ShadowMapper { public: ShadowMapper(const glm::ivec2 & size); - std::array update(const SceneProvider &, const glm::vec3 & direction, const Camera &) const; + template struct Definitions { + std::array projections; + std::array regions; + }; + Definitions<1> update(const SceneProvider &, const glm::vec3 & direction, const Camera &) const; class FixedPoint : public Program { public: -- cgit v1.2.3