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 ControlWindow controlWindow;
39 std::vector<Skybox> skyboxes;
42 Assimp::Importer importer; // Need to keep this around, otherwise stuff disappears!
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;
49 Model *targetModel; // The model that the selection is happening on
50 Model::VertexLookup closestVertex;
52 std::map<VertIdx, glm::vec3> manipulators;
53 VertIdx curManipulator = {-1,-1};
55 BlendshapeModel bsModel;
56 bool playBlendshapeAnim = false;
63 std::vector<Light> lights;
65 bool discoLights = false;
67 int windowWidth = 800, windowHeight = 600;
76 return (float)windowWidth / (float)windowHeight;
79 inline glm::mat4 projMat() {
80 return glm::perspective(fov, aspect(), znear, zfar);
83 inline glm::mat4 viewMat() {
84 return glm::lookAt(camPos, camPos + camFront, camUp);
87 void setProjectionAndViewUniforms(GLuint progId) {
88 GLuint projLoc = glGetUniformLocation(progId, "projection");
89 glm::mat4 proj = projMat();
90 glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));
92 GLuint viewLoc = glGetUniformLocation(progId, "view");
93 glm::mat4 view = viewMat();
94 glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
96 GLuint camPosLoc = glGetUniformLocation(progId, "camPos");
97 glUniform3fv(camPosLoc, 1, glm::value_ptr(camPos));
100 void setLightColorAndPos(GLuint progId, glm::vec3 lightPos, glm::vec4 lightColor) {
101 GLuint lightColorLoc = glGetUniformLocation(progId, "lightColor");
102 glUniform4fv(lightColorLoc, 1, glm::value_ptr(lightColor));
104 GLuint lightPosLoc = glGetUniformLocation(progId, "vLightPos");
105 glUniform3fv(lightPosLoc, 1, glm::value_ptr(lightPos));
107 GLuint viewPosLoc = glGetUniformLocation(progId, "vViewPos");
108 glUniform3fv(viewPosLoc, 1, glm::value_ptr(camPos));
111 void drawBox(glm::mat4 trans, glm::vec3 color) {
112 glUseProgram(plainProg->progId);
113 glBindVertexArray(lightVao);
114 setProjectionAndViewUniforms(plainProg->progId);
115 glm::mat4 model = glm::scale(trans, glm::vec3(0.3));
116 GLuint modelLoc = glGetUniformLocation(plainProg->progId, "model");
117 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
119 GLuint colorLoc = glGetUniformLocation(plainProg->progId, "color");
120 glUniform4fv(colorLoc, 1, glm::value_ptr(color));
122 glDrawArrays(GL_TRIANGLES, 0, 36);
125 void drawLight(Light &light) {
126 drawBox(light.trans, light.color);
129 int findNodeTrans(const struct aiNode *n, const struct aiString name, glm::mat4 *dest) {
130 if (strcmp(n->mName.data, name.data) == 0) {
131 *dest = aiMatrixToMat4(n->mTransformation);
134 for (int i = 0; i < n->mNumChildren; i++) {
135 if (findNodeTrans(n->mChildren[i], name, dest) == 0) {
136 glm::mat4 t = aiMatrixToMat4(n->mTransformation);
144 glm::mat4 worldSpaceToModelSpace(aiNode *node, glm::mat4 m) {
145 aiNode *parent = node;
147 std::vector<glm::mat4> trans;
148 while (parent != nullptr) {
149 /* res = res * glm::inverse(aiMatrixToMat4(parent->mTransformation)); */
150 trans.push_back(glm::inverse(aiMatrixToMat4(parent->mTransformation)));
151 parent = parent->mParent;
153 while (!trans.empty()) { res = trans.back() * res; trans.pop_back(); }
157 void highlightVertex() {
158 drawBox(glm::translate(glm::mat4(1), closestVertex.pos), {1, 1, 0.5});
162 glClearColor(0.5, 0.5, 0.5, 1);
163 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
164 glViewport(0, 0, windowWidth * 2, windowHeight * 2);
166 float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
168 glUseProgram(getUtilProg()->progId);
169 setProjectionAndViewUniforms(getUtilProg()->progId);
171 glUseProgram(pbrProg->progId);
172 setProjectionAndViewUniforms(pbrProg->progId);
174 size_t numLights = lights.size() + (discoLights ? 3 : 0);
175 glm::vec3 lightPositions[numLights], lightColors[numLights];
176 for (int i = 0; i < lights.size(); i++) {
177 lightPositions[i] = glm::vec3(lights[i].trans[3]);
178 lightColors[i] = lights[i].color;
182 for (int i = numLights - 3; i < numLights; i++) {
183 auto m = glm::translate(glm::mat4(1.f), glm::vec3(-2.5, 0, 0));
184 m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(1, 0, 0));
185 m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 1, 0));
186 m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 0, 1));
187 lightPositions[i] = glm::vec3(m * glm::vec4(5, 0, 0, 1));
188 lightColors[i] = glm::vec3(0.2);
189 if (i % 3 == 0) lightColors[i].x = sin(d);
190 if (i % 3 == 1) lightColors[i].y = cos(d * 3);
191 if (i % 3 == 2) lightColors[i].z = cos(d);
195 glUniform1ui(glGetUniformLocation(pbrProg->progId, "numLights"), numLights);
196 glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightPositions"), numLights, glm::value_ptr(lightPositions[0]));
197 glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightColors"), numLights, glm::value_ptr(lightColors[0]));
201 glm::vec3 targetPos(sin(d) * 2 + 3, -2, 1);
202 Light targetLight = { glm::translate(glm::mat4(1), targetPos), {0.5, 1, 1} };
203 drawLight(targetLight);
204 inverseKinematics(*sceneModel->find("Shoulder.L"), *sceneModel->find("Finger.L"), targetPos);
206 targetPos = { sin(d * 2) * 2 - 5, 2.5, 0 };
207 targetLight = { glm::translate(glm::mat4(1), targetPos), {1, 1, 0.5} };
208 drawLight(targetLight);
209 inverseKinematics(*sceneModel->find("Shoulder.R"), *sceneModel->find("Finger.R"), targetPos);
213 if (curMode == Default)
214 sceneModel->draw(skyboxes[activeSkybox], d * 1000);
216 if (curMode == Blendshapes) {
219 for (auto v: manipulators) {
220 glm::vec3 color = { 0.4, 1, 0 };
221 if (closestVertex.meshIdx == v.first.first &&
222 closestVertex.vertIdx == v.first.second)
224 drawBox(glm::translate(glm::mat4(1), v.second), color);
226 glm::vec3 origVertex = aiVector3DToVec3(bsModel.model->meshes[v.first.first].ai.mVertices[v.first.second]);
227 drawBox(glm::translate(glm::mat4(1), origVertex), {0,0,1});
230 bsModel.model->draw(skyboxes[activeSkybox], d * 1000);
233 for (Light &light: lights) drawLight(light);
236 /* if (discoLights) { */
237 /* for (int i = numLights - 3; i < numLights; i++) { */
238 /* Light l = { lightPositions[i], lightColors[i] }; */
243 skyboxes[activeSkybox].draw(projMat(), viewMat());
248 void setupLightBuffers(GLuint progId) {
249 auto vertices = cube();
250 GLuint verticesSize = 36 * 3 * sizeof(GLfloat);
252 glGenVertexArrays(1, &lightVao);
254 glBindVertexArray(lightVao);
255 glGenBuffers(1, &vbo);
256 glBindBuffer(GL_ARRAY_BUFFER, vbo);
257 glBufferData(GL_ARRAY_BUFFER, verticesSize, NULL, GL_STATIC_DRAW);
258 glBufferSubData(GL_ARRAY_BUFFER, 0, verticesSize, glm::value_ptr(vertices[0]));
259 GLuint posLoc = glGetAttribLocation(progId, "vPosition");
260 glEnableVertexAttribArray(posLoc);
261 glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
264 bool needToCalculateClosestVertex = false;
265 bool needToInterpolateBlendshapes = false;
267 class Delegate : public ControlWindowDelegate {
270 virtual void weightChanged(int blendshape, float weight) {
271 bsModel.blendshapes[blendshape].weight = weight;
272 needToInterpolateBlendshapes = true;
275 virtual void solveWeights(std::vector<float> &newWeights) {
276 ::solveWeights(&bsModel, manipulators);
277 for (int i = 0; i < newWeights.size(); i++)
278 newWeights[i] = bsModel.blendshapes[i].weight;
279 needToInterpolateBlendshapes = true;
282 virtual void resetManipulators() {
283 manipulators.clear();
284 curManipulator = { -1, -1 };
287 virtual void playbackChanged(bool playing) {
288 playBlendshapeAnim = playing;
298 plainProg = new Program("plainvertex.glsl", "plainfrag.glsl");
299 glUseProgram(plainProg->progId);
300 setupLightBuffers(plainProg->progId);
301 plainProg->validate();
303 skyboxes.push_back(Skybox(Image("skyboxes/loft/Newport_Loft_Ref.hdr")));
304 skyboxes.push_back(Skybox(Image("skyboxes/wooden_lounge_8k.hdr")));
305 skyboxes.push_back(Skybox(Image("skyboxes/machine_shop_02_8k.hdr")));
306 skyboxes.push_back(Skybox(Image("skyboxes/pink_sunrise_8k.hdr")));
308 pbrProg = new Program("pbrvert.glsl", "pbrfrag.glsl");
309 glUseProgram(pbrProg->progId);
311 if (curMode == Default) {
312 const std::string scenePath = "models/cowedboy.glb";
313 const aiScene *scene = importer.ReadFile(
314 scenePath, aiProcess_Triangulate | aiProcess_CalcTangentSpace |
315 aiProcess_GenNormals | aiProcess_FlipUVs);
317 std::cerr << importer.GetErrorString() << std::endl;
321 if (scene->mNumCameras > 0) {
322 aiCamera *cam = scene->mCameras[0];
324 if (findNodeTrans(scene->mRootNode, cam->mName, &camTrans) != 0)
325 abort(); // there must be a node with the same name as camera
327 camPos = {camTrans[3][0], camTrans[3][1], camTrans[3][2]};
329 glm::vec3 camLookAt =
330 glm::vec3(cam->mLookAt.x, cam->mLookAt.y, cam->mLookAt.z);
331 camFront = camLookAt - camPos;
333 camUp = glm::vec3(cam->mUp.x, cam->mUp.y, cam->mUp.z);
335 fov = cam->mHorizontalFOV;
336 // TODO: aspectRatio = cam->mAspect;
337 znear = cam->mClipPlaneNear;
338 zfar = cam->mClipPlaneFar;
341 for (int i = 0; i < scene->mNumLights; i++) {
342 aiLight *light = scene->mLights[i];
344 findNodeTrans(scene->mRootNode, light->mName, &trans);
345 glm::vec3 col = {light->mColorAmbient.r, light->mColorAmbient.g,
346 light->mColorAmbient.b};
347 Light l = {trans, col};
351 sceneModel = new Model(scene, *pbrProg);
354 if (curMode == Blendshapes) {
355 loadBlendshapes("models/high-res-blendshapes/", *pbrProg, &bsModel);
356 targetModel = bsModel.model;
358 size_t numBlends = bsModel.blendshapes.size();
359 std::vector<std::string> names(numBlends);
360 for (int i = 0; i < numBlends; i++) names[i] = bsModel.blendshapes[i].name;
361 controlWindow = createControlWindow(names, &cwDelegate);
363 camPos = { 0, 22, 81 };
364 camFront = { 0, 0, -1 };
370 glEnable(GL_DEPTH_TEST);
371 glEnable(GL_CULL_FACE);
372 // prevent edge artifacts in specular cubemaps
373 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
375 glViewport(0, 0, windowWidth * 2, windowHeight * 2);
378 bool keyStates[256] = {false};
380 void keyboard(unsigned char key, int x, int y) {
381 keyStates[key] = true;
383 activeSkybox = (activeSkybox + 1) % skyboxes.size();
385 discoLights = !discoLights;
388 void keyboardUp(unsigned char key, int x, int y) {
389 keyStates[key] = false;
394 /* #define ENABLE_MOVEMENT */
396 #ifdef ENABLE_MOVEMENT
397 float xSpeed = 0.f, ySpeed = 0.f, zSpeed = 0.f;
399 #pragma clang diagnostic push
400 #pragma clang diagnostic ignored "-Wchar-subscripts"
413 #pragma clang diagnostic pop
415 camPos.x += xSpeed * sin(yaw) + zSpeed * cos(yaw);
417 camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw);
420 if (curMode == Blendshapes) {
421 float xSpeed = 0, ySpeed = 0, zSpeed = 0;
422 #pragma clang diagnostic push
423 #pragma clang diagnostic ignored "-Wchar-subscripts"
436 #pragma clang diagnostic pop
438 if (playBlendshapeAnim) {
439 stepBlendshapeAnim(&bsModel);
440 needToInterpolateBlendshapes = true;
441 std::vector<float> newWeights(bsModel.blendshapes.size());
442 for (int i = 0; i < bsModel.blendshapes.size(); i++)
443 newWeights[i] = bsModel.blendshapes[i].weight;
444 updateWeights(&controlWindow, newWeights);
447 if (curManipulator.first != -1 && curManipulator.second != -1) {
448 manipulators[curManipulator].x += xSpeed;
449 manipulators[curManipulator].y += ySpeed;
450 manipulators[curManipulator].z += zSpeed;
453 if (needToInterpolateBlendshapes) {
454 interpolateBlendshapes(&bsModel);
455 needToInterpolateBlendshapes = false;
458 if (needToCalculateClosestVertex) {
459 GLint vpArr[4]; glGetIntegerv(GL_VIEWPORT, vpArr);
460 glm::vec4 viewport(vpArr[0], vpArr[1], vpArr[2], vpArr[3]);
461 glm::vec3 selectedPos = glm::unProject(glm::vec3(mouseX * 2, viewport[3] - mouseY * 2, 1), // hidpi
466 closestVertex = targetModel->closestVertex(targetModel->getRoot(), camPos, selectedPos);
467 needToCalculateClosestVertex = false;
472 glutTimerFunc(16, timer, 0);
475 int prevMouseX, prevMouseY;
476 bool firstMouse = true;
478 void motion(int x, int y) {
479 #ifdef ENABLE_MOVEMENT
485 int dx = x - prevMouseX, dy = y - prevMouseY;
490 const float sensitivity = 0.005f;
491 yaw += dx * sensitivity;
492 pitch -= dy * sensitivity;
495 front.x = cos(pitch) * cos(yaw);
496 front.y = sin(pitch);
497 front.z = cos(pitch) * sin(yaw);
498 camFront = glm::normalize(front);
500 if (pitch < -1.57079632679 || pitch >= 1.57079632679) {
501 camUp = glm::vec3(0, -1, 0);
503 camUp = glm::vec3(0, 1, 0);
507 mouseX = x; mouseY = y;
508 needToCalculateClosestVertex = true;
512 void mouse(int button, int state, int x, int y) {
513 if (isPanelFocused(controlWindow))
515 if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
516 VertIdx idx = { closestVertex.meshIdx, closestVertex.vertIdx };
517 if (manipulators.count(idx) <= 0)
518 manipulators[idx] = closestVertex.pos;
519 curManipulator = idx;
522 #ifdef ENABLE_MOVEMENT
523 if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
528 void reshape(int newWidth, int newHeight) {
529 windowWidth = newWidth, windowHeight = newHeight;
532 int main(int argc, char** argv) {
533 glutInit(&argc, argv);
534 glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);
535 glutInitWindowSize(windowWidth, windowHeight);
536 glutCreateWindow("Physically Based Rendering");
537 glutDisplayFunc(display);
538 glutReshapeFunc(reshape);
543 curMode = Blendshapes;
546 glutKeyboardFunc(keyboard);
547 glutKeyboardUpFunc(keyboardUp);
548 glutTimerFunc(16, timer, 0);
549 glutMotionFunc(motion);
550 glutPassiveMotionFunc(motion);
551 glutMouseFunc(mouse);