5 #include "ext_draw_range_elements.h"
6 #include "nv_primitive_restart.h"
7 #include "vertexarray.h"
14 void append(vector<unsigned char> &data, T i)
16 data.insert(data.end(), sizeof(T), 0);
17 *(T *)(&data[data.size()-sizeof(T)]) = i;
20 template<typename T, typename U>
23 if(!static_cast<T>(~n))
29 template<typename T, typename U>
30 void expand(vector<unsigned char> &data)
32 unsigned count = data.size()/sizeof(T);
33 data.resize(count*sizeof(U));
34 for(unsigned i=count; i--;)
35 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
38 template<typename T, typename U>
39 void shrink(vector<unsigned char> &data)
41 unsigned count = data.size()/sizeof(T);
42 for(unsigned i=0; i<count; ++i)
43 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
44 data.resize(count*sizeof(U));
52 unsigned Batch::restart_index = 0;
54 Batch::Batch(PrimitiveType t):
56 data_type(UNSIGNED_BYTE),
61 /* XXX Should probably provide a fallback to glDrawElements since this class
62 is pretty much required to render anything. */
63 static Require _req(EXT_draw_range_elements);
70 void Batch::set_data_type(DataType t)
72 if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
73 throw invalid_argument("Batch::set_data_type");
74 if(t==UNSIGNED_BYTE && max_index>0xFE)
75 throw invalid_operation("Batch::set_data_type");
76 else if(t==UNSIGNED_SHORT && max_index>0xFFFE)
77 throw invalid_operation("Batch::set_data_type");
79 if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT)
80 expand<unsigned char, unsigned short>(data);
81 else if(data_type==UNSIGNED_BYTE && t==UNSIGNED_INT)
82 expand<unsigned char, unsigned>(data);
83 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
84 expand<unsigned short, unsigned>(data);
85 else if(data_type==UNSIGNED_INT && t==UNSIGNED_BYTE)
86 shrink<unsigned, unsigned char>(data);
87 else if(data_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
88 shrink<unsigned, unsigned short>(data);
89 else if(data_type==UNSIGNED_SHORT && t==UNSIGNED_BYTE)
90 shrink<unsigned short, unsigned char>(data);
97 Batch &Batch::append(unsigned i)
107 void Batch::append(const vector<unsigned> &ind)
112 data.reserve(data.size()+ind.size()*get_index_size());
113 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
120 void Batch::append(const Batch &other)
122 if(other.prim_type!=prim_type)
123 throw invalid_argument("Batch::append");
124 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN || prim_type==POLYGON)
125 static Require _req(NV_primitive_restart);
127 if(other.data.empty())
130 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES || prim_type==QUADS)
132 else if(NV_primitive_restart)
135 if(data_type==UNSIGNED_SHORT)
136 ::append<unsigned short>(data, 0xFFFF);
137 else if(data_type==UNSIGNED_INT)
138 ::append<unsigned>(data, 0xFFFFFFFF);
140 data.push_back(0xFF);
142 else if(prim_type==TRIANGLE_STRIP)
144 append(get_index(size()-1));
145 append(other.get_index(0));
147 append(other.get_index(0));
149 else if(prim_type==QUAD_STRIP)
151 append(get_index(size()-1));
152 append(get_index(size()-1));
153 append(other.get_index(0));
154 append(other.get_index(0));
157 unsigned count = other.size();
158 for(unsigned i=0; i<count; ++i)
159 append_index(other.get_index(i));
165 void Batch::append_index(unsigned i)
168 min_index = max_index = i;
171 min_index = min(min_index, i);
172 max_index = max(max_index, i);
175 if((data_type==UNSIGNED_BYTE || data_type==UNSIGNED_SHORT) && max_index>0xFFFE)
176 set_data_type(UNSIGNED_INT);
177 else if(data_type==UNSIGNED_BYTE && max_index>0xFE)
178 set_data_type(UNSIGNED_SHORT);
180 if(data_type==UNSIGNED_SHORT)
181 ::append<unsigned short>(data, i);
182 else if(data_type==UNSIGNED_INT)
183 ::append<unsigned>(data, i);
188 void Batch::upload_data() const
190 get_buffer()->sub_data(get_offset(), data.size(), &data[0]);
193 unsigned Batch::get_index_size() const
195 if(data_type==UNSIGNED_SHORT)
196 return sizeof(unsigned short);
197 else if(data_type==UNSIGNED_INT)
198 return sizeof(unsigned);
199 return sizeof(unsigned char);
202 unsigned Batch::get_index(unsigned i) const
204 if(data_type==UNSIGNED_SHORT)
205 return *(unsigned short *)&data[i*sizeof(unsigned short)];
206 else if(data_type==UNSIGNED_INT)
207 return *(unsigned *)&data[i*sizeof(unsigned )];
212 void Batch::draw() const
217 if(data_type==UNSIGNED_SHORT)
219 else if(data_type==UNSIGNED_INT)
224 if(index!=restart_index)
227 glEnableClientState(GL_PRIMITIVE_RESTART_NV);
228 glPrimitiveRestartIndexNV(index);
229 restart_index = index;
232 else if(restart_index && restart_index<=max_index)
234 glDisableClientState(GL_PRIMITIVE_RESTART_NV);
243 BufferAlias<ELEMENT_ARRAY_BUFFER> alias(*get_buffer());
244 Bind bind_ibuf(alias, true);
246 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, reinterpret_cast<void *>(get_offset()));
249 glDrawRangeElements(prim_type, min_index, max_index, size(), data_type, &data[0]);
253 Batch::Loader::Loader(Batch &b):
254 DataFile::ObjectLoader<Batch>(b)
256 add("indices", &Loader::indices);
259 void Batch::Loader::indices(const vector<unsigned> &ind)