1 #include <msp/gl/extensions/arb_draw_instanced.h>
2 #include <msp/gl/extensions/msp_primitive_restart.h>
7 #include "vertexarray.h"
14 void append(vector<Msp::UInt8> &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<Msp::UInt8> &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<Msp::UInt8> &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 index_type(UNSIGNED_SHORT),
57 gl_index_type(get_gl_type(index_type)),
66 void Batch::set_index_type(DataType t)
68 if(t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
69 throw invalid_argument("Batch::set_data_type");
70 if(t==UNSIGNED_SHORT && max_index>0xFFFE)
71 throw invalid_operation("Batch::set_data_type");
73 if(index_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
74 expand<UInt16, UInt32>(data);
75 else if(index_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
76 shrink<UInt32, UInt16>(data);
79 gl_index_type = get_gl_type(t);
84 Batch &Batch::append(unsigned i)
94 Batch &Batch::append(const vector<unsigned> &ind)
99 data.reserve(data.size()+ind.size()*get_index_size());
100 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
109 bool Batch::can_append(PrimitiveType other_type)
111 if(other_type!=prim_type)
113 else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
114 return MSP_primitive_restart;
119 Batch &Batch::append(const Batch &other)
121 if(other.prim_type!=prim_type)
122 throw invalid_argument("Batch::append");
123 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
124 static Require _req(MSP_primitive_restart);
126 if(other.data.empty())
129 // TODO allow appending triangles to a triangle strip
131 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES)
133 else if(MSP_primitive_restart)
136 if(index_type==UNSIGNED_INT)
137 ::append<UInt32>(data, 0xFFFFFFFF);
139 ::append<UInt16>(data, 0xFFFF);
141 else if(prim_type==TRIANGLE_STRIP)
143 append(get_index(size()-1));
144 append(other.get_index(0));
146 append(other.get_index(0));
149 unsigned count = other.size();
150 for(unsigned i=0; i<count; ++i)
151 append_index(other.get_index(i));
159 void Batch::append_index(unsigned i)
164 max_index = max(max_index, i);
166 if(index_type==UNSIGNED_SHORT && max_index>0xFFFE)
167 set_index_type(UNSIGNED_INT);
169 if(index_type==UNSIGNED_INT)
170 ::append<UInt32>(data, i);
172 ::append<UInt16>(data, i);
175 unsigned Batch::get_index_size() const
177 return (index_type==UNSIGNED_INT ? sizeof(UInt32) : sizeof(UInt16));
180 unsigned Batch::get_index(unsigned i) const
182 if(index_type==UNSIGNED_INT)
183 return *(UInt32 *)&data[i*sizeof(UInt32)];
185 return *(UInt16 *)&data[i*sizeof(UInt16)];
188 void Batch::draw() const
190 const void *data_ptr = setup_draw();
191 glDrawElements(prim_type, size(), gl_index_type, data_ptr);
194 void Batch::draw_instanced(unsigned count) const
196 static Require req(ARB_draw_instanced);
198 const void *data_ptr = setup_draw();
199 glDrawElementsInstanced(prim_type, size(), gl_index_type, data_ptr, count);
202 const void *Batch::setup_draw() const
205 throw invalid_operation("Batch::setup_draw");
209 unsigned index = (index_type==UNSIGNED_INT ? 0xFFFFFFFF : 0xFFFF);
211 if(index!=restart_index)
212 set_restart_index(index);
214 else if(restart_index && restart_index<=max_index)
215 set_restart_index(0);
219 return reinterpret_cast<const void *>(get_offset());
222 void Batch::set_restart_index(unsigned index)
227 glEnable(GL_PRIMITIVE_RESTART);
228 glPrimitiveRestartIndex(index);
231 glDisable(GL_PRIMITIVE_RESTART);
233 restart_index = index;
237 Batch::Loader::Loader(Batch &b):
238 DataFile::ObjectLoader<Batch>(b)
240 add("indices", &Loader::indices);
243 void Batch::Loader::indices(const vector<unsigned> &ind)