+ if(restart)
+ {
+ unsigned index;
+ if(data_type==UNSIGNED_SHORT)
+ index = 0xFFFF;
+ else if(data_type==UNSIGNED_INT)
+ index = 0xFFFFFFFF;
+ else
+ index = 0xFF;
+
+ if(index!=restart_index)
+ {
+ if(!restart_index)
+ glEnableClientState(GL_PRIMITIVE_RESTART_NV);
+ glPrimitiveRestartIndexNV(index);
+ restart_index = index;
+ }
+ }
+ else if(restart_index && restart_index<max_index)
+ {
+ glDisableClientState(GL_PRIMITIVE_RESTART_NV);
+ restart_index = 0;
+ }
+
+ if(ibuf)
+ {
+ 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;
+ }
+ }
+
+ BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*ibuf);
+ Bind bind_ibuf(alias, true);
+
+ glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, (void *)ibuf_offset);
+ }
+ else
+ glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
+}
+
+unsigned Batch::get_index_size() const
+{
+ if(data_type==UNSIGNED_SHORT)
+ return sizeof(unsigned short);
+ else if(data_type==UNSIGNED_INT)
+ return sizeof(unsigned);
+ return sizeof(unsigned char);
+}
+
+template<typename T>
+void Batch::append_index(T i)
+{
+ data.insert(data.end(), sizeof(T), 0);
+ *(T *)(&data[data.size()-sizeof(T)]) = i;
+}
+
+unsigned Batch::get_index(unsigned i) const
+{
+ if(data_type==UNSIGNED_SHORT)
+ return *(unsigned short *)&data[i*sizeof(unsigned short)];
+ else if(data_type==UNSIGNED_INT)
+ return *(unsigned *)&data[i*sizeof(unsigned )];
+ else
+ return data[i];
+}
+
+template<typename T, typename U>
+void Batch::expand_data()
+{
+ unsigned count = data.size()/sizeof(T);
+ data.resize(count*sizeof(U));
+ for(unsigned i=count; i--;)
+ *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
+}
+
+template<typename T, typename U>
+void Batch::shrink_data()
+{
+ unsigned count = data.size()/sizeof(T);
+ for(unsigned i=0; i<count; ++i)
+ *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
+ data.resize(count*sizeof(U));
+}
+
+template<typename T, typename U>
+U Batch::convert(T i) const
+{
+ if(!static_cast<T>(~i))
+ return ~0;
+ else
+ return i;
+}
+
+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();
+ }