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