-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdeferred.cc
228 lines (196 loc) · 7.02 KB
/
deferred.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
// clang-format off
// Must precede glfw/glad, to include OpenGL functions.
#include <qrk/quarkgl.h>
// clang-format on
#include <glm/ext.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <random>
const char* lampShaderSource = R"SHADER(
#version 460 core
out vec4 fragColor;
uniform vec3 lightColor;
void main() { fragColor = vec4(lightColor, 1.0); }
)SHADER";
int main() {
constexpr int width = 1280, height = 960;
qrk::Window win(width, height, "Deferred rendering", /* fullscreen */ false,
/* samples */ 0);
win.setClearColor(glm::vec4(0.0f, 0.0f, 0.0f, 1.0f));
win.enableMouseCapture();
win.setEscBehavior(qrk::EscBehavior::UNCAPTURE_MOUSE_OR_CLOSE);
win.setMouseButtonBehavior(qrk::MouseButtonBehavior::CAPTURE_MOUSE);
// Create camera.
auto camera = std::make_shared<qrk::Camera>(
/* position */ glm::vec3(-5.0f, 1.0f, 7.0f));
camera->lookAt(glm::vec3(0.0f, 0.0f, 0.0f));
auto cameraControls = std::make_shared<qrk::FlyCameraControls>();
cameraControls->setSpeed(2.0f);
win.bindCamera(camera);
win.bindCameraControls(cameraControls);
// Create light registry and add lights.
auto lightRegistry = std::make_shared<qrk::LightRegistry>();
lightRegistry->setViewSource(camera);
// We explicitly _don't_ add the lightregistry to the geometry pass.
// geometryPassShader.addUniformSource(lightRegistry);
std::mt19937 gen(42);
// Create random lights.
std::vector<std::shared_ptr<qrk::PointLight>> lights;
constexpr int NUM_LIGHTS = 32;
for (int i = 0; i < NUM_LIGHTS; i++) {
auto light = std::make_shared<qrk::PointLight>(
glm::vec3(((gen() % 100) / 100.0f) * 6.0f - 3.0f,
((gen() % 100) / 100.0f) * 6.0f - 4.0f,
((gen() % 100) / 100.0f) * 6.0f - 3.0f));
glm::vec3 lightColor(
((gen() % 100) / 200.0f) * 5 + 0.5f, // between 0.5 and 3.0
((gen() % 100) / 200.0f) * 5 + 0.5f, // between 0.5 and 3.0
((gen() % 100) / 200.0f) * 5 + 0.5f // between 0.5 and 3.0
);
light->setDiffuse(lightColor);
light->setSpecular(lightColor);
qrk::Attenuation attenuation = {
.constant = 0.0f, .linear = 0.0f, .quadratic = 1.0f};
light->setAttenuation(attenuation);
lightRegistry->addLight(light);
lights.push_back(light);
}
qrk::Shader lampShader(qrk::ShaderPath("examples/shaders/model.vert"),
qrk::ShaderInline(lampShaderSource));
lampShader.addUniformSource(camera);
qrk::CubeMesh lightCube;
// Create the scene.
std::vector<qrk::Renderable*> meshes;
auto helmet = std::make_unique<qrk::Model>(
"examples/assets/DamagedHelmet/DamagedHelmet.gltf");
meshes.push_back(helmet.get());
// clang-format off
std::vector<glm::vec3> meshPositions = {
glm::vec3(-3.0f, 0.0f, -3.0f),
glm::vec3( 0.0f, 0.0f, -3.0f),
glm::vec3( 3.0f, 0.0f, -3.0f),
glm::vec3(-3.0f, 0.0f, 0.0f),
glm::vec3( 0.0f, 0.0f, 0.0f),
glm::vec3( 3.0f, 0.0f, 0.0f),
glm::vec3(-3.0f, 0.0f, 3.0f),
glm::vec3( 0.0f, 0.0f, 3.0f),
glm::vec3( 3.0f, 0.0f, 3.0f)
};
// clang-format on
// Build the G-Buffer and prepare deferred shading.
qrk::DeferredGeometryPassShader geometryPassShader;
geometryPassShader.addUniformSource(camera);
auto gBuffer = std::make_shared<qrk::GBuffer>(win.getSize());
auto textureRegistry = std::make_shared<qrk::TextureRegistry>();
textureRegistry->addTextureSource(gBuffer);
qrk::ScreenQuadMesh screenQuad;
qrk::ScreenShader gBufferVisShader(
qrk::ShaderPath("examples/shaders/gbuffer.frag"));
// Set up the lighting pass.
qrk::ScreenShader lightingPassShader(
qrk::ShaderPath("examples/shaders/deferred_lighting.frag"));
lightingPassShader.addUniformSource(lightRegistry);
lightingPassShader.addUniformSource(textureRegistry);
lightingPassShader.setVec3("ambient", glm::vec3(0.05f));
lightingPassShader.setFloat("emissionStrength", 5.0f);
lightingPassShader.setFloat("emissionAttenuation.constant", 0.0f);
lightingPassShader.setFloat("emissionAttenuation.linear", 0.0f);
lightingPassShader.setFloat("emissionAttenuation.quadratic", 1.0f);
int gBufferVis = 0;
constexpr int NUM_GBUFFER_VIS = 8;
win.addKeyPressHandler(GLFW_KEY_1, [&](int mods) {
gBufferVis = (gBufferVis + 1) % NUM_GBUFFER_VIS;
switch (gBufferVis) {
case 0:
printf("Switching to full lighting\n");
break;
case 1:
printf("Drawing G-Buffer positions\n");
break;
case 2:
printf("Drawing G-Buffer AO\n");
break;
case 3:
printf("Drawing G-Buffer normals\n");
break;
case 4:
printf("Drawing G-Buffer roughness\n");
break;
case 5:
printf("Drawing G-Buffer albedo\n");
break;
case 6:
printf("Drawing G-Buffer metallic\n");
break;
case 7:
printf("Drawing G-Buffer emission\n");
break;
};
});
printf("Controls:\n");
printf("- WASD: movement\n");
printf("- Mouse: camera\n");
printf("- 1: cycle through G-buffer\n");
printf("Generated %d random point lights\n", NUM_LIGHTS);
// win.enableFaceCull();
win.loop([&](float deltaTime) {
// Step 1: geometry pass. Build the G-Buffer.
gBuffer->activate();
gBuffer->clear();
geometryPassShader.updateUniforms();
// Draw meshes.
for (auto mesh : meshes) {
for (auto pos : meshPositions) {
mesh->setModelTransform(glm::scale(
glm::rotate(glm::translate(glm::mat4(1.0f), pos),
glm::radians(0.0f), glm::vec3(1.0f, 0.0f, 0.0f)),
glm::vec3(1.0f)));
mesh->draw(geometryPassShader);
}
}
gBuffer->deactivate();
win.setViewport();
if (gBufferVis > 0) {
switch (gBufferVis) {
case 1:
case 2:
screenQuad.setTexture(gBuffer->getPositionAOTexture());
break;
case 3:
case 4:
screenQuad.setTexture(gBuffer->getNormalRoughnessTexture());
break;
case 5:
case 6:
screenQuad.setTexture(gBuffer->getAlbedoMetallicTexture());
break;
case 7:
screenQuad.setTexture(gBuffer->getEmissionTexture());
break;
};
gBufferVisShader.setInt("gBufferVis", gBufferVis);
screenQuad.draw(gBufferVisShader);
return;
}
// Continue.
// Step 2: lighting pass.
lightingPassShader.updateUniforms();
// Bind textures.
screenQuad.unsetTexture();
screenQuad.draw(lightingPassShader, textureRegistry.get());
// Step 3: forward render anything else on top.
// Before we do so, we have to blit the depth buffer.
gBuffer->blitToDefault(GL_DEPTH_BUFFER_BIT);
// Draw the lights.
lampShader.updateUniforms();
for (auto& light : lights) {
lightCube.setModelTransform(
glm::scale(glm::translate(glm::mat4(1.0f), light->getPosition()),
glm::vec3(0.2f)));
lampShader.setVec3("lightColor", light->getDiffuse());
lightCube.draw(lampShader);
}
});
return 0;
}