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 void 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)
119 bool Batch::can_append(PrimitiveType other_type)
121 if(other_type!=prim_type)
123 else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
124 return MSP_primitive_restart || NV_primitive_restart;
129 void Batch::append(const Batch &other)
131 if(other.prim_type!=prim_type)
132 throw invalid_argument("Batch::append");
133 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
135 if(!MSP_primitive_restart)
137 static Require _req(NV_primitive_restart);
138 // Make sure we have glEnable/DisableClientState as well
139 static Require _req2(MSP_legacy_features);
143 if(other.data.empty())
146 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
148 else if(MSP_primitive_restart || NV_primitive_restart)
151 if(data_type==UNSIGNED_SHORT)
152 ::append<unsigned short>(data, 0xFFFF);
153 else if(data_type==UNSIGNED_INT)
154 ::append<unsigned>(data, 0xFFFFFFFF);
156 data.push_back(0xFF);
158 else if(prim_type==TRIANGLE_STRIP)
160 append(get_index(size()-1));
161 append(other.get_index(0));
163 append(other.get_index(0));
165 else if(prim_type==QUAD_STRIP)
167 append(get_index(size()-1));
168 append(get_index(size()-1));
169 append(other.get_index(0));
170 append(other.get_index(0));
173 unsigned count = other.size();
174 for(unsigned i=0; i<count; ++i)
175 append_index(other.get_index(i));
181 void Batch::append_index(unsigned i)
184 min_index = max_index = i;
187 min_index = min(min_index, i);
188 max_index = max(max_index, i);
191 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
192 set_data_type(UNSIGNED_INT);
193 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
194 set_data_type(UNSIGNED_SHORT);
196 if(data_type==UNSIGNED_SHORT)
197 ::append<unsigned short>(data, i);
198 else if(data_type==UNSIGNED_INT)
199 ::append<unsigned>(data, i);
204 unsigned Batch::get_index_size() const
206 if(data_type==UNSIGNED_SHORT)
207 return sizeof(unsigned short);
208 else if(data_type==UNSIGNED_INT)
209 return sizeof(unsigned);
210 return sizeof(unsigned char);
213 unsigned Batch::get_index(unsigned i) const
215 if(data_type==UNSIGNED_SHORT)
216 return *(unsigned short *)&data[i*sizeof(unsigned short)];
217 else if(data_type==UNSIGNED_INT)
218 return *(unsigned *)&data[i*sizeof(unsigned )];
223 void Batch::draw() const
228 if(data_type==UNSIGNED_SHORT)
230 else if(data_type==UNSIGNED_INT)
235 if(index!=restart_index)
236 set_restart_index(index);
238 else if(restart_index && restart_index<=max_index)
239 set_restart_index(0);
241 Buffer *ibuf = get_buffer();
242 const void *data_ptr;
243 BindRestore _bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
249 data_ptr = reinterpret_cast<const void *>(get_offset());
254 if(EXT_draw_range_elements)
255 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, data_ptr);
257 glDrawElements(prim_type, size(), data_type, data_ptr);
260 void Batch::set_restart_index(unsigned index)
262 if(MSP_primitive_restart)
267 glEnable(GL_PRIMITIVE_RESTART);
268 glPrimitiveRestartIndex(index);
271 glDisable(GL_PRIMITIVE_RESTART);
278 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
279 glPrimitiveRestartIndexNV(index);
282 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
285 restart_index = index;
289 Batch::Loader::Loader(Batch &b):
290 DataFile::ObjectLoader<Batch>(b)
292 add("indices", &Loader::indices);
295 void Batch::Loader::indices(const vector<unsigned> &ind)