3 This file is part of libmspgl
4 Copyright © 2007-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
11 #include "extension.h"
12 #include "nv_primitive_restart.h"
13 #include "vertexarray.h"
20 unsigned Batch::restart_index = 0;
22 Batch::Batch(PrimitiveType t):
24 data_type(UNSIGNED_BYTE),
40 void Batch::set_data_type(DataType t)
42 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
43 throw InvalidParameterValue("Batch data type must be an unsigned integer");
44 if(t==UNSIGNED_BYTE && max_index>0xFE)
45 throw InvalidState("UNSIGNED_BYTE can't hold all indices in Batch");
46 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
47 throw InvalidState("UNSIGNED_SHORT can't hold all indices in Batch");
49 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
50 expand_data<unsigned char, unsigned short>();
51 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
52 expand_data<unsigned char, unsigned>();
53 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
54 expand_data<unsigned short, unsigned>();
55 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
56 shrink_data<unsigned, unsigned char>();
57 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
58 shrink_data<unsigned, unsigned short>();
59 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
60 shrink_data<unsigned short, unsigned char>();
63 update_ibuf_offsets();
67 void Batch::use_index_buffer(Buffer *buf, Batch *prev)
69 if(buf && prev && prev->ibuf!=buf)
70 throw InvalidParameterValue("Previous batch is not in the same buffer");
83 prev->next_in_ibuf = this;
84 ibuf_offset = prev->ibuf_offset+prev->data.size();
92 Batch &Batch::append(unsigned i)
95 min_index = max_index = i;
98 min_index = min(min_index, i);
99 max_index = max(max_index, i);
102 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
103 set_data_type(UNSIGNED_INT);
104 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
105 set_data_type(UNSIGNED_SHORT);
107 if(data_type==UNSIGNED_SHORT)
108 append_index<unsigned short>(i);
109 else if(data_type==UNSIGNED_INT)
110 append_index<unsigned>(i);
114 update_ibuf_offsets();
120 void Batch::append(const vector<unsigned> &ind)
126 min_index = max_index = ind.front();
128 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
130 min_index = min(min_index, *i);
131 max_index = max(max_index, *i);
134 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
135 set_data_type(UNSIGNED_INT);
136 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
137 set_data_type(UNSIGNED_SHORT);
139 unsigned base = data.size();
140 data.resize(data.size()+ind.size()*get_index_size());
141 if(data_type==UNSIGNED_SHORT)
143 unsigned short *ptr = reinterpret_cast<unsigned short *>(&data[base]);
144 for(unsigned i=0; i<ind.size(); ++i)
147 else if(data_type==UNSIGNED_INT)
149 unsigned *ptr = reinterpret_cast<unsigned *>(&data[base]);
150 for(unsigned i=0; i<ind.size(); ++i)
155 for(unsigned i=0; i<ind.size(); ++i)
156 data[base+i] = ind[i];
160 void Batch::append(const Batch &other)
162 if(other.prim_type!=prim_type)
163 throw InvalidParameterValue("Can't concatenate batches with different primitive types");
164 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP)
165 throw InvalidState("Can't concatenate line strips or loops");
166 else if(prim_type==POLYGON)
167 throw InvalidState("Can't concatenate polygons");
168 else if(prim_type==TRIANGLE_FAN)
169 static RequireExtension _ext("GL_NV_primitive_restart");
171 if(other.data.empty())
174 if(is_supported("GL_NV_primitive_restart"))
177 if(data_type==UNSIGNED_SHORT)
178 append_index<unsigned short>(0xFFFF);
179 else if(data_type==UNSIGNED_INT)
180 append_index<unsigned>(0xFFFFFFFF);
182 data.push_back(0xFF);
184 else if(prim_type==TRIANGLE_STRIP)
186 append(get_index(size()-1));
187 append(other.get_index(0));
189 append(other.get_index(0));
191 else if(prim_type==QUAD_STRIP)
193 append(get_index(size()-1));
194 append(get_index(size()-1));
195 append(other.get_index(0));
196 append(other.get_index(0));
199 unsigned count = other.size();
200 for(unsigned i=0; i<count; ++i)
201 append(other.get_index(i));
204 void Batch::draw() const
209 if(data_type==UNSIGNED_SHORT)
211 else if(data_type==UNSIGNED_INT)
216 if(index!=restart_index)
219 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
220 glPrimitiveRestartIndexNV(index);
221 restart_index = index;
224 else if(restart_index && restart_index<max_index)
226 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
234 const Batch *b = this;
235 for(; b->prev_in_ibuf; b=b->prev_in_ibuf) ;
237 unsigned chain_size = 0;
238 for(const Batch *a=b; a; a=a->next_in_ibuf)
239 chain_size += a->data.size();
241 ibuf->data(chain_size, 0);
243 for(; b; b=b->next_in_ibuf)
245 ibuf->sub_data(b->ibuf_offset, b->data.size(), &b->data[0]);
250 BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*ibuf);
251 Bind bind_ibuf(alias, true);
253 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, (void *)ibuf_offset);
256 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
259 unsigned Batch::get_index_size() const
261 if(data_type==UNSIGNED_SHORT)
262 return sizeof(unsigned short);
263 else if(data_type==UNSIGNED_INT)
264 return sizeof(unsigned);
265 return sizeof(unsigned char);
269 void Batch::append_index(T i)
271 data.insert(data.end(), sizeof(T), 0);
272 *(T *)(&data[data.size()-sizeof(T)]) = i;
275 unsigned Batch::get_index(unsigned i) const
277 if(data_type==UNSIGNED_SHORT)
278 return *(unsigned short *)&data[i*sizeof(unsigned short)];
279 else if(data_type==UNSIGNED_INT)
280 return *(unsigned *)&data[i*sizeof(unsigned )];
285 template<typename T, typename U>
286 void Batch::expand_data()
288 unsigned count = data.size()/sizeof(T);
289 data.resize(count*sizeof(U));
290 for(unsigned i=count; i--;)
291 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
294 template<typename T, typename U>
295 void Batch::shrink_data()
297 unsigned count = data.size()/sizeof(T);
298 for(unsigned i=0; i<count; ++i)
299 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
300 data.resize(count*sizeof(U));
303 template<typename T, typename U>
304 U Batch::convert(T i) const
306 if(!static_cast<T>(~i))
312 void Batch::unlink_from_ibuf()
315 next_in_ibuf->prev_in_ibuf = prev_in_ibuf;
318 prev_in_ibuf->next_in_ibuf = next_in_ibuf;
319 prev_in_ibuf->update_ibuf_offsets();
321 else if(next_in_ibuf)
323 next_in_ibuf->ibuf_offset = 0;
324 next_in_ibuf->update_ibuf_offsets();
328 void Batch::update_ibuf_offsets()
330 for(Batch *b=this; b->next_in_ibuf; b=b->next_in_ibuf)
331 b->next_in_ibuf->ibuf_offset = b->ibuf_offset+b->data.size();
335 Batch::Loader::Loader(Batch &b):
336 DataFile::ObjectLoader<Batch>(b)
338 add("indices", &Loader::indices);
341 void Batch::Loader::indices(const vector<unsigned> &ind)