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):
52 set_index_type(UNSIGNED_SHORT);
55 void Batch::set_index_type(DataType t)
59 if(t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
60 throw invalid_argument("Batch::set_data_type");
61 if(t==UNSIGNED_SHORT && max_index>0xFFFE)
62 throw invalid_operation("Batch::set_data_type");
64 if(index_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
65 expand<uint16_t, uint32_t>(data);
66 else if(index_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
67 shrink<uint32_t, uint16_t>(data);
70 BatchBackend::set_index_type(t);
75 void Batch::set_patch_size(unsigned s)
77 if(prim_type!=PATCHES)
78 throw invalid_operation("Batch::set_patch_size");
80 throw invalid_argument("Batch::set_patch_size");
85 Batch &Batch::append(unsigned i)
95 Batch &Batch::append(const vector<unsigned> &ind)
100 data.reserve(data.size()+ind.size()*get_index_size());
110 bool Batch::can_append(PrimitiveType other_type)
112 if(other_type!=prim_type)
114 else if(prim_type==LINE_STRIP || prim_type==TRIANGLE_FAN)
115 return check_restart(false);
120 Batch &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==TRIANGLE_FAN)
127 if(other.data.empty())
130 // TODO allow appending triangles to a triangle strip
132 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES)
134 else if(check_restart(false))
136 if(index_type==UNSIGNED_INT)
137 ::append<uint32_t>(data, 0xFFFFFFFF);
139 ::append<uint16_t>(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_t>(data, i);
172 ::append<uint16_t>(data, i);
175 unsigned Batch::get_index(size_t i) const
177 if(index_type==UNSIGNED_INT)
178 return *(uint32_t *)&data[i*sizeof(uint32_t)];
180 return *(uint16_t *)&data[i*sizeof(uint16_t)];
184 Batch::Loader::Loader(Batch &b):
185 DataFile::ObjectLoader<Batch>(b)
187 add("indices", &Loader::indices);
188 add("patch_size", &Loader::patch_size);
191 void Batch::Loader::indices(const vector<unsigned> &ind)
196 void Batch::Loader::patch_size(unsigned s)
198 obj.set_patch_size(s);