]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/mesh.cpp
Only allow VertexArray's format to be set once
[libs/gl.git] / source / core / mesh.cpp
index f63b32933c64245f988004df8fbca9377d62541c..ea14d590b3012815526247555a4d4a2c74d473d1 100644 (file)
@@ -14,16 +14,15 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-Mesh::Mesh(ResourceManager *rm):
-       vertices(VERTEX3)
+Mesh::Mesh(ResourceManager *rm)
 {
        init(rm);
 }
 
-Mesh::Mesh(const VertexFormat &f, ResourceManager *rm):
-       vertices(f)
+Mesh::Mesh(const VertexFormat &f, ResourceManager *rm)
 {
        init(rm);
+       storage(f);
 }
 
 void Mesh::init(ResourceManager *rm)
@@ -46,6 +45,15 @@ Mesh::~Mesh()
        delete ibuf;
 }
 
+void Mesh::storage(const VertexFormat &fmt)
+{
+       if(!vertices.get_format().empty())
+               throw invalid_operation("Mesh::storage");
+
+       vertices.set_format(fmt);
+       vtx_setup.set_format(fmt);
+}
+
 void Mesh::clear()
 {
        vertices.clear();
@@ -99,6 +107,8 @@ unsigned Mesh::get_n_vertices() const
 
 float *Mesh::modify_vertex(unsigned i)
 {
+       if(vertices.get_format().empty())
+               throw invalid_operation("Mesh::modify_vertex");
        return vertices.modify(i);
 }
 
@@ -243,11 +253,13 @@ Mesh::Loader::Loader(Mesh &m, bool g):
        allow_gl_calls(g)
 {
        add("batch",    &Loader::batch);
+       add("storage",  &Loader::storage);
        add("vertices", &Loader::vertices);
+       add("vertices", &Loader::vertices_with_format);
        add("winding",  &Loader::winding);
 }
 
-void Mesh::Loader::vertices(const vector<VertexAttribute> &a)
+void Mesh::Loader::storage(const vector<VertexAttribute> &a)
 {
        if(a.empty())
                throw invalid_argument("No vertex attributes");
@@ -255,13 +267,20 @@ void Mesh::Loader::vertices(const vector<VertexAttribute> &a)
        VertexFormat fmt;
        for(vector<VertexAttribute>::const_iterator i=a.begin(); i!=a.end(); ++i)
                fmt = (fmt, *i);
-       obj.vertices.reset(fmt);
+       obj.storage(fmt);
+}
+
+void Mesh::Loader::vertices()
+{
        load_sub(obj.vertices);
        if(allow_gl_calls)
-       {
                obj.check_buffers(VERTEX_BUFFER);
-               obj.vtx_setup.refresh();
-       }
+}
+
+void Mesh::Loader::vertices_with_format(const vector<VertexAttribute> &a)
+{
+       storage(a);
+       vertices();
 }
 
 void Mesh::Loader::batch(PrimitiveType p)
@@ -315,7 +334,6 @@ bool Mesh::AsyncLoader::process()
        else if(phase==1)
        {
                mesh.resize_buffers();
-               mesh.vtx_setup.refresh();
                vertex_updater = mesh.vertices.refresh_async();
                if(!mesh.batches.empty())
                        index_updater = mesh.batches.front().refresh_async();