1 #include <msp/gl/extensions/ext_draw_range_elements.h>
2 #include <msp/gl/extensions/nv_primitive_restart.h>
8 #include "vertexarray.h"
15 void append(vector<unsigned char> &data, T i)
17 data.insert(data.end(), sizeof(T), 0);
18 *(T *)(&data[data.size()-sizeof(T)]) = i;
21 template<typename T, typename U>
24 if(!static_cast<T>(~n))
30 template<typename T, typename U>
31 void expand(vector<unsigned char> &data)
33 unsigned count = data.size()/sizeof(T);
34 data.resize(count*sizeof(U));
35 for(unsigned i=count; i--;)
36 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
39 template<typename T, typename U>
40 void shrink(vector<unsigned char> &data)
42 unsigned count = data.size()/sizeof(T);
43 for(unsigned i=0; i<count; ++i)
44 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
45 data.resize(count*sizeof(U));
53 unsigned Batch::restart_index = 0;
55 Batch::Batch(PrimitiveType t):
57 data_type(UNSIGNED_BYTE),
68 void Batch::set_data_type(DataType t)
70 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
71 throw invalid_argument("Batch::set_data_type");
72 if(t==UNSIGNED_BYTE && max_index>0xFE)
73 throw invalid_operation("Batch::set_data_type");
74 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
75 throw invalid_operation("Batch::set_data_type");
77 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
78 expand<unsigned char, unsigned short>(data);
79 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
80 expand<unsigned char, unsigned>(data);
81 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
82 expand<unsigned short, unsigned>(data);
83 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
84 shrink<unsigned, unsigned char>(data);
85 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
86 shrink<unsigned, unsigned short>(data);
87 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
88 shrink<unsigned short, unsigned char>(data);
95 Batch &Batch::append(unsigned i)
105 void Batch::append(const vector<unsigned> &ind)
110 data.reserve(data.size()+ind.size()*get_index_size());
111 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
118 bool Batch::can_append(PrimitiveType other_type)
120 if(other_type!=prim_type)
122 else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
123 return NV_primitive_restart;
128 void Batch::append(const Batch &other)
130 if(other.prim_type!=prim_type)
131 throw invalid_argument("Batch::append");
132 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
133 static Require _req(NV_primitive_restart);
135 if(other.data.empty())
138 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
140 else if(NV_primitive_restart)
143 if(data_type==UNSIGNED_SHORT)
144 ::append<unsigned short>(data, 0xFFFF);
145 else if(data_type==UNSIGNED_INT)
146 ::append<unsigned>(data, 0xFFFFFFFF);
148 data.push_back(0xFF);
150 else if(prim_type==TRIANGLE_STRIP)
152 append(get_index(size()-1));
153 append(other.get_index(0));
155 append(other.get_index(0));
157 else if(prim_type==QUAD_STRIP)
159 append(get_index(size()-1));
160 append(get_index(size()-1));
161 append(other.get_index(0));
162 append(other.get_index(0));
165 unsigned count = other.size();
166 for(unsigned i=0; i<count; ++i)
167 append_index(other.get_index(i));
173 void Batch::append_index(unsigned i)
176 min_index = max_index = i;
179 min_index = min(min_index, i);
180 max_index = max(max_index, i);
183 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
184 set_data_type(UNSIGNED_INT);
185 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
186 set_data_type(UNSIGNED_SHORT);
188 if(data_type==UNSIGNED_SHORT)
189 ::append<unsigned short>(data, i);
190 else if(data_type==UNSIGNED_INT)
191 ::append<unsigned>(data, i);
196 unsigned Batch::get_index_size() const
198 if(data_type==UNSIGNED_SHORT)
199 return sizeof(unsigned short);
200 else if(data_type==UNSIGNED_INT)
201 return sizeof(unsigned);
202 return sizeof(unsigned char);
205 unsigned Batch::get_index(unsigned i) const
207 if(data_type==UNSIGNED_SHORT)
208 return *(unsigned short *)&data[i*sizeof(unsigned short)];
209 else if(data_type==UNSIGNED_INT)
210 return *(unsigned *)&data[i*sizeof(unsigned )];
215 void Batch::draw() const
220 if(data_type==UNSIGNED_SHORT)
222 else if(data_type==UNSIGNED_INT)
227 if(index!=restart_index)
230 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
231 glPrimitiveRestartIndexNV(index);
232 restart_index = index;
235 else if(restart_index && restart_index<=max_index)
237 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
241 Buffer *ibuf = get_buffer();
242 const void *data_ptr;
243 BindRestore _bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
249 data_ptr = reinterpret_cast<const void *>(get_offset());
254 if(EXT_draw_range_elements)
255 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, data_ptr);
257 glDrawElements(prim_type, size(), data_type, data_ptr);
261 Batch::Loader::Loader(Batch &b):
262 DataFile::ObjectLoader<Batch>(b)
264 add("indices", &Loader::indices);
267 void Batch::Loader::indices(const vector<unsigned> &ind)