-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbloom.cc
268 lines (228 loc) · 9.88 KB
/
bloom.cc
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
// clang-format off
// Must precede glfw/glad, to include OpenGL functions.
#include <qrk/quarkgl.h>
// clang-format on
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
int main() {
constexpr int width = 1280, height = 960;
qrk::Window win(width, height, "Bloom", /* fullscreen */ false,
/* samples */ 4);
win.setClearColor(glm::vec4(0.1f, 0.1f, 0.1f, 1.0f));
win.setEscBehavior(qrk::EscBehavior::UNCAPTURE_MOUSE_OR_CLOSE);
win.setMouseButtonBehavior(qrk::MouseButtonBehavior::NONE);
auto camera = std::make_shared<qrk::Camera>(
/* position */ glm::vec3(-7.0f, 3.0f, 7.0f));
camera->lookAt(glm::vec3(0.0f, 0.0f, 0.0f));
auto cameraControls = std::make_shared<qrk::FlyCameraControls>();
cameraControls->setSpeed(10.0f);
win.bindCamera(camera);
win.bindCameraControls(cameraControls);
qrk::Shader mainShader(qrk::ShaderPath("examples/shaders/model.vert"),
qrk::ShaderPath("examples/shaders/bloom.frag"));
mainShader.addUniformSource(camera);
qrk::Shader lampShader(qrk::ShaderPath("examples/shaders/model.vert"),
qrk::ShaderPath("examples/shaders/bloom_lamp.frag"));
lampShader.addUniformSource(camera);
// Create light registry and add lights.
auto lightRegistry = std::make_shared<qrk::LightRegistry>();
lightRegistry->setViewSource(camera);
mainShader.addUniformSource(lightRegistry);
std::vector<std::shared_ptr<qrk::PointLight>> lights;
lights.push_back(std::make_shared<qrk::PointLight>(
/*position=*/glm::vec3(0.0f, 0.5f, 1.5f),
/*diffuse=*/glm::vec3(5.0f)));
lights.push_back(std::make_shared<qrk::PointLight>(
/*position=*/glm::vec3(-4.0f, 0.5f, -3.0f),
/*diffuse=*/glm::vec3(10.0f, 0.0f, 0.0f)));
lights.push_back(std::make_shared<qrk::PointLight>(
/*position=*/glm::vec3(3.0f, 0.5f, 1.0f),
/*diffuse=*/glm::vec3(0.0f, 0.0f, 15.0f)));
lights.push_back(std::make_shared<qrk::PointLight>(
/*position=*/glm::vec3(-0.8f, 2.4f, -1.0f),
/*diffuse=*/glm::vec3(0.0f, 5.0f, 0.0f)));
qrk::Attenuation attenuation = {
.constant = 0.0f, .linear = 0.0f, .quadratic = 1.0f};
for (auto& light : lights) {
light->setSpecular(glm::vec3(0.0f));
light->setAttenuation(attenuation);
lightRegistry->addLight(light);
}
// TODO: Pull this out into a material class.
mainShader.setVec3("material.ambient", glm::vec3(0.0f));
mainShader.setFloat("material.shininess", 32.0f);
mainShader.setFloat("material.emissionAttenuation.constant", 1.0f);
mainShader.setFloat("material.emissionAttenuation.linear", 0.09f);
mainShader.setFloat("material.emissionAttenuation.quadratic", 1.032f);
// Create the scene.
std::vector<qrk::Mesh*> meshes;
qrk::CubeMesh floor("examples/assets/wood.png");
floor.setModelTransform(glm::scale(
glm::rotate(glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)),
glm::vec3(25.0f, 1.0f, 25.0f)));
meshes.push_back(&floor);
qrk::CubeMesh cube1("examples/assets/container2.png");
cube1.setModelTransform(glm::scale(
glm::rotate(glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 1.5f, 0.0f)),
glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)),
glm::vec3(1.0f)));
meshes.push_back(&cube1);
qrk::CubeMesh cube2("examples/assets/container2.png");
cube2.setModelTransform(glm::scale(
glm::rotate(glm::translate(glm::mat4(1.0f), glm::vec3(2.0f, 0.0f, 1.0f)),
glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)),
glm::vec3(1.0f)));
meshes.push_back(&cube2);
qrk::CubeMesh cube3("examples/assets/container2.png");
cube3.setModelTransform(glm::scale(
glm::rotate(glm::translate(glm::mat4(1.0f), glm::vec3(-1.0f, -1.0f, 2.0f)),
glm::radians(60.0f),
glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f))),
glm::vec3(2.0f)));
meshes.push_back(&cube3);
qrk::CubeMesh cube4("examples/assets/container2.png");
cube4.setModelTransform(glm::scale(
glm::rotate(glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 2.7f, 4.0f)),
glm::radians(23.0f),
glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f))),
glm::vec3(2.5f)));
meshes.push_back(&cube4);
qrk::CubeMesh cube5("examples/assets/container2.png");
cube5.setModelTransform(glm::scale(
glm::rotate(glm::translate(glm::mat4(1.0f), glm::vec3(-2.0f, 1.0f, -3.0f)),
glm::radians(124.0f),
glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f))),
glm::vec3(2.0f)));
meshes.push_back(&cube5);
qrk::CubeMesh cube6("examples/assets/container2.png");
cube6.setModelTransform(glm::scale(
glm::rotate(glm::translate(glm::mat4(1.0f), glm::vec3(-3.0f, 0.0f, 0.0f)),
glm::radians(0.0f), glm::vec3(0.0f, 1.0f, 0.0f)),
glm::vec3(1.0f)));
meshes.push_back(&cube6);
// Framebuffer.
qrk::Framebuffer mainFb(win.getSize());
auto colorAttachment = mainFb.attachTexture(qrk::BufferType::COLOR_HDR_ALPHA);
auto bloomAttachment = mainFb.attachTexture(qrk::BufferType::COLOR_HDR_ALPHA);
mainFb.attachRenderbuffer(qrk::BufferType::DEPTH_AND_STENCIL);
qrk::ScreenQuadMesh screenQuad;
qrk::ScreenQuadMesh bloomQuad(bloomAttachment.asTexture());
// Create ping-pong buffers for blurring.
qrk::PingPongPass blurBuffer(win.getSize());
qrk::GaussianBlurShader blurShader;
// Create pass for resample based bloom.
qrk::BloomPass bloomPass(win.getSize());
int numResampleMips = bloomPass.getNumMips();
qrk::ScreenShader screenShader(qrk::ShaderPath("examples/shaders/hdr.frag"));
screenShader.setBool("useHdr", false);
qrk::ScreenLodShader screenLodShader;
qrk::ScreenShader bloomScreenShader(
qrk::ShaderPath("examples/shaders/bloom_screen.frag"));
bool useBloom = true;
win.addKeyPressHandler(GLFW_KEY_1, [&](int mods) { useBloom = !useBloom; });
bool useResampleBloom = true;
win.addKeyPressHandler(GLFW_KEY_2, [&](int mods) {
useResampleBloom = !useResampleBloom;
printf("useResampleBloom = %d\n", useResampleBloom);
});
int drawOptionResampleBloom = 0;
const int NUM_DRAW_OPTIONS_RESAMPLE_BLOOM = numResampleMips + 1;
win.addKeyPressHandler(GLFW_KEY_3, [&](int mods) {
drawOptionResampleBloom =
(drawOptionResampleBloom + 1) % NUM_DRAW_OPTIONS_RESAMPLE_BLOOM;
printf("drawOptionResampleBloom = %d\n", drawOptionResampleBloom);
});
int drawOptionBlurBloom = 0;
constexpr int NUM_DRAW_OPTIONS_BLUR_BLOOM = 3;
win.addKeyPressHandler(GLFW_KEY_4, [&](int mods) {
drawOptionBlurBloom =
(drawOptionBlurBloom + 1) % NUM_DRAW_OPTIONS_BLUR_BLOOM;
printf("drawOptionBlurBloom = %d\n", drawOptionBlurBloom);
});
printf("Controls:\n");
printf("- WASD: movement\n");
printf("- Mouse: camera\n");
printf("- 1: enable/disable bloom\n");
printf("- 2: switch blur-based/resampling bloom\n");
printf("- 3: cycle mip levels of resampling bloom\n");
printf("- 4: cycle stages of blur bloom\n");
printf("Number of mip levels for resampling: %d\n", numResampleMips);
// win.enableFaceCull();
win.loop([&](float deltaTime) {
// Draw the scene to the HDR framebuffer.
mainFb.activate();
mainFb.clear();
mainShader.updateUniforms();
// Draw meshes and lights
for (auto mesh : meshes) {
mesh->draw(mainShader);
}
lampShader.updateUniforms();
for (auto& light : lights) {
qrk::CubeMesh lightCube;
lightCube.setModelTransform(glm::scale(
glm::translate(glm::mat4(1.0f), light->getPosition()), glm::vec3(1.0f)));
lampShader.setVec3("lightColor", light->getDiffuse());
lightCube.draw(lampShader);
}
mainFb.deactivate();
win.setViewport();
screenShader.updateUniforms();
if (useResampleBloom) {
// Resampling bloom technique.
bloomPass.multipassDraw(/*sourceFb=*/mainFb);
win.setViewport();
// Debug, look at the first 4 downsampled mips.
if (drawOptionResampleBloom >= 1 &&
drawOptionResampleBloom < numResampleMips + 1) {
int mipTarget = drawOptionResampleBloom - 1;
bloomPass.selectMip(mipTarget);
screenQuad.setTexture(bloomPass.getOutput());
screenShader.setFloat("colorStrength", 0.04f);
screenQuad.draw(screenShader);
bloomPass.deselectMip();
return;
}
// drawOptionResampleBloom == 0
qrk::Texture bloomTexture = bloomPass.getOutput();
bloomTexture.bindToUnit(1);
bloomScreenShader.setInt("bloomTexture", 1);
bloomScreenShader.setBool("useBloom", useBloom);
bloomScreenShader.setBool("interpolateBloom", true);
bloomScreenShader.setFloat("bloomStrength", 0.04f);
screenQuad.setTexture(colorAttachment);
screenQuad.draw(bloomScreenShader);
} else {
// Gaussian blur based bloom effect.
if (drawOptionBlurBloom == 1) {
screenShader.setFloat("colorStrength", 1.0f);
screenQuad.setTexture(bloomAttachment);
screenQuad.draw(screenShader);
return;
}
// Ping-pong between horizontal and vertical gaussian blurs.
// 5 horizontal, 5 vertical.
blurBuffer.multipassDraw(
bloomAttachment.asTexture(), blurShader, /*passes=*/5,
[&] { blurShader.setHorizontal(!blurShader.getHorizontal()); });
qrk::Texture blurredTexture = blurBuffer.getOutput();
if (drawOptionBlurBloom == 2) {
screenShader.setFloat("colorStrength", 1.0f);
screenQuad.setTexture(blurredTexture);
screenQuad.draw(screenShader);
return;
}
// drawOptionBlurBloom == 0
qrk::Texture bloomTexture = blurredTexture;
bloomTexture.bindToUnit(1);
bloomScreenShader.setInt("bloomTexture", 1);
bloomScreenShader.setBool("useBloom", useBloom);
bloomScreenShader.setBool("interpolateBloom", false);
screenQuad.setTexture(colorAttachment);
screenQuad.draw(bloomScreenShader);
}
});
return 0;
}