]> git.tdb.fi Git - libs/gl.git/blobdiff - source/primitivebuilder.cpp
Support multiple sets of texture coordinates in VertexArrays
[libs/gl.git] / source / primitivebuilder.cpp
index e461cf211bf77f2fe79b7f25b67a579c2d99fca3..22106a344a828326c7329c5f482bc2a230e56a92 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspgl
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
+Copyright © 2008, 2010  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
@@ -12,6 +12,7 @@ namespace GL {
 
 PrimitiveBuilder::PrimitiveBuilder(VertexArray &a):
        array(a),
+       vab(array),
        in_batch(false)
 { }
 
@@ -20,9 +21,8 @@ void PrimitiveBuilder::begin(PrimitiveType t)
        if(in_batch)
                throw InvalidState("begin() already called");
 
-       type=t;
-       in_batch=true;
-       builder=array.modify();
+       type = t;
+       in_batch = true;
 
        begin_();
 }
@@ -32,12 +32,20 @@ void PrimitiveBuilder::end()
        if(!in_batch)
                throw InvalidState("end() called without begin()");
 
-       builder=0;
-       in_batch=false;
+       in_batch = false;
 
        end_();
 }
 
+void PrimitiveBuilder::element(unsigned i)
+{
+       if(!in_batch)
+               throw InvalidState("Element specification not between begin() and end()");
+       if(i>=array.size())
+               throw InvalidParameterValue("Element index out of range");
+       element_(i);
+}
+
 PrimitiveType PrimitiveBuilder::get_type() const
 {
        if(!in_batch)
@@ -47,13 +55,16 @@ PrimitiveType PrimitiveBuilder::get_type() const
 
 void PrimitiveBuilder::vertex_(float x, float y, float z, float w)
 {
-       if(!in_batch)
-               throw InvalidState("Vertex specification not between begin() and end()");
+       vab.color(col);
+       vab.normal(nor);
+       for(std::map<unsigned, Vector4>::iterator i=texc.begin(); i!=texc.end(); ++i)
+               vab.multitexcoord(i->first, i->second);
+       for(std::map<unsigned, Vector4>::iterator i=attr.begin(); i!=attr.end(); ++i)
+               vab.attrib(i->first, i->second);
+       vab.vertex(x, y, z, w);
 
-       builder->texcoord(ts, tt, tr,tq);
-       builder->color(cr, cg, cb, ca);
-       builder->normal(nx, ny, nz);
-       builder->vertex(x, y, z, w);
+       if(in_batch)
+               element_(array.size()-1);
 }
 
 } // namespace GL