10 #include <OpenGL/glew.h>
12 #include <GLUT/glut.h>
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>
21 #include "program.hpp"
26 #include "blendshapes.hpp"
29 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
35 Program *textureProg, *plainProg, *reflectProg, *pbrProg;
37 std::vector<Skybox> skyboxes;
40 Assimp::Importer importer; // Need to keep this around, otherwise stuff disappears!
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;
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;
58 std::vector<Light> lights;
60 bool discoLights = false;
62 int windowWidth = 800, windowHeight = 600;
71 return (float)windowWidth / (float)windowHeight;
74 inline glm::mat4 projMat() {
75 return glm::perspective(fov, aspect(), znear, zfar);
78 inline glm::mat4 viewMat() {
79 return glm::lookAt(camPos, camPos + camFront, camUp);
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));
87 GLuint viewLoc = glGetUniformLocation(progId, "view");
88 glm::mat4 view = viewMat();
89 glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
91 GLuint camPosLoc = glGetUniformLocation(progId, "camPos");
92 glUniform3fv(camPosLoc, 1, glm::value_ptr(camPos));
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));
99 GLuint lightPosLoc = glGetUniformLocation(progId, "vLightPos");
100 glUniform3fv(lightPosLoc, 1, glm::value_ptr(lightPos));
102 GLuint viewPosLoc = glGetUniformLocation(progId, "vViewPos");
103 glUniform3fv(viewPosLoc, 1, glm::value_ptr(camPos));
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));
114 GLuint colorLoc = glGetUniformLocation(plainProg->progId, "color");
115 glUniform4fv(colorLoc, 1, glm::value_ptr(color));
117 glDrawArrays(GL_TRIANGLES, 0, 36);
120 void drawLight(Light &light) {
121 drawBox(light.trans, light.color);
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);
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);
139 glm::mat4 worldSpaceToModelSpace(aiNode *node, glm::mat4 m) {
140 aiNode *parent = node;
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;
148 while (!trans.empty()) { res = trans.back() * res; trans.pop_back(); }
152 void highlightVertex() {
153 drawBox(glm::translate(glm::mat4(1), closestVertex), {1, 1, 0.5});
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);
161 float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
163 glUseProgram(getUtilProg()->progId);
164 setProjectionAndViewUniforms(getUtilProg()->progId);
166 glUseProgram(pbrProg->progId);
167 setProjectionAndViewUniforms(pbrProg->progId);
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;
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);
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]));
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);
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);
208 if (curMode == Default)
209 sceneModel->draw(skyboxes[activeSkybox], d * 1000);
211 if (curMode == Blendshapes) {
214 for (auto v: manipulators)
215 drawBox(glm::translate(glm::mat4(1), v), {0.2, 1, 0});
217 blendshapes.model->draw(skyboxes[activeSkybox], d * 1000);
220 for (Light &light: lights) drawLight(light);
223 /* if (discoLights) { */
224 /* for (int i = numLights - 3; i < numLights; i++) { */
225 /* Light l = { lightPositions[i], lightColors[i] }; */
230 skyboxes[activeSkybox].draw(projMat(), viewMat());
235 void setupLightBuffers(GLuint progId) {
236 auto vertices = cube();
237 GLuint verticesSize = 36 * 3 * sizeof(GLfloat);
239 glGenVertexArrays(1, &lightVao);
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);
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);
258 void loadBlendshapes() {
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);
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;
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);
284 camPos = { 0, 22, 81 };
285 camFront = { 0, 0, -1 };
294 plainProg = new Program("plainvertex.glsl", "plainfrag.glsl");
295 glUseProgram(plainProg->progId);
296 setupLightBuffers(plainProg->progId);
297 plainProg->validate();
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")));
304 pbrProg = new Program("pbrvert.glsl", "pbrfrag.glsl");
305 glUseProgram(pbrProg->progId);
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);
313 std::cerr << importer.GetErrorString() << std::endl;
317 if (scene->mNumCameras > 0) {
318 aiCamera *cam = scene->mCameras[0];
320 if (findNodeTrans(scene->mRootNode, cam->mName, &camTrans) != 0)
321 abort(); // there must be a node with the same name as camera
323 camPos = {camTrans[3][0], camTrans[3][1], camTrans[3][2]};
325 glm::vec3 camLookAt =
326 glm::vec3(cam->mLookAt.x, cam->mLookAt.y, cam->mLookAt.z);
327 camFront = camLookAt - camPos;
329 camUp = glm::vec3(cam->mUp.x, cam->mUp.y, cam->mUp.z);
331 fov = cam->mHorizontalFOV;
332 // TODO: aspectRatio = cam->mAspect;
333 znear = cam->mClipPlaneNear;
334 zfar = cam->mClipPlaneFar;
337 for (int i = 0; i < scene->mNumLights; i++) {
338 aiLight *light = scene->mLights[i];
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};
347 sceneModel = new Model(scene, *pbrProg);
350 if (curMode == Blendshapes) {
354 glEnable(GL_DEPTH_TEST);
355 glEnable(GL_CULL_FACE);
356 // prevent edge artifacts in specular cubemaps
357 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
359 glViewport(0, 0, windowWidth * 2, windowHeight * 2);
362 bool keyStates[256] = {false};
364 void keyboard(unsigned char key, int x, int y) {
365 keyStates[key] = true;
367 activeSkybox = (activeSkybox + 1) % skyboxes.size();
369 discoLights = !discoLights;
372 void keyboardUp(unsigned char key, int x, int y) {
373 keyStates[key] = false;
377 bool needToCalculateClosestVertex = false;
379 /* #define ENABLE_MOVEMENT */
381 #ifdef ENABLE_MOVEMENT
382 float xSpeed = 0.f, ySpeed = 0.f, zSpeed = 0.f;
384 #pragma clang diagnostic push
385 #pragma clang diagnostic ignored "-Wchar-subscripts"
398 #pragma clang diagnostic pop
400 camPos.x += xSpeed * sin(yaw) + zSpeed * cos(yaw);
402 camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw);
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
413 closestVertex = targetModel->closestVertex(targetModel->getRoot(), camPos, selectedPos).first;
414 needToCalculateClosestVertex = false;
418 glutTimerFunc(16, timer, 0);
421 int prevMouseX, prevMouseY;
422 bool firstMouse = true;
424 void motion(int x, int y) {
425 #ifdef ENABLE_MOVEMENT
431 int dx = x - prevMouseX, dy = y - prevMouseY;
436 const float sensitivity = 0.005f;
437 yaw += dx * sensitivity;
438 pitch -= dy * sensitivity;
441 front.x = cos(pitch) * cos(yaw);
442 front.y = sin(pitch);
443 front.z = cos(pitch) * sin(yaw);
444 camFront = glm::normalize(front);
446 if (pitch < -1.57079632679 || pitch >= 1.57079632679) {
447 camUp = glm::vec3(0, -1, 0);
449 camUp = glm::vec3(0, 1, 0);
453 mouseX = x; mouseY = y;
454 needToCalculateClosestVertex = true;
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);
462 #ifdef ENABLE_MOVEMENT
463 if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
468 void reshape(int newWidth, int newHeight) {
469 windowWidth = newWidth, windowHeight = newHeight;
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);
483 curMode = Blendshapes;
486 glutKeyboardFunc(keyboard);
487 glutKeyboardUpFunc(keyboardUp);
488 glutTimerFunc(16, timer, 0);
489 glutMotionFunc(motion);
490 glutPassiveMotionFunc(motion);
491 glutMouseFunc(mouse);