]> git.tdb.fi Git - libs/gl.git/blobdiff - source/mesh.cpp
Add vertex array object support to Mesh
[libs/gl.git] / source / mesh.cpp
index 2ca040e6feb68e3eb65239a4c05e4eadfd997053..d474a27f116cc0c3e7175f7a64488120d4e83805 100644 (file)
@@ -1,12 +1,8 @@
-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
+#include <msp/gl/extensions/arb_vertex_array_object.h>
+#include <msp/gl/extensions/arb_vertex_shader.h>
 #include "buffer.h"
 #include "mesh.h"
+#include "renderer.h"
 
 using namespace std;
 
@@ -15,106 +11,231 @@ namespace GL {
 
 Mesh::Mesh():
        vertices(VERTEX3),
-       ibuf(0)
+       vbuf(0),
+       ibuf(0),
+       vao_id(0),
+       defer_buffers(true),
+       dirty(0),
+       winding(0)
 { }
 
 Mesh::Mesh(const VertexFormat &f):
        vertices(f),
-       ibuf(0)
+       vbuf(0),
+       ibuf(0),
+       vao_id(0),
+       defer_buffers(true),
+       dirty(2),
+       winding(0)
 { }
 
-void Mesh::use_vertex_buffer(bool b)
+Mesh::~Mesh()
 {
+       delete vbuf;
+       delete ibuf;
+       if(vao_id)
+               glDeleteVertexArrays(1, &vao_id);
+}
+
+void Mesh::clear()
+{
+       vertices.clear();
+       batches.clear();
+}
+
+void Mesh::use_buffers(bool b)
+{
+       defer_buffers = false;
        if(b)
-       {
-               vertices.use_vertex_buffer();
-               if(!ibuf)
-                       ibuf=new Buffer(ELEMENT_ARRAY_BUFFER);
-               update_index_buffer();
-       }
+               create_buffers();
        else
        {
-               vertices.use_vertex_buffer(0);
+               vertices.use_buffer(0);
+               delete vbuf;
+               vbuf = 0;
                delete ibuf;
-               ibuf=0;
+               ibuf = 0;
        }
 }
 
-float *Mesh::get_vertex(unsigned i)
+void Mesh::create_buffers()
 {
-       return vertices[i];
-}
+       defer_buffers = false;
 
-void Mesh::add_batch(const Batch &b)
-{
-       batches.push_back(b);
-       update_index_buffer();
-}
+       if(!vbuf)
+               vbuf = new Buffer(ARRAY_BUFFER);
+       vertices.use_buffer(vbuf);
 
-void Mesh::clear()
-{
-       vertices.clear();
-       batches.clear();
+       if(!ibuf)
+               ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
+
+       if(ARB_vertex_array_object && !vao_id)
+               glGenVertexArrays(1, &vao_id);
 }
 
-void Mesh::draw() const
+void Mesh::refresh() const
 {
-       vertices.apply();
-       if(ibuf)
+       vertices.refresh();
+       if(dirty)
        {
-               ibuf->bind();
-               unsigned offset=0;
-               for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
+               if(dirty&1)
                {
-                       i->draw_with_buffer(offset);
-                       offset+=i->size();
+                       unbind();
+                       for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
+                               i->refresh();
                }
-               ibuf->unbind();
+
+               if(dirty&2)
+               {
+                       glBindVertexArray(vao_id);
+                       BufferAlias<ARRAY_BUFFER> vbuf_alias(*vbuf);
+                       Bind bind_vbuf(vbuf_alias);
+
+                       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());
+                       glBindVertexArray(0);
+               }
+
+               dirty = 0;
        }
+}
+
+unsigned Mesh::get_n_vertices() const
+{
+       return vertices.size();
+}
+
+float *Mesh::modify_vertex(unsigned i)
+{
+       return vertices.modify(i);
+}
+
+void Mesh::add_batch(const Batch &b)
+{
+       if(defer_buffers)
+               create_buffers();
+
+       dirty |= 1;
+       if(!batches.empty() && batches.back().can_append(b.get_type()))
+               batches.back().append(b);
        else
        {
-               for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
-                       i->draw();
+               Batch *prev = (batches.empty() ? 0 : &batches.back());
+               batches.push_back(b);
+               if(ibuf)
+                       batches.back().use_buffer(ibuf, prev);
        }
 }
 
-void Mesh::update_index_buffer()
+void Mesh::set_winding(const WindingTest *w)
 {
-       if(!ibuf)
-               return;
+       winding = w;
+}
+
+void Mesh::draw() const
+{
+       refresh();
+
+       if(!current())
+       {
+               vertices.apply();
+               if(ibuf)
+                       ibuf->bind_to(ELEMENT_ARRAY_BUFFER);
+       }
+
+       Bind bind_winding(winding);
 
-       unsigned total=0;
        for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
-               total+=i->size();
+               i->draw();
+
+       if(!current() && ibuf)
+               Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
+}
+
+void Mesh::draw(Renderer &renderer) const
+{
+       vertices.apply();
+       renderer.set_element_buffer(ibuf);
+       renderer.set_winding_test(winding);
 
-       ibuf->data(total*sizeof(unsigned), 0);
-       unsigned offset=0;
        for(list<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
+               renderer.draw(*i);
+}
+
+void Mesh::bind() const
+{
+       /* If VAOs are not supported, vao_id is zero and set_current won't get
+       called.  Thus unbind won't try to call a null function either. */
+       if(!vao_id)
+               unbind();
+       else
        {
-               ibuf->sub_data(offset*sizeof(unsigned), i->size()*sizeof(unsigned), &i->get_indices()[0]);
-               offset+=i->size();
+               if(Buffer::current(ELEMENT_ARRAY_BUFFER))
+               {
+                       unbind();
+                       Buffer::unbind_from(ELEMENT_ARRAY_BUFFER);
+               }
+               if(set_current(this))
+                       glBindVertexArray(vao_id);
        }
-       ibuf->unbind();
+}
+
+void Mesh::unbind()
+{
+       if(set_current(0))
+               glBindVertexArray(0);
 }
 
 
 Mesh::Loader::Loader(Mesh &m):
        DataFile::ObjectLoader<Mesh>(m)
 {
-       add("vertices", &Loader::vertices);
        add("batch",    &Loader::batch);
+       add("vertices", &Loader::vertices);
+       add("winding",  &Loader::winding);
 }
 
-void Mesh::Loader::vertices(VertexFormat f)
+void Mesh::Loader::vertices(const vector<VertexComponent> &c)
 {
-       obj.vertices.reset(f);
+       if(c.empty())
+               throw invalid_argument("No vertex components");
+
+       VertexFormat fmt;
+       for(vector<VertexComponent>::const_iterator i=c.begin(); i!=c.end(); ++i)
+               fmt = (fmt, *i);
+       obj.vertices.reset(fmt);
+       obj.dirty |= 2;
        load_sub(obj.vertices);
 }
 
 void Mesh::Loader::batch(PrimitiveType p)
 {
-       obj.batches.push_back(Batch(p));
-       load_sub(obj.batches.back());
+       Batch btc(p);
+       load_sub(btc);
+       obj.add_batch(btc);
+}
+
+void Mesh::Loader::winding(FaceWinding w)
+{
+       if(w==CLOCKWISE)
+               obj.winding = &WindingTest::clockwise();
+       else if(w==COUNTERCLOCKWISE)
+               obj.winding = &WindingTest::counterclockwise();
 }
 
 } // namespace GL