]> git.tdb.fi Git - libs/gl.git/blob - source/primitivebuilder.cpp
Support specifying elements in PrimitiveBuilder
[libs/gl.git] / source / primitivebuilder.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  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         in_batch(false)
16 { }
17
18 void PrimitiveBuilder::begin(PrimitiveType t)
19 {
20         if(in_batch)
21                 throw InvalidState("begin() already called");
22
23         type=t;
24         in_batch=true;
25         builder=array.modify();
26
27         begin_();
28 }
29
30 void PrimitiveBuilder::end()
31 {
32         if(!in_batch)
33                 throw InvalidState("end() called without begin()");
34
35         builder=0;
36         in_batch=false;
37
38         end_();
39 }
40
41 void PrimitiveBuilder::element(unsigned i)
42 {
43         if(!in_batch)
44                 throw InvalidState("Element specification not between begin() and end()");
45         if(i>=array.size())
46                 throw InvalidParameterValue("Element index out of range");
47         element_(i);
48 }
49
50 PrimitiveType PrimitiveBuilder::get_type() const
51 {
52         if(!in_batch)
53                 throw InvalidState("Not between begin() and end()");
54         return type;
55 }
56
57 void PrimitiveBuilder::vertex_(float x, float y, float z, float w)
58 {
59         builder->texcoord(ts, tt, tr,tq);
60         builder->color(cr, cg, cb, ca);
61         builder->normal(nx, ny, nz);
62         builder->vertex(x, y, z, w);
63
64         if(in_batch)
65                 element_(array.size()-1);
66 }
67
68 } // namespace GL
69 } // namespace Msp