1 #include <msp/gl/extensions/msp_primitive_restart.h>
6 #include "vertexarray.h"
13 void append(vector<uint8_t> &data, T i)
15 data.insert(data.end(), sizeof(T), 0);
16 *(T *)(&data[data.size()-sizeof(T)]) = i;
19 template<typename T, typename U>
22 if(!static_cast<T>(~n))
28 template<typename T, typename U>
29 void expand(vector<uint8_t> &data)
31 unsigned count = data.size()/sizeof(T);
32 data.resize(count*sizeof(U));
33 for(unsigned i=count; i--;)
34 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
37 template<typename T, typename U>
38 void shrink(vector<uint8_t> &data)
40 unsigned count = data.size()/sizeof(T);
41 for(unsigned i=0; i<count; ++i)
42 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
43 data.resize(count*sizeof(U));
51 Batch::Batch(PrimitiveType t):
53 gl_prim_type(GL::get_gl_primitive_type(prim_type)),
54 index_type(UNSIGNED_SHORT),
55 gl_index_type(get_gl_type(index_type)),
63 void Batch::set_index_type(DataType t)
67 if(t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
68 throw invalid_argument("Batch::set_data_type");
69 if(t==UNSIGNED_SHORT && max_index>0xFFFE)
70 throw invalid_operation("Batch::set_data_type");
72 if(index_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
73 expand<uint16_t, uint32_t>(data);
74 else if(index_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
75 shrink<uint32_t, uint16_t>(data);
78 gl_index_type = get_gl_type(t);
83 Batch &Batch::append(unsigned i)
93 Batch &Batch::append(const vector<unsigned> &ind)
98 data.reserve(data.size()+ind.size()*get_index_size());
108 bool Batch::can_append(PrimitiveType other_type)
110 if(other_type!=prim_type)
112 else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
113 return MSP_primitive_restart;
118 Batch &Batch::append(const Batch &other)
120 if(other.prim_type!=prim_type)
121 throw invalid_argument("Batch::append");
122 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
123 static Require _req(MSP_primitive_restart);
125 if(other.data.empty())
128 // TODO allow appending triangles to a triangle strip
130 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES)
132 else if(MSP_primitive_restart)
134 if(index_type==UNSIGNED_INT)
135 ::append<uint32_t>(data, 0xFFFFFFFF);
137 ::append<uint16_t>(data, 0xFFFF);
139 else if(prim_type==TRIANGLE_STRIP)
141 append(get_index(size()-1));
142 append(other.get_index(0));
144 append(other.get_index(0));
147 unsigned count = other.size();
148 for(unsigned i=0; i<count; ++i)
149 append_index(other.get_index(i));
157 void Batch::append_index(unsigned i)
162 max_index = max(max_index, i);
164 if(index_type==UNSIGNED_SHORT && max_index>0xFFFE)
165 set_index_type(UNSIGNED_INT);
167 if(index_type==UNSIGNED_INT)
168 ::append<uint32_t>(data, i);
170 ::append<uint16_t>(data, i);
173 unsigned Batch::get_index_size() const
175 return (index_type==UNSIGNED_INT ? sizeof(uint32_t) : sizeof(uint16_t));
178 unsigned Batch::get_index(unsigned i) const
180 if(index_type==UNSIGNED_INT)
181 return *(uint32_t *)&data[i*sizeof(uint32_t)];
183 return *(uint16_t *)&data[i*sizeof(uint16_t)];
187 Batch::Loader::Loader(Batch &b):
188 DataFile::ObjectLoader<Batch>(b)
190 add("indices", &Loader::indices);
193 void Batch::Loader::indices(const vector<unsigned> &ind)