X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fmesh.cpp;h=f6a060215692a01f73ba2f849f83cfdc6c82f9b2;hb=e9a898f;hp=4414a90e6577910e7881dee65877134e7cd1ba69;hpb=fe9836f2d8d7abb0480582c544611a5b248310cc;p=libs%2Fgl.git diff --git a/source/core/mesh.cpp b/source/core/mesh.cpp index 4414a90e..f6a06021 100644 --- a/source/core/mesh.cpp +++ b/source/core/mesh.cpp @@ -89,7 +89,6 @@ void Mesh::check_buffers(unsigned mask) ibuf = new Buffer; if(!batches.empty()) batches.front().change_buffer(ibuf); - vtx_setup.set_index_buffer(*ibuf); dirty |= INDEX_BUFFER; #ifdef DEBUG @@ -97,6 +96,9 @@ void Mesh::check_buffers(unsigned mask) vbuf->set_debug_name(debug_name+" [IBO]"); #endif } + + if(!batches.empty()) + vtx_setup.set_index_buffer(*ibuf, batches.front().get_index_type()); } } @@ -105,7 +107,7 @@ unsigned Mesh::get_n_vertices() const return vertices.size(); } -float *Mesh::modify_vertex(unsigned i) +char *Mesh::modify_vertex(unsigned i) { if(vertices.get_format().empty()) throw invalid_operation("Mesh::modify_vertex"); @@ -127,7 +129,7 @@ void Mesh::add_batch(const Batch &b) bool reallocate = (batches.size()==batches.capacity()); if(reallocate) { - for(vector::iterator i=batches.end(); i!=batches.begin(); ) + for(auto i=batches.end(); i!=batches.begin(); ) (--i)->use_buffer(0); } @@ -136,16 +138,29 @@ void Mesh::add_batch(const Batch &b) if(reallocate) { prev = 0; - for(vector::iterator i=batches.begin(); i!=batches.end(); ++i) + for(Batch &a: batches) { - i->use_buffer(ibuf, prev); - prev = &*i; + a.use_buffer(ibuf, prev); + prev = &a; } } else batches.back().use_buffer(ibuf, prev); } + DataType existing_type = batches.front().get_index_type(); + DataType added_type = batches.back().get_index_type(); + if(existing_type!=added_type) + { + if(get_type_size(existing_type)>get_type_size(added_type)) + batches.back().set_index_type(existing_type); + else + { + for(Batch &a: batches) + a.set_index_type(added_type); + } + } + check_buffers(INDEX_BUFFER); } @@ -187,13 +202,13 @@ void Mesh::draw(Renderer &renderer, const VertexSetup *vs, unsigned count) const if(!count) { - for(vector::const_iterator i=batches.begin(); i!=batches.end(); ++i) - renderer.draw(*i); + for(const Batch &b: batches) + renderer.draw(b); } else { - for(vector::const_iterator i=batches.begin(); i!=batches.end(); ++i) - renderer.draw_instanced(*i, count); + for(const Batch &b: batches) + renderer.draw_instanced(b, count); } } @@ -259,14 +274,14 @@ Mesh::Loader::Loader(Mesh &m, bool g): add("winding", &Mesh::face_winding); } -void Mesh::Loader::storage(const vector &a) +void Mesh::Loader::storage(const vector &attrs) { - if(a.empty()) + if(attrs.empty()) throw invalid_argument("No vertex attributes"); VertexFormat fmt; - for(vector::const_iterator i=a.begin(); i!=a.end(); ++i) - fmt = (fmt, *i); + for(VertexAttribute a: attrs) + fmt = (fmt, a); obj.storage(fmt); }