]> git.tdb.fi Git - libs/gl.git/blob - source/vertexbuilder.h
Drop Id tags and copyright notices from files
[libs/gl.git] / source / vertexbuilder.h
1 #ifndef MSP_GL_VERTEXBUILDER_H_
2 #define MSP_GL_VERTEXBUILDER_H_
3
4 #include <map>
5 #include "color.h"
6 #include "matrix.h"
7 #include "vector.h"
8
9 namespace Msp {
10 namespace GL {
11
12 /**
13 Base class for classes that build vertices from a series of function calls.
14 The operating model closely follows that of OpenGL immediate mode: vertex
15 attributes can be specified at any time, and when a vertex() function is
16 called, a vertex is created with the active attribute values.
17
18 A derived class must overload the 4-argument vertex_() function to process the
19 data.  Attributes can be read from protected member variables.
20 */
21 class VertexBuilder
22 {
23 protected:
24         MatrixStack mtx;
25         Vector3 nor;
26         Color col;
27         std::map<unsigned, Vector4> texc;
28         std::map<unsigned, Vector4> attr;
29
30 public:
31         VertexBuilder(): nor(0, 0, 1) { }
32
33         virtual ~VertexBuilder() { }
34
35         MatrixStack &matrix()
36         { return mtx; }
37
38         void vertex(float x, float y)
39         { vertex(x, y, 0, 1); }
40
41         void vertex(float x, float y, float z)
42         { vertex(x, y, z, 1); }
43
44         void vertex(float x, float y, float z, float w)
45         { vertex(Vector4(x, y, z, w)); }
46
47         void vertex(const Vector4 &v)
48         { vertex_(mtx.top()*v); }
49
50 protected:
51         virtual void vertex_(const Vector4 &) = 0;
52
53 public:
54         void normal(float x, float y, float z)
55         { normal(Vector3(x, y, z)); }
56
57         void normal(const Vector3 &n)
58         {
59                 Vector4 tn = mtx.top()*Vector4(n.x, n.y, n.z, 0);
60                 nor = Vector3(tn.x, tn.y, tn.z);
61         }
62
63         void texcoord(float s)
64         { texcoord(s, 0, 0, 1); }
65
66         void texcoord(float s, float t)
67         { texcoord(s, t, 0, 1); }
68
69         void texcoord(float s, float t, float r)
70         { texcoord(s, t, r, 1); }
71
72         void texcoord(float s, float t, float r, float q)
73         { texcoord(Vector4(s, t, r, q)); }
74
75         void texcoord(const Vector4 &t)
76         { multitexcoord(0, t); }
77
78         void multitexcoord(unsigned i, float s)
79         { multitexcoord(i, s, 0, 0, 1); }
80
81         void multitexcoord(unsigned i, float s, float t)
82         { multitexcoord(i, s, t, 0, 1); }
83
84         void multitexcoord(unsigned i, float s, float t, float r)
85         { multitexcoord(i, s, t, r, 1); }
86
87         void multitexcoord(unsigned i, float s, float t, float r, float q)
88         { multitexcoord(i, Vector4(s, t, r, q)); }
89
90         void multitexcoord(unsigned i, const Vector4 &t)
91         { texc[i] = t; }
92
93         void color(unsigned char r, unsigned char g, unsigned char b)
94         { color(r, g, b, 255); }
95
96         void color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
97         { color(r/255.f, g/255.f, b/255.f, a/255.f); }
98
99         void color(float r, float g, float b)
100         { color(r, g, b, 1); }
101
102         void color(float r, float g, float b, float a)
103         { color(Color(r, g, b, a)); }
104
105         void color(const Color &c)
106         { col = c; }
107
108         void attrib(unsigned i, float x)
109         { attrib(i, x, 0, 0, 1); }
110
111         void attrib(unsigned i, float x, float y)
112         { attrib(i, x, y, 0, 1); }
113
114         void attrib(unsigned i, float x, float y, float z)
115         { attrib(i, x, y, z, 1); }
116
117         void attrib(unsigned i, float x, float y, float z, float w)
118         { attrib(i, Vector4(x, y, z, w)); }
119
120         void attrib(unsigned i, const Vector4 &a)
121         { attr[i] = a; }
122 };
123
124 } // namespace GL
125 } // namespace Msp
126
127 #endif