16 glGenVertexArrays(1, &va);
17 glGenBuffers(ATTRIB_COUNT, handle);
21 glDeleteBuffers(ATTRIB_COUNT, handle);
22 glDeleteVertexArrays(1, &va);
25 Model::Model(Model &&other)
26 : vertices(std::move(other.vertices))
27 , colors(std::move(other.colors))
28 , normals(std::move(other.normals))
29 , indices(std::move(other.indices))
31 , dirty(other.dirty) {
33 for (int i = 0; i < ATTRIB_COUNT; ++i) {
34 handle[i] = other.handle[i];
39 Model &Model::operator =(Model &&other) {
40 std::swap(vertices, other.vertices);
41 std::swap(colors, other.colors);
42 std::swap(normals, other.normals);
43 std::swap(indices, other.indices);
44 std::swap(va, other.va);
45 for (int i = 0; i < ATTRIB_COUNT; ++i) {
46 std::swap(handle[i], other.handle[i]);
61 void Model::Reserve(int v, int i) {
69 void Model::CheckUpdate() {
71 glBindVertexArray(va);
76 void Model::Update() {
77 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
78 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
79 glEnableVertexAttribArray(ATTRIB_VERTEX);
80 glVertexAttribPointer(
81 ATTRIB_VERTEX, // location (for shader)
84 GL_FALSE, // normalized
90 if (colors.size() < vertices.size()) {
91 std::cerr << "Model: not enough colors!" << std::endl;
92 colors.resize(vertices.size(), { 1, 0, 1 });
95 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
96 glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
97 glEnableVertexAttribArray(ATTRIB_COLOR);
98 glVertexAttribPointer(
99 ATTRIB_COLOR, // location (for shader)
102 GL_FALSE, // normalized
108 if (normals.size() < vertices.size()) {
109 std::cerr << "Model: not enough normals!" << std::endl;
110 normals.resize(vertices.size(), { 0, 1, 0 });
113 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_NORMAL]);
114 glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), normals.data(), GL_STATIC_DRAW);
115 glEnableVertexAttribArray(ATTRIB_NORMAL);
116 glVertexAttribPointer(
117 ATTRIB_NORMAL, // location (for shader)
120 GL_FALSE, // normalized
125 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
126 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
133 glBindVertexArray(va);
139 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
142 indices.size(), // count
143 GL_UNSIGNED_INT, // type
149 OutlineModel::OutlineModel()
156 glGenVertexArrays(1, &va);
157 glGenBuffers(ATTRIB_COUNT, handle);
160 OutlineModel::~OutlineModel() {
161 glDeleteBuffers(ATTRIB_COUNT, handle);
162 glDeleteVertexArrays(1, &va);
166 void OutlineModel::Clear() {
173 void OutlineModel::Reserve(int v, int i) {
180 void OutlineModel::Update() {
181 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_VERTEX]);
182 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
183 glEnableVertexAttribArray(ATTRIB_VERTEX);
184 glVertexAttribPointer(
185 ATTRIB_VERTEX, // location (for shader)
188 GL_FALSE, // normalized
194 if (colors.size() < vertices.size()) {
195 std::cerr << "OutlineModel: not enough colors!" << std::endl;
196 colors.resize(vertices.size(), { 1, 0, 1 });
199 glBindBuffer(GL_ARRAY_BUFFER, handle[ATTRIB_COLOR]);
200 glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), colors.data(), GL_STATIC_DRAW);
201 glEnableVertexAttribArray(ATTRIB_COLOR);
202 glVertexAttribPointer(
203 ATTRIB_COLOR, // location (for shader)
206 GL_FALSE, // normalized
211 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
212 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(Index), indices.data(), GL_STATIC_DRAW);
218 void OutlineModel::Draw() {
219 glBindVertexArray(va);
225 glEnable(GL_LINE_SMOOTH);
228 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, handle[ATTRIB_INDEX]);
231 indices.size(), // count
232 GL_UNSIGNED_SHORT, // type