diff options
-rw-r--r-- | gfx/gl/camera.cpp | 36 | ||||
-rw-r--r-- | gfx/gl/camera.h | 18 | ||||
-rw-r--r-- | gfx/gl/sceneRenderer.cpp | 2 | ||||
-rw-r--r-- | gfx/gl/shadowMapper.cpp | 16 | ||||
-rw-r--r-- | test/test-maths.cpp | 2 | ||||
-rw-r--r-- | test/test-render.cpp | 14 |
6 files changed, 44 insertions, 44 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); diff --git a/test/test-maths.cpp b/test/test-maths.cpp index cc97250..3f8c2a0 100644 --- a/test/test-maths.cpp +++ b/test/test-maths.cpp @@ -261,7 +261,7 @@ BOOST_DATA_TEST_CASE(curve1, BOOST_AUTO_TEST_CASE(camera_clicks) { - Camera camera {::origin, ::half_pi, 1.25F, .1F, 10000.F}; + Camera camera {::origin, ::half_pi, 1.25F, 1000, 10000000}; constexpr float centre {0.5F}, right {0.9F}, left {0.1F}, top {1.F}, bottom {0.F}; camera.setForward(::north); BOOST_CHECK_EQUAL(camera.unProject({centre, centre}).start, ::origin); diff --git a/test/test-render.cpp b/test/test-render.cpp index cb6b1b4..22da73b 100644 --- a/test/test-render.cpp +++ b/test/test-render.cpp @@ -67,19 +67,19 @@ BOOST_GLOBAL_FIXTURE(ApplicationBase); BOOST_GLOBAL_FIXTURE(TestMainWindow); BOOST_DATA_TEST_CASE(cam, - boost::unit_test::data::xrange(0.5F, 30.F, 1.3F) * boost::unit_test::data::xrange(0.5F, 10.F, 0.3F) - * boost::unit_test::data::xrange(50.F, 500.F, 70.F), + boost::unit_test::data::xrange(500, 30000, 1300) * boost::unit_test::data::xrange(500, 10000, 300) + * boost::unit_test::data::xrange(50000, 500000, 70000), dist, near, far) { - static constexpr glm::vec4 pos {-10, -10, 60, 0}; + static constexpr GlobalPosition4D pos {-10, -10, 60000, 0}; const Camera cam {pos, half_pi, 1.F, near, far}; const auto e = cam.extentsAtDist(dist); - BOOST_CHECK_CLOSE_VEC(e[0], pos + glm::vec4(-dist, dist, -dist, dist)); - BOOST_CHECK_CLOSE_VEC(e[1], pos + glm::vec4(-dist, dist, dist, dist)); - BOOST_CHECK_CLOSE_VEC(e[2], pos + glm::vec4(dist, dist, -dist, dist)); - BOOST_CHECK_CLOSE_VEC(e[3], pos + glm::vec4(dist, dist, dist, dist)); + BOOST_CHECK_CLOSE_VECI(e[0], pos + GlobalPosition4D(-dist, dist, -dist, dist)); + BOOST_CHECK_CLOSE_VECI(e[1], pos + GlobalPosition4D(-dist, dist, dist, dist)); + BOOST_CHECK_CLOSE_VECI(e[2], pos + GlobalPosition4D(dist, dist, -dist, dist)); + BOOST_CHECK_CLOSE_VECI(e[3], pos + GlobalPosition4D(dist, dist, dist, dist)); } BOOST_FIXTURE_TEST_SUITE(w, TestRenderOutput); |