-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigationcontrols.cpp
97 lines (84 loc) · 3.45 KB
/
navigationcontrols.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
86
87
88
89
90
91
92
93
94
95
96
97
#include "navigationcontrols.h"
#include "ressources/imgui/imgui.h"
NavigationControls::NavigationControls(GLFWwindow *window, Camera *camera):Controls(window, camera), lastPosCursor(-1,-1)
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
mouseSpeed = 0.02;
}
void NavigationControls::update(float deltaTime, Shader *shader)
{
// On test d'abord si on est sur la fenêtre imgui ou pas
if (/*!io.WantCaptureMouse*/true){
int state = glfwGetMouseButton(m_Window, GLFW_MOUSE_BUTTON_LEFT);
double xpos, ypos;
glfwGetCursorPos(m_Window, &xpos, &ypos);
if (state == GLFW_PRESS) {
if (lastPosCursor.x != -1) {
m_Camera->horizontalAngle += mouseSpeed * deltaTime * float(xpos - lastPosCursor.x);
m_Camera->verticalAngle += mouseSpeed * deltaTime * float(ypos - lastPosCursor.y);
// Limit vertical angle to avoid flipping
if (m_Camera->verticalAngle > glm::radians(89.0f)) {
m_Camera->verticalAngle = glm::radians(89.0f);
} else if (m_Camera->verticalAngle < glm::radians(-89.0f)) {
m_Camera->verticalAngle = glm::radians(-89.0f);
}
}
lastPosCursor = glm::vec2(xpos, ypos);
} else {
lastPosCursor = glm::vec2(-1, -1);
}
glm::vec3 direction(
cos(m_Camera->verticalAngle) * sin(m_Camera->horizontalAngle),
sin(m_Camera->verticalAngle),
cos(m_Camera->verticalAngle) * cos(m_Camera->horizontalAngle)
);
// Right vector
glm::vec3 right = glm::vec3(
sin(m_Camera->horizontalAngle - 3.14f/2.0f),
0,
cos(m_Camera->horizontalAngle - 3.14f/2.0f)
);
// Up vector : perpendicular to both direction and right
glm::vec3 up = glm::cross( right, direction );
// Move forward
if (glfwGetKey(m_Window, GLFW_KEY_UP ) == GLFW_PRESS){
m_Camera->position += direction * deltaTime * speed;
}
// Move backward
if (glfwGetKey(m_Window, GLFW_KEY_DOWN ) == GLFW_PRESS){
m_Camera->position -= direction * deltaTime * speed;
}
// Strafe right
if (glfwGetKey(m_Window, GLFW_KEY_RIGHT ) == GLFW_PRESS){
m_Camera->position += right * deltaTime * speed;
}
// Strafe left
if (glfwGetKey(m_Window, GLFW_KEY_LEFT ) == GLFW_PRESS){
m_Camera->position -= right * deltaTime * speed;
}
// Move forward
if (glfwGetKey(m_Window, GLFW_KEY_W ) == GLFW_PRESS){
m_Camera->position += direction * deltaTime * speed;
}
// Move backward
if (glfwGetKey(m_Window, GLFW_KEY_S ) == GLFW_PRESS){
m_Camera->position -= direction * deltaTime * speed;
}
// Strafe right
if (glfwGetKey(m_Window, GLFW_KEY_D ) == GLFW_PRESS){
m_Camera->position += right * deltaTime * speed;
}
// Strafe left
if (glfwGetKey(m_Window, GLFW_KEY_A ) == GLFW_PRESS){
m_Camera->position -= right * deltaTime * speed;
}
// go up
if (glfwGetKey(m_Window, GLFW_KEY_SPACE ) == GLFW_PRESS){
m_Camera->position += up * deltaTime * speed;
}
// go down
if (glfwGetKey(m_Window, GLFW_KEY_LEFT_SHIFT ) == GLFW_PRESS){
m_Camera->position -= up * deltaTime * speed;
}
}
}