]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarray.h
Convert VertexArray into a Bufferable
[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 "bufferable.h"
10 #include "datatype.h"
11 #include "primitivetype.h"
12 #include "vertexarraybuilder.h"
13 #include "vertexformat.h"
14
15 namespace Msp {
16 namespace GL {
17
18 class Buffer;
19
20 class VertexArray: public Bindable<VertexArray>, public Bufferable
21 {
22 public:
23         class Loader: public DataFile::Loader, public VertexArrayBuilder
24         {
25         public:
26                 Loader(VertexArray &);
27         };
28
29 private:
30         struct Array
31         {
32                 unsigned char component;
33                 unsigned char offset;
34
35                 Array();
36         };
37
38         VertexFormat format;
39         std::vector<float> data;
40         unsigned stride;
41         std::vector<Array> arrays;
42         mutable bool dirty;
43
44         VertexArray(const VertexArray &);
45         VertexArray &operator=(const VertexArray &);
46 public:
47         VertexArray(const VertexFormat &);
48         ~VertexArray();
49
50         void reset(const VertexFormat &);
51         const VertexFormat &get_format() const { return format; }
52 private:
53         static unsigned get_array_slot(unsigned char);
54
55 public:
56         /// Deprecated alias for use_buffer
57         void use_vertex_buffer(Buffer *b) { use_buffer(b); }
58
59         void clear();
60         void reserve(unsigned);
61         float *append();
62         float *modify(unsigned);
63 private:
64         virtual unsigned get_data_size() const;
65         virtual void upload_data() const;
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