From: Mikko Rasa Date: Sun, 22 Apr 2018 08:54:16 +0000 (+0300) Subject: Store mesh batches in vector instead of list X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=90e99be82f2bb48ed950fa067b1da8404c86b152;p=libs%2Fgl.git Store mesh batches in vector instead of list --- diff --git a/source/mesh.cpp b/source/mesh.cpp index 8c1e79c4..b3b719c5 100644 --- a/source/mesh.cpp +++ b/source/mesh.cpp @@ -128,10 +128,29 @@ void Mesh::add_batch(const Batch &b) batches.back().append(b); else { + bool reallocate = (batches.size()==batches.capacity()); + if(reallocate && ibuf) + { + for(vector::iterator i=batches.end(); i!=batches.begin(); ) + (--i)->use_buffer(0); + } + Batch *prev = (batches.empty() ? 0 : &batches.back()); batches.push_back(b); if(ibuf) - batches.back().use_buffer(ibuf, prev); + { + if(reallocate) + { + prev = 0; + for(vector::iterator i=batches.begin(); i!=batches.end(); ++i) + { + i->use_buffer(ibuf, prev); + prev = &*i; + } + } + else + batches.back().use_buffer(ibuf, prev); + } } } @@ -158,7 +177,7 @@ void Mesh::draw() const BindRestore bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER); Bind bind_winding(winding); - for(list::const_iterator i=batches.begin(); i!=batches.end(); ++i) + for(vector::const_iterator i=batches.begin(); i!=batches.end(); ++i) i->draw(); } @@ -174,7 +193,7 @@ void Mesh::draw(Renderer &renderer) const renderer.set_mesh(this); renderer.set_winding_test(winding); - for(list::const_iterator i=batches.begin(); i!=batches.end(); ++i) + for(vector::const_iterator i=batches.begin(); i!=batches.end(); ++i) renderer.draw(*i); } diff --git a/source/mesh.h b/source/mesh.h index e9feb675..70370633 100644 --- a/source/mesh.h +++ b/source/mesh.h @@ -52,7 +52,7 @@ private: }; VertexArray vertices; - std::list batches; + std::vector batches; Buffer *vbuf; Buffer *ibuf; unsigned vao_id; @@ -82,7 +82,7 @@ public: float *modify_vertex(unsigned); void add_batch(const Batch &b); - const std::list &get_batches() { return batches; } + const std::vector &get_batches() const { return batches; } void set_winding(const WindingTest *);