]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarray.h
Refactor the internal interface of Bufferable a bit
[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
43         VertexArray(const VertexArray &);
44         VertexArray &operator=(const VertexArray &);
45 public:
46         VertexArray(const VertexFormat &);
47         ~VertexArray();
48
49         void reset(const VertexFormat &);
50         const VertexFormat &get_format() const { return format; }
51 private:
52         static unsigned get_array_slot(unsigned char);
53
54 public:
55         /// Deprecated alias for use_buffer
56         void use_vertex_buffer(Buffer *b) { use_buffer(b); }
57
58         void clear();
59         void reserve(unsigned);
60         float *append();
61         float *modify(unsigned);
62 private:
63         virtual unsigned get_data_size() const;
64         virtual const void *get_data_pointer() const { return &data[0]; }
65
66 public:
67         unsigned size() const { return data.size()/stride; }
68         const std::vector<float> &get_data() const { return data; }
69         const float *operator[](unsigned i) const { return &data[0]+i*stride; }
70
71         void apply() const;
72 private:
73         static void apply_arrays(const std::vector<Array> *, const std::vector<Array> *, const float *, unsigned);
74 };
75
76 } // namespace GL
77 } // namespace Msp
78
79 #endif