summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-12-28 14:04:01 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2022-12-28 14:04:06 +0000
commit35f73ba19deb13ff1971983bc9c21fe342c91214 (patch)
treec085093b87e42ed497b42575747da890b09c5078
parentUse the existence of .substr(...) to test if a T is stringlike (diff)
downloadilt-35f73ba19deb13ff1971983bc9c21fe342c91214.tar.bz2
ilt-35f73ba19deb13ff1971983bc9c21fe342c91214.tar.xz
ilt-35f73ba19deb13ff1971983bc9c21fe342c91214.zip
Reduce camera extents to the point they cross the sea floor boundary
-rw-r--r--gfx/gl/camera.cpp15
1 files changed, 14 insertions, 1 deletions
diff --git a/gfx/gl/camera.cpp b/gfx/gl/camera.cpp
index ff706e7..5ae5fb0 100644
--- a/gfx/gl/camera.cpp
+++ b/gfx/gl/camera.cpp
@@ -1,4 +1,5 @@
#include "camera.h"
+#include <glm/gtx/intersect.hpp> // IWYU pragma: keep
#include <glm/gtx/transform.hpp> // IWYU pragma: keep
#include <maths.h>
#include <ray.hpp>
@@ -35,6 +36,14 @@ Camera::upFromForward(const glm::vec3 & forward)
std::array<glm::vec3, 4>
Camera::extentsAtDist(const float dist) const
{
+ const auto adjustToSeafloor = [this](glm::vec3 & target) {
+ const auto vec = glm::normalize(target - position);
+ constexpr glm::vec3 seafloor {0, 0, -1.5};
+ float outdist;
+ if (glm::intersectRayPlane(position, vec, seafloor, ::up, outdist)) {
+ target = vec * outdist + position;
+ }
+ };
const auto depth = -(2.f * (dist - near) * far) / (dist * (near - far)) - 1.f;
static constexpr const std::array extents {-1.F, 1.F};
std::array<glm::vec3, 4> out {};
@@ -44,7 +53,11 @@ Camera::extentsAtDist(const float dist) const
const glm::vec4 in {x, y, depth, 1.f};
const auto out = inverseViewProjection * in;
- *outitr++ = out / out.w;
+ *outitr = out / out.w;
+ if (outitr->z < -1.5f) {
+ adjustToSeafloor(*outitr);
+ }
+ outitr++;
}
}
return out;