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. */
Batch::~Batch()
{
- unlink_from_ibuf();
}
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())
else
data.push_back(i);
- update_ibuf_offsets();
+ update_offset();
dirty = true;
return *this;
for(unsigned i=0; i<ind.size(); ++i)
data[base+i] = ind[i];
}
+ dirty = true;
}
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)
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]);
#include <vector>
#include <msp/datafile/objectloader.h>
+#include "bufferable.h"
#include "datatype.h"
#include "primitivetype.h"
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>
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;
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(); }