]> git.tdb.fi Git - libs/gl.git/commitdiff
Add append() method and and operator[] to VertexArray
authorMikko Rasa <tdb@tdb.fi>
Fri, 5 Sep 2008 22:36:05 +0000 (22:36 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 5 Sep 2008 22:36:05 +0000 (22:36 +0000)
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
source/mesh.h
source/primitivebuilder.cpp
source/primitivebuilder.h
source/vertexarray.cpp
source/vertexarray.h
source/vertexarraybuilder.cpp
source/vertexarraybuilder.h

index 44005514d97a74deb54fdb339d42436b5f74ed0a..3a275e5b0731a3332e08fdbfb5e9e76f14be0339 100644 (file)
@@ -28,11 +28,6 @@ void Mesh::use_vertex_buffer(bool b)
                vertices.use_vertex_buffer(0);
 }
 
                vertices.use_vertex_buffer(0);
 }
 
-RefPtr<MeshBuilder> Mesh::modify()
-{
-       return new MeshBuilder(*this);
-}
-
 void Mesh::add_batch(const Batch &b)
 {
        batches.push_back(b);
 void Mesh::add_batch(const Batch &b)
 {
        batches.push_back(b);
index 8e1f906cf2dcbbd89ecb9c556a721991b7899b1a..e3753f8b867ce1516f66b680d5cc488142430209 100644 (file)
@@ -10,7 +10,6 @@ Distributed under the LGPL
 
 #include <msp/datafile/loader.h>
 #include "batch.h"
 
 #include <msp/datafile/loader.h>
 #include "batch.h"
-#include "meshbuilder.h"
 #include "vertexarray.h"
 
 namespace Msp {
 #include "vertexarray.h"
 
 namespace Msp {
@@ -41,7 +40,6 @@ public:
        Mesh(VertexFormat f);
 
        void use_vertex_buffer(bool);
        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; }
        const VertexArray &get_vertices() const { return vertices; }
        void add_batch(const Batch &b);
        const std::list<Batch> &get_batches() { return batches; }
index 92b2cb0eb0b76ef97c73875bd00f14651409c3b8..6a4d9bf0b866ccd1585a68ef05acf9b1c42df682 100644 (file)
@@ -12,6 +12,7 @@ namespace GL {
 
 PrimitiveBuilder::PrimitiveBuilder(VertexArray &a):
        array(a),
 
 PrimitiveBuilder::PrimitiveBuilder(VertexArray &a):
        array(a),
+       vab(array),
        in_batch(false)
 { }
 
        in_batch(false)
 { }
 
@@ -22,7 +23,6 @@ void PrimitiveBuilder::begin(PrimitiveType t)
 
        type=t;
        in_batch=true;
 
        type=t;
        in_batch=true;
-       builder=array.modify();
 
        begin_();
 }
 
        begin_();
 }
@@ -32,7 +32,6 @@ void PrimitiveBuilder::end()
        if(!in_batch)
                throw InvalidState("end() called without begin()");
 
        if(!in_batch)
                throw InvalidState("end() called without begin()");
 
-       builder=0;
        in_batch=false;
 
        end_();
        in_batch=false;
 
        end_();
@@ -56,10 +55,10 @@ PrimitiveType PrimitiveBuilder::get_type() const
 
 void PrimitiveBuilder::vertex_(float x, float y, float z, float w)
 {
 
 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);
 
        if(in_batch)
                element_(array.size()-1);
index ee8a8477c0cb9fac49e673502e067fd936615948..8cc1e7292b29598f1f93a228f4ee837459f1ff0a 100644 (file)
@@ -27,7 +27,7 @@ class PrimitiveBuilder: public VertexBuilder
 {
 protected:
        VertexArray &array;
 {
 protected:
        VertexArray &array;
-       RefPtr<VertexArrayBuilder> builder;
+       VertexArrayBuilder vab;
        PrimitiveType type;
        bool in_batch;
 
        PrimitiveType type;
        bool in_batch;
 
index 81a2acd5182a413777c39f0ebe944fe617b79b22..c166e98718338c55bcfb175dde856d7c44e41c74 100644 (file)
@@ -71,11 +71,6 @@ void VertexArray::reset(VertexFormat f)
        stride=get_stride(format);
 }
 
        stride=get_stride(format);
 }
 
-RefPtr<VertexArrayBuilder> VertexArray::modify()
-{
-       return new VertexArrayBuilder(*this);
-}
-
 void VertexArray::apply() const
 {
        if(format==NODATA)
 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)
 void VertexArray::set_array(unsigned array, unsigned bit, unsigned mask) const
 {
        if((enabled_arrays&mask) && !bit)
index f0baf2d4a84fa8896b21c351149d9cd28612ee04..8e72d9554c7e39078b2ddebed2e94d5f8434fd59 100644 (file)
@@ -23,8 +23,6 @@ class VertexBuffer;
 
 class VertexArray
 {
 
 class VertexArray
 {
-       friend class VertexArrayBuilder;
-
 public:
        class Loader: public DataFile::Loader, public 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);
        unsigned     size() const { return data.size()/stride; }
        void         clear();
        void         reset(VertexFormat);
-       RefPtr<VertexArrayBuilder> modify();
        void         apply() const;
        void         update_data();
        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;
 
 private:
        void set_array(unsigned, unsigned, unsigned) const;
 
index 32fa8392bda138029e5b1bae3d89163fbfea6d9c..c64c8ebfc882fca127b9771b2d1818af9ddeabaa 100644 (file)
@@ -12,7 +12,6 @@ namespace Msp {
 namespace GL {
 
 VertexArrayBuilder::VertexArrayBuilder(VertexArray &a):
 namespace GL {
 
 VertexArrayBuilder::VertexArrayBuilder(VertexArray &a):
-       data(a.data),
        array(a)
 { }
 
        array(a)
 { }
 
@@ -23,27 +22,28 @@ VertexArrayBuilder::~VertexArrayBuilder()
 
 void VertexArrayBuilder::vertex_(float x, float y, float z, float w)
 {
 
 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:
        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:
                        break;
                case 4:
-                       data.push_back(nx);
-                       data.push_back(ny);
-                       data.push_back(nz);
+                       *+ptr++=nx;
+                       *+ptr++=ny;
+                       *+ptr++=nz;
                        break;
                case 8:
                        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)
                        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);
                                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
                        {
                        }
                        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;
                }
                        }
                        break;
                }
index 0b8eef860c2b36952a2fc73d058e81de19d6701b..0065c536b8aa52b297d9111d821e26d77fe74d63 100644 (file)
@@ -20,8 +20,6 @@ class VertexArray;
 class VertexArrayBuilder: public VertexBuilder
 {
 public:
 class VertexArrayBuilder: public VertexBuilder
 {
 public:
-       std::vector<float> &data;
-
        VertexArrayBuilder(VertexArray &);
        ~VertexArrayBuilder();
 
        VertexArrayBuilder(VertexArray &);
        ~VertexArrayBuilder();