5 #include "nv_primitive_restart.h"
6 #include "vertexarray.h"
13 unsigned Batch::restart_index = 0;
15 Batch::Batch(PrimitiveType t):
17 data_type(UNSIGNED_BYTE),
33 void Batch::set_data_type(DataType t)
35 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
36 throw InvalidParameterValue("Batch data type must be an unsigned integer");
37 if(t==UNSIGNED_BYTE && max_index>0xFE)
38 throw InvalidState("UNSIGNED_BYTE can't hold all indices in Batch");
39 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
40 throw InvalidState("UNSIGNED_SHORT can't hold all indices in Batch");
42 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
43 expand_data<unsigned char, unsigned short>();
44 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
45 expand_data<unsigned char, unsigned>();
46 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
47 expand_data<unsigned short, unsigned>();
48 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
49 shrink_data<unsigned, unsigned char>();
50 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
51 shrink_data<unsigned, unsigned short>();
52 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
53 shrink_data<unsigned short, unsigned char>();
56 update_ibuf_offsets();
60 void Batch::use_index_buffer(Buffer *buf, Batch *prev)
62 if(buf && prev && prev->ibuf!=buf)
63 throw InvalidParameterValue("Previous batch is not in the same buffer");
76 prev->next_in_ibuf = this;
77 ibuf_offset = prev->ibuf_offset+prev->data.size();
85 Batch &Batch::append(unsigned i)
88 min_index = max_index = i;
91 min_index = min(min_index, i);
92 max_index = max(max_index, i);
95 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
96 set_data_type(UNSIGNED_INT);
97 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
98 set_data_type(UNSIGNED_SHORT);
100 if(data_type==UNSIGNED_SHORT)
101 append_index<unsigned short>(i);
102 else if(data_type==UNSIGNED_INT)
103 append_index<unsigned>(i);
107 update_ibuf_offsets();
113 void Batch::append(const vector<unsigned> &ind)
119 min_index = max_index = ind.front();
121 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
123 min_index = min(min_index, *i);
124 max_index = max(max_index, *i);
127 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
128 set_data_type(UNSIGNED_INT);
129 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
130 set_data_type(UNSIGNED_SHORT);
132 unsigned base = data.size();
133 data.resize(data.size()+ind.size()*get_index_size());
134 if(data_type==UNSIGNED_SHORT)
136 unsigned short *ptr = reinterpret_cast<unsigned short *>(&data[base]);
137 for(unsigned i=0; i<ind.size(); ++i)
140 else if(data_type==UNSIGNED_INT)
142 unsigned *ptr = reinterpret_cast<unsigned *>(&data[base]);
143 for(unsigned i=0; i<ind.size(); ++i)
148 for(unsigned i=0; i<ind.size(); ++i)
149 data[base+i] = ind[i];
153 void Batch::append(const Batch &other)
155 if(other.prim_type!=prim_type)
156 throw InvalidParameterValue("Can't concatenate batches with different primitive types");
157 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP)
158 throw InvalidState("Can't concatenate line strips or loops");
159 else if(prim_type==POLYGON)
160 throw InvalidState("Can't concatenate polygons");
161 else if(prim_type==TRIANGLE_FAN)
162 static RequireExtension _ext("GL_NV_primitive_restart");
164 if(other.data.empty())
167 if(is_supported("GL_NV_primitive_restart"))
170 if(data_type==UNSIGNED_SHORT)
171 append_index<unsigned short>(0xFFFF);
172 else if(data_type==UNSIGNED_INT)
173 append_index<unsigned>(0xFFFFFFFF);
175 data.push_back(0xFF);
177 else if(prim_type==TRIANGLE_STRIP)
179 append(get_index(size()-1));
180 append(other.get_index(0));
182 append(other.get_index(0));
184 else if(prim_type==QUAD_STRIP)
186 append(get_index(size()-1));
187 append(get_index(size()-1));
188 append(other.get_index(0));
189 append(other.get_index(0));
192 unsigned count = other.size();
193 for(unsigned i=0; i<count; ++i)
194 append(other.get_index(i));
197 void Batch::draw() const
202 if(data_type==UNSIGNED_SHORT)
204 else if(data_type==UNSIGNED_INT)
209 if(index!=restart_index)
212 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
213 glPrimitiveRestartIndexNV(index);
214 restart_index = index;
217 else if(restart_index && restart_index<max_index)
219 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
227 const Batch *b = this;
228 for(; b->prev_in_ibuf; b=b->prev_in_ibuf) ;
230 unsigned chain_size = 0;
231 for(const Batch *a=b; a; a=a->next_in_ibuf)
232 chain_size += a->data.size();
234 ibuf->data(chain_size, 0);
236 for(; b; b=b->next_in_ibuf)
238 ibuf->sub_data(b->ibuf_offset, b->data.size(), &b->data[0]);
243 BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*ibuf);
244 Bind bind_ibuf(alias, true);
246 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, (void *)ibuf_offset);
249 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
252 unsigned Batch::get_index_size() const
254 if(data_type==UNSIGNED_SHORT)
255 return sizeof(unsigned short);
256 else if(data_type==UNSIGNED_INT)
257 return sizeof(unsigned);
258 return sizeof(unsigned char);
262 void Batch::append_index(T i)
264 data.insert(data.end(), sizeof(T), 0);
265 *(T *)(&data[data.size()-sizeof(T)]) = i;
268 unsigned Batch::get_index(unsigned i) const
270 if(data_type==UNSIGNED_SHORT)
271 return *(unsigned short *)&data[i*sizeof(unsigned short)];
272 else if(data_type==UNSIGNED_INT)
273 return *(unsigned *)&data[i*sizeof(unsigned )];
278 template<typename T, typename U>
279 void Batch::expand_data()
281 unsigned count = data.size()/sizeof(T);
282 data.resize(count*sizeof(U));
283 for(unsigned i=count; i--;)
284 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
287 template<typename T, typename U>
288 void Batch::shrink_data()
290 unsigned count = data.size()/sizeof(T);
291 for(unsigned i=0; i<count; ++i)
292 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
293 data.resize(count*sizeof(U));
296 template<typename T, typename U>
297 U Batch::convert(T i) const
299 if(!static_cast<T>(~i))
305 void Batch::unlink_from_ibuf()
308 next_in_ibuf->prev_in_ibuf = prev_in_ibuf;
311 prev_in_ibuf->next_in_ibuf = next_in_ibuf;
312 prev_in_ibuf->update_ibuf_offsets();
314 else if(next_in_ibuf)
316 next_in_ibuf->ibuf_offset = 0;
317 next_in_ibuf->update_ibuf_offsets();
321 void Batch::update_ibuf_offsets()
323 for(Batch *b=this; b->next_in_ibuf; b=b->next_in_ibuf)
324 b->next_in_ibuf->ibuf_offset = b->ibuf_offset+b->data.size();
328 Batch::Loader::Loader(Batch &b):
329 DataFile::ObjectLoader<Batch>(b)
331 add("indices", &Loader::indices);
334 void Batch::Loader::indices(const vector<unsigned> &ind)