summaryrefslogtreecommitdiff
path: root/gfx/gl/camera.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-12-29 00:08:10 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2022-12-29 00:08:10 +0000
commitc8cd95cc043e2f069e6d6ef8406007987b46f484 (patch)
treefe2a411ec4c5fd25d7c943911f79d0dd03a187d7 /gfx/gl/camera.cpp
parentAdd helper to produce cartesian product of two arrays (diff)
downloadilt-c8cd95cc043e2f069e6d6ef8406007987b46f484.tar.bz2
ilt-c8cd95cc043e2f069e6d6ef8406007987b46f484.tar.xz
ilt-c8cd95cc043e2f069e6d6ef8406007987b46f484.zip
Tidy Camera::extentsAtDist
Diffstat (limited to 'gfx/gl/camera.cpp')
-rw-r--r--gfx/gl/camera.cpp36
1 files changed, 15 insertions, 21 deletions
diff --git a/gfx/gl/camera.cpp b/gfx/gl/camera.cpp
index 5ae5fb0..b52eca7 100644
--- a/gfx/gl/camera.cpp
+++ b/gfx/gl/camera.cpp
@@ -1,4 +1,5 @@
#include "camera.h"
+#include <collections.hpp>
#include <glm/gtx/intersect.hpp> // IWYU pragma: keep
#include <glm/gtx/transform.hpp> // IWYU pragma: keep
#include <maths.h>
@@ -36,29 +37,22 @@ 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 clampToSeaFloor = [this](const glm::vec3 & target) {
+ if (target.z < -1.5f) {
+ 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)) {
+ return vec * outdist + position;
+ }
}
+ return target;
};
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 {};
- auto outitr = out.begin();
- for (auto x : extents) {
- for (auto y : extents) {
- const glm::vec4 in {x, y, depth, 1.f};
-
- const auto out = inverseViewProjection * in;
- *outitr = out / out.w;
- if (outitr->z < -1.5f) {
- adjustToSeafloor(*outitr);
- }
- outitr++;
- }
- }
- return out;
+ static constexpr const auto cartesianExtents = extents * extents;
+ return cartesianExtents * [&depth, this, &clampToSeaFloor](const auto & extent) {
+ const glm::vec4 in {extent.first, extent.second, depth, 1.f};
+ return clampToSeaFloor(perspective_divide(inverseViewProjection * in));
+ };
}