]> git.tdb.fi Git - libs/gl.git/blob - source/batch.h
696891ba3047ce1a557c9f36ddeee64b82c17882
[libs/gl.git] / source / batch.h
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007, 2009-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_GL_BATCH_H_
9 #define MSP_GL_BATCH_H_
10
11 #include <vector>
12 #include <msp/datafile/objectloader.h>
13 #include "datatype.h"
14 #include "primitivetype.h"
15
16 namespace Msp {
17 namespace GL {
18
19 class Buffer;
20
21 /**
22 Stores primitive type and element indices for a single GL draw call.  Data
23 type for indices is automatically chosen to accommodate the largest index in
24 the Batch.
25
26 This is a pretty low-level class and mainly intended to be used by the Mesh
27 class.
28 */
29 class Batch
30 {
31 public:
32         class Loader: public DataFile::ObjectLoader<Batch>
33         {
34         public:
35                 Loader(Batch &);
36         private:
37                 void indices(const std::vector<unsigned> &);
38         };
39
40 private:
41         PrimitiveType prim_type;
42         DataType data_type;
43         std::vector<unsigned char> data;
44         unsigned min_index;
45         unsigned max_index;
46         bool restart;
47         Buffer *ibuf;
48         unsigned ibuf_offset;
49         Batch *next_in_ibuf;
50         Batch *prev_in_ibuf;
51         mutable bool dirty;
52
53         static unsigned restart_index;
54
55 public:
56         Batch(PrimitiveType t);
57         ~Batch();
58
59         PrimitiveType get_type() const { return prim_type; }
60         void set_data_type(DataType);
61         DataType get_data_type() const { return data_type; }
62         void use_index_buffer(Buffer *, Batch * = 0);
63         Batch &append(unsigned);
64         void append(const std::vector<unsigned> &);
65         void append(const Batch &);
66         unsigned size() const { return data.size()/get_index_size(); }
67         unsigned get_index(unsigned) const;
68         void draw() const;
69
70 private:
71         unsigned get_index_size() const;
72
73         template<typename T>
74         void append_index(T);
75
76         template<typename T, typename U>
77         void expand_data();
78
79         template<typename T, typename U>
80         void shrink_data();
81
82         template<typename T, typename U>
83         U convert(T) const;
84
85         void unlink_from_ibuf();
86         void update_ibuf_offsets();
87 };
88
89 } // namespace GL
90 } // namespace Msp
91
92 #endif