From 7d6b7b18221e670810bdd0505995b8f1fa012eb4 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 30 Aug 2012 22:42:52 +0300 Subject: [PATCH] Make use of Bufferable in Batch --- source/batch.cpp | 88 +++++++----------------------------------------- source/batch.h | 15 +++------ source/mesh.cpp | 2 +- 3 files changed, 18 insertions(+), 87 deletions(-) diff --git a/source/batch.cpp b/source/batch.cpp index 1d9b1d4c..785fca82 100644 --- a/source/batch.cpp +++ b/source/batch.cpp @@ -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(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 &ind) for(unsigned i=0; isub_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 alias(*ibuf); + BufferAlias alias(*get_buffer()); Bind bind_ibuf(alias, true); - glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast(ibuf_offset)); + glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast(get_offset())); } else glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]); diff --git a/source/batch.h b/source/batch.h index dcc2168b..3c54089a 100644 --- a/source/batch.h +++ b/source/batch.h @@ -3,6 +3,7 @@ #include #include +#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 @@ -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 &); 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(); } diff --git a/source/mesh.cpp b/source/mesh.cpp index 9757df5c..0e58aa26 100644 --- a/source/mesh.cpp +++ b/source/mesh.cpp @@ -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); } } -- 2.45.2