1 #include <msp/gl/extensions/ext_draw_range_elements.h>
2 #include <msp/gl/extensions/msp_legacy_features.h>
3 #include <msp/gl/extensions/nv_primitive_restart.h>
9 #include "vertexarray.h"
16 void append(vector<unsigned char> &data, T i)
18 data.insert(data.end(), sizeof(T), 0);
19 *(T *)(&data[data.size()-sizeof(T)]) = i;
22 template<typename T, typename U>
25 if(!static_cast<T>(~n))
31 template<typename T, typename U>
32 void expand(vector<unsigned char> &data)
34 unsigned count = data.size()/sizeof(T);
35 data.resize(count*sizeof(U));
36 for(unsigned i=count; i--;)
37 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
40 template<typename T, typename U>
41 void shrink(vector<unsigned char> &data)
43 unsigned count = data.size()/sizeof(T);
44 for(unsigned i=0; i<count; ++i)
45 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
46 data.resize(count*sizeof(U));
54 unsigned Batch::restart_index = 0;
56 Batch::Batch(PrimitiveType t):
58 data_type(UNSIGNED_BYTE),
63 /* Make sure we have glEnable/DisableClientState to go with
64 NV_primitive_restart */
65 if(NV_primitive_restart)
66 (bool)MSP_legacy_features;
73 void Batch::set_data_type(DataType t)
75 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
76 throw invalid_argument("Batch::set_data_type");
77 if(t==UNSIGNED_BYTE && max_index>0xFE)
78 throw invalid_operation("Batch::set_data_type");
79 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
80 throw invalid_operation("Batch::set_data_type");
82 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
83 expand<unsigned char, unsigned short>(data);
84 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
85 expand<unsigned char, unsigned>(data);
86 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
87 expand<unsigned short, unsigned>(data);
88 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
89 shrink<unsigned, unsigned char>(data);
90 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
91 shrink<unsigned, unsigned short>(data);
92 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
93 shrink<unsigned short, unsigned char>(data);
100 Batch &Batch::append(unsigned i)
110 void Batch::append(const vector<unsigned> &ind)
115 data.reserve(data.size()+ind.size()*get_index_size());
116 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
123 bool Batch::can_append(PrimitiveType other_type)
125 if(other_type!=prim_type)
127 else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
128 return NV_primitive_restart;
133 void Batch::append(const Batch &other)
135 if(other.prim_type!=prim_type)
136 throw invalid_argument("Batch::append");
137 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
138 static Require _req(NV_primitive_restart);
140 if(other.data.empty())
143 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
145 else if(NV_primitive_restart)
148 if(data_type==UNSIGNED_SHORT)
149 ::append<unsigned short>(data, 0xFFFF);
150 else if(data_type==UNSIGNED_INT)
151 ::append<unsigned>(data, 0xFFFFFFFF);
153 data.push_back(0xFF);
155 else if(prim_type==TRIANGLE_STRIP)
157 append(get_index(size()-1));
158 append(other.get_index(0));
160 append(other.get_index(0));
162 else if(prim_type==QUAD_STRIP)
164 append(get_index(size()-1));
165 append(get_index(size()-1));
166 append(other.get_index(0));
167 append(other.get_index(0));
170 unsigned count = other.size();
171 for(unsigned i=0; i<count; ++i)
172 append_index(other.get_index(i));
178 void Batch::append_index(unsigned i)
181 min_index = max_index = i;
184 min_index = min(min_index, i);
185 max_index = max(max_index, i);
188 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
189 set_data_type(UNSIGNED_INT);
190 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
191 set_data_type(UNSIGNED_SHORT);
193 if(data_type==UNSIGNED_SHORT)
194 ::append<unsigned short>(data, i);
195 else if(data_type==UNSIGNED_INT)
196 ::append<unsigned>(data, i);
201 unsigned Batch::get_index_size() const
203 if(data_type==UNSIGNED_SHORT)
204 return sizeof(unsigned short);
205 else if(data_type==UNSIGNED_INT)
206 return sizeof(unsigned);
207 return sizeof(unsigned char);
210 unsigned Batch::get_index(unsigned i) const
212 if(data_type==UNSIGNED_SHORT)
213 return *(unsigned short *)&data[i*sizeof(unsigned short)];
214 else if(data_type==UNSIGNED_INT)
215 return *(unsigned *)&data[i*sizeof(unsigned )];
220 void Batch::draw() const
225 if(data_type==UNSIGNED_SHORT)
227 else if(data_type==UNSIGNED_INT)
232 if(index!=restart_index)
235 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
236 glPrimitiveRestartIndexNV(index);
237 restart_index = index;
240 else if(restart_index && restart_index<=max_index)
242 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
246 Buffer *ibuf = get_buffer();
247 const void *data_ptr;
248 BindRestore _bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
254 data_ptr = reinterpret_cast<const void *>(get_offset());
259 if(EXT_draw_range_elements)
260 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, data_ptr);
262 glDrawElements(prim_type, size(), data_type, data_ptr);
266 Batch::Loader::Loader(Batch &b):
267 DataFile::ObjectLoader<Batch>(b)
269 add("indices", &Loader::indices);
272 void Batch::Loader::indices(const vector<unsigned> &ind)