fcc7a1eb90c033f6c1b3a5edec943ff759e16ac8
[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(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) }
50 };
51
52 int activeLight = 0;
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
112         float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
113
114         glUseProgram(pbrProg->progId);
115         setProjectionAndViewUniforms(pbrProg->progId);
116
117         glm::vec3 lightPositions[4], lightColors[4];
118         for (int i = 0; i < 4; i++) {
119                 lightPositions[i] = lights[i].pos;
120                 lightColors[i] = lights[i].color;
121         }
122         
123         glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightPositions"), 4, glm::value_ptr(lightPositions[0]));
124         glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightColors"), 4, glm::value_ptr(lightColors[0]));
125
126         pbr->draw(skyboxes[activeSkybox]);
127
128         for (Light &light: lights) drawLight(light);
129
130         skyboxes[activeSkybox].draw(projMat(), viewMat());
131
132         glutSwapBuffers();
133 }
134
135 void setupLightBuffers(GLuint progId) {
136         auto vertices = cube();
137         GLuint verticesSize = 36 * 3 * sizeof(GLfloat);
138
139         glGenVertexArrays(1, &lightVao);
140         GLuint vbo;
141         glBindVertexArray(lightVao);
142         glGenBuffers(1, &vbo);
143         glBindBuffer(GL_ARRAY_BUFFER, vbo);
144         glBufferData(GL_ARRAY_BUFFER, verticesSize, NULL, GL_STATIC_DRAW);
145         glBufferSubData(GL_ARRAY_BUFFER, 0, verticesSize, glm::value_ptr(vertices[0]));
146         GLuint posLoc = glGetAttribLocation(progId, "vPosition");
147         glEnableVertexAttribArray(posLoc);
148         glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
149 }
150
151 void init() {
152         plainProg = new Program("plainvertex.glsl", "plainfrag.glsl");
153         glUseProgram(plainProg->progId);
154         setupLightBuffers(plainProg->progId);
155         plainProg->validate();
156
157         skyboxes.push_back(Skybox(Image("skyboxes/loft/Newport_Loft_Ref.hdr")));
158         skyboxes.push_back(Skybox(Image("skyboxes/monumentValley/Road_to_MonumentValley_Ref.hdr")));
159         skyboxes.push_back(Skybox(Image("skyboxes/factory/Factory_Catwalk_2k.hdr")));
160
161         pbrProg = new Program("pbrvert.glsl", "pbrfrag.glsl");
162         glUseProgram(pbrProg->progId);
163         pbr = new Model("models/sphereMetal.gltf", *pbrProg);
164
165         glEnable(GL_DEPTH_TEST); 
166         glEnable(GL_CULL_FACE); 
167         // prevent edge artifacts in specular cubemaps
168         glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
169 }
170
171 bool* keyStates = new bool[256];
172
173 void keyboard(unsigned char key, int x, int y) {
174         keyStates[key] = true;
175         if (key == 'z')
176                 activeSkybox = (activeSkybox + 1) % skyboxes.size();
177         if (key == 'x')
178                 activeLight = (activeLight + 1) % lights.size();
179 }
180
181 void keyboardUp(unsigned char key, int x, int y) {
182         keyStates[key] = false;
183 }
184
185 void timer(int _) {
186         float xSpeed = 0.f, ySpeed = 0.f, zSpeed = 0.f;
187         if (keyStates['w'])
188                 zSpeed = 0.1f;
189         if (keyStates['s'])
190                 zSpeed = -0.1f;
191         if (keyStates['a'])
192                 xSpeed = 0.1f;
193         if (keyStates['d'])
194                 xSpeed = -0.1f;
195         if (keyStates['q'])
196                 ySpeed = 0.1f;
197         if (keyStates['e'])
198                 ySpeed = -0.1f;
199
200         if (keyStates['j']) lights[activeLight].pos.z += 0.1f;
201         if (keyStates['k']) lights[activeLight].pos.z -= 0.1f;
202         if (keyStates['h']) lights[activeLight].pos.x -= 0.1f;
203         if (keyStates['l']) lights[activeLight].pos.x += 0.1f;
204         if (keyStates['m']) lights[activeLight].pos.y -= 0.1f;
205         if (keyStates['n']) lights[activeLight].pos.y += 0.1f;
206
207         camPos.x += xSpeed * sin(yaw) + zSpeed * cos(yaw);
208         camPos.y += ySpeed;
209         camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw);
210         glutPostRedisplay();
211         glutTimerFunc(16, timer, 0);
212 }
213
214 int prevMouseX, prevMouseY;
215 bool firstMouse = true;
216
217 void motion(int x, int y) {
218         if (firstMouse) {
219                 prevMouseX = x;
220                 prevMouseY = y;
221                 firstMouse = false;
222         }
223         int dx = x - prevMouseX, dy = y - prevMouseY;
224
225         prevMouseX = x;
226         prevMouseY = y;
227
228         const float sensitivity = 0.005f;
229         yaw += dx * sensitivity;
230         pitch -= dy * sensitivity;
231
232         glm::vec3 front;
233         front.x = cos(pitch) * cos(yaw);
234         front.y = sin(pitch);
235         front.z = cos(pitch) * sin(yaw);
236         camFront = glm::normalize(front);
237
238         if (pitch < -1.57079632679 || pitch >= 1.57079632679) {
239                 camUp = glm::vec3(0, -1, 0);
240         } else {
241                 camUp = glm::vec3(0, 1, 0);
242         }
243 }
244
245 void mouse(int button, int state, int x, int y) {
246         if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
247                 firstMouse = true;
248 }
249
250 void reshape(int newWidth, int newHeight) {
251         glViewport(0, 0, newWidth * 2, newHeight * 2);
252         windowWidth = newWidth, windowHeight = newHeight;
253 }
254
255 int main(int argc, char** argv) {
256         glutInit(&argc, argv);
257         glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);
258         glutInitWindowSize(windowWidth, windowHeight);
259         int win = glutCreateWindow("Physically Based Rendering");
260         glutDisplayFunc(display);
261         glutReshapeFunc(reshape);
262
263         glewInit();
264         
265         init();
266         
267         makeRetina();
268
269         glutKeyboardFunc(keyboard);
270         glutKeyboardUpFunc(keyboardUp);
271         glutTimerFunc(16, timer, 0);
272         glutMotionFunc(motion);
273         glutMouseFunc(mouse);
274
275         glutMainLoop();
276
277         return 0;
278 }
279