-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
66 lines (57 loc) · 2.28 KB
/
Camera.cpp
File metadata and controls
66 lines (57 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// Created by ju on 10/31/25.
//
#include "Camera.hpp"
#include "platform/GlfwWindow.hpp"
glm::vec2 Camera::getMousePosition(GLFWwindow* window) {
double xPos, yPos;
glfwGetCursorPos(window, &xPos, &yPos);
return {xPos, yPos};
}
glm::vec2 Camera::getDeltaMousePosition(GLFWwindow *window) {
if (firstMousePos) {
firstMousePos = false;
mousePosition = getMousePosition(window);
return {0,0};
}
auto newMousePosition = getMousePosition(window);
glm::vec2 deltaMousePosition = mousePosition - newMousePosition;
mousePosition = newMousePosition;
return deltaMousePosition;
}
bool cursor_enabled = true;
void Camera::processInput( GlfwWindow& glfw_window) {
GLFWwindow* window = glfw_window.window;
float cameraSpeed = 0.2f;
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
cameraSpeed = 0.5f;
if (glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS)
cursor_enabled = !cursor_enabled;
glfw_window.set_cursor_disabled(cursor_enabled);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
position += glm::normalize(glm::vec3(front.x, 0, front.z) ) * cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
position -= glm::normalize(glm::vec3(front.x, 0, front.z) ) * cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
position -= glm::normalize(glm::cross(front, up)) * cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
position += glm::normalize(glm::cross(front, up)) * cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
position.y += cameraSpeed;
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
position.y -= cameraSpeed;
glm::vec2 mouseDelta = getDeltaMousePosition(window);
float sensitivity = 0.02f;
mouseDelta*= sensitivity;
yaw -= mouseDelta.x;
pitch += mouseDelta.y;
if(pitch > 89.0f)
pitch = 89.0f;
if(pitch < -89.0f)
pitch = -89.0f;
glm::vec3 direction;
direction.x = static_cast<float>(cos(glm::radians(yaw)) * cos(glm::radians(pitch)));
direction.y = static_cast<float>(sin(glm::radians(pitch)));
direction.z = static_cast<float>(sin(glm::radians(yaw)) * cos(glm::radians(pitch)));
front = glm::normalize(direction);
}