6 #include "nv_primitive_restart.h"
7 #include "vertexarray.h"
8 #include "version_1_2.h"
15 unsigned Batch::restart_index = 0;
17 Batch::Batch(PrimitiveType t):
19 data_type(UNSIGNED_BYTE),
29 /* XXX Should probably provide a fallback to glDrawElements since this class
30 is pretty much required to render anything. */
31 static RequireVersion _ver(1, 2);
39 void Batch::set_data_type(DataType t)
41 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
42 throw invalid_argument("Batch::set_data_type");
43 if(t==UNSIGNED_BYTE && max_index>0xFE)
44 throw invalid_operation("Batch::set_data_type");
45 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
46 throw invalid_operation("Batch::set_data_type");
48 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
49 expand_data<unsigned char, unsigned short>();
50 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
51 expand_data<unsigned char, unsigned>();
52 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
53 expand_data<unsigned short, unsigned>();
54 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
55 shrink_data<unsigned, unsigned char>();
56 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
57 shrink_data<unsigned, unsigned short>();
58 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
59 shrink_data<unsigned short, unsigned char>();
62 update_ibuf_offsets();
66 void Batch::use_index_buffer(Buffer *buf, Batch *prev)
68 if(buf && prev && prev->ibuf!=buf)
69 throw invalid_argument("Batch::use_index_buffer");
82 prev->next_in_ibuf = this;
83 ibuf_offset = prev->ibuf_offset+prev->data.size();
91 Batch &Batch::append(unsigned i)
94 min_index = max_index = i;
97 min_index = min(min_index, i);
98 max_index = max(max_index, i);
101 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
102 set_data_type(UNSIGNED_INT);
103 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
104 set_data_type(UNSIGNED_SHORT);
106 if(data_type==UNSIGNED_SHORT)
107 append_index<unsigned short>(i);
108 else if(data_type==UNSIGNED_INT)
109 append_index<unsigned>(i);
113 update_ibuf_offsets();
119 void Batch::append(const vector<unsigned> &ind)
125 min_index = max_index = ind.front();
127 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
129 min_index = min(min_index, *i);
130 max_index = max(max_index, *i);
133 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
134 set_data_type(UNSIGNED_INT);
135 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
136 set_data_type(UNSIGNED_SHORT);
138 unsigned base = data.size();
139 data.resize(data.size()+ind.size()*get_index_size());
140 if(data_type==UNSIGNED_SHORT)
142 unsigned short *ptr = reinterpret_cast<unsigned short *>(&data[base]);
143 for(unsigned i=0; i<ind.size(); ++i)
146 else if(data_type==UNSIGNED_INT)
148 unsigned *ptr = reinterpret_cast<unsigned *>(&data[base]);
149 for(unsigned i=0; i<ind.size(); ++i)
154 for(unsigned i=0; i<ind.size(); ++i)
155 data[base+i] = ind[i];
159 void Batch::append(const Batch &other)
161 if(other.prim_type!=prim_type)
162 throw invalid_argument("Batch::append");
163 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP)
164 throw incompatible_data("Batch::append");
165 else if(prim_type==POLYGON)
166 throw incompatible_data("Batch::append");
167 else if(prim_type==TRIANGLE_FAN)
168 static RequireExtension _ext("GL_NV_primitive_restart");
170 if(other.data.empty())
173 if(is_supported("GL_NV_primitive_restart"))
176 if(data_type==UNSIGNED_SHORT)
177 append_index<unsigned short>(0xFFFF);
178 else if(data_type==UNSIGNED_INT)
179 append_index<unsigned>(0xFFFFFFFF);
181 data.push_back(0xFF);
183 else if(prim_type==TRIANGLE_STRIP)
185 append(get_index(size()-1));
186 append(other.get_index(0));
188 append(other.get_index(0));
190 else if(prim_type==QUAD_STRIP)
192 append(get_index(size()-1));
193 append(get_index(size()-1));
194 append(other.get_index(0));
195 append(other.get_index(0));
198 unsigned count = other.size();
199 for(unsigned i=0; i<count; ++i)
200 append(other.get_index(i));
203 void Batch::draw() const
208 if(data_type==UNSIGNED_SHORT)
210 else if(data_type==UNSIGNED_INT)
215 if(index!=restart_index)
218 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
219 glPrimitiveRestartIndexNV(index);
220 restart_index = index;
223 else if(restart_index && restart_index<max_index)
225 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
233 const Batch *b = this;
234 for(; b->prev_in_ibuf; b=b->prev_in_ibuf) ;
236 unsigned chain_size = 0;
237 for(const Batch *a=b; a; a=a->next_in_ibuf)
238 chain_size += a->data.size();
240 ibuf->data(chain_size, 0);
242 for(; b; b=b->next_in_ibuf)
244 ibuf->sub_data(b->ibuf_offset, b->data.size(), &b->data[0]);
249 BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*ibuf);
250 Bind bind_ibuf(alias, true);
252 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast<void *>(ibuf_offset));
255 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
258 unsigned Batch::get_index_size() const
260 if(data_type==UNSIGNED_SHORT)
261 return sizeof(unsigned short);
262 else if(data_type==UNSIGNED_INT)
263 return sizeof(unsigned);
264 return sizeof(unsigned char);
268 void Batch::append_index(T i)
270 data.insert(data.end(), sizeof(T), 0);
271 *(T *)(&data[data.size()-sizeof(T)]) = i;
274 unsigned Batch::get_index(unsigned i) const
276 if(data_type==UNSIGNED_SHORT)
277 return *(unsigned short *)&data[i*sizeof(unsigned short)];
278 else if(data_type==UNSIGNED_INT)
279 return *(unsigned *)&data[i*sizeof(unsigned )];
284 template<typename T, typename U>
285 void Batch::expand_data()
287 unsigned count = data.size()/sizeof(T);
288 data.resize(count*sizeof(U));
289 for(unsigned i=count; i--;)
290 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
293 template<typename T, typename U>
294 void Batch::shrink_data()
296 unsigned count = data.size()/sizeof(T);
297 for(unsigned i=0; i<count; ++i)
298 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
299 data.resize(count*sizeof(U));
302 template<typename T, typename U>
303 U Batch::convert(T i) const
305 if(!static_cast<T>(~i))
311 void Batch::unlink_from_ibuf()
314 next_in_ibuf->prev_in_ibuf = prev_in_ibuf;
317 prev_in_ibuf->next_in_ibuf = next_in_ibuf;
318 prev_in_ibuf->update_ibuf_offsets();
320 else if(next_in_ibuf)
322 next_in_ibuf->ibuf_offset = 0;
323 next_in_ibuf->update_ibuf_offsets();
327 void Batch::update_ibuf_offsets()
329 for(Batch *b=this; b->next_in_ibuf; b=b->next_in_ibuf)
330 b->next_in_ibuf->ibuf_offset = b->ibuf_offset+b->data.size();
334 Batch::Loader::Loader(Batch &b):
335 DataFile::ObjectLoader<Batch>(b)
337 add("indices", &Loader::indices);
340 void Batch::Loader::indices(const vector<unsigned> &ind)