SpiecsEngine
 
Loading...
Searching...
No Matches
CameraController.cpp
Go to the documentation of this file.
1/**
2* @file CameraController.cpp.
3* @brief The CameraController Class Implementation.
4* @author The Cherno & Spices.
5*/
6
7#include "Pchheader.h"
9#include "World/Entity.h"
10#include "Render/FrameInfo.h"
11#include "Core/Input/Input.h"
12#include "Core/Input/MouseButtonCodes.h"
13#include "Systems/SlateSystem.h"
14#include "Core/Input/KeyCodes.h"
15
16namespace Spices {
17
19 {
21
22 Entity entity( m_Owner, FrameInfo::Get().m_World.get());
23 m_CameraTranComp = &entity.GetComponent<TransformComponent>();
24 m_Camera = entity.GetComponent<CameraComponent>().GetCamera();
25 }
26
28 {
30
31 m_Camera->IncreaseStableFrames();
32
33 /**
34 * @breif Only update view while viewport is hovered.
35 */
36 if (!SlateSystem::GetRegister()->GetViewPort()->IsHovered()) return;
37
39 {
40 const glm::vec2& mouse { Input::GetMouseX(), Input::GetMouseY() };
41 const glm::vec2 delta = (mouse - m_InitialMousePosition) * 0.003f;
42 m_InitialMousePosition = mouse;
43
45 {
46 MousePan(delta);
47 m_Camera->ResetStableFrames();
48 }
50 {
51 MouseRotate(delta);
52 m_Camera->ResetStableFrames();
53 }
55 {
56 MouseZoom(delta.y);
57 m_Camera->ResetStableFrames();
58 }
59
61 }
62 }
63
65 {
66 EventDispatcher dispatcher(e);
67
71 }
72
74 {
76
77 return false;
78 }
79
81 {
83
84 if (!SlateSystem::GetRegister()->GetViewPort()->IsHovered()) return false;
85
86 const float ratio = static_cast<float>(m_ViewportWidth) / static_cast<float>(m_ViewportHeight);
87
88 const float delta = e.GetYOffset() * 0.1f;
89
90 m_ZoomLevel += delta;
91 m_ZoomLevel = std::max(m_ZoomLevel, 0.2f);
92
93 if (m_Camera->GetProjectionType() == ProjectionType::Orthographic)
94 {
95 m_Camera->SetOrthographic(-ratio * m_ZoomLevel, ratio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel, 0.001f, 100000.0f);
96 }
97
98 MouseZoom(delta);
100 m_Camera->ResetStableFrames();
101 return false;
102 }
103
105 {
107
110 const float ratio = static_cast<float>(m_ViewportWidth) / static_cast<float>(m_ViewportHeight);
111
112 switch (m_Camera->GetProjectionType())
113 {
114 case ProjectionType::Perspective:
115 m_Camera->SetPerspective(ratio);
116 break;
117 case ProjectionType::Orthographic:
118 m_Camera->SetOrthographic(-ratio * m_ZoomLevel, ratio * m_ZoomLevel, -m_ZoomLevel, m_ZoomLevel, 0.001f, 100000.0f);
119 break;
120 }
121
122 m_Camera->ResetStableFrames();
123 return false;
124 }
125
126 void CameraController::MousePan(const glm::vec2& delta)
127 {
129
130 auto [xSpeed, ySpeed] = PanSpeed();
131 m_FocalPoint += -GetRightDirection() * delta.x * xSpeed * m_Distance;
132 m_FocalPoint += -GetUpDirection() * delta.y * ySpeed * m_Distance;
133 }
134
135 void CameraController::MouseRotate(const glm::vec2& delta) const
136 {
138
139 glm::vec3 rot = m_CameraTranComp->GetRotation();
140 const float yawSign = GetUpDirection().y < 0 ? -1.0f : 1.0f;
141
142 rot.y -= glm::degrees(yawSign * delta.x * RotationSpeed());
143 rot.x += glm::degrees(delta.y * RotationSpeed());
144
145 m_CameraTranComp->SetRotation(rot);
146 }
147
148 void CameraController::MouseZoom(const float& delta)
149 {
151
152 m_Distance += delta * ZoomSpeed();
153 if (m_Distance < 1.0f)
154 {
155 m_FocalPoint += GetForwardDirection();
156 m_Distance = 1.0f;
157 }
158 }
159
160 std::pair<float, float> CameraController::PanSpeed() const
161 {
163
164 const float x = std::min(static_cast<float>(m_ViewportWidth) / 1000.0f, 2.4f); // max = 2.4f
165 float xFactor = 0.0366f * (x * x) - 0.1778f * x + 0.3021f;
166
167 const float y = std::min(static_cast<float>(m_ViewportHeight) / 1000.0f, 2.4f); // max = 2.4f
168 float yFactor = 0.0366f * (y * y) - 0.1778f * y + 0.3021f;
169
170 return { xFactor, yFactor };
171 }
172
174 {
176
177 return 0.8f;
178 }
179
181 {
183
184 float distance = m_Distance * 0.2f;
185 distance = std::max(distance, 0.0f);
186 float speed = distance * distance;
187 speed = std::min(speed, 100.0f); // max speed = 100
188 return speed;
189 }
190
192 {
194
195 const glm::vec3 pos = CalculatePosition();
196 m_CameraTranComp->SetPosition(pos);
197 // m_Yaw = m_Pitch = 0.0f; // Lock the camera's rotation
198 }
199
201 {
203
204 return m_FocalPoint - GetForwardDirection() * m_Distance;
205 }
206
208 {
210
211 return glm::rotate(GetOrientation(), glm::vec3(0.0f, -1.0f, 0.0f));
212 }
213
215 {
217
218 return glm::rotate(GetOrientation(), glm::vec3(1.0f, 0.0f, 0.0f));
219 }
220
222 {
224
225 return glm::rotate(GetOrientation(), glm::vec3(0.0f, 0.0f, 1.0f));
226 }
227
229 {
231
232 const glm::vec3& rot = m_CameraTranComp->GetRotation();
233 return glm::quat({ glm::radians(rot.x), glm::radians(rot.y), glm::radians(rot.z) });
234 }
235}
#define BIND_EVENT_FN(x)
Bind Event.
Definition Event.h:88
#define SPICES_PROFILE_ZONE
virtual void OnEvent(Event &e) override
The interface inherited from NativeScriptComponent, which is called on the global event function poin...
glm::vec3 GetForwardDirection() const
Get Camera Forward(z) Direction.
uint32_t m_ViewportWidth
The Viewport size.
glm::vec3 GetUpDirection() const
Get Camera Up(y) Direction.
bool OnKeyPressed(KeyPressedEvent &e)
Event OnKeyPressed. We do nothing here.
bool OnMouseScroll(MouseScrolledEvent &e)
Event OnMouseScroll. Scale the camera.
void MousePan(const glm::vec2 &delta)
Calculate Camera Drag.
void MouseZoom(const float &delta)
Calculate Camera Zoom.
void UpdateView() const
Setting camera transform component position. We already set rotation in MouseRotate().
std::pair< float, float > PanSpeed() const
Calculate Camera Drag speed.
glm::quat GetOrientation() const
Get Camera quaternion rotation.
void MouseRotate(const glm::vec2 &delta) const
Calculate Camera Rotate.
virtual void OnConstruction() override
The interface inherited from NativeScriptComponent, which is called on this attached to an entity....
bool OnSlateResized(SlateResizeEvent &e)
Event OnWindowResized. Reset Camera aspect ratio.
float ZoomSpeed() const
Calculate Camera Zoom speed.
float m_Distance
The camera spring arm. Init with 10.
glm::vec3 GetRightDirection() const
Get Camera Right(x) Direction.
TransformComponent * m_CameraTranComp
The camera transform component pointer get from owner's transform component.
glm::vec3 CalculatePosition() const
Calculate camera transform component position using the class member parameters.
virtual void OnTick(TimeStep &ts) override
The interface inherited from NativeScriptComponent, which is called every engine loop frame....
float RotationSpeed() const
Calculate Camera Rotate speed.
float m_ZoomLevel
Zoom Level, for orthographic type camera.
CameraController Class. This class inherit from NativeScriptComponent. It receives Mouse Event and Ke...
Entity Class. This class defines the specific behaves of Entity.
Definition Entity.h:20
EventDispatcher(Event &event)
Constructor Function.
Definition Event.h:172
This Class store a Specific Event type first and Dispatch a event handle function to it.
Definition Event.h:156
This Class is the basic Event Class. Inherit from it and create specific event class.
Definition Event.h:96
static bool IsKeyPressed(const int &keycode)
Query If given Key is Pressed.
Definition Input.h:34
static bool IsMouseButtonPressed(const int &button)
Query If given Mouse Button is Pressed.
Definition Input.h:41
This Class Is a wrapper of Platform Specific Input Query.
Definition Input.h:16
This Class is inherited from KeyEvent Class.
Definition KeyEvent.h:57
const float & GetYOffset() const
Get Mouse offset in Y.
Definition MouseEvent.h:114
This Class is inherited from Event Class.
Definition MouseEvent.h:86
const uint32_t & GetHeight() const
Get New Viewport Height.
Definition SlateEvent.h:46
const uint32_t & GetWidth() const
Get New Viewport Width.
Definition SlateEvent.h:40
This Class is inherited from Event Class. Called by Viewport Resize.
Definition SlateEvent.h:18
This Class handles our engine time step during frames. Global Unique.
Definition TimeStep.h:22
TransformComponent Class. This class defines the specific behaves of TransformComponent.