forked from yahiaetman/OpenGL-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex11_transformation.cpp
86 lines (66 loc) · 2.58 KB
/
ex11_transformation.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
#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>
class TransformationApplication : public our::Application {
our::ShaderProgram program;
our::Mesh quad;
glm::mat4 transformation_matrix = glm::mat4(1.0f);
our::WindowConfiguration getWindowConfiguration() override {
return { "Transformation Matrix", {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();
// Create a mesh quad with colors passing the elements
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);
}
void onDraw(double deltaTime) override {
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
// Set the uniform data of transform as a matrix for the vertex shader.
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("Transformation Matrix");
ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.25f);
for(uint8_t row = 0; row < 4; ++row){
for(uint8_t column = 0; column < 4; ++column){
ImGui::PushID((row << 2) + column);
ImGui::DragFloat("", &(transformation_matrix[column][row]),0.1f);
ImGui::SameLine(0, 0);
ImGui::PopID();
}
ImGui::NewLine();
}
if(ImGui::Button("Reset")){
transformation_matrix = glm::mat4(1.0f);
}
ImGui::End();
}
};
int main(int argc, char** argv) {
return TransformationApplication().run();
}