forked from yahiaetman/OpenGL-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex16_face_culling.cpp
172 lines (127 loc) · 5.74 KB
/
ex16_face_culling.cpp
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
#include <application.hpp>
#include <shader.hpp>
#include <imgui-utils/utils.hpp>
#include <mesh/mesh.hpp>
#include <mesh/mesh-utils.hpp>
#include <mesh/common-vertex-types.hpp>
#include <mesh/common-vertex-attributes.hpp>
#include <camera/camera.hpp>
#include <camera/controllers/fly_camera_controller.hpp>
#include <glm/gtx/euler_angles.hpp>
struct Transform {
glm::vec3 translation, rotation, scale;
Transform(
const glm::vec3& translation = {0,0,0},
const glm::vec3& rotation = {0,0,0},
const glm::vec3& scale = {1,1,1}
): translation(translation), rotation(rotation), scale(scale) {}
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);
}
};
class DepthTestingAndFaceCullingApplication : public our::Application {
our::ShaderProgram program;
our::Mesh triangle, model;
std::vector<Transform> objects;
Transform triangle_transform;
our::Camera camera;
our::FlyCameraController camera_controller;
bool enable_depth_test = false;
GLenum depth_function = GL_LEQUAL;
bool enable_face_culling = false;
GLenum culled_face = GL_BACK;
GLenum front_face_winding = GL_CCW;
bool draw_triangle = true;
our::WindowConfiguration getWindowConfiguration() override {
return { "Face Culling", {1280, 720}, false };
}
void onInitialize() override {
program.create();
program.attach("assets/shaders/ex11_transformation/transform.vert", GL_VERTEX_SHADER);
program.attach("assets/shaders/ex11_transformation/tint.frag", GL_FRAGMENT_SHADER);
program.link();
triangle.create({our::setup_buffer_accessors<our::ColoredVertex>});
triangle.setVertexData<our::ColoredVertex>(0, {
{{-0.5, -0.5, 0},{255, 0, 0, 255}},
{{ 0.5, -0.5, 0},{ 0, 255, 0, 255}},
{{ 0.0, 0.5, 0},{ 0, 0, 255, 255}}
},GL_STATIC_DRAW);
triangle.setElementData<GLuint>({
0, 1, 2
},GL_STATIC_DRAW);
our::mesh_utils::Cuboid(model, true);
objects.push_back({ {0,-1,0}, {0,0,0}, {11,2,11} });
objects.push_back({ {-4,1,-4}, {0,0,0}, {2,2,2} });
objects.push_back({ {4,1,-4}, {0,0,0}, {2,2,2} });
objects.push_back({ {-4,1,4}, {0,0,0}, {2,2,2} });
objects.push_back({ {4,1,4}, {0,0,0}, {2,2,2} });
triangle_transform = { {0,1,0}, {0,0,0}, {2,2,2} };
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);
glClearColor(0, 0, 0, 0);
}
void onDraw(double deltaTime) override {
camera_controller.update(deltaTime);
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); // This line enables face culling.
glCullFace(culled_face);
glFrontFace(front_face_winding);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
program.set("tint", glm::vec4(1,1,1,1));
for(const auto& object : objects) {
program.set("transform", camera.getVPMatrix() * object.to_mat4());
model.draw();
}
if(draw_triangle){
program.set("transform", camera.getVPMatrix() * triangle_transform.to_mat4());
triangle.draw();
}
}
void onDestroy() override {
program.destroy();
model.destroy();
triangle.destroy();
camera_controller.release();
}
void onImmediateGui(ImGuiIO &io) override {
ImGui::Begin("Objects");
our::ReorderableList(objects.begin(), objects.end(),
[](size_t index, Transform& transform){
ImGui::DragFloat3("Translation", glm::value_ptr(transform.translation), 1.0f);
ImGui::DragFloat3("Rotation", glm::value_ptr(transform.rotation), 0.1f);
ImGui::DragFloat3("Scale", glm::value_ptr(transform.scale), 0.1f);
}, [this](size_t index){
objects.insert(objects.begin() + index, Transform());
}, [this](size_t index){
objects.erase(objects.begin() + index);
});
ImGui::End();
ImGui::Begin("Controls");
ImGui::Text("Depth Testing");
ImGui::Checkbox("Enable Depth Testing", &enable_depth_test);
our::OptionMapCombo("Comparison Function", depth_function, our::gl_enum_options::comparison_functions);
ImGui::Separator();
ImGui::Text("Face Culling");
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::Separator();
ImGui::Text("Triangle Vertex Order: Red -> Green -> Blue");
ImGui::DragFloat3("Translation", glm::value_ptr(triangle_transform.translation), 1.0f);
ImGui::DragFloat3("Rotation", glm::value_ptr(triangle_transform.rotation), 0.1f);
ImGui::DragFloat3("Scale", glm::value_ptr(triangle_transform.scale), 0.1f);
ImGui::End();
}
};
int main(int argc, char** argv) {
return DepthTestingAndFaceCullingApplication().run();
}