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
vertices.use_vertex_buffer(0);
}
-RefPtr<MeshBuilder> Mesh::modify()
-{
- return new MeshBuilder(*this);
-}
-
void Mesh::add_batch(const Batch &b)
{
batches.push_back(b);
#include <msp/datafile/loader.h>
#include "batch.h"
-#include "meshbuilder.h"
#include "vertexarray.h"
namespace Msp {
Mesh(VertexFormat f);
void use_vertex_buffer(bool);
- RefPtr<MeshBuilder> modify();
const VertexArray &get_vertices() const { return vertices; }
void add_batch(const Batch &b);
const std::list<Batch> &get_batches() { return batches; }
PrimitiveBuilder::PrimitiveBuilder(VertexArray &a):
array(a),
+ vab(array),
in_batch(false)
{ }
type=t;
in_batch=true;
- builder=array.modify();
begin_();
}
if(!in_batch)
throw InvalidState("end() called without begin()");
- builder=0;
in_batch=false;
end_();
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);
{
protected:
VertexArray &array;
- RefPtr<VertexArrayBuilder> builder;
+ VertexArrayBuilder vab;
PrimitiveType type;
bool in_batch;
stride=get_stride(format);
}
-RefPtr<VertexArrayBuilder> VertexArray::modify()
-{
- return new VertexArrayBuilder(*this);
-}
-
void VertexArray::apply() const
{
if(format==NODATA)
}
}
+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)
class VertexArray
{
- friend class VertexArrayBuilder;
-
public:
class Loader: public DataFile::Loader, public VertexArrayBuilder
{
unsigned size() const { return data.size()/stride; }
void clear();
void reset(VertexFormat);
- RefPtr<VertexArrayBuilder> 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;
namespace GL {
VertexArrayBuilder::VertexArrayBuilder(VertexArray &a):
- data(a.data),
array(a)
{ }
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)
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;
}
class VertexArrayBuilder: public VertexBuilder
{
public:
- std::vector<float> &data;
-
VertexArrayBuilder(VertexArray &);
~VertexArrayBuilder();