summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
Diffstat (limited to 'gfx')
-rw-r--r--gfx/gl/sceneRenderer.cpp2
-rw-r--r--gfx/gl/shaders/shadowDynamicPoint.vs1
-rw-r--r--gfx/gl/shaders/shadowFixedPoint.vs1
-rw-r--r--gfx/gl/shadowMapper.cpp36
-rw-r--r--gfx/gl/shadowMapper.h3
5 files changed, 34 insertions, 9 deletions
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 <glm/gtc/type_ptr.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/matrix.hpp>
@@ -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<float>::infinity()};
+ static constexpr glm::vec4 border {std::numeric_limits<float>::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 <glm/vec2.hpp>
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: