SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
Camera.hpp
Go to the documentation of this file.
1
#ifndef _CAMERA_HPP_
2
#define _CAMERA_HPP_
3
4
#include <
Utility/Exception.hpp
>
5
#include <
Core/GameObject.hpp
>
6
#include <
Camera/ViewFrustum.hpp
>
7
#include <
Math/Matrix.hpp
>
8
#include <
Math/Vector.hpp
>
9
10
namespace
Sleak
{
11
/// Perspective or orthographic projection mode for a Camera.
12
/// @ingroup camera
13
enum class
ProjectionType
{
Perspective
,
Orthographic
};
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"
,
62
Math::Vector3D
Position
=
Math::Vector3D
(0, 0, -3.5),
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;}
80
Math::Vector3D
GetPosition
()
const
{
return
Position
;}
81
82
Math::Vector3D
GetDirection
()
const
{
return
(
LookTarget
-
Position
).
Normalized
(); }
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; }
92
Math::Vector3D
GetLookTarget
()
const
{
return
LookTarget
;}
93
94
void
SetUp
(
const
Math::Vector3D
& up) {
Up
= up;}
95
Math::Vector3D
GetUp
()
const
{
return
Up
;}
96
97
void
SetProjectionType
(
ProjectionType
type
) {this->type =
type
;}
98
ProjectionType
GetProjectionType
()
const
{
return
type
;}
99
100
/// Updates the viewport dimensions and recomputes the projection matrix.
101
void
OnResize(uint32_t
width
, uint32_t
height
);
102
103
static
const
Math::Matrix4
&
GetMainViewMatrix
() {
104
return
Camera::View
;
105
}
106
107
static
const
Math::Matrix4
&
GetMainProjectionMatrix
() {
108
return
Camera::Projection
;
109
}
110
111
static
const
Math::Vector3D
&
GetMainCameraPosition
() {
112
return
Camera::MainPosition
;
113
}
114
115
static
const
ViewFrustum
&
GetMainViewFrustum
() {
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
125
float
fieldOfView
;
126
float
nearPlane
;
127
float
farPlane
;
128
float
width
;
129
float
height
;
130
131
ProjectionType
type
=
ProjectionType::Perspective
;
132
133
Math::Vector3D
Position
;
134
Math::Vector3D
LookTarget
;
135
Math::Vector3D
Up
=
Math::Vector3D::Up
();
136
137
static
Math::Matrix4
View
;
138
static
Math::Matrix4
Projection
;
139
static
Math::Vector3D
MainPosition
;
140
static
ViewFrustum
s_frustum
;
141
};
142
}
143
144
#endif
width
int width
Definition
Application.cpp:40
height
int height
Definition
Application.cpp:41
Exception.hpp
GameObject.hpp
Matrix.hpp
Vector.hpp
ViewFrustum.hpp
Sleak::Camera::SetFieldOfView
void SetFieldOfView(float fov)
Definition
Camera.hpp:69
Sleak::Camera::SetNearPlane
void SetNearPlane(float nearPlaneValue)
Definition
Camera.hpp:72
Sleak::Camera::GetMainViewFrustum
static const ViewFrustum & GetMainViewFrustum()
Definition
Camera.hpp:115
Sleak::Camera::SetPosition
void SetPosition(const Math::Vector3D &position)
Definition
Camera.hpp:78
Sleak::Camera::height
float height
Definition
Camera.hpp:129
Sleak::Camera::GetPosition
Math::Vector3D GetPosition() const
Definition
Camera.hpp:80
Sleak::Camera::GetFieldOfView
float GetFieldOfView() const
Definition
Camera.hpp:70
Sleak::Camera::GetMainProjectionMatrix
static const Math::Matrix4 & GetMainProjectionMatrix()
Definition
Camera.hpp:107
Sleak::Camera::Update
void Update(float DeltaTime) override
Recalculates the view/projection matrices and frustum while active.
Definition
Camera.cpp:31
Sleak::Camera::Projection
static Math::Matrix4 Projection
Definition
Camera.hpp:138
Sleak::Camera::GetFarPlane
float GetFarPlane() const
Definition
Camera.hpp:76
Sleak::Camera::GetLookTarget
Math::Vector3D GetLookTarget() const
Definition
Camera.hpp:92
Sleak::Camera::AddDirection
void AddDirection(const Math::Vector3D &direction)
Definition
Camera.hpp:86
Sleak::Camera::GetUp
Math::Vector3D GetUp() const
Definition
Camera.hpp:95
Sleak::Camera::GetMainCameraPosition
static const Math::Vector3D & GetMainCameraPosition()
Definition
Camera.hpp:111
Sleak::Camera::SetFarPlane
void SetFarPlane(float farPlaneValue)
Definition
Camera.hpp:75
Sleak::Camera::GetDirection
Math::Vector3D GetDirection() const
Definition
Camera.hpp:82
Sleak::Camera::GetProjectionType
ProjectionType GetProjectionType() const
Definition
Camera.hpp:98
Sleak::Camera::SetProjectionType
void SetProjectionType(ProjectionType type)
Definition
Camera.hpp:97
Sleak::Camera::SetDirection
void SetDirection(const Math::Vector3D &direction)
Definition
Camera.hpp:83
Sleak::Camera::farPlane
float farPlane
Definition
Camera.hpp:127
Sleak::Camera::s_frustum
static ViewFrustum s_frustum
Definition
Camera.hpp:140
Sleak::Camera::View
static Math::Matrix4 View
Definition
Camera.hpp:137
Sleak::Camera::width
float width
Definition
Camera.hpp:128
Sleak::Camera::MainPosition
static Math::Vector3D MainPosition
Definition
Camera.hpp:139
Sleak::Camera::Position
Math::Vector3D Position
Definition
Camera.hpp:133
Sleak::Camera::GetNearPlane
float GetNearPlane() const
Definition
Camera.hpp:73
Sleak::Camera::nearPlane
float nearPlane
Definition
Camera.hpp:126
Sleak::Camera::type
ProjectionType type
Definition
Camera.hpp:131
Sleak::Camera::AddLookTarget
void AddLookTarget(const Math::Vector3D &target)
Definition
Camera.hpp:91
Sleak::Camera::fieldOfView
float fieldOfView
Definition
Camera.hpp:125
Sleak::Camera::GetMainViewMatrix
static const Math::Matrix4 & GetMainViewMatrix()
Definition
Camera.hpp:103
Sleak::Camera::SetUp
void SetUp(const Math::Vector3D &up)
Definition
Camera.hpp:94
Sleak::Camera::LookTarget
Math::Vector3D LookTarget
Definition
Camera.hpp:134
Sleak::Camera::SetLookTarget
void SetLookTarget(const Math::Vector3D &target)
Definition
Camera.hpp:90
Sleak::Camera::Up
Math::Vector3D Up
Definition
Camera.hpp:135
Sleak::Camera::Camera
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
Sleak::Camera::Initialize
void Initialize() override
Initializes the object, components and children; repeats no-op.
Definition
Camera.cpp:24
Sleak::Camera::AddPosition
void AddPosition(const Math::Vector3D &position)
Definition
Camera.hpp:79
Sleak::GameObject::GameObject
GameObject(const std::string &name="GameObject")
Definition
GameObject.hpp:64
Sleak::Math::Vector3D
Definition
Vector.hpp:354
Sleak::Math::Vector3D::Normalized
Vector3D Normalized() const
Definition
Vector.hpp:455
Sleak::Math::Vector3D::Up
static Vector3D Up()
Definition
Vector.hpp:498
Sleak::ViewFrustum
Definition
ViewFrustum.hpp:35
Sleak::ProjectionType
ProjectionType
Definition
Camera.hpp:13
Sleak::ProjectionType::Orthographic
@ Orthographic
Definition
Camera.hpp:13
Sleak::ProjectionType::Perspective
@ Perspective
Definition
Camera.hpp:13
Sleak::Math::Matrix4
Matrix< float, 4, 4 > Matrix4
Definition
Matrix.hpp:413
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
include
public
Camera
Camera.hpp
Generated on
for SleakEngine by
1.18.0