-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleOcean.cpp
More file actions
463 lines (370 loc) · 16.5 KB
/
Copy pathSimpleOcean.cpp
File metadata and controls
463 lines (370 loc) · 16.5 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "GLUIComponent.h"
#include "PostProcessing/MistPostProcessing.h"
#include "SampleHelper.h"
#include "Scene/CameraController.h"
#include "Skybox.h"
#include "imgui.h"
#include <GL/glew.h>
#include <GLSample.h>
#include <GLSampleWindow.h>
#include <ImageImport.h>
#include <ShaderLoader.h>
#include <glm/fwd.hpp>
#include <glm/geometric.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
namespace glsample {
/**
* @brief
*
*/
class SimpleOcean : public GLSampleWindow {
public:
SimpleOcean() : GLSampleWindow() {
this->setTitle("Simple Ocean");
this->simpleOceanSettingComponent = std::make_shared<SimpleOceanSettingComponent>(*this);
this->addUIComponent(this->simpleOceanSettingComponent);
/* Default camera position and orientation. */
this->camera.setPosition(glm::vec3(0, 500.5f, 0));
this->camera.lookAt(glm::vec3(500.f, 100.0f, 500.0f));
this->camera.setNear(1.0f);
this->camera.setFar(4500.0f);
}
static const size_t nrMaxWaves = 128;
using Wave = struct wave_t {
glm::vec4 waveAmpSpeedStepness; /* */
glm::vec4 direction; /* */
};
struct UniformOceanBufferBlock {
glm::mat4 model{};
glm::mat4 view{};
glm::mat4 proj{};
glm::mat4 modelView{};
glm::mat4 modelViewProjection{};
/* light source. */
DirectionalLightData directional;
CameraInstanceData camera;
/* */
Wave waves[nrMaxWaves]{};
/* */
int nrWaves = 64;
float time = 0.0f;
float stepness = 1;
float rolling = 1;
/* Material */
glm::vec4 oceanColor = glm::vec4(0.4, 0.65, 1, 1);
glm::vec4 specularColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
glm::vec4 ambientColor = glm::vec4(0.2, 0.2, 0.2, 1.0f);
float shininess = 8;
float fresnelPower = 1.333f;
};
/* Combined uniform block. */
struct uniform_buffer_block {
UniformOceanBufferBlock ocean; /* */
} uniform_stage_buffer;
/* */
MeshObject plan;
Skybox skybox;
MistPostProcessing mistprocessing;
/* */
unsigned int normal_texture{};
unsigned int reflection_texture{};
unsigned int irradiance_texture{};
unsigned int color_texture{};
/* */
unsigned int simpleOcean_program = 0;
unsigned int simpleOceanGerstner_program = 0;
/* Uniform buffers. */
unsigned int uniform_buffer_binding = 0;
unsigned int uniform_buffer = 0;
static const size_t nrUniformBuffer = 3;
std::array<UBORange, nrUniformBuffer> UniformBuffers;
size_t uniformAlignBufferSize = sizeof(uniform_buffer_block);
size_t oceanUniformSize = 0;
class SimpleOceanSettingComponent : public GLUIComponent<SimpleOcean> {
public:
SimpleOceanSettingComponent(SimpleOcean &sample)
: GLUIComponent(sample), uniform(this->getRefSample().uniform_stage_buffer) {
this->setName("Simple Ocean Settings");
}
void draw() override {
/* */
ImGui::TextUnformatted("Light Setting");
ImGui::ColorEdit4("Light", &this->uniform.ocean.directional.lightColor[0],
ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_Float);
ImGui::ColorEdit4("Ambient", &this->uniform.ocean.ambientColor[0],
ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_Float);
ImGui::ColorEdit4("Specular Color", &this->uniform.ocean.specularColor[0],
ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_Float);
if (ImGui::DragFloat3("Light Direction", &this->uniform.ocean.directional.lightDirection[0])) {
this->uniform.ocean.directional.lightDirection =
glm::normalize(this->uniform.ocean.directional.lightDirection);
}
/* */
ImGui::TextUnformatted("Ocean");
ImGui::Checkbox("Use Gerstner", &this->useGerstner);
ImGui::DragInt("Number Waves", &this->uniform.ocean.nrWaves, 1, 0, nrMaxWaves);
ImGui::DragFloat("Stepness", &this->uniform.ocean.stepness, 1, 0);
ImGui::DragFloat("rolling", &this->uniform.ocean.rolling, 1, 0);
if (ImGui::CollapsingHeader("Ocean Wave Setttings", &ocean_setting_visable,
ImGuiTreeNodeFlags_CollapsingHeader)) {
for (int i = 0; i < this->uniform.ocean.nrWaves; i++) {
ImGui::PushID(i);
ImGui::Text("Wave %d", i);
ImGui::DragFloat("WaveLength", &this->uniform.ocean.waves[i].waveAmpSpeedStepness[0]);
ImGui::DragFloat("Amplitude", &this->uniform.ocean.waves[i].waveAmpSpeedStepness[1]);
ImGui::DragFloat("Speed", &this->uniform.ocean.waves[i].waveAmpSpeedStepness[2]);
ImGui::DragFloat("Steepness", &this->uniform.ocean.waves[i].waveAmpSpeedStepness[3]);
if (ImGui::DragFloat2("Direction", &this->uniform.ocean.waves[i].direction[0])) {
}
ImGui::PopID();
/* */
}
}
ImGui::TextUnformatted("Ocean Material");
ImGui::DragFloat("Shinines", &this->uniform.ocean.shininess);
ImGui::DragFloat("Fresnel Power", &this->uniform.ocean.fresnelPower);
ImGui::ColorEdit4("Ocean Base Color", &this->uniform.ocean.oceanColor[0],
ImGuiColorEditFlags_HDR | ImGuiColorEditFlags_Float);
ImGui::TextUnformatted("Fog Settings");
ImGui::DragInt("Fog Type",
(int *)&this->getRefSample().mistprocessing.mistsettings.fogSettings.fogType);
ImGui::ColorEdit4("Fog Color",
&this->getRefSample().mistprocessing.mistsettings.fogSettings.fogColor[0],
ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR);
ImGui::DragFloat("Fog Density",
&this->getRefSample().mistprocessing.mistsettings.fogSettings.fogDensity);
ImGui::DragFloat("Fog Intensity",
&this->getRefSample().mistprocessing.mistsettings.fogSettings.fogIntensity);
ImGui::DragFloat("Fog Start", &this->getRefSample().mistprocessing.mistsettings.fogSettings.fogStart);
ImGui::DragFloat("Fog End", &this->getRefSample().mistprocessing.mistsettings.fogSettings.fogEnd);
/* */
ImGui::TextUnformatted("Debug");
ImGui::Checkbox("WireFrame", &this->showWireFrame);
ImGui::Checkbox("Use MistFog", &this->useMistFogPost);
drawCameraController(this->getRefSample().camera);
}
bool showWireFrame = false;
bool useGerstner = false;
bool useMistFogPost = false;
private:
struct uniform_buffer_block &uniform;
bool ocean_setting_visable = true;
};
std::shared_ptr<SimpleOceanSettingComponent> simpleOceanSettingComponent;
CameraController camera;
/* Simple Ocean Wave. */
const std::string vertexSimpleOceanShaderPath = "Shaders/simpleocean/simpleocean.vert.spv";
const std::string fragmentSimpleOceanShaderPath = "Shaders/simpleocean/simpleocean.frag.spv";
const std::string vertexSimpleOceanGerstnerShaderPath = "Shaders/simpleocean/simpleocean_gerstner.vert.spv";
/* Skybox. */
const std::string vertexSkyboxPanoramicShaderPath = "Shaders/skybox/skybox.vert.spv";
const std::string fragmentSkyboxPanoramicShaderPath = "Shaders/skybox/panoramic.frag.spv";
void Release() override {
/* */
glDeleteProgram(this->simpleOcean_program);
/* */
glDeleteTextures(1, (const GLuint *)&this->reflection_texture);
glDeleteTextures(1, (const GLuint *)&this->normal_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>();
{
/* Load shader source. */
const std::vector<uint32_t> vertex_simple_ocean_binary =
IOUtil::readFileData<uint32_t>(vertexSimpleOceanShaderPath, this->getFileSystem());
const std::vector<uint32_t> vertex_simple_ocean_gerstner_binary =
IOUtil::readFileData<uint32_t>(vertexSimpleOceanGerstnerShaderPath, this->getFileSystem());
const std::vector<uint32_t> fragment_simple_ocean_binary =
IOUtil::readFileData<uint32_t>(fragmentSimpleOceanShaderPath, this->getFileSystem());
fragcore::ShaderCompiler::CompilerConvertOption compilerOptions;
compilerOptions.target = fragcore::ShaderLanguage::GLSL;
compilerOptions.glslVersion = this->getShaderVersion();
/* Load shader programs. */
this->simpleOcean_program = ShaderLoader::loadGraphicProgram(
compilerOptions, &vertex_simple_ocean_binary, &fragment_simple_ocean_binary);
this->simpleOceanGerstner_program = ShaderLoader::loadGraphicProgram(
compilerOptions, &vertex_simple_ocean_gerstner_binary, &fragment_simple_ocean_binary);
}
/* Setup graphic pipeline settings. */
glUseProgram(this->simpleOcean_program);
int uniform_buffer_index = glGetUniformBlockIndex(this->simpleOcean_program, "UniformBufferBlock");
glUniform1i(glGetUniformLocation(this->simpleOcean_program, "ReflectionTexture"), 0);
glUniform1i(glGetUniformLocation(this->simpleOcean_program, "NormalTexture"), 1);
glUniform1i(glGetUniformLocation(this->simpleOcean_program, "IrradianceTexture"), 10);
glUniformBlockBinding(this->simpleOcean_program, uniform_buffer_index, this->uniform_buffer_binding);
glUseProgram(0);
/* load Textures */
TextureImporter textureImporter(this->getFileSystem());
this->reflection_texture = textureImporter.loadImage2D(panoramicPath, ColorSpace::RawLinear);
this->skybox.init(this->reflection_texture, Skybox::loadDefaultPanoramicProgram(this->getFileSystem()));
/* */
MiscProcessingUtil util(this->getFileSystem());
util.computeDiffuseIrradianceCubeMap(this->reflection_texture, this->irradiance_texture, 32, 32);
/* */
this->color_texture = CommonUtil::createColorTexture(1, 1, Color(0, 1, 0, 1));
/* */
this->mistprocessing.initialize(this->getFileSystem());
/* Allocate uniform buffers. */
this->UniformBuffers[0] = CommonUtil::getBuffer(this->getUniformPool(), this->uniformAlignBufferSize);
this->UniformBuffers[1] = CommonUtil::getBuffer(this->getUniformPool(), this->uniformAlignBufferSize);
this->UniformBuffers[2] = CommonUtil::getBuffer(this->getUniformPool(), this->uniformAlignBufferSize);
/* Align uniform buffer in respect to driver requirement. */
GLint minMapBufferSize = 0;
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &minMapBufferSize);
this->oceanUniformSize = Math::align<size_t>(sizeof(UniformOceanBufferBlock), (size_t)minMapBufferSize);
this->uniformAlignBufferSize = Math::align<size_t>(this->oceanUniformSize, (size_t)minMapBufferSize);
/* Create uniform buffer. */
glGenBuffers(1, &this->uniform_buffer);
glBindBuffer(GL_UNIFORM_BUFFER, this->uniform_buffer);
glBufferData(GL_UNIFORM_BUFFER, this->uniformAlignBufferSize * nrUniformBuffer, nullptr, GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
/* Load geometry. */
CommonUtil::loadPlan(this->plan, 1, 1024, 1024);
/* Initilize Waves. */
for (size_t i = 0; i < nrMaxWaves; i++) {
float waveLength = ((i * 2.2) + 1);
float waveAmplitude = 0.3f / (i + 1) * (0.05f + (nrMaxWaves - i) * 0.0008f);
float waveSpeed = (i + 1) * 0.1f;
this->uniform_stage_buffer.ocean.waves[i].waveAmpSpeedStepness =
glm::vec4(waveLength, waveAmplitude, waveSpeed, 1);
this->uniform_stage_buffer.ocean.waves[i].direction = glm::normalize(glm::vec4(
(2 * fragcore::Math::random<float>()) - 1.0, (2 * fragcore::Math::random<float>()) - 1.0, 0, 0));
}
}
void onResize(int width, int height) override { this->camera.setAspect((float)width / (float)height); }
void draw() override {
size_t width = 0, height = 0;
this->getCurrentFrameBufferSize(&width, &height);
/* */
glViewport(0, 0, width, height);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT);
/* Skybox */
this->skybox.render(this->camera);
/* Ocean. */
{
/* */
glBindBufferRange(GL_UNIFORM_BUFFER, this->uniform_buffer_binding, this->uniform_buffer,
(this->getFrameCount() % this->nrUniformBuffer) * this->uniformAlignBufferSize,
this->oceanUniformSize);
if (this->simpleOceanSettingComponent->useGerstner) {
glUseProgram(this->simpleOceanGerstner_program);
} else {
glUseProgram(this->simpleOcean_program);
}
/* */
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
/* */
glDisable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/* */
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->reflection_texture);
/* */
glActiveTexture(GL_TEXTURE0 + 1);
glBindTexture(GL_TEXTURE_2D, this->normal_texture);
/* */
glActiveTexture(GL_TEXTURE0 + 10);
glBindTexture(GL_TEXTURE_CUBE_MAP, this->irradiance_texture);
/* */
glActiveTexture(GL_TEXTURE0 + (int)GBuffer::Depth);
glBindTexture(
GL_TEXTURE_2D,
this->getDefaultFrameBufferObj()->attachments[this->getDefaultFrameBufferObj()->depthIndex]);
glCullFace(GL_FRONT);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
/* Draw triangle. */
glBindVertexArray(this->plan.vao);
glDrawElements(GL_TRIANGLES, this->plan.nrIndicesElements, GL_UNSIGNED_INT, nullptr);
/* */
glCullFace(GL_BACK);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
/* Draw triangle. */
glBindVertexArray(this->plan.vao);
glDrawElements(GL_TRIANGLES, this->plan.nrIndicesElements, GL_UNSIGNED_INT, nullptr);
/* */
if (this->simpleOceanSettingComponent->showWireFrame) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDepthFunc(GL_LEQUAL);
/* */
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->color_texture);
glDrawElements(GL_TRIANGLES, this->plan.nrIndicesElements, GL_UNSIGNED_INT, nullptr);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
glBindVertexArray(0);
glUseProgram(0);
}
/* Post processing. */
if (this->simpleOceanSettingComponent->useMistFogPost) {
this->mistprocessing.render(
this->irradiance_texture, this->getDefaultFrameBufferObj()->attachments[0],
this->getDefaultFrameBufferObj()->attachments[this->getDefaultFrameBufferObj()->depthIndex]);
}
}
void update() override {
/* Update Camera. */
const float elapsedTime = this->getTimer().getElapsed<float>();
this->camera.update(this->getTimer().deltaTime<float>());
/* Update Ocean uniforms. */
{
this->uniform_stage_buffer.ocean.model = glm::mat4(1.0f);
this->uniform_stage_buffer.ocean.model = glm::rotate(this->uniform_stage_buffer.ocean.model,
glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
this->uniform_stage_buffer.ocean.model =
glm::scale(this->uniform_stage_buffer.ocean.model, glm::vec3(2000.0f));
this->uniform_stage_buffer.ocean.view = this->camera.getViewMatrix();
this->uniform_stage_buffer.ocean.proj = this->camera.getProjectionMatrix();
this->uniform_stage_buffer.ocean.modelViewProjection = this->uniform_stage_buffer.ocean.proj *
this->uniform_stage_buffer.ocean.view *
this->uniform_stage_buffer.ocean.model;
this->uniform_stage_buffer.ocean.time = elapsedTime;
this->uniform_stage_buffer.ocean.camera.position = glm::vec4(this->camera.getPosition(), 0);
this->uniform_stage_buffer.ocean.camera.near = this->camera.getNear();
this->uniform_stage_buffer.ocean.camera.far = this->camera.getFar();
}
this->mistprocessing.mistsettings.proj = this->camera.getProjectionMatrix();
this->mistprocessing.mistsettings.fogSettings.cameraNear = this->camera.getNear();
this->mistprocessing.mistsettings.fogSettings.cameraFar = this->camera.getFar();
this->mistprocessing.mistsettings.viewRotation = glm::inverse(camera.getRotationMatrix());
/* */
glBindBuffer(GL_UNIFORM_BUFFER, this->uniform_buffer);
uint8_t *uniformPointer = static_cast<uint8_t *>(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.ocean, sizeof(this->uniform_stage_buffer.ocean));
glUnmapBuffer(GL_UNIFORM_BUFFER);
}
}; // namespace glsample
class SimpleOceanGLSample : public GLSample<SimpleOcean> {
public:
SimpleOceanGLSample() : GLSample<SimpleOcean>() {}
void customOptions(cxxopts::OptionAdder &options) override {
options("S,skybox", "Skybox Texture File Path",
cxxopts::value<std::string>()->default_value("asset/industrial_sunset_puresky_4k.exr"));
}
};
} // namespace glsample
int main(int argc, const char **argv) {
try {
glsample::SimpleOceanGLSample sample;
sample.run(argc, argv);
} catch (const std::exception &ex) {
std::cerr << cxxexcept::getStackMessage(ex) << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}