5 #include "ext_draw_range_elements.h"
6 #include "nv_primitive_restart.h"
7 #include "vertexarray.h"
14 void append(vector<unsigned char> &data, T i)
16 data.insert(data.end(), sizeof(T), 0);
17 *(T *)(&data[data.size()-sizeof(T)]) = i;
20 template<typename T, typename U>
23 if(!static_cast<T>(~n))
29 template<typename T, typename U>
30 void expand(vector<unsigned char> &data)
32 unsigned count = data.size()/sizeof(T);
33 data.resize(count*sizeof(U));
34 for(unsigned i=count; i--;)
35 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
38 template<typename T, typename U>
39 void shrink(vector<unsigned char> &data)
41 unsigned count = data.size()/sizeof(T);
42 for(unsigned i=0; i<count; ++i)
43 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
44 data.resize(count*sizeof(U));
52 unsigned Batch::restart_index = 0;
54 Batch::Batch(PrimitiveType t):
56 data_type(UNSIGNED_BYTE),
66 /* XXX Should probably provide a fallback to glDrawElements since this class
67 is pretty much required to render anything. */
68 static Require _req(EXT_draw_range_elements);
76 void Batch::set_data_type(DataType t)
78 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
79 throw invalid_argument("Batch::set_data_type");
80 if(t==UNSIGNED_BYTE && max_index>0xFE)
81 throw invalid_operation("Batch::set_data_type");
82 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
83 throw invalid_operation("Batch::set_data_type");
85 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
86 expand<unsigned char, unsigned short>(data);
87 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
88 expand<unsigned char, unsigned>(data);
89 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
90 expand<unsigned short, unsigned>(data);
91 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
92 shrink<unsigned, unsigned char>(data);
93 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
94 shrink<unsigned, unsigned short>(data);
95 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
96 shrink<unsigned short, unsigned char>(data);
99 update_ibuf_offsets();
103 void Batch::use_index_buffer(Buffer *buf, Batch *prev)
105 if(buf && prev && prev->ibuf!=buf)
106 throw invalid_argument("Batch::use_index_buffer");
119 prev->next_in_ibuf = this;
120 ibuf_offset = prev->ibuf_offset+prev->data.size();
128 void Batch::unlink_from_ibuf()
131 next_in_ibuf->prev_in_ibuf = prev_in_ibuf;
134 prev_in_ibuf->next_in_ibuf = next_in_ibuf;
135 prev_in_ibuf->update_ibuf_offsets();
137 else if(next_in_ibuf)
139 next_in_ibuf->ibuf_offset = 0;
140 next_in_ibuf->update_ibuf_offsets();
144 void Batch::update_ibuf_offsets()
146 for(Batch *b=this; b->next_in_ibuf; b=b->next_in_ibuf)
147 b->next_in_ibuf->ibuf_offset = b->ibuf_offset+b->data.size();
150 Batch &Batch::append(unsigned i)
153 min_index = max_index = i;
156 min_index = min(min_index, i);
157 max_index = max(max_index, i);
160 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
161 set_data_type(UNSIGNED_INT);
162 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
163 set_data_type(UNSIGNED_SHORT);
165 if(data_type==UNSIGNED_SHORT)
166 ::append<unsigned short>(data, i);
167 else if(data_type==UNSIGNED_INT)
168 ::append<unsigned>(data, i);
172 update_ibuf_offsets();
178 void Batch::append(const vector<unsigned> &ind)
184 min_index = max_index = ind.front();
186 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
188 min_index = min(min_index, *i);
189 max_index = max(max_index, *i);
192 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
193 set_data_type(UNSIGNED_INT);
194 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
195 set_data_type(UNSIGNED_SHORT);
197 unsigned base = data.size();
198 data.resize(data.size()+ind.size()*get_index_size());
199 if(data_type==UNSIGNED_SHORT)
201 unsigned short *ptr = reinterpret_cast<unsigned short *>(&data[base]);
202 for(unsigned i=0; i<ind.size(); ++i)
205 else if(data_type==UNSIGNED_INT)
207 unsigned *ptr = reinterpret_cast<unsigned *>(&data[base]);
208 for(unsigned i=0; i<ind.size(); ++i)
213 for(unsigned i=0; i<ind.size(); ++i)
214 data[base+i] = ind[i];
218 void Batch::append(const Batch &other)
220 if(other.prim_type!=prim_type)
221 throw invalid_argument("Batch::append");
222 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP)
223 throw incompatible_data("Batch::append");
224 else if(prim_type==POLYGON)
225 throw incompatible_data("Batch::append");
226 else if(prim_type==TRIANGLE_FAN)
227 static Require _req(NV_primitive_restart);
229 if(other.data.empty())
232 if(NV_primitive_restart)
235 if(data_type==UNSIGNED_SHORT)
236 ::append<unsigned short>(data, 0xFFFF);
237 else if(data_type==UNSIGNED_INT)
238 ::append<unsigned>(data, 0xFFFFFFFF);
240 data.push_back(0xFF);
242 else if(prim_type==TRIANGLE_STRIP)
244 append(get_index(size()-1));
245 append(other.get_index(0));
247 append(other.get_index(0));
249 else if(prim_type==QUAD_STRIP)
251 append(get_index(size()-1));
252 append(get_index(size()-1));
253 append(other.get_index(0));
254 append(other.get_index(0));
257 unsigned count = other.size();
258 for(unsigned i=0; i<count; ++i)
259 append(other.get_index(i));
262 unsigned Batch::get_index_size() const
264 if(data_type==UNSIGNED_SHORT)
265 return sizeof(unsigned short);
266 else if(data_type==UNSIGNED_INT)
267 return sizeof(unsigned);
268 return sizeof(unsigned char);
271 unsigned Batch::get_index(unsigned i) const
273 if(data_type==UNSIGNED_SHORT)
274 return *(unsigned short *)&data[i*sizeof(unsigned short)];
275 else if(data_type==UNSIGNED_INT)
276 return *(unsigned *)&data[i*sizeof(unsigned )];
281 void Batch::draw() const
286 if(data_type==UNSIGNED_SHORT)
288 else if(data_type==UNSIGNED_INT)
293 if(index!=restart_index)
296 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
297 glPrimitiveRestartIndexNV(index);
298 restart_index = index;
301 else if(restart_index && restart_index<max_index)
303 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
311 const Batch *b = this;
312 for(; b->prev_in_ibuf; b=b->prev_in_ibuf) ;
314 unsigned chain_size = 0;
315 for(const Batch *a=b; a; a=a->next_in_ibuf)
316 chain_size += a->data.size();
318 ibuf->data(chain_size, 0);
320 for(; b; b=b->next_in_ibuf)
322 ibuf->sub_data(b->ibuf_offset, b->data.size(), &b->data[0]);
327 BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*ibuf);
328 Bind bind_ibuf(alias, true);
330 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast<void *>(ibuf_offset));
333 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
337 Batch::Loader::Loader(Batch &b):
338 DataFile::ObjectLoader<Batch>(b)
340 add("indices", &Loader::indices);
343 void Batch::Loader::indices(const vector<unsigned> &ind)