]> git.tdb.fi Git - libs/gl.git/blob - source/vertexformat.h
Turn VertexFormat into a class and hide the data member
[libs/gl.git] / source / vertexformat.h
1 #ifndef MSP_GL_VERTEXFORMAT_H_
2 #define MSP_GL_VERTEXFORMAT_H_
3
4 #include <msp/strings/lexicalcast.h>
5
6 namespace Msp {
7 namespace GL {
8
9 enum VertexComponent
10 {
11         VERTEX2 = 1,
12         VERTEX3,
13         VERTEX4,
14         NORMAL3 = 6,
15         COLOR4_UBYTE = 8,
16         COLOR3_FLOAT = 10,
17         COLOR4_FLOAT,
18         TEXCOORD1 = 12,
19         TEXCOORD2,
20         TEXCOORD3,
21         TEXCOORD4,
22         ATTRIB1 = 44,
23         ATTRIB2,
24         ATTRIB3,
25         ATTRIB4
26 };
27
28 class VertexFormat
29 {
30 private:
31         unsigned char *data;
32
33 public:
34         VertexFormat();
35         VertexFormat(VertexComponent);
36         VertexFormat(const VertexFormat &);
37         VertexFormat &operator=(const VertexFormat &);
38         ~VertexFormat();
39
40         VertexFormat operator,(VertexComponent c) const;
41         VertexFormat operator,(unsigned i) const;
42
43         bool empty() const { return !data || !data[0]; }
44         const unsigned char *begin() const { return data ? data+1 : 0; }
45         const unsigned char *end() const { return data ? data+1+data[0] : 0; }
46         unsigned stride() const;
47         int offset(VertexComponent, unsigned = 0) const;
48 };
49
50 inline VertexFormat operator,(VertexComponent c1, VertexComponent c2)
51 { return (VertexFormat(c1), c2); }
52
53 inline VertexFormat operator,(VertexComponent c, unsigned i)
54 { return (VertexFormat(c), i); }
55
56 inline unsigned get_component_type(unsigned char c)
57 { return c>>2; }
58
59 inline unsigned get_component_size(unsigned char c)
60 { return (c&3)+1; }
61
62 inline unsigned get_stride(const VertexFormat &f)
63 { return f.stride(); }
64
65 void operator>>(const LexicalConverter &, VertexComponent &);
66 void operator>>(const LexicalConverter &, VertexFormat &);
67
68 } // namespace GL
69 } // namespace Msp
70
71 #endif