-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
108 lines (85 loc) · 1.84 KB
/
Copy pathCamera.cpp
File metadata and controls
108 lines (85 loc) · 1.84 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
#include "Camera.h"
Camera::Camera()
{
}
Camera::Camera(glm::vec3 initialPosition, glm::vec3 initialUpDirection, GLfloat initialYaw, GLfloat initialPitch, GLfloat initialMovementSpeed, GLfloat initialTurnSpeed)
{
position = initialPosition;
worldUp = initialUpDirection;
yaw = initialYaw;
pitch = initialPitch;
front = glm::vec3(0.0f, 0.0f, -1.0f);
movementSpeed = initialMovementSpeed;
turnSpeed = initialTurnSpeed;
update();
}
void Camera::KeyControl(bool* keys, GLfloat deltaTime)
{
GLfloat velocity = movementSpeed * deltaTime;
if (keys[GLFW_KEY_W])
{
position += front * velocity;
}
if (keys[GLFW_KEY_S])
{
position -= front * velocity;
}
if (keys[GLFW_KEY_A])
{
position -= right * velocity;
}
if (keys[GLFW_KEY_D])
{
position += right * velocity;
}
if (keys[GLFW_KEY_LEFT_SHIFT])
{
position -= up * velocity;
}
if (keys[GLFW_KEY_SPACE])
{
position += up * velocity;
}
}
void Camera::MouseControl(GLfloat xChange, GLfloat yChange)
{
xChange *= turnSpeed;
yChange *= turnSpeed;
yaw += xChange;
pitch += yChange;
// This prevents the camera from flipping over at the top.
if (pitch > 89.0f)
{
pitch = 89.0f;
}
// This prevents the camera from flipping over at the bottom.
if (pitch < -89.0f)
{
pitch = -89.0f;
}
update();
}
glm::vec3 Camera::GetCameraPosition()
{
return position;
}
glm::vec3 Camera::GetCameraDirection()
{
return glm::normalize(front);
}
glm::mat4 Camera::CalculateViewMatrix()
{
return glm::lookAt(position, position + front, up);
}
void Camera::update()
{
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
front = glm::normalize(front);
right = glm::normalize(glm::cross(front, worldUp));
up = glm::normalize(glm::cross(right, front));
}
Camera::~Camera()
{
}