-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFBO.cpp
325 lines (286 loc) · 11.4 KB
/
FBO.cpp
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
#include "FBO.h"
// static variables
std::vector<glm::mat4> FBO::lightSpaceMatrix;
std::vector<float> FBO::shadowCascadeLevels;
std::vector<glm::vec3> FBO::playerPos;
unsigned int FBO::sceneFBO;
unsigned int FBO::pingpongFBO[2];
unsigned int FBO::colorBuffers[2];
unsigned int FBO::pColorBuffers[2];
unsigned int FBO::dm;
unsigned int FBO::matricesUBO;
float FBO::timePassed = 0.0f;
glm::vec3 FBO::lightPos = glm::vec3(-2.0f, 2.0f, -1.0f);
FBO::FBO() {}
FBO::FBO(float np, float fp) {
near_ = np;
far_ = fp;
shadowCascadeLevels = { far_ / 50.0f, far_ / 25.0f, far_ / 10.0f, far_ / 2.0f };
// Initialize frame buffer
glGenFramebuffers(1, &depthFBO);
glGenTextures(1, &dm);
glBindTexture(GL_TEXTURE_2D_ARRAY, dm);
glTexImage3D(
GL_TEXTURE_2D_ARRAY,
0,
GL_DEPTH_COMPONENT32F,
SHADOW_WIDTH,
SHADOW_HEIGHT,
5,
0,
GL_DEPTH_COMPONENT,
GL_FLOAT,
nullptr);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
float borderColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameterfv(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_BORDER_COLOR, borderColor);
glEnable(GL_DEPTH_TEST);
glBindFramebuffer(GL_FRAMEBUFFER, depthFBO);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, dm, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glGenBuffers(1, &matricesUBO);
glBindBuffer(GL_UNIFORM_BUFFER, matricesUBO);
glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::mat4x4) * 16, nullptr, GL_STATIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, matricesUBO);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
FBO::FBO(int width, int height) {
glGenFramebuffers(1, &sceneFBO);
glBindFramebuffer(GL_FRAMEBUFFER, sceneFBO);
glGenTextures(2, colorBuffers);
for (unsigned int i = 0; i < 2; i++)
{
glBindTexture(GL_TEXTURE_2D, colorBuffers[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // we clamp to the edge as the blur filter would otherwise sample repeated texture values!
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// attach texture to framebuffer
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, colorBuffers[i], 0);
}
glGenRenderbuffers(1, &rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo);
// tell OpenGL which color attachments we'll use (of this framebuffer) for rendering
attachments[0] = GL_COLOR_ATTACHMENT0;
attachments[1] = GL_COLOR_ATTACHMENT1;
glDrawBuffers(2, attachments);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glGenFramebuffers(2, pingpongFBO);
glGenTextures(2, pColorBuffers);
for (unsigned int i = 0; i < 2; i++)
{
glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[i]);
glBindTexture(GL_TEXTURE_2D, pColorBuffers[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // we clamp to the edge as the blur filter would otherwise sample repeated texture values!
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pColorBuffers[i], 0);
// also check if framebuffers are complete (no need for depth buffer)
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "Framebuffer not complete!" << std::endl;
}
}
glm::mat4 FBO::setLightSpaceMatrix(float np, float fp, glm::mat4 view, int width, int height) {
const auto proj = glm::perspective(
glm::radians(60.0f), float(width) / float(height), np,
fp);
const auto inv = glm::inverse(proj * view);
glm::vec3 center = glm::vec3(0, 0, 0);
std::vector<glm::vec4> corners;
for (unsigned int x = 0; x < 2; ++x)
{
for (unsigned int y = 0; y < 2; ++y)
{
for (unsigned int z = 0; z < 2; ++z)
{
const glm::vec4 pt =
inv * glm::vec4(
2.0f * x - 1.0f,
2.0f * y - 1.0f,
2.0f * z - 1.0f,
1.0f);
corners.push_back(pt / pt.w);
}
}
}
for (const auto& v : corners)
{
center += glm::vec3(v);
}
center /= corners.size();
const auto lightView = glm::lookAt(
center + lightPos,
center,
glm::vec3(0.0f, 1.0f, 0.0f)
);
float minX = std::numeric_limits<float>::max();
float maxX = std::numeric_limits<float>::min();
float minY = std::numeric_limits<float>::max();
float maxY = std::numeric_limits<float>::min();
float minZ = std::numeric_limits<float>::max();
float maxZ = std::numeric_limits<float>::min();
for (const auto& v : corners)
{
const auto trf = lightView * v;
minX = std::min(minX, trf.x);
maxX = std::max(maxX, trf.x);
minY = std::min(minY, trf.y);
maxY = std::max(maxY, trf.y);
minZ = std::min(minZ, trf.z);
maxZ = std::max(maxZ, trf.z);
}
constexpr float zMult = 10.0f;
if (minZ < 0)
{
minZ *= zMult;
}
else
{
minZ /= zMult;
}
if (maxZ < 0)
{
maxZ /= zMult;
}
else
{
maxZ *= zMult;
}
const glm::mat4 lightProjection = glm::ortho(minX, maxX, minY, maxY, minZ, maxZ);
return lightProjection * lightView;
}
void FBO::resize(int width, int height) {
for (unsigned int i = 0; i < 2; i++)
{
glBindTexture(GL_TEXTURE_2D, colorBuffers[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
}
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
for (unsigned int i = 0; i < 2; i++)
{
glBindFramebuffer(GL_FRAMEBUFFER, pingpongFBO[i]);
glBindTexture(GL_TEXTURE_2D, pColorBuffers[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
}
}
void FBO::draw(int width, int height, float time, glm::mat4 cam) {
if (width <= 0 || height <= 0) return;
timePassed = totalTime - (time);
float degrees = glm::clamp(timePassed / totalTime, 0.0f, 1.0f);
degrees = glm::mix(0.0f, 6.28f, degrees);
lightPos = glm::vec3(glm::rotate(degrees, glm::vec3(0.0f, 1.0f, 0.0f)) * glm::vec4(-2.0f, 2.0f, -1.0f, 1.0f));
std::vector<glm::mat4> ret;
for (size_t i = 0; i < shadowCascadeLevels.size() + 1; ++i)
{
if (i == 0)
{
ret.push_back(setLightSpaceMatrix(near_, shadowCascadeLevels[i], cam, width, height));
}
else if (i < shadowCascadeLevels.size())
{
ret.push_back(setLightSpaceMatrix(shadowCascadeLevels[i - 1], shadowCascadeLevels[i], cam, width, height));
}
else
{
ret.push_back(setLightSpaceMatrix(shadowCascadeLevels[i - 1], far_, cam, width, height));
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, depthFBO);
glClearDepth(1.0f);
glClear(GL_DEPTH_BUFFER_BIT);
glCullFace(GL_FRONT); // peter panning
lightSpaceMatrix = ret;
glBindBuffer(GL_UNIFORM_BUFFER, matricesUBO);
for (size_t i = 0; i < ret.size(); ++i)
{
glBufferSubData(GL_UNIFORM_BUFFER, i * sizeof(glm::mat4x4), sizeof(glm::mat4x4), &lightSpaceMatrix[i]);
}
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
void FBO::draw(GLuint shader) {
glBindFramebuffer(GL_FRAMEBUFFER, sceneFBO);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader);
glUniform1i(glGetUniformLocation(shader, "bloom"), 0);
}
void FBO::bloomBlur(GLuint shader) {
glUseProgram(shader);
unsigned int amount = 5;
bool horizontal = true, first_iteration = true;
for (unsigned int i = 0; i < amount; i++)
{
glUniform1i(glGetUniformLocation(shader, "image"), 0);
glBindFramebuffer(GL_FRAMEBUFFER, FBO::pingpongFBO[horizontal]);
glUniform1i(glGetUniformLocation(shader, "horizontal"), horizontal);
glBindTexture(GL_TEXTURE_2D, first_iteration ? FBO::colorBuffers[1] : FBO::pColorBuffers[!horizontal]); // bind texture of other framebuffer (or scene if first iteration)
renderQuad();
horizontal = !horizontal;
if (first_iteration)
first_iteration = false;
}
glUseProgram(0);
glActiveTexture(GL_TEXTURE0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void FBO::finalDraw(GLuint shader) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, FBO::colorBuffers[0]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, FBO::pColorBuffers[0]);
glUniform1i(glGetUniformLocation(shader, "scene"), 0);
glUniform1i(glGetUniformLocation(shader, "bloomBlur"), 1);
glUniform1i(glGetUniformLocation(shader, "bloom"), 1);
glUniform1f(glGetUniformLocation(shader, "exposure"), 1.0f);
renderQuad();
glUseProgram(0);
}
void FBO::renderQuad() {
// glUseProgram(modelShaderProgram);
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_2D_ARRAY, FBO::dm);
// glUniform1i(glGetUniformLocation(modelShaderProgram, "map"), 0);
unsigned int quadVAO = 0;
unsigned int quadVBO;
if (quadVAO == 0)
{
float quadVertices[] = {
// positions // texture Coords
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
1.0f, -1.0f, 0.0f, 1.0f, 0.0f,
};
// setup plane VAO
glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &quadVBO);
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
}
glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindVertexArray(0);
// glUseProgram(0);
}