From: Mikko Rasa Date: Thu, 28 Nov 2013 20:21:59 +0000 (+0200) Subject: Move vertex buffer management from VertexArray to Mesh X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=99b3477d16295c9b90daa0a397bfc375dd3aac4d;p=libs%2Fgl.git Move vertex buffer management from VertexArray to Mesh It was getting in the way of implementing vertex array objects. --- diff --git a/source/mesh.cpp b/source/mesh.cpp index 572ac619..281bb7c2 100644 --- a/source/mesh.cpp +++ b/source/mesh.cpp @@ -9,20 +9,23 @@ namespace GL { Mesh::Mesh(): vertices(VERTEX3), + vbuf(0), ibuf(0), - defer_ibuf(true), + defer_buffers(true), winding(0) { } Mesh::Mesh(const VertexFormat &f): vertices(f), + vbuf(0), ibuf(0), - defer_ibuf(true), + defer_buffers(true), winding(0) { } Mesh::~Mesh() { + delete vbuf; delete ibuf; } @@ -34,22 +37,31 @@ void Mesh::clear() void Mesh::use_buffers(bool b) { + defer_buffers = false; if(b) - { - vertices.use_vertex_buffer(); - if(!ibuf) - ibuf = new Buffer(ELEMENT_ARRAY_BUFFER); - defer_ibuf = false; - } + create_buffers(); else { vertices.use_vertex_buffer(0); + delete vbuf; + vbuf = 0; delete ibuf; ibuf = 0; - defer_ibuf = false; } } +void Mesh::create_buffers() +{ + defer_buffers = false; + + if(!vbuf) + vbuf = new Buffer(ARRAY_BUFFER); + vertices.use_vertex_buffer(vbuf); + + if(!ibuf) + ibuf = new Buffer(ELEMENT_ARRAY_BUFFER); +} + unsigned Mesh::get_n_vertices() const { return vertices.size(); @@ -62,11 +74,8 @@ float *Mesh::modify_vertex(unsigned i) void Mesh::add_batch(const Batch &b) { - if(defer_ibuf) - { - ibuf = new Buffer(ELEMENT_ARRAY_BUFFER); - defer_ibuf = false; - } + if(defer_buffers) + create_buffers(); if(!batches.empty() && batches.back().can_append(b.get_type())) batches.back().append(b); diff --git a/source/mesh.h b/source/mesh.h index 83f2fb07..6181d5fb 100644 --- a/source/mesh.h +++ b/source/mesh.h @@ -35,8 +35,9 @@ public: private: VertexArray vertices; std::list batches; + Buffer *vbuf; Buffer *ibuf; - bool defer_ibuf; + bool defer_buffers; const WindingTest *winding; public: @@ -46,7 +47,10 @@ public: void clear(); void use_buffers(bool); +private: + void create_buffers(); +public: const VertexArray &get_vertices() const { return vertices; } unsigned get_n_vertices() const; float *modify_vertex(unsigned); diff --git a/source/vertexarray.cpp b/source/vertexarray.cpp index 5065f305..3fca527d 100644 --- a/source/vertexarray.cpp +++ b/source/vertexarray.cpp @@ -11,7 +11,6 @@ namespace Msp { namespace GL { VertexArray::VertexArray(const VertexFormat &f): - defer_vbuf(true), dirty(false) { reset(f); @@ -76,21 +75,9 @@ unsigned VertexArray::get_array_slot(unsigned char comp) } } -void VertexArray::use_vertex_buffer() -{ - if(vbuf) - return; - - vbuf = new Buffer(ARRAY_BUFFER); - defer_vbuf = false; - dirty = true; -} - void VertexArray::use_vertex_buffer(Buffer *b) { vbuf = b; - vbuf.keep(); - defer_vbuf = false; dirty = true; } @@ -120,11 +107,6 @@ float *VertexArray::modify(unsigned i) void VertexArray::set_dirty() { dirty = true; - if(defer_vbuf) - { - vbuf = new Buffer(ARRAY_BUFFER); - defer_vbuf = false; - } } void VertexArray::apply() const diff --git a/source/vertexarray.h b/source/vertexarray.h index 82a6b6ac..af1817b1 100644 --- a/source/vertexarray.h +++ b/source/vertexarray.h @@ -38,8 +38,7 @@ private: std::vector data; unsigned stride; std::vector arrays; - RefPtr vbuf; - bool defer_vbuf; + Buffer *vbuf; mutable bool dirty; VertexArray(const VertexArray &); @@ -54,7 +53,6 @@ private: static unsigned get_array_slot(unsigned char); public: - void use_vertex_buffer(); void use_vertex_buffer(Buffer *); void clear();