X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fmesh.cpp;h=45c550a0333ed4244de41b976cca39f8f4d905fa;hb=126ac7ca4053b765eab1473d2865bce728ef2428;hp=413986f463d60180d50f8b0d37982256e7c43ac5;hpb=8b99547d099c96bd8cc6037e2026db123b2523c1;p=libs%2Fgl.git diff --git a/source/mesh.cpp b/source/mesh.cpp index 413986f4..45c550a0 100644 --- a/source/mesh.cpp +++ b/source/mesh.cpp @@ -1,6 +1,7 @@ #include #include #include "buffer.h" +#include "error.h" #include "mesh.h" #include "renderer.h" @@ -15,7 +16,7 @@ Mesh::Mesh(): ibuf(0), vao_id(0), defer_buffers(true), - dirty(0), + dirty(false), winding(0) { } @@ -25,7 +26,7 @@ Mesh::Mesh(const VertexFormat &f): ibuf(0), vao_id(0), defer_buffers(true), - dirty(2), + dirty(true), winding(0) { } @@ -73,49 +74,29 @@ void Mesh::create_buffers() glGenVertexArrays(1, &vao_id); } -void Mesh::refresh() const +void Mesh::setup_vao() const { - vertices.refresh(); - if(dirty) + Bind bind_vbuf(vbuf, ARRAY_BUFFER); + + const VertexFormat &fmt = vertices.get_format(); + unsigned stride = get_stride(fmt)*sizeof(float); + float *ptr = 0; + for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c) { - if(dirty&1) - { - for(list::const_iterator i=batches.begin(); i!=batches.end(); ++i) - i->refresh(); - } - - if(dirty&2) - { - Bind bind_vbuf(vbuf, ARRAY_BUFFER); - - bool bind_here = !current(); - if(bind_here) - glBindVertexArray(vao_id); - - const VertexFormat &fmt = vertices.get_format(); - unsigned stride = get_stride(fmt)*sizeof(float); - float *ptr = 0; - for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c) - { - unsigned t = get_component_type(*c); - if(t>=get_component_type(ATTRIB1)) - t -= get_component_type(ATTRIB1); - unsigned sz = get_component_size(*c); - if(*c==COLOR4_UBYTE) - glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, ptr); - else - glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, ptr); - glEnableVertexAttribArray(t); - ptr += sz; - } - glBindBuffer(ELEMENT_ARRAY_BUFFER, ibuf->get_id()); - - if(bind_here) - glBindVertexArray(0); - } - - dirty = 0; + unsigned t = get_component_type(*c); + if(t>=get_component_type(ATTRIB1)) + t -= get_component_type(ATTRIB1); + unsigned sz = get_component_size(*c); + if(*c==COLOR4_UBYTE) + glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, ptr); + else + glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, ptr); + glEnableVertexAttribArray(t); + ptr += sz; } + glBindBuffer(ELEMENT_ARRAY_BUFFER, ibuf->get_id()); + + dirty = false; } unsigned Mesh::get_n_vertices() const @@ -152,28 +133,21 @@ void Mesh::set_winding(const WindingTest *w) void Mesh::draw() const { - refresh(); + const Mesh *cur = current(); + if(cur && cur!=this) + throw invalid_operation("Mesh::draw"); if(!current()) - { vertices.apply(); - if(ibuf) - ibuf->bind_to(ELEMENT_ARRAY_BUFFER); - } - + Bind bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER); Bind bind_winding(winding); for(list::const_iterator i=batches.begin(); i!=batches.end(); ++i) i->draw(); - - if(!current() && ibuf) - Buffer::unbind_from(ELEMENT_ARRAY_BUFFER); } void Mesh::draw(Renderer &renderer) const { - refresh(); - renderer.set_mesh(this); renderer.set_element_buffer(ibuf); renderer.set_winding_test(winding); @@ -189,7 +163,12 @@ void Mesh::bind() const if(!vao_id) unbind(); else if(set_current(this)) + { glBindVertexArray(vao_id); + vertices.refresh(); + if(dirty) + setup_vao(); + } } void Mesh::unbind() @@ -216,7 +195,7 @@ void Mesh::Loader::vertices(const vector &c) for(vector::const_iterator i=c.begin(); i!=c.end(); ++i) fmt = (fmt, *i); obj.vertices.reset(fmt); - obj.dirty |= 2; + obj.dirty = true; load_sub(obj.vertices); }