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
76 glBindVertexArray(vao);
78 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
79 glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, 0);
81 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
85 void MeshGL::draw_normals(float scale) const
88 glGenVertexArrays(1, &nvao);
89 glBindVertexArray(nvao);
91 glGenBuffers(1, &nvbo);
92 glBindBuffer(GL_ARRAY_BUFFER, nvbo);
94 glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(Vec3) * 2, 0, GL_STATIC_DRAW);
95 Vec3 *data = (Vec3 *)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
97 for (size_t i = 0; i < normals.size(); i++)
99 *data++ = vertices[i];
100 *data++ = vertices[i] + normals[i] * scale;
102 glUnmapBuffer(GL_ARRAY_BUFFER);
104 glVertexAttribPointer(MESH_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
105 glEnableVertexAttribArray(MESH_VERTEX);
106 glBindBuffer(GL_ARRAY_BUFFER, 0);
108 glBindVertexArray(nvao);
111 glDrawArrays(GL_LINES, 0, normals.size() * 2);
112 glBindVertexArray(0);
115 void MeshGL::update_vertex_data()
120 void MeshGL::update_vbo()
122 if(vertices.empty()) {
123 printf("empty vertices\n");
129 glGenVertexArrays(1, &vao);
130 glBindVertexArray(vao);
135 glGenBuffers(1, &vbo_vertices);
136 glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
137 if(num_vertices != (int)vertices.size())
138 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vec3),
139 &vertices[0], GL_STATIC_DRAW);
141 glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vec3),
143 glVertexAttribPointer(MESH_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
148 glGenBuffers(1, &vbo_normals);
149 glBindBuffer(GL_ARRAY_BUFFER, vbo_normals);
150 if(num_vertices != (int)normals.size())
151 glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(Vec3),
152 &normals[0], GL_STATIC_DRAW);
154 glBufferSubData(GL_ARRAY_BUFFER, 0, normals.size() * sizeof(Vec3),
156 glVertexAttribPointer(MESH_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
158 /* texture coordinates */
161 glGenBuffers(1, &vbo_tex_coords);
162 glBindBuffer(GL_ARRAY_BUFFER, vbo_tex_coords);
163 if(num_vertices != (int)tex_coords.size())
164 glBufferData(GL_ARRAY_BUFFER, tex_coords.size() * sizeof(Vec2), &tex_coords[0], GL_STATIC_DRAW);
166 glBufferSubData(GL_ARRAY_BUFFER, 0, tex_coords.size() * sizeof(Vec2), &tex_coords[0]);
167 glVertexAttribPointer(MESH_TEXTURE, 2, GL_FLOAT, GL_FALSE, sizeof(Vec2), 0);
169 num_vertices = vertices.size();
174 glGenBuffers(1, &ibo);
175 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
176 if(num_indices != (int)indices.size())
177 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * 2,
178 &indices[0], GL_STATIC_DRAW);
180 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indices.size() * 2,
183 num_indices = indices.size();
185 glEnableVertexAttribArray(MESH_VERTEX);
186 glEnableVertexAttribArray(MESH_NORMAL);
187 glEnableVertexAttribArray(MESH_TEXTURE);
189 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
190 glBindBuffer(GL_ARRAY_BUFFER, 0);
191 glBindVertexArray(0);
194 void MeshGL::destroy_vbo()
197 glDeleteBuffers(1, &vbo_vertices);
199 glDeleteBuffers(1, &vbo_normals);
201 glDeleteBuffers(1, &vbo_tex_coords);
203 glDeleteBuffers(1, &ibo);
205 glDeleteVertexArrays(1, &vao);
208 glDeleteBuffers(1, &nvbo);
210 glDeleteVertexArrays(1, &nvao);