]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarray.h
Move vertex buffer management from VertexArray to Mesh
[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         Buffer *vbuf;
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         void use_vertex_buffer(Buffer *);
57
58         void clear();
59         void reserve(unsigned);
60         float *append();
61         float *modify(unsigned);
62 private:
63         void set_dirty();
64
65 public:
66         unsigned size() const { return data.size()/stride; }
67         const std::vector<float> &get_data() const { return data; }
68         const float *operator[](unsigned i) const { return &data[0]+i*stride; }
69
70         void apply() const;
71 private:
72         static void apply_arrays(const std::vector<Array> *, const std::vector<Array> *, const float *, unsigned);
73 };
74
75 } // namespace GL
76 } // namespace Msp
77
78 #endif