]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexarray.h
290fa10c455f07b56f95c6ab0da3cfa9cc8f5fab
[libs/gl.git] / source / core / vertexarray.h
1 #ifndef MSP_GL_VERTEXARRAY_H_
2 #define MSP_GL_VERTEXARRAY_H_
3
4 #include <vector>
5 #include <msp/datafile/loader.h>
6 #include "bufferable.h"
7 #include "vertexarraybuilder.h"
8 #include "vertexformat.h"
9
10 namespace Msp {
11 namespace GL {
12
13 /**
14 Stores vertex data.
15
16 The array's contents can be modified with the append and modify methods.  To
17 obtain the location of an individual component within the vertex, use
18 VertexFormat::offset.
19
20 A higher-level interface for filling in vertex data is available in the
21 VertexArrayBuilder class.
22 */
23 class VertexArray: public Bufferable
24 {
25 public:
26         class Loader: public DataFile::Loader, public VertexArrayBuilder
27         {
28         public:
29                 Loader(VertexArray &);
30         };
31
32 private:
33         VertexFormat format;
34         std::vector<char> data;
35         unsigned stride;
36
37         VertexArray(const VertexArray &);
38         VertexArray &operator=(const VertexArray &);
39 public:
40         VertexArray();
41
42         /// Construct a VertexArray and set its format.
43         VertexArray(const VertexFormat &);
44
45         /// Sets the format of the VertexArray.
46         void set_format(const VertexFormat &);
47
48         const VertexFormat &get_format() const { return format; }
49
50         /// Clears all vertices from the array.
51         void clear();
52
53         /// Reserve space for vertices.
54         void reserve(unsigned);
55
56         /// Append a new vertex at the end of the array and return its location.
57         char *append();
58
59         /// Returns the location of a vertex for modification.
60         char *modify(unsigned);
61 private:
62         virtual unsigned get_data_size() const;
63         virtual const void *get_data_pointer() const { return &data[0]; }
64
65 public:
66         unsigned size() const { return data.size()/stride; }
67         const std::vector<char> &get_data() const { return data; }
68         const char *operator[](unsigned i) const { return &data[0]+i*stride; }
69 };
70
71 } // namespace GL
72 } // namespace Msp
73
74 #endif