-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.h
142 lines (115 loc) · 3.71 KB
/
Camera.h
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Camera.h
#ifndef CAMERA_H
#define CAMERA_H
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "imgui.h"
class Camera {
public:
glm::vec3 target;
float distance;
float pitch, yaw;
float nearPlane, farPlane; // Add these members
Camera()
: target(0.0f), distance(10.0f), pitch(0.0f), yaw(-90.0f),
lastX(0.0f), lastY(0.0f), firstMouse(true),
rightButtonPressed(false), middleButtonPressed(false),
nearPlane(0.1f), farPlane(1000.0f) {} // Initialize near and far planes
glm::mat4 getViewMatrix() {
glm::vec3 position;
position.x = target.x + distance * cos(glm::radians(pitch)) * cos(glm::radians(yaw));
position.y = target.y + distance * sin(glm::radians(pitch));
position.z = target.z + distance * cos(glm::radians(pitch)) * sin(glm::radians(yaw));
return glm::lookAt(position, target, glm::vec3(0, 1, 0));
}
glm::mat4 getProjectionMatrix(float aspectRatio, float nearPlane, float farPlane) {
return glm::perspective(glm::radians(45.0f), aspectRatio, nearPlane, farPlane);
}
glm::vec3 getPosition() {
glm::vec3 position;
position.x = target.x + distance * cos(glm::radians(pitch)) * cos(glm::radians(yaw));
position.y = target.y + distance * sin(glm::radians(pitch));
position.z = target.z + distance * cos(glm::radians(pitch)) * sin(glm::radians(yaw));
return position;
}
void processInput(GLFWwindow* window) {
ImGuiIO& io = ImGui::GetIO();
if (io.WantCaptureMouse) return;
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
// Handle rotation with right mouse button
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS) {
if (!rightButtonPressed) {
firstMouse = true;
rightButtonPressed = true;
}
if (firstMouse) {
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos; // Reversed since y-coordinates go from bottom to top
lastX = xpos;
lastY = ypos;
float sensitivity = 0.1f;
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
if (pitch > 89.0f) pitch = 89.0f;
if (pitch < -89.0f) pitch = -89.0f;
}
else {
rightButtonPressed = false;
}
// Handle panning with middle mouse button
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS) {
if (!middleButtonPressed) {
firstMouse = true;
middleButtonPressed = true;
}
if (firstMouse) {
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
float panSensitivity = 0.005f; // Adjust as needed
xoffset *= panSensitivity * distance;
yoffset *= panSensitivity * distance;
// Calculate right and up vectors
glm::vec3 front;
front.x = cos(glm::radians(pitch)) * cos(glm::radians(yaw));
front.y = sin(glm::radians(pitch));
front.z = cos(glm::radians(pitch)) * sin(glm::radians(yaw));
front = glm::normalize(front);
glm::vec3 right = glm::normalize(glm::cross(front, glm::vec3(0, 1, 0)));
glm::vec3 up = glm::normalize(glm::cross(right, front));
// Adjust the target position
target -= right * xoffset;
target += up * yoffset;
}
else {
middleButtonPressed = false;
}
}
static void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
Camera* cam = static_cast<Camera*>(glfwGetWindowUserPointer(window));
if (cam) {
cam->distance *= (1.0f - static_cast<float>(yoffset) * 0.1f);
if (cam->distance < 1.0f) cam->distance = 1.0f;
if (cam->distance > 1000.0f) cam->distance = 1000.0f;
}
}
private:
double lastX, lastY;
bool firstMouse;
bool rightButtonPressed;
bool middleButtonPressed;
};
#endif // CAMERA_H