Final project!
[opengl.git] / main.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <iostream>
4 #include <array>
5 #include <vector>
6 #ifdef __APPLE__
7 #include <GL/glew.h>
8 #include "cocoa.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 "model.hpp"
18 #include "program.hpp"
19 #include "skybox.hpp"
20 #include "image.hpp"
21
22 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
23
24 using namespace std;
25
26 GLuint lightVao;
27
28 Program *textureProg, *plainProg, *reflectProg, *pbrProg;
29
30 std::vector<Skybox> skyboxes;
31 int activeSkybox = 0;
32
33 Model *chest, *mirrorCube, *pbr;
34                           
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;
39
40 struct Light {
41         glm::vec3 pos;
42         glm::vec3 color;
43 };
44
45 std::vector<Light> lights = {
46         { glm::vec3(5, 2, -5), glm::vec3(1) },
47         { glm::vec3(0, 2, -5), glm::vec3(1) },
48         { glm::vec3(-5, 2, -5), glm::vec3(1) },
49 };
50
51 int activeLight = 0;
52
53 bool discoLights = false;
54
55 int windowWidth = 800, windowHeight = 600;
56
57 float aspect() {
58         return (float)windowWidth / (float)windowHeight;
59 }       
60
61 glm::mat4 projMat() {
62         return glm::perspective(glm::radians(45.f), aspect(), 0.01f, 10000.f);
63 }
64
65 glm::mat4 viewMat() {
66         return glm::lookAt(camPos, camPos + camFront, camUp);
67 }
68
69 void setProjectionAndViewUniforms(GLuint progId) {
70         GLuint projLoc = glGetUniformLocation(progId, "projection");
71         glm::mat4 proj = projMat();
72         glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj));
73
74         GLuint viewLoc = glGetUniformLocation(progId, "view");
75         glm::mat4 view = viewMat();
76         glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
77
78         GLuint camPosLoc = glGetUniformLocation(progId, "camPos");
79         glUniform3fv(camPosLoc, 1, glm::value_ptr(camPos));
80 }
81
82 void setLightColorAndPos(GLuint progId, glm::vec3 lightPos, glm::vec4 lightColor) {
83         GLuint lightColorLoc = glGetUniformLocation(progId, "lightColor");
84         glUniform4fv(lightColorLoc, 1, glm::value_ptr(lightColor));
85
86         GLuint lightPosLoc = glGetUniformLocation(progId, "vLightPos");
87         glUniform3fv(lightPosLoc, 1, glm::value_ptr(lightPos));
88
89         GLuint viewPosLoc = glGetUniformLocation(progId, "vViewPos");
90         glUniform3fv(viewPosLoc, 1, glm::value_ptr(camPos));
91 }
92
93 void drawLight(Light &light) {
94         glUseProgram(plainProg->progId);
95         glBindVertexArray(lightVao);
96         setProjectionAndViewUniforms(plainProg->progId);
97         glm::mat4 model = glm::translate(glm::mat4(1.f), light.pos);
98         model = glm::scale(model, glm::vec3(0.2));
99         GLuint modelLoc = glGetUniformLocation(plainProg->progId, "model");
100         glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
101
102         GLuint colorLoc = glGetUniformLocation(plainProg->progId, "color");
103         glUniform4fv(colorLoc, 1, glm::value_ptr(light.color));
104                 
105         glDrawArrays(GL_TRIANGLES, 0, 36);
106 }
107
108
109 void display() {
110         glClearColor(0.5, 0.5, 0.5, 1);
111         glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
112         glViewport(0, 0, windowWidth * 2, windowHeight * 2);
113
114         float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
115
116         glUseProgram(pbrProg->progId);
117         setProjectionAndViewUniforms(pbrProg->progId);
118
119         glm::vec3 lightPositions[6], lightColors[6];
120         for (int i = 0; i < 3; i++) {
121                 lightPositions[i] = lights[i].pos;
122                 lightColors[i] = lights[i].color;
123         }
124
125         for (int i = 3; i < 6; i++) {
126                 if (discoLights) {
127                         auto m = glm::translate(glm::mat4(1.f), glm::vec3(-2.5, 0, 0));
128                         m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(1, 0, 0));
129                         m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 1, 0));
130                         m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 0, 1));
131                         lightPositions[i] = glm::vec3(m * glm::vec4(5, 0, 0, 1));
132                         lightColors[i] = glm::vec3(0.2);
133                         if (i == 3) lightColors[i].x = sin(d);
134                         if (i == 4) lightColors[i].y = cos(d * 3);
135                         if (i == 5) lightColors[i].z = cos(d);
136                 } else {
137                         lightPositions[i] = glm::vec3(0);
138                         lightColors[i] = glm::vec3(0);
139                 }
140         }
141         
142         glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightPositions"), 6, glm::value_ptr(lightPositions[0]));
143         glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightColors"), 6, glm::value_ptr(lightColors[0]));
144
145         /* pbr->getRoot()->model = glm::rotate(glm::mat4(1.f), glm::radians(d * 10), glm::vec3(0, 1, 0)); */
146         pbr->draw(skyboxes[activeSkybox], d * 1000);
147
148         for (Light &light: lights) drawLight(light);
149
150         if (discoLights) {
151                 for (int i = 3; i < 6; i++) {
152                         Light l { lightPositions[i], lightColors[i] };
153                         drawLight(l);
154                 }
155         }
156
157         skyboxes[activeSkybox].draw(projMat(), viewMat());
158
159         glutSwapBuffers();
160 }
161
162 void setupLightBuffers(GLuint progId) {
163         auto vertices = cube();
164         GLuint verticesSize = 36 * 3 * sizeof(GLfloat);
165
166         glGenVertexArrays(1, &lightVao);
167         GLuint vbo;
168         glBindVertexArray(lightVao);
169         glGenBuffers(1, &vbo);
170         glBindBuffer(GL_ARRAY_BUFFER, vbo);
171         glBufferData(GL_ARRAY_BUFFER, verticesSize, NULL, GL_STATIC_DRAW);
172         glBufferSubData(GL_ARRAY_BUFFER, 0, verticesSize, glm::value_ptr(vertices[0]));
173         GLuint posLoc = glGetAttribLocation(progId, "vPosition");
174         glEnableVertexAttribArray(posLoc);
175         glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
176 }
177
178 void init() {
179         plainProg = new Program("plainvertex.glsl", "plainfrag.glsl");
180         glUseProgram(plainProg->progId);
181         setupLightBuffers(plainProg->progId);
182         plainProg->validate();
183
184         skyboxes.push_back(Skybox(Image("skyboxes/loft/Newport_Loft_Ref.hdr")));
185         skyboxes.push_back(Skybox(Image("skyboxes/monumentValley/Road_to_MonumentValley_Ref.hdr")));
186         skyboxes.push_back(Skybox(Image("skyboxes/factory/Factory_Catwalk_2k.hdr")));
187
188         pbrProg = new Program("pbrvert.glsl", "pbrfrag.glsl");
189         glUseProgram(pbrProg->progId);
190         pbr = new Model("models/newtonsCradle.gltf", *pbrProg);
191
192         glEnable(GL_DEPTH_TEST); 
193         glEnable(GL_CULL_FACE); 
194         // prevent edge artifacts in specular cubemaps
195         glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
196
197         glViewport(0, 0, windowWidth, windowHeight);
198 }
199
200 bool* keyStates = new bool[256];
201
202 void keyboard(unsigned char key, int x, int y) {
203         keyStates[key] = true;
204         if (key == 'z')
205                 activeSkybox = (activeSkybox + 1) % skyboxes.size();
206         if (key == 'x')
207                 activeLight = (activeLight + 1) % lights.size();
208         if (key == 'c')
209                 discoLights = !discoLights;
210 }
211
212 void keyboardUp(unsigned char key, int x, int y) {
213         keyStates[key] = false;
214 }
215
216 void timer(int _) {
217         float xSpeed = 0.f, ySpeed = 0.f, zSpeed = 0.f;
218         if (keyStates['w'])
219                 zSpeed = 0.1f;
220         if (keyStates['s'])
221                 zSpeed = -0.1f;
222         if (keyStates['a'])
223                 xSpeed = 0.1f;
224         if (keyStates['d'])
225                 xSpeed = -0.1f;
226         if (keyStates['q'])
227                 ySpeed = 0.1f;
228         if (keyStates['e'])
229                 ySpeed = -0.1f;
230
231         if (keyStates['j']) lights[activeLight].pos.z += 0.1f;
232         if (keyStates['k']) lights[activeLight].pos.z -= 0.1f;
233         if (keyStates['h']) lights[activeLight].pos.x -= 0.1f;
234         if (keyStates['l']) lights[activeLight].pos.x += 0.1f;
235         if (keyStates['m']) lights[activeLight].pos.y -= 0.1f;
236         if (keyStates['n']) lights[activeLight].pos.y += 0.1f;
237
238         camPos.x += xSpeed * sin(yaw) + zSpeed * cos(yaw);
239         camPos.y += ySpeed;
240         camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw);
241         glutPostRedisplay();
242         glutTimerFunc(16, timer, 0);
243 }
244
245 int prevMouseX, prevMouseY;
246 bool firstMouse = true;
247
248 void motion(int x, int y) {
249         if (firstMouse) {
250                 prevMouseX = x;
251                 prevMouseY = y;
252                 firstMouse = false;
253         }
254         int dx = x - prevMouseX, dy = y - prevMouseY;
255
256         prevMouseX = x;
257         prevMouseY = y;
258
259         const float sensitivity = 0.005f;
260         yaw += dx * sensitivity;
261         pitch -= dy * sensitivity;
262
263         glm::vec3 front;
264         front.x = cos(pitch) * cos(yaw);
265         front.y = sin(pitch);
266         front.z = cos(pitch) * sin(yaw);
267         camFront = glm::normalize(front);
268
269         if (pitch < -1.57079632679 || pitch >= 1.57079632679) {
270                 camUp = glm::vec3(0, -1, 0);
271         } else {
272                 camUp = glm::vec3(0, 1, 0);
273         }
274 }
275
276 void mouse(int button, int state, int x, int y) {
277         if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
278                 firstMouse = true;
279 }
280
281 void reshape(int newWidth, int newHeight) {
282         windowWidth = newWidth, windowHeight = newHeight;
283 }
284
285 int main(int argc, char** argv) {
286         glutInit(&argc, argv);
287         glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);
288         glutInitWindowSize(windowWidth, windowHeight);
289         int win = glutCreateWindow("Physically Based Rendering");
290         makeRetina();
291         glutDisplayFunc(display);
292         glutReshapeFunc(reshape);
293
294         glewInit();
295         
296         init();
297
298         glutKeyboardFunc(keyboard);
299         glutKeyboardUpFunc(keyboardUp);
300         glutTimerFunc(16, timer, 0);
301         glutMotionFunc(motion);
302         glutMouseFunc(mouse);
303
304         glutMainLoop();
305
306         return 0;
307 }
308