-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
89 lines (72 loc) · 1.73 KB
/
Camera.cpp
File metadata and controls
89 lines (72 loc) · 1.73 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
#include "Camera.h"
#include <glm/gtc/type_ptr.hpp>
#include <cmath>
const float WINDOW_WIDTH = 800.0f;
const float WINDOW_HEIGHT = 800.0f;
Camera::Camera(const glm::vec3& pos, const glm::vec3& worldUp) :
position(pos),
yaw(-90.0f),
pitch(0.0f),
worldUp(worldUp),
sensitivity(0.1f),
speed(0.05f),
fov(60.0f),
aspect(WINDOW_WIDTH / WINDOW_HEIGHT),
nearPlane(0.01f),
farPlane(100.0f)
{
updateCameraVectors();
}
void Camera::updateCameraVectors()
{
float yawRad = glm::radians(yaw);
float pitchRad = glm::radians(pitch);
glm::vec3 newFront;
newFront.x = std::cos(pitchRad) * std::cos(yawRad);
newFront.y = std::sin(pitchRad);
newFront.z = std::cos(pitchRad) * std::sin(yawRad);
front = glm::normalize(newFront);
right = glm::normalize(glm::cross(front, worldUp));
up = glm::normalize(glm::cross(right, front));
}
void Camera::moveForward(float distance)
{
position += front * distance * speed;
}
void Camera::moveRight(float distance)
{
position += right * distance * speed;
}
void Camera::moveUp(float distance)
{
position += worldUp * distance * speed;
}
void Camera::rotate(float xOffset, float yOffset)
{
xOffset *= sensitivity;
yOffset *= sensitivity;
yaw += xOffset;
pitch -= yOffset;
if (pitch > 89.0f) pitch = 89.0f;
if (pitch < -89.0f) pitch = -89.0f;
updateCameraVectors();
}
void Camera::setRotation(float yawAngle, float pitchAngle)
{
yaw = yawAngle;
pitch = pitchAngle;
// Clamp pitch
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
updateCameraVectors();
}
glm::mat4 Camera::getViewMatrix() const
{
return glm::lookAt(position, position + front, up);
}
glm::mat4 Camera::getProjectionMatrix() const
{
return glm::perspective(glm::radians(fov), aspect, nearPlane, farPlane);
}