]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarray.h
Deactivate all arrays when deleting the active VertexArray
[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 "bindable.h"
9 #include "datatype.h"
10 #include "primitivetype.h"
11 #include "vertexarraybuilder.h"
12 #include "vertexformat.h"
13
14 namespace Msp {
15 namespace GL {
16
17 class Buffer;
18
19 class VertexArray: public Bindable<VertexArray>
20 {
21 public:
22         class Loader: public DataFile::Loader, public VertexArrayBuilder
23         {
24         public:
25                 Loader(VertexArray &);
26         };
27
28 private:
29         struct Array
30         {
31                 unsigned char component;
32                 unsigned char offset;
33
34                 Array();
35         };
36
37         VertexFormat format;
38         std::vector<float> data;
39         unsigned stride;
40         std::vector<Array> arrays;
41         RefPtr<Buffer> vbuf;
42         bool defer_vbuf;
43         mutable bool dirty;
44
45         VertexArray(const VertexArray &);
46         VertexArray &operator=(const VertexArray &);
47 public:
48         VertexArray(const VertexFormat &);
49         ~VertexArray();
50
51         void reset(const VertexFormat &);
52         const VertexFormat &get_format() const { return format; }
53 private:
54         static unsigned get_array_slot(unsigned char);
55
56 public:
57         void use_vertex_buffer();
58         void use_vertex_buffer(Buffer *);
59
60         void clear();
61         void reserve(unsigned);
62         float *append();
63         float *modify(unsigned);
64 private:
65         void set_dirty();
66
67 public:
68         unsigned size() const { return data.size()/stride; }
69         const std::vector<float> &get_data() const { return data; }
70         const float *operator[](unsigned i) const { return &data[0]+i*stride; }
71
72         void apply() const;
73 private:
74         static void apply_arrays(const std::vector<Array> *, const std::vector<Array> *, const float *, unsigned);
75 };
76
77 } // namespace GL
78 } // namespace Msp
79
80 #endif