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