NoJobEngine 2.0.7
Loading...
Searching...
No Matches
EditorCamera.cpp
Go to the documentation of this file.
2
3#include <GLFW/glfw3.h>
4#include <glm/gtc/matrix_transform.hpp>
5
6#include <algorithm>
7#include <cmath>
8
9namespace NoJob
10{
12 float fovDegrees,
13 float aspectRatio,
14 float nearClip,
15 float farClip)
16 : m_FOV(fovDegrees),
17 m_AspectRatio(aspectRatio),
18 m_NearClip(nearClip),
19 m_FarClip(farClip)
20 {
21 RecalculateProjection();
22 RecalculateView();
23 }
24
26 GLFWwindow* window,
27 float deltaTime,
28 bool viewportHovered)
29 {
30 const bool rightMouseDown =
31 glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT)
32 == GLFW_PRESS;
33
34 const bool looking = viewportHovered && rightMouseDown;
35
36 if (!looking)
37 {
38 m_WasLooking = false;
39 return;
40 }
41
42 double mouseX = 0.0;
43 double mouseY = 0.0;
44 glfwGetCursorPos(window, &mouseX, &mouseY);
45
46 if (!m_WasLooking)
47 {
48 m_LastMouseX = mouseX;
49 m_LastMouseY = mouseY;
50 m_WasLooking = true;
51 }
52
53 const float deltaX =
54 static_cast<float>(mouseX - m_LastMouseX);
55
56 const float deltaY =
57 static_cast<float>(m_LastMouseY - mouseY);
58
59 m_LastMouseX = mouseX;
60 m_LastMouseY = mouseY;
61
62 m_Yaw += deltaX * m_MouseSensitivity;
63 m_Pitch += deltaY * m_MouseSensitivity;
64 m_Pitch = std::clamp(m_Pitch, -89.0f, 89.0f);
65
66 const glm::vec3 forward = GetForwardDirection();
67 const glm::vec3 right = GetRightDirection();
68 const glm::vec3 worldUp{ 0.0f, 1.0f, 0.0f };
69
70 float speed = m_MoveSpeed;
71 if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
72 speed *= 2.5f;
73
74 if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
75 m_Position += forward * speed * deltaTime;
76
77 if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
78 m_Position -= forward * speed * deltaTime;
79
80 if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
81 m_Position += right * speed * deltaTime;
82
83 if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
84 m_Position -= right * speed * deltaTime;
85
86 if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
87 m_Position += worldUp * speed * deltaTime;
88
89 if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
90 m_Position -= worldUp * speed * deltaTime;
91
92 RecalculateView();
93 }
94
95 void EditorCamera::SetViewportSize(float width, float height)
96 {
97 if (width <= 0.0f || height <= 0.0f)
98 return;
99
100 const float aspect = width / height;
101 if (std::abs(aspect - m_AspectRatio) < 0.0001f)
102 return;
103
104 m_AspectRatio = aspect;
105 RecalculateProjection();
106 }
107
108 glm::vec3 EditorCamera::GetForwardDirection() const
109 {
110 const float yaw = glm::radians(m_Yaw);
111 const float pitch = glm::radians(m_Pitch);
112
113 glm::vec3 direction;
114 direction.x = std::cos(yaw) * std::cos(pitch);
115 direction.y = std::sin(pitch);
116 direction.z = std::sin(yaw) * std::cos(pitch);
117
118 return glm::normalize(direction);
119 }
120
121 glm::vec3 EditorCamera::GetRightDirection() const
122 {
123 return glm::normalize(
124 glm::cross(
125 GetForwardDirection(),
126 glm::vec3(0.0f, 1.0f, 0.0f)));
127 }
128
129 void EditorCamera::RecalculateView()
130 {
131 const glm::vec3 forward = GetForwardDirection();
132
133 m_ViewMatrix = glm::lookAt(
134 m_Position,
135 m_Position + forward,
136 glm::vec3(0.0f, 1.0f, 0.0f));
137 }
138
139 void EditorCamera::RecalculateProjection()
140 {
141 m_ProjectionMatrix = glm::perspective(
142 glm::radians(m_FOV),
143 m_AspectRatio,
144 m_NearClip,
145 m_FarClip);
146 }
147}
EditorCamera declarations for the Editor subsystem.
EditorCamera(float fovDegrees=45.0f, float aspectRatio=16.0f/9.0f, float nearClip=0.1f, float farClip=1000.0f)
void OnUpdate(GLFWwindow *window, float deltaTime, bool viewportHovered)
void SetViewportSize(float width, float height)