2 #include <msp/strings/format.h>
3 #include <msp/strings/lexicalcast.h>
4 #include <msp/strings/utils.h>
6 #include "vertexformat.h"
13 VertexFormat::VertexFormat():
17 VertexFormat::VertexFormat(VertexComponent c):
18 data(new unsigned char[8])
24 VertexFormat::VertexFormat(const VertexFormat &f):
30 VertexFormat &VertexFormat::operator=(const VertexFormat &f)
35 data = new unsigned char[(f.data[0]&~7)+8];
36 copy(f.data, f.data+f.data[0]+1, data);
44 VertexFormat::~VertexFormat()
49 VertexFormat VertexFormat::operator,(VertexComponent c) const
51 VertexFormat r = *this;
54 const unsigned char n = ++r.data[0];
57 unsigned char *newdt = new unsigned char[n+8];
58 copy(r.data, r.data+n, newdt);
66 r.data = new unsigned char[8];
74 VertexFormat VertexFormat::operator,(unsigned i) const
77 throw invalid_operation("VertexFormat::operator,");
78 VertexFormat r = *this;
79 unsigned char *c = r.data+r.data[0];
80 *c = make_indexed_component(static_cast<VertexComponent>(*c), i);
85 unsigned VertexFormat::stride() const
88 for(const unsigned char *i=begin(); i!=end(); ++i)
89 s += get_component_size(*i);
93 int VertexFormat::offset(VertexComponent comp) const
95 unsigned type = get_component_type(comp);
96 unsigned size = get_component_size(comp);
98 for(const unsigned char *i=begin(); i!=end(); ++i)
100 if(get_component_type(*i)==type)
102 if(get_component_size(*i)>=size)
108 offs += get_component_size(*i);
114 VertexComponent make_indexed_component(VertexComponent comp, unsigned index)
116 if(comp>=TEXCOORD1 && comp<=TEXCOORD4)
119 throw out_of_range("make_indexed_component");
121 else if(comp>=ATTRIB1 && comp<=ATTRIB4)
124 throw out_of_range("make_indexed_component");
127 throw invalid_argument("make_indexed_component");
128 return static_cast<VertexComponent>(comp+index*4);
131 void operator>>(const LexicalConverter &conv, VertexComponent &c)
133 const string &str = conv.get();
134 if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
135 c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
136 else if(str=="NORMAL3")
138 else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
139 c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
140 else if(str=="COLOR4_UBYTE")
142 else if(str=="TANGENT3")
144 else if(str=="BINORMAL3")
146 else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
149 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
150 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
151 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
153 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
155 else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
160 n = lexical_cast<unsigned>(str.substr(8));
162 catch(const lexical_error &)
164 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
166 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
169 throw lexical_error(format("conversion of '%s' to VertexComponent", str));