-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
131 lines (107 loc) · 3.33 KB
/
Camera.cpp
File metadata and controls
131 lines (107 loc) · 3.33 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
128
129
130
131
#include "Camera.h"
#define YAW_MINIMUM_ROTATION glm::radians(5.0f)
Camera::Camera(int width, int height, glm::vec3 position)
{
Camera::width = width;
Camera::height = height;
Position = position;
}
void Camera::UpdateMatrix(float FOVdeg, float nearPlane, float farPlane)
{
//Matrix initialization
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
//Setting view matrix to be what the camera is looking at
view = glm::lookAt(Position, Position + Forward, Up);
//Calculating projection matrix to provide perspective to the view
projection = glm::perspective(glm::radians(FOVdeg), (float)(width / height), nearPlane, farPlane);
//Calculating and storing camera matrix
cameraMatrix = projection * view;
}
void Camera::Matrix(Shader& shader, const char* uniform)
{
//Updating cameraMatrix to the uniform variable in the vertex shader
glUniformMatrix4fv(glGetUniformLocation(shader.ID, uniform), 1, GL_FALSE, glm::value_ptr(cameraMatrix));
}
void Camera::Inputs(GLFWwindow* window)
{
//Directions
//Forward
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
Position += speed * Forward;
}
//Backwards
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
Position += speed * -Forward;
}
//Left
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
Position += speed * -glm::normalize(glm::cross(Forward, Up));
}
//Rigth
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
Position += speed * glm::normalize(glm::cross(Forward, Up));
}
//Up
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{
Position += speed * Up;
}
//Down
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
{
Position += speed * -Up;
}
//Sprint
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
{
speed = runSpeed;
}
else if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_RELEASE)
{
speed = baseSpeed;
}
//Rotation
//Left Click
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
//Hiding cursor
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
//Avoiding sudden movements when clicking other than in the middle of the screen to rotate
if (firstClick)
{
//Setting cursor to screen center
glfwSetCursorPos(window, width / 2, height / 2);
firstClick = false;
}
double mouseX;
double mouseY;
//Getting cursor position
glfwGetCursorPos(window, &mouseX, &mouseY);
//Movement relative to window in order to rotate multiplied by sensitivity in order to control the rotation rate
float rotX = sensitivity * (float)(mouseY - (height / 2)) / height;
float rotY = sensitivity * (float)(mouseX - (height / 2)) / height;
//Pitch rotation
glm::vec3 newForward = glm::rotate(Forward, glm::radians(-rotX), glm::normalize(glm::normalize(glm::cross(Forward, Up))));
//Avoiding rotating indefinitely over the X axis (pitch)
if (!((glm::angle(newForward, Up) <= YAW_MINIMUM_ROTATION) or (glm::angle(newForward, -Up) <= YAW_MINIMUM_ROTATION)))
{
Forward = newForward;
}
//Yaw rotation
Forward = glm::rotate(Forward, glm::radians(-rotY), Up);
//Setting the cursor to the center
glfwSetCursorPos(window, width / 2, height / 2);
}
//Left Click release
else if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_RELEASE)
{
//Cursor visible again
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
firstClick = true;
}
}