-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcamera.cpp
99 lines (81 loc) · 2.98 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
//
// Created by kskun on 2022/5/17.
//
#include "camera.h"
#include <glfw/glfw3.h>
#include <glm/gtc/matrix_transform.hpp>
static const float cameraMouseSpeed = 0.05f,
cameraKeyboardSpeed = 10.0f,
cameraScrollSpeed = 5.0f;
Camera::Camera(glm::vec3 position, float fov) :
position(position), fov(fov) {}
glm::mat4 Camera::getViewMatrix() const {
return glm::lookAt(position, position + front, up);
}
void Camera::handleMouseInput(float x, float y, bool pressed) {
if (firstMouseInput || !pressed) {
lastMouse[0] = x;
lastMouse[1] = y;
firstMouseInput = false;
return;
}
float dx = x - lastMouse[0], dy = y - lastMouse[1];
yaw -= dx * cameraMouseSpeed;
pitch += dy * cameraMouseSpeed;
pitch = std::max(std::min(pitch, 90.0f), -90.0f); // clamp to [-90.0, 90.0] degree
front = glm::normalize(glm::vec3(
std::cos(glm::radians(yaw)) * std::cos(glm::radians(pitch)),
std::sin(glm::radians(pitch)),
std::sin(glm::radians(yaw)) * std::cos(glm::radians(pitch))
));
right = glm::cross(front, up);
lastMouse[0] = x;
lastMouse[1] = y;
}
void Camera::handleKeyboardInput(int key, float deltaTime) {
if (key == GLFW_KEY_W) {
position += front * cameraKeyboardSpeed * deltaTime;
} else if (key == GLFW_KEY_S) {
position -= front * cameraKeyboardSpeed * deltaTime;
} else if (key == GLFW_KEY_A) {
position -= right * cameraKeyboardSpeed * deltaTime;
} else if (key == GLFW_KEY_D) {
position += right * cameraKeyboardSpeed * deltaTime;
}
}
void Camera::handleScrollInput(float y) {
fov -= y * cameraScrollSpeed;
fov = std::max(std::min(fov, 80.0f), 1.0f);
}
static float modelRotationCameraMouseSpeed = 0.1f,
modelRotationCameraScrollSpeed = 1.0f;
void ModelRotationCamera::handleMouseInput(float x, float y, bool pressed) {
if (firstMouseInput || !pressed) {
lastMouse[0] = x;
lastMouse[1] = y;
firstMouseInput = false;
return;
}
float dx = x - lastMouse[0], dy = y - lastMouse[1];
yaw += dx * modelRotationCameraMouseSpeed;
pitch -= dy * modelRotationCameraMouseSpeed;
pitch = std::max(std::min(pitch, 90.0f), -90.0f); // clamp to [-90.0, 90.0] degree
front = glm::normalize(glm::vec3(
std::cos(glm::radians(yaw)) * std::cos(glm::radians(pitch)),
std::sin(glm::radians(pitch)),
std::sin(glm::radians(yaw)) * std::cos(glm::radians(pitch))
));
right = glm::cross(front, up);
position = center - front * distance;
lastMouse[0] = x;
lastMouse[1] = y;
}
void ModelRotationCamera::handleKeyboardInput(int key, float deltaTime) {
Camera::handleKeyboardInput(key, deltaTime);
center = position + front * distance;
}
void ModelRotationCamera::handleScrollInput(float y) {
distance -= y * modelRotationCameraScrollSpeed;
distance = std::max(std::min(distance, 100.0f), 1e-6f);
position = center - front * distance;
}