9 #include <OpenGL/glew.h>
11 #include <GLUT/glut.h>
13 #include <glm/glm.hpp>
14 #include <glm/ext.hpp>
15 #include <glm/gtc/type_ptr.hpp>
16 #include <assimp/Importer.hpp>
17 #include <assimp/scene.h>
18 #include <assimp/postprocess.h>
20 #include "program.hpp"
26 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
32 Program *textureProg, *plainProg, *reflectProg, *pbrProg;
34 std::vector<Skybox> skyboxes;
37 Assimp::Importer importer; // Need to keep this around, otherwise stuff disappears!
40 glm::vec3 camPos = {0, 0, -5}, camFront = {0, 0, 1}, camUp = {0, 1, 0};
41 float fov = glm::radians(30.f), znear = 0.01f, zfar = 10000.f;
42 float yaw = 1.57, pitch = 0;
49 std::vector<Light> lights;
51 bool discoLights = false;
53 int windowWidth = 800, windowHeight = 600;
56 return (float)windowWidth / (float)windowHeight;
60 return glm::perspective(fov, aspect(), znear, zfar);
64 return glm::lookAt(camPos, camPos + camFront, camUp);
67 void setProjectionAndViewUniforms(GLuint progId) {
68 GLuint projLoc = glGetUniformLocation(progId, "projection");
69 glm::mat4 proj = projMat();
70 glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));
72 GLuint viewLoc = glGetUniformLocation(progId, "view");
73 glm::mat4 view = viewMat();
74 glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
76 GLuint camPosLoc = glGetUniformLocation(progId, "camPos");
77 glUniform3fv(camPosLoc, 1, glm::value_ptr(camPos));
80 void setLightColorAndPos(GLuint progId, glm::vec3 lightPos, glm::vec4 lightColor) {
81 GLuint lightColorLoc = glGetUniformLocation(progId, "lightColor");
82 glUniform4fv(lightColorLoc, 1, glm::value_ptr(lightColor));
84 GLuint lightPosLoc = glGetUniformLocation(progId, "vLightPos");
85 glUniform3fv(lightPosLoc, 1, glm::value_ptr(lightPos));
87 GLuint viewPosLoc = glGetUniformLocation(progId, "vViewPos");
88 glUniform3fv(viewPosLoc, 1, glm::value_ptr(camPos));
91 void drawLight(Light &light) {
92 glUseProgram(plainProg->progId);
93 glBindVertexArray(lightVao);
94 setProjectionAndViewUniforms(plainProg->progId);
95 glm::mat4 model = glm::scale(light.trans, glm::vec3(0.2));
96 GLuint modelLoc = glGetUniformLocation(plainProg->progId, "model");
97 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
99 GLuint colorLoc = glGetUniformLocation(plainProg->progId, "color");
100 glUniform4fv(colorLoc, 1, glm::value_ptr(light.color));
102 glDrawArrays(GL_TRIANGLES, 0, 36);
105 int findNodeTrans(const struct aiNode *n, const struct aiString name, glm::mat4 *dest) {
106 if (strcmp(n->mName.data, name.data) == 0) {
107 *dest = aiMatrixToMat4(n->mTransformation);
110 for (int i = 0; i < n->mNumChildren; i++) {
111 if (findNodeTrans(n->mChildren[i], name, dest) == 0) {
112 glm::mat4 t = aiMatrixToMat4(n->mTransformation);
120 glm::mat4 worldSpaceToModelSpace(aiNode *node, glm::mat4 m) {
121 aiNode *parent = node;
123 std::vector<glm::mat4> trans;
124 while (parent != nullptr) {
125 /* res = res * glm::inverse(aiMatrixToMat4(parent->mTransformation)); */
126 trans.push_back(glm::inverse(aiMatrixToMat4(parent->mTransformation)));
127 parent = parent->mParent;
129 while (!trans.empty()) { res = trans.back() * res; trans.pop_back(); }
134 glClearColor(0.5, 0.5, 0.5, 1);
135 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
136 glViewport(0, 0, windowWidth * 2, windowHeight * 2);
138 float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
140 glUseProgram(getUtilProg()->progId);
141 setProjectionAndViewUniforms(getUtilProg()->progId);
143 glUseProgram(pbrProg->progId);
144 setProjectionAndViewUniforms(pbrProg->progId);
146 size_t numLights = lights.size() + (discoLights ? 3 : 0);
147 glm::vec3 lightPositions[numLights], lightColors[numLights];
148 for (int i = 0; i < lights.size(); i++) {
149 lightPositions[i] = glm::vec3(lights[i].trans[3]);
150 lightColors[i] = lights[i].color;
154 for (int i = numLights - 3; i < numLights; i++) {
155 auto m = glm::translate(glm::mat4(1.f), glm::vec3(-2.5, 0, 0));
156 m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(1, 0, 0));
157 m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 1, 0));
158 m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 0, 1));
159 lightPositions[i] = glm::vec3(m * glm::vec4(5, 0, 0, 1));
160 lightColors[i] = glm::vec3(0.2);
161 if (i % 3 == 0) lightColors[i].x = sin(d);
162 if (i % 3 == 1) lightColors[i].y = cos(d * 3);
163 if (i % 3 == 2) lightColors[i].z = cos(d);
167 glUniform1ui(glGetUniformLocation(pbrProg->progId, "numLights"), numLights);
168 glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightPositions"), numLights, glm::value_ptr(lightPositions[0]));
169 glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightColors"), numLights, glm::value_ptr(lightColors[0]));
171 glm::vec3 targetPos(sin(d) * 2 - 2, 2, 0);
172 Light targetLight = { glm::translate(glm::mat4(1), targetPos), {0.5, 1, 1} };
173 drawLight(targetLight);
174 inverseKinematic(*sceneModel->find("Bottom Bone"), *sceneModel->find("Toppest Bone"), targetPos);
176 /* std::array<glm::vec3, 3> jointPositions; std::array<float, 2> jointDistances; */
178 /* std::array<std::string, 3> jointNames = { "Bottom Bone", "Middle Bone", "Top Bone" }; */
179 /* for (int i = 0; i < 3; i++) { */
180 /* glm::mat4 trans; */
181 /* findNodeTrans(&sceneModel->getRoot()->ai, aiString(jointNames[i]), &trans); */
182 /* jointPositions[i] = glm::vec3(trans[3]); */
185 /* jointDistances[i - 1] = glm::distance(jointPositions[i], jointPositions[i - 1]); */
188 /* glm::vec3 targetPos(sin(d * 10.f), cos(d * 10.f), 0); */
189 /* auto newPositions = fabrik(targetPos, jointPositions, jointDistances); */
191 /* for (int i = 0; i < 3; i++) { */
192 /* glm::mat4 absTrans(1); */
193 /* findNodeTrans(&sceneModel->getRoot()->ai, aiString(jointNames[i]), */
195 /* glm::mat4 newAbsTrans = absTrans; */
196 /* newAbsTrans[3] = glm::vec4(newPositions[i], newAbsTrans[3][3]); */
198 /* auto node = sceneModel->getRoot()->ai.FindNode(jointNames[i].c_str()); */
200 /* auto newTrans = worldSpaceToModelSpace(node->mParent, newAbsTrans); */
202 /* node->mTransformation = mat4ToaiMatrix(newTrans); */
204 /* sceneModel->find("Top Bone")->transform = glm::rotate(glm::mat4(1), d / 5.f, { 1, 0, 0}); */
205 /* sceneModel->find("Bottom Bone")->transform = glm::rotate(glm::mat4(1), d / 3.f, { 1, 0, 0}); */
207 sceneModel->draw(skyboxes[activeSkybox], d * 1000);
209 for (Light &light: lights) drawLight(light);
212 /* if (discoLights) { */
213 /* for (int i = numLights - 3; i < numLights; i++) { */
214 /* Light l = { lightPositions[i], lightColors[i] }; */
219 skyboxes[activeSkybox].draw(projMat(), viewMat());
224 void setupLightBuffers(GLuint progId) {
225 auto vertices = cube();
226 GLuint verticesSize = 36 * 3 * sizeof(GLfloat);
228 glGenVertexArrays(1, &lightVao);
230 glBindVertexArray(lightVao);
231 glGenBuffers(1, &vbo);
232 glBindBuffer(GL_ARRAY_BUFFER, vbo);
233 glBufferData(GL_ARRAY_BUFFER, verticesSize, NULL, GL_STATIC_DRAW);
234 glBufferSubData(GL_ARRAY_BUFFER, 0, verticesSize, glm::value_ptr(vertices[0]));
235 GLuint posLoc = glGetAttribLocation(progId, "vPosition");
236 glEnableVertexAttribArray(posLoc);
237 glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
244 plainProg = new Program("plainvertex.glsl", "plainfrag.glsl");
245 glUseProgram(plainProg->progId);
246 setupLightBuffers(plainProg->progId);
247 plainProg->validate();
249 skyboxes.push_back(Skybox(Image("skyboxes/loft/Newport_Loft_Ref.hdr")));
250 skyboxes.push_back(Skybox(Image("skyboxes/wooden_lounge_8k.hdr")));
251 skyboxes.push_back(Skybox(Image("skyboxes/machine_shop_02_8k.hdr")));
252 skyboxes.push_back(Skybox(Image("skyboxes/pink_sunrise_8k.hdr")));
254 pbrProg = new Program("pbrvert.glsl", "pbrfrag.glsl");
255 glUseProgram(pbrProg->progId);
257 const std::string scenePath = "models/ik.glb";
258 const aiScene *scene = importer.ReadFile(scenePath, aiProcess_Triangulate | aiProcess_CalcTangentSpace | aiProcess_GenNormals | aiProcess_FlipUVs);
260 std::cerr << importer.GetErrorString() << std::endl;
264 if (scene->mNumCameras > 0) {
265 aiCamera *cam = scene->mCameras[0];
267 if (findNodeTrans(scene->mRootNode, cam->mName, &camTrans) != 0)
268 abort(); // there must be a node with the same name as camera
270 camPos = { camTrans[3][0], camTrans[3][1], camTrans[3][2] };
272 glm::vec3 camLookAt = glm::vec3(cam->mLookAt.x, cam->mLookAt.y, cam->mLookAt.z);
273 camFront = camLookAt - camPos;
275 camUp = glm::vec3(cam->mUp.x, cam->mUp.y, cam->mUp.z);
277 fov = cam->mHorizontalFOV;
278 // TODO: aspectRatio = cam->mAspect;
279 znear = cam->mClipPlaneNear;
280 zfar = cam->mClipPlaneFar;
283 for (int i = 0; i < scene->mNumLights; i++) {
284 aiLight *light = scene->mLights[i];
285 glm::mat4 trans; findNodeTrans(scene->mRootNode, light->mName, &trans);
286 glm::vec3 col = { light->mColorAmbient.r, light->mColorAmbient.g, light->mColorAmbient.b };
287 Light l = { trans, col };
291 sceneModel = new Model(scene, *pbrProg);
293 glEnable(GL_DEPTH_TEST);
294 glEnable(GL_CULL_FACE);
295 // prevent edge artifacts in specular cubemaps
296 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
298 glViewport(0, 0, windowWidth, windowHeight);
301 bool keyStates[256] = {false};
303 void keyboard(unsigned char key, int x, int y) {
304 keyStates[key] = true;
306 activeSkybox = (activeSkybox + 1) % skyboxes.size();
308 discoLights = !discoLights;
311 void keyboardUp(unsigned char key, int x, int y) {
312 keyStates[key] = false;
315 #define ENABLE_MOVEMENT
318 #ifdef ENABLE_MOVEMENT
319 float xSpeed = 0.f, ySpeed = 0.f, zSpeed = 0.f;
321 #pragma clang diagnostic push
322 #pragma clang diagnostic ignored "-Wchar-subscripts"
335 #pragma clang diagnostic pop
337 camPos.x += xSpeed * sin(yaw) + zSpeed * cos(yaw);
339 camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw);
342 glutTimerFunc(16, timer, 0);
345 int prevMouseX, prevMouseY;
346 bool firstMouse = true;
348 void motion(int x, int y) {
349 #ifdef ENABLE_MOVEMENT
355 int dx = x - prevMouseX, dy = y - prevMouseY;
360 const float sensitivity = 0.005f;
361 yaw += dx * sensitivity;
362 pitch -= dy * sensitivity;
365 front.x = cos(pitch) * cos(yaw);
366 front.y = sin(pitch);
367 front.z = cos(pitch) * sin(yaw);
368 camFront = glm::normalize(front);
370 if (pitch < -1.57079632679 || pitch >= 1.57079632679) {
371 camUp = glm::vec3(0, -1, 0);
373 camUp = glm::vec3(0, 1, 0);
378 void mouse(int button, int state, int x, int y) {
379 if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
383 void reshape(int newWidth, int newHeight) {
384 windowWidth = newWidth, windowHeight = newHeight;
387 int main(int argc, char** argv) {
388 glutInit(&argc, argv);
389 glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);
390 glutInitWindowSize(windowWidth, windowHeight);
391 glutCreateWindow("Physically Based Rendering");
392 glutDisplayFunc(display);
393 glutReshapeFunc(reshape);
399 glutKeyboardFunc(keyboard);
400 glutKeyboardUpFunc(keyboardUp);
401 glutTimerFunc(16, timer, 0);
402 glutMotionFunc(motion);
403 glutMouseFunc(mouse);