1 #include <msp/gl/extensions/ext_draw_range_elements.h>
2 #include <msp/gl/extensions/msp_legacy_features.h>
3 #include <msp/gl/extensions/msp_primitive_restart.h>
4 #include <msp/gl/extensions/nv_primitive_restart.h>
10 #include "vertexarray.h"
17 void append(vector<unsigned char> &data, T i)
19 data.insert(data.end(), sizeof(T), 0);
20 *(T *)(&data[data.size()-sizeof(T)]) = i;
23 template<typename T, typename U>
26 if(!static_cast<T>(~n))
32 template<typename T, typename U>
33 void expand(vector<unsigned char> &data)
35 unsigned count = data.size()/sizeof(T);
36 data.resize(count*sizeof(U));
37 for(unsigned i=count; i--;)
38 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
41 template<typename T, typename U>
42 void shrink(vector<unsigned char> &data)
44 unsigned count = data.size()/sizeof(T);
45 for(unsigned i=0; i<count; ++i)
46 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
47 data.resize(count*sizeof(U));
55 unsigned Batch::restart_index = 0;
57 Batch::Batch(PrimitiveType t):
59 data_type(UNSIGNED_BYTE),
69 void Batch::set_data_type(DataType t)
71 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
72 throw invalid_argument("Batch::set_data_type");
73 if(t==UNSIGNED_BYTE && max_index>0xFE)
74 throw invalid_operation("Batch::set_data_type");
75 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
76 throw invalid_operation("Batch::set_data_type");
78 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
79 expand<unsigned char, unsigned short>(data);
80 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
81 expand<unsigned char, unsigned>(data);
82 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
83 expand<unsigned short, unsigned>(data);
84 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
85 shrink<unsigned, unsigned char>(data);
86 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
87 shrink<unsigned, unsigned short>(data);
88 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
89 shrink<unsigned short, unsigned char>(data);
96 Batch &Batch::append(unsigned i)
106 Batch &Batch::append(const vector<unsigned> &ind)
111 data.reserve(data.size()+ind.size()*get_index_size());
112 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
121 bool Batch::can_append(PrimitiveType other_type)
123 if(other_type!=prim_type)
125 else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
126 return MSP_primitive_restart || NV_primitive_restart;
131 Batch &Batch::append(const Batch &other)
133 if(other.prim_type!=prim_type)
134 throw invalid_argument("Batch::append");
135 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
137 if(!MSP_primitive_restart)
139 static Require _req(NV_primitive_restart);
140 // Make sure we have glEnable/DisableClientState as well
141 static Require _req2(MSP_legacy_features);
145 if(other.data.empty())
148 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
150 else if(MSP_primitive_restart || NV_primitive_restart)
153 if(data_type==UNSIGNED_SHORT)
154 ::append<unsigned short>(data, 0xFFFF);
155 else if(data_type==UNSIGNED_INT)
156 ::append<unsigned>(data, 0xFFFFFFFF);
158 data.push_back(0xFF);
160 else if(prim_type==TRIANGLE_STRIP)
162 append(get_index(size()-1));
163 append(other.get_index(0));
165 append(other.get_index(0));
167 else if(prim_type==QUAD_STRIP)
169 append(get_index(size()-1));
170 append(get_index(size()-1));
171 append(other.get_index(0));
172 append(other.get_index(0));
175 unsigned count = other.size();
176 for(unsigned i=0; i<count; ++i)
177 append_index(other.get_index(i));
185 void Batch::append_index(unsigned i)
188 min_index = max_index = i;
191 min_index = min(min_index, i);
192 max_index = max(max_index, i);
195 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
196 set_data_type(UNSIGNED_INT);
197 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
198 set_data_type(UNSIGNED_SHORT);
200 if(data_type==UNSIGNED_SHORT)
201 ::append<unsigned short>(data, i);
202 else if(data_type==UNSIGNED_INT)
203 ::append<unsigned>(data, i);
208 unsigned Batch::get_index_size() const
210 if(data_type==UNSIGNED_SHORT)
211 return sizeof(unsigned short);
212 else if(data_type==UNSIGNED_INT)
213 return sizeof(unsigned);
214 return sizeof(unsigned char);
217 unsigned Batch::get_index(unsigned i) const
219 if(data_type==UNSIGNED_SHORT)
220 return *(unsigned short *)&data[i*sizeof(unsigned short)];
221 else if(data_type==UNSIGNED_INT)
222 return *(unsigned *)&data[i*sizeof(unsigned )];
227 void Batch::draw() const
232 if(data_type==UNSIGNED_SHORT)
234 else if(data_type==UNSIGNED_INT)
239 if(index!=restart_index)
240 set_restart_index(index);
242 else if(restart_index && restart_index<=max_index)
243 set_restart_index(0);
245 Buffer *ibuf = get_buffer();
246 const void *data_ptr;
247 BindRestore _bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
253 data_ptr = reinterpret_cast<const void *>(get_offset());
258 if(EXT_draw_range_elements)
259 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, data_ptr);
261 glDrawElements(prim_type, size(), data_type, data_ptr);
264 void Batch::set_restart_index(unsigned index)
266 if(MSP_primitive_restart)
271 glEnable(GL_PRIMITIVE_RESTART);
272 glPrimitiveRestartIndex(index);
275 glDisable(GL_PRIMITIVE_RESTART);
282 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
283 glPrimitiveRestartIndexNV(index);
286 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
289 restart_index = index;
293 Batch::Loader::Loader(Batch &b):
294 DataFile::ObjectLoader<Batch>(b)
296 add("indices", &Loader::indices);
299 void Batch::Loader::indices(const vector<unsigned> &ind)