3 #include <gmath/gmath.h>
20 MeshGL::MeshGL(const MeshGL &mesh)
22 indices = mesh.indices;
23 vertices = mesh.vertices;
24 normals = mesh.normals;
25 tex_coords = mesh.tex_coords;
34 * if we set these to the actual
35 * vertices.size() and indices.size()
36 * update_vbo will have no effect
43 MeshGL &MeshGL::operator=(const MeshGL &mesh)
48 /* to avoid OpenGL leaks */
51 /* what the copy constructor does */
52 indices = mesh.indices;
53 vertices = mesh.vertices;
54 normals = mesh.normals;
55 tex_coords = mesh.tex_coords;
68 void MeshGL::draw() const
70 glBindVertexArray(vao);
72 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
73 glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, 0);
75 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
79 void MeshGL::update_vertex_data()
84 void MeshGL::update_vbo()
86 if(vertices.empty()) {
87 printf("empty vertices\n");
93 glGenVertexArrays(1, &vao);
94 glBindVertexArray(vao);
99 glGenBuffers(1, &vbo_vertices);
100 glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
101 if(num_vertices != (int)vertices.size())
102 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vec3),
103 &vertices[0], GL_STATIC_DRAW);
105 glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vec3),
107 glVertexAttribPointer(MESH_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
112 glGenBuffers(1, &vbo_normals);
113 glBindBuffer(GL_ARRAY_BUFFER, vbo_normals);
114 if(num_vertices != (int)normals.size())
115 glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(Vec3),
116 &normals[0], GL_STATIC_DRAW);
118 glBufferSubData(GL_ARRAY_BUFFER, 0, normals.size() * sizeof(Vec3),
120 glVertexAttribPointer(MESH_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
122 /* texture coordinates */
125 glGenBuffers(1, &vbo_tex_coords);
126 glBindBuffer(GL_ARRAY_BUFFER, vbo_tex_coords);
127 if(num_vertices != (int)tex_coords.size())
128 glBufferData(GL_ARRAY_BUFFER, tex_coords.size() * sizeof(Vec2), &tex_coords[0], GL_STATIC_DRAW);
130 glBufferSubData(GL_ARRAY_BUFFER, 0, tex_coords.size() * sizeof(Vec2), &tex_coords[0]);
131 glVertexAttribPointer(MESH_TEXTURE, 2, GL_FLOAT, GL_FALSE, sizeof(Vec2), 0);
133 num_vertices = vertices.size();
138 glGenBuffers(1, &ibo);
139 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
140 if(num_indices != (int)indices.size())
141 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * 2,
142 &indices[0], GL_STATIC_DRAW);
144 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indices.size() * 2,
147 num_indices = indices.size();
149 glEnableVertexAttribArray(MESH_VERTEX);
150 glEnableVertexAttribArray(MESH_NORMAL);
151 glEnableVertexAttribArray(MESH_TEXTURE);
153 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
154 glBindBuffer(GL_ARRAY_BUFFER, 0);
155 glBindVertexArray(0);
158 void MeshGL::destroy_vbo()
161 glDeleteBuffers(1, &vbo_vertices);
163 glDeleteBuffers(1, &vbo_normals);
165 glDeleteBuffers(1, &vbo_tex_coords);
168 glDeleteVertexArrays(1, &vao);