]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarray.h
Refactor AnimationPlayer ticking
[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 /**
21 Stores vertex data.  Both legacy and generic attributes are supported.  Mixing
22 the two is possible but discouraged, as driver-specific issues may arise.
23
24 The array's contents can be modified with the append and modify methods.  To
25 obtain the location of an individual component within the vertex, use
26 VertexFormat::offset.
27
28 A higher-level interface for filling in vertex data is available in the
29 VertexArrayBuilder class.
30 */
31 class VertexArray: public Bindable<VertexArray>, public Bufferable
32 {
33 public:
34         class Loader: public DataFile::Loader, public VertexArrayBuilder
35         {
36         public:
37                 Loader(VertexArray &);
38         };
39
40 private:
41         struct Array
42         {
43                 unsigned char component;
44                 unsigned char offset;
45
46                 Array();
47         };
48
49         VertexFormat format;
50         std::vector<float> data;
51         unsigned stride;
52         std::vector<Array> arrays;
53         bool legacy;
54
55         static bool legacy_used;
56
57         VertexArray(const VertexArray &);
58         VertexArray &operator=(const VertexArray &);
59 public:
60         VertexArray(const VertexFormat &);
61         ~VertexArray();
62
63         /// Resets the VertexArray to a different format.  All data is cleared.
64         void reset(const VertexFormat &);
65
66         const VertexFormat &get_format() const { return format; }
67 private:
68         static unsigned get_array_slot(unsigned char);
69
70 public:
71         /// Clears all vertices from the array.
72         void clear();
73
74         /// Reserve space for vertices.
75         void reserve(unsigned);
76
77         /// Append a new vertex at the end of the array and return its location.
78         float *append();
79
80         /// Returns the location of a vertex for modification.
81         float *modify(unsigned);
82 private:
83         virtual unsigned get_data_size() const;
84         virtual const void *get_data_pointer() const { return &data[0]; }
85
86 public:
87         unsigned size() const { return data.size()/stride; }
88         const std::vector<float> &get_data() const { return data; }
89         const float *operator[](unsigned i) const { return &data[0]+i*stride; }
90
91         /** Equivalent to apply(true).  For compatibility with the Bindable
92         interface. */
93         void bind() const { apply(); }
94
95         /** Applies component arrays to the GL.  If legacy is true, they are applied
96         as is.  If legacy is false, any legacy attributes are converted to generic
97         attributes. */
98         void apply(bool legacy = true) const;
99
100 private:
101         static void apply_arrays(const std::vector<Array> *, const std::vector<Array> *, const float *, unsigned, bool);
102 public:
103         static void unbind();
104 };
105
106 } // namespace GL
107 } // namespace Msp
108
109 #endif