2 #include <gmath/gmath.h>
19 MeshGL::MeshGL(const MeshGL &mesh)
21 indices = mesh.indices;
22 vertices = mesh.vertices;
23 normals = mesh.normals;
24 tex_coords = mesh.tex_coords;
33 * if we set these to the actual
34 * vertices.size() and indices.size()
35 * update_vbo will have no effect
42 MeshGL &MeshGL::operator=(const MeshGL &mesh)
47 /* to avoid OpenGL leaks */
50 /* what the copy constructor does */
51 indices = mesh.indices;
52 vertices = mesh.vertices;
53 normals = mesh.normals;
54 tex_coords = mesh.tex_coords;
67 void MeshGL::draw() const
72 glBindVertexArray(vao);
74 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
75 glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, 0);
76 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
81 void MeshGL::update_vertex_data()
86 void MeshGL::update_vbo()
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);
136 glGenBuffers(1, &ibo);
137 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
138 if(num_indices != (int)indices.size())
139 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * 2,
140 &indices[0], GL_STATIC_DRAW);
142 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indices.size() * 2,
145 num_indices = indices.size();
146 num_vertices = vertices.size();
148 glEnableVertexAttribArray(MESH_VERTEX);
149 glEnableVertexAttribArray(MESH_NORMAL);
150 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);