]> git.tdb.fi Git - libs/gl.git/blob - source/core/vertexarray.h
Check the flat qualifier from the correct member
[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.  Each vertex has one or more attributes defined by a
15 VertexFormat.
16
17 The array's contents can be modified with the append and modify methods.  To
18 obtain the location of an individual component within the vertex, use
19 VertexFormat::offset.
20
21 VertexArrayBuilder provides a convenient way of filling in vertex data.
22
23 Applications normally don't need to deal with VertexArrays directly.  They're
24 managed by the Mesh and InstanceArray classes.
25 */
26 class VertexArray: public Bufferable
27 {
28 public:
29         class Loader: public DataFile::Loader, public VertexArrayBuilder
30         {
31         public:
32                 Loader(VertexArray &);
33         };
34
35 private:
36         VertexFormat format;
37         std::vector<char> data;
38         unsigned stride = 0;
39
40 public:
41         VertexArray() = default;
42
43         /** Constructs a VertexArray and sets its format. */
44         VertexArray(const VertexFormat &);
45
46         /** Sets the format of the VertexArray.  The format cannot be changed once
47         set. */
48         void set_format(const VertexFormat &);
49
50         const VertexFormat &get_format() const { return format; }
51
52         /** Clears all vertices from the array.  Vertex format is retained. */
53         void clear();
54
55         /** Reserve space for vertices.  If n is smaller than the current size,
56         nothing is done. */
57         void reserve(std::size_t n);
58
59         /** Append a new vertex at the end of the array and return a pointer to it.
60         The array is marked as dirty. */
61         char *append();
62
63         /** Returns a pointer to a vertex for modification.  The array is marked as
64         dirty. */
65         char *modify(std::size_t);
66 private:
67         virtual std::size_t get_data_size() const;
68         virtual const void *get_data_pointer() const { return &data[0]; }
69
70 public:
71         std::size_t size() const { return data.size()/stride; }
72         const std::vector<char> &get_data() const { return data; }
73         const char *operator[](std::size_t i) const { return &data[0]+i*stride; }
74 };
75
76 } // namespace GL
77 } // namespace Msp
78
79 #endif