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(VertexAttribute a):
23 VertexFormat VertexFormat::operator,(VertexAttribute a) const
25 if(count>=MAX_ATTRIBUTES)
26 throw invalid_operation("VertexFormat::operator,");
28 VertexFormat r = *this;
29 r.attributes[r.count++] = a;
34 VertexFormat VertexFormat::operator,(unsigned i) const
37 throw invalid_operation("VertexFormat::operator,");
39 VertexFormat r = *this;
40 unsigned char &a = r.attributes[r.count-1];
41 a = make_indexed_attribute(static_cast<VertexAttribute>(a), i);
46 bool VertexFormat::operator==(const VertexFormat &other) const
48 if(count!=other.count)
50 return equal(attributes, attributes+count, other.attributes);
53 unsigned VertexFormat::stride() const
56 for(const unsigned char *i=begin(); i!=end(); ++i)
57 s += get_attribute_size(*i);
61 int VertexFormat::offset(VertexAttribute attr) const
63 unsigned sem = get_attribute_semantic(attr);
64 unsigned sz = get_attribute_size(attr);
66 for(const unsigned char *i=begin(); i!=end(); ++i)
68 if(get_attribute_semantic(*i)==sem)
70 if(get_attribute_size(*i)>=sz)
76 offs += get_attribute_size(*i);
83 VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index)
85 if(attr>=TEXCOORD1 && attr<=TEXCOORD4)
88 throw out_of_range("make_indexed_attribute");
90 else if(attr>=ATTRIB1 && attr<=ATTRIB4)
93 throw out_of_range("make_indexed_attribute");
96 throw invalid_argument("make_indexed_attribute");
97 return static_cast<VertexAttribute>(attr+index*4);
100 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
102 const string &str = conv.get();
103 if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
104 a = static_cast<VertexAttribute>(VERTEX2+(str[6]-'2'));
105 else if(str=="NORMAL3")
107 else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
108 a = static_cast<VertexAttribute>(COLOR3_FLOAT+(str[5]-'3'));
109 else if(str=="COLOR4_UBYTE")
111 else if(str=="TANGENT3")
113 else if(str=="BINORMAL3")
115 else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
118 a = static_cast<VertexAttribute>(TEXCOORD1+(str[8]-'1'));
119 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
120 a = static_cast<VertexAttribute>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
122 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
124 else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
129 n = lexical_cast<unsigned>(str.substr(8));
131 catch(const lexical_error &)
133 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));
135 a = static_cast<VertexAttribute>(ATTRIB1+(str[6]-'1')+n*4);
138 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));