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;
74 void MeshGL::draw() const
77 ((MeshGL*)this)->update_vertex_data();
79 glBindVertexArray(vao);
81 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
82 glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, 0);
84 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
88 void MeshGL::draw_normals(float scale) const
91 glGenVertexArrays(1, &nvao);
92 glBindVertexArray(nvao);
94 glGenBuffers(1, &nvbo);
95 glBindBuffer(GL_ARRAY_BUFFER, nvbo);
97 glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(Vec3) * 2, 0, GL_STATIC_DRAW);
98 Vec3 *data = (Vec3 *)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
100 for (size_t i = 0; i < normals.size(); i++)
102 *data++ = vertices[i];
103 *data++ = vertices[i] + normals[i] * scale;
105 glUnmapBuffer(GL_ARRAY_BUFFER);
107 glVertexAttribPointer(MESH_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
108 glEnableVertexAttribArray(MESH_VERTEX);
109 glBindBuffer(GL_ARRAY_BUFFER, 0);
111 glBindVertexArray(nvao);
114 glDrawArrays(GL_LINES, 0, normals.size() * 2);
115 glBindVertexArray(0);
118 void MeshGL::update_vertex_data()
123 void MeshGL::update_vbo()
125 if(vertices.empty()) {
126 printf("empty vertices\n");
132 glGenVertexArrays(1, &vao);
133 glBindVertexArray(vao);
138 glGenBuffers(1, &vbo_vertices);
139 glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
140 if(num_vertices != (int)vertices.size())
141 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vec3),
142 &vertices[0], GL_STATIC_DRAW);
144 glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vec3),
146 glVertexAttribPointer(MESH_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
151 glGenBuffers(1, &vbo_normals);
152 glBindBuffer(GL_ARRAY_BUFFER, vbo_normals);
153 if(num_vertices != (int)normals.size())
154 glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(Vec3),
155 &normals[0], GL_STATIC_DRAW);
157 glBufferSubData(GL_ARRAY_BUFFER, 0, normals.size() * sizeof(Vec3),
159 glVertexAttribPointer(MESH_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
161 /* texture coordinates */
164 glGenBuffers(1, &vbo_tex_coords);
165 glBindBuffer(GL_ARRAY_BUFFER, vbo_tex_coords);
166 if(num_vertices != (int)tex_coords.size())
167 glBufferData(GL_ARRAY_BUFFER, tex_coords.size() * sizeof(Vec2), &tex_coords[0], GL_STATIC_DRAW);
169 glBufferSubData(GL_ARRAY_BUFFER, 0, tex_coords.size() * sizeof(Vec2), &tex_coords[0]);
170 glVertexAttribPointer(MESH_TEXTURE, 2, GL_FLOAT, GL_FALSE, sizeof(Vec2), 0);
172 num_vertices = vertices.size();
177 glGenBuffers(1, &ibo);
178 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
179 if(num_indices != (int)indices.size())
180 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * 2,
181 &indices[0], GL_STATIC_DRAW);
183 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indices.size() * 2,
186 num_indices = indices.size();
188 glEnableVertexAttribArray(MESH_VERTEX);
189 glEnableVertexAttribArray(MESH_NORMAL);
190 glEnableVertexAttribArray(MESH_TEXTURE);
192 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
193 glBindBuffer(GL_ARRAY_BUFFER, 0);
194 glBindVertexArray(0);
197 void MeshGL::destroy_vbo()
200 glDeleteBuffers(1, &vbo_vertices);
202 glDeleteBuffers(1, &vbo_normals);
204 glDeleteBuffers(1, &vbo_tex_coords);
206 glDeleteBuffers(1, &ibo);
208 glDeleteVertexArrays(1, &vao);
211 glDeleteBuffers(1, &nvbo);
213 glDeleteVertexArrays(1, &nvao);