X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fopengl%2Fmesh-gl.cc;h=531ac7888181513e6d7ff348ff4cf976590df53e;hb=d90ed8aef9e3547eee75ad793c352ee022d35050;hp=119d043b884e1969e4e2c3e9c0c031764a79fb80;hpb=63d7f3b0e70ab5e3d530c579b1881967c96b0b92;p=demo diff --git a/src/opengl/mesh-gl.cc b/src/opengl/mesh-gl.cc index 119d043..531ac78 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() @@ -62,10 +69,15 @@ MeshGL::~MeshGL() vertices.clear(); normals.clear(); + tex_coords.clear(); + indices.clear(); } void MeshGL::draw() const { + if(!vdata_valid) { + ((MeshGL *)this)->update_vertex_data(); + } glBindVertexArray(vao); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); @@ -75,9 +87,40 @@ void MeshGL::draw() const glBindVertexArray(0); } -void MeshGL::update_vertex_data() +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); +} + +bool MeshGL::update_vertex_data() { update_vbo(); + return true; } void MeshGL::update_vbo() @@ -163,6 +206,12 @@ void MeshGL::destroy_vbo() if(vbo_tex_coords) glDeleteBuffers(1, &vbo_tex_coords); if(ibo) - if(vao) - glDeleteVertexArrays(1, &vao); -} \ No newline at end of file + glDeleteBuffers(1, &ibo); + if(vao) + glDeleteVertexArrays(1, &vao); + + if(nvbo) + glDeleteBuffers(1, &nvbo); + if(nvao) + glDeleteVertexArrays(1, &nvao); +}