]> git.tdb.fi Git - libs/gl.git/blob - source/vertexarray.h
Support multiple sets of texture coordinates in VertexArrays
[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         Buffer *vbuf;
55         bool own_vbuf;
56
57         VertexArray(const VertexArray &);
58         VertexArray &operator=(const VertexArray &);
59 public:
60         VertexArray(const VertexFormat &);
61         ~VertexArray();
62
63         const VertexFormat &get_format() const { return format; }
64         const std::vector<float> &get_data() const { return data; }
65         void         use_vertex_buffer();
66         void         use_vertex_buffer(Buffer *);
67         void         reserve(unsigned);
68         unsigned     size() const { return data.size()/stride; }
69         void         clear();
70         void         reset(const VertexFormat &);
71         void         apply() const;
72         void         update_data();
73         float        *append();
74         float        *operator[](unsigned i) { return &data[0]+i*stride; }
75         const float  *operator[](unsigned i) const { return &data[0]+i*stride; }
76
77 private:
78         static ArrayMask enabled_arrays;
79 };
80
81 void array_element(int);
82 void draw_arrays(PrimitiveType, int, unsigned);
83 void draw_elements(PrimitiveType, unsigned, DataType, const void *);
84 void draw_range_elements(PrimitiveType, unsigned, unsigned, unsigned, DataType, const void *);
85
86 inline void draw_elements(PrimitiveType mode, unsigned count, const unsigned *indices)
87 { draw_elements(mode, count, UNSIGNED_INT, indices); }
88
89 inline void draw_elements(PrimitiveType mode, unsigned count, const unsigned short *indices)
90 { draw_elements(mode, count, UNSIGNED_SHORT, indices); }
91
92 inline void draw_range_elements(PrimitiveType mode, unsigned low, unsigned high, unsigned count, const unsigned short *indices)
93 { draw_range_elements(mode, low, high, count, UNSIGNED_SHORT, indices); }
94
95 inline void draw_range_elements(PrimitiveType mode, unsigned low, unsigned high, unsigned count, const unsigned *indices)
96 { draw_range_elements(mode, low, high, count, UNSIGNED_INT, indices); }
97
98 } // namespace GL
99 } // namespace Msp
100
101 #endif