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):
23 VertexFormat VertexFormat::operator,(VertexComponent c) const
25 if(count>=MAX_COMPONENTS)
26 throw invalid_operation("VertexFormat::operator,");
28 VertexFormat r = *this;
29 r.components[r.count++] = c;
34 VertexFormat VertexFormat::operator,(unsigned i) const
37 throw invalid_operation("VertexFormat::operator,");
39 VertexFormat r = *this;
40 unsigned char *c = &r.components[r.count-1];
41 *c = make_indexed_component(static_cast<VertexComponent>(*c), i);
46 bool VertexFormat::operator==(const VertexFormat &other) const
48 if(count!=other.count)
50 return equal(components, components+count, other.components);
53 unsigned VertexFormat::stride() const
56 for(const unsigned char *i=begin(); i!=end(); ++i)
57 s += get_component_size(*i);
61 int VertexFormat::offset(VertexComponent comp) const
63 unsigned type = get_component_type(comp);
64 unsigned size = get_component_size(comp);
66 for(const unsigned char *i=begin(); i!=end(); ++i)
68 if(get_component_type(*i)==type)
70 if(get_component_size(*i)>=size)
76 offs += get_component_size(*i);
82 VertexComponent make_indexed_component(VertexComponent comp, unsigned index)
84 if(comp>=TEXCOORD1 && comp<=TEXCOORD4)
87 throw out_of_range("make_indexed_component");
89 else if(comp>=ATTRIB1 && comp<=ATTRIB4)
92 throw out_of_range("make_indexed_component");
95 throw invalid_argument("make_indexed_component");
96 return static_cast<VertexComponent>(comp+index*4);
99 void operator>>(const LexicalConverter &conv, VertexComponent &c)
101 const string &str = conv.get();
102 if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
103 c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
104 else if(str=="NORMAL3")
106 else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
107 c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
108 else if(str=="COLOR4_UBYTE")
110 else if(str=="TANGENT3")
112 else if(str=="BINORMAL3")
114 else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
117 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
118 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
119 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
121 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
123 else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
128 n = lexical_cast<unsigned>(str.substr(8));
130 catch(const lexical_error &)
132 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
134 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
137 throw lexical_error(format("conversion of '%s' to VertexComponent", str));