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)
86 if(attr>=TEXCOORD1 && attr<=TEXCOORD4)
89 throw out_of_range("make_indexed_attribute");
91 else if(attr>=RAW_ATTRIB1 && attr<=RAW_ATTRIB4)
93 else if(attr<GENERIC1 || attr>GENERIC4)
94 throw invalid_argument("make_indexed_attribute");
96 if(static_cast<int>((base>>3)+index)>=31)
97 throw out_of_range("make_indexed_attribute");
99 return static_cast<VertexAttribute>(base+index*8);
103 bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr)
105 string::const_iterator i = str.begin();
106 for(; *name; ++name, ++i)
109 if(i==str.end() || *i<'0'+min_size || *i>'0'+max_size)
111 VertexAttribute result = static_cast<VertexAttribute>(base_attr+(*i-'0'-min_size));
115 if(*i!='_' || ++i==str.end())
119 result = make_indexed_attribute(result, lexical_cast<unsigned>(string(i, str.end())));
123 // The operator>> will throw a more appropriate exception
132 void operator>>(const LexicalConverter &conv, VertexAttribute &a)
134 const string &str = conv.get();
137 else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
138 a = static_cast<VertexAttribute>(COLOR3_FLOAT+(str[5]-'3'));
139 else if(str=="COLOR4_UBYTE")
141 else if(str=="TANGENT3")
143 else if(str=="BINORMAL3")
145 else if(!convert_attribute(str, "VERTEX", 2, 4, a, VERTEX2) &&
146 !convert_attribute(str, "GROUP", 1, 4, a, GROUP1) &&
147 !convert_attribute(str, "WEIGHT", 1, 4, a, WEIGHT1) &&
148 !convert_attribute(str, "TEXCOORD", 1, 4, a, TEXCOORD1) &&
149 !convert_attribute(str, "GENERIC", 1, 4, a, GENERIC1))
150 throw lexical_error(format("conversion of '%s' to VertexAttribute", str));