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