From 16f6f15328b3a6eec87b1b5e5822368966d44a38 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 5 Sep 2008 22:36:05 +0000 Subject: [PATCH] Add append() method and and operator[] to VertexArray Remove modify() methods from VertexArray and Mesh in favor of constructing Builders in stack Refactor VertexArrayBuilder to not need reference to the array's data vector Fix PrimitiveBuilder so that creating vertices outside of begin/end works --- source/mesh.cpp | 5 ----- source/mesh.h | 2 -- source/primitivebuilder.cpp | 11 +++++------ source/primitivebuilder.h | 2 +- source/vertexarray.cpp | 11 ++++++----- source/vertexarray.h | 6 +++--- source/vertexarraybuilder.cpp | 34 +++++++++++++++++----------------- source/vertexarraybuilder.h | 2 -- 8 files changed, 32 insertions(+), 41 deletions(-) diff --git a/source/mesh.cpp b/source/mesh.cpp index 44005514..3a275e5b 100644 --- a/source/mesh.cpp +++ b/source/mesh.cpp @@ -28,11 +28,6 @@ void Mesh::use_vertex_buffer(bool b) vertices.use_vertex_buffer(0); } -RefPtr Mesh::modify() -{ - return new MeshBuilder(*this); -} - void Mesh::add_batch(const Batch &b) { batches.push_back(b); diff --git a/source/mesh.h b/source/mesh.h index 8e1f906c..e3753f8b 100644 --- a/source/mesh.h +++ b/source/mesh.h @@ -10,7 +10,6 @@ Distributed under the LGPL #include #include "batch.h" -#include "meshbuilder.h" #include "vertexarray.h" namespace Msp { @@ -41,7 +40,6 @@ public: Mesh(VertexFormat f); void use_vertex_buffer(bool); - RefPtr modify(); const VertexArray &get_vertices() const { return vertices; } void add_batch(const Batch &b); const std::list &get_batches() { return batches; } diff --git a/source/primitivebuilder.cpp b/source/primitivebuilder.cpp index 92b2cb0e..6a4d9bf0 100644 --- a/source/primitivebuilder.cpp +++ b/source/primitivebuilder.cpp @@ -12,6 +12,7 @@ namespace GL { PrimitiveBuilder::PrimitiveBuilder(VertexArray &a): array(a), + vab(array), in_batch(false) { } @@ -22,7 +23,6 @@ void PrimitiveBuilder::begin(PrimitiveType t) type=t; in_batch=true; - builder=array.modify(); begin_(); } @@ -32,7 +32,6 @@ void PrimitiveBuilder::end() if(!in_batch) throw InvalidState("end() called without begin()"); - builder=0; in_batch=false; end_(); @@ -56,10 +55,10 @@ PrimitiveType PrimitiveBuilder::get_type() const void PrimitiveBuilder::vertex_(float x, float y, float z, float w) { - builder->texcoord(ts, tt, tr,tq); - builder->color(cr, cg, cb, ca); - builder->normal(nx, ny, nz); - builder->vertex(x, y, z, w); + vab.texcoord(ts, tt, tr,tq); + vab.color(cr, cg, cb, ca); + vab.normal(nx, ny, nz); + vab.vertex(x, y, z, w); if(in_batch) element_(array.size()-1); diff --git a/source/primitivebuilder.h b/source/primitivebuilder.h index ee8a8477..8cc1e729 100644 --- a/source/primitivebuilder.h +++ b/source/primitivebuilder.h @@ -27,7 +27,7 @@ class PrimitiveBuilder: public VertexBuilder { protected: VertexArray &array; - RefPtr builder; + VertexArrayBuilder vab; PrimitiveType type; bool in_batch; diff --git a/source/vertexarray.cpp b/source/vertexarray.cpp index 81a2acd5..c166e987 100644 --- a/source/vertexarray.cpp +++ b/source/vertexarray.cpp @@ -71,11 +71,6 @@ void VertexArray::reset(VertexFormat f) stride=get_stride(format); } -RefPtr VertexArray::modify() -{ - return new VertexArrayBuilder(*this); -} - void VertexArray::apply() const { if(format==NODATA) @@ -134,6 +129,12 @@ void VertexArray::update_data() } } +float *VertexArray::append() +{ + data.insert(data.end(), stride, 0.0f); + return &*data.end()-stride; +} + void VertexArray::set_array(unsigned array, unsigned bit, unsigned mask) const { if((enabled_arrays&mask) && !bit) diff --git a/source/vertexarray.h b/source/vertexarray.h index f0baf2d4..8e72d955 100644 --- a/source/vertexarray.h +++ b/source/vertexarray.h @@ -23,8 +23,6 @@ class VertexBuffer; class VertexArray { - friend class VertexArrayBuilder; - public: class Loader: public DataFile::Loader, public VertexArrayBuilder { @@ -53,9 +51,11 @@ public: unsigned size() const { return data.size()/stride; } void clear(); void reset(VertexFormat); - RefPtr modify(); void apply() const; void update_data(); + float *append(); + float *operator[](unsigned i) { return &data[0]+i*stride; } + const float *operator[](unsigned i) const { return &data[0]+i*stride; } private: void set_array(unsigned, unsigned, unsigned) const; diff --git a/source/vertexarraybuilder.cpp b/source/vertexarraybuilder.cpp index 32fa8392..c64c8ebf 100644 --- a/source/vertexarraybuilder.cpp +++ b/source/vertexarraybuilder.cpp @@ -12,7 +12,6 @@ namespace Msp { namespace GL { VertexArrayBuilder::VertexArrayBuilder(VertexArray &a): - data(a.data), array(a) { } @@ -23,27 +22,28 @@ VertexArrayBuilder::~VertexArrayBuilder() void VertexArrayBuilder::vertex_(float x, float y, float z, float w) { + float *ptr=array.append(); for(uint fmt=array.get_format(); fmt; fmt>>=4) { uint size=(fmt&3)+1; switch(fmt&12) { case 0: - data.push_back(x); - data.push_back(y); - if(size>=3) data.push_back(z); - if(size>=4) data.push_back(w); + *ptr++=x; + *ptr++=y; + if(size>=3) *ptr++=z; + if(size>=4) *ptr++=w; break; case 4: - data.push_back(nx); - data.push_back(ny); - data.push_back(nz); + *+ptr++=nx; + *+ptr++=ny; + *+ptr++=nz; break; case 8: - data.push_back(ts); - if(size>=2) data.push_back(tt); - if(size>=3) data.push_back(tr); - if(size>=4) data.push_back(tq); + *+ptr++=ts; + if(size>=2) *+ptr++=tt; + if(size>=3) *+ptr++=tr; + if(size>=4) *+ptr++=tq; break; case 12: if(size==1) @@ -53,14 +53,14 @@ void VertexArrayBuilder::vertex_(float x, float y, float z, float w) u.c[1]=(ubyte)(cg*255); u.c[2]=(ubyte)(cb*255); u.c[3]=(ubyte)(ca*255); - data.push_back(u.f); + *+ptr++=u.f; } else { - data.push_back(cr); - data.push_back(cg); - data.push_back(cb); - if(size>=4) data.push_back(ca); + *+ptr++=cr; + *+ptr++=cg; + *+ptr++=cb; + if(size>=4) *+ptr++=ca; } break; } diff --git a/source/vertexarraybuilder.h b/source/vertexarraybuilder.h index 0b8eef86..0065c536 100644 --- a/source/vertexarraybuilder.h +++ b/source/vertexarraybuilder.h @@ -20,8 +20,6 @@ class VertexArray; class VertexArrayBuilder: public VertexBuilder { public: - std::vector &data; - VertexArrayBuilder(VertexArray &); ~VertexArrayBuilder(); -- 2.43.0