-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
114 lines (92 loc) · 3.23 KB
/
Camera.cpp
File metadata and controls
114 lines (92 loc) · 3.23 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "Camera.h"
#include <glm/gtc/matrix_inverse.hpp>
Camera::Camera(float fov, float aspectRatio, float nearPlane, float farPlane)
: fov(fov), aspectRatio(aspectRatio), nearPlane(nearPlane), farPlane(farPlane) {
// Initial position - normal height now that Y is fixed
position = glm::vec3(0.0f, 3.0f, 8.0f); // 3m up, 8m back
worldUp = glm::vec3(0.0f, 1.0f, 0.0f);
// Look forward and slightly down
yaw = -90.0f;
pitch = -10.0f; // Look down 10 degrees
// Save initial state for reset
initialPosition = position;
initialYaw = yaw;
initialPitch = pitch;
initialFov = fov;
updateCameraVectors();
}
void Camera::reset() {
position = initialPosition;
yaw = initialYaw;
pitch = initialPitch;
fov = initialFov;
updateCameraVectors();
}
glm::mat4 Camera::getViewMatrix() const {
return glm::lookAt(position, position + front, up);
}
glm::mat4 Camera::getProjectionMatrix() const {
return glm::perspective(glm::radians(fov), aspectRatio, nearPlane, farPlane);
}
glm::mat4 Camera::getViewInverse() const {
return glm::inverse(getViewMatrix());
}
glm::mat4 Camera::getProjInverse() const {
return glm::inverse(getProjectionMatrix());
}
void Camera::update(float deltaTime) {
// This method can be used for smooth interpolation or physics-based camera movement
// For now, all updates happen in processKeyboard
}
void Camera::processKeyboard(GLFWwindow* window, float deltaTime) {
float velocity = movementSpeed * deltaTime;
// WASD movement
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
position += front * velocity;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
position -= front * velocity;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
position -= right * velocity;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
position += right * velocity;
// Q/E for up/down
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS)
position -= up * velocity;
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)
position += up * velocity;
// Shift to move faster
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
movementSpeed = 10.0f;
else
movementSpeed = 5.0f;
}
void Camera::processMouseMovement(float xOffset, float yOffset) {
xOffset *= mouseSensitivity;
yOffset *= mouseSensitivity;
yaw += xOffset;
pitch += yOffset; // Mouse up = look up (renderer Y is now corrected)
// Constrain pitch to avoid screen flip
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
updateCameraVectors();
}
void Camera::processMouseScroll(float yOffset) {
fov -= yOffset;
if (fov < 1.0f)
fov = 1.0f;
if (fov > 120.0f)
fov = 120.0f;
}
void Camera::updateCameraVectors() {
// Calculate the new front vector
glm::vec3 newFront;
newFront.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
newFront.y = sin(glm::radians(pitch));
newFront.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
front = glm::normalize(newFront);
// Recalculate right and up vectors
right = glm::normalize(glm::cross(front, worldUp));
up = glm::normalize(glm::cross(right, front));
}