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;
32 * if we set these to the actual
33 * vertices.size() and indices.size()
34 * update_vbo will have no effect
41 MeshGL &MeshGL::operator=(const MeshGL &mesh)
46 /* to avoid OpenGL leaks */
49 /* what the copy constructor does */
50 indices = mesh.indices;
51 vertices = mesh.vertices;
52 normals = mesh.normals;
53 tex_coords = mesh.tex_coords;
66 void MeshGL::draw() const
68 glBindVertexArray(vao);
70 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
71 glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, 0);
72 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
77 void MeshGL::update_vertex_data()
82 void MeshGL::update_vbo()
86 glGenVertexArrays(1, &vao);
87 glBindVertexArray(vao);
92 glGenBuffers(1, &vbo_vertices);
93 glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
94 if(num_vertices != (int)vertices.size())
95 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float) * 3,
96 &vertices, GL_STATIC_DRAW);
98 glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(float) * 3,
100 num_vertices = vertices.size();
101 glBindBuffer(GL_ARRAY_BUFFER, 0);
106 glGenBuffers(1, &vbo_normals);
107 glBindBuffer(GL_ARRAY_BUFFER, vbo_normals);
108 if(num_vertices != (int)normals.size())
109 glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(float) * 3,
110 &normals, GL_STATIC_DRAW);
112 glBufferSubData(GL_ARRAY_BUFFER, 0, normals.size() * sizeof(float) * 3,
114 glBindBuffer(GL_ARRAY_BUFFER, 0);
116 /* texture coordinates */
119 glGenBuffers(1, &vbo_tex_coords);
120 if(num_vertices != (int)tex_coords.size())
121 glBufferData(GL_ARRAY_BUFFER, tex_coords.size() * sizeof(float) * 2, &tex_coords, GL_STATIC_DRAW);
123 glBufferSubData(GL_ARRAY_BUFFER, 0, tex_coords.size() * sizeof(float) * 2, &tex_coords);
124 glBindBuffer(GL_ARRAY_BUFFER, 0);
129 glGenBuffers(1, &ibo);
130 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
131 if(num_indices != (int)indices.size())
132 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * 2,
133 &indices[0], GL_STATIC_DRAW);
135 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indices.size() * 2,
137 num_indices = indices.size();
138 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
140 glEnableVertexAttribArray(MESH_VERTEX);
141 glEnableVertexAttribArray(MESH_NORMAL);
142 glEnableVertexAttribArray(MESH_TEXTURE);
144 glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
145 glVertexAttribPointer(MESH_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
147 glBindBuffer(GL_ARRAY_BUFFER, vbo_normals);
148 glVertexAttribPointer(MESH_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
150 glBindBuffer(GL_ARRAY_BUFFER, vbo_tex_coords);
151 glVertexAttribPointer(MESH_TEXTURE, 2, GL_FLOAT, GL_FALSE, sizeof(Vec2), 0);
153 glBindVertexArray(0);
156 void MeshGL::destroy_vbo()
159 glDeleteBuffers(1, &vbo_vertices);
161 glDeleteBuffers(1, &vbo_normals);
163 glDeleteBuffers(1, &vbo_tex_coords);
166 glDeleteVertexArrays(1, &vao);