]> git.tdb.fi Git - libs/gl.git/commitdiff
Make use of Bufferable in Batch
authorMikko Rasa <tdb@tdb.fi>
Thu, 30 Aug 2012 19:42:52 +0000 (22:42 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 30 Aug 2012 19:42:52 +0000 (22:42 +0300)
source/batch.cpp
source/batch.h
source/mesh.cpp

index 1d9b1d4cd4ab7081a3bf95522056f61b91375217..785fca826ae30e5c9c524b0a2f08a242a2aad15f 100644 (file)
@@ -56,12 +56,7 @@ Batch::Batch(PrimitiveType t):
        data_type(UNSIGNED_BYTE),
        min_index(0),
        max_index(0),
-       restart(false),
-       ibuf(0),
-       ibuf_offset(0),
-       next_in_ibuf(0),
-       prev_in_ibuf(0),
-       dirty(false)
+       restart(false)
 {
        /* XXX Should probably provide a fallback to glDrawElements since this class
        is pretty much required to render anything. */
@@ -70,7 +65,6 @@ Batch::Batch(PrimitiveType t):
 
 Batch::~Batch()
 {
-       unlink_from_ibuf();
 }
 
 void Batch::set_data_type(DataType t)
@@ -96,57 +90,10 @@ void Batch::set_data_type(DataType t)
                shrink<unsigned short, unsigned char>(data);
 
        data_type = t;
-       update_ibuf_offsets();
+       update_offset();
        dirty = true;
 }
 
-void Batch::use_index_buffer(Buffer *buf, Batch *prev)
-{
-       if(buf && prev && prev->ibuf!=buf)
-               throw invalid_argument("Batch::use_index_buffer");
-
-       if(!buf)
-       {
-               prev = 0;
-               unlink_from_ibuf();
-       }
-
-       ibuf = buf;
-       prev_in_ibuf = prev;
-       next_in_ibuf = 0;
-       if(prev)
-       {
-               prev->next_in_ibuf = this;
-               ibuf_offset = prev->ibuf_offset+prev->data.size();
-       }
-       else
-               ibuf_offset = 0;
-
-       dirty = true;
-}
-
-void Batch::unlink_from_ibuf()
-{
-       if(next_in_ibuf)
-               next_in_ibuf->prev_in_ibuf = prev_in_ibuf;
-       if(prev_in_ibuf)
-       {
-               prev_in_ibuf->next_in_ibuf = next_in_ibuf;
-               prev_in_ibuf->update_ibuf_offsets();
-       }
-       else if(next_in_ibuf)
-       {
-               next_in_ibuf->ibuf_offset = 0;
-               next_in_ibuf->update_ibuf_offsets();
-       }
-}
-
-void Batch::update_ibuf_offsets()
-{
-       for(Batch *b=this; b->next_in_ibuf; b=b->next_in_ibuf)
-               b->next_in_ibuf->ibuf_offset = b->ibuf_offset+b->data.size();
-}
-
 Batch &Batch::append(unsigned i)
 {
        if(data.empty())
@@ -169,7 +116,7 @@ Batch &Batch::append(unsigned i)
        else
                data.push_back(i);
        
-       update_ibuf_offsets();
+       update_offset();
        dirty = true;
 
        return *this;
@@ -213,6 +160,7 @@ void Batch::append(const vector<unsigned> &ind)
                for(unsigned i=0; i<ind.size(); ++i)
                        data[base+i] = ind[i];
        }
+       dirty = true;
 }
 
 void Batch::append(const Batch &other)
@@ -259,6 +207,11 @@ void Batch::append(const Batch &other)
                append(other.get_index(i));
 }
 
+void Batch::upload_data() const
+{
+       get_buffer()->sub_data(get_offset(), data.size(), &data[0]);
+}
+
 unsigned Batch::get_index_size() const
 {
        if(data_type==UNSIGNED_SHORT)
@@ -304,30 +257,15 @@ void Batch::draw() const
                restart_index = 0;
        }
 
-       if(ibuf)
+       if(get_buffer())
        {
                if(dirty)
-               {
-                       const Batch *b = this;
-                       for(; b->prev_in_ibuf; b=b->prev_in_ibuf) ;
-
-                       unsigned chain_size = 0;
-                       for(const Batch *a=b; a; a=a->next_in_ibuf)
-                               chain_size += a->data.size();
-
-                       ibuf->data(chain_size, 0);
-
-                       for(; b; b=b->next_in_ibuf)
-                       {
-                               ibuf->sub_data(b->ibuf_offset, b->data.size(), &b->data[0]);
-                               b->dirty = false;
-                       }
-               }
+                       update_buffer();
 
-               BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*ibuf);
+               BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*get_buffer());
                Bind bind_ibuf(alias, true);
 
-               glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast<void *>(ibuf_offset));
+               glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast<void *>(get_offset()));
        }
        else
                glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
index dcc2168b312f7c6323442b7e2b52296639156ff7..3c54089a1a449e07ff91c7f1f09774c4c57c276a 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <vector>
 #include <msp/datafile/objectloader.h>
+#include "bufferable.h"
 #include "datatype.h"
 #include "primitivetype.h"
 
@@ -19,7 +20,7 @@ the Batch.
 This is a pretty low-level class and mainly intended to be used by the Mesh
 class.
 */
-class Batch
+class Batch: public Bufferable
 {
 public:
        class Loader: public DataFile::ObjectLoader<Batch>
@@ -37,11 +38,6 @@ private:
        unsigned min_index;
        unsigned max_index;
        bool restart;
-       Buffer *ibuf;
-       unsigned ibuf_offset;
-       Batch *next_in_ibuf;
-       Batch *prev_in_ibuf;
-       mutable bool dirty;
 
        static unsigned restart_index;
 
@@ -52,16 +48,13 @@ public:
        PrimitiveType get_type() const { return prim_type; }
        void set_data_type(DataType);
        DataType get_data_type() const { return data_type; }
-       void use_index_buffer(Buffer *, Batch * = 0);
-private:
-       void unlink_from_ibuf();
-       void update_ibuf_offsets();
 
-public:
        Batch &append(unsigned);
        void append(const std::vector<unsigned> &);
        void append(const Batch &);
 private:
+       virtual unsigned get_data_size() const { return data.size(); }
+       virtual void upload_data() const;
        unsigned get_index_size() const;
 public:
        unsigned size() const { return data.size()/get_index_size(); }
index 9757df5cc9e20f364ff96880f115b1707a1f8f5e..0e58aa269a1bbdb7679aa36737f0a79ecdb95663 100644 (file)
@@ -85,7 +85,7 @@ void Mesh::add_batch(const Batch &b)
                Batch *prev = (batches.empty() ? 0 : &batches.back());
                batches.push_back(b);
                if(ibuf)
-                       batches.back().use_index_buffer(ibuf, prev);
+                       batches.back().use_buffer(ibuf, prev);
        }
 }