-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.cpp
91 lines (68 loc) · 2.46 KB
/
component.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
#include "component.hpp"
component::component(glm::vec4 color) : color(color) {
this->create_buffers();
}
component::component() : component(BLACK) { }
component::component(const component & c) : color(c.color), vertices(c.vertices), indices(c.indices) {
this->create_buffers();
}
component::~component() {
this->delete_buffers();
}
void component::apply(std::shared_ptr<component> animation) {
for (int chan : animation->color.get_channels()) {
if (animation->color[chan]->active) {
this->color[chan] = animation->color[chan]->copy();
}
}
}
void component::draw(const glm::mat4 & view, const glm::mat4 & projection) {
this->get_shader()->use();
this->bind_uniforms();
this->get_shader()->set_mat4("view", view);
this->get_shader()->set_mat4("projection", projection);
this->update_vertices();
this->update_indices();
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void component::create_buffers() {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
/*
glBindVertexArray(VAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void *) 0);
glBindVertexArray(0);
*/
}
void component::init_buffers() {
glBindVertexArray(VAO);
this->calc_vertices();
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices.front(), GL_STATIC_DRAW);
this->calc_indices();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices.front(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void *) 0);
glBindVertexArray(0);
}
void component::delete_buffers() {
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
}
void component::update_vertices() {
this->calc_vertices();
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(glm::vec3), &vertices.front());
}
void component::update_indices() {
this->calc_indices();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indices.size() * sizeof(unsigned int), &indices.front());
}