]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarray.h
Restore old buffer binding when setting data
[libs/gl.git] / source / vertexarray.h
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007-2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_GL_VERTEXARRAY_H_
9 #define MSP_GL_VERTEXARRAY_H_
10
11 #include <climits>
12 #include <vector>
13 #include <msp/core/refptr.h>
14 #include <msp/datafile/loader.h>
15 #include "datatype.h"
16 #include "primitivetype.h"
17 #include "vertexarraybuilder.h"
18 #include "vertexformat.h"
19
20 namespace Msp {
21 namespace GL {
22
23 class Buffer;
24
25 class VertexArray
26 {
27 public:
28         class Loader: public DataFile::Loader, public VertexArrayBuilder
29         {
30         public:
31                 Loader(VertexArray &);
32         };
33
34 private:
35         struct ArrayMask
36         {
37                 enum
38                 {
39                         B = (sizeof(unsigned)*CHAR_BIT),
40                         N = (63+B)/B
41                 };
42
43                 unsigned mask[N];
44
45                 ArrayMask();
46
47                 void set(unsigned);
48                 bool is_set(unsigned) const;
49         };
50
51         VertexFormat format;
52         std::vector<float> data;
53         unsigned stride;
54         RefPtr<Buffer> vbuf;
55         bool defer_vbuf;
56         mutable bool dirty;
57
58         static ArrayMask enabled_arrays;
59
60         VertexArray(const VertexArray &);
61         VertexArray &operator=(const VertexArray &);
62 public:
63         VertexArray(const VertexFormat &);
64         ~VertexArray();
65
66         const VertexFormat &get_format() const { return format; }
67         const std::vector<float> &get_data() const { return data; }
68         void         use_vertex_buffer();
69         void         use_vertex_buffer(Buffer *);
70         void         reserve(unsigned);
71         unsigned     size() const { return data.size()/stride; }
72         void         clear();
73         void         reset(const VertexFormat &);
74         void         apply() const;
75         float        *append();
76         float *modify(unsigned);
77         float *operator[](unsigned i) { return modify(i); }
78         const float  *operator[](unsigned i) const { return &data[0]+i*stride; }
79
80 private:
81         void set_dirty();
82 };
83
84 void array_element(int);
85 void draw_arrays(PrimitiveType, int, unsigned);
86 void draw_elements(PrimitiveType, unsigned, DataType, const void *);
87 void draw_range_elements(PrimitiveType, unsigned, unsigned, unsigned, DataType, const void *);
88
89 inline void draw_elements(PrimitiveType mode, unsigned count, const unsigned *indices)
90 { draw_elements(mode, count, UNSIGNED_INT, indices); }
91
92 inline void draw_elements(PrimitiveType mode, unsigned count, const unsigned short *indices)
93 { draw_elements(mode, count, UNSIGNED_SHORT, indices); }
94
95 inline void draw_range_elements(PrimitiveType mode, unsigned low, unsigned high, unsigned count, const unsigned short *indices)
96 { draw_range_elements(mode, low, high, count, UNSIGNED_SHORT, indices); }
97
98 inline void draw_range_elements(PrimitiveType mode, unsigned low, unsigned high, unsigned count, const unsigned *indices)
99 { draw_range_elements(mode, low, high, count, UNSIGNED_INT, indices); }
100
101 } // namespace GL
102 } // namespace Msp
103
104 #endif