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