summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2023-12-11 20:11:23 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2023-12-11 20:16:22 +0000
commitdaed922f23daa8a8e2659d8aea7864cccd5e08c7 (patch)
treee8a1c949f3341f331408d91826038ab63346e32d /gfx
parentHelper for testing integer vector approximations (diff)
downloadilt-daed922f23daa8a8e2659d8aea7864cccd5e08c7.tar.bz2
ilt-daed922f23daa8a8e2659d8aea7864cccd5e08c7.tar.xz
ilt-daed922f23daa8a8e2659d8aea7864cccd5e08c7.zip
Integer camera
Diffstat (limited to 'gfx')
-rw-r--r--gfx/gl/camera.cpp36
-rw-r--r--gfx/gl/camera.h18
-rw-r--r--gfx/gl/sceneRenderer.cpp2
-rw-r--r--gfx/gl/shadowMapper.cpp16
4 files changed, 36 insertions, 36 deletions
diff --git a/gfx/gl/camera.cpp b/gfx/gl/camera.cpp
index 15f76c4..a0d76ab 100644
--- a/gfx/gl/camera.cpp
+++ b/gfx/gl/camera.cpp
@@ -1,14 +1,14 @@
#include "camera.h"
#include <collections.h>
-#include <glm/gtx/intersect.hpp> // IWYU pragma: keep
-#include <glm/gtx/transform.hpp> // IWYU pragma: keep
-#include <math.h>
+#include <glm/gtx/transform.hpp>
#include <maths.h>
#include <ray.h>
-Camera::Camera(Position3D pos, Angle fov, Angle aspect, Distance zNear, Distance zFar) :
+Camera::Camera(GlobalPosition3D pos, Angle fov, Angle aspect, GlobalDistance zNear, GlobalDistance zFar) :
position {pos}, forward {::north}, up {::up}, near {zNear}, far {zFar},
- projection {glm::perspective(fov, aspect, zNear, zFar)}, viewProjection {}, inverseViewProjection {}
+ projection {
+ glm::perspective(fov, aspect, static_cast<RelativeDistance>(zNear), static_cast<RelativeDistance>(zFar))},
+ viewProjection {}, inverseViewProjection {}
{
updateView();
}
@@ -25,7 +25,7 @@ void
Camera::updateView()
{
viewProjection = projection * glm::lookAt(origin, forward, up);
- inverseViewProjection = glm::inverse(projection * glm::lookAt(position, position + forward, up));
+ inverseViewProjection = glm::inverse(viewProjection);
}
Direction3D
@@ -35,21 +35,21 @@ Camera::upFromForward(const Direction3D & forward)
return glm::cross(forward, right);
}
-std::array<glm::vec4, 4>
-Camera::extentsAtDist(const float dist) const
+std::array<GlobalPosition4D, 4>
+Camera::extentsAtDist(const GlobalDistance dist) const
{
- const auto clampToSeaFloor = [this, dist](const Position3D & target) {
- if (target.z < -1.5F) {
- const auto vec = glm::normalize(target - position);
- constexpr Position3D seafloor {0, 0, -1.5F};
- float outdist {};
- if (glm::intersectRayPlane(position, vec, seafloor, ::up, outdist)) {
- return (vec * outdist + position) ^ outdist;
- }
+ const auto clampToSeaFloor = [this, dist](GlobalPosition3D target) -> GlobalPosition4D {
+ target += position;
+ if (target.z < -1500) {
+ const auto diff = (target - position);
+ const auto something = (-1500 - position.z) / diff.z;
+ return {position + diff * something, dist};
}
- return target ^ dist;
+ return {target, dist};
};
- const auto depth = -(2.F * (dist - near) * far) / (dist * (near - far)) - 1.F;
+ const auto depth = -(2.F * (static_cast<float>(dist - near)) * static_cast<float>(far))
+ / (static_cast<float>(dist) * (static_cast<float>(near - far)))
+ - 1.F;
static constexpr const std::array extents {-1.F, 1.F};
static constexpr const auto cartesianExtents = extents * extents;
return cartesianExtents * [&depth, this, &clampToSeaFloor](const auto & extent) {
diff --git a/gfx/gl/camera.h b/gfx/gl/camera.h
index 469df0d..eca7b8f 100644
--- a/gfx/gl/camera.h
+++ b/gfx/gl/camera.h
@@ -7,7 +7,7 @@
class Camera {
public:
- Camera(Position3D, Angle fov, Angle aspect, Distance zNear, Distance zFar);
+ Camera(GlobalPosition3D, Angle fov, Angle aspect, GlobalDistance zNear, GlobalDistance zFar);
[[nodiscard]] glm::mat4
getViewProjection() const
@@ -18,7 +18,7 @@ public:
[[nodiscard]] Ray unProject(const ScreenRelCoord &) const;
void
- setPosition(const Position3D & p)
+ setPosition(const GlobalPosition3D & p)
{
position = p;
updateView();
@@ -39,23 +39,23 @@ public:
}
void
- setView(const Position3D & p, const Direction3D & f)
+ setView(const GlobalPosition3D & p, const Direction3D & f)
{
position = p;
setForward(f);
}
void
- setView(const Position3D & p, const Direction3D & f, const Direction3D & u)
+ setView(const GlobalPosition3D & p, const Direction3D & f, const Direction3D & u)
{
position = p;
setView(f, u);
}
void
- lookAt(const Position3D & target)
+ lookAt(const GlobalPosition3D & target)
{
- setForward(glm::normalize(target - position));
+ setForward(glm::normalize(RelativePosition3D(target - position)));
}
[[nodiscard]] auto
@@ -70,18 +70,18 @@ public:
return position;
}
- [[nodiscard]] std::array<glm::vec4, 4> extentsAtDist(float) const;
+ [[nodiscard]] std::array<GlobalPosition4D, 4> extentsAtDist(GlobalDistance) const;
[[nodiscard]] static Direction3D upFromForward(const Direction3D & forward);
private:
void updateView();
- Position3D position;
+ GlobalPosition3D position;
Direction3D forward;
Direction3D up;
- float near, far;
+ GlobalDistance near, far;
glm::mat4 projection;
glm::mat4 viewProjection, inverseViewProjection;
};
diff --git a/gfx/gl/sceneRenderer.cpp b/gfx/gl/sceneRenderer.cpp
index 53178e5..582c28c 100644
--- a/gfx/gl/sceneRenderer.cpp
+++ b/gfx/gl/sceneRenderer.cpp
@@ -15,7 +15,7 @@ static constexpr const std::array<const glm::i8vec4, 4> displayVAOdata {{
}};
SceneRenderer::SceneRenderer(ScreenAbsCoord s, GLuint o) :
- camera {{-1250000.0F, -1250000.0F, 35.0F}, quarter_pi, ratio(s), 100.F, 10000000.0F}, size {s}, output {o},
+ camera {{-1250000, -1250000, 35.0F}, quarter_pi, ratio(s), 100, 10000000}, size {s}, output {o},
lighting {lighting_vs, lighting_fs}, shadowMapper {{2048, 2048}}
{
shader.setViewPort({0, 0, size.x, size.y});
diff --git a/gfx/gl/shadowMapper.cpp b/gfx/gl/shadowMapper.cpp
index 66905df..deb0630 100644
--- a/gfx/gl/shadowMapper.cpp
+++ b/gfx/gl/shadowMapper.cpp
@@ -79,12 +79,12 @@ constexpr std::array<std::array<TextureRelRegion, ShadowMapper::SHADOW_BANDS>, S
{0.25F, 0.25F, 0.75F, 0.75F}, // upper right
}},
}};
-constexpr std::array<float, ShadowMapper::SHADOW_BANDS + 1> shadowBands {
- 1000.F,
- 250000.F,
- 750000.F,
- 2500000.F,
- 10000000.F,
+constexpr std::array<GlobalDistance, ShadowMapper::SHADOW_BANDS + 1> shadowBands {
+ 1000,
+ 250000,
+ 750000,
+ 2500000,
+ 10000000,
};
static_assert(viewports.size() == shadowMapRegions.size());
static_assert(shadowBands.size() == shadowMapRegions.size() + 1);
@@ -114,7 +114,7 @@ ShadowMapper::getBandViewExtents(const Camera & camera, const glm::mat4 & lightV
bandViewExtents.emplace_back(extents * [&lightView](const auto & e) -> Position3D {
return lightView * glm::vec4(Position3D {e}, 1);
});
- if (std::none_of(extents.begin(), extents.end(), [targetDist = dist * 0.99F](const glm::vec4 & e) {
+ if (std::none_of(extents.begin(), extents.end(), [targetDist = dist - 1](const auto & e) {
return e.w > targetDist;
})) {
break;
@@ -130,8 +130,8 @@ ShadowMapper::update(const SceneProvider & scene, const Direction3D & dir, const
glClear(GL_DEPTH_BUFFER_BIT);
glCullFace(GL_FRONT);
- const auto lightView = glm::lookAt(camera.getPosition(), camera.getPosition() + dir, up);
const auto lightViewDir = glm::lookAt(origin, dir, up);
+ const auto lightView = lightViewDir * glm::translate(RelativePosition3D {-camera.getPosition()});
const auto lightViewPoint = camera.getPosition();
const auto bandViewExtents = getBandViewExtents(camera, lightView);