]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarray.h
Shuffle the members of VertexArray into a sensible order
[libs/gl.git] / source / vertexarray.h
1 #ifndef MSP_GL_VERTEXARRAY_H_
2 #define MSP_GL_VERTEXARRAY_H_
3
4 #include <climits>
5 #include <vector>
6 #include <msp/core/refptr.h>
7 #include <msp/datafile/loader.h>
8 #include "datatype.h"
9 #include "primitivetype.h"
10 #include "vertexarraybuilder.h"
11 #include "vertexformat.h"
12
13 namespace Msp {
14 namespace GL {
15
16 class Buffer;
17
18 class VertexArray
19 {
20 public:
21         class Loader: public DataFile::Loader, public VertexArrayBuilder
22         {
23         public:
24                 Loader(VertexArray &);
25         };
26
27 private:
28         struct ArrayMask
29         {
30                 enum
31                 {
32                         B = (sizeof(unsigned)*CHAR_BIT),
33                         N = (63+B)/B
34                 };
35
36                 unsigned mask[N];
37
38                 ArrayMask();
39
40                 void set(unsigned);
41                 bool is_set(unsigned) const;
42         };
43
44         VertexFormat format;
45         std::vector<float> data;
46         unsigned stride;
47         RefPtr<Buffer> vbuf;
48         bool defer_vbuf;
49         mutable bool dirty;
50
51         static ArrayMask enabled_arrays;
52
53         VertexArray(const VertexArray &);
54         VertexArray &operator=(const VertexArray &);
55 public:
56         VertexArray(const VertexFormat &);
57         ~VertexArray();
58
59         void reset(const VertexFormat &);
60         const VertexFormat &get_format() const { return format; }
61
62         void use_vertex_buffer();
63         void use_vertex_buffer(Buffer *);
64
65         void clear();
66         void reserve(unsigned);
67         float *append();
68         float *modify(unsigned);
69 private:
70         void set_dirty();
71
72 public:
73         unsigned size() const { return data.size()/stride; }
74         const std::vector<float> &get_data() const { return data; }
75         const float *operator[](unsigned i) const { return &data[0]+i*stride; }
76
77         void apply() const;
78 };
79
80 } // namespace GL
81 } // namespace Msp
82
83 #endif