Rendering Engine 0.2.0
Modular Graphics Rendering Engine | v0.2.0
Loading...
Searching...
No Matches
rendering_engine::Camera2D Class Reference

Represents a 2D camera with position, rotation, and zoom control. More...

#include <camera_2d.hpp>

Public Member Functions

 Camera2D (IApplication &app)
 Constructs a 2D camera at the origin, with no rotation and unit zoom.
virtual ~Camera2D ()=default
 Virtual destructor.
virtual void Initialize ()
 Initializes the camera (calls Reset()).
virtual void Update (float deltaTime)
 Updates the camera logic (no-op for now).
void SetViewportSize (float width, float height)
 Sets the viewport dimensions used to compute the projection matrix.
glm::mat4 GetProjectionMatrix () const
 Returns the current orthographic projection matrix.
void SetPosition (const glm::vec2 &position)
 Sets the camera position.
void SetRotation (float angleDegrees)
 Sets the camera rotation angle in degrees (counterclockwise, around Z).
void SetZoom (float zoom)
 Sets the camera zoom (uniform scale).
void Reset ()
 Resets the camera to the default view (origin, zero rotation, unit zoom).
glm::vec2 GetPosition () const
 Gets the camera position.
float GetRotation () const
 Gets the camera rotation angle in degrees.
float GetZoom () const
 Gets the current zoom factor.
const glm::mat4 & GetWorldView () const
 Gets the world-view (model) matrix for the camera.

Protected Member Functions

glm::mat4 ComputeOrthographicMatrix (float left, float right, float top, float bottom, float nearPlane, float farPlane) const

Protected Attributes

IApplicationmApp
std::unique_ptr< SceneComponent2DmSceneComponent
glm::vec2 mViewportSize

Detailed Description

Represents a 2D camera with position, rotation, and zoom control.

Camera2D defines the view in a 2D scene, using a SceneComponent2D for transform (position, rotation, zoom). The camera provides basic pan, rotation, and uniform zoom features.

The engine uses a left-handed coordinate system:

  • X+ : Right
  • Y+ : Up Rotation is counterclockwise, in degrees, around the Z axis (out of the screen).

By default, the camera is at the origin, with zero rotation and unit zoom.

See also
rendering_engine::SceneComponent2D

Definition at line 35 of file camera_2d.hpp.

Constructor & Destructor Documentation

◆ Camera2D()

rendering_engine::Camera2D::Camera2D ( IApplication & app)

Constructs a 2D camera at the origin, with no rotation and unit zoom.

Definition at line 8 of file camera_2d.cpp.

9 :
10 mApp(app),
11 mSceneComponent(std::make_unique<SceneComponent2D>()),
12 mViewportSize(glm::vec2(0.0f, 0.0f))
13{}
std::unique_ptr< SceneComponent2D > mSceneComponent

◆ ~Camera2D()

virtual rendering_engine::Camera2D::~Camera2D ( )
virtualdefault

Virtual destructor.

Member Function Documentation

◆ ComputeOrthographicMatrix()

glm::mat4 rendering_engine::Camera2D::ComputeOrthographicMatrix ( float left,
float right,
float top,
float bottom,
float nearPlane,
float farPlane ) const
protected

Definition at line 92 of file camera_2d.cpp.

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}

◆ GetPosition()

glm::vec2 rendering_engine::Camera2D::GetPosition ( ) const

Gets the camera position.

Returns
2D position (glm::vec2).

Definition at line 72 of file camera_2d.cpp.

73{
74 return mSceneComponent->GetPosition();
75}

◆ GetProjectionMatrix()

glm::mat4 rendering_engine::Camera2D::GetProjectionMatrix ( ) const

Returns the current orthographic projection matrix.

The matrix is built from the camera�s viewport size, and is typically multiplied with the view (world) matrix to transform coordinates into clip space.

Returns
4�4 orthographic projection matrix.

Definition at line 30 of file camera_2d.cpp.

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}

◆ GetRotation()

float rendering_engine::Camera2D::GetRotation ( ) const

Gets the camera rotation angle in degrees.

Returns
Rotation angle (degrees).

Definition at line 77 of file camera_2d.cpp.

78{
79 return mSceneComponent->GetRotation();
80}

◆ GetWorldView()

const glm::mat4 & rendering_engine::Camera2D::GetWorldView ( ) const

Gets the world-view (model) matrix for the camera.

Returns
Model/view matrix (glm::mat3).
Note
Use this to transform between world and screen space for rendering.

Definition at line 87 of file camera_2d.cpp.

88{
89 return mSceneComponent->GetModelMatrix();
90}

◆ GetZoom()

float rendering_engine::Camera2D::GetZoom ( ) const

Gets the current zoom factor.

Returns
Zoom factor.

Definition at line 82 of file camera_2d.cpp.

83{
84 return mSceneComponent->GetScale().x;
85}

◆ Initialize()

void rendering_engine::Camera2D::Initialize ( )
virtual

Initializes the camera (calls Reset()).

Definition at line 15 of file camera_2d.cpp.

16{
17 Reset();
18
19}
void Reset()
Resets the camera to the default view (origin, zero rotation, unit zoom).
Definition camera_2d.cpp:60

◆ Reset()

void rendering_engine::Camera2D::Reset ( )

Resets the camera to the default view (origin, zero rotation, unit zoom).

Definition at line 60 of file camera_2d.cpp.

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}
void SetViewportSize(float width, float height)
Sets the viewport dimensions used to compute the projection matrix.
Definition camera_2d.cpp:25

◆ SetPosition()

void rendering_engine::Camera2D::SetPosition ( const glm::vec2 & position)

Sets the camera position.

Parameters
position2D position (glm::vec2).

Definition at line 45 of file camera_2d.cpp.

46{
47 mSceneComponent->SetPosition(position);
48}

◆ SetRotation()

void rendering_engine::Camera2D::SetRotation ( float angleDegrees)

Sets the camera rotation angle in degrees (counterclockwise, around Z).

Parameters
angleDegreesAngle in degrees.

Definition at line 50 of file camera_2d.cpp.

51{
52 mSceneComponent->SetRotation(angleDegrees);
53}

◆ SetViewportSize()

void rendering_engine::Camera2D::SetViewportSize ( float width,
float height )

Sets the viewport dimensions used to compute the projection matrix.

This defines the visible area of the camera in screen-space units. The projection matrix will be recomputed based on these dimensions.

Parameters
widthViewport width in pixels or world units.
heightViewport height in pixels or world units.

Definition at line 25 of file camera_2d.cpp.

26{
27 mViewportSize = { width, height };
28}

◆ SetZoom()

void rendering_engine::Camera2D::SetZoom ( float zoom)

Sets the camera zoom (uniform scale).

Parameters
zoomZoom factor (1.0 = no zoom).

Definition at line 55 of file camera_2d.cpp.

56{
57 mSceneComponent->SetScale(glm::vec2(zoom, zoom));
58}

◆ Update()

void rendering_engine::Camera2D::Update ( float deltaTime)
virtual

Updates the camera logic (no-op for now).

Parameters
deltaTimeTime since last update (seconds).

Definition at line 21 of file camera_2d.cpp.

22{
23}

Member Data Documentation

◆ mApp

IApplication& rendering_engine::Camera2D::mApp
protected

Definition at line 135 of file camera_2d.hpp.

◆ mSceneComponent

std::unique_ptr<SceneComponent2D> rendering_engine::Camera2D::mSceneComponent
protected

Definition at line 136 of file camera_2d.hpp.

◆ mViewportSize

glm::vec2 rendering_engine::Camera2D::mViewportSize
protected

Definition at line 137 of file camera_2d.hpp.


The documentation for this class was generated from the following files: