-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
85 lines (72 loc) · 2.02 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
#include "Camera.hpp"
Camera::Camera(GLFWwindow* window, glm::vec3 position)
:
window(window),
mouseSensitivity(1.0),
position(position),
moveInput(glm::vec3(0.0f)),
up(glm::vec3(0.0f)),
right(glm::vec3(0.0f)),
front(glm::vec3(0.0f)),
moveSpeed(1.0f)
{
//to make camera look to positive Z
rotation.y = 90.f;
}
glm::mat4 Camera::getViewMatrix() const
{
glm::vec3 pos(position.x, position.y, position.z);
glm::vec3 fr(front.x, front.y, front.z);
glm::vec3 u(up.x, up.y, up.z);
return glm::lookAt(pos, pos + fr, u);
}
void Camera::update(float deltaTime)
{
movementInput();
AscendDescent(deltaTime);
position += (front * moveInput.z + right * moveInput.x) * moveSpeed * deltaTime;
calculateViewMatrix();
}
void Camera::processMouseInput(float xOffset, float yOffset)
{
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) != GLFW_PRESS)
return;
xOffset *= mouseSensitivity;
yOffset *= mouseSensitivity;
rotation.y += xOffset;
rotation.x += yOffset;
if (rotation.x > 89.0f)
rotation.x = 89.0f;
if (rotation.x < -89.0f)
rotation.x = -89.0f;
}
void Camera::AscendDescent(float deltaTime)
{
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
position.y += deltaTime;
else if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
position.y -= deltaTime;
}
void Camera::movementInput()
{
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
moveInput.z = 1;
else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
moveInput.z = -1;
else moveInput.z = 0;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
moveInput.x = 1;
else if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
moveInput.x = -1;
else moveInput.x = 0;
}
void Camera::calculateViewMatrix()
{
glm::vec3 dir;
dir.x = cos(glm::radians(rotation.y)) * cos(glm::radians(rotation.x));
dir.y = sin(glm::radians(rotation.x));
dir.z = sin(glm::radians(rotation.y)) * cos(glm::radians(rotation.x));
front = glm::normalize(dir);
right = glm::normalize(glm::cross(front, glm::vec3(0.0, 1.0, 0.0)));
up = glm::normalize(glm::cross(right, front));
}