From 379b07fa1fb953cc175028fcb95dfbb64c392451 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 11 Dec 2022 14:22:57 +0000 Subject: Make Camera members private with accessors and helpful setters --- gfx/followCameraController.cpp | 12 ++++-------- gfx/gl/camera.cpp | 7 ++++--- gfx/gl/camera.h | 38 ++++++++++++++++++++++++++++++++++++-- test/test-maths.cpp | 10 +++++----- test/test-render.cpp | 6 ++---- ui/manualCameraController.cpp | 5 ++--- 6 files changed, 53 insertions(+), 25 deletions(-) diff --git a/gfx/followCameraController.cpp b/gfx/followCameraController.cpp index 42d1666..d15ecda 100644 --- a/gfx/followCameraController.cpp +++ b/gfx/followCameraController.cpp @@ -20,20 +20,16 @@ FollowCameraController::updateCamera(Camera * camera) const switch (mode) { case Mode::Pan: - camera->forward = glm::normalize(pos - camera->pos); - camera->up = up; + camera->lookAt(pos); break; case Mode::Ride: - camera->pos = pos + (up * 4.8F); - camera->forward = !-sincosf(rot.y); - camera->up = up; + camera->setView(pos + (up * 4.8F), !-sincosf(rot.y)); break; case Mode::ISO: - camera->pos = pos + ((up + north + east) * 40.F); - camera->forward = glm::normalize(down + south + west); - camera->up = glm::normalize(up - north - east); + camera->setView(pos + ((up + north + east) * 40.F), glm::normalize(down + south + west), + glm::normalize(up - north - east)); break; } } diff --git a/gfx/gl/camera.cpp b/gfx/gl/camera.cpp index 7438739..0e81282 100644 --- a/gfx/gl/camera.cpp +++ b/gfx/gl/camera.cpp @@ -4,19 +4,20 @@ #include Camera::Camera(glm::vec3 pos, float fov, float aspect, float zNear, float zFar) : - pos {pos}, forward {::north}, up {::up}, projection {glm::perspective(fov, aspect, zNear, zFar)} + position {pos}, forward {::north}, up {::up}, projection {glm::perspective(fov, aspect, zNear, zFar)} { } glm::mat4 Camera::getViewProjection() const { - return projection * glm::lookAt(pos, pos + forward, up); + return projection * glm::lookAt(position, position + forward, up); } Ray Camera::unProject(const glm::vec2 & mouse) const { static constexpr const glm::vec4 screen {0, 0, 1, 1}; - return {pos, glm::normalize(glm::unProject(mouse ^ 1, glm::lookAt(::origin, forward, up), projection, screen))}; + return {position, + glm::normalize(glm::unProject(mouse ^ 1, glm::lookAt(::origin, forward, up), projection, screen))}; } diff --git a/gfx/gl/camera.h b/gfx/gl/camera.h index e22d23f..d82802f 100644 --- a/gfx/gl/camera.h +++ b/gfx/gl/camera.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include class Camera { @@ -10,10 +11,43 @@ public: [[nodiscard]] glm::mat4 getViewProjection() const; [[nodiscard]] Ray unProject(const glm::vec2 &) const; - glm::vec3 pos; + void + setPosition(const glm::vec3 & p) + { + position = p; + } + void + setForward(const glm::vec3 & f) + { + forward = f; + } + void + setView(const glm::vec3 & p, const glm::vec3 & f, const glm::vec3 & u = ::up) + { + position = p; + forward = f; + up = u; + } + void + lookAt(const glm::vec3 & target) + { + setForward(glm::normalize(target - position)); + } + [[nodiscard]] auto + getForward() const + { + return forward; + } + [[nodiscard]] auto + getPosition() const + { + return position; + } + +private: + glm::vec3 position; glm::vec3 forward; glm::vec3 up; -private: glm::mat4 projection; }; diff --git a/test/test-maths.cpp b/test/test-maths.cpp index 70cf206..d6c0fc1 100644 --- a/test/test-maths.cpp +++ b/test/test-maths.cpp @@ -256,7 +256,7 @@ BOOST_AUTO_TEST_CASE(camera_clicks) { Camera camera {::origin, ::half_pi, 1.25F, .1F, 10000.F}; constexpr float centre {0.5F}, right {0.9F}, left {0.1F}, top {1.F}, bottom {0.F}; - camera.forward = ::north; + camera.setForward(::north); BOOST_CHECK_EQUAL(camera.unProject({centre, centre}).start, ::origin); BOOST_CHECK_CLOSE_VEC(camera.unProject({centre, centre}).direction, ::north); BOOST_CHECK_CLOSE_VEC(camera.unProject({left, centre}).direction, glm::normalize(::north + ::west)); @@ -268,20 +268,20 @@ BOOST_AUTO_TEST_CASE(camera_clicks) BOOST_CHECK_CLOSE_VEC(camera.unProject({left, bottom}).direction, glm::normalize(::north + ::west + ::down)); BOOST_CHECK_CLOSE_VEC(camera.unProject({right, bottom}).direction, glm::normalize(::north + ::east + ::down)); - camera.forward = ::east; + camera.setForward(::east); BOOST_CHECK_CLOSE_VEC(camera.unProject({centre, centre}).direction, ::east); BOOST_CHECK_CLOSE_VEC(camera.unProject({left, centre}).direction, glm::normalize(::north + ::east)); BOOST_CHECK_CLOSE_VEC(camera.unProject({right, centre}).direction, glm::normalize(::south + ::east)); - camera.forward = glm::normalize(::north + ::down); + camera.setForward(glm::normalize(::north + ::down)); BOOST_CHECK_CLOSE_VEC(camera.unProject({centre, centre}).direction, glm::normalize(::north + ::down)); BOOST_CHECK_CLOSE_VEC(camera.unProject({centre, top}).direction, glm::normalize(::north)); - camera.forward = glm::normalize(::north + ::west + ::down); + camera.setForward(glm::normalize(::north + ::west + ::down)); BOOST_CHECK_CLOSE_VEC(camera.unProject({centre, centre}).direction, glm::normalize(::north + ::west + ::down)); BOOST_CHECK_CLOSE_VEC(camera.unProject({centre, top}).direction, glm::normalize(::north + ::west + ::up * 0.2F)); - camera.forward = glm::normalize(::north + ::west); + camera.setForward(glm::normalize(::north + ::west)); BOOST_CHECK_CLOSE_VEC(camera.unProject({centre, centre}).direction, glm::normalize(::north + ::west)); BOOST_CHECK_CLOSE_VEC(camera.unProject({centre, top}).direction, glm::normalize(::north + ::west + ::up * 1.2F)); BOOST_CHECK_CLOSE_VEC(camera.unProject({right, centre}).direction, glm::normalize(::north)); diff --git a/test/test-render.cpp b/test/test-render.cpp index d6e4094..77501ac 100644 --- a/test/test-render.cpp +++ b/test/test-render.cpp @@ -100,8 +100,7 @@ BOOST_FIXTURE_TEST_SUITE(w, TestRenderOutput); BOOST_AUTO_TEST_CASE(basic) { SceneRenderer ss {size, output}; - ss.camera.pos = {-10, -10, 60}; - ss.camera.forward = glm::normalize(glm::vec3 {1, 1, -0.5F}); + ss.camera.setView({-10, -10, 60}, glm::normalize(glm::vec3 {1, 1, -0.5F})); TestScene scene; ss.render(scene); glDisable(GL_DEBUG_OUTPUT); @@ -111,8 +110,7 @@ BOOST_AUTO_TEST_CASE(basic) BOOST_AUTO_TEST_CASE(pointlight) { SceneRenderer ss {size, output}; - ss.camera.pos = {-10, -10, 60}; - ss.camera.forward = glm::normalize(glm::vec3 {1, 1, -0.5F}); + ss.camera.setView({-10, -10, 60}, glm::normalize(glm::vec3 {1, 1, -0.5F})); class PointLightScene : public TestScene { public: void diff --git a/ui/manualCameraController.cpp b/ui/manualCameraController.cpp index 74b526b..6d4a22a 100644 --- a/ui/manualCameraController.cpp +++ b/ui/manualCameraController.cpp @@ -78,7 +78,6 @@ ManualCameraController::render(const UIShader &, const Position &) const void ManualCameraController::updateCamera(Camera * camera) const { - camera->forward = glm::normalize(sincosf(direction) ^ -sin(pitch)); - camera->pos = !focus + up * 3.F - (camera->forward * std::pow(dist, 1.3F)); - camera->up = up; + camera->setView(!focus + up * 3.F - (camera->getForward() * std::pow(dist, 1.3F)), + glm::normalize(sincosf(direction) ^ -sin(pitch))); } -- cgit v1.2.3