LkEngine 0.1.2
 
Loading...
Searching...
No Matches
Camera.h
Go to the documentation of this file.
1
5#pragma once
6
8#include "LkEngine/Core/LObject/ObjectPtr.h"
9
10#include "CameraBase.h"
11
12#include "LkEngine/Core/Math/MathLibrary.h"
13#include "LkEngine/Core/Input/Keyboard.h"
14#include "LkEngine/Core/Input/Mouse.h"
15
16namespace LkEngine {
17
27 enum class ECameraAction : uint16_t
28 {
29 None = 0,
30 Pan = LK_BIT(0),
31 Rotate = LK_BIT(1),
32 Zoom = LK_BIT(2),
33 };
34 LK_ENUM_CLASS(ECameraAction);
35 LK_ENUM_RANGE_FLAGS_BY_FIRST_AND_LAST(ECameraAction, ECameraAction::None, ECameraAction::Zoom);
36
41 enum class ECameraModifier : int32_t
42 {
43 None = 0,
44 MouseEnabled = LK_BIT(1),
45 KeyboardEnabled = LK_BIT(2),
46 PitchLocked = LK_BIT(3),
47 YawLocked = LK_BIT(4),
48 Damping = LK_BIT(5),
49 };
50 LK_ENUM_CLASS(ECameraModifier);
51
59 class LCamera : public LObject
60 {
61 LK_DECLARE_MULTICAST_DELEGATE(FCameraProjectionChanged, const ECameraProjection);
62 LK_DECLARE_MULTICAST_DELEGATE(FCameraActivityChanged, LCamera*, const bool);
63
65 LK_DECLARE_MULTICAST_DELEGATE(FCameraInputModified); /* Keep? */
66
67 public:
68 LCamera();
69 LCamera(const glm::mat4& InProjection);
70 LCamera(const float InDegFov, const float InWidth, const float InHeight,
71 const float InNearP, const float InFarP);
72 virtual ~LCamera() = default;
73
74 virtual ECameraType GetType() const { return ECameraType::None; }
75 FORCEINLINE ECameraProjection GetProjectionType() const { return ProjectionType; }
76
77 FORCEINLINE bool IsActive() const { return bIsActive; }
78
79 FORCEINLINE void SetPerspective(const float InVerticalFovDeg, const float InNearClip = 0.1f, const float InFarClip = 1000.0f)
80 {
81 if ((ProjectionType != ECameraProjection::Perspective)
82 || (DegPerspectiveFov != InVerticalFovDeg)
83 || (PerspectiveNear != InNearClip)
84 || (PerspectiveFar != InFarClip))
85 {
86 ProjectionType = ECameraProjection::Perspective;
87 DegPerspectiveFov = InVerticalFovDeg;
88 PerspectiveNear = InNearClip;
89 PerspectiveFar = InFarClip;
90
91 OnCameraProjectionChanged.Broadcast(ECameraProjection::Perspective);
92 }
93 }
94
95 FORCEINLINE void SetOrthographic(const float InWidth, const float InHeight, const float InNearClip = -1.0f, const float InFarClip = 1.0f)
96 {
97 if ((ProjectionType != ECameraProjection::Perspective)
98 || (OrthographicNear != InNearClip)
99 || (OrthographicFar != InFarClip))
100 {
101 ProjectionType = ECameraProjection::Orthographic;
102 OrthographicNear = InNearClip;
103 OrthographicFar = InFarClip;
104
105 OnCameraProjectionChanged.Broadcast(ECameraProjection::Orthographic);
106 }
107 }
108
109 FORCEINLINE void SetProjectionType(ECameraProjection InProjection)
110 {
111 if (ProjectionType != InProjection)
112 {
113 ProjectionType = InProjection;
114 OnCameraProjectionChanged.Broadcast(ProjectionType);
115 }
116 }
117
118 FORCEINLINE void SetPitch(const float InPitch) { Pitch = InPitch; }
119 FORCEINLINE float GetPitch() const { return Pitch; }
120 FORCEINLINE float GetPitchDelta() const { return PitchDelta; }
121
122 FORCEINLINE void SetYaw(const float InYaw) { Yaw = InYaw; }
123 FORCEINLINE float GetYaw() const { return Yaw; }
124 FORCEINLINE float GetYawDelta() const { return YawDelta; }
125
126 FORCEINLINE void SetActive(const bool InActive)
127 {
128 if (bIsActive != InActive)
129 {
130 bIsActive = InActive;
131 OnCameraActivityChanged.Broadcast(this, bIsActive);
132 }
133 }
134
135 FORCEINLINE const glm::mat4& GetViewMatrix() const { return ViewMatrix; }
136 FORCEINLINE const glm::mat4& GetProjectionMatrix() const { return ProjectionMatrix; }
137 FORCEINLINE glm::mat4 GetViewProjection() const { return GetProjectionMatrix() * ViewMatrix; }
138 FORCEINLINE float GetRotation() { return glm::radians(Rotation); }
139 FORCEINLINE float GetRotationSpeed() const { return RotationSpeed; }
140
141 FORCEINLINE const glm::vec3& GetOrigin() const { return Origin; }
142 FORCEINLINE const glm::vec3& GetFocalPoint() const { return FocalPoint; }
143 FORCEINLINE const glm::vec3& GetDirection() const { return Direction; }
144
145 FORCEINLINE float GetPerspectiveNearClip() const { return PerspectiveNear; }
146 FORCEINLINE float GetPerspectiveFarClip() const { return PerspectiveFar; }
147 FORCEINLINE void SetPerspectiveNearClip(const float InNearClip) { PerspectiveNear = InNearClip; }
148 FORCEINLINE void SetPerspectiveFarClip(const float InFarClip) { PerspectiveFar = InFarClip; }
149
150 FORCEINLINE float GetOrthographicSize() const { return OrthographicSize; }
151 FORCEINLINE float GetOrthographicNearClip() const { return OrthographicNear; }
152 FORCEINLINE float GetOrthographicFarClip() const { return OrthographicFar; }
153 FORCEINLINE void SetOrthographicNearClip(const float InNearClip) { OrthographicNear = InNearClip; }
154 FORCEINLINE void SetOrthographicFarClip(const float InFarClip) { OrthographicFar = InFarClip; }
155
156 FORCEINLINE void SetProjectionMatrix(const glm::mat4& InProjection) { ProjectionMatrix = InProjection; }
157 FORCEINLINE void SetPerspectiveProjectionMatrix(const float InRadFov,
158 const float InWidth,
159 const float InHeight,
160 const float InNearP,
161 const float InFarP)
162 {
163 LK_CORE_ASSERT((InWidth > 0) && (InHeight > 0), "Cannot set projection matrix with invalid arguments");
164 ProjectionMatrix = glm::perspectiveFov(InRadFov, InWidth, InHeight, InNearP, InFarP);
165 }
166
167 FORCEINLINE void SetOrthoProjectionMatrix(const float InWidth, const float InHeight, const float InNearP, const float InFarP)
168 {
169 LK_CORE_ASSERT((InWidth > 0) && (InHeight > 0));
170 ProjectionMatrix = glm::ortho(
171 -(InWidth * 0.50f), (InWidth * 0.50f),
172 -(InHeight * 0.50f), (InHeight * 0.50f),
173 InNearP, InFarP);
174 }
175
176 template<enum EAngleUnit = EAngleUnit::Degree>
177 float GetPerspectiveFov() const;
178
179 template<enum EAngleUnit = EAngleUnit::Degree>
180 float GetPerspectiveVerticalFov() const;
181
182 FORCEINLINE glm::quat GetOrientation() const
183 {
184 return glm::quat(glm::vec3(-Pitch - PitchDelta, -Yaw - YawDelta, 0.0f));
185 }
186
187 FORCEINLINE glm::vec3 GetUpDirection() const
188 {
189 return glm::rotate(GetOrientation(), glm::vec3(0.0f, 1.0f, 0.0f));
190 }
191
192 FORCEINLINE glm::vec3 GetRightDirection() const
193 {
194 return glm::rotate(GetOrientation(), glm::vec3(1.0f, 0.0f, 0.0f));
195 }
196
197 template<typename TVector = glm::vec3>
198 TVector GetForwardDirection() const
199 {
200 return TVector(glm::rotate(GetOrientation(), glm::vec3(0.0f, 0.0f, -1.0f)));
201 }
202
203 void SetMouseEnabled(const bool InEnabled)
204 {
205 const bool MouseEnabled = (ModifierFlags & ECameraModifier::MouseEnabled);
206 if (MouseEnabled != InEnabled)
207 {
208 if (MouseEnabled)
209 {
210 ModifierFlags &= ~ECameraModifier::MouseEnabled;
211 }
212 else
213 {
214 ModifierFlags |= ECameraModifier::MouseEnabled;
215 }
216
217 OnCameraInputModified.Broadcast();
218 }
219 }
220
221 void SetKeyboardEnabled(const bool InEnabled)
222 {
223 const bool KeyboardEnabled = ModifierFlags & ECameraModifier::KeyboardEnabled;
224 if (KeyboardEnabled != InEnabled)
225 {
226 if (KeyboardEnabled)
227 {
228 ModifierFlags &= ~ECameraModifier::KeyboardEnabled;
229 }
230 else
231 {
232 ModifierFlags |= ECameraModifier::KeyboardEnabled;
233 }
234
235 OnCameraInputModified.Broadcast();
236 }
237 }
238
239 public:
246 inline static FCameraActivityChanged OnCameraActivityChanged;
247 FCameraProjectionChanged OnCameraProjectionChanged;
248 FCameraInputModified OnCameraInputModified;
249 protected:
250 bool bIsActive = false;
251 ECameraProjection ProjectionType = ECameraProjection::Perspective;
252
253 int32_t ModifierFlags = 0;
254 static_assert(std::is_same_v<decltype(ModifierFlags), std::underlying_type_t<ECameraModifier>>);
255
256 glm::vec3 Origin = { 0.0f, 0.0f, 0.0f };
257 glm::vec3 Direction{};
258 glm::vec3 FocalPoint{};
259 glm::vec3 PositionDelta = { 0.0f, 0.0f, 0.0f };
260
261 float Pitch = 0.0f;
262 float PitchDelta = 0.0f;
263 float Yaw = 0.0f;
264 float YawDelta = 0.0f;
265
266 float AspectRatio = (16.0f / 9.0f);
267
268 float DegPerspectiveFov = 45.0f;
269 float PerspectiveNear = 0.1f;
270 float PerspectiveFar = 1000.0f;
271
272 float OrthographicSize = 10.0f;
273 float OrthographicNear = -1.0f;
274 float OrthographicFar = 1.0f;
275
276 float Rotation = 0.0f;
277 float RotationSpeed = 0.280f;
278
279 glm::mat4 ViewMatrix = glm::mat4(1.0f);
280 glm::mat4 ProjectionMatrix = glm::mat4(1.0f);
281
282 private:
283 friend class LEditorLayer;
284 friend class LSceneSerializer;
285
286 LCLASS(LCamera);
287 };
288
289 template<>
290 inline float LCamera::GetPerspectiveFov<EAngleUnit::Degree>() const
291 {
292 return DegPerspectiveFov;
293 }
294
295 template<>
296 inline float LCamera::GetPerspectiveFov<EAngleUnit::Radian>() const
297 {
298 return glm::radians(DegPerspectiveFov);
299 }
300
301 template<>
302 inline float LCamera::GetPerspectiveVerticalFov<EAngleUnit::Degree>() const
303 {
304 return DegPerspectiveFov;
305 }
306
307 template<>
308 inline float LCamera::GetPerspectiveVerticalFov<EAngleUnit::Radian>() const
309 {
310 return glm::radians(DegPerspectiveFov);
311 }
312
315}
LObject implementation.
Definition Camera.h:60
static FCameraActivityChanged OnCameraActivityChanged
Definition Camera.h:246
Definition Object.h:46
#define LCLASS(Class)
Definition CoreMacros.h:226
ECameraAction
Definition Camera.h:28
ECameraModifier
Definition Camera.h:42
Definition Asset.h:11