-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcamera.cpp
More file actions
123 lines (110 loc) · 3.57 KB
/
camera.cpp
File metadata and controls
123 lines (110 loc) · 3.57 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
#include "camera.h"
#include "init.h"
float cameraSpeed = 0.05f;
glm::vec3 cameraPosition = glm::vec3(0.0f, 5.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(1.0f, -0.6f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
bool firstMouse = true;
float lastX = 400.0f;
float lastY = 300.0f;
float yaw = -90.0f;
float pitch = 0.0f;
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
if (button == GLFW_MOUSE_BUTTON_LEFT) {
if (action == GLFW_PRESS) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
firstMouse = true;
} else if (action == GLFW_RELEASE) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}
}
void mouseCallback(GLFWwindow* window, double xpos, double ypos) {
if (firstMouse) {
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) {
float xOffset = xpos - lastX;
float yOffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
float sensitivity = 0.1f;
xOffset *= sensitivity;
yOffset *= sensitivity;
yaw += xOffset;
pitch += yOffset;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
}
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
float cameraSpeedMultiplier = 5.0f;
if (key == GLFW_KEY_UP) {
cameraPosition += cameraSpeed * cameraSpeedMultiplier * cameraFront;
}
if (key == GLFW_KEY_DOWN) {
cameraPosition -= cameraSpeed * cameraSpeedMultiplier * cameraFront;
}
if (key == GLFW_KEY_LEFT) {
cameraPosition -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed * cameraSpeedMultiplier;
}
if (key == GLFW_KEY_RIGHT) {
cameraPosition += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed * cameraSpeedMultiplier;
}
if (key == GLFW_KEY_N && action == GLFW_PRESS) {
set_snow();
}
if (key == GLFW_KEY_R && action == GLFW_PRESS) {
set_rain();
}
if (key == GLFW_KEY_F && action == GLFW_PRESS) {
if (fogStartDistance < 500){
fogStartDistance = 500;
}
else
{
fogStartDistance = 15;
}
updateFog();
}
if (key == GLFW_KEY_KP_ADD && action == GLFW_PRESS) {
fogStartDistance += 5;
updateFog();
}
if (key == GLFW_KEY_KP_SUBTRACT && action == GLFW_PRESS) {
if (fogStartDistance > 5)
fogStartDistance -= 5;
updateFog();
}
if (key == GLFW_KEY_H && action == GLFW_PRESS) {
temperature += 5;
updateTemp();
}
if (key == GLFW_KEY_C && action == GLFW_PRESS) {
temperature -= 5;
updateTemp();
}
if (key == GLFW_KEY_S && action == GLFW_PRESS) {
set_sun();
}
if (key == GLFW_KEY_G && action == GLFW_PRESS) {
set_hail();
}
}
glm::mat4 look_at(GLint viewUniform){
glm::mat4 view = glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);
glUniformMatrix4fv(viewUniform, 1, GL_FALSE, glm::value_ptr(view));
return view;
}