-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleReflection.cpp
More file actions
390 lines (305 loc) · 12.9 KB
/
SimpleReflection.cpp
File metadata and controls
390 lines (305 loc) · 12.9 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "ModelImporter.h"
#include "Scene/Scene.h"
#include "Scene/SceneHelper.h"
#include "Skybox.h"
#include <GL/glew.h>
#include <GLSample.h>
#include <GLSampleWindow.h>
#include <ImageImport.h>
#include <ShaderLoader.h>
#include <glm/gtc/matrix_transform.hpp>
namespace glsample {
/**
* @brief Stencil buffer simple sample.
*
*/
class SimpleReflection : public GLSampleWindow {
public:
SimpleReflection() : GLSampleWindow() {
this->setTitle("Simple Reflection");
this->simpleReflectionSettingComponent =
std::make_shared<SimpleOceanSettingComponent>(this->uniform_stage_buffer, this->depthstencil_texture);
this->addUIComponent(this->simpleReflectionSettingComponent);
/* Default camera position and orientation. */
this->camera.setPosition(glm::vec3(15.5f));
this->camera.lookAt(glm::vec3(0.f));
}
struct UniformObjectBufferBlock {
glm::mat4 model{};
glm::mat4 view{};
glm::mat4 proj{};
glm::mat4 modelView{};
glm::mat4 modelViewProjection{};
/* Light source. */
glm::vec4 direction = glm::vec4(0.7, 0.7, 1, 1);
glm::vec4 lightColor = glm::vec4(1, 1, 1, 1);
glm::vec4 specularColor = glm::vec4(1, 1, 1, 1);
glm::vec4 ambientColor = glm::vec4(0.2, 0.2, 0.2, 1);
glm::vec4 viewDir{};
float shininess = 8;
} uniform_stage_buffer;
/* */
MeshObject plan;
Skybox skybox;
Scene *scene{};
/* G-Buffer */
unsigned int multipass_framebuffer{};
unsigned int multipass_program{};
unsigned int multipass_texture_width{};
unsigned int multipass_texture_height{};
unsigned int multipass_texture{};
unsigned int depthstencil_texture{};
/* */
unsigned int skybox_texture{};
/* */
unsigned int graphic_program{};
unsigned int skybox_program{};
/* Uniform buffers. */
unsigned int uniform_buffer_binding = 0;
unsigned int uniform_buffer{};
const size_t nrUniformBuffer = 3;
size_t uniformAlignBufferSize = sizeof(UniformObjectBufferBlock);
class SimpleOceanSettingComponent : public nekomimi::UIComponent {
public:
SimpleOceanSettingComponent(struct UniformObjectBufferBlock &uniform, unsigned int &depth)
: depth(depth), uniform(uniform) {
this->setName("Simple Reflection Settings");
}
void draw() override {
ImGui::TextUnformatted("Material Settings");
ImGui::ColorEdit4("Ambient Color", &this->uniform.ambientColor[0],
ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_Float);
ImGui::DragFloat("Shinnes", &this->uniform.shininess);
ImGui::TextUnformatted("Debug");
/* */
ImGui::Checkbox("WireFrame", &this->showWireFrame);
ImGui::TextUnformatted("Depth Texture");
ImGui::Image(static_cast<ImTextureID>(this->depth), ImVec2(512, 512));
}
bool showWireFrame = false;
unsigned int &depth;
private:
struct UniformObjectBufferBlock &uniform;
};
std::shared_ptr<SimpleOceanSettingComponent> simpleReflectionSettingComponent;
CameraController camera;
const std::string vertexShaderPath = "Shaders/phongblinn/phongblinn_directional_light.vert.spv";
const std::string fragmentShaderPath = "Shaders/phongblinn/phong_directional_light.frag.spv";
void Release() override {
/* */
glDeleteProgram(this->skybox_program);
glDeleteProgram(this->graphic_program);
/* */
glDeleteTextures(1, (const GLuint *)&this->skybox_texture);
glDeleteBuffers(1, &this->uniform_buffer);
/* */
glDeleteVertexArrays(1, &this->plan.vao);
glDeleteBuffers(1, &this->plan.vbo);
glDeleteBuffers(1, &this->plan.ibo);
}
void Initialize() override {
const std::string panoramicPath = this->getResult()["skybox"].as<std::string>();
const std::string modelPath = this->getResult()["model"].as<std::string>();
{
/* Load shader source data. */
const std::vector<uint32_t> vertex_simple_ocean_binary =
IOUtil::readFileData<uint32_t>(vertexShaderPath, this->getFileSystem());
const std::vector<uint32_t> fragment_simple_ocean_binary =
IOUtil::readFileData<uint32_t>(fragmentShaderPath, this->getFileSystem());
fragcore::ShaderCompiler::CompilerConvertOption compilerOptions;
compilerOptions.target = fragcore::ShaderLanguage::GLSL;
compilerOptions.glslVersion = this->getShaderVersion();
/* Load shader programs. */
this->graphic_program = ShaderLoader::loadGraphicProgram(compilerOptions, &vertex_simple_ocean_binary,
&fragment_simple_ocean_binary);
this->skybox_program = Skybox::loadDefaultPanoramicProgram(this->getFileSystem());
}
/* Setup graphic pipeline settings. */
glUseProgram(this->graphic_program);
int uniform_buffer_index = glGetUniformBlockIndex(this->graphic_program, "UniformBufferBlock");
glUniform1i(glGetUniformLocation(this->graphic_program, "DiffuseTexture"), 0);
glUniform1i(glGetUniformLocation(this->graphic_program, "NormalTexture"), 1);
glUniformBlockBinding(this->graphic_program, uniform_buffer_index, this->uniform_buffer_binding);
glUseProgram(0);
/* load Textures */
TextureImporter textureImporter(this->getFileSystem());
this->skybox_texture = textureImporter.loadImage2D(panoramicPath, ColorSpace::RawLinear);
this->skybox.init(this->skybox_texture, this->skybox_program);
/* Align uniform buffer in respect to driver requirement. */
GLint minMapBufferSize = 0;
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &minMapBufferSize);
this->uniformAlignBufferSize =
fragcore::Math::align<size_t>(sizeof(UniformObjectBufferBlock), (size_t)minMapBufferSize);
/* Create uniform buffer. */
glGenBuffers(1, &this->uniform_buffer);
glBindBuffer(GL_UNIFORM_BUFFER, this->uniform_buffer);
glBufferData(GL_UNIFORM_BUFFER, this->uniformAlignBufferSize * this->nrUniformBuffer, nullptr,
GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
/* */
ModelImporter modelLoader = ModelImporter(this->getFileSystem());
modelLoader.loadContent(modelPath, 0);
this->scene = SceneHelper::loadFrom(modelLoader);
/* Plan. */
CommonUtil::loadPlan(this->plan, 1);
/* Create multipass framebuffer. */
glGenFramebuffers(1, &this->multipass_framebuffer);
/* */
glGenTextures(1, &this->multipass_texture);
glGenTextures(1, &this->depthstencil_texture);
onResize(this->width(), this->height());
}
void onResize(int width, int height) override {
this->multipass_texture_width = width;
this->multipass_texture_height = height;
glBindFramebuffer(GL_FRAMEBUFFER, this->multipass_framebuffer);
/* Resize the image. */
glBindTexture(GL_TEXTURE_2D, this->multipass_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, this->multipass_texture_width, this->multipass_texture_height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
/* Border clamped to max value, it makes the outside area. */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
FVALIDATE_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 0));
FVALIDATE_GL_CALL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0.0f));
FVALIDATE_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0));
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->multipass_texture, 0);
glBindTexture(GL_TEXTURE_2D, this->depthstencil_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, this->multipass_texture_width,
this->multipass_texture_height, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
/* Border clamped to max value, it makes the outside area. */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_TEXTURE_2D,
this->depthstencil_texture, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glReadBuffer(GL_COLOR_ATTACHMENT0);
/* Validate if created properly.*/
const int frameStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (frameStatus != GL_FRAMEBUFFER_COMPLETE) {
throw RuntimeException("Failed to create framebuffer, {}", frameStatus);
}
glBindFramebuffer(GL_FRAMEBUFFER, this->getDefaultFramebuffer());
this->camera.setAspect((float)width / (float)height);
}
void draw() override {
size_t width = 0, height = 0;
this->getCurrentFrameBufferSize(&width, &height);
/* */
glBindBufferRange(GL_UNIFORM_BUFFER, this->uniform_buffer_binding, this->uniform_buffer,
(this->getFrameCount() % this->nrUniformBuffer) * this->uniformAlignBufferSize,
this->uniformAlignBufferSize);
/* */
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, this->multipass_framebuffer);
glViewport(0, 0, width, height);
glClearColor(0.01f, 0.01f, 0.01f, 1.0f);
glStencilMask(0xff);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
/* Create Stencils mask of the plan. */
{
glUseProgram(this->graphic_program);
glEnable(GL_STENCIL_TEST);
glDisable(GL_DEPTH_TEST);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glStencilFunc(GL_NEVER, 255, 0xFF);
glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP);
glStencilMask(0xFF);
/* Draw triangle. */
glBindVertexArray(this->plan.vao);
glDrawElements(GL_TRIANGLES, this->plan.nrIndicesElements, GL_UNSIGNED_INT, nullptr);
glBindVertexArray(0);
glUseProgram(0);
}
/* Normal Object */
{
glUseProgram(this->graphic_program);
glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glDepthMask(GL_TRUE);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glStencilFunc(GL_EQUAL, 255, 0xff);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glStencilMask(0x0);
this->scene->render();
glUseProgram(0);
}
glDisable(GL_STENCIL_TEST);
/* Render reflected objects. */
{
glUseProgram(this->graphic_program);
glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
// glStencilOp(GLenum fail, GLenum zfail, GLenum zpass)
// glDepthMask(GL_TRUE);
// glStencilFunc(GL_EQUAL, 1, 0xFF);
// glStencilMask(0);
/* */
this->scene->render();
glUseProgram(0);
}
this->skybox.render(this->camera);
// Transfer the result.
{
glBindFramebuffer(GL_READ_FRAMEBUFFER, this->multipass_framebuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, this->getDefaultFramebuffer());
/* */
glViewport(0, 0, width, height);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glBlitFramebuffer(0, 0, this->multipass_texture_width, this->multipass_texture_height, 0, 0, width,
height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, this->getDefaultFramebuffer());
}
}
void update() override {
/* Update Camera. */
this->camera.update(this->getTimer().deltaTime<float>());
this->scene->update(this->getTimer().deltaTime<float>());
/* */
this->uniform_stage_buffer.proj = this->camera.getProjectionMatrix();
this->uniform_stage_buffer.model = glm::mat4(1.0f);
this->uniform_stage_buffer.model = glm::scale(this->uniform_stage_buffer.model, glm::vec3(0.85f));
this->uniform_stage_buffer.view = this->camera.getViewMatrix();
this->uniform_stage_buffer.modelViewProjection =
this->uniform_stage_buffer.proj * this->uniform_stage_buffer.view * this->uniform_stage_buffer.model;
this->uniform_stage_buffer.viewDir = glm::vec4(this->camera.getLookDirection(), 0);
/* */
{
glBindBuffer(GL_UNIFORM_BUFFER, this->uniform_buffer);
void *uniformPointer = glMapBufferRange(
GL_UNIFORM_BUFFER,
((this->getFrameCount() + 1) % this->nrUniformBuffer) * this->uniformAlignBufferSize,
this->uniformAlignBufferSize,
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
memcpy(uniformPointer, &this->uniform_stage_buffer, sizeof(this->uniform_stage_buffer));
glUnmapBuffer(GL_UNIFORM_BUFFER);
}
}
}; // namespace glsample
class SimpleReflectionGLSample : public GLSample<SimpleReflection> {
public:
SimpleReflectionGLSample() : GLSample<SimpleReflection>() {}
void customOptions(cxxopts::OptionAdder &options) override {
options("S,skybox", "SkyboxPath",
cxxopts::value<std::string>()->default_value("asset/snowy_forest_4k.exr"))(
"M,model", "Model Path", cxxopts::value<std::string>()->default_value("asset/rungholt/house.obj"));
}
};
} // namespace glsample
int main(int argc, const char **argv) {
try {
glsample::SimpleReflectionGLSample sample;
sample.run(argc, argv);
} catch (const std::exception &ex) {
std::cerr << cxxexcept::getStackMessage(ex) << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}