From 35f73ba19deb13ff1971983bc9c21fe342c91214 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 28 Dec 2022 14:04:01 +0000 Subject: Reduce camera extents to the point they cross the sea floor boundary --- gfx/gl/camera.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'gfx/gl') 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 // IWYU pragma: keep #include // IWYU pragma: keep #include #include @@ -35,6 +36,14 @@ Camera::upFromForward(const glm::vec3 & forward) std::array 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 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; -- cgit v1.2.3