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