libdonut  2.3.2
Application framework for cross-platform game development in C++20
Camera.hpp
Go to the documentation of this file.
1 #ifndef DONUT_GRAPHICS_CAMERA_HPP
2 #define DONUT_GRAPHICS_CAMERA_HPP
3 
4 #include <donut/math.hpp>
5 
6 namespace donut::graphics {
7 
16  vec2 offset{0.0f, 0.0f};
17 
21  vec2 size{1.0f, 1.0f};
22 };
23 
31  float verticalFieldOfView = 1.28700221758656877361f;
32 
36  float aspectRatio = 1.0f;
37 
41  float nearZ = 0.01f;
42 
46  float farZ = 1000.0f;
47 };
48 
53 class Camera {
54 public:
63  [[nodiscard]] static Camera createOrthographic(const CameraOrthographicOptions& options, const mat4& viewMatrix) noexcept {
64  return Camera{ortho(options.offset.x, options.offset.x + options.size.x, options.offset.y, options.offset.y + options.size.y), viewMatrix};
65  }
66 
75  [[nodiscard]] static Camera createOrthographic(const CameraOrthographicOptions& options) noexcept {
76  return createOrthographic(options, identity<mat4>());
77  }
78 
89  [[nodiscard]] static Camera createOrthographic(const CameraOrthographicOptions& options, vec3 position, vec3 target, vec3 up) noexcept {
90  return createOrthographic(options, lookAt(position, target, up));
91  }
100  [[nodiscard]] static Camera createPerspective(const CameraPerspectiveOptions& options, const mat4& viewMatrix) noexcept {
101  return Camera{perspective(options.verticalFieldOfView, options.aspectRatio, options.nearZ, options.farZ), viewMatrix};
102  }
103 
112  [[nodiscard]] static Camera createPerspective(const CameraPerspectiveOptions& options) noexcept {
113  return createPerspective(options, identity<mat4>());
114  }
115 
126  [[nodiscard]] static Camera createPerspective(const CameraPerspectiveOptions& options, vec3 position, vec3 target, vec3 up) noexcept {
127  return createPerspective(options, lookAt(position, target, up));
128  }
129 
133  constexpr Camera() noexcept
134  : projectionMatrix(identity<mat4>())
135  , viewMatrix(identity<mat4>()) {}
136 
143  constexpr Camera(const mat4& projectionMatrix, const mat4& viewMatrix) noexcept
144  : projectionMatrix(projectionMatrix)
145  , viewMatrix(viewMatrix) {}
146 
152  void setProjectionOrthographic(const CameraOrthographicOptions& options) noexcept {
153  setProjectionMatrix(ortho(options.offset.x, options.offset.x + options.size.x, options.offset.y, options.offset.y + options.size.y));
154  }
155 
161  void setProjectionPerspective(const CameraPerspectiveOptions& options) noexcept {
162  setProjectionMatrix(perspective(options.verticalFieldOfView, options.aspectRatio, options.nearZ, options.farZ));
163  }
164 
173  void setView(vec3 newPosition, vec3 newTarget, vec3 newUp) noexcept {
174  setViewMatrix(lookAt(newPosition, newTarget, newUp));
175  }
176 
182  void setProjectionMatrix(const mat4& newProjectionMatrix) noexcept {
183  projectionMatrix = newProjectionMatrix;
184  }
185 
191  void setViewMatrix(const mat4& newViewMatrix) noexcept {
192  viewMatrix = newViewMatrix;
193  }
194 
200  [[nodiscard]] const mat4& getProjectionMatrix() const noexcept {
201  return projectionMatrix;
202  }
203 
209  [[nodiscard]] const mat4& getViewMatrix() const noexcept {
210  return viewMatrix;
211  }
212 
213 private:
214  mat4 projectionMatrix;
215  mat4 viewMatrix;
216 };
217 
218 } // namespace donut::graphics
219 
220 #endif
Combined view-projection matrix, defining the perspective for a Renderer to render from.
Definition: Camera.hpp:53
static Camera createPerspective(const CameraPerspectiveOptions &options) noexcept
Create a camera with a perspective projection and an identity view matrix at the default position.
Definition: Camera.hpp:112
void setView(vec3 newPosition, vec3 newTarget, vec3 newUp) noexcept
Set the view of the camera.
Definition: Camera.hpp:173
static Camera createPerspective(const CameraPerspectiveOptions &options, vec3 position, vec3 target, vec3 up) noexcept
Create a camera with a perspective projection.
Definition: Camera.hpp:126
static Camera createOrthographic(const CameraOrthographicOptions &options, const mat4 &viewMatrix) noexcept
Create a camera with an orthographic projection.
Definition: Camera.hpp:63
constexpr Camera() noexcept
Construct a camera with an identity projection matrix and view matrix.
Definition: Camera.hpp:133
void setProjectionPerspective(const CameraPerspectiveOptions &options) noexcept
Set the projection of the camera to a perspective projection.
Definition: Camera.hpp:161
constexpr Camera(const mat4 &projectionMatrix, const mat4 &viewMatrix) noexcept
Construct a camera with a specific projection matrix and view matrix.
Definition: Camera.hpp:143
static Camera createOrthographic(const CameraOrthographicOptions &options, vec3 position, vec3 target, vec3 up) noexcept
Create a camera with an orthographic projection.
Definition: Camera.hpp:89
static Camera createOrthographic(const CameraOrthographicOptions &options) noexcept
Create a camera with an orthographic projection and an identity view matrix at the default position.
Definition: Camera.hpp:75
const mat4 & getViewMatrix() const noexcept
Get the view matrix of the camera.
Definition: Camera.hpp:209
void setProjectionOrthographic(const CameraOrthographicOptions &options) noexcept
Set the projection of the camera to an orthographic projection.
Definition: Camera.hpp:152
void setViewMatrix(const mat4 &newViewMatrix) noexcept
Set the view matrix of the camera.
Definition: Camera.hpp:191
void setProjectionMatrix(const mat4 &newProjectionMatrix) noexcept
Set the projection matrix of the camera.
Definition: Camera.hpp:182
const mat4 & getProjectionMatrix() const noexcept
Get the projection matrix of the camera.
Definition: Camera.hpp:200
static Camera createPerspective(const CameraPerspectiveOptions &options, const mat4 &viewMatrix) noexcept
Create a camera with a perspective projection.
Definition: Camera.hpp:100
Definition: Buffer.hpp:7
Configuration options for a Camera with an orthographic projection.
Definition: Camera.hpp:11
vec2 offset
Bottom left corner of the orthographic projection, in framebuffer coordinates.
Definition: Camera.hpp:16
vec2 size
Size of the orthographic projection, in framebuffer coordinates.
Definition: Camera.hpp:21
Configuration options for a Camera with a perspective projection.
Definition: Camera.hpp:27
float verticalFieldOfView
Vertical field of view of the projection, in radians.
Definition: Camera.hpp:31
float aspectRatio
Aspect ratio of the projection, X/Y.
Definition: Camera.hpp:36
float nearZ
Distance to the near plane of the projection, in view coordinates.
Definition: Camera.hpp:41
float farZ
Distance to the far plane of the projection, in view coordinates.
Definition: Camera.hpp:46