Update gitattributes
[opengl.git] / main.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <iostream>
4 #include <array>
5 #include <vector>
6 #include <dirent.h>
7 #ifdef __APPLE__
8 #include <GL/glew.h>
9 #else
10 #include <OpenGL/glew.h>
11 #endif
12 #include <GLUT/glut.h>
13 #include "shapes.hpp"
14 #include <glm/glm.hpp>
15 #include <glm/ext.hpp>
16 #include <glm/gtc/type_ptr.hpp>
17 #include <assimp/Importer.hpp>
18 #include <assimp/scene.h>
19 #include <assimp/postprocess.h>
20 #include "model.hpp"
21 #include "program.hpp"
22 #include "skybox.hpp"
23 #include "image.hpp"
24 #include "util.hpp"
25 #include "ik.hpp"
26 #include "blendshapes.hpp"
27 #include "ui.hpp"
28
29 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
30
31 using namespace std;
32
33 GLuint lightVao, cursorVao;
34 GLuint cursorNumIndices;
35
36 Program *textureProg, *plainProg, *reflectProg, *pbrProg, *cursorProg;
37
38 ControlWindow controlWindow;
39
40 std::vector<Skybox> skyboxes;
41 int activeSkybox = 0;
42
43 Assimp::Importer importer; // Need to keep this around, otherwise stuff disappears!
44 Model *sceneModel;
45
46 glm::vec3 camPos = {0, 0, -5}, camFront = {0, 0, 1}, camUp = {0, 1, 0};
47 float fov = glm::radians(30.f), znear = 0.01f, zfar = 10000.f;
48 float yaw = 1.57, pitch = 0;
49
50 Model *targetModel; // The model that the selection is happening on
51 Model::VertexLookup closestVertex;
52 // How close a vertex needs to be to the cursor before it is "over"
53 const float closestVertexThreshold = 0.5;
54
55 std::map<VertIdx, glm::vec3> manipulators;
56 VertIdx curManipulator = {-1,-1};
57
58 BlendshapeModel bsModel;
59 bool playBlendshapeAnim = false;
60
61 struct Light {
62         glm::mat4 trans;
63         glm::vec3 color;
64 };
65
66 std::vector<Light> lights;
67
68 bool discoLights = false;
69
70 int windowWidth = 800, windowHeight = 600;
71
72 enum Mode {
73         Default,
74         Blendshapes
75 };
76 Mode curMode;
77
78 float aspect() {
79         return (float)windowWidth / (float)windowHeight;
80 }       
81
82 inline glm::mat4 projMat() {
83         return glm::perspective(fov, aspect(), znear, zfar);
84 }
85
86 inline glm::mat4 viewMat() {
87         return glm::lookAt(camPos, camPos + camFront, camUp);
88 }
89
90 void setProjectionAndViewUniforms(GLuint progId) {
91         GLuint projLoc = glGetUniformLocation(progId, "projection");
92         glm::mat4 proj = projMat();
93         glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));
94
95         GLuint viewLoc = glGetUniformLocation(progId, "view");
96         glm::mat4 view = viewMat();
97         glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
98
99         GLuint camPosLoc = glGetUniformLocation(progId, "camPos");
100         glUniform3fv(camPosLoc, 1, glm::value_ptr(camPos));
101 }
102
103 void setLightColorAndPos(GLuint progId, glm::vec3 lightPos, glm::vec4 lightColor) {
104         GLuint lightColorLoc = glGetUniformLocation(progId, "lightColor");
105         glUniform4fv(lightColorLoc, 1, glm::value_ptr(lightColor));
106
107         GLuint lightPosLoc = glGetUniformLocation(progId, "vLightPos");
108         glUniform3fv(lightPosLoc, 1, glm::value_ptr(lightPos));
109
110         GLuint viewPosLoc = glGetUniformLocation(progId, "vViewPos");
111         glUniform3fv(viewPosLoc, 1, glm::value_ptr(camPos));
112 }
113
114 void drawPlainProg(Program *p, GLuint vao, glm::mat4 trans, glm::vec3 color) {
115         glUseProgram(p->progId);
116         glBindVertexArray(vao);
117         setProjectionAndViewUniforms(p->progId);
118         glm::mat4 model = glm::scale(trans, glm::vec3(0.3));
119         GLuint modelLoc = glGetUniformLocation(p->progId, "model");
120         glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
121
122         GLuint colorLoc = glGetUniformLocation(p->progId, "color");
123         glUniform4fv(colorLoc, 1, glm::value_ptr(color));
124                 
125         glDrawArrays(GL_TRIANGLES, 0, 36);
126 }
127
128 void drawLight(Light &light) {
129         drawPlainProg(plainProg, lightVao, light.trans, light.color);
130 }
131
132 int findNodeTrans(const struct aiNode *n, const struct aiString name, glm::mat4 *dest) {
133         if (strcmp(n->mName.data, name.data) == 0) {
134                 *dest = aiMatrixToMat4(n->mTransformation);
135                 return 0;
136         }
137         for (int i = 0; i < n->mNumChildren; i++) {
138                 if (findNodeTrans(n->mChildren[i], name, dest) == 0) {
139                         glm::mat4 t = aiMatrixToMat4(n->mTransformation);
140                         *dest = t * *dest;
141                         return 0;
142                 }
143         }
144         return 1;
145 }
146
147 glm::mat4 worldSpaceToModelSpace(aiNode *node, glm::mat4 m) {
148         aiNode *parent = node;
149         glm::mat4 res = m;
150         std::vector<glm::mat4> trans;
151         while (parent != nullptr) {
152                 /* res = res * glm::inverse(aiMatrixToMat4(parent->mTransformation)); */
153                 trans.push_back(glm::inverse(aiMatrixToMat4(parent->mTransformation)));
154                 parent = parent->mParent;
155         }
156         while (!trans.empty()) { res = trans.back() * res; trans.pop_back(); }
157         return res;
158 }
159
160 void drawCursor(glm::vec3 pos, glm::vec3 color, float scale = 1) {
161         glUseProgram(cursorProg->progId);
162         glBindVertexArray(cursorVao);
163         setProjectionAndViewUniforms(cursorProg->progId);
164         glm::mat4 model = glm::scale(glm::translate(glm::mat4(1), pos), glm::vec3(scale * 0.4));
165         GLuint modelLoc = glGetUniformLocation(cursorProg->progId, "model");
166         glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
167
168         GLuint colorLoc = glGetUniformLocation(cursorProg->progId, "color");
169         glUniform4fv(colorLoc, 1, glm::value_ptr(color));
170                 
171         glDrawElements(GL_TRIANGLES, cursorNumIndices, GL_UNSIGNED_INT, 0);
172 }
173
174 void display() {
175         glClearColor(0.5, 0.5, 0.5, 1);
176         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
177         glViewport(0, 0, windowWidth * 2, windowHeight * 2);
178
179         float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
180
181         glUseProgram(getUtilProg()->progId);
182         setProjectionAndViewUniforms(getUtilProg()->progId);
183
184         glUseProgram(pbrProg->progId);
185         setProjectionAndViewUniforms(pbrProg->progId);
186
187         size_t numLights = lights.size() + (discoLights ? 3 : 0);
188         glm::vec3 lightPositions[numLights], lightColors[numLights];
189         for (int i = 0; i < lights.size(); i++) {
190                 lightPositions[i] = glm::vec3(lights[i].trans[3]);
191                 lightColors[i] = lights[i].color;
192         }
193
194         if (discoLights) {
195                 for (int i = numLights - 3; i < numLights; i++) {
196                         auto m = glm::translate(glm::mat4(1.f), glm::vec3(-2.5, 0, 0));
197                         m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(1, 0, 0));
198                         m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 1, 0));
199                         m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 0, 1));
200                         lightPositions[i] = glm::vec3(m * glm::vec4(5, 0, 0, 1));
201                         lightColors[i] = glm::vec3(0.2);
202                         if (i % 3 == 0) lightColors[i].x = sin(d);
203                         if (i % 3 == 1) lightColors[i].y = cos(d * 3);
204                         if (i % 3 == 2) lightColors[i].z = cos(d);
205                 }
206         }
207
208         glUniform1ui(glGetUniformLocation(pbrProg->progId, "numLights"), numLights);
209         glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightPositions"), numLights, glm::value_ptr(lightPositions[0]));
210         glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightColors"), numLights, glm::value_ptr(lightColors[0]));
211
212 #ifdef COWEDBOY_IK
213         { 
214                 glm::vec3 targetPos(sin(d) * 2 + 3, -2, 1);
215                 Light targetLight = { glm::translate(glm::mat4(1), targetPos), {0.5, 1, 1}  };
216                 drawLight(targetLight);
217                 inverseKinematics(*sceneModel->find("Shoulder.L"), *sceneModel->find("Finger.L"), targetPos);
218
219                 targetPos = { sin(d * 2) * 2 - 5, 2.5, 0 };
220                 targetLight = { glm::translate(glm::mat4(1), targetPos), {1, 1, 0.5}  };
221                 drawLight(targetLight);
222                 inverseKinematics(*sceneModel->find("Shoulder.R"), *sceneModel->find("Finger.R"), targetPos);
223         }
224 #endif
225
226         if (curMode == Default)
227                 sceneModel->draw(skyboxes[activeSkybox], d * 1000);
228
229         if (curMode == Blendshapes) {
230                 if (closestVertex.distance < closestVertexThreshold)
231                         drawCursor(closestVertex.pos, {0.5,0,1}, 0.9);
232
233                 for (auto v: manipulators) {
234                         glm::vec3 color = { 0.4, 1, 0 };
235                         if (closestVertex.meshIdx == v.first.first &&
236                                 closestVertex.vertIdx == v.first.second)
237                                 color = {1, 0, 0};
238                         drawCursor(v.second, color);
239
240                         glm::vec3 origVertex = aiVector3DToVec3(bsModel.model->meshes[v.first.first].ai.mVertices[v.first.second]);
241                         drawCursor(origVertex, {0,0,1}, 0.7);
242                 }
243
244                 bsModel.model->draw(skyboxes[activeSkybox], d * 1000);
245         }
246
247         for (Light &light: lights) drawLight(light);
248
249         // TODO: restore
250         /* if (discoLights) { */
251         /*      for (int i = numLights - 3; i < numLights; i++) { */
252         /*              Light l = { lightPositions[i], lightColors[i] }; */
253         /*              drawLight(l); */
254         /*      } */
255         /* } */
256
257         skyboxes[activeSkybox].draw(projMat(), viewMat());
258
259         glutSwapBuffers();
260 }
261
262 void setupPlainBuffers(GLuint progId, std::vector<glm::vec3> vertices) {
263         GLuint verticesSize = vertices.size() * sizeof(glm::vec3);
264
265         glGenVertexArrays(1, &lightVao);
266         GLuint vbo;
267         glBindVertexArray(lightVao);
268         glGenBuffers(1, &vbo);
269         glBindBuffer(GL_ARRAY_BUFFER, vbo);
270         glBufferData(GL_ARRAY_BUFFER, verticesSize, NULL, GL_STATIC_DRAW);
271         glBufferSubData(GL_ARRAY_BUFFER, 0, verticesSize, glm::value_ptr(vertices[0]));
272         GLuint posLoc = glGetAttribLocation(progId, "vPosition");
273         glEnableVertexAttribArray(posLoc);
274         glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
275 }
276
277 GLuint setupBuffersWithIndices(GLuint progId, GLuint vao,
278                                                          std::vector<glm::vec3> vertices,
279                                                          std::vector<GLuint> indices) {
280         GLuint vbos[2];
281         glBindVertexArray(vao);
282         glGenBuffers(2, vbos);
283
284         int verticesSize = vertices.size() * sizeof(glm::vec3);
285         // positions
286         glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
287         glBufferData(GL_ARRAY_BUFFER, verticesSize, NULL, GL_STATIC_DRAW);
288         glBufferSubData(GL_ARRAY_BUFFER, 0, verticesSize,
289                                         glm::value_ptr(vertices[0]));
290
291         GLuint posLoc = glGetAttribLocation(progId, "vPosition");
292         glEnableVertexAttribArray(posLoc);
293         glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
294
295         // indices
296         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[1]);
297         glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
298
299         return indices.size();
300 }
301
302 bool needToCalculateClosestVertex = false;
303 bool needToInterpolateBlendshapes = false;
304
305 class Delegate : public ControlWindowDelegate {
306         public:
307
308         virtual void weightChanged(int blendshape, float weight) {
309                 bsModel.blendshapes[blendshape].weight = weight;
310                 needToInterpolateBlendshapes = true;
311         }
312
313         virtual void solveWeights(std::vector<float> &newWeights) {
314                 ::solveWeights(&bsModel, manipulators);
315                 for (int i = 0; i < newWeights.size(); i++)
316                         newWeights[i] = bsModel.blendshapes[i].weight;
317                 needToInterpolateBlendshapes = true;
318         }
319
320         virtual void resetManipulators() {
321                 manipulators.clear();
322                 curManipulator = { -1, -1 };
323         }
324
325         virtual void playbackChanged(bool playing) {
326                 playBlendshapeAnim = playing;
327         }
328 };
329
330 Delegate cwDelegate;
331
332
333 void init() {
334         initUtilProg();
335
336         plainProg = new Program("plainvertex.glsl", "plainfrag.glsl");
337         glUseProgram(plainProg->progId);
338         setupPlainBuffers(plainProg->progId, cube());
339         plainProg->validate();
340
341         cursorProg = new Program("plainvertex.glsl", "plainfrag.glsl");
342         glUseProgram(cursorProg->progId);
343         glGenVertexArrays(1, &cursorVao);
344         cursorNumIndices = setupBuffersWithIndices(cursorProg->progId, cursorVao, sphere(), sphereIndices());
345         cursorProg->validate();
346
347         skyboxes.push_back(Skybox(Image("skyboxes/loft/Newport_Loft_Ref.hdr")));
348         skyboxes.push_back(Skybox(Image("skyboxes/wooden_lounge_8k.hdr")));
349         skyboxes.push_back(Skybox(Image("skyboxes/machine_shop_02_8k.hdr")));
350         skyboxes.push_back(Skybox(Image("skyboxes/pink_sunrise_8k.hdr")));
351
352         pbrProg = new Program("pbrvert.glsl", "pbrfrag.glsl");
353         glUseProgram(pbrProg->progId);
354
355         if (curMode == Default) {
356                 const std::string scenePath = "models/cowedboy.glb";
357                 const aiScene *scene = importer.ReadFile(
358                                 scenePath, aiProcess_Triangulate | aiProcess_CalcTangentSpace |
359                                 aiProcess_GenNormals | aiProcess_FlipUVs);
360                 if (!scene) {
361                         std::cerr << importer.GetErrorString() << std::endl;
362                         exit(1);
363                 }
364
365                 if (scene->mNumCameras > 0) {
366                         aiCamera *cam = scene->mCameras[0];
367                         glm::mat4 camTrans;
368                         if (findNodeTrans(scene->mRootNode, cam->mName, &camTrans) != 0)
369                                 abort(); // there must be a node with the same name as camera
370
371                         camPos = {camTrans[3][0], camTrans[3][1], camTrans[3][2]};
372
373                         glm::vec3 camLookAt =
374                                 glm::vec3(cam->mLookAt.x, cam->mLookAt.y, cam->mLookAt.z);
375                         camFront = camLookAt - camPos;
376
377                         camUp = glm::vec3(cam->mUp.x, cam->mUp.y, cam->mUp.z);
378
379                         fov = cam->mHorizontalFOV;
380                         // TODO: aspectRatio = cam->mAspect;
381                         znear = cam->mClipPlaneNear;
382                         zfar = cam->mClipPlaneFar;
383                 }
384
385                 for (int i = 0; i < scene->mNumLights; i++) {
386                         aiLight *light = scene->mLights[i];
387                         glm::mat4 trans;
388                         findNodeTrans(scene->mRootNode, light->mName, &trans);
389                         glm::vec3 col = {light->mColorAmbient.r, light->mColorAmbient.g,
390                                 light->mColorAmbient.b};
391                         Light l = {trans, col};
392                         lights.push_back(l);
393                 }
394
395                 sceneModel = new Model(scene, *pbrProg);
396         }
397
398         if (curMode == Blendshapes) {
399                 loadBlendshapes("models/high-res-blendshapes/", *pbrProg, &bsModel);
400                 targetModel = bsModel.model;
401
402                 size_t numBlends = bsModel.blendshapes.size();
403                 std::vector<std::string> names(numBlends);
404                 for (int i = 0; i < numBlends; i++) names[i] = bsModel.blendshapes[i].name;
405                 controlWindow = createControlWindow(names, &cwDelegate);
406
407                 camPos = { 0, 18, 81 };
408                 camFront = { 0, 0, -1 };
409                 camUp = { 0, 1, 0 };
410                 zfar = 10000;
411                 znear = 0.1f;
412         }
413
414         glEnable(GL_DEPTH_TEST); 
415         glEnable(GL_CULL_FACE); 
416         // prevent edge artifacts in specular cubemaps
417         glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
418
419         glViewport(0, 0, windowWidth * 2, windowHeight * 2);
420 }
421
422 bool keyStates[256] = {false};
423
424 void keyboard(unsigned char key, int x, int y) {
425         keyStates[key] = true;
426         if (key == 'z')
427                 activeSkybox = (activeSkybox + 1) % skyboxes.size();
428         if (key == 'c')
429                 discoLights = !discoLights;
430 }
431
432 void keyboardUp(unsigned char key, int x, int y) {
433         keyStates[key] = false;
434 }
435
436 int mouseX, mouseY;
437
438 /* #define ENABLE_MOVEMENT */
439 void timer(int _) {
440 #ifdef ENABLE_MOVEMENT
441         float xSpeed = 0.f, ySpeed = 0.f, zSpeed = 0.f;
442
443 #pragma clang diagnostic push
444 #pragma clang diagnostic ignored "-Wchar-subscripts"
445         if (keyStates['w'])
446                 zSpeed = 0.1f;
447         if (keyStates['s'])
448                 zSpeed = -0.1f;
449         if (keyStates['a'])
450                 xSpeed = 0.1f;
451         if (keyStates['d'])
452                 xSpeed = -0.1f;
453         if (keyStates['q'])
454                 ySpeed = 0.1f;
455         if (keyStates['e'])
456                 ySpeed = -0.1f;
457 #pragma clang diagnostic pop
458
459         camPos.x += xSpeed * sin(yaw) + zSpeed * cos(yaw);
460         camPos.y += ySpeed;
461         camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw);
462 #endif
463
464         if (curMode == Blendshapes) {
465                 float xSpeed = 0, ySpeed = 0, zSpeed = 0;
466 #pragma clang diagnostic push
467 #pragma clang diagnostic ignored "-Wchar-subscripts"
468                 if (keyStates['w'])
469                         zSpeed = 0.1f;
470                 if (keyStates['s'])
471                         zSpeed = -0.1f;
472                 if (keyStates['a'])
473                         xSpeed = 0.1f;
474                 if (keyStates['d'])
475                         xSpeed = -0.1f;
476                 if (keyStates['q'])
477                         ySpeed = 0.1f;
478                 if (keyStates['e'])
479                         ySpeed = -0.1f;
480 #pragma clang diagnostic pop
481
482                 if (playBlendshapeAnim) {
483                         stepBlendshapeAnim(&bsModel);
484                         needToInterpolateBlendshapes = true;
485                         std::vector<float> newWeights(bsModel.blendshapes.size());
486                         for (int i = 0; i < bsModel.blendshapes.size(); i++)
487                                 newWeights[i] = bsModel.blendshapes[i].weight;
488                         updateWeights(&controlWindow, newWeights);
489                 }
490
491                 if (curManipulator.first != -1 && curManipulator.second != -1) {
492                         manipulators[curManipulator].x += xSpeed;
493                         manipulators[curManipulator].y += ySpeed;
494                         manipulators[curManipulator].z += zSpeed;
495                 }
496
497                 if (needToInterpolateBlendshapes) {
498                         interpolateBlendshapes(&bsModel);
499                         needToInterpolateBlendshapes = false;
500                 }
501
502                 if (needToCalculateClosestVertex) {
503                         GLint vpArr[4]; glGetIntegerv(GL_VIEWPORT, vpArr);
504                         glm::vec4 viewport(vpArr[0], vpArr[1], vpArr[2], vpArr[3]);
505                         glm::vec3 selectedPos = glm::unProject(glm::vec3(mouseX * 2, viewport[3] - mouseY * 2, 1), // hidpi
506                                         viewMat(),
507                                         projMat(),
508                                         viewport);
509
510                         closestVertex = targetModel->closestVertex(targetModel->getRoot(), camPos, selectedPos);
511                         needToCalculateClosestVertex = false;
512                 }
513         }
514
515         glutPostRedisplay();
516         glutTimerFunc(16, timer, 0);
517 }
518
519 int prevMouseX, prevMouseY;
520 bool firstMouse = true;
521
522 void motion(int x, int y) {
523         if (firstMouse) {
524                 prevMouseX = x;
525                 prevMouseY = y;
526                 firstMouse = false;
527         }
528         float dx = x - prevMouseX, dy = y - prevMouseY;
529         prevMouseX = x; prevMouseY = y;
530         if (curMode == Blendshapes) {
531                 if (closestVertex.distance > closestVertexThreshold) {
532                         const glm::vec3 origin(0,18,0);
533                         const float sensitivity = 0.003f;
534                         auto camMat = glm::translate(glm::mat4(1), origin + camPos);
535                         auto rotation = glm::rotate(glm::rotate(glm::mat4(1), -dx * sensitivity, {0, 1, 0}),
536                                         -dy * sensitivity, {1, 0, 0});
537                         auto rotAroundOrig = camMat * rotation * glm::translate(glm::mat4(1), origin - camPos);
538                         camPos = rotAroundOrig * glm::vec4(camPos, 0);
539                         camFront = origin - camPos; // face center
540                 }
541                 needToCalculateClosestVertex = true;
542         }
543 }
544
545 void passiveMotion(int x, int y) {
546         if (firstMouse) {
547                 prevMouseX = x;
548                 prevMouseY = y;
549                 firstMouse = false;
550         }
551         mouseX = x; mouseY = y;
552         prevMouseX = x;
553         prevMouseY = y;
554 #ifdef ENABLE_MOVEMENT
555
556
557         int dx = x - prevMouseX, dy = y - prevMouseY;
558
559         prevMouseX = x;
560         prevMouseY = y;
561
562         const float sensitivity = 0.005f;
563         yaw += dx * sensitivity;
564         pitch -= dy * sensitivity;
565
566         glm::vec3 front;
567         front.x = cos(pitch) * cos(yaw);
568         front.y = sin(pitch);
569         front.z = cos(pitch) * sin(yaw);
570         camFront = glm::normalize(front);
571
572         if (pitch < -1.57079632679 || pitch >= 1.57079632679) {
573                 camUp = glm::vec3(0, -1, 0);
574         } else {
575                 camUp = glm::vec3(0, 1, 0);
576         }
577 #endif
578         if (curMode == Blendshapes)
579                 needToCalculateClosestVertex = true;
580 }
581
582 void mouse(int button, int state, int x, int y) {
583         if (isPanelFocused(controlWindow))
584                 return;
585
586         if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
587                 if (closestVertex.distance < closestVertexThreshold) {
588                         VertIdx idx = { closestVertex.meshIdx, closestVertex.vertIdx };
589                         if (manipulators.count(idx) <= 0)
590                                 manipulators[idx] = closestVertex.pos;
591                         curManipulator = idx;
592                 }
593         }
594
595 #ifdef ENABLE_MOVEMENT
596         if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
597                 firstMouse = true;
598 #endif
599 }
600
601 void reshape(int newWidth, int newHeight) {
602         windowWidth = newWidth, windowHeight = newHeight;
603 }
604
605 int main(int argc, char** argv) {
606         glutInit(&argc, argv);
607         glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);
608         glutInitWindowSize(windowWidth, windowHeight);
609         glutCreateWindow("Physically Based Rendering");
610         glutDisplayFunc(display);
611         glutReshapeFunc(reshape);
612
613         glewInit();
614
615         // TODO: parse argv
616         curMode = Blendshapes;
617         init();
618
619         glutKeyboardFunc(keyboard);
620         glutKeyboardUpFunc(keyboardUp);
621         glutTimerFunc(16, timer, 0);
622         glutMotionFunc(motion);
623         glutPassiveMotionFunc(passiveMotion);
624         glutMouseFunc(mouse);
625
626         glutMainLoop();
627
628         return 0;
629 }
630