X-Git-Url: https://eleni.mutantstargoat.com/git/?a=blobdiff_plain;f=src%2Fopengl%2Fmesh-gl.cc;h=dd73bf386f8d1d13e31df73a8680da236ac20fbb;hb=47982b199010496e34eefb95044275fb231cba18;hp=42c2af9251e9dac300fe228188abf6206b2a932a;hpb=4bc86b416f29b4889075ad5c8dfdb1e11454a6c3;p=demo diff --git a/src/opengl/mesh-gl.cc b/src/opengl/mesh-gl.cc index 42c2af9..dd73bf3 100644 --- a/src/opengl/mesh-gl.cc +++ b/src/opengl/mesh-gl.cc @@ -1,4 +1,5 @@ #include +#include #include #include "mesh-gl.h" @@ -14,6 +15,9 @@ MeshGL::MeshGL() num_vertices = 0; num_indices = 0; + + /* draw normals */ + nvao = nvbo = 0; } MeshGL::MeshGL(const MeshGL &mesh) @@ -29,6 +33,9 @@ MeshGL::MeshGL(const MeshGL &mesh) ibo = 0; vao = 0; + /* draw normals */ + nvao = nvbo = 0; + /* * if we set these to the actual * vertices.size() and indices.size() @@ -66,15 +73,45 @@ MeshGL::~MeshGL() void MeshGL::draw() const { - if(!vao) - return; - + if(!vdata_valid) { + ((MeshGL*)this)->update_vertex_data(); + } glBindVertexArray(vao); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); - glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_INT, 0); + glDrawElements(GL_TRIANGLES, num_indices, GL_UNSIGNED_SHORT, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + glBindVertexArray(0); +} +void MeshGL::draw_normals(float scale) const +{ + if(!nvao) { + glGenVertexArrays(1, &nvao); + glBindVertexArray(nvao); + + glGenBuffers(1, &nvbo); + glBindBuffer(GL_ARRAY_BUFFER, nvbo); + + glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(Vec3) * 2, 0, GL_STATIC_DRAW); + Vec3 *data = (Vec3 *)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); + + for (size_t i = 0; i < normals.size(); i++) + { + *data++ = vertices[i]; + *data++ = vertices[i] + normals[i] * scale; + } + glUnmapBuffer(GL_ARRAY_BUFFER); + + glVertexAttribPointer(MESH_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0); + glEnableVertexAttribArray(MESH_VERTEX); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } else { + glBindVertexArray(nvao); + } + + glDrawArrays(GL_LINES, 0, normals.size() * 2); glBindVertexArray(0); } @@ -85,8 +122,10 @@ void MeshGL::update_vertex_data() void MeshGL::update_vbo() { - if(!num_vertices) + if(vertices.empty()) { + printf("empty vertices\n"); return; + } /* vao */ if(!vao) @@ -130,6 +169,8 @@ void MeshGL::update_vbo() glBufferSubData(GL_ARRAY_BUFFER, 0, tex_coords.size() * sizeof(Vec2), &tex_coords[0]); glVertexAttribPointer(MESH_TEXTURE, 2, GL_FLOAT, GL_FALSE, sizeof(Vec2), 0); + num_vertices = vertices.size(); + /* indices */ if(!ibo) @@ -143,13 +184,11 @@ void MeshGL::update_vbo() &indices[0]); num_indices = indices.size(); - num_vertices = vertices.size(); glEnableVertexAttribArray(MESH_VERTEX); glEnableVertexAttribArray(MESH_NORMAL); glEnableVertexAttribArray(MESH_TEXTURE); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); @@ -164,6 +203,12 @@ void MeshGL::destroy_vbo() if(vbo_tex_coords) glDeleteBuffers(1, &vbo_tex_coords); if(ibo) - if(vao) - glDeleteVertexArrays(1, &vao); + glDeleteBuffers(1, &ibo); + if (vao) + glDeleteVertexArrays(1, &vao); + + if(nvbo) + glDeleteBuffers(1, &nvbo); + if(nvao) + glDeleteVertexArrays(1, &nvao); } \ No newline at end of file