diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-12-11 14:28:46 +0000 |
---|---|---|
committer | Dan Goodliffe <dan.goodliffe@octal.co.uk> | 2022-12-12 09:21:50 +0000 |
commit | bb76f72831af73adb1b2deebd4b9235fa51fa7df (patch) | |
tree | 97e8ba1bedb42642366262c66b6170adf8d2d1b8 /gfx | |
parent | Have Camera remember all its properties (diff) | |
download | ilt-bb76f72831af73adb1b2deebd4b9235fa51fa7df.tar.bz2 ilt-bb76f72831af73adb1b2deebd4b9235fa51fa7df.tar.xz ilt-bb76f72831af73adb1b2deebd4b9235fa51fa7df.zip |
Cache view and viewProject matrices, update only on camera movement
Diffstat (limited to 'gfx')
-rw-r--r-- | gfx/gl/camera.cpp | 14 | ||||
-rw-r--r-- | gfx/gl/camera.h | 13 |
2 files changed, 20 insertions, 7 deletions
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;
};
|