Bones and skeletal animation
[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         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[4], lightColors[4];
119         for (int i = 0; i < 4; i++) {
120                 lightPositions[i] = lights[i].pos;
121                 lightColors[i] = lights[i].color;
122         }
123         
124         glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightPositions"), 4, glm::value_ptr(lightPositions[0]));
125         glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightColors"), 4, glm::value_ptr(lightColors[0]));
126
127         pbr->draw(skyboxes[activeSkybox], d * 1000);
128
129         for (Light &light: lights) drawLight(light);
130
131         skyboxes[activeSkybox].draw(projMat(), viewMat());
132
133         glutSwapBuffers();
134 }
135
136 void setupLightBuffers(GLuint progId) {
137         auto vertices = cube();
138         GLuint verticesSize = 36 * 3 * sizeof(GLfloat);
139
140         glGenVertexArrays(1, &lightVao);
141         GLuint vbo;
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);
150 }
151
152 void init() {
153         plainProg = new Program("plainvertex.glsl", "plainfrag.glsl");
154         glUseProgram(plainProg->progId);
155         setupLightBuffers(plainProg->progId);
156         plainProg->validate();
157
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")));
161
162         pbrProg = new Program("pbrvert.glsl", "pbrfrag.glsl");
163         glUseProgram(pbrProg->progId);
164         pbr = new Model("models/newtonsCradle.gltf", *pbrProg);
165
166         glEnable(GL_DEPTH_TEST); 
167         glEnable(GL_CULL_FACE); 
168         // prevent edge artifacts in specular cubemaps
169         glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
170
171         glViewport(0, 0, windowWidth, windowHeight);
172 }
173
174 bool* keyStates = new bool[256];
175
176 void keyboard(unsigned char key, int x, int y) {
177         keyStates[key] = true;
178         if (key == 'z')
179                 activeSkybox = (activeSkybox + 1) % skyboxes.size();
180         if (key == 'x')
181                 activeLight = (activeLight + 1) % lights.size();
182 }
183
184 void keyboardUp(unsigned char key, int x, int y) {
185         keyStates[key] = false;
186 }
187
188 void timer(int _) {
189         float xSpeed = 0.f, ySpeed = 0.f, zSpeed = 0.f;
190         if (keyStates['w'])
191                 zSpeed = 0.1f;
192         if (keyStates['s'])
193                 zSpeed = -0.1f;
194         if (keyStates['a'])
195                 xSpeed = 0.1f;
196         if (keyStates['d'])
197                 xSpeed = -0.1f;
198         if (keyStates['q'])
199                 ySpeed = 0.1f;
200         if (keyStates['e'])
201                 ySpeed = -0.1f;
202
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;
209
210         camPos.x += xSpeed * sin(yaw) + zSpeed * cos(yaw);
211         camPos.y += ySpeed;
212         camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw);
213         glutPostRedisplay();
214         glutTimerFunc(16, timer, 0);
215 }
216
217 int prevMouseX, prevMouseY;
218 bool firstMouse = true;
219
220 void motion(int x, int y) {
221         if (firstMouse) {
222                 prevMouseX = x;
223                 prevMouseY = y;
224                 firstMouse = false;
225         }
226         int dx = x - prevMouseX, dy = y - prevMouseY;
227
228         prevMouseX = x;
229         prevMouseY = y;
230
231         const float sensitivity = 0.005f;
232         yaw += dx * sensitivity;
233         pitch -= dy * sensitivity;
234
235         glm::vec3 front;
236         front.x = cos(pitch) * cos(yaw);
237         front.y = sin(pitch);
238         front.z = cos(pitch) * sin(yaw);
239         camFront = glm::normalize(front);
240
241         if (pitch < -1.57079632679 || pitch >= 1.57079632679) {
242                 camUp = glm::vec3(0, -1, 0);
243         } else {
244                 camUp = glm::vec3(0, 1, 0);
245         }
246 }
247
248 void mouse(int button, int state, int x, int y) {
249         if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
250                 firstMouse = true;
251 }
252
253 void reshape(int newWidth, int newHeight) {
254         windowWidth = newWidth, windowHeight = newHeight;
255 }
256
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");
262         makeRetina();
263         glutDisplayFunc(display);
264         glutReshapeFunc(reshape);
265
266         glewInit();
267         
268         init();
269
270         glutKeyboardFunc(keyboard);
271         glutKeyboardUpFunc(keyboardUp);
272         glutTimerFunc(16, timer, 0);
273         glutMotionFunc(motion);
274         glutMouseFunc(mouse);
275
276         glutMainLoop();
277
278         return 0;
279 }
280