-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcamera.cpp
153 lines (134 loc) · 3.95 KB
/
camera.cpp
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
143
144
145
146
147
148
149
150
151
152
153
/**************************************************************************
* 3to4++ - https://github.com/rayzchen/3to4++
*-------------------------------------------------------------------------
* Copyright 2024 Ray Chen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**************************************************************************/
#include <GLFW/glfw3.h>
#include <linmath.h>
#include <cmath>
#include "constants.h"
#include "camera.h"
Camera::Camera(float a_fov, float a_width, float a_height, float a_near, float a_far) {
fov = a_fov;
aspect = a_width / a_height;
near = a_near;
far = a_far;
mat4x4_perspective(projection, fov, aspect, near, far);
yaw = 0.0f;
pitch = 0.0f;
zoom = 15.0f;
recalculate = true;
mousePressed = false;
width = a_width;
height = a_height;
sensitivity = 5.0f;
deceleration = 5.0f;
yawVel = 0.0f;
pitchVel = 0.0f;
}
mat4x4* Camera::getViewMat() {
if (recalculate) {
calcViewMat();
recalculate = false;
}
return &view;
}
mat4x4* Camera::getProjection() {
return &projection;
}
void Camera::calcViewMat() {
mat4x4_identity(view);
mat4x4_translate_in_place(view, 0, 0, -zoom);
mat4x4_rotate_X(view, view, -pitch);
mat4x4_rotate_Y(view, view, -yaw);
}
float Camera::getYaw() {
return yaw;
}
void Camera::setYaw(float yaw) {
this->yaw = fmod(yaw, 2 * M_PI);
// Force positive
if (this->yaw < 0) this->yaw += 2 * M_PI;
recalculate = true;
}
float Camera::getPitch() {
return pitch;
}
void Camera::setPitch(float pitch) {
this->pitch = pitch;
if (this->pitch > M_PI_2) {
this->pitch = M_PI_2;
} else if (this->pitch < -M_PI_2) {
this->pitch = -M_PI_2;
}
recalculate = true;
}
float Camera::getZoom() {
return zoom;
}
void Camera::setZoom(float zoom) {
this->zoom = zoom;
if (this->zoom > 25.0f) {
this->zoom = 25.0f;
} else if (this->zoom < 5.0f) {
this->zoom = 5.0f;
}
recalculate = true;
}
bool Camera::inputFlipped() {
return yaw > M_PI_2 && yaw < 3 * M_PI_2;
}
void Camera::scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {
if (yoffset == 0) {
return;
}
float zoom = this->getZoom();
zoom -= yoffset * 0.5f;
this->setZoom(zoom);
}
void Camera::framebufferSizeCallback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
this->width = width;
this->height = height;
this->aspect = (float)width / (float)height;
mat4x4_perspective(this->projection, this->fov, this->aspect, this->near, this->far);
}
void Camera::setMousePressed(bool pressed) {
mousePressed = pressed;
}
bool Camera::updateMouse(GLFWwindow* window, double dt) {
yawVel *= (1.0f - dt * deceleration);
pitchVel *= (1.0f - dt * deceleration);
if (std::abs(yawVel) < 1e-5) yawVel = 0.0f;
if (std::abs(pitchVel) < 1e-5) pitchVel = 0.0f;
setYaw(yaw - yawVel * dt);
setPitch(pitch - pitchVel * dt);
bool updated = (yawVel != 0.0f) || (pitchVel != 0.0f);
if (mousePressed) {
double curX, curY;
glfwGetCursorPos(window, &curX, &curY);
if (lastX != -1.0f && lastY != -1.0f) {
yawVel = (curX - lastX) * sensitivity / dt / width;
pitchVel = (curY - lastY) * sensitivity / dt / height;
}
lastX = curX;
lastY = curY;
} else {
lastX = -1.0f;
lastY = -1.0f;
}
return updated;
}