Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
camera_2d.hpp
Go to the documentation of this file.
1// This file is part of the Rendering Engine project.
2// Author: Alexander Obzherin <alexanderobzherin@gmail.com>
3// Copyright (c) 2025 Alexander Obzherin
4// Distributed under the terms of the zlib License. See LICENSE.md for details.
5
6#pragma once
7
8#include <glm/glm.hpp>
9#include <glm/gtc/matrix_transform.hpp>
10#include <memory>
11
13
14namespace rendering_engine
15{
16class IApplication;
18
19/**
20 * @class Camera2D
21 * @brief Represents a 2D camera with position, rotation, and zoom control.
22 *
23 * Camera2D defines the view in a 2D scene, using a SceneComponent2D for transform (position, rotation, zoom).
24 * The camera provides basic pan, rotation, and uniform zoom features.
25 *
26 * The engine uses a left-handed coordinate system:
27 * - X+ : Right
28 * - Y+ : Up
29 * Rotation is counterclockwise, in degrees, around the Z axis (out of the screen).
30 *
31 * By default, the camera is at the origin, with zero rotation and unit zoom.
32 *
33 * @see rendering_engine::SceneComponent2D
34 */
36{
37public:
38 /**
39 * @brief Constructs a 2D camera at the origin, with no rotation and unit zoom.
40 */
42
43 /**
44 * @brief Virtual destructor.
45 */
46 virtual ~Camera2D() = default;
47
48 /**
49 * @brief Initializes the camera (calls Reset()).
50 */
51 virtual void Initialize();
52
53 /**
54 * @brief Updates the camera logic (no-op for now).
55 * @param deltaTime Time since last update (seconds).
56 */
57 virtual void Update(float deltaTime);
58
59 /**
60 * @brief Sets the viewport dimensions used to compute the projection matrix.
61 *
62 * This defines the visible area of the camera in screen-space units.
63 * The projection matrix will be recomputed based on these dimensions.
64 *
65 * @param width Viewport width in pixels or world units.
66 * @param height Viewport height in pixels or world units.
67 */
68 void SetViewportSize(float width, float height);
69
70 /**
71 * @brief Returns the current orthographic projection matrix.
72 *
73 * The matrix is built from the camera�s viewport size, and is typically
74 * multiplied with the view (world) matrix to transform coordinates into clip space.
75 *
76 * @return 4�4 orthographic projection matrix.
77 */
78 glm::mat4 GetProjectionMatrix() const;
79
80 /**
81 * @brief Sets the camera position.
82 * @param position 2D position (glm::vec2).
83 */
84 void SetPosition(const glm::vec2& position);
85
86 /**
87 * @brief Sets the camera rotation angle in degrees (counterclockwise, around Z).
88 * @param angleDegrees Angle in degrees.
89 */
90 void SetRotation(float angleDegrees);
91
92 /**
93 * @brief Sets the camera zoom (uniform scale).
94 * @param zoom Zoom factor (1.0 = no zoom).
95 */
96 void SetZoom(float zoom);
97
98 /**
99 * @brief Resets the camera to the default view (origin, zero rotation, unit zoom).
100 */
101 void Reset();
102
103 /**
104 * @brief Gets the camera position.
105 * @return 2D position (glm::vec2).
106 */
107 glm::vec2 GetPosition() const;
108
109 /**
110 * @brief Gets the camera rotation angle in degrees.
111 * @return Rotation angle (degrees).
112 */
113 float GetRotation() const;
114
115 /**
116 * @brief Gets the current zoom factor.
117 * @return Zoom factor.
118 */
119 float GetZoom() const;
120
121 /**
122 * @brief Gets the world-view (model) matrix for the camera.
123 * @return Model/view matrix (glm::mat3).
124 * @note Use this to transform between world and screen space for rendering.
125 */
126 const glm::mat4& GetWorldView() const;
127
128protected:
130 float left, float right,
131 float top, float bottom,
132 float nearPlane, float farPlane) const;
133
134protected:
136 std::unique_ptr<SceneComponent2D> mSceneComponent;
137 glm::vec2 mViewportSize;
138};
139
140} // namespace rendering_engine
glm::mat4 ComputeOrthographicMatrix(float left, float right, float top, float bottom, float nearPlane, float farPlane) const
Definition camera_2d.cpp:92
glm::mat4 GetProjectionMatrix() const
Returns the current orthographic projection matrix.
Definition camera_2d.cpp:30
void SetRotation(float angleDegrees)
Sets the camera rotation angle in degrees (counterclockwise, around Z).
Definition camera_2d.cpp:50
void SetPosition(const glm::vec2 &position)
Sets the camera position.
Definition camera_2d.cpp:45
glm::vec2 GetPosition() const
Gets the camera position.
Definition camera_2d.cpp:72
std::unique_ptr< SceneComponent2D > mSceneComponent
void Reset()
Resets the camera to the default view (origin, zero rotation, unit zoom).
Definition camera_2d.cpp:60
void SetViewportSize(float width, float height)
Sets the viewport dimensions used to compute the projection matrix.
Definition camera_2d.cpp:25
void SetZoom(float zoom)
Sets the camera zoom (uniform scale).
Definition camera_2d.cpp:55
const glm::mat4 & GetWorldView() const
Gets the world-view (model) matrix for the camera.
Definition camera_2d.cpp:87
Camera2D(IApplication &app)
Constructs a 2D camera at the origin, with no rotation and unit zoom.
Definition camera_2d.cpp:8
virtual ~Camera2D()=default
Virtual destructor.
float GetRotation() const
Gets the camera rotation angle in degrees.
Definition camera_2d.cpp:77
virtual void Update(float deltaTime)
Updates the camera logic (no-op for now).
Definition camera_2d.cpp:21
float GetZoom() const
Gets the current zoom factor.
Definition camera_2d.cpp:82
virtual void Initialize()
Initializes the camera (calls Reset()).
Definition camera_2d.cpp:15
Defines a generic application interface for rendering-based programs.
Represents a 2D transformable scene component with position, rotation, and scale.
#define RE_API