-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
89 lines (71 loc) · 2.44 KB
/
Camera.cpp
File metadata and controls
89 lines (71 loc) · 2.44 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"
Camera::Camera(GLint width, GLint height, glm::vec3 position) : width{ width }, height{ height }, position{ position }
{
}
void Camera::updateMatrix(GLfloat fov, GLfloat zNear, GLfloat zFar)
{
// The model matrix is for turning local coordinates into world coordinates. (Not here)
glm::mat4 view = glm::lookAt(position, position + orientation, up); // For turning world coordinates into view coordinates.
glm::mat4 projection = glm::perspective(glm::radians(fov), (float)width / height, zNear, zFar); // For turning view coordinates into clip coordinates.
cameraMatrix = projection * view;
}
void Camera::matrix(Shader& shader, const GLchar* uniform)
{
glUniformMatrix4fv(glGetUniformLocation(shader.handle, uniform), 1, GL_FALSE, glm::value_ptr(cameraMatrix));
}
void Camera::handleMovementInputs(GLFWwindow* window)
{
if (firstClick)
{
glfwSetCursorPos(window, width * 0.5, height * 0.5);
firstClick = false;
}
double mouseX;
double mouseY;
glfwGetCursorPos(window, &mouseX, &mouseY);
float rotX = sensitivity * (float)(mouseY - (height * 0.5)) / height;
float rotY = sensitivity * (float)(mouseX - (width * 0.5)) / width;
// Calculates upcoming vertical change in the Orientation
glm::vec3 newOrientation = glm::rotate(orientation, glm::radians(-rotX), glm::normalize(glm::cross(orientation, up)));
// Decides whether or not the next vertical Orientation is legal or not
if (abs(glm::angle(newOrientation, up) - glm::radians(90.0f)) <= glm::radians(85.0f))
{
orientation = newOrientation;
}
// Rotates the Orientation left and right
orientation = glm::rotate(orientation, glm::radians(-rotY), up);
// Sets mouse cursor to the middle of the screen so that it doesn't end up roaming around
glfwSetCursorPos(window, width * 0.5, height * 0.5);
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL))
{
speed = SPRINTING_SPEED;
}
else
{
speed = WALKING_SPEED;
}
if (glfwGetKey(window, GLFW_KEY_W)) // GLFW_PRESS is 1 so this works
{
position += speed * orientation;
}
if (glfwGetKey(window, GLFW_KEY_A))
{
position += speed * -glm::normalize(glm::cross(orientation, up));
}
if (glfwGetKey(window, GLFW_KEY_S))
{
position += speed * -orientation;
}
if (glfwGetKey(window, GLFW_KEY_D))
{
position += speed * glm::normalize(glm::cross(orientation, up));
}
if (glfwGetKey(window, GLFW_KEY_SPACE))
{
position += speed * up;
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT))
{
position += speed * -up;
}
}