-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrols.cpp
More file actions
132 lines (107 loc) · 3.36 KB
/
controls.cpp
File metadata and controls
132 lines (107 loc) · 3.36 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
132
/*
Taken and modified from Tutorials - 6
*/
// Include GLFW
#include <GLFW/glfw3.h>
extern GLFWwindow* window; // The "extern" keyword here is to access the variable "window" declared in tutorialXXX.cpp. This is a hack to keep the tutorials simple. Please avoid this.
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
#include <controls.hpp>
mat4 ViewMatrix;
mat4 ProjectionMatrix;
vec3 position = vec3(0, 1, 0);
mat4 getViewMatrix() {
return ViewMatrix;
}
mat4 getProjectionMatrix() {
return ProjectionMatrix;
}
vec3 getCameraPosition() {
return position;
}
float horizontalAngle = 3.14f;
float verticalAngle = 0.0f;
float initialFoV = 60.0f;
float speed = 3.0f; // 3 units /sec
float mouseSpeed = 0.005f;
// screen size
int width = 1980, height = 1080;
void computeMatricesFromInputs() {
// it will run once
static double lastTime = glfwGetTime();
double currTime = glfwGetTime();
float deltaTime = float(currTime - lastTime);
// mouse pos
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
// Reset mouse position for next frame
glfwSetCursorPos(window, 1980 / 2, 1080 / 2);
// angles
horizontalAngle += mouseSpeed * float(width / static_cast<double>(2) - xpos);
verticalAngle += mouseSpeed * float(height / static_cast<double>(2) - ypos);
// Clamp the vertical angle to prevent the camera from flipping
// 89.9 degrees in radians is about 1.57
if (verticalAngle > 1.57f) {
verticalAngle = 1.57f;
}
if (verticalAngle < -1.57f) {
verticalAngle = -1.57f;
}
// looking direction
vec3 direction(
cos(verticalAngle) * sin(horizontalAngle),
sin(verticalAngle),
cos(verticalAngle) * cos(horizontalAngle)
);
// right vector
vec3 right = vec3(
sin(horizontalAngle - 3.14f / 2.0f),
0,
cos(horizontalAngle - 3.14f / 2.0f)
);
// up vector
vec3 up = cross(right, direction);
// Move forward
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS ||
glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
position += direction * deltaTime * speed;
}
// Move backward
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS ||
glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
position -= direction * deltaTime * speed;
}
// Strafe right
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS ||
glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
position += right * deltaTime * speed;
}
// Strafe left
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS ||
glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
position -= right * deltaTime * speed;
}
// move above
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT ) == GLFW_PRESS ||
glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS) {
position += up * deltaTime * speed;
}
// move down
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS ||
glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS) {
position -= up * deltaTime * speed;
}
float FoV = initialFoV;
// Projection matrix : fov Field of View, 16:9 ratio, display range : 0.1 unit <-> 100 units
ProjectionMatrix = glm::perspective(glm::radians(FoV), 16.0f / 9.0f, 0.1f, 100.0f);
// Camera matrix
ViewMatrix = glm::lookAt(
position, // Camera is here
position + direction, // and looks here : at the same position, plus "direction"
up // Head is up (set to 0,-1,0 to look upside-down)
);
// For the next frame, the "last time" will be "now"
lastTime = currTime;
}