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