1 #include <msp/gl/extensions/msp_primitive_restart.h>
6 #include "vertexarray.h"
13 void append(vector<Msp::UInt8> &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<Msp::UInt8> &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<Msp::UInt8> &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 unsigned Batch::restart_index = 0;
53 Batch::Batch(PrimitiveType t):
55 gl_prim_type(GL::get_gl_primitive_type(prim_type)),
56 index_type(UNSIGNED_SHORT),
57 gl_index_type(get_gl_type(index_type)),
66 void Batch::set_index_type(DataType t)
70 if(t!=UNSIGNED_SHORT && t!=UNSIGNED_INT)
71 throw invalid_argument("Batch::set_data_type");
72 if(t==UNSIGNED_SHORT && max_index>0xFFFE)
73 throw invalid_operation("Batch::set_data_type");
75 if(index_type==UNSIGNED_SHORT && t==UNSIGNED_INT)
76 expand<UInt16, UInt32>(data);
77 else if(index_type==UNSIGNED_INT && t==UNSIGNED_SHORT)
78 shrink<UInt32, UInt16>(data);
81 gl_index_type = get_gl_type(t);
86 Batch &Batch::append(unsigned i)
96 Batch &Batch::append(const vector<unsigned> &ind)
101 data.reserve(data.size()+ind.size()*get_index_size());
102 for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
111 bool Batch::can_append(PrimitiveType other_type)
113 if(other_type!=prim_type)
115 else if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
116 return MSP_primitive_restart;
121 Batch &Batch::append(const Batch &other)
123 if(other.prim_type!=prim_type)
124 throw invalid_argument("Batch::append");
125 if(prim_type==LINE_STRIP || prim_type==LINE_LOOP || prim_type==TRIANGLE_FAN)
126 static Require _req(MSP_primitive_restart);
128 if(other.data.empty())
131 // TODO allow appending triangles to a triangle strip
133 if(prim_type==POINTS || prim_type==LINES || prim_type==TRIANGLES)
135 else if(MSP_primitive_restart)
138 if(index_type==UNSIGNED_INT)
139 ::append<UInt32>(data, 0xFFFFFFFF);
141 ::append<UInt16>(data, 0xFFFF);
143 else if(prim_type==TRIANGLE_STRIP)
145 append(get_index(size()-1));
146 append(other.get_index(0));
148 append(other.get_index(0));
151 unsigned count = other.size();
152 for(unsigned i=0; i<count; ++i)
153 append_index(other.get_index(i));
161 void Batch::append_index(unsigned i)
166 max_index = max(max_index, i);
168 if(index_type==UNSIGNED_SHORT && max_index>0xFFFE)
169 set_index_type(UNSIGNED_INT);
171 if(index_type==UNSIGNED_INT)
172 ::append<UInt32>(data, i);
174 ::append<UInt16>(data, i);
177 unsigned Batch::get_index_size() const
179 return (index_type==UNSIGNED_INT ? sizeof(UInt32) : sizeof(UInt16));
182 unsigned Batch::get_index(unsigned i) const
184 if(index_type==UNSIGNED_INT)
185 return *(UInt32 *)&data[i*sizeof(UInt32)];
187 return *(UInt16 *)&data[i*sizeof(UInt16)];
191 Batch::Loader::Loader(Batch &b):
192 DataFile::ObjectLoader<Batch>(b)
194 add("indices", &Loader::indices);
197 void Batch::Loader::indices(const vector<unsigned> &ind)