forked from yahiaetman/OpenGL-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex25_blending.cpp
More file actions
334 lines (264 loc) · 12.3 KB
/
ex25_blending.cpp
File metadata and controls
334 lines (264 loc) · 12.3 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
#include <application.hpp>
#include <shader.hpp>
#include <imgui-utils/utils.hpp>
#include <mesh/mesh.hpp>
#include <mesh/mesh-utils.hpp>
#include <texture/texture-utils.h>
#include <camera/camera.hpp>
#include <camera/controllers/fly_camera_controller.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <json/json.hpp>
#include <fstream>
#include <unordered_map>
namespace glm {
template<length_t L, typename T, qualifier Q>
void from_json(const nlohmann::json& j, vec<L, T, Q>& v){
for(length_t index = 0; index < L; ++index)
v[index] = j[index].get<T>();
}
}
struct Transform {
glm::vec4 tint;
glm::vec3 translation, rotation, scale;
bool transparent;
std::optional<std::string> mesh;
std::string texture;
std::unordered_map<std::string, std::shared_ptr<Transform>> children;
explicit Transform(
const glm::vec4& tint = {1,1,1,1},
const glm::vec3& translation = {0,0,0},
const glm::vec3& rotation = {0,0,0},
const glm::vec3& scale = {1,1,1},
bool transparent = false,
const std::optional<std::string>& mesh = std::nullopt,
const std::string& texture = ""
): tint(tint), translation(translation), rotation(rotation), scale(scale), transparent(transparent),
mesh(mesh), texture(texture) {}
[[nodiscard]] glm::mat4 to_mat4() const {
return glm::translate(glm::mat4(1.0f), translation) *
glm::yawPitchRoll(rotation.y, rotation.x, rotation.z) *
glm::scale(glm::mat4(1.0f), scale);
}
};
struct MeshRenderCommand {
bool transparent;
float depth;
glm::vec4 tint;
glm::mat4 transformation;
std::weak_ptr<our::Mesh> mesh;
GLuint texture;
bool operator<(const MeshRenderCommand& other) const {
if(transparent != other.transparent) return transparent < other.transparent;
else if(transparent) return depth > other.depth;
else return depth < other.depth;
}
};
class BlendingApplication : public our::Application {
our::ShaderProgram default_program, alpha_test_program;
std::unordered_map<std::string, std::shared_ptr<our::Mesh>> meshes;
std::unordered_map<std::string, GLuint> textures;
GLuint sampler;
std::shared_ptr<Transform> root;
std::vector<MeshRenderCommand> render_commands;
our::Camera camera;
our::FlyCameraController camera_controller;
bool enable_depth_test = true;
GLenum depth_function = GL_LEQUAL;
bool enable_transparent_depth_write = true;
bool enable_face_culling = true;
GLenum culled_face = GL_BACK;
GLenum front_face_winding = GL_CCW;
bool enable_blending;
GLenum blend_equation = GL_FUNC_ADD;
GLenum blend_source_function = GL_SRC_ALPHA, blend_destination_function = GL_ONE_MINUS_SRC_ALPHA;
glm::vec4 blend_constant_color = {1.0f,1.0f,1.0f,1.0f};
bool enable_alpha_test = false;
float alpha_test_threshold = 0.5;
bool enable_alpha_to_coverage = false;
bool sort_render_commands = false;
void configureOpenGL() override {
our::Application::configureOpenGL();
glfwWindowHint(GLFW_SAMPLES, 4);
}
our::WindowConfiguration getWindowConfiguration() override {
return { "Sampler Objects", {1280, 720}, false };
}
void onInitialize() override {
default_program.create();
default_program.attach("assets/shaders/ex22_texture_sampling/transform.vert", GL_VERTEX_SHADER);
default_program.attach("assets/shaders/ex22_texture_sampling/texture.frag", GL_FRAGMENT_SHADER);
default_program.link();
alpha_test_program.create();
alpha_test_program.attach("assets/shaders/ex22_texture_sampling/transform.vert", GL_VERTEX_SHADER);
alpha_test_program.attach("assets/shaders/ex25_blending/alpha_test.frag", GL_FRAGMENT_SHADER);
alpha_test_program.link();
GLuint texture;
glGenTextures(1, &texture);
our::texture_utils::singleColor(texture, {255,255,255,255});
textures["white"] = texture;
glGenTextures(1, &texture);
our::texture_utils::checkerBoard(texture, {256,256}, {128,128}, {255, 255, 255, 255}, {16, 16, 16, 255});
textures["checkerboard"] = texture;
glGenTextures(1, &texture);
our::texture_utils::loadImage(texture, "assets/images/common/color-grid.png");
textures["color-grid"] = texture;
glGenTextures(1, &texture);
our::texture_utils::loadImage(texture, "assets/images/common/moon.jpg");
textures["moon"] = texture;
glGenTextures(1, &texture);
our::texture_utils::loadImage(texture, "assets/images/ex25_blending/glass-panels.png");
textures["glass-panels"] = texture;
glGenTextures(1, &texture);
our::texture_utils::loadImage(texture, "assets/images/ex25_blending/metal.png");
textures["metal"] = texture;
glGenTextures(1, &texture);
our::texture_utils::loadImage(texture, "assets/images/ex25_blending/fog.png");
textures["fog"] = texture;
meshes["cube"] = std::make_shared<our::Mesh>();
our::mesh_utils::Cuboid(*(meshes["cube"]));
meshes["plane"] = std::make_shared<our::Mesh>();
our::mesh_utils::Plane(*(meshes["plane"]), {1, 1}, false, {0, 0, 0}, {1, 1}, {0, 0}, {100, 100});
meshes["sphere"] = std::make_shared<our::Mesh>();
our::mesh_utils::Sphere(*(meshes["sphere"]), {32, 16}, false);
glGenSamplers(1, &sampler);
glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_REPEAT);
glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_REPEAT);
int width, height;
glfwGetFramebufferSize(window, &width, &height);
camera.setEyePosition({10, 10, 10});
camera.setTarget({0, 0, 0});
camera.setUp({0, 1, 0});
camera.setupPerspective(glm::pi<float>()/2, static_cast<float>(width)/height, 0.1f, 100.0f);
camera_controller.initialize(this, &camera);
camera_controller.setFieldOfViewSensitivity(0.05f );
std::ifstream file_in("assets/data/ex25_blending/scene.json");
nlohmann::json json;
file_in >> json;
file_in.close();
root = loadNode(json);
glClearColor(0.88,0.65,0.15, 1);
}
std::shared_ptr<Transform> loadNode(const nlohmann::json& json){
auto node = std::make_shared<Transform>(
json.value<glm::vec4>("tint", {1,1,1,1}),
json.value<glm::vec3>("translation", {0, 0, 0}),
json.value<glm::vec3>("rotation", {0, 0, 0}),
json.value<glm::vec3>("scale", {1, 1, 1}),
json.value<bool>("transparent", false)
);
if(json.contains("mesh")){
node->mesh = json["mesh"].get<std::string>();
}
if(json.contains("texture")){
node->texture = json["texture"].get<std::string>();
}
if(json.contains("children")){
for(auto& [name, child]: json["children"].items()){
node->children[name] = loadNode(child);
}
}
return node;
}
void buildRenderCommands(const std::shared_ptr<Transform>& node, const glm::mat4& parent_transform_matrix){
glm::mat4 transform_matrix = parent_transform_matrix * node->to_mat4();
if(node->mesh.has_value()){
if(auto mesh_it = meshes.find(node->mesh.value()); mesh_it != meshes.end()) {
GLuint texture = 0;
if(auto tex_it = textures.find(node->texture); tex_it != textures.end())
texture = tex_it->second;
glm::vec4 transformed_center = transform_matrix * glm::vec4(0, 0, 0, 1);
float depth = transformed_center.z / transformed_center.w;
render_commands.push_back({
node->transparent,
depth,
node->tint,
transform_matrix,
mesh_it->second,
texture
});
}
}
for(auto& [name, child]: node->children){
buildRenderCommands(child, transform_matrix);
}
}
void onDraw(double deltaTime) override {
camera_controller.update(deltaTime);
auto& program = enable_alpha_test ? alpha_test_program : default_program;
if(enable_depth_test) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST);
glDepthFunc(depth_function);
if(enable_face_culling) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE);
glCullFace(culled_face);
glFrontFace(front_face_winding);
glBlendEquation(blend_equation);
glBlendFunc(blend_source_function, blend_destination_function);
glBlendColor(blend_constant_color.r, blend_constant_color.g, blend_constant_color.b, blend_constant_color.a);
if(enable_alpha_to_coverage) glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
else glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
glUseProgram(program);
if(enable_alpha_test) program.set("alpha_threshold", alpha_test_threshold);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindSampler(0, sampler);
program.set("sampler", 0);
render_commands.clear();
buildRenderCommands(root, camera.getVPMatrix());
if(sort_render_commands)
std::sort(std::begin(render_commands), std::end(render_commands));
for(auto& render_command: render_commands){
if(render_command.transparent && enable_blending) glEnable(GL_BLEND);
else glDisable(GL_BLEND);
glDepthMask(!render_command.transparent || enable_transparent_depth_write);
glBindTexture(GL_TEXTURE_2D, render_command.texture);
program.set("tint", render_command.tint);
program.set("transform", render_command.transformation);
render_command.mesh.lock()->draw();
}
glDepthMask(GL_TRUE);
}
void onDestroy() override {
default_program.destroy();
alpha_test_program.destroy();
glDeleteSamplers(1, &sampler);
for(auto& [name, texture]: textures){
glDeleteTextures(1, &texture);
}
textures.clear();
for(auto& [name, mesh]: meshes){
mesh->destroy();
}
meshes.clear();
}
void onImmediateGui(ImGuiIO &io) override {
ImGui::Begin("Controls");
ImGui::Checkbox("Enable Blending", &enable_blending);
our::OptionMapCombo("Equation", blend_equation, our::gl_enum_options::blend_equations);
our::OptionMapCombo("Source Function", blend_source_function, our::gl_enum_options::blend_functions);
our::OptionMapCombo("Destination Function", blend_destination_function, our::gl_enum_options::blend_functions);
ImGui::ColorEdit4("Blend Constant Color", glm::value_ptr(blend_constant_color), ImGuiColorEditFlags_HDR);
ImGui::Separator();
ImGui::Checkbox("Enable Sorting", &sort_render_commands);
ImGui::TextWrapped("Sorting will render opaque objects first followed by transparent objects.");
ImGui::TextWrapped("Opaque objects are renderer from nearest to farthest.");
ImGui::TextWrapped("transparent objects are renderer from farthest to nearest.");
ImGui::Separator();
ImGui::Checkbox("Enable Alpha Testing", &enable_alpha_test);
ImGui::DragFloat("Alpha Threshold", &alpha_test_threshold, 0.01f, 0.0f, 1.0f);
ImGui::Separator();
ImGui::Checkbox("Enable Alpha To Coverage", &enable_alpha_to_coverage);
ImGui::Separator();
ImGui::Checkbox("Enable Depth Testing", &enable_depth_test);
our::OptionMapCombo("Comparison Function", depth_function, our::gl_enum_options::comparison_functions);
ImGui::Checkbox("Enable Transparent Depth Write", &enable_transparent_depth_write);
ImGui::Separator();
ImGui::Checkbox("Enable Face Culling", &enable_face_culling);
our::OptionMapCombo("Face To Cull", culled_face, our::gl_enum_options::facets);
our::OptionMapCombo("Front Face", front_face_winding, our::gl_enum_options::face_windings);
ImGui::End();
}
};
int main(int argc, char** argv) {
return BlendingApplication().run();
}