-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUserControls.cpp
More file actions
191 lines (164 loc) · 5.19 KB
/
Copy pathUserControls.cpp
File metadata and controls
191 lines (164 loc) · 5.19 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "UserControls.h"
#include <glfw/glfw3.h>
#include <ctime>
extern GLFWwindow* window;
bool hasInterp = false;
UserControls::UserControls(glm::vec3 pos, float hAngle, float vAngle, bool interpolate)
{
currentPosition = pos;
currentHorizontalAngle = hAngle;
currentVerticalAngle = vAngle;
interpolateColors = interpolate;
}
glm::mat4 UserControls::getViewMatrix()
{
return ViewMatrix;
}
glm::mat4 UserControls::getProjectionMatrix()
{
return ProjectionMatrix;
}
void UserControls::handleKeyboard(Mesh* mesh, Radiosity* radiosity)
{
//Subdivide
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS)// && !hasInterp)
{
if (glfwGetKey(window, GLFW_KEY_E) == GLFW_RELEASE)
{
printf("Subdividing mesh...\n");
mesh->Subdivide();
mesh->cacheVerticesFacesAndColors();
mesh->PrepareToDraw();
printf("Loading faces...\n");
radiosity->loadSceneFacesFromMesh(mesh);
radiosity->PrepareUnshotRadiosityValues();
}
}
//Reset
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
{
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_RELEASE)
{
printf("Resetting mesh...\n");
mesh->ResetMesh();
mesh->cacheVerticesFacesAndColors();
mesh->PrepareToDraw();
printf("Loading faces...\n");
radiosity->loadSceneFacesFromMesh(mesh);
radiosity->PrepareUnshotRadiosityValues();
}
}
//Radiosity
if (glfwGetKey(window, GLFW_KEY_I) == GLFW_PRESS)
{
if (glfwGetKey(window, GLFW_KEY_I) == GLFW_RELEASE)
{
hasInterp = true;
printf("Radiosity iteration. Please wait, this could take a while...\n");
printf("Calculating radiosity values...\n");
radiosity->calculateRadiosityValues();
printf("Preparing to re-draw scene...\n");
radiosity->setMeshFaceColors();
if (interpolateColors)
mesh->cacheVerticesFacesAndColors_Radiosity_II();
else
mesh->cacheVerticesFacesAndColors();
mesh->PrepareToDraw();
}
}
//dump to bitmap
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_RELEASE && false)
{
time_t now = time(0);
string bmpName(to_string(now).append(".bmp"));
int windowWidth;
int windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
mesh->OutputToBitmap(bmpName, windowWidth, windowHeight);
//printf("Screenshot saved: %s\n", bmpName);
}
}
}
void UserControls::computeMatrices(float initialFoV, float nearClippingPlane, float farClippingPlane, float speed, float mouseSpeed)
{
// glfwGetTime is called only once, the first time this function is called
static double lastTime = glfwGetTime();
// Compute time difference between current and last frame
double currentTime = glfwGetTime();
float deltaTime = float(currentTime - lastTime);
//get current window size
int windowWidth;
int windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
// Get mouse position
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
// Reset mouse position for next frame
glfwSetCursorPos(window, windowWidth / 2, windowHeight / 2);
// Compute new orientation
currentHorizontalAngle += mouseSpeed * float(windowWidth / 2 - xpos);
currentVerticalAngle += mouseSpeed * float(windowHeight / 2 - ypos);
// Direction : Spherical coordinates to Cartesian coordinates conversion
glm::vec3 direction(
cos(currentVerticalAngle) * sin(currentHorizontalAngle),
sin(currentVerticalAngle),
cos(currentVerticalAngle) * cos(currentHorizontalAngle)
);
// Right vector
glm::vec3 right = glm::vec3(
sin(currentHorizontalAngle - 3.14f / 2.0f),
0,
cos(currentHorizontalAngle - 3.14f / 2.0f)
);
// Up vector
glm::vec3 up = glm::cross(right, direction);
//handle arrow keys here! The position vlaue is needed for the View matrix!
// Move forward
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
{
currentPosition += direction * deltaTime * speed;
}
// Move backward
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS)
{
currentPosition -= direction * deltaTime * speed;
}
// Strafe right
if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
{
currentPosition += right * deltaTime * speed;
}
// Strafe left
if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
{
currentPosition -= right * deltaTime * speed;
}
// Reset the User to the default position
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{
currentPosition = glm::vec3(0, 0, 5);
currentHorizontalAngle = 3.14f;
currentVerticalAngle = 0.0f;
// Direction : Spherical coordinates to Cartesian coordinates conversion
glm::vec3 direction(
cos(currentVerticalAngle) * sin(currentHorizontalAngle),
sin(currentVerticalAngle),
cos(currentVerticalAngle) * cos(currentHorizontalAngle)
);
}
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
if (windowHeight == 0)
windowHeight = 1;
float windowRatio = (float)windowWidth / (float)windowHeight;
ProjectionMatrix = glm::perspective(initialFoV, windowRatio, nearClippingPlane, farClippingPlane);
// Camera matrix
ViewMatrix = glm::lookAt(
currentPosition, // Camera is here
currentPosition + 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 = currentTime;
}