GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Camera3D.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_GRAPHICS_3D_CAMERA_3D_HPP
5#define GREM_GRAPHICS_3D_CAMERA_3D_HPP
6
8
11#include <GREM/core/math.hpp>
14
15namespace grem::graphics {
16
17class Device; // Forward declaration, to avoid including Device.hpp.
18
27 vec2 offset{0.0f, 0.0f};
28
32 vec2 size{1.0f, 1.0f};
33
39 float nearZ = 0.0f;
40
46 float farZ = 0.0f;
47
55 [[nodiscard]] constexpr bool operator==(const OrthographicProjection3D& other) const = default;
56};
57
70 float verticalFieldOfView = 1.28700221758656877361f;
71
78 float aspectRatio = 1.0f;
79
83 float nearZ = 0.01f;
84
90 float farZ = 1000.0f;
91
99 [[nodiscard]] constexpr bool operator==(const PerspectiveProjection3D& other) const = default;
100};
101
105struct Projection3D : Variant<mat4, OrthographicProjection3D, PerspectiveProjection3D> {
106 using Variant::Variant;
107};
108
121 vec3 position{0.0f, 0.0f, 0.0f};
122
131 quat orientation{0.0f, 0.0f, 0.0f, 1.0f};
132
141 vec3 scale{1.0f, 1.0f, 1.0f};
142};
143
147struct View3D : Variant<mat4, WorldView3D> {
148 using Variant::Variant;
149};
150
158 float exposure = 1.0f;
159
167 [[nodiscard]] constexpr bool operator==(const Camera3DOptions& other) const noexcept = default;
168};
169
173class Camera3D {
174public:
187
191 using ParameterBuffer = UniformBuffer<Parameters, "Camera3DParameters">;
192
208 explicit Camera3D(Device& device, const Projection3D& projection = mat4{1.0f}, const View3D& view = mat4{1.0f}, const Camera3DOptions& options = {})
209 : parameterBuffer(device) {
210 setProjectionAndViewAndOptions(projection, view, options);
211 }
212
224 void setProjection(const Projection3D& newProjection) {
225 setProjectionWithoutFlush(newProjection);
226 flush();
227 }
228
240 void setView(const View3D& newView) {
241 setViewWithoutFlush(newView);
242 flush();
243 }
244
255 void setOptions(const Camera3DOptions& newOptions) {
256 setOptionsWithoutFlush(newOptions);
257 flush();
258 }
259
273 void setProjectionAndView(const Projection3D& newProjection, const View3D& newView) {
274 setProjectionWithoutFlush(newProjection);
275 setViewWithoutFlush(newView);
276 flush();
277 }
278
291 void setViewAndOptions(const View3D& newView, const Camera3DOptions& newOptions) {
292 setViewWithoutFlush(newView);
293 setOptionsWithoutFlush(newOptions);
294 flush();
295 }
296
312 void setProjectionAndViewAndOptions(const Projection3D& newProjection, const View3D& newView, const Camera3DOptions& newOptions) {
313 setProjectionWithoutFlush(newProjection);
314 setViewWithoutFlush(newView);
315 setOptionsWithoutFlush(newOptions);
316 flush();
317 }
318
324 [[nodiscard]] const ParameterBuffer& getParameterBuffer() const noexcept {
325 return parameterBuffer;
326 }
327
333 [[nodiscard]] const mat4& getProjectionMatrix() const noexcept {
334 return projectionMatrix;
335 }
336
342 [[nodiscard]] const mat4& getViewMatrix() const noexcept {
343 return viewMatrix;
344 }
345
351 [[nodiscard]] Camera3DOptions getOptions() const noexcept {
352 return options;
353 }
354
355private:
356 void setProjectionWithoutFlush(const Projection3D& newProjection) {
357 GREM_MATCH(newProjection) {
358 GREM_CASE(const mat4& newProjectionMatrix) {
359 projectionMatrix = newProjectionMatrix;
360 break;
361 }
362 GREM_CASE(const OrthographicProjection3D& newOrthographicProjection) {
363 if (newOrthographicProjection.nearZ < newOrthographicProjection.farZ) {
364 projectionMatrix = ortho(newOrthographicProjection.offset.x, newOrthographicProjection.offset.x + newOrthographicProjection.size.x,
365 newOrthographicProjection.offset.y, newOrthographicProjection.offset.y + newOrthographicProjection.size.y, newOrthographicProjection.nearZ,
366 newOrthographicProjection.farZ);
367 } else {
368 projectionMatrix = ortho(newOrthographicProjection.offset.x, newOrthographicProjection.offset.x + newOrthographicProjection.size.x,
369 newOrthographicProjection.offset.y, newOrthographicProjection.offset.y + newOrthographicProjection.size.y);
370 }
371 break;
372 }
373 GREM_CASE(const PerspectiveProjection3D& newPerspectiveProjection) {
374 if (newPerspectiveProjection.nearZ < newPerspectiveProjection.farZ) {
375 projectionMatrix = perspective(newPerspectiveProjection.verticalFieldOfView, newPerspectiveProjection.aspectRatio, newPerspectiveProjection.nearZ,
376 newPerspectiveProjection.farZ);
377 } else {
378 projectionMatrix = infinitePerspective(newPerspectiveProjection.verticalFieldOfView, newPerspectiveProjection.aspectRatio, newPerspectiveProjection.nearZ);
379 }
380 break;
381 }
382 }
383 }
384
385 void setViewWithoutFlush(const View3D& newView) {
386 GREM_MATCH(newView) {
387 GREM_CASE(const mat4& newViewMatrix) {
388 viewMatrix = newViewMatrix;
389 break;
390 }
391 GREM_CASE(const WorldView3D& newWorldView) {
392 const mat3 inverseBasis = transpose(mat3{rotateScale(newWorldView.orientation, newWorldView.scale)});
393 const vec3 inversePosition = inverseBasis * -newWorldView.position;
394 viewMatrix = {
395 vec4{inverseBasis[0], 0.0f},
396 vec4{inverseBasis[1], 0.0f},
397 vec4{inverseBasis[2], 0.0f},
398 vec4{inversePosition, 1.0f},
399 };
400 break;
401 }
402 }
403 }
404
405 void setOptionsWithoutFlush(const Camera3DOptions& newOptions) {
406 options = newOptions;
407 }
408
409 void flush() {
410 const mat3 basis = transpose(mat3{viewMatrix});
411 const vec3 position = basis * -vec3{viewMatrix[3]};
412
413 const float nearZ = projectionMatrix[3][2] / projectionMatrix[2][2];
414 const float farZ = (projectionMatrix[2][2] == -1.0f) ? Limits<float>::MAX : projectionMatrix[3][2] / (projectionMatrix[2][2] + 1.0f);
415
416 parameterBuffer.upload(Parameters{
417 .cameraProjectionMatrix = projectionMatrix,
418 .cameraViewMatrix = viewMatrix,
419 .cameraViewProjectionMatrix = projectionMatrix * viewMatrix,
420 .cameraNearAndFarPlaneDistances{nearZ, farZ},
421 .cameraPosition = position,
422 .cameraBasis = basis,
423 .cameraExposure = options.exposure,
424 });
425 }
426
427 ParameterBuffer parameterBuffer;
428 mat4 projectionMatrix{1.0f};
429 mat4 viewMatrix{1.0f};
430 Camera3DOptions options{};
431};
432
433} // namespace grem::graphics
434
435#endif
#define GREM_MATCH(...)
Definition Variant.hpp:1997
#define GREM_CASE(...)
Definition Variant.hpp:1998
friend class Variant
JSON value type using the default allocator.
Definition Variant.hpp:1402
const mat4 & getViewMatrix() const noexcept
Get the view matrix of the camera.
Definition Camera3D.hpp:342
void setOptions(const Camera3DOptions &newOptions)
Set the configuration options of the camera.
Definition Camera3D.hpp:255
void setProjectionAndViewAndOptions(const Projection3D &newProjection, const View3D &newView, const Camera3DOptions &newOptions)
Set both the projection and view of the camera, as well as its configuration options.
Definition Camera3D.hpp:312
void setProjectionAndView(const Projection3D &newProjection, const View3D &newView)
Set both the projection and view of the camera.
Definition Camera3D.hpp:273
Camera3DOptions getOptions() const noexcept
Get the configuration options of the camera.
Definition Camera3D.hpp:351
void setProjection(const Projection3D &newProjection)
Set the projection of the camera.
Definition Camera3D.hpp:224
const ParameterBuffer & getParameterBuffer() const noexcept
Get the parameter buffer of the camera.
Definition Camera3D.hpp:324
Camera3D(Device &device, const Projection3D &projection=mat4{1.0f}, const View3D &view=mat4{1.0f}, const Camera3DOptions &options={})
Construct a camera.
Definition Camera3D.hpp:208
const mat4 & getProjectionMatrix() const noexcept
Get the projection matrix of the camera.
Definition Camera3D.hpp:333
UniformBuffer< Parameters, "Camera3DParameters"> ParameterBuffer
Shader buffer for 3D camera parameters.
Definition Camera3D.hpp:191
void setViewAndOptions(const View3D &newView, const Camera3DOptions &newOptions)
Set both the view and configuration options of the camera.
Definition Camera3D.hpp:291
void setView(const View3D &newView)
Set the view of the camera.
Definition Camera3D.hpp:240
Persistent system managing the rendering context of a Window.
Definition Device.hpp:68
GPU memory buffer containing a fixed-size struct of uniform parameters to be read by a shader program...
Definition buffers.hpp:426
Definition buffer_layouts.hpp:20
qua< float > quat
Definition math.hpp:892
vec< 4, float > vec4
Definition math.hpp:440
vec< 2, float > vec2
Definition math.hpp:438
GREM_ALWAYS_INLINE constexpr mat< R, C, T > transpose(const mat< C, R, T > &a)
Definition math.hpp:2247
vec< 3, float > vec3
Definition math.hpp:439
mat< 3, 3, float > mat3
Definition math.hpp:805
GREM_ALWAYS_INLINE mat< 4, 4, T > GREM_VECTORCALL infinitePerspective(T verticalFieldOfView, T aspectRatio, T nearZ)
Definition math.hpp:3046
GREM_ALWAYS_INLINE mat< 3, 3, T > GREM_VECTORCALL rotateScale(T rotation, vec< 2, T > scale)
Create a combined rotation and scale matrix describing a 2D transformation.
Definition math.hpp:2765
GREM_ALWAYS_INLINE mat< 4, 4, T > GREM_VECTORCALL perspective(T verticalFieldOfView, T aspectRatio, T nearZ, T farZ)
Definition math.hpp:3035
GREM_ALWAYS_INLINE mat< 4, 4, T > GREM_VECTORCALL ortho(T left, T right, T bottom, T top)
Definition math.hpp:3005
mat< 4, 4, float > mat4
Definition math.hpp:806
static constexpr T MAX
Definition fundamentals.hpp:29
Configuration options for a Camera3D.
Definition Camera3D.hpp:154
float exposure
Exposure parameter of the camera.
Definition Camera3D.hpp:158
constexpr bool operator==(const Camera3DOptions &other) const noexcept=default
Compare these options to another set of options for equality.
Struct of shader parameters representing the state of a Camera3D.
Definition Camera3D.hpp:178
mat3 cameraBasis
Current right, up and backward direction vectors (local X, Y and Z axes) of the camera in world space...
Definition Camera3D.hpp:184
float cameraExposure
Current exposure parameter of the camera.
Definition Camera3D.hpp:185
mat4 cameraViewProjectionMatrix
Current combined view-projection matrix of the camera.
Definition Camera3D.hpp:181
vec2 cameraNearAndFarPlaneDistances
Current near (X) and far (Y) plane distances of the camera.
Definition Camera3D.hpp:182
vec3 cameraPosition
Current position of the camera in world space.
Definition Camera3D.hpp:183
mat4 cameraProjectionMatrix
Current projection matrix of the camera.
Definition Camera3D.hpp:179
mat4 cameraViewMatrix
Current view matrix of the camera.
Definition Camera3D.hpp:180
Configuration of an orthographic projection in 3D.
Definition Camera3D.hpp:22
float farZ
Distance to the far plane of the projection, in view coordinates.
Definition Camera3D.hpp:46
vec2 offset
Bottom left corner of the orthographic projection, in render target coordinates.
Definition Camera3D.hpp:27
vec2 size
Size of the orthographic projection, in render target coordinates.
Definition Camera3D.hpp:32
constexpr bool operator==(const OrthographicProjection3D &other) const =default
Compare these options to another set of options for equality.
float nearZ
Distance to the near plane of the projection, in view coordinates.
Definition Camera3D.hpp:39
Configuration of a perspective projection in 3D.
Definition Camera3D.hpp:61
constexpr bool operator==(const PerspectiveProjection3D &other) const =default
Compare these options to another set of options for equality.
float nearZ
Distance to the near plane of the projection, in view coordinates.
Definition Camera3D.hpp:83
float verticalFieldOfView
Vertical field of view of the projection, in radians.
Definition Camera3D.hpp:70
float farZ
Distance to the far plane of the projection, in view coordinates.
Definition Camera3D.hpp:90
float aspectRatio
Aspect ratio of the projection, X/Y.
Definition Camera3D.hpp:78
Projection matrix or configuration of a projection in 3D.
Definition Camera3D.hpp:105
constexpr Variant() noexcept(std::is_nothrow_default_constructible_v< variant_alternative_t< 0, Variant > >)
Default-construct a variant with the first variant alternative, if it is default-constructible.
Definition Variant.hpp:351
View matrix or configuration of a view in 3D.
Definition Camera3D.hpp:147
constexpr Variant() noexcept(std::is_nothrow_default_constructible_v< variant_alternative_t< 0, Variant > >)
Default-construct a variant with the first variant alternative, if it is default-constructible.
Definition Variant.hpp:351
Configuration of a world-space view in 3D.
Definition Camera3D.hpp:112
quat orientation
World-space orientation of the view around its origin.
Definition Camera3D.hpp:131
vec3 scale
World-space scale of the view.
Definition Camera3D.hpp:141
vec3 position
World-space position of the view origin.
Definition Camera3D.hpp:121