9 void append(vector<uint8_t> &data, T i)
11 data.insert(data.end(), sizeof(T), 0);
12 *(T *)(&data[data.size()-sizeof(T)]) = i;
15 template<typename T, typename U>
18 if(!static_cast<T>(~n))
24 template<typename T, typename U>
25 void expand(vector<uint8_t> &data)
27 unsigned count = data.size()/sizeof(T);
28 data.resize(count*sizeof(U));
29 for(unsigned i=count; i--;)
30 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
33 template<typename T, typename U>
34 void shrink(vector<uint8_t> &data)
36 unsigned count = data.size()/sizeof(T);
37 for(unsigned i=0; i<count; ++i)
38 *(U *)(&data[i*sizeof(U)]) = convert<T, U>(*(T *)(&data[i*sizeof(T)]));
39 data.resize(count*sizeof(U));
47 Batch::Batch(PrimitiveType t):
53 set_index_type(UNSIGNED_SHORT);
56 void Batch::set_index_type(DataType t)
60 if(t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
61 throw invalid_argument("Batch::set_data_type");
62 if(t==UNSIGNED_SHORT && max_index>0xFFFE)
63 throw invalid_operation("Batch::set_data_type");
65 if(index_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
66 expand<uint16_t, uint32_t>(data);
67 else if(index_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
68 shrink<uint32_t, uint16_t>(data);
71 BatchBackend::set_index_type(t);
76 Batch &Batch::append(unsigned i)
86 Batch &Batch::append(const vector<unsigned> &ind)
91 data.reserve(data.size()+ind.size()*get_index_size());
101 bool Batch::can_append(PrimitiveType other_type)
103 if(other_type!=prim_type)
105 else if(prim_type==LINE_STRIP || prim_type==TRIANGLE_FAN)
106 return check_restart(false);
111 Batch &Batch::append(const Batch &other)
113 if(other.prim_type!=prim_type)
114 throw invalid_argument("Batch::append");
115 if(prim_type==LINE_STRIP || prim_type==TRIANGLE_FAN)
118 if(other.data.empty())
121 // TODO allow appending triangles to a triangle strip
123 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES)
125 else if(check_restart(false))
127 if(index_type==UNSIGNED_INT)
128 ::append<uint32_t>(data, 0xFFFFFFFF);
130 ::append<uint16_t>(data, 0xFFFF);
132 else if(prim_type==TRIANGLE_STRIP)
134 append(get_index(size()-1));
135 append(other.get_index(0));
137 append(other.get_index(0));
140 unsigned count = other.size();
141 for(unsigned i=0; i<count; ++i)
142 append_index(other.get_index(i));
150 void Batch::append_index(unsigned i)
155 max_index = max(max_index, i);
157 if(index_type==UNSIGNED_SHORT && max_index>0xFFFE)
158 set_index_type(UNSIGNED_INT);
160 if(index_type==UNSIGNED_INT)
161 ::append<uint32_t>(data, i);
163 ::append<uint16_t>(data, i);
166 unsigned Batch::get_index(size_t i) const
168 if(index_type==UNSIGNED_INT)
169 return *(uint32_t *)&data[i*sizeof(uint32_t)];
171 return *(uint16_t *)&data[i*sizeof(uint16_t)];
175 Batch::Loader::Loader(Batch &b):
176 DataFile::ObjectLoader<Batch>(b)
178 add("indices", &Loader::indices);
181 void Batch::Loader::indices(const vector<unsigned> &ind)