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>
18 #include "program.hpp"
22 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
28 Program *textureProg, *plainProg, *reflectProg, *pbrProg;
30 std::vector<Skybox> skyboxes;
33 Model *chest, *mirrorCube, *pbr;
35 glm::vec3 camPos = glm::vec3(0.0f, 0.0f, -5.f);
36 glm::vec3 camFront = glm::vec3(0.0f, 0.0f, 1.0f);
37 glm::vec3 camUp = glm::vec3(0.0f, 1.0f, 0.0f);
38 float yaw = 1.57, pitch = 0;
45 std::vector<Light> lights = {
46 { glm::vec3(0, 0, 3), glm::vec3(1) },
47 { glm::vec3(0, 3, 0), glm::vec3(1) },
48 { glm::vec3(3, 0, 0), glm::vec3(1) },
49 { glm::vec3(3, 0, 0), glm::vec3(1) }
54 int windowWidth = 800, windowHeight = 600;
57 return (float)windowWidth / (float)windowHeight;
61 return glm::perspective(glm::radians(45.f), aspect(), 0.01f, 10000.f);
65 return glm::lookAt(camPos, camPos + camFront, camUp);
68 void setProjectionAndViewUniforms(GLuint progId) {
69 GLuint projLoc = glGetUniformLocation(progId, "projection");
70 glm::mat4 proj = projMat();
71 glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));
73 GLuint viewLoc = glGetUniformLocation(progId, "view");
74 glm::mat4 view = viewMat();
75 glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
77 GLuint camPosLoc = glGetUniformLocation(progId, "camPos");
78 glUniform3fv(camPosLoc, 1, glm::value_ptr(camPos));
81 void setLightColorAndPos(GLuint progId, glm::vec3 lightPos, glm::vec4 lightColor) {
82 GLuint lightColorLoc = glGetUniformLocation(progId, "lightColor");
83 glUniform4fv(lightColorLoc, 1, glm::value_ptr(lightColor));
85 GLuint lightPosLoc = glGetUniformLocation(progId, "vLightPos");
86 glUniform3fv(lightPosLoc, 1, glm::value_ptr(lightPos));
88 GLuint viewPosLoc = glGetUniformLocation(progId, "vViewPos");
89 glUniform3fv(viewPosLoc, 1, glm::value_ptr(camPos));
92 void drawLight(Light &light) {
93 glUseProgram(plainProg->progId);
94 glBindVertexArray(lightVao);
95 setProjectionAndViewUniforms(plainProg->progId);
96 glm::mat4 model = glm::translate(glm::mat4(1.f), light.pos);
97 model = glm::scale(model, glm::vec3(0.2));
98 GLuint modelLoc = glGetUniformLocation(plainProg->progId, "model");
99 glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
101 GLuint colorLoc = glGetUniformLocation(plainProg->progId, "color");
102 glUniform4fv(colorLoc, 1, glm::value_ptr(light.color));
104 glDrawArrays(GL_TRIANGLES, 0, 36);
109 glClearColor(0.5, 0.5, 0.5, 1);
110 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
111 glViewport(0, 0, windowWidth * 2, windowHeight * 2);
113 float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
115 glUseProgram(pbrProg->progId);
116 setProjectionAndViewUniforms(pbrProg->progId);
118 glm::vec3 lightPositions[4], lightColors[4];
119 for (int i = 0; i < 4; i++) {
120 lightPositions[i] = lights[i].pos;
121 lightColors[i] = lights[i].color;
124 glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightPositions"), 4, glm::value_ptr(lightPositions[0]));
125 glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightColors"), 4, glm::value_ptr(lightColors[0]));
127 pbr->draw(skyboxes[activeSkybox], d * 1000);
129 for (Light &light: lights) drawLight(light);
131 skyboxes[activeSkybox].draw(projMat(), viewMat());
136 void setupLightBuffers(GLuint progId) {
137 auto vertices = cube();
138 GLuint verticesSize = 36 * 3 * sizeof(GLfloat);
140 glGenVertexArrays(1, &lightVao);
142 glBindVertexArray(lightVao);
143 glGenBuffers(1, &vbo);
144 glBindBuffer(GL_ARRAY_BUFFER, vbo);
145 glBufferData(GL_ARRAY_BUFFER, verticesSize, NULL, GL_STATIC_DRAW);
146 glBufferSubData(GL_ARRAY_BUFFER, 0, verticesSize, glm::value_ptr(vertices[0]));
147 GLuint posLoc = glGetAttribLocation(progId, "vPosition");
148 glEnableVertexAttribArray(posLoc);
149 glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
153 plainProg = new Program("plainvertex.glsl", "plainfrag.glsl");
154 glUseProgram(plainProg->progId);
155 setupLightBuffers(plainProg->progId);
156 plainProg->validate();
158 skyboxes.push_back(Skybox(Image("skyboxes/loft/Newport_Loft_Ref.hdr")));
159 skyboxes.push_back(Skybox(Image("skyboxes/monumentValley/Road_to_MonumentValley_Ref.hdr")));
160 skyboxes.push_back(Skybox(Image("skyboxes/factory/Factory_Catwalk_2k.hdr")));
162 pbrProg = new Program("pbrvert.glsl", "pbrfrag.glsl");
163 glUseProgram(pbrProg->progId);
164 pbr = new Model("models/newtonsCradle.gltf", *pbrProg);
166 glEnable(GL_DEPTH_TEST);
167 glEnable(GL_CULL_FACE);
168 // prevent edge artifacts in specular cubemaps
169 glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
171 glViewport(0, 0, windowWidth, windowHeight);
174 bool* keyStates = new bool[256];
176 void keyboard(unsigned char key, int x, int y) {
177 keyStates[key] = true;
179 activeSkybox = (activeSkybox + 1) % skyboxes.size();
181 activeLight = (activeLight + 1) % lights.size();
184 void keyboardUp(unsigned char key, int x, int y) {
185 keyStates[key] = false;
189 float xSpeed = 0.f, ySpeed = 0.f, zSpeed = 0.f;
203 if (keyStates['j']) lights[activeLight].pos.z += 0.1f;
204 if (keyStates['k']) lights[activeLight].pos.z -= 0.1f;
205 if (keyStates['h']) lights[activeLight].pos.x -= 0.1f;
206 if (keyStates['l']) lights[activeLight].pos.x += 0.1f;
207 if (keyStates['m']) lights[activeLight].pos.y -= 0.1f;
208 if (keyStates['n']) lights[activeLight].pos.y += 0.1f;
210 camPos.x += xSpeed * sin(yaw) + zSpeed * cos(yaw);
212 camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw);
214 glutTimerFunc(16, timer, 0);
217 int prevMouseX, prevMouseY;
218 bool firstMouse = true;
220 void motion(int x, int y) {
226 int dx = x - prevMouseX, dy = y - prevMouseY;
231 const float sensitivity = 0.005f;
232 yaw += dx * sensitivity;
233 pitch -= dy * sensitivity;
236 front.x = cos(pitch) * cos(yaw);
237 front.y = sin(pitch);
238 front.z = cos(pitch) * sin(yaw);
239 camFront = glm::normalize(front);
241 if (pitch < -1.57079632679 || pitch >= 1.57079632679) {
242 camUp = glm::vec3(0, -1, 0);
244 camUp = glm::vec3(0, 1, 0);
248 void mouse(int button, int state, int x, int y) {
249 if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
253 void reshape(int newWidth, int newHeight) {
254 windowWidth = newWidth, windowHeight = newHeight;
257 int main(int argc, char** argv) {
258 glutInit(&argc, argv);
259 glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);
260 glutInitWindowSize(windowWidth, windowHeight);
261 int win = glutCreateWindow("Physically Based Rendering");
263 glutDisplayFunc(display);
264 glutReshapeFunc(reshape);
270 glutKeyboardFunc(keyboard);
271 glutKeyboardUpFunc(keyboardUp);
272 glutTimerFunc(16, timer, 0);
273 glutMotionFunc(motion);
274 glutMouseFunc(mouse);