3 #include <gmath/gmath.h>
23 MeshGL::MeshGL(const MeshGL &mesh)
25 indices = mesh.indices;
26 vertices = mesh.vertices;
27 normals = mesh.normals;
28 tex_coords = mesh.tex_coords;
40 * if we set these to the actual
41 * vertices.size() and indices.size()
42 * update_vbo will have no effect
49 MeshGL &MeshGL::operator=(const MeshGL &mesh)
54 /* to avoid OpenGL leaks */
57 /* what the copy constructor does */
58 indices = mesh.indices;
59 vertices = mesh.vertices;
60 normals = mesh.normals;
61 tex_coords = mesh.tex_coords;
76 void MeshGL::draw() const
79 ((MeshGL *)this)->update_vertex_data();
81 glBindVertexArray(vao);
83 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
84 glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, 0);
86 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
90 void MeshGL::draw_normals(float scale) const
93 glGenVertexArrays(1, &nvao);
94 glBindVertexArray(nvao);
96 glGenBuffers(1, &nvbo);
97 glBindBuffer(GL_ARRAY_BUFFER, nvbo);
99 glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(Vec3) * 2, 0, GL_STATIC_DRAW);
100 Vec3 *data = (Vec3 *)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
102 for(size_t i = 0; i < normals.size(); i++) {
103 *data++ = vertices[i];
104 *data++ = vertices[i] + normals[i] * scale;
106 glUnmapBuffer(GL_ARRAY_BUFFER);
108 glVertexAttribPointer(MESH_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
109 glEnableVertexAttribArray(MESH_VERTEX);
110 glBindBuffer(GL_ARRAY_BUFFER, 0);
113 glBindVertexArray(nvao);
116 glDrawArrays(GL_LINES, 0, normals.size() * 2);
117 glBindVertexArray(0);
120 bool MeshGL::update_vertex_data()
126 void MeshGL::update_vbo()
128 if(vertices.empty()) {
129 printf("empty vertices\n");
135 glGenVertexArrays(1, &vao);
136 glBindVertexArray(vao);
141 glGenBuffers(1, &vbo_vertices);
142 glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
143 if(num_vertices != (int)vertices.size())
144 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vec3),
145 &vertices[0], GL_STATIC_DRAW);
147 glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vec3),
149 glVertexAttribPointer(MESH_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
154 glGenBuffers(1, &vbo_normals);
155 glBindBuffer(GL_ARRAY_BUFFER, vbo_normals);
156 if(num_vertices != (int)normals.size())
157 glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(Vec3),
158 &normals[0], GL_STATIC_DRAW);
160 glBufferSubData(GL_ARRAY_BUFFER, 0, normals.size() * sizeof(Vec3),
162 glVertexAttribPointer(MESH_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
164 /* texture coordinates */
167 glGenBuffers(1, &vbo_tex_coords);
168 glBindBuffer(GL_ARRAY_BUFFER, vbo_tex_coords);
169 if(num_vertices != (int)tex_coords.size())
170 glBufferData(GL_ARRAY_BUFFER, tex_coords.size() * sizeof(Vec2), &tex_coords[0], GL_STATIC_DRAW);
172 glBufferSubData(GL_ARRAY_BUFFER, 0, tex_coords.size() * sizeof(Vec2), &tex_coords[0]);
173 glVertexAttribPointer(MESH_TEXTURE, 2, GL_FLOAT, GL_FALSE, sizeof(Vec2), 0);
175 num_vertices = vertices.size();
180 glGenBuffers(1, &ibo);
181 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
182 if(num_indices != (int)indices.size())
183 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * 2,
184 &indices[0], GL_STATIC_DRAW);
186 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indices.size() * 2,
189 num_indices = indices.size();
191 glEnableVertexAttribArray(MESH_VERTEX);
192 glEnableVertexAttribArray(MESH_NORMAL);
193 glEnableVertexAttribArray(MESH_TEXTURE);
195 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
196 glBindBuffer(GL_ARRAY_BUFFER, 0);
197 glBindVertexArray(0);
200 void MeshGL::destroy_vbo()
203 glDeleteBuffers(1, &vbo_vertices);
205 glDeleteBuffers(1, &vbo_normals);
207 glDeleteBuffers(1, &vbo_tex_coords);
209 glDeleteBuffers(1, &ibo);
211 glDeleteVertexArrays(1, &vao);
214 glDeleteBuffers(1, &nvbo);
216 glDeleteVertexArrays(1, &nvao);