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