]> git.tdb.fi Git - libs/gl.git/blobdiff - source/builders/primitivebuilder.cpp
Rearrange soucre files into subdirectories
[libs/gl.git] / source / builders / primitivebuilder.cpp
diff --git a/source/builders/primitivebuilder.cpp b/source/builders/primitivebuilder.cpp
new file mode 100644 (file)
index 0000000..dd86990
--- /dev/null
@@ -0,0 +1,75 @@
+#include "error.h"
+#include "primitivebuilder.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+PrimitiveBuilder::PrimitiveBuilder(VertexArray &a):
+       array(a),
+       vab(array),
+       in_batch(false),
+       offs(0)
+{ }
+
+void PrimitiveBuilder::begin(PrimitiveType t)
+{
+       if(in_batch)
+               throw invalid_operation("PrimitiveBuilder::begin");
+
+       type = t;
+       in_batch = true;
+
+       begin_();
+}
+
+void PrimitiveBuilder::end()
+{
+       if(!in_batch)
+               throw invalid_operation("PrimitiveBuilder::end");
+
+       in_batch = false;
+
+       end_();
+}
+
+void PrimitiveBuilder::offset(unsigned o)
+{
+       if(o>array.size())
+               throw out_of_range("PrimitiveBuilder::offset");
+       offs = o;
+}
+
+void PrimitiveBuilder::element(unsigned i)
+{
+       if(!in_batch)
+               throw invalid_operation("PrimitiveBuilder::element");
+       if(offs+i>=array.size())
+               throw out_of_range("PrimitiveBuilder::element");
+       element_(offs+i);
+}
+
+PrimitiveType PrimitiveBuilder::get_type() const
+{
+       if(!in_batch)
+               throw invalid_operation("PrimitiveBuilder::get_type");
+       return type;
+}
+
+void PrimitiveBuilder::vertex_(const Vector4 &v)
+{
+       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(v);
+
+       if(in_batch)
+               element_(array.size()-1);
+}
+
+} // namespace GL
+} // namespace Msp