1 #include <msp/gl/extensions/ext_draw_range_elements.h>
2 #include <msp/gl/extensions/nv_primitive_restart.h>
7 #include "vertexarray.h"
14 void append(vector<unsigned char> &data, T i)
16 data.insert(data.end(), sizeof(T), 0);
17 *(T *)(&data[data.size()-sizeof(T)]) = i;
20 template<typename T, typename U>
23 if(!static_cast<T>(~n))
29 template<typename T, typename U>
30 void expand(vector<unsigned char> &data)
32 unsigned count = data.size()/sizeof(T);
33 data.resize(count*sizeof(U));
34 for(unsigned i=count; i--;)
35 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
38 template<typename T, typename U>
39 void shrink(vector<unsigned char> &data)
41 unsigned count = data.size()/sizeof(T);
42 for(unsigned i=0; i<count; ++i)
43 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
44 data.resize(count*sizeof(U));
52 unsigned Batch::restart_index = 0;
54 Batch::Batch(PrimitiveType t):
56 data_type(UNSIGNED_BYTE),
61 /* XXX Should probably provide a fallback to glDrawElements since this class
62 is pretty much required to render anything. */
63 static Require _req(EXT_draw_range_elements);
70 void Batch::set_data_type(DataType t)
72 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
73 throw invalid_argument("Batch::set_data_type");
74 if(t==UNSIGNED_BYTE && max_index>0xFE)
75 throw invalid_operation("Batch::set_data_type");
76 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
77 throw invalid_operation("Batch::set_data_type");
79 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
80 expand<unsigned char, unsigned short>(data);
81 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
82 expand<unsigned char, unsigned>(data);
83 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
84 expand<unsigned short, unsigned>(data);
85 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
86 shrink<unsigned, unsigned char>(data);
87 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
88 shrink<unsigned, unsigned short>(data);
89 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
90 shrink<unsigned short, unsigned char>(data);
97 Batch &Batch::append(unsigned i)
107 void Batch::append(const vector<unsigned> &ind)
112 data.reserve(data.size()+ind.size()*get_index_size());
113 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
120 bool Batch::can_append(PrimitiveType other_type)
122 if(other_type!=prim_type)
124 else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN || prim_type==POLYGON)
125 return NV_primitive_restart;
130 void Batch::append(const Batch &other)
132 if(other.prim_type!=prim_type)
133 throw invalid_argument("Batch::append");
134 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN || prim_type==POLYGON)
135 static Require _req(NV_primitive_restart);
137 if(other.data.empty())
140 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
142 else if(NV_primitive_restart)
145 if(data_type==UNSIGNED_SHORT)
146 ::append<unsigned short>(data, 0xFFFF);
147 else if(data_type==UNSIGNED_INT)
148 ::append<unsigned>(data, 0xFFFFFFFF);
150 data.push_back(0xFF);
152 else if(prim_type==TRIANGLE_STRIP)
154 append(get_index(size()-1));
155 append(other.get_index(0));
157 append(other.get_index(0));
159 else if(prim_type==QUAD_STRIP)
161 append(get_index(size()-1));
162 append(get_index(size()-1));
163 append(other.get_index(0));
164 append(other.get_index(0));
167 unsigned count = other.size();
168 for(unsigned i=0; i<count; ++i)
169 append_index(other.get_index(i));
175 void Batch::append_index(unsigned i)
178 min_index = max_index = i;
181 min_index = min(min_index, i);
182 max_index = max(max_index, i);
185 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
186 set_data_type(UNSIGNED_INT);
187 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
188 set_data_type(UNSIGNED_SHORT);
190 if(data_type==UNSIGNED_SHORT)
191 ::append<unsigned short>(data, i);
192 else if(data_type==UNSIGNED_INT)
193 ::append<unsigned>(data, i);
198 void Batch::upload_data() const
200 get_buffer()->sub_data(get_offset(), data.size(), &data[0]);
203 unsigned Batch::get_index_size() const
205 if(data_type==UNSIGNED_SHORT)
206 return sizeof(unsigned short);
207 else if(data_type==UNSIGNED_INT)
208 return sizeof(unsigned);
209 return sizeof(unsigned char);
212 unsigned Batch::get_index(unsigned i) const
214 if(data_type==UNSIGNED_SHORT)
215 return *(unsigned short *)&data[i*sizeof(unsigned short)];
216 else if(data_type==UNSIGNED_INT)
217 return *(unsigned *)&data[i*sizeof(unsigned )];
222 void Batch::draw() const
227 if(data_type==UNSIGNED_SHORT)
229 else if(data_type==UNSIGNED_INT)
234 if(index!=restart_index)
237 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
238 glPrimitiveRestartIndexNV(index);
239 restart_index = index;
242 else if(restart_index && restart_index<=max_index)
244 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
253 BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*get_buffer());
254 Bind bind_ibuf(alias, true);
256 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast<void *>(get_offset()));
259 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
263 Batch::Loader::Loader(Batch &b):
264 DataFile::ObjectLoader<Batch>(b)
266 add("indices", &Loader::indices);
269 void Batch::Loader::indices(const vector<unsigned> &ind)