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)
122 data.reserve(data.size()+ind.size()*get_index_size());
123 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
127 void Batch::append(const Batch &other)
129 if(other.prim_type!=prim_type)
130 throw InvalidParameterValue("Can't concatenate batches with different primitive types");
131 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP)
132 throw InvalidState("Can't concatenate line strips or loops");
133 else if(prim_type==POLYGON)
134 throw InvalidState("Can't concatenate polygons");
135 else if(prim_type==TRIANGLE_FAN)
136 static RequireExtension _ext("GL_NV_primitive_restart");
138 if(other.data.empty())
141 if(is_supported("GL_NV_primitive_restart"))
144 if(data_type==UNSIGNED_SHORT)
145 append_index<unsigned short>(0xFFFF);
146 else if(data_type==UNSIGNED_INT)
147 append_index<unsigned>(0xFFFFFFFF);
149 data.push_back(0xFF);
151 else if(prim_type==TRIANGLE_STRIP)
153 append(get_index(size()-1));
154 append(other.get_index(0));
156 append(other.get_index(0));
158 else if(prim_type==QUAD_STRIP)
160 append(get_index(size()-1));
161 append(get_index(size()-1));
162 append(other.get_index(0));
163 append(other.get_index(0));
166 unsigned count = other.size();
167 for(unsigned i=0; i<count; ++i)
168 append(other.get_index(i));
171 void Batch::draw() const
176 if(data_type==UNSIGNED_SHORT)
178 else if(data_type==UNSIGNED_INT)
183 if(index!=restart_index)
186 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
187 glPrimitiveRestartIndexNV(index);
188 restart_index = index;
191 else if(restart_index && restart_index<max_index)
193 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
201 const Batch *b = this;
202 for(; b->prev_in_ibuf; b=b->prev_in_ibuf) ;
204 unsigned chain_size = 0;
205 for(const Batch *a=b; a; a=a->next_in_ibuf)
206 chain_size += a->data.size();
208 ibuf->data(chain_size, 0);
210 for(; b; b=b->next_in_ibuf)
212 ibuf->sub_data(b->ibuf_offset, b->data.size(), &b->data[0]);
217 BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*ibuf);
218 Bind bind_ibuf(alias, true);
220 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, (void *)ibuf_offset);
223 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
226 unsigned Batch::get_index_size() const
228 if(data_type==UNSIGNED_SHORT)
229 return sizeof(unsigned short);
230 else if(data_type==UNSIGNED_INT)
231 return sizeof(unsigned);
232 return sizeof(unsigned char);
236 void Batch::append_index(T i)
238 data.insert(data.end(), sizeof(T), 0);
239 *(T *)(&data[data.size()-sizeof(T)]) = i;
242 unsigned Batch::get_index(unsigned i) const
244 if(data_type==UNSIGNED_SHORT)
245 return *(unsigned short *)&data[i*sizeof(unsigned short)];
246 else if(data_type==UNSIGNED_INT)
247 return *(unsigned *)&data[i*sizeof(unsigned )];
252 template<typename T, typename U>
253 void Batch::expand_data()
255 unsigned count = data.size()/sizeof(T);
256 data.resize(count*sizeof(U));
257 for(unsigned i=count; i--;)
258 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
261 template<typename T, typename U>
262 void Batch::shrink_data()
264 unsigned count = data.size()/sizeof(T);
265 for(unsigned i=0; i<count; ++i)
266 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
267 data.resize(count*sizeof(U));
270 template<typename T, typename U>
271 U Batch::convert(T i) const
273 if(!static_cast<T>(~i))
279 void Batch::unlink_from_ibuf()
282 next_in_ibuf->prev_in_ibuf = prev_in_ibuf;
285 prev_in_ibuf->next_in_ibuf = next_in_ibuf;
286 prev_in_ibuf->update_ibuf_offsets();
288 else if(next_in_ibuf)
290 next_in_ibuf->ibuf_offset = 0;
291 next_in_ibuf->update_ibuf_offsets();
295 void Batch::update_ibuf_offsets()
297 for(Batch *b=this; b->next_in_ibuf; b=b->next_in_ibuf)
298 b->next_in_ibuf->ibuf_offset = b->ibuf_offset+b->data.size();
302 Batch::Loader::Loader(Batch &b):
303 DataFile::ObjectLoader<Batch>(b)
305 add("indices", &Loader::indices);
308 void Batch::Loader::indices(const vector<unsigned> &ind)