X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbatch.cpp;h=afb9787b4d7f8bd7f34c0491120f668efc3c163b;hb=6afbace895a7bbcf216ab8e48280ea0303ab5892;hp=228ff20bf7a0d395d6fbec42c3cf938d13dbd3fa;hpb=083578c2ca0aabfa60e8872e23d53e9101795dff;p=libs%2Fgl.git diff --git a/source/batch.cpp b/source/batch.cpp index 228ff20b..afb9787b 100644 --- a/source/batch.cpp +++ b/source/batch.cpp @@ -1,14 +1,8 @@ -/* $Id$ - -This file is part of libmspgl -Copyright © 2007-2010 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - #include "batch.h" #include "bindable.h" #include "buffer.h" -#include "extension.h" +#include "error.h" +#include "ext_draw_range_elements.h" #include "nv_primitive_restart.h" #include "vertexarray.h" @@ -30,7 +24,11 @@ Batch::Batch(PrimitiveType t): next_in_ibuf(0), prev_in_ibuf(0), dirty(false) -{ } +{ + /* XXX Should probably provide a fallback to glDrawElements since this class + is pretty much required to render anything. */ + static Require _req(EXT_draw_range_elements); +} Batch::~Batch() { @@ -40,11 +38,11 @@ Batch::~Batch() void Batch::set_data_type(DataType t) { if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT) - throw InvalidParameterValue("Batch data type must be an unsigned integer"); + throw invalid_argument("Batch::set_data_type"); if(t==UNSIGNED_BYTE && max_index>0xFE) - throw InvalidState("UNSIGNED_BYTE can't hold all indices in Batch"); + throw invalid_operation("Batch::set_data_type"); else if(t==UNSIGNED_SHORT && max_index>0xFFFE) - throw InvalidState("UNSIGNED_SHORT can't hold all indices in Batch"); + throw invalid_operation("Batch::set_data_type"); if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT) expand_data(); @@ -67,7 +65,7 @@ void Batch::set_data_type(DataType t) void Batch::use_index_buffer(Buffer *buf, Batch *prev) { if(buf && prev && prev->ibuf!=buf) - throw InvalidParameterValue("Previous batch is not in the same buffer"); + throw invalid_argument("Batch::use_index_buffer"); if(!buf) { @@ -119,26 +117,59 @@ Batch &Batch::append(unsigned i) void Batch::append(const vector &ind) { - data.reserve(data.size()+ind.size()*get_index_size()); + if(ind.empty()) + return; + + if(data.empty()) + min_index = max_index = ind.front(); + for(vector::const_iterator i=ind.begin(); i!=ind.end(); ++i) - append(*i); + { + min_index = min(min_index, *i); + max_index = max(max_index, *i); + } + + if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE) + set_data_type(UNSIGNED_INT); + else if(data_type==UNSIGNED_BYTE && max_index>0xFE) + set_data_type(UNSIGNED_SHORT); + + unsigned base = data.size(); + data.resize(data.size()+ind.size()*get_index_size()); + if(data_type==UNSIGNED_SHORT) + { + unsigned short *ptr = reinterpret_cast(&data[base]); + for(unsigned i=0; i(&data[base]); + for(unsigned i=0; i alias(*ibuf); Bind bind_ibuf(alias, true); - glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, (void *)ibuf_offset); + glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast(ibuf_offset)); } else glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);