Refactor vertex builders
[libs/gl.git] / source / builders / primitivebuilder.cpp
1 #include "error.h"
2 #include "primitivebuilder.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GL {
8
9 PrimitiveBuilder::PrimitiveBuilder(VertexArray &a):
10         array(a),
11         vab(array),
12         in_batch(false),
13         offs(0)
14 { }
15
16 void PrimitiveBuilder::begin(PrimitiveType t)
17 {
18         if(in_batch)
19                 throw invalid_operation("PrimitiveBuilder::begin");
20
21         type = t;
22         in_batch = true;
23
24         begin_();
25 }
26
27 void PrimitiveBuilder::end()
28 {
29         if(!in_batch)
30                 throw invalid_operation("PrimitiveBuilder::end");
31
32         in_batch = false;
33
34         end_();
35 }
36
37 void PrimitiveBuilder::offset(unsigned o)
38 {
39         if(o>array.size())
40                 throw out_of_range("PrimitiveBuilder::offset");
41         offs = o;
42 }
43
44 void PrimitiveBuilder::element(unsigned i)
45 {
46         if(!in_batch)
47                 throw invalid_operation("PrimitiveBuilder::element");
48         if(offs+i>=array.size())
49                 throw out_of_range("PrimitiveBuilder::element");
50         element_(offs+i);
51 }
52
53 PrimitiveType PrimitiveBuilder::get_type() const
54 {
55         if(!in_batch)
56                 throw invalid_operation("PrimitiveBuilder::get_type");
57         return type;
58 }
59
60 void PrimitiveBuilder::vertex_(const Vector4 &v)
61 {
62         const VertexFormat &format = array.get_format();
63         for(const unsigned char *c=format.begin(); c!=format.end(); ++c)
64         {
65                 unsigned t = get_component_type(*c);
66                 if(t<attr.size())
67                         vab.attrib(t, attr[t]);
68         }
69         vab.vertex(v);
70
71         if(in_batch)
72                 element_(array.size()-1);
73 }
74
75 } // namespace GL
76 } // namespace Msp