forked from yahiaetman/OpenGL-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex10_model_loading.cpp
106 lines (85 loc) · 3.06 KB
/
ex10_model_loading.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
#include <application.hpp>
#include <shader.hpp>
#include <imgui-utils/utils.hpp>
#include <mesh/mesh.hpp>
#include <mesh/common-vertex-types.hpp>
#include <mesh/mesh-utils.hpp>
struct Vertex {
glm::vec3 position;
glm::vec<4, glm::uint8, glm::defaultp> color;
};
class MeshApplication : public our::Application {
our::ShaderProgram program;
our::Mesh quad, model;
glm::vec4 clear_color;
uint8_t mesh_to_render_index;
our::WindowConfiguration getWindowConfiguration() override {
return { "Model Loading", {1280, 720}, false };
}
void onInitialize() override {
program.create();
program.attach("assets/shaders/ex06_multiple_attributes/multiple_attributes.vert", GL_VERTEX_SHADER);
program.attach("assets/shaders/ex04_varyings/varying_color.frag", GL_FRAGMENT_SHADER);
program.link();
quad.create({
[](){
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(Vertex), (void*)offsetof(Vertex, position));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, true, sizeof(Vertex), (void*)offsetof(Vertex, color));
}
});
quad.setVertexData<Vertex>(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);
our::mesh_utils::loadOBJ(model, "assets/models/Suzanne/Suzanne.obj");
}
void onDraw(double deltaTime) override {
glClearColor(clear_color.r, clear_color.g, clear_color.b, clear_color.a);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
switch (mesh_to_render_index) {
case 0:
quad.draw();
break;
case 1:
model.draw();
break;
}
}
void onDestroy() override {
program.destroy();
quad.destroy();
model.destroy();
}
void onImmediateGui(ImGuiIO &io) override {
ImGui::Begin("Controls");
const char* model_names[] = {
"Quad",
"Model"
};
const size_t model_count = sizeof(model_names)/sizeof(model_names[0]);
if(ImGui::BeginCombo("Mesh", model_names[mesh_to_render_index])){
for(size_t index = 0; index < model_count; ++index){
bool is_selected = mesh_to_render_index == index;
if(ImGui::Selectable(model_names[index], is_selected))
mesh_to_render_index = index;
if(is_selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
ImGui::ColorEdit4("Clear Color", glm::value_ptr(clear_color));
ImGui::End();
}
};
int main(int argc, char** argv) {
return MeshApplication().run();
}