-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
391 lines (302 loc) · 12.7 KB
/
Copy pathMain.cpp
File metadata and controls
391 lines (302 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stb/stb_image.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <vector>
#include "Classes/glAbstractions/shaderClass.h"
#include "Classes/glAbstractions/VBO.h"
#include "Classes/glAbstractions/EBO.h"
#include "Classes/glAbstractions/VAO.h"
#include "Classes/glAbstractions/Texture.h"
#include "Classes/ViewElements/Camera.h"
#include "Classes/Utils/GeneralUtilities.h"
#include "Classes/Objects/Sphere.h"
#include "Classes/Objects/Material.h"
using namespace std;
struct PointLight {
glm::vec3 lightPos;
glm::vec3 lightColor;
float intensity;
};
const unsigned int SCR_WIDTH = 1920;
const unsigned int SCR_HEIGHT = 1080;
void resize(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
void renderSpheres(vector<Sphere> spheres, Shader& program, glm::mat4 model, VAO& vao, VBO& vbo);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void setPointLightUniforms(vector<PointLight>& pointLights, Shader& shaderProgram);
void renderLights(vector<PointLight>& pointLights, Shader& lightShaderProgram, glm::mat4& view, glm::mat4& proj, VAO& vaoLight, VBO& vbo, Sphere& sphere);
void configUniforms(Shader& shaderProgram, Sphere& sphere);
float CLIP_NEAR = 0.1f;
float CLIP_FAR = 500.0f;
const float ambientStrength = 0.1;
const float radius = 10.0;
float deltaTime = 0.0f; // Time between current frame and last frame
float lastFrame = 0.0f; // Time of last frame
float lastX = 400, lastY = 300;
float yaw = -90.0f;
float pitch = 0.0f;
float fov = 70.0f;
float movementSpeed = 2.5f;
glm::vec3 cameraPosition = glm::vec3(0.0, 0.0, 10.0);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
const float sensitivity = 0.5f;
glm::vec3 worldUp = glm::vec3(0.0, 1.0, 0.0);
Camera camera(cameraPosition, worldUp, yaw, pitch, fov, sensitivity, movementSpeed);
bool paused = false;
float paused_time = 0;
const char* wallPath = "Textures/wall.jpg";
const char* awesomePath = "Textures/awesomeface.png";
vector<PointLight> pointLights = {
{glm::vec3(0.0), glm::vec3(1.0), 300},
/*{glm::vec3(-5.0, -5.0, -5.0), glm::vec3(1.0, 0.0, 0.0)},
{glm::vec3(5.0, 5.0, 5.0), glm::vec3(0.0, 1.0, 1.0)},*/
};
int layoutVertex = 0, layoutUV = 1, layoutNormal = 2;
int stepVertex = 3, stepUV = 2, stepNormal = 3;
int stride = stepVertex + stepUV;
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Omor", NULL, NULL);
glfwSetFramebufferSizeCallback(window, resize);
if (window == NULL) {
std::cout << "Falha na criação da janela" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
gladLoadGL();
glfwSetFramebufferSizeCallback(window, resize);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glEnable(GL_DEPTH_TEST);
Material awesome = Material(glm::vec3(1.0), glm::vec3(1.0), glm::vec3(1.0), 20.0, awesomePath, "", "");
Material wall = Material(glm::vec3(1.0), glm::vec3(1.0), glm::vec3(1.0), 50.0, wallPath, "", "");
Material jade = Material(glm::vec3(0.135, 0.2225, 0.1575), glm::vec3(0.54, 0.89, 0.63), glm::vec3(0.316228, 0.316228, 0.316228), 0.1, "", "", "");
Material obsidian = Material(glm::vec3(0.05375, 0.05, 0.06625), glm::vec3(0.18275, 0.17, 0.22525), glm::vec3(0.332741, 0.328634, 0.346435), 0.3, "", "", "");
Material silver = Material(glm::vec3(0.19225, 0.19225, 0.19225), glm::vec3(0.50754, 0.50754, 0.50754), glm::vec3(0.508273, 0.508273, 0.508273), 0.4, "", "", "");
Material caixa = Material(glm::vec3(0.19225, 0.19225, 0.19225), glm::vec3(0.50754, 0.50754, 0.50754), glm::vec3(0.508273, 0.508273, 0.508273), 7, "Textures/container2.png", "Textures/container2_specular.png", "");
Material caixaGlow = Material(glm::vec3(0.19225, 0.19225, 0.19225), glm::vec3(0.50754, 0.50754, 0.50754), glm::vec3(0.508273, 0.508273, 0.508273), 7, "Textures/container2.png", "Textures/container2_specular.png", "Textures/matrix.jpg");
vector<Material> materialList = {
awesome,
wall,
Material(glm::vec3(0.0215, 0.1745, 0.0215), glm::vec3(0.07568, 0.61424, 0.07568),glm::vec3(0.633, 0.727811, 0.633), 6, "", "", ""),
jade,
obsidian,
silver,
caixa,
caixaGlow
};
vector<float> radius = { 5.33f, 7.5f, 14.0f, 10.2f };
int sphereResolution = 30;
glm::vec3 objColor = glm::vec3(1.0, 0.0, 0.0);
vector<Sphere> sphereVector;
Sphere lightSphere(10.0, 15, materialList[0], glm::vec3(0.0));
for (int i = 0; i < radius.size(); i++) sphereVector.push_back(Sphere(radius[i], sphereResolution, materialList[i+3], generateRandomSpacedPositions(i+10)));
Shader shaderProgram("Shaderfiles/3d.vert", "Shaderfiles/3d.frag");
//Shader bloomProgram("Shaderfiles/3d.vert", "Shaderfiles/blur.frag");
VAO VAO1;
VBO VBO1;
Shader lightShaderProgram("Shaderfiles/light.vert", "Shaderfiles/light.frag");
VAO lightVAO;
VAO vaoLight;
vaoLight.Bind();
vaoLight.LinkVBO(VBO1, layoutVertex, stepVertex, stride, 0);
vaoLight.LinkVBO(VBO1, layoutUV, stepUV, stride, stepVertex);
vaoLight.LinkVBO(VBO1, layoutNormal, stepNormal, stride, stepVertex + stepUV);
vaoLight.Unbind();
VBO1.Unbind();
// BLOOM TEXTURE
//Texture bloom(1);
//Texture bloom(1);
//bloom.Bind();
//bloom.LinkTexBuffer(SCR_WIDTH, SCR_HEIGHT);
//bloom.SetTexParameters();
//int mipmapLevels = 0; // 4
//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, bloom.ID, mipmapLevels);
// FAZER COM O DOWNSCALE DEPOIS
//Texture downscaledBloom(1);
//downscaledBloom.Bind();
//glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 0, 0, SCR_WIDTH/32, SCR_HEIGHT/32, 0);
glm::vec3 dirLightColor = glm::vec3(0.3, 0.2, 0.8);
shaderProgram.Activate();
shaderProgram.setVec3Float("dirLight.lightColor", dirLightColor);
shaderProgram.setVec3Float("dirLight.direction", glm::vec3(-0.2f, -1.0f, -0.3f));
glm::vec3 pointLightColor = glm::vec3(1.0, 1.0, 1.0);
shaderProgram.setFloat("ambientStrength", ambientStrength);
//loop de renderização
while (!glfwWindowShouldClose(window))
{
processInput(window);
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
glClearColor(0.13f, 0.13f, 0.13f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shaderProgram.Activate();
VBO1.Bind();
glm::mat4 model = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 proj = glm::mat4(1.0f);
glm::vec3 cameraTarget = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 cameraDirection = glm::normalize(camera.position - cameraTarget);
camera.right = glm::normalize(glm::cross(camera.up, cameraDirection));
camera.up = glm::cross(cameraDirection, camera.right);
view = glm::lookAt(camera.position, camera.position + camera.front, camera.up);
proj = glm::perspective(glm::radians(camera.fov), (float)(SCR_WIDTH / SCR_HEIGHT), CLIP_NEAR, CLIP_FAR);
shaderProgram.setVec3Float("camPos", camera.position);
shaderProgram.setMat4("model", model);
shaderProgram.setMat4("view", view);
shaderProgram.setMat4("proj", proj);
VAO1.Bind();
renderSpheres(sphereVector, shaderProgram, model, VAO1, VBO1);
VAO1.Unbind();
vaoLight.Bind();
renderLights(pointLights, lightShaderProgram, view, proj, vaoLight, VBO1, lightSphere);
vaoLight.Unbind();
glfwPollEvents();
glfwSwapBuffers(window);
}
VAO1.Delete();
VBO1.Delete();
//EBO1.Delete();
shaderProgram.Delete();
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
void processInput(GLFWwindow* window) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) deltaTime *= 4.0;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboardInput(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessKeyboardInput(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessKeyboardInput(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessKeyboardInput(RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS && (float)glfwGetTime() - paused_time > 0.5) {
paused = !paused;
cout << (paused ? "true" : "false") << "\n";
if (paused) paused_time = (float)glfwGetTime();
}
}
void resize(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
void renderSpheres(vector<Sphere> spheres, Shader& program, glm::mat4 model, VAO& vao, VBO& vbo) {
float angle = (float)glfwGetTime() - paused_time;
if(paused) angle = paused_time;
int i = 0;
for (Sphere sphere : spheres) {
vbo = sphere.getVBO();
vao.LinkVBO(vbo, layoutVertex, stepVertex, stride, 0);
vao.LinkVBO(vbo, layoutUV, stepUV, stride, stepVertex);
i++;
angle /= i;
glm::vec3 pos = sphere.pos;
float rotationX = pos.x * cos(angle);
float amplitude = 3.0;
float rotationY = sin(angle) * amplitude;
float rotationZ = pos.x * sin(angle)-3.5;
glm::vec3 position = glm::vec3(rotationX, rotationY, rotationZ);
model = glm::mat4(1.0f);
model = glm::translate(model, position);
//model = glm::rotate(model, glm::radians(((float)glfwGetTime()) * 50), glm::vec3(0.0f, 0.0f, 1.0f));
program.setMat4("model", model);
configUniforms(program, sphere);
glDrawArrays(GL_TRIANGLES, 0, sphere.verticesCount);
}
}
void mouse_callback(GLFWwindow* window, double xPos, double yPos) {
if (camera.firstTouch)
{
lastX = xPos;
lastY = yPos;
camera.firstTouch = false;
return;
}
float deltaX = xPos - lastX;
float deltaY = lastY - yPos;
lastX = xPos;
lastY = yPos;
deltaX *= camera.sensitivity;
deltaY *= camera.sensitivity;
camera.ProcessMouseMovement(deltaX, deltaY, true);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(yoffset);
}
void setPointLightUniforms(vector<PointLight>& pointLights, Shader& shaderProgram) {
for (int i = 0; i < pointLights.size(); i++)
{
char buffer[64];
sprintf_s(buffer, "pointLights[%i].position", i);
shaderProgram.setVec3Float(buffer, pointLights[i].lightPos);
sprintf_s(buffer, "pointLights[%i].lightColor", i);
shaderProgram.setVec3Float(buffer, pointLights[i].lightColor);
sprintf_s(buffer, "pointLights[%i].intensity", i);
shaderProgram.setFloat(buffer, pointLights[i].intensity);
}
}
void renderLights(vector<PointLight>& pointLights, Shader& lightShaderProgram, glm::mat4& view, glm::mat4& proj, VAO& vaoLight, VBO& vbo, Sphere& sphere) {
glm::mat4 lightModel = glm::mat4(1.0);
lightShaderProgram.Activate();
for (auto& pointLight : pointLights)
{
vbo = sphere.getVBO();
vaoLight.LinkVBO(vbo, layoutVertex, stepVertex, stride, 0);
lightModel = glm::mat4(1.0);
lightShaderProgram.setMat4("view", view);
lightShaderProgram.setMat4("proj", proj);
lightShaderProgram.setVec3Float("objectColor", pointLight.lightColor);
lightModel = glm::translate(lightModel, pointLight.lightPos);
lightShaderProgram.setMat4("model", lightModel);
//configUniforms(lightShaderProgram, sphere);
glDrawArrays(GL_TRIANGLES, 0, sphere.verticesCount);
}
}
void configUniforms(Shader& shaderProgram, Sphere& sphere) {
shaderProgram.setBool("usesDiffuseMap", sphere.material.usesDiffuseMap);
shaderProgram.setBool("usesSpecularMap", sphere.material.usesSpecularMap);
shaderProgram.setBool("usesGlowMap", sphere.material.usesGlowMap);
if (sphere.material.usesDiffuseMap) {
shaderProgram.setInt("diffuseMap", 0);
sphere.material.diffuseMap.ActiveTexture(GL_TEXTURE0);
sphere.material.diffuseMap.Bind();
}if (sphere.material.usesSpecularMap) {
shaderProgram.setInt("specularMap", 1);
sphere.material.specularMap.ActiveTexture(GL_TEXTURE1);
sphere.material.specularMap.Bind();
}if (sphere.material.usesGlowMap) {
shaderProgram.setInt("glowMap", 2);
sphere.material.glowMap.ActiveTexture(GL_TEXTURE2);
sphere.material.glowMap.Bind();
}
shaderProgram.setVec3Float("diffuseColor", sphere.material.diffuse);
shaderProgram.setVec3Float("ambientColor", sphere.material.ambient);
shaderProgram.setVec3Float("specularColor", sphere.material.specular);
shaderProgram.setFloat("shininess", sphere.material.shininess);
setPointLightUniforms(pointLights, shaderProgram);
}