776b3d6e283eac14f54b0c086da5d090e79800e5
[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.h"
28
29 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
30
31 using namespace std;
32
33 GLuint lightVao;
34
35 Program *textureProg, *plainProg, *reflectProg, *pbrProg;
36
37 std::vector<Skybox> skyboxes;
38 int activeSkybox = 0;
39
40 Assimp::Importer importer; // Need to keep this around, otherwise stuff disappears!
41 Model *sceneModel;
42
43 glm::vec3 camPos = {0, 0, -5}, camFront = {0, 0, 1}, camUp = {0, 1, 0};
44 float fov = glm::radians(30.f), znear = 0.01f, zfar = 10000.f;
45 float yaw = 1.57, pitch = 0;
46
47 Model *targetModel; // The model that the selection is happening on
48 glm::vec3 closestVertex;
49 std::vector<glm::vec3> manipulators;
50 Blendshapes blendshapes;
51 float *blendshapeWeights;
52
53 struct Light {
54         glm::mat4 trans;
55         glm::vec3 color;
56 };
57
58 std::vector<Light> lights;
59
60 bool discoLights = false;
61
62 int windowWidth = 800, windowHeight = 600;
63
64 enum Mode {
65         Default,
66         Blendshapes
67 };
68 Mode curMode;
69
70 float aspect() {
71         return (float)windowWidth / (float)windowHeight;
72 }       
73
74 inline glm::mat4 projMat() {
75         return glm::perspective(fov, aspect(), znear, zfar);
76 }
77
78 inline glm::mat4 viewMat() {
79         return glm::lookAt(camPos, camPos + camFront, camUp);
80 }
81
82 void setProjectionAndViewUniforms(GLuint progId) {
83         GLuint projLoc = glGetUniformLocation(progId, "projection");
84         glm::mat4 proj = projMat();
85         glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));
86
87         GLuint viewLoc = glGetUniformLocation(progId, "view");
88         glm::mat4 view = viewMat();
89         glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
90
91         GLuint camPosLoc = glGetUniformLocation(progId, "camPos");
92         glUniform3fv(camPosLoc, 1, glm::value_ptr(camPos));
93 }
94
95 void setLightColorAndPos(GLuint progId, glm::vec3 lightPos, glm::vec4 lightColor) {
96         GLuint lightColorLoc = glGetUniformLocation(progId, "lightColor");
97         glUniform4fv(lightColorLoc, 1, glm::value_ptr(lightColor));
98
99         GLuint lightPosLoc = glGetUniformLocation(progId, "vLightPos");
100         glUniform3fv(lightPosLoc, 1, glm::value_ptr(lightPos));
101
102         GLuint viewPosLoc = glGetUniformLocation(progId, "vViewPos");
103         glUniform3fv(viewPosLoc, 1, glm::value_ptr(camPos));
104 }
105
106 void drawBox(glm::mat4 trans, glm::vec3 color) {
107         glUseProgram(plainProg->progId);
108         glBindVertexArray(lightVao);
109         setProjectionAndViewUniforms(plainProg->progId);
110         glm::mat4 model = glm::scale(trans, glm::vec3(0.3));
111         GLuint modelLoc = glGetUniformLocation(plainProg->progId, "model");
112         glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
113
114         GLuint colorLoc = glGetUniformLocation(plainProg->progId, "color");
115         glUniform4fv(colorLoc, 1, glm::value_ptr(color));
116                 
117         glDrawArrays(GL_TRIANGLES, 0, 36);
118 }
119
120 void drawLight(Light &light) {
121         drawBox(light.trans, light.color);
122 }
123
124 int findNodeTrans(const struct aiNode *n, const struct aiString name, glm::mat4 *dest) {
125         if (strcmp(n->mName.data, name.data) == 0) {
126                 *dest = aiMatrixToMat4(n->mTransformation);
127                 return 0;
128         }
129         for (int i = 0; i < n->mNumChildren; i++) {
130                 if (findNodeTrans(n->mChildren[i], name, dest) == 0) {
131                         glm::mat4 t = aiMatrixToMat4(n->mTransformation);
132                         *dest = t * *dest;
133                         return 0;
134                 }
135         }
136         return 1;
137 }
138
139 glm::mat4 worldSpaceToModelSpace(aiNode *node, glm::mat4 m) {
140         aiNode *parent = node;
141         glm::mat4 res = m;
142         std::vector<glm::mat4> trans;
143         while (parent != nullptr) {
144                 /* res = res * glm::inverse(aiMatrixToMat4(parent->mTransformation)); */
145                 trans.push_back(glm::inverse(aiMatrixToMat4(parent->mTransformation)));
146                 parent = parent->mParent;
147         }
148         while (!trans.empty()) { res = trans.back() * res; trans.pop_back(); }
149         return res;
150 }
151
152 void highlightVertex() {
153         drawBox(glm::translate(glm::mat4(1), closestVertex), {1, 1, 0.5});
154 }
155
156 void display() {
157         glClearColor(0.5, 0.5, 0.5, 1);
158         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
159         glViewport(0, 0, windowWidth * 2, windowHeight * 2);
160
161         float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
162
163         glUseProgram(getUtilProg()->progId);
164         setProjectionAndViewUniforms(getUtilProg()->progId);
165
166         glUseProgram(pbrProg->progId);
167         setProjectionAndViewUniforms(pbrProg->progId);
168
169         size_t numLights = lights.size() + (discoLights ? 3 : 0);
170         glm::vec3 lightPositions[numLights], lightColors[numLights];
171         for (int i = 0; i < lights.size(); i++) {
172                 lightPositions[i] = glm::vec3(lights[i].trans[3]);
173                 lightColors[i] = lights[i].color;
174         }
175
176         if (discoLights) {
177                 for (int i = numLights - 3; i < numLights; i++) {
178                         auto m = glm::translate(glm::mat4(1.f), glm::vec3(-2.5, 0, 0));
179                         m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(1, 0, 0));
180                         m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 1, 0));
181                         m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 0, 1));
182                         lightPositions[i] = glm::vec3(m * glm::vec4(5, 0, 0, 1));
183                         lightColors[i] = glm::vec3(0.2);
184                         if (i % 3 == 0) lightColors[i].x = sin(d);
185                         if (i % 3 == 1) lightColors[i].y = cos(d * 3);
186                         if (i % 3 == 2) lightColors[i].z = cos(d);
187                 }
188         }
189
190         glUniform1ui(glGetUniformLocation(pbrProg->progId, "numLights"), numLights);
191         glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightPositions"), numLights, glm::value_ptr(lightPositions[0]));
192         glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightColors"), numLights, glm::value_ptr(lightColors[0]));
193
194 #ifdef COWEDBOY_IK
195         { 
196                 glm::vec3 targetPos(sin(d) * 2 + 3, -2, 1);
197                 Light targetLight = { glm::translate(glm::mat4(1), targetPos), {0.5, 1, 1}  };
198                 drawLight(targetLight);
199                 inverseKinematics(*sceneModel->find("Shoulder.L"), *sceneModel->find("Finger.L"), targetPos);
200
201                 targetPos = { sin(d * 2) * 2 - 5, 2.5, 0 };
202                 targetLight = { glm::translate(glm::mat4(1), targetPos), {1, 1, 0.5}  };
203                 drawLight(targetLight);
204                 inverseKinematics(*sceneModel->find("Shoulder.R"), *sceneModel->find("Finger.R"), targetPos);
205         }
206 #endif
207
208         if (curMode == Default)
209                 sceneModel->draw(skyboxes[activeSkybox], d * 1000);
210
211         if (curMode == Blendshapes) {
212                 highlightVertex();
213
214                 for (auto v: manipulators)
215                         drawBox(glm::translate(glm::mat4(1), v), {0.2, 1, 0});
216
217                 blendshapes.model->draw(skyboxes[activeSkybox], d * 1000);
218         }
219
220         for (Light &light: lights) drawLight(light);
221
222         // TODO: restore
223         /* if (discoLights) { */
224         /*      for (int i = numLights - 3; i < numLights; i++) { */
225         /*              Light l = { lightPositions[i], lightColors[i] }; */
226         /*              drawLight(l); */
227         /*      } */
228         /* } */
229
230         skyboxes[activeSkybox].draw(projMat(), viewMat());
231
232         glutSwapBuffers();
233 }
234
235 void setupLightBuffers(GLuint progId) {
236         auto vertices = cube();
237         GLuint verticesSize = 36 * 3 * sizeof(GLfloat);
238
239         glGenVertexArrays(1, &lightVao);
240         GLuint vbo;
241         glBindVertexArray(lightVao);
242         glGenBuffers(1, &vbo);
243         glBindBuffer(GL_ARRAY_BUFFER, vbo);
244         glBufferData(GL_ARRAY_BUFFER, verticesSize, NULL, GL_STATIC_DRAW);
245         glBufferSubData(GL_ARRAY_BUFFER, 0, verticesSize, glm::value_ptr(vertices[0]));
246         GLuint posLoc = glGetAttribLocation(progId, "vPosition");
247         glEnableVertexAttribArray(posLoc);
248         glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
249 }
250
251 void weightsChanged(int blendshape, float weight) {
252         blendshapeWeights[blendshape] = weight;
253         std::vector<float> weights;
254         weights.assign(blendshapeWeights, blendshapeWeights + blendshapes.deltas.size());
255         interpolateBlendshapes(&blendshapes, weights);
256 }
257
258 void loadBlendshapes() {
259
260         // get all the obj files
261         std::vector<std::string> blends;
262         const std::string modelDir = "models/high-res-blendshapes/";
263         DIR *blendDir = opendir(modelDir.c_str());
264         while (dirent *e = readdir(blendDir)) {
265                 if (e->d_type & DT_DIR) continue;
266                 const std::string name(e->d_name);
267                 if (name == "neutral.obj") continue;
268                 blends.push_back(name);
269         }
270         closedir(blendDir);
271
272         std::vector<std::string> blendFps;
273         for (auto blend: blends) blendFps.push_back(modelDir + blend);
274         createBlendshapes(blendFps, modelDir + "neutral.obj", *pbrProg, &blendshapes);
275         targetModel = blendshapes.model;
276
277         size_t numBlends = blends.size();
278         blendshapeWeights = new float[numBlends];
279         for (int i = 0; i < numBlends; i++) blendshapeWeights[i] = 0;
280         const char *names[numBlends];
281         for (int i = 0; i < numBlends; i++) names[i] = blends[i].c_str();
282         createControlWindow(numBlends, names, weightsChanged);
283
284         camPos = { 0, 22, 81 };
285         camFront = { 0, 0, -1 };
286         camUp = { 0, 1, 0 };
287         zfar = 10000;
288         znear = 0.1f;
289 }
290
291 void init() {
292         initUtilProg();
293
294         plainProg = new Program("plainvertex.glsl", "plainfrag.glsl");
295         glUseProgram(plainProg->progId);
296         setupLightBuffers(plainProg->progId);
297         plainProg->validate();
298
299         skyboxes.push_back(Skybox(Image("skyboxes/loft/Newport_Loft_Ref.hdr")));
300         skyboxes.push_back(Skybox(Image("skyboxes/wooden_lounge_8k.hdr")));
301         skyboxes.push_back(Skybox(Image("skyboxes/machine_shop_02_8k.hdr")));
302         skyboxes.push_back(Skybox(Image("skyboxes/pink_sunrise_8k.hdr")));
303
304         pbrProg = new Program("pbrvert.glsl", "pbrfrag.glsl");
305         glUseProgram(pbrProg->progId);
306
307         if (curMode == Default) {
308                 const std::string scenePath = "models/cowedboy.glb";
309                 const aiScene *scene = importer.ReadFile(
310                                 scenePath, aiProcess_Triangulate | aiProcess_CalcTangentSpace |
311                                 aiProcess_GenNormals | aiProcess_FlipUVs);
312                 if (!scene) {
313                         std::cerr << importer.GetErrorString() << std::endl;
314                         exit(1);
315                 }
316
317                 if (scene->mNumCameras > 0) {
318                         aiCamera *cam = scene->mCameras[0];
319                         glm::mat4 camTrans;
320                         if (findNodeTrans(scene->mRootNode, cam->mName, &camTrans) != 0)
321                                 abort(); // there must be a node with the same name as camera
322
323                         camPos = {camTrans[3][0], camTrans[3][1], camTrans[3][2]};
324
325                         glm::vec3 camLookAt =
326                                 glm::vec3(cam->mLookAt.x, cam->mLookAt.y, cam->mLookAt.z);
327                         camFront = camLookAt - camPos;
328
329                         camUp = glm::vec3(cam->mUp.x, cam->mUp.y, cam->mUp.z);
330
331                         fov = cam->mHorizontalFOV;
332                         // TODO: aspectRatio = cam->mAspect;
333                         znear = cam->mClipPlaneNear;
334                         zfar = cam->mClipPlaneFar;
335                 }
336
337                 for (int i = 0; i < scene->mNumLights; i++) {
338                         aiLight *light = scene->mLights[i];
339                         glm::mat4 trans;
340                         findNodeTrans(scene->mRootNode, light->mName, &trans);
341                         glm::vec3 col = {light->mColorAmbient.r, light->mColorAmbient.g,
342                                 light->mColorAmbient.b};
343                         Light l = {trans, col};
344                         lights.push_back(l);
345                 }
346
347                 sceneModel = new Model(scene, *pbrProg);
348         }
349
350         if (curMode == Blendshapes) {
351                 loadBlendshapes();
352         }
353
354         glEnable(GL_DEPTH_TEST); 
355         glEnable(GL_CULL_FACE); 
356         // prevent edge artifacts in specular cubemaps
357         glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
358
359         glViewport(0, 0, windowWidth * 2, windowHeight * 2);
360 }
361
362 bool keyStates[256] = {false};
363
364 void keyboard(unsigned char key, int x, int y) {
365         keyStates[key] = true;
366         if (key == 'z')
367                 activeSkybox = (activeSkybox + 1) % skyboxes.size();
368         if (key == 'c')
369                 discoLights = !discoLights;
370 }
371
372 void keyboardUp(unsigned char key, int x, int y) {
373         keyStates[key] = false;
374 }
375
376 int mouseX, mouseY;
377 bool needToCalculateClosestVertex = false;
378
379 /* #define ENABLE_MOVEMENT */
380 void timer(int _) {
381 #ifdef ENABLE_MOVEMENT
382         float xSpeed = 0.f, ySpeed = 0.f, zSpeed = 0.f;
383
384 #pragma clang diagnostic push
385 #pragma clang diagnostic ignored "-Wchar-subscripts"
386         if (keyStates['w'])
387                 zSpeed = 0.1f;
388         if (keyStates['s'])
389                 zSpeed = -0.1f;
390         if (keyStates['a'])
391                 xSpeed = 0.1f;
392         if (keyStates['d'])
393                 xSpeed = -0.1f;
394         if (keyStates['q'])
395                 ySpeed = 0.1f;
396         if (keyStates['e'])
397                 ySpeed = -0.1f;
398 #pragma clang diagnostic pop
399
400         camPos.x += xSpeed * sin(yaw) + zSpeed * cos(yaw);
401         camPos.y += ySpeed;
402         camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw);
403 #endif
404
405         if (needToCalculateClosestVertex) {
406                 GLint vpArr[4]; glGetIntegerv(GL_VIEWPORT, vpArr);
407                 glm::vec4 viewport(vpArr[0], vpArr[1], vpArr[2], vpArr[3]);
408                 glm::vec3 selectedPos = glm::unProject(glm::vec3(mouseX * 2, viewport[3] - mouseY * 2, 1), // hidpi
409                                 viewMat(),
410                                 projMat(),
411                                 viewport);
412
413                 closestVertex = targetModel->closestVertex(targetModel->getRoot(), camPos, selectedPos).first;
414                 needToCalculateClosestVertex = false;
415         }
416
417         glutPostRedisplay();
418         glutTimerFunc(16, timer, 0);
419 }
420
421 int prevMouseX, prevMouseY;
422 bool firstMouse = true;
423
424 void motion(int x, int y) {
425 #ifdef ENABLE_MOVEMENT
426         if (firstMouse) {
427                 prevMouseX = x;
428                 prevMouseY = y;
429                 firstMouse = false;
430         }
431         int dx = x - prevMouseX, dy = y - prevMouseY;
432
433         prevMouseX = x;
434         prevMouseY = y;
435
436         const float sensitivity = 0.005f;
437         yaw += dx * sensitivity;
438         pitch -= dy * sensitivity;
439
440         glm::vec3 front;
441         front.x = cos(pitch) * cos(yaw);
442         front.y = sin(pitch);
443         front.z = cos(pitch) * sin(yaw);
444         camFront = glm::normalize(front);
445
446         if (pitch < -1.57079632679 || pitch >= 1.57079632679) {
447                 camUp = glm::vec3(0, -1, 0);
448         } else {
449                 camUp = glm::vec3(0, 1, 0);
450         }
451 #endif
452
453         mouseX = x; mouseY = y;
454         needToCalculateClosestVertex = true;
455
456 }
457
458 void mouse(int button, int state, int x, int y) {
459         if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
460                 manipulators.push_back(closestVertex);
461
462 #ifdef ENABLE_MOVEMENT
463         if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
464                 firstMouse = true;
465 #endif
466 }
467
468 void reshape(int newWidth, int newHeight) {
469         windowWidth = newWidth, windowHeight = newHeight;
470 }
471
472 int main(int argc, char** argv) {
473         glutInit(&argc, argv);
474         glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);
475         glutInitWindowSize(windowWidth, windowHeight);
476         glutCreateWindow("Physically Based Rendering");
477         glutDisplayFunc(display);
478         glutReshapeFunc(reshape);
479
480         glewInit();
481
482         // TODO: parse argv
483         curMode = Blendshapes;
484         init();
485
486         glutKeyboardFunc(keyboard);
487         glutKeyboardUpFunc(keyboardUp);
488         glutTimerFunc(16, timer, 0);
489         glutMotionFunc(motion);
490         glutPassiveMotionFunc(motion);
491         glutMouseFunc(mouse);
492
493         glutMainLoop();
494
495         return 0;
496 }
497