-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
128 lines (110 loc) · 3.21 KB
/
Copy pathCamera.cpp
File metadata and controls
128 lines (110 loc) · 3.21 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
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// Created by 郭珂桢 on 2024/5/20.
//
#include "Camera.h"
#include <cmath>
Camera::Camera(const Config &config, glm::vec3 position, glm::vec3 up, float yaw, float pitch)
: m_config(config), Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED),
MouseSensitivity(config.mouseSensitivity),
Zoom(static_cast<float>(config.fov))
{
m_projectionMatrix = makeProjectionMatrix(config);
Position = position;
WorldUp = up;
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
updateMatrices();
}
const glm::mat4& Camera::GetViewMatrix() const
{
return m_viewMatrix;
}
const glm::mat4 &Camera::GetProjectionMatrix() const
{
return m_projectionMatrix;
}
const glm::mat4 &Camera::GetProjectionViewMatrix() const
{
return m_projectionViewMatrix;
}
void Camera::ProcessKeyboard(Camera_Movement direction, float deltaTime)
{
float velocity = MovementSpeed * deltaTime;
if (direction == FORWARD)
Position += Front * velocity;
if (direction == BACKWARD)
Position -= Front * velocity;
if (direction == LEFT)
Position -= Right * velocity;
if (direction == RIGHT)
Position += Right * velocity;
if (direction == UP)
Position += Up * velocity;
if (direction == DOWN)
Position -= Up * velocity;
updateMatrices();
}
void Camera::ProcessMouseMovement(float xoffset, float yoffset)
{
xoffset *= MouseSensitivity;
yoffset *= MouseSensitivity;
Yaw += xoffset;
Pitch = glm::clamp(Pitch + yoffset, -PITCH_LIMIT, PITCH_LIMIT);
updateCameraVectors();
updateMatrices();
}
void Camera::ProcessMouseScroll(float yoffset)
{
Zoom -= (float)yoffset * 2.0f;
if (Zoom < 30.0f)
Zoom = 30.0f;
if (Zoom > 110.0f)
Zoom = 110.0f;
updateMatrices();
}
void Camera::UpdateAspectRatio(int width, int height)
{
m_config.windowX = width;
m_config.windowY = height;
updateMatrices();
}
void Camera::SetPosition(const glm::vec3& position)
{
Position = position;
updateMatrices();
}
void Camera::SetLook(float yaw, float pitch)
{
Yaw = yaw;
Pitch = glm::clamp(pitch, -PITCH_LIMIT, PITCH_LIMIT);
updateCameraVectors();
updateMatrices();
}
void Camera::updateMatrices()
{
m_projectionMatrix = glm::perspective(glm::radians(Zoom),
(float)m_config.windowX / (float)m_config.windowY,
0.1f, 1000.0f);
m_viewMatrix = glm::lookAt(Position, Position + Front, Up);
m_projectionViewMatrix = m_projectionMatrix * m_viewMatrix;
}
void Camera::updateCameraVectors()
{
const float yawR = glm::radians(Yaw);
const float pitchR = glm::radians(Pitch);
glm::vec3 front;
front.x = std::cos(yawR) * std::cos(pitchR);
front.y = std::sin(pitchR);
front.z = std::sin(yawR) * std::cos(pitchR);
Front = glm::normalize(front);
Right = glm::normalize(glm::cross(Front, WorldUp));
Up = glm::normalize(glm::cross(Right, Front));
}
glm::mat4 Camera::makeProjectionMatrix(const Config &config)
{
float x = static_cast<float>(config.windowX);
float y = static_cast<float>(config.windowY);
float fov = (float)config.fov;
return glm::perspective(glm::radians(fov), x / y, 0.1f, 1000.0f);
}