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
69 glBindVertexArray(vao);
71 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
72 glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, 0);
74 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
78 void MeshGL::update_vertex_data()
83 void MeshGL::update_vbo()
85 if(vertices.empty()) {
86 printf("empty vertices\n");
92 glGenVertexArrays(1, &vao);
93 glBindVertexArray(vao);
98 glGenBuffers(1, &vbo_vertices);
99 glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
100 if(num_vertices != (int)vertices.size())
101 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vec3),
102 &vertices[0], GL_STATIC_DRAW);
104 glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vec3),
106 glVertexAttribPointer(MESH_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
111 glGenBuffers(1, &vbo_normals);
112 glBindBuffer(GL_ARRAY_BUFFER, vbo_normals);
113 if(num_vertices != (int)normals.size())
114 glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(Vec3),
115 &normals[0], GL_STATIC_DRAW);
117 glBufferSubData(GL_ARRAY_BUFFER, 0, normals.size() * sizeof(Vec3),
119 glVertexAttribPointer(MESH_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
121 /* texture coordinates */
124 glGenBuffers(1, &vbo_tex_coords);
125 glBindBuffer(GL_ARRAY_BUFFER, vbo_tex_coords);
126 if(num_vertices != (int)tex_coords.size())
127 glBufferData(GL_ARRAY_BUFFER, tex_coords.size() * sizeof(Vec2), &tex_coords[0], GL_STATIC_DRAW);
129 glBufferSubData(GL_ARRAY_BUFFER, 0, tex_coords.size() * sizeof(Vec2), &tex_coords[0]);
130 glVertexAttribPointer(MESH_TEXTURE, 2, GL_FLOAT, GL_FALSE, sizeof(Vec2), 0);
132 num_vertices = vertices.size();
137 glGenBuffers(1, &ibo);
138 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
139 if(num_indices != (int)indices.size())
140 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * 2,
141 &indices[0], GL_STATIC_DRAW);
143 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indices.size() * 2,
146 num_indices = indices.size();
148 glEnableVertexAttribArray(MESH_VERTEX);
149 glEnableVertexAttribArray(MESH_NORMAL);
150 glEnableVertexAttribArray(MESH_TEXTURE);
152 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
153 glBindBuffer(GL_ARRAY_BUFFER, 0);
154 glBindVertexArray(0);
157 void MeshGL::destroy_vbo()
160 glDeleteBuffers(1, &vbo_vertices);
162 glDeleteBuffers(1, &vbo_normals);
164 glDeleteBuffers(1, &vbo_tex_coords);
167 glDeleteVertexArrays(1, &vao);