-
Notifications
You must be signed in to change notification settings - Fork 12
/
ex12_composition.cpp
142 lines (115 loc) · 4.82 KB
/
ex12_composition.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
#include <application.hpp>
#include <shader.hpp>
#include <imgui-utils/utils.hpp>
#include <mesh/mesh.hpp>
#include <mesh/common-vertex-types.hpp>
#include <mesh/common-vertex-attributes.hpp>
#include <glm/gtx/euler_angles.hpp>
enum class TransformationType : int {
Translation = 0,
Rotation = 1,
Scaling = 2
};
struct Transformation {
TransformationType type;
glm::vec3 value;
};
class CompositionApplication : public our::Application {
our::ShaderProgram program;
our::Mesh quad;
std::vector<Transformation> transformations;
our::WindowConfiguration getWindowConfiguration() override {
return { "Transformation Matrix Composition", {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();
quad.create({our::setup_buffer_accessors<our::ColoredVertex>});
quad.setVertexData<our::ColoredVertex>(0, {
{{-0.5, -0.5, 0},{255, 0, 0, 255}},
{{ 0.5, -0.5, 0},{ 0, 255, 0, 255}},
{{ 0.5, 0.5, 0},{ 0, 0, 255, 255}},
{{-0.5, 0.5, 0},{255, 255, 0, 255}}
},GL_STATIC_DRAW);
quad.setElementData<GLuint>({
0, 1, 2,
2, 3, 0
},GL_STATIC_DRAW);
glClearColor(0, 0, 0, 0);
}
// We compose the transformation according to the array set by the GUI.
// It multiplies the selected matrix type by the last matrix value.
// The start value of the matrix is identity.
glm::mat4 compose(){
glm::mat4 transformation_matrix(1.0f);
for(const auto& transformation : transformations){
switch (transformation.type) {
case TransformationType::Translation:
transformation_matrix = glm::translate(glm::mat4(1.0f), transformation.value) * transformation_matrix;
break;
case TransformationType::Rotation:
transformation_matrix = glm::yawPitchRoll(transformation.value.y, transformation.value.x, transformation.value.z) * transformation_matrix;
break;
case TransformationType::Scaling:
transformation_matrix = glm::scale(glm::mat4(1.0f), transformation.value) * transformation_matrix;
break;
}
}
return transformation_matrix;
}
void onDraw(double deltaTime) override {
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
auto transformation_matrix = compose();
program.set("transform", transformation_matrix);
program.set("tint", glm::vec4(1,1,1,1));
quad.draw();
}
void onDestroy() override {
program.destroy();
quad.destroy();
}
void onImmediateGui(ImGuiIO &io) override {
ImGui::Begin("Controls");
ImGui::Text("Transformations");
const char* transformation_names[] = {
"Translation",
"Rotation",
"Scaling"
};
our::ReorderableList(transformations.begin(), transformations.end(),
[transformation_names](size_t index, Transformation& transform){
auto selected = static_cast<int>(transform.type);
if(ImGui::BeginCombo("Type", transformation_names[selected])){
for(int selectable = 0; selectable < 3; ++selectable){
bool is_selected = selected == selectable;
if(ImGui::Selectable(transformation_names[selectable], is_selected))
selected = selectable;
if(is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
transform.type = static_cast<TransformationType>(selected);
}
ImGui::DragFloat3("Value", glm::value_ptr(transform.value), 0.1f);
}, [this](size_t index){
transformations.insert(transformations.begin() + index, { TransformationType::Translation, glm::vec3() });
}, [this](size_t index){
transformations.erase(transformations.begin() + index);
});
if(ImGui::Button("Clear")){
transformations.clear();
}
ImGui::Separator();
ImGui::Text("Result:");
auto matrix = compose();
for(int row = 0; row < 4; ++row)
ImGui::Text("%f\t%f\t%f\t%f", matrix[0][row], matrix[1][row], matrix[2][row], matrix[3][row]);
ImGui::End();
}
};
int main(int argc, char** argv) {
return CompositionApplication().run();
}