-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
300 lines (248 loc) · 7.74 KB
/
main.cpp
File metadata and controls
300 lines (248 loc) · 7.74 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <iostream>
#include <algorithm>
#include <vector>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <GL/gl.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtx/string_cast.hpp>
#include "camera.h"
#include "texture.h"
#include "cloth.h"
#include "LoadShaders.h"
#define HEIGHT 1000
#define WIDTH 1200
using namespace std;
using namespace glm;
GLuint program;
GLint texLoc;
float currentTime, deltaTime, lastTime = 0.0f;
vec3 initcamPos(0.f, 59.f, 192.f);
vec3 initscene_center(0.f, 10.f, 0.f);
vec3 initViewUP(0.0f, -1.0f, 0.0f);
mat4 viewMatrix;
// GLfloat nearVal = 1.0f, farVal = 10000.0f;
bool wireFrame = false;
bool north = false, south = false, west = false, east = false;
bool top = false, down = false;
bool rotateViewClock = false, rotateViewCounterClock = false;
bool rotateGazeClock = false, rotateGazeCounterClock = false;
// int initWidth = 800, initHeight = 800, currWidth, currHeight;
vec3 camPos(0.f, 59.f, 192.f);
vec3 scene_center(0.f, 10.f, 0.f);
vec3 viewUP(0.0f, -1.0f, 0.0f);
void cbfun(GLFWwindow* window, int key, int scancode, int action, int mods) {
vec3 gazeVector = scene_center - camPos; // Gaze Vector
vec3 gaze = gazeVector / length(gazeVector); // GazeVector/Magnitude(W-Axis)
vec3 uAxis = cross(viewUP, -gaze); // u-axis
vec3 u = uAxis / length(uAxis);
vec3 vAxis = cross(-gazeVector, uAxis); // v-axis
if (action == GLFW_PRESS || action != GLFW_RELEASE) {
if (key == GLFW_KEY_ESCAPE) {
exit(EXIT_SUCCESS);
}
else {
switch (key) {
case GLFW_KEY_I:
camPos = initcamPos;
scene_center = initscene_center;
viewUP = initViewUP;
// viewMatrix = lookAt(camPos, scene_center, viewUP);
break;
case GLFW_KEY_O:
if (wireFrame)
wireFrame = false;
else
wireFrame = true;
break;
case GLFW_KEY_UP:
if (north == true) {
camPos += 1.5f * gaze;
scene_center += 1.5f * gaze;
// viewMatrix = lookAt(camPos, scene_center, viewUP);
north = false;
}
else
north = true;
break;
case GLFW_KEY_DOWN:
if (south == true) {
camPos -= 2.0f * gaze;
scene_center -= 2.0f * gaze;
// viewMatrix = lookAt(camPos, scene_center, viewUP);
south = false;
}
else
south = true;
break;
case GLFW_KEY_LEFT:
if (east == true) {
camPos -= 2.0f * u;
scene_center -= 2.0f * u;
// viewMatrix = lookAt(camPos, scene_center, viewUP);
east = false;
}
else
east = true;
break;
case GLFW_KEY_RIGHT:
if (west == true) {
camPos += 2.0f * u;
scene_center += 2.0f * u;
// viewMatrix = lookAt(camPos, scene_center, viewUP);
west = false;
}
else
west = true;
break;
case GLFW_KEY_R:
if (top == true) {
camPos -= 0.5f * viewUP;
scene_center -= 0.5f * viewUP;
// viewMatrix = lookAt(camPos, scene_center, viewUP);
top = false;
}
else
top = true;
break;
case GLFW_KEY_T:
if (down == true) {
camPos += 0.5f * viewUP;
scene_center += 0.5f * viewUP;
// viewMatrix = lookAt(camPos, scene_center, viewUP);
down = false;
}
else
down = true;
break;
case GLFW_KEY_Z:
if (rotateViewClock == true) {
mat4 matrix = rotate(mat4(1.0f), -(1.0f * 3.14f) / 180.0f, viewUP);
camPos = vec3(matrix * vec4(camPos, 1.0f));
scene_center = vec3(matrix * vec4(scene_center, 1.0f));
// viewMatrix = lookAt(camPos, scene_center, viewUP);
rotateViewClock = false;
}
else
rotateViewClock = true;
break;
case GLFW_KEY_X:
if (rotateViewCounterClock == true) {
mat4 matrix = rotate(mat4(1.0f), (1.0f * 3.14f) / 180.0f, viewUP);
camPos = vec3(matrix * vec4(camPos, 1.0f));
scene_center = vec3(matrix * vec4(scene_center, 1.0f));
// viewMatrix = lookAt(camPos, scene_center, viewUP);
rotateViewCounterClock = false;
}
else
rotateViewCounterClock = true;
break;
case GLFW_KEY_C:
if (rotateGazeClock == true) {
mat4 matrix = rotate(mat4(1.0f), (1.0f * 3.14f) / 180.0f, gaze);
viewUP = vec3(matrix * vec4(viewUP, 1.0f));
// viewMatrix = lookAt(camPos, scene_center, viewUP);
rotateGazeClock = false;
}
else
rotateGazeClock = true;
break;
case GLFW_KEY_V:
if (rotateGazeCounterClock == true) {
mat4 matrix = rotate(mat4(1.0f), -(1.0f * 3.14f) / 180.0f, gaze);
viewUP = vec3(matrix * vec4(viewUP, 1.0f));
// viewMatrix = lookAt(camPos, scene_center, viewUP);
rotateGazeCounterClock = false;
}
else
rotateGazeCounterClock = true;
break;
default:
return;
}
}
}
}
void display(GLFWwindow* window, MouseRotator rotator, Cloth* cloth, Texture* tex) {
vec3 clear_color = vec3(0.3, 0.45, 0.6);
glClearColor(clear_color.x, clear_color.y, clear_color.z, 1.0f);
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepthf(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
currentTime = glfwGetTime();
deltaTime = currentTime - lastTime;
lastTime = currentTime;
glUseProgram(program);
// Uniforms
GLint MV_Loc, P_Loc;
MV_Loc = glGetUniformLocation(program, "MV");
P_Loc = glGetUniformLocation(program, "MVP");
mat4 mvMatrix, MVP, projectionMatrix;
mat4 M = mat4(1.0f);
mat4 VRotX = rotate(M, (rotator.phi), vec3(0.0f, -1.0f, 0.0f)); // Rotation about y-axis
mat4 VRotY = rotate(M, (rotator.theta + 0.2f), vec3(-1.0f, 0.0f, 0.0f)); // Rotation about x-axis
vec4 camPos = VRotX * VRotY * vec4(0.0f, 20.0f, 200.0f + 0.2f * rotator.zoom, 1.0f);
viewMatrix = lookAt(vec3(camPos), scene_center, viewUP);
projectionMatrix = perspective(50.0f, float(WIDTH / HEIGHT), 0.1f, 4000.0f);
mvMatrix = viewMatrix * M;
MVP = projectionMatrix * mvMatrix;
glUniformMatrix4fv(MV_Loc, 1, GL_FALSE, &mvMatrix[0][0]);
glUniformMatrix4fv(P_Loc, 1, GL_FALSE, &MVP[0][0]);
texLoc = glGetUniformLocation(program, "texColor");
glUniform1i(texLoc, 0);
glBindTexture(GL_TEXTURE_2D, tex->texture);
if (!glfwGetKey(window, GLFW_KEY_P)) cloth->update(window, deltaTime);
if (glfwGetKey(window, GLFW_KEY_G)) cloth->gravity *= -1.0f;
cloth->draw(window);
}
int main() {
if (!glfwInit()) {
cerr << "GLFW initialization failure" << endl;
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Cloth Simulation", NULL, NULL);
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental = true;
if (glewInit() != GLEW_OK) {
cerr << "GLEW initialization failure" << endl;
exit(EXIT_FAILURE);
}
vec3 clear_color = vec3(0.3, 0.45, 0.6);
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
// Controls
MouseRotator rotator;
rotator.init(window);
// Create cloth
Cloth* cloth = new Cloth(35, 35, 15, 15);
Texture* tex = new Texture("cat.jpg", 15, 15);
ShaderInfo shaders[] = {{GL_VERTEX_SHADER, "cloth.vert"}, {GL_FRAGMENT_SHADER, "cloth.frag"}, {GL_NONE, NULL}};
program = LoadShaders(shaders);
if (program == NULL) {
cerr << "Failed shader load" << endl;
exit(EXIT_FAILURE);
}
// glfwSetKeyCallback(window, cbfun);
while (!glfwWindowShouldClose(window)) {
rotator.poll(window);
display(window, rotator, cloth, tex);
glfwSwapInterval(0);
glfwSwapBuffers(window);
glfwPollEvents();
if (glfwGetKey(window, GLFW_KEY_ESCAPE)) break;
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}