SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Camera.hpp
Go to the documentation of this file.
1#ifndef _CAMERA_HPP_
2#define _CAMERA_HPP_
3
5#include <Core/GameObject.hpp>
7#include <Math/Matrix.hpp>
8#include <Math/Vector.hpp>
9
10namespace Sleak {
11 /// Perspective or orthographic projection mode for a Camera.
12 /// @ingroup camera
14
15 /// GameObject that owns the engine's single active view/projection
16 /// matrices and view frustum. Position/orientation come from CameraController subclasses.
17 ///
18 /// Create one, add it to the scene, and call
19 /// SceneBase::SetActiveCamera() so the renderer knows which view to
20 /// draw from. Because Camera is a GameObject, it also carries
21 /// components: attach a FreeLookCameraController for a debug or
22 /// spectator view, or a FirstPersonController plus a
23 /// ColliderComponent and RigidbodyComponent for a player character
24 /// that collides with the world.
25 ///
26 /// The view and projection matrices are static, shared state. The
27 /// active camera rewrites them each Update() through
28 /// GetMainViewMatrix(), GetMainProjectionMatrix(),
29 /// GetMainCameraPosition(), and GetMainViewFrustum(), which is how the
30 /// renderer and CullingSystem read the current view. Only one camera
31 /// should be active at a time.
32 ///
33 /// Orientation is expressed as a look target rather than a rotation.
34 /// SetLookTarget() aims at a world point and SetDirection() aims along
35 /// a vector; GetDirection() returns the normalized facing.
36 ///
37 /// @code{.cpp}
38 /// auto* cam = new Sleak::Camera(
39 /// "MainCamera",
40 /// Sleak::Math::Vector3D(-5.0f, 3.0f, -5.0f),
41 /// /*fov=*/60.0f, /*near=*/0.01f, /*far=*/200.0f);
42 ///
43 /// cam->SetLookTarget(Sleak::Math::Vector3D(0.0f, 0.0f, 0.0f));
44 /// cam->AddComponent<Sleak::FreeLookCameraController>();
45 /// cam->Initialize();
46 ///
47 /// if (auto* ctrl =
48 /// cam->GetComponent<Sleak::FreeLookCameraController>()) {
49 /// ctrl->SetEnabled(true);
50 /// }
51 ///
52 /// AddObject(cam);
53 /// SetActiveCamera(cam);
54 /// @endcode
55 ///
56 /// @see CameraController, FreeLookCameraController,
57 /// FirstPersonController, ViewFrustum, CullingSystem
58 /// @ingroup camera
59 class ENGINE_API Camera : public GameObject {
60 public:
61 Camera(std::string name = "Camera",
63 float fov = 60, float near = 1.0f, float far = 1000.0f);
64
65 void Initialize() override;
66 /// Recalculates the view/projection matrices and frustum while active.
67 void Update(float DeltaTime) override;
68
69 void SetFieldOfView(float fov) {fieldOfView = fov;}
70 float GetFieldOfView() const {return fieldOfView;}
71
72 void SetNearPlane(float nearPlaneValue) {nearPlane = nearPlaneValue;}
73 float GetNearPlane() const {return nearPlane;}
74
75 void SetFarPlane(float farPlaneValue) {farPlane = farPlaneValue;}
76 float GetFarPlane() const {return farPlane;}
77
78 void SetPosition(const Math::Vector3D& position) {Position = position;}
79 void AddPosition(const Math::Vector3D& position) {Position += position;}
81
83 void SetDirection(const Math::Vector3D& direction) {
84 LookTarget = Position + direction.Normalized();
85 }
86 void AddDirection(const Math::Vector3D& direction) {
87 LookTarget += Position + direction.Normalized();
88 }
89
90 void SetLookTarget(const Math::Vector3D& target) {LookTarget = target;}
91 void AddLookTarget(const Math::Vector3D& target) { LookTarget += target; }
93
94 void SetUp(const Math::Vector3D& up) {Up = up;}
95 Math::Vector3D GetUp() const {return Up;}
96
99
100 /// Updates the viewport dimensions and recomputes the projection matrix.
101 void OnResize(uint32_t width, uint32_t height);
102
104 return Camera::View;
105 }
106
108 return Camera::Projection;
109 }
110
113 }
114
116 return Camera::s_frustum;
117 }
118
119 protected:
120 /// Rebuilds the static view matrix and view frustum from position/target/up, and begins culling for the frame.
121 void RecalculateViewMatrix();
122 /// Rebuilds the static projection matrix from FOV/aspect/near/far or the orthographic extents.
123 void RecalculateProjectionMatrix();
124
127 float farPlane;
128 float width;
129 float height;
130
132
136
141 };
142}
143
144#endif
int width
int height
void SetFieldOfView(float fov)
Definition Camera.hpp:69
void SetNearPlane(float nearPlaneValue)
Definition Camera.hpp:72
static const ViewFrustum & GetMainViewFrustum()
Definition Camera.hpp:115
void SetPosition(const Math::Vector3D &position)
Definition Camera.hpp:78
Math::Vector3D GetPosition() const
Definition Camera.hpp:80
float GetFieldOfView() const
Definition Camera.hpp:70
static const Math::Matrix4 & GetMainProjectionMatrix()
Definition Camera.hpp:107
void Update(float DeltaTime) override
Recalculates the view/projection matrices and frustum while active.
Definition Camera.cpp:31
static Math::Matrix4 Projection
Definition Camera.hpp:138
float GetFarPlane() const
Definition Camera.hpp:76
Math::Vector3D GetLookTarget() const
Definition Camera.hpp:92
void AddDirection(const Math::Vector3D &direction)
Definition Camera.hpp:86
Math::Vector3D GetUp() const
Definition Camera.hpp:95
static const Math::Vector3D & GetMainCameraPosition()
Definition Camera.hpp:111
void SetFarPlane(float farPlaneValue)
Definition Camera.hpp:75
Math::Vector3D GetDirection() const
Definition Camera.hpp:82
ProjectionType GetProjectionType() const
Definition Camera.hpp:98
void SetProjectionType(ProjectionType type)
Definition Camera.hpp:97
void SetDirection(const Math::Vector3D &direction)
Definition Camera.hpp:83
float farPlane
Definition Camera.hpp:127
static ViewFrustum s_frustum
Definition Camera.hpp:140
static Math::Matrix4 View
Definition Camera.hpp:137
static Math::Vector3D MainPosition
Definition Camera.hpp:139
Math::Vector3D Position
Definition Camera.hpp:133
float GetNearPlane() const
Definition Camera.hpp:73
float nearPlane
Definition Camera.hpp:126
ProjectionType type
Definition Camera.hpp:131
void AddLookTarget(const Math::Vector3D &target)
Definition Camera.hpp:91
float fieldOfView
Definition Camera.hpp:125
static const Math::Matrix4 & GetMainViewMatrix()
Definition Camera.hpp:103
void SetUp(const Math::Vector3D &up)
Definition Camera.hpp:94
Math::Vector3D LookTarget
Definition Camera.hpp:134
void SetLookTarget(const Math::Vector3D &target)
Definition Camera.hpp:90
Math::Vector3D Up
Definition Camera.hpp:135
Camera(std::string name="Camera", Math::Vector3D Position=Math::Vector3D(0, 0, -3.5), float fov=60, float near=1.0f, float far=1000.0f)
Definition Camera.cpp:14
void Initialize() override
Initializes the object and its components; called once before the first Update.
Definition Camera.cpp:24
void AddPosition(const Math::Vector3D &position)
Definition Camera.hpp:79
GameObject(const std::string &name="GameObject")
Vector3D Normalized() const
Definition Vector.hpp:454
static Vector3D Up()
Definition Vector.hpp:497
ProjectionType
Definition Camera.hpp:13
Matrix< float, 4, 4 > Matrix4
Definition Matrix.hpp:413
Root namespace for everything the engine exposes.
Definition Camera.hpp:10