-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
297 lines (225 loc) · 9.45 KB
/
Main.cpp
File metadata and controls
297 lines (225 loc) · 9.45 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
//------- Ignore this ----------
#include<filesystem>
namespace fs = std::filesystem;
//------------------------------
#include"Model.h"
const unsigned int width = 800;
const unsigned int height = 800;
// Number of samples per pixel for MSAA
unsigned int samples = 8;
// Controls the gamma function
float gamma = 2.2f;
float rectangleVertices[] =
{
// Coords // texCoords
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f
};
// Vertices for plane with texture
std::vector<Vertex> vertices =
{
Vertex{glm::vec3(-1.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec2(0.0f, 0.0f)},
Vertex{glm::vec3(-1.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec2(0.0f, 1.0f)},
Vertex{glm::vec3(1.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec2(1.0f, 1.0f)},
Vertex{glm::vec3(1.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec2(1.0f, 0.0f)}
};
// Indices for plane with texture
std::vector<GLuint> indices =
{
0, 1, 2,
0, 2, 3
};
int main()
{
// Initialize GLFW
glfwInit();
// Tell GLFW what version of OpenGL we are using
// In this case we are using OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Only use this if you don't have a framebuffer
//glfwWindowHint(GLFW_SAMPLES, samples);
// Tell GLFW we are using the CORE profile
// So that means we only have the modern functions
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Create a GLFWwindow object of 800 by 800 pixels, naming it "YoutubeOpenGL"
GLFWwindow* window = glfwCreateWindow(width, height, "YoutubeOpenGL", NULL, NULL);
// Error check if the window fails to create
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Introduce the window into the current context
glfwMakeContextCurrent(window);
//Load GLAD so it configures OpenGL
gladLoadGL();
// Specify the viewport of OpenGL in the Window
// In this case the viewport goes from x = 0, y = 0, to x = 800, y = 800
glViewport(0, 0, width, height);
// Generates shaders
Shader shaderProgram("default.vert", "default.frag", "default.geom");
Shader framebufferProgram("framebuffer.vert", "framebuffer.frag");
// Take care of all the light related things
glm::vec4 lightColor = glm::vec4(100.0f, 100.0f, 100.0f, 1.0f);
glm::vec3 lightPos = glm::vec3(0.5f, 0.5f, 0.5f);
shaderProgram.Activate();
glUniform4f(glGetUniformLocation(shaderProgram.ID, "lightColor"), lightColor.x, lightColor.y, lightColor.z, lightColor.w);
glUniform3f(glGetUniformLocation(shaderProgram.ID, "lightPos"), lightPos.x, lightPos.y, lightPos.z);
framebufferProgram.Activate();
glUniform1i(glGetUniformLocation(framebufferProgram.ID, "screenTexture"), 0);
glUniform1f(glGetUniformLocation(framebufferProgram.ID, "gamma"), gamma);
// Enables the Depth Buffer
glEnable(GL_DEPTH_TEST);
// Enables Multisampling
glEnable(GL_MULTISAMPLE);
// Enables Cull Facing
glEnable(GL_CULL_FACE);
// Keeps front faces
glCullFace(GL_FRONT);
// Uses counter clock-wise standard
glFrontFace(GL_CCW);
// Creates camera object
Camera camera(width, height, glm::vec3(0.0f, 0.0f, 2.0f));
// Prepare framebuffer rectangle VBO and VAO
unsigned int rectVAO, rectVBO;
glGenVertexArrays(1, &rectVAO);
glGenBuffers(1, &rectVBO);
glBindVertexArray(rectVAO);
glBindBuffer(GL_ARRAY_BUFFER, rectVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(rectangleVertices), &rectangleVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
// Variables to create periodic event for FPS displaying
double prevTime = 0.0;
double crntTime = 0.0;
double timeDiff;
// Keeps track of the amount of frames in timeDiff
unsigned int counter = 0;
// Use this to disable VSync (not advized)
//glfwSwapInterval(0);
// Create Frame Buffer Object
unsigned int FBO;
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
// Create Framebuffer Texture
unsigned int framebufferTexture;
glGenTextures(1, &framebufferTexture);
glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, framebufferTexture);
glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, samples, GL_RGB16F, width, height, GL_TRUE);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // Prevents edge bleeding
glTexParameteri(GL_TEXTURE_2D_MULTISAMPLE, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // Prevents edge bleeding
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, framebufferTexture, 0);
// Create Render Buffer Object
unsigned int RBO;
glGenRenderbuffers(1, &RBO);
glBindRenderbuffer(GL_RENDERBUFFER, RBO);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_DEPTH24_STENCIL8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, RBO);
// Error checking framebuffer
auto fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (fboStatus != GL_FRAMEBUFFER_COMPLETE)
std::cout << "Framebuffer error: " << fboStatus << std::endl;
// Create Frame Buffer Object
unsigned int postProcessingFBO;
glGenFramebuffers(1, &postProcessingFBO);
glBindFramebuffer(GL_FRAMEBUFFER, postProcessingFBO);
// Create Framebuffer Texture
unsigned int postProcessingTexture;
glGenTextures(1, &postProcessingTexture);
glBindTexture(GL_TEXTURE_2D, postProcessingTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, postProcessingTexture, 0);
// Error checking framebuffer
fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (fboStatus != GL_FRAMEBUFFER_COMPLETE)
std::cout << "Post-Processing Framebuffer error: " << fboStatus << std::endl;
std::vector<Texture> textures =
{
Texture((parentDir + diffusePath).c_str(), "diffuse", 0)
};
// Plane with the texture
Mesh plane(vertices, indices, textures);
// Normal map for the plane
Texture normalMap((parentDir + normalPath).c_str(), "normal", 1);
Texture displacementMap((parentDir + displacementPath).c_str(), "displacement", 2);
// Main while loop
while (!glfwWindowShouldClose(window))
{
// Updates counter and times
crntTime = glfwGetTime();
timeDiff = crntTime - prevTime;
counter++;
if (timeDiff >= 1.0 / 30.0)
{
// Creates new title
std::string FPS = std::to_string((1.0 / timeDiff) * counter);
std::string ms = std::to_string((timeDiff / counter) * 1000);
std::string newTitle = "YoutubeOpenGL - " + FPS + "FPS / " + ms + "ms";
glfwSetWindowTitle(window, newTitle.c_str());
// Resets times and counter
prevTime = crntTime;
counter = 0;
// Use this if you have disabled VSync
//camera.Inputs(window);
}
// Bind the custom framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
// Specify the color of the background
glClearColor(pow(0.07f, gamma), pow(0.13f, gamma), pow(0.17f, gamma), 1.0f);
// Clean the back buffer and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Enable depth testing since it's disabled when drawing the framebuffer rectangle
glEnable(GL_DEPTH_TEST);
// Handles camera inputs (delete this if you have disabled VSync)
camera.Inputs(window);
// Updates and exports the camera matrix to the Vertex Shader
camera.updateMatrix(45.0f, 0.1f, 100.0f);
shaderProgram.Activate();
normalMap.Bind();
glUniform1i(glGetUniformLocation(shaderProgram.ID, "normal0"), 1);
displacementMap.Bind();
glUniform1i(glGetUniformLocation(shaderProgram.ID, "displacement0"), 2);
// Draw the normal model
plane.Draw(shaderProgram, camera);
// Make it so the multisampling FBO is read while the post-processing FBO is drawn
glBindFramebuffer(GL_READ_FRAMEBUFFER, FBO);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, postProcessingFBO);
// Conclude the multisampling and copy it to the post-processing FBO
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
// Bind the default framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Draw the framebuffer rectangle
framebufferProgram.Activate();
glBindVertexArray(rectVAO);
glDisable(GL_DEPTH_TEST); // prevents framebuffer rectangle from being discarded
glBindTexture(GL_TEXTURE_2D, postProcessingTexture);
glDrawArrays(GL_TRIANGLES, 0, 6);
// Swap the back buffer with the front buffer
glfwSwapBuffers(window);
// Take care of all GLFW events
glfwPollEvents();
}
// Delete all the objects we've created
shaderProgram.Delete();
glDeleteFramebuffers(1, &FBO);
glDeleteFramebuffers(1, &postProcessingFBO);
// Delete window before ending the program
glfwDestroyWindow(window);
// Terminate GLFW before ending the program
glfwTerminate();
return 0;
}