-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpost_processing.cc
257 lines (210 loc) · 7.33 KB
/
post_processing.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
// 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>
const char* textureVertexSource = R"SHADER(
#version 460 core
layout(location = 0) in vec3 vertexPos;
layout(location = 1) in vec2 vertexTexCoords;
out vec2 texCoords;
out vec3 fragPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(vertexPos, 1.0);
texCoords = vertexTexCoords;
fragPos = vec3(view * model * vec4(vertexPos, 1.0));
}
)SHADER";
const char* textureFragmentSource = R"SHADER(
#version 460 core
in vec2 texCoords;
out vec4 fragColor;
uniform sampler2D texture0;
void main() { fragColor = texture(texture0, texCoords); }
)SHADER";
const char* screenVertexSource = R"SHADER(
#version 460 core
layout(location = 0) in vec2 vertexPos;
layout(location = 1) in vec2 vertexTexCoords;
out vec2 texCoords;
void main() {
gl_Position = vec4(vertexPos, 0.0, 1.0);
texCoords = vertexTexCoords;
}
)SHADER";
const char* screenFragmentSource = R"SHADER(
#version 460 core
#pragma qrk_include < gamma.frag >
#pragma qrk_include < post_processing.frag >
#pragma qrk_include < window.frag >
in vec2 texCoords;
out vec4 fragColor;
uniform sampler2D screenTexture;
void main() {
if (qrk_isWindowLeftHalf()) {
if (qrk_isWindowTopHalf()) {
fragColor = texture(screenTexture, texCoords);
} else {
fragColor = qrk_blurKernel(screenTexture, texCoords);
}
} else {
if (qrk_isWindowTopHalf()) {
fragColor = qrk_grayscale(texture(screenTexture, texCoords));
} else {
fragColor = qrk_edgeKernel(screenTexture, texCoords);
}
}
fragColor.rgb = qrk_gammaCorrect(fragColor.rgb);
}
)SHADER";
// clang-format off
const float cubeVertices[] = {
// positions // texture coords
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
const float planeVertices[] = {
// positions // texture coords
5.0f, -0.5f, 5.0f, 2.0f, 0.0f,
-5.0f, -0.5f, 5.0f, 0.0f, 0.0f,
-5.0f, -0.5f, -5.0f, 0.0f, 2.0f,
5.0f, -0.5f, 5.0f, 2.0f, 0.0f,
-5.0f, -0.5f, -5.0f, 0.0f, 2.0f,
5.0f, -0.5f, -5.0f, 2.0f, 2.0f
};
const float quadVertices[] = {
// positions // texture coords
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
// clang-format on
int main() {
auto win = std::make_shared<qrk::Window>(1280, 960, "Post processing");
win->setClearColor(glm::vec4(0.1f, 0.1f, 0.1f, 1.0f));
win->enableMouseCapture();
win->setEscBehavior(qrk::EscBehavior::UNCAPTURE_MOUSE_OR_CLOSE);
win->setMouseButtonBehavior(qrk::MouseButtonBehavior::CAPTURE_MOUSE);
auto camera =
std::make_shared<qrk::Camera>(/* position */ glm::vec3(0.0f, 1.0f, 3.0f));
camera->lookAt(glm::vec3(0.0f));
auto cameraControls = std::make_shared<qrk::FlyCameraControls>();
win->bindCamera(camera);
win->bindCameraControls(cameraControls);
qrk::Shader mainShader((qrk::ShaderInline(textureVertexSource)),
qrk::ShaderInline(textureFragmentSource));
qrk::Shader screenShader((qrk::ShaderInline(screenVertexSource)),
qrk::ShaderInline(screenFragmentSource));
// TODO: Clean this up to use mesh primitives.
// Create a VAO for the boxes.
qrk::VertexArray cubeVarray;
cubeVarray.loadVertexData(cubeVertices, sizeof(cubeVertices));
cubeVarray.addVertexAttrib(3, GL_FLOAT);
cubeVarray.addVertexAttrib(2, GL_FLOAT);
cubeVarray.finalizeVertexAttribs();
// Create a VAO for the plane.
qrk::VertexArray planeVarray;
planeVarray.loadVertexData(planeVertices, sizeof(planeVertices));
planeVarray.addVertexAttrib(3, GL_FLOAT);
planeVarray.addVertexAttrib(2, GL_FLOAT);
planeVarray.finalizeVertexAttribs();
// Create a VAO for the screen quad.
qrk::VertexArray quadVarray;
quadVarray.loadVertexData(quadVertices, sizeof(quadVertices));
quadVarray.addVertexAttrib(2, GL_FLOAT);
quadVarray.addVertexAttrib(2, GL_FLOAT);
quadVarray.finalizeVertexAttribs();
// Load textures.
qrk::Texture cubeTexture =
qrk::Texture::load("examples/assets/container.jpg");
qrk::Texture floorTexture = qrk::Texture::load("examples/assets/metal.png");
// Framebuffer.
qrk::Framebuffer fb(win->getSize());
auto colorAttachment = fb.attachTexture(qrk::BufferType::COLOR);
fb.attachRenderbuffer(qrk::BufferType::DEPTH_AND_STENCIL);
screenShader.addUniformSource(win);
printf("Controls:\n");
printf("- WASD: movement\n");
printf("- Mouse: camera\n");
win->loop([&](float deltaTime) {
fb.activate();
fb.clear();
glm::mat4 view = camera->getViewTransform();
glm::mat4 projection = camera->getProjectionTransform();
// Setup shader and textures for the framebuffer.
mainShader.activate();
mainShader.setMat4("view", view);
mainShader.setMat4("projection", projection);
mainShader.setInt("texture0", 0);
glm::mat4 model(1.0f);
// Draw cubes.
cubeVarray.activate();
cubeTexture.bindToUnit(0);
model = glm::translate(glm::mat4(1.0f), glm::vec3(-1.0f, 0.0f, -1.0f));
mainShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
model = glm::translate(glm::mat4(1.0f), glm::vec3(2.0f, 0.0f, 0.0f));
mainShader.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
// Draw floor.
planeVarray.activate();
floorTexture.bindToUnit(0);
mainShader.setMat4("model", glm::mat4(1.0f));
glDrawArrays(GL_TRIANGLES, 0, 6);
planeVarray.deactivate();
fb.deactivate();
win->setViewport();
// Finally, draw to the screen based on the framebuffer contents.
screenShader.updateUniforms();
screenShader.activate();
screenShader.setInt("screenTexture", 0);
quadVarray.activate();
glBindTexture(GL_TEXTURE_2D, colorAttachment.id);
win->disableDepthTest();
glDrawArrays(GL_TRIANGLES, 0, 6);
win->enableDepthTest();
});
return 0;
}