From bb76f72831af73adb1b2deebd4b9235fa51fa7df Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 11 Dec 2022 14:28:46 +0000 Subject: Cache view and viewProject matrices, update only on camera movement --- gfx/gl/camera.cpp | 14 ++++++++------ gfx/gl/camera.h | 13 ++++++++++++- 2 files changed, 20 insertions(+), 7 deletions(-) (limited to 'gfx') diff --git a/gfx/gl/camera.cpp b/gfx/gl/camera.cpp index 1c46200..994d8ab 100644 --- a/gfx/gl/camera.cpp +++ b/gfx/gl/camera.cpp @@ -7,12 +7,7 @@ Camera::Camera(glm::vec3 pos, float fov, float aspect, float zNear, float zFar) position {pos}, forward {::north}, up {::up}, fov {fov}, aspect {aspect}, near {zNear}, far {zFar}, projection {glm::perspective(fov, aspect, zNear, zFar)} { -} - -glm::mat4 -Camera::getViewProjection() const -{ - return projection * glm::lookAt(position, position + forward, up); + updateView(); } Ray @@ -22,3 +17,10 @@ Camera::unProject(const glm::vec2 & mouse) const return {position, glm::normalize(glm::unProject(mouse ^ 1, glm::lookAt(::origin, forward, up), projection, screen))}; } + +void +Camera::updateView() +{ + view = glm::lookAt(position, position + forward, up); + viewProjection = projection * view; +} diff --git a/gfx/gl/camera.h b/gfx/gl/camera.h index 5b9d339..94eb8fc 100644 --- a/gfx/gl/camera.h +++ b/gfx/gl/camera.h @@ -8,18 +8,24 @@ class Camera { public: Camera(glm::vec3 pos, float fov, float aspect, float zNear, float zFar); - [[nodiscard]] glm::mat4 getViewProjection() const; + [[nodiscard]] glm::mat4 + getViewProjection() const + { + return viewProjection; + } [[nodiscard]] Ray unProject(const glm::vec2 &) const; void setPosition(const glm::vec3 & p) { position = p; + updateView(); } void setForward(const glm::vec3 & f) { forward = f; + updateView(); } void setView(const glm::vec3 & p, const glm::vec3 & f, const glm::vec3 & u = ::up) @@ -27,6 +33,7 @@ public: position = p; forward = f; up = u; + updateView(); } void lookAt(const glm::vec3 & target) @@ -45,10 +52,14 @@ public: } private: + void updateView(); + glm::vec3 position; glm::vec3 forward; glm::vec3 up; float fov, aspect, near, far; glm::mat4 projection; + glm::mat4 view; + glm::mat4 viewProjection; }; -- cgit v1.2.3