Elevate Engine 1
Loading...
Searching...
No Matches
Camera.cpp
Go to the documentation of this file.
1#include "eepch.h"
2#include "Camera.h"
3
4#include "glm/glm.hpp"
5#include "glm/gtc/matrix_transform.hpp"
6
9
12
13Elevate::Camera::Camera(float fov, bool overrideCurrent)
14{
15 m_FOV = fov;
16 Window& window = Application::Get().GetWindow();
17 m_aspectRatio = (float)window.GetWidth() / (float)window.GetHeight();
18
19 m_canBeMainCamera = overrideCurrent;
20}
21
22Elevate::Camera::Camera(float fov, float aspectRatio, bool overrideCurrent)
23{
24 m_FOV = fov;
25 Window& window = Application::Get().GetWindow();
26 m_aspectRatio = aspectRatio;
27
28 m_canBeMainCamera = overrideCurrent;
29}
30
32{
33 UpdateProjectionMatrix();
34 UpdateCameraVectors();
35
36 if (m_canBeMainCamera)
37 {
39 }
40
41 // todo : change this bit of code to be in the camera manager
43}
44
49
51{
52 UpdateCameraVectors();
53}
54
55const void Elevate::Camera::UpdateAspectRatio(float aspectRatio)
56{
57 if (m_aspectRatio != aspectRatio)
58 {
59 m_aspectRatio = aspectRatio;
60 UpdateProjectionMatrix();
61 }
62}
63
65{
66 return m_projectionMatrix * GenViewMatrix();
67}
68
70{
71 return glm::lookAt(gameObject->GetPosition(), gameObject->GetPosition() + m_front, m_up);
72}
73
74inline void Elevate::Camera::SetFOV(float fov)
75{
76 if (m_FOV != fov)
77 {
78 m_FOV = fov;
79 UpdateProjectionMatrix();
80 }
81}
82
83inline void Elevate::Camera::SetNear(float nearPlane)
84{
85 if (m_near != nearPlane)
86 {
87 m_near = nearPlane;
88 UpdateProjectionMatrix();
89 }
90}
91
92inline void Elevate::Camera::SetFar(float farPlane)
93{
94 if (m_far != farPlane)
95 {
96 m_far = farPlane;
97 UpdateProjectionMatrix();
98 }
99}
100
101glm::mat4 Elevate::Camera::GenProjectionMatrix()
102{
103 return glm::perspective(glm::radians(m_FOV), m_aspectRatio, m_near, m_far);
104}
105
106void Elevate::Camera::UpdateProjectionMatrix()
107{
108 m_projectionMatrix = GenProjectionMatrix();
109}
110
112{
113 float pitch = glm::radians(gameObject->GetRotation().x);
114 float yaw = glm::radians(gameObject->GetRotation().y);
115
116 glm::vec3 front;
117 front.x = cos(yaw) * cos(pitch);
118 front.y = sin(pitch);
119 front.z = sin(yaw) * cos(pitch);
120 m_front = glm::normalize(front);
121
122 m_right = glm::normalize(glm::cross(glm::vec3(0.0f, 1.0f, 0.0f), m_front));
123
124 m_up = glm::normalize(glm::cross(m_front, m_right));
125}
126
127// ONLY IN THE EDITOR
128#ifdef EE_EDITOR_BUILD
130
132{
133 UpdateCameraVectors();
134 DrawDebugFrustum();
135}
136
137void Elevate::Camera::DrawDebugFrustum()
138{
139 auto corners = CalculateFrustumCorners();
140
141 glm::vec4 color = glm::vec4(0.96f, 0.47f, 0.12f, 1.0f);
142
143 // Draw the lines from the near corners to the far corners
144 for (int i = 0; i < 4; i++)
145 {
146 DebugRenderer::AddDebugLine({ corners[i], corners[i + 4], color });
147 }
148
149 // Draw near plane
150 DebugRenderer::AddDebugLine({ corners[0], corners[1], color });
151 DebugRenderer::AddDebugLine({ corners[1], corners[2], color });
152 DebugRenderer::AddDebugLine({ corners[2], corners[3], color });
153 DebugRenderer::AddDebugLine({ corners[3], corners[0], color });
154
155 // Draw far plane
156 DebugRenderer::AddDebugLine({ corners[4], corners[5], color });
157 DebugRenderer::AddDebugLine({ corners[5], corners[6], color });
158 DebugRenderer::AddDebugLine({ corners[6], corners[7], color });
159 DebugRenderer::AddDebugLine({ corners[7], corners[4], color });
160}
161
162std::array<glm::vec3, 8> Elevate::Camera::CalculateFrustumCorners(float visualFarScale)
163{
164 std::array<glm::vec3, 8> corners;
165
166 glm::vec3 position = gameObject->GetGlobalPosition();
167 glm::vec3 front = GetFrontVec();
168 glm::vec3 right = GetRightVec();
169 glm::vec3 up = GetUpVec();
170
171 float tanHalfFOV = tan(glm::radians(m_FOV * 0.5f));
172
173 float visualFar = m_far * visualFarScale;
174 float nearHeight = 2.0f * tanHalfFOV * m_near;
175 float nearWidth = nearHeight * m_aspectRatio;
176
177 float farHeight = 2.0f * tanHalfFOV * visualFar;
178 float farWidth = farHeight * m_aspectRatio;
179
180 glm::vec3 nearCenter = position + front * m_near;
181 glm::vec3 farCenter = position + front * visualFar;
182
183 // Near Plane Corners
184 corners[0] = nearCenter - (up * (nearHeight * 0.5f)) - (right * (nearWidth * 0.5f)); // near-bottom-left
185 corners[1] = nearCenter - (up * (nearHeight * 0.5f)) + (right * (nearWidth * 0.5f)); // near-bottom-right
186 corners[2] = nearCenter + (up * (nearHeight * 0.5f)) + (right * (nearWidth * 0.5f)); // near-top-right
187 corners[3] = nearCenter + (up * (nearHeight * 0.5f)) - (right * (nearWidth * 0.5f)); // near-top-left
188
189 // Far Plane Corners
190 corners[4] = farCenter - (up * (farHeight * 0.5f)) - (right * (farWidth * 0.5f)); // far-bottom-left
191 corners[5] = farCenter - (up * (farHeight * 0.5f)) + (right * (farWidth * 0.5f)); // far-bottom-right
192 corners[6] = farCenter + (up * (farHeight * 0.5f)) + (right * (farWidth * 0.5f)); // far-top-right
193 corners[7] = farCenter + (up * (farHeight * 0.5f)) - (right * (farWidth * 0.5f)); // far-top-left
194
195 return corners;
196}
197#endif
static Application & Get()
Definition Application.h:62
static void NotifyDestruction(Camera *camera)
static void SetCurrent(Camera *current)
void SetFOV(float fov)
Definition Camera.cpp:74
Camera(float fov, float aspectRatio, bool overrideCurrent=true)
Definition Camera.cpp:13
void UpdateCameraVectors()
Definition Camera.cpp:111
void Destroy() override
Definition Camera.cpp:45
void OnSetRotation() override
Definition Camera.cpp:50
void SetNear(float nearPlane)
Definition Camera.cpp:83
bool overrideCurrent
Definition Camera.h:20
const void UpdateAspectRatio(float aspectRatio)
Definition Camera.cpp:55
void Init() override
Definition Camera.cpp:31
void SetFar(float farPlane)
Definition Camera.cpp:92
glm::mat4 GenViewMatrix() const
Definition Camera.cpp:69
glm::mat4 GenViewProjectionMatrix() const
Definition Camera.cpp:64
void RenderWhenSelected() override
Definition Camera.h:58
static void AddDebugLine(DebugLineData line)
static void SetDefaultListener(GameObject *obj)
virtual unsigned int GetWidth() const =0
virtual unsigned int GetHeight() const =0