Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
camera_2d.cpp
Go to the documentation of this file.
1#include "camera_2d.hpp"
3#include "i_application.hpp"
4
5namespace rendering_engine
6{
7
9 :
10 mApp(app),
11 mSceneComponent(std::make_unique<SceneComponent2D>()),
12 mViewportSize(glm::vec2(0.0f, 0.0f))
13{}
14
16{
17 Reset();
18
19}
20
21void Camera2D::Update(float deltaTime)
22{
23}
24
25void Camera2D::SetViewportSize(float width, float height)
26{
27 mViewportSize = { width, height };
28}
29
31{
32 float halfWidth = mViewportSize.x * 0.5f;
33 float halfHeight = mViewportSize.y * 0.5f;
34
35 float left = -halfWidth;
36 float right = halfWidth;
37 float bottom = -halfHeight;
38 float top = halfHeight;
39 float near = -1.0f;
40 float far = 1.0f;
41
42 return glm::ortho(left, right, bottom, top, near, far);
43}
44
45void Camera2D::SetPosition(const glm::vec2& position)
46{
47 mSceneComponent->SetPosition(position);
48}
49
50void Camera2D::SetRotation(float angleDegrees)
51{
52 mSceneComponent->SetRotation(angleDegrees);
53}
54
55void Camera2D::SetZoom(float zoom)
56{
57 mSceneComponent->SetScale(glm::vec2(zoom, zoom));
58}
59
61{
62 const glm::vec2 startPos = glm::vec2(0.0f, 0.0f);
63 const float startRot = 0.0f;
64 const float startZoom = 1.0f;
65 mSceneComponent->SetPosition(startPos);
66 mSceneComponent->SetRotation(startRot);
67 mSceneComponent->SetScale(glm::vec2(startZoom, startZoom));
68
69 SetViewportSize(mApp.GetScreenSettings().width, mApp.GetScreenSettings().height);
70}
71
72glm::vec2 Camera2D::GetPosition() const
73{
74 return mSceneComponent->GetPosition();
75}
76
78{
79 return mSceneComponent->GetRotation();
80}
81
82float Camera2D::GetZoom() const
83{
84 return mSceneComponent->GetScale().x;
85}
86
87const glm::mat4& Camera2D::GetWorldView() const
88{
89 return mSceneComponent->GetModelMatrix();
90}
91
92glm::mat4 Camera2D::ComputeOrthographicMatrix(float left, float right, float top, float bottom, float nearPlane, float farPlane) const
93{
94 glm::mat4 result(1.0f);
95
96 result[0][0] = 2.0f / (right - left);
97 result[1][1] = 2.0f / (bottom - top);
98 result[2][2] = 1.0f / (farPlane - nearPlane);
99
100 result[3][0] = -(right + left) / (right - left);
101 result[3][1] = -(bottom + top) / (bottom - top);
102 result[3][2] = -nearPlane / (farPlane - nearPlane);
103
104 return result;
105}
106
107} // 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
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.