]> git.tdb.fi Git - libs/gl.git/blob - source/vertexbuilder.h
Fix a stale pointer issue with Animation
[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         void set_matrix(const Matrix &m)
37         { mtx = m; }
38
39         void transform(const Matrix &m)
40         { mtx *= m; }
41
42         const Matrix &get_matrix() const
43         { return mtx.top(); }
44
45         // Deprecated
46         MatrixStack &matrix()
47         { return mtx; }
48
49         void vertex(float x, float y)
50         { vertex(x, y, 0, 1); }
51
52         void vertex(float x, float y, float z)
53         { vertex(x, y, z, 1); }
54
55         void vertex(float x, float y, float z, float w)
56         { vertex(Vector4(x, y, z, w)); }
57
58         void vertex(const Vector3 &v)
59         { vertex(Vector4(v.x, v.y, v.z, 1)); }
60
61         void vertex(const Vector4 &v)
62         { vertex_(mtx.top()*v); }
63
64 protected:
65         virtual void vertex_(const Vector4 &) = 0;
66
67 public:
68         void normal(float x, float y, float z)
69         { normal(Vector3(x, y, z)); }
70
71         void normal(const Vector3 &n)
72         {
73                 Vector4 tn = mtx.top()*Vector4(n.x, n.y, n.z, 0);
74                 nor = Vector3(tn.x, tn.y, tn.z);
75         }
76
77         void tangent(float x, float y, float z)
78         { tangent(Vector3(x, y, z)); }
79
80         void tangent(const Vector3 &t)
81         {
82                 Vector4 tt = mtx.top()*Vector4(t.x, t.y, t.z, 0);
83                 attrib(get_component_type(TANGENT3), tt);
84         }
85
86         void binormal(float x, float y, float z)
87         { binormal(Vector3(x, y, z)); }
88
89         void binormal(const Vector3 &b)
90         {
91                 Vector4 tb = mtx.top()*Vector4(b.x, b.y, b.z, 0);
92                 attrib(get_component_type(BINORMAL3), tb);
93         }
94
95         void texcoord(float s)
96         { texcoord(s, 0, 0, 1); }
97
98         void texcoord(float s, float t)
99         { texcoord(s, t, 0, 1); }
100
101         void texcoord(float s, float t, float r)
102         { texcoord(s, t, r, 1); }
103
104         void texcoord(float s, float t, float r, float q)
105         { texcoord(Vector4(s, t, r, q)); }
106
107         void texcoord(const Vector4 &t)
108         { multitexcoord(0, t); }
109
110         void multitexcoord(unsigned i, float s)
111         { multitexcoord(i, s, 0, 0, 1); }
112
113         void multitexcoord(unsigned i, float s, float t)
114         { multitexcoord(i, s, t, 0, 1); }
115
116         void multitexcoord(unsigned i, float s, float t, float r)
117         { multitexcoord(i, s, t, r, 1); }
118
119         void multitexcoord(unsigned i, float s, float t, float r, float q)
120         { multitexcoord(i, Vector4(s, t, r, q)); }
121
122         void multitexcoord(unsigned i, const Vector4 &t)
123         { texc[i] = t; }
124
125         void color(unsigned char r, unsigned char g, unsigned char b)
126         { color(r, g, b, 255); }
127
128         void color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
129         { color(r/255.f, g/255.f, b/255.f, a/255.f); }
130
131         void color(float r, float g, float b)
132         { color(r, g, b, 1); }
133
134         void color(float r, float g, float b, float a)
135         { color(Color(r, g, b, a)); }
136
137         void color(const Color &c)
138         { col = c; }
139
140         void attrib(unsigned i, float x)
141         { attrib(i, x, 0, 0, 1); }
142
143         void attrib(unsigned i, float x, float y)
144         { attrib(i, x, y, 0, 1); }
145
146         void attrib(unsigned i, float x, float y, float z)
147         { attrib(i, x, y, z, 1); }
148
149         void attrib(unsigned i, float x, float y, float z, float w)
150         { attrib(i, Vector4(x, y, z, w)); }
151
152         void attrib(unsigned i, const Vector4 &a)
153         { attr[i] = a; }
154 };
155
156 } // namespace GL
157 } // namespace Msp
158
159 #endif