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 unsigned VertexFormat::stride() const
49 for(const unsigned char *i=begin(); i!=end(); ++i)
50 s += get_component_size(*i);
54 int VertexFormat::offset(VertexComponent comp) const
56 unsigned type = get_component_type(comp);
57 unsigned size = get_component_size(comp);
59 for(const unsigned char *i=begin(); i!=end(); ++i)
61 if(get_component_type(*i)==type)
63 if(get_component_size(*i)>=size)
69 offs += get_component_size(*i);
75 VertexComponent make_indexed_component(VertexComponent comp, unsigned index)
77 if(comp>=TEXCOORD1 && comp<=TEXCOORD4)
80 throw out_of_range("make_indexed_component");
82 else if(comp>=ATTRIB1 && comp<=ATTRIB4)
85 throw out_of_range("make_indexed_component");
88 throw invalid_argument("make_indexed_component");
89 return static_cast<VertexComponent>(comp+index*4);
92 void operator>>(const LexicalConverter &conv, VertexComponent &c)
94 const string &str = conv.get();
95 if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
96 c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
97 else if(str=="NORMAL3")
99 else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
100 c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
101 else if(str=="COLOR4_UBYTE")
103 else if(str=="TANGENT3")
105 else if(str=="BINORMAL3")
107 else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
110 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
111 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
112 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
114 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
116 else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
121 n = lexical_cast<unsigned>(str.substr(8));
123 catch(const lexical_error &)
125 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
127 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
130 throw lexical_error(format("conversion of '%s' to VertexComponent", str));