]> git.tdb.fi Git - libs/gl.git/blob - source/vertexformat.h
Load VertexFormat for Mesh as an array of VertexComponents
[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 /** A single vertex component.  Nvidia drivers have aliasing between the
10 fixed-functions and generic vertex attributes, despite the standard not
11 allowing it.  We use the same attribute indices here to avoid problems. */
12 enum VertexComponent
13 {
14         VERTEX2 = 1,
15         VERTEX3,
16         VERTEX4,
17         NORMAL3 = 10,
18         COLOR4_UBYTE = 12,
19         COLOR3_FLOAT = 14,
20         COLOR4_FLOAT,
21         TEXCOORD1 = 32,
22         TEXCOORD2,
23         TEXCOORD3,
24         TEXCOORD4,
25         ATTRIB1 = 64,
26         ATTRIB2,
27         ATTRIB3,
28         ATTRIB4
29 };
30
31 class VertexFormat
32 {
33 private:
34         unsigned char *data;
35
36 public:
37         VertexFormat();
38         VertexFormat(VertexComponent);
39         VertexFormat(const VertexFormat &);
40         VertexFormat &operator=(const VertexFormat &);
41         ~VertexFormat();
42
43         VertexFormat operator,(VertexComponent c) const;
44         VertexFormat operator,(unsigned i) const;
45
46         bool empty() const { return !data || !data[0]; }
47         const unsigned char *begin() const { return data ? data+1 : 0; }
48         const unsigned char *end() const { return data ? data+1+data[0] : 0; }
49         unsigned stride() const;
50         int offset(VertexComponent) const;
51 };
52
53 inline VertexFormat operator,(VertexComponent c1, VertexComponent c2)
54 { return (VertexFormat(c1), c2); }
55
56 inline VertexFormat operator,(VertexComponent c, unsigned i)
57 { return (VertexFormat(c), i); }
58
59 VertexComponent make_indexed_component(VertexComponent, unsigned);
60
61 inline unsigned get_component_type(unsigned char c)
62 { return c>>2; }
63
64 inline unsigned get_component_size(unsigned char c)
65 { return (c&3)+1; }
66
67 inline unsigned get_stride(const VertexFormat &f)
68 { return f.stride(); }
69
70 void operator>>(const LexicalConverter &, VertexComponent &);
71
72 } // namespace GL
73 } // namespace Msp
74
75 #endif