1 #include <msp/gl/extensions/ext_draw_range_elements.h>
2 #include <msp/gl/extensions/nv_primitive_restart.h>
8 #include "vertexarray.h"
15 void append(vector<unsigned char> &data, T i)
17 data.insert(data.end(), sizeof(T), 0);
18 *(T *)(&data[data.size()-sizeof(T)]) = i;
21 template<typename T, typename U>
24 if(!static_cast<T>(~n))
30 template<typename T, typename U>
31 void expand(vector<unsigned char> &data)
33 unsigned count = data.size()/sizeof(T);
34 data.resize(count*sizeof(U));
35 for(unsigned i=count; i--;)
36 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
39 template<typename T, typename U>
40 void shrink(vector<unsigned char> &data)
42 unsigned count = data.size()/sizeof(T);
43 for(unsigned i=0; i<count; ++i)
44 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
45 data.resize(count*sizeof(U));
53 unsigned Batch::restart_index = 0;
55 Batch::Batch(PrimitiveType t):
57 data_type(UNSIGNED_BYTE),
62 /* XXX Should probably provide a fallback to glDrawElements since this class
63 is pretty much required to render anything. */
64 static Require _req(EXT_draw_range_elements);
71 void Batch::set_data_type(DataType t)
73 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
74 throw invalid_argument("Batch::set_data_type");
75 if(t==UNSIGNED_BYTE && max_index>0xFE)
76 throw invalid_operation("Batch::set_data_type");
77 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
78 throw invalid_operation("Batch::set_data_type");
80 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
81 expand<unsigned char, unsigned short>(data);
82 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
83 expand<unsigned char, unsigned>(data);
84 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
85 expand<unsigned short, unsigned>(data);
86 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
87 shrink<unsigned, unsigned char>(data);
88 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
89 shrink<unsigned, unsigned short>(data);
90 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
91 shrink<unsigned short, unsigned char>(data);
98 Batch &Batch::append(unsigned i)
108 void Batch::append(const vector<unsigned> &ind)
113 data.reserve(data.size()+ind.size()*get_index_size());
114 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 || prim_type==POLYGON)
126 return NV_primitive_restart;
131 void 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 || prim_type==POLYGON)
136 static Require _req(NV_primitive_restart);
138 if(other.data.empty())
141 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
143 else if(NV_primitive_restart)
146 if(data_type==UNSIGNED_SHORT)
147 ::append<unsigned short>(data, 0xFFFF);
148 else if(data_type==UNSIGNED_INT)
149 ::append<unsigned>(data, 0xFFFFFFFF);
151 data.push_back(0xFF);
153 else if(prim_type==TRIANGLE_STRIP)
155 append(get_index(size()-1));
156 append(other.get_index(0));
158 append(other.get_index(0));
160 else if(prim_type==QUAD_STRIP)
162 append(get_index(size()-1));
163 append(get_index(size()-1));
164 append(other.get_index(0));
165 append(other.get_index(0));
168 unsigned count = other.size();
169 for(unsigned i=0; i<count; ++i)
170 append_index(other.get_index(i));
176 void Batch::append_index(unsigned i)
179 min_index = max_index = i;
182 min_index = min(min_index, i);
183 max_index = max(max_index, i);
186 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
187 set_data_type(UNSIGNED_INT);
188 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
189 set_data_type(UNSIGNED_SHORT);
191 if(data_type==UNSIGNED_SHORT)
192 ::append<unsigned short>(data, i);
193 else if(data_type==UNSIGNED_INT)
194 ::append<unsigned>(data, i);
199 void Batch::upload_data() const
201 get_buffer()->sub_data(get_offset(), data.size(), &data[0]);
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)
238 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
239 glPrimitiveRestartIndexNV(index);
240 restart_index = index;
243 else if(restart_index && restart_index<=max_index)
245 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
249 Buffer *ibuf = get_buffer();
250 BindRestore _bind_ibuf(ibuf, ELEMENT_ARRAY_BUFFER);
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)