GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Camera2D.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_2D_CAMERA_2D_HPP
5#define GREM_GRAPHICS_2D_CAMERA_2D_HPP
6
8
10#include <GREM/core/math.hpp>
13
14namespace grem::graphics {
15
16class Device; // Forward declaration, to avoid including Device.hpp.
17
26 vec2 offset{0.0f, 0.0f};
27
31 vec2 size{1.0f, 1.0f};
32
40 [[nodiscard]] constexpr bool operator==(const OrthographicProjection2D& other) const = default;
41};
42
46struct Projection2D : Variant<mat3, OrthographicProjection2D> {
47 using Variant::Variant;
48};
49
62 vec2 position{0.0f, 0.0f};
63
72 float angle = 0.0f;
73
82 vec2 scale{1.0f, 1.0f};
83};
84
88struct View2D : Variant<mat3, WorldView2D> {
89 using Variant::Variant;
90};
91
103 [[nodiscard]] constexpr bool operator==(const Camera2DOptions& other) const noexcept = default;
104};
105
109class Camera2D {
110public:
121
125 using ParameterBuffer = UniformBuffer<Parameters, "Camera2DParameters">;
126
142 explicit Camera2D(Device& device, const Projection2D& projection = mat3{1.0f}, const View2D& view = mat3{1.0f}, const Camera2DOptions& options = {})
143 : parameterBuffer(device)
144 , options(options) {
145 setProjectionAndView(projection, view);
146 }
147
159 void setProjection(const Projection2D& newProjection) {
160 setProjectionWithoutFlush(newProjection);
161 flush();
162 }
163
175 void setView(const View2D& newView) {
176 setViewWithoutFlush(newView);
177 flush();
178 }
179
190 void setOptions(const Camera2DOptions& newOptions) {
191 options = newOptions;
192 }
193
207 void setProjectionAndView(const Projection2D& newProjection, const View2D& newView) {
208 setProjectionWithoutFlush(newProjection);
209 setViewWithoutFlush(newView);
210 flush();
211 }
212
225 void setViewAndOptions(const View2D& newView, const Camera2DOptions& newOptions) {
226 setViewWithoutFlush(newView);
227 options = newOptions;
228 flush();
229 }
230
246 void setProjectionAndViewAndOptions(const Projection2D& newProjection, const View2D& newView, const Camera2DOptions& newOptions) {
247 setProjectionWithoutFlush(newProjection);
248 setViewWithoutFlush(newView);
249 options = newOptions;
250 flush();
251 }
252
258 [[nodiscard]] const ParameterBuffer& getParameterBuffer() const noexcept {
259 return parameterBuffer;
260 }
261
267 [[nodiscard]] const mat3& getProjectionMatrix() const noexcept {
268 return projectionMatrix;
269 }
270
276 [[nodiscard]] const mat3& getViewMatrix() const noexcept {
277 return viewMatrix;
278 }
279
285 [[nodiscard]] Camera2DOptions getOptions() const noexcept {
286 return options;
287 }
288
289private:
290 void setProjectionWithoutFlush(const Projection2D& newProjection) {
291 GREM_MATCH(newProjection) {
292 GREM_CASE(const mat3& newProjectionMatrix) {
293 projectionMatrix = newProjectionMatrix;
294 break;
295 }
296 GREM_CASE(const OrthographicProjection2D& newOrthographicProjection) {
297 projectionMatrix = scale(2.0f / newOrthographicProjection.size) * translate(newOrthographicProjection.size * -0.5f - newOrthographicProjection.offset);
298 break;
299 }
300 }
301 }
302
303 void setViewWithoutFlush(const View2D& newView) {
304 GREM_MATCH(newView) {
305 GREM_CASE(const mat3& newViewMatrix) {
306 viewMatrix = newViewMatrix;
307 break;
308 }
309 GREM_CASE(const WorldView2D& newWorldView) {
310 const mat2 inverseBasis = transpose(mat2{rotateScale(newWorldView.angle, newWorldView.scale)});
311 const vec2 inversePosition = inverseBasis * -newWorldView.position;
312 viewMatrix = {
313 vec3{inverseBasis[0], 0.0f},
314 vec3{inverseBasis[1], 0.0f},
315 vec3{inversePosition, 1.0f},
316 };
317 break;
318 }
319 }
320 }
321
322 void flush() {
323 const mat2 basis = transpose(mat2{viewMatrix});
324 const vec2 position = basis * -vec2{viewMatrix[2]};
325 parameterBuffer.upload(Parameters{
326 .cameraProjectionMatrix = projectionMatrix,
327 .cameraViewMatrix = viewMatrix,
328 .cameraViewProjectionMatrix = projectionMatrix * viewMatrix,
329 .cameraPosition = position,
330 .cameraBasis = basis,
331 });
332 }
333
334 ParameterBuffer parameterBuffer;
335 mat3 projectionMatrix{1.0f};
336 mat3 viewMatrix{1.0f};
337 [[no_unique_address]] Camera2DOptions options;
338};
339
340} // namespace grem::graphics
341
342#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
Camera2DOptions getOptions() const noexcept
Get the configuration options of the camera.
Definition Camera2D.hpp:285
const mat3 & getViewMatrix() const noexcept
Get the view matrix of the camera.
Definition Camera2D.hpp:276
void setProjectionAndViewAndOptions(const Projection2D &newProjection, const View2D &newView, const Camera2DOptions &newOptions)
Set both the projection and view of the camera, as well as its configuration options.
Definition Camera2D.hpp:246
void setProjection(const Projection2D &newProjection)
Set the projection of the camera.
Definition Camera2D.hpp:159
Camera2D(Device &device, const Projection2D &projection=mat3{1.0f}, const View2D &view=mat3{1.0f}, const Camera2DOptions &options={})
Construct a camera.
Definition Camera2D.hpp:142
void setViewAndOptions(const View2D &newView, const Camera2DOptions &newOptions)
Set both the view and configuration options of the camera.
Definition Camera2D.hpp:225
void setView(const View2D &newView)
Set the view of the camera.
Definition Camera2D.hpp:175
void setOptions(const Camera2DOptions &newOptions)
Set the configuration options of the camera.
Definition Camera2D.hpp:190
const ParameterBuffer & getParameterBuffer() const noexcept
Get the parameter buffer of the camera.
Definition Camera2D.hpp:258
UniformBuffer< Parameters, "Camera2DParameters"> ParameterBuffer
Shader buffer for 2D camera parameters.
Definition Camera2D.hpp:125
const mat3 & getProjectionMatrix() const noexcept
Get the projection matrix of the camera.
Definition Camera2D.hpp:267
void setProjectionAndView(const Projection2D &newProjection, const View2D &newView)
Set both the projection and view of the camera.
Definition Camera2D.hpp:207
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
GREM_ALWAYS_INLINE constexpr mat< 3, 3, T > GREM_VECTORCALL translate(vec< 2, T > translation)
Create a translation matrix describing a 2D translation.
Definition math.hpp:2631
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< 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
mat< 2, 2, float > mat2
Definition math.hpp:804
GREM_ALWAYS_INLINE constexpr mat< 3, 3, T > GREM_VECTORCALL scale(vec< 2, T > scale)
Create a scale matrix describing a 2D scale.
Definition math.hpp:2665
Configuration options for a Camera2D.
Definition Camera2D.hpp:95
constexpr bool operator==(const Camera2DOptions &other) const noexcept=default
Compare these options to another set of options for equality.
Struct of shader parameters representing the state of a Camera2D.
Definition Camera2D.hpp:114
mat3 cameraViewMatrix
Current view matrix of the camera.
Definition Camera2D.hpp:116
mat2 cameraBasis
Current right and up direction vectors (local X and Y axes) of the camera.
Definition Camera2D.hpp:119
mat3 cameraViewProjectionMatrix
Current combined view-projection matrix of the camera.
Definition Camera2D.hpp:117
vec2 cameraPosition
Current position of the camera in world space.
Definition Camera2D.hpp:118
mat3 cameraProjectionMatrix
Current projection matrix of the camera.
Definition Camera2D.hpp:115
Configuration of an orthographic projection in 2D.
Definition Camera2D.hpp:21
vec2 size
Size of the orthographic projection, in render target coordinates.
Definition Camera2D.hpp:31
constexpr bool operator==(const OrthographicProjection2D &other) const =default
Compare these options to another set of options for equality.
vec2 offset
Bottom left corner of the orthographic projection, in render target coordinates.
Definition Camera2D.hpp:26
Projection matrix or configuration of a projection in 2D.
Definition Camera2D.hpp:46
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 2D.
Definition Camera2D.hpp:88
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 2D.
Definition Camera2D.hpp:53
vec2 scale
World-space scale of the view.
Definition Camera2D.hpp:82
vec2 position
World-space position of the view origin.
Definition Camera2D.hpp:62
float angle
World-space orientation of the view around its origin, in radians.
Definition Camera2D.hpp:72