6 #include "nv_primitive_restart.h"
7 #include "vertexarray.h"
14 unsigned Batch::restart_index = 0;
16 Batch::Batch(PrimitiveType t):
18 data_type(UNSIGNED_BYTE),
34 void Batch::set_data_type(DataType t)
36 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
37 throw invalid_argument("Batch::set_data_type");
38 if(t==UNSIGNED_BYTE && max_index>0xFE)
39 throw invalid_operation("Batch::set_data_type");
40 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
41 throw invalid_operation("Batch::set_data_type");
43 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
44 expand_data<unsigned char, unsigned short>();
45 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
46 expand_data<unsigned char, unsigned>();
47 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
48 expand_data<unsigned short, unsigned>();
49 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
50 shrink_data<unsigned, unsigned char>();
51 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
52 shrink_data<unsigned, unsigned short>();
53 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
54 shrink_data<unsigned short, unsigned char>();
57 update_ibuf_offsets();
61 void Batch::use_index_buffer(Buffer *buf, Batch *prev)
63 if(buf && prev && prev->ibuf!=buf)
64 throw invalid_argument("Batch::use_index_buffer");
77 prev->next_in_ibuf = this;
78 ibuf_offset = prev->ibuf_offset+prev->data.size();
86 Batch &Batch::append(unsigned i)
89 min_index = max_index = i;
92 min_index = min(min_index, i);
93 max_index = max(max_index, i);
96 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
97 set_data_type(UNSIGNED_INT);
98 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
99 set_data_type(UNSIGNED_SHORT);
101 if(data_type==UNSIGNED_SHORT)
102 append_index<unsigned short>(i);
103 else if(data_type==UNSIGNED_INT)
104 append_index<unsigned>(i);
108 update_ibuf_offsets();
114 void Batch::append(const vector<unsigned> &ind)
120 min_index = max_index = ind.front();
122 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
124 min_index = min(min_index, *i);
125 max_index = max(max_index, *i);
128 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
129 set_data_type(UNSIGNED_INT);
130 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
131 set_data_type(UNSIGNED_SHORT);
133 unsigned base = data.size();
134 data.resize(data.size()+ind.size()*get_index_size());
135 if(data_type==UNSIGNED_SHORT)
137 unsigned short *ptr = reinterpret_cast<unsigned short *>(&data[base]);
138 for(unsigned i=0; i<ind.size(); ++i)
141 else if(data_type==UNSIGNED_INT)
143 unsigned *ptr = reinterpret_cast<unsigned *>(&data[base]);
144 for(unsigned i=0; i<ind.size(); ++i)
149 for(unsigned i=0; i<ind.size(); ++i)
150 data[base+i] = ind[i];
154 void Batch::append(const Batch &other)
156 if(other.prim_type!=prim_type)
157 throw invalid_argument("Batch::append");
158 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP)
159 throw incompatible_data("Batch::append");
160 else if(prim_type==POLYGON)
161 throw incompatible_data("Batch::append");
162 else if(prim_type==TRIANGLE_FAN)
163 static RequireExtension _ext("GL_NV_primitive_restart");
165 if(other.data.empty())
168 if(is_supported("GL_NV_primitive_restart"))
171 if(data_type==UNSIGNED_SHORT)
172 append_index<unsigned short>(0xFFFF);
173 else if(data_type==UNSIGNED_INT)
174 append_index<unsigned>(0xFFFFFFFF);
176 data.push_back(0xFF);
178 else if(prim_type==TRIANGLE_STRIP)
180 append(get_index(size()-1));
181 append(other.get_index(0));
183 append(other.get_index(0));
185 else if(prim_type==QUAD_STRIP)
187 append(get_index(size()-1));
188 append(get_index(size()-1));
189 append(other.get_index(0));
190 append(other.get_index(0));
193 unsigned count = other.size();
194 for(unsigned i=0; i<count; ++i)
195 append(other.get_index(i));
198 void Batch::draw() const
203 if(data_type==UNSIGNED_SHORT)
205 else if(data_type==UNSIGNED_INT)
210 if(index!=restart_index)
213 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
214 glPrimitiveRestartIndexNV(index);
215 restart_index = index;
218 else if(restart_index && restart_index<max_index)
220 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
228 const Batch *b = this;
229 for(; b->prev_in_ibuf; b=b->prev_in_ibuf) ;
231 unsigned chain_size = 0;
232 for(const Batch *a=b; a; a=a->next_in_ibuf)
233 chain_size += a->data.size();
235 ibuf->data(chain_size, 0);
237 for(; b; b=b->next_in_ibuf)
239 ibuf->sub_data(b->ibuf_offset, b->data.size(), &b->data[0]);
244 BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*ibuf);
245 Bind bind_ibuf(alias, true);
247 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast<void *>(ibuf_offset));
250 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
253 unsigned Batch::get_index_size() const
255 if(data_type==UNSIGNED_SHORT)
256 return sizeof(unsigned short);
257 else if(data_type==UNSIGNED_INT)
258 return sizeof(unsigned);
259 return sizeof(unsigned char);
263 void Batch::append_index(T i)
265 data.insert(data.end(), sizeof(T), 0);
266 *(T *)(&data[data.size()-sizeof(T)]) = i;
269 unsigned Batch::get_index(unsigned i) const
271 if(data_type==UNSIGNED_SHORT)
272 return *(unsigned short *)&data[i*sizeof(unsigned short)];
273 else if(data_type==UNSIGNED_INT)
274 return *(unsigned *)&data[i*sizeof(unsigned )];
279 template<typename T, typename U>
280 void Batch::expand_data()
282 unsigned count = data.size()/sizeof(T);
283 data.resize(count*sizeof(U));
284 for(unsigned i=count; i--;)
285 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
288 template<typename T, typename U>
289 void Batch::shrink_data()
291 unsigned count = data.size()/sizeof(T);
292 for(unsigned i=0; i<count; ++i)
293 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
294 data.resize(count*sizeof(U));
297 template<typename T, typename U>
298 U Batch::convert(T i) const
300 if(!static_cast<T>(~i))
306 void Batch::unlink_from_ibuf()
309 next_in_ibuf->prev_in_ibuf = prev_in_ibuf;
312 prev_in_ibuf->next_in_ibuf = next_in_ibuf;
313 prev_in_ibuf->update_ibuf_offsets();
315 else if(next_in_ibuf)
317 next_in_ibuf->ibuf_offset = 0;
318 next_in_ibuf->update_ibuf_offsets();
322 void Batch::update_ibuf_offsets()
324 for(Batch *b=this; b->next_in_ibuf; b=b->next_in_ibuf)
325 b->next_in_ibuf->ibuf_offset = b->ibuf_offset+b->data.size();
329 Batch::Loader::Loader(Batch &b):
330 DataFile::ObjectLoader<Batch>(b)
332 add("indices", &Loader::indices);
335 void Batch::Loader::indices(const vector<unsigned> &ind)