]> git.tdb.fi Git - libs/gl.git/blob - source/primitivebuilder.cpp
61bde9b96f8862881deb00e0719564dae1b08549
[libs/gl.git] / source / primitivebuilder.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2008, 2010-2011  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "primitivebuilder.h"
9
10 namespace Msp {
11 namespace GL {
12
13 PrimitiveBuilder::PrimitiveBuilder(VertexArray &a):
14         array(a),
15         vab(array),
16         in_batch(false),
17         offs(0)
18 { }
19
20 void PrimitiveBuilder::begin(PrimitiveType t)
21 {
22         if(in_batch)
23                 throw InvalidState("begin() already called");
24
25         type = t;
26         in_batch = true;
27
28         begin_();
29 }
30
31 void PrimitiveBuilder::end()
32 {
33         if(!in_batch)
34                 throw InvalidState("end() called without begin()");
35
36         in_batch = false;
37
38         end_();
39 }
40
41 void PrimitiveBuilder::offset(unsigned o)
42 {
43         if(o>array.size())
44                 throw InvalidParameterValue("Element offset out of range");
45         offs = o;
46 }
47
48 void PrimitiveBuilder::element(unsigned i)
49 {
50         if(!in_batch)
51                 throw InvalidState("Element specification not between begin() and end()");
52         if(offs+i>=array.size())
53                 throw InvalidParameterValue("Element index out of range");
54         element_(offs+i);
55 }
56
57 PrimitiveType PrimitiveBuilder::get_type() const
58 {
59         if(!in_batch)
60                 throw InvalidState("Not between begin() and end()");
61         return type;
62 }
63
64 void PrimitiveBuilder::vertex_(const Vector4 &v)
65 {
66         vab.color(col);
67         vab.normal(nor);
68         for(std::map<unsigned, Vector4>::iterator i=texc.begin(); i!=texc.end(); ++i)
69                 vab.multitexcoord(i->first, i->second);
70         for(std::map<unsigned, Vector4>::iterator i=attr.begin(); i!=attr.end(); ++i)
71                 vab.attrib(i->first, i->second);
72         vab.vertex(v);
73
74         if(in_batch)
75                 element_(array.size()-1);
76 }
77
78 } // namespace GL
79 } // namespace Msp