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),
64 /* Make sure we have glEnable/DisableClientState to go with
65 NV_primitive_restart */
66 if(!MSP_primitive_restart && NV_primitive_restart)
67 (void)(bool)MSP_legacy_features;
74 void Batch::set_data_type(DataType t)
76 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
77 throw invalid_argument("Batch::set_data_type");
78 if(t==UNSIGNED_BYTE && max_index>0xFE)
79 throw invalid_operation("Batch::set_data_type");
80 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
81 throw invalid_operation("Batch::set_data_type");
83 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
84 expand<unsigned char, unsigned short>(data);
85 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
86 expand<unsigned char, unsigned>(data);
87 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
88 expand<unsigned short, unsigned>(data);
89 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
90 shrink<unsigned, unsigned char>(data);
91 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
92 shrink<unsigned, unsigned short>(data);
93 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
94 shrink<unsigned short, unsigned char>(data);
101 Batch &Batch::append(unsigned i)
111 void Batch::append(const vector<unsigned> &ind)
116 data.reserve(data.size()+ind.size()*get_index_size());
117 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
124 bool Batch::can_append(PrimitiveType other_type)
126 if(other_type!=prim_type)
128 else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
129 return MSP_primitive_restart || NV_primitive_restart;
134 void Batch::append(const Batch &other)
136 if(other.prim_type!=prim_type)
137 throw invalid_argument("Batch::append");
138 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
140 if(!MSP_primitive_restart)
141 static Require _req(NV_primitive_restart);
144 if(other.data.empty())
147 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
149 else if(MSP_primitive_restart || NV_primitive_restart)
152 if(data_type==UNSIGNED_SHORT)
153 ::append<unsigned short>(data, 0xFFFF);
154 else if(data_type==UNSIGNED_INT)
155 ::append<unsigned>(data, 0xFFFFFFFF);
157 data.push_back(0xFF);
159 else if(prim_type==TRIANGLE_STRIP)
161 append(get_index(size()-1));
162 append(other.get_index(0));
164 append(other.get_index(0));
166 else if(prim_type==QUAD_STRIP)
168 append(get_index(size()-1));
169 append(get_index(size()-1));
170 append(other.get_index(0));
171 append(other.get_index(0));
174 unsigned count = other.size();
175 for(unsigned i=0; i<count; ++i)
176 append_index(other.get_index(i));
182 void Batch::append_index(unsigned i)
185 min_index = max_index = i;
188 min_index = min(min_index, i);
189 max_index = max(max_index, i);
192 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
193 set_data_type(UNSIGNED_INT);
194 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
195 set_data_type(UNSIGNED_SHORT);
197 if(data_type==UNSIGNED_SHORT)
198 ::append<unsigned short>(data, i);
199 else if(data_type==UNSIGNED_INT)
200 ::append<unsigned>(data, i);
205 unsigned Batch::get_index_size() const
207 if(data_type==UNSIGNED_SHORT)
208 return sizeof(unsigned short);
209 else if(data_type==UNSIGNED_INT)
210 return sizeof(unsigned);
211 return sizeof(unsigned char);
214 unsigned Batch::get_index(unsigned i) const
216 if(data_type==UNSIGNED_SHORT)
217 return *(unsigned short *)&data[i*sizeof(unsigned short)];
218 else if(data_type==UNSIGNED_INT)
219 return *(unsigned *)&data[i*sizeof(unsigned )];
224 void Batch::draw() const
229 if(data_type==UNSIGNED_SHORT)
231 else if(data_type==UNSIGNED_INT)
236 if(index!=restart_index)
237 set_restart_index(index);
239 else if(restart_index && restart_index<=max_index)
240 set_restart_index(0);
242 Buffer *ibuf = get_buffer();
243 const void *data_ptr;
244 BindRestore _bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
250 data_ptr = reinterpret_cast<const void *>(get_offset());
255 if(EXT_draw_range_elements)
256 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, data_ptr);
258 glDrawElements(prim_type, size(), data_type, data_ptr);
261 void Batch::set_restart_index(unsigned index)
263 if(MSP_primitive_restart)
268 glEnable(GL_PRIMITIVE_RESTART);
269 glPrimitiveRestartIndex(index);
272 glDisable(GL_PRIMITIVE_RESTART);
279 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
280 glPrimitiveRestartIndexNV(index);
283 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
286 restart_index = index;
290 Batch::Loader::Loader(Batch &b):
291 DataFile::ObjectLoader<Batch>(b)
293 add("indices", &Loader::indices);
296 void Batch::Loader::indices(const vector<unsigned> &ind)