2 #include <msp/strings/lexicalcast.h>
3 #include <msp/strings/utils.h>
5 #include "vertexformat.h"
12 VertexFormat::VertexFormat():
16 VertexFormat::VertexFormat(VertexComponent c):
17 data(new unsigned char[8])
23 VertexFormat::VertexFormat(const VertexFormat &f):
28 data = new unsigned char[(f.data[0]&~7)+8];
29 memcpy(data, f.data, f.data[0]+1);
33 VertexFormat &VertexFormat::operator=(const VertexFormat &f)
38 data = new unsigned char[(f.data[0]&~7)+8];
39 memcpy(data, f.data, f.data[0]+1);
47 VertexFormat::~VertexFormat()
52 unsigned VertexFormat::stride() const
55 for(const unsigned char *i=begin(); i!=end(); ++i)
60 int VertexFormat::offset(VertexComponent comp, unsigned index) const
62 if((comp<TEXCOORD1 && index>0) || (comp<ATTRIB1 && index>=8) || index>=53)
63 throw InvalidParameterValue("Vertex component index out of range");
65 unsigned type = (comp>>2)+index;
66 unsigned size = comp&3;
68 for(const unsigned char *i=begin(); i!=end(); ++i)
70 if(static_cast<unsigned>(*i>>2)==type)
84 VertexFormat operator,(const VertexFormat &f, VertexComponent c)
89 const unsigned char n = ++r.data[0];
92 unsigned char *newdt = new unsigned char[n+9];
93 memcpy(newdt, r.data, n);
101 r.data = new unsigned char[8];
109 VertexFormat operator,(const VertexFormat &f, unsigned i)
112 throw InvalidState("VertexFormat has no components");
114 unsigned char *c = r.data+r.data[0];
115 if((*c<TEXCOORD1 && i>0) || (*c<ATTRIB1 && i>=8) || i>=53)
116 throw InvalidParameterValue("Vertex component index out of range");
122 void operator>>(const LexicalConverter &conv, VertexComponent &c)
124 const string &str = conv.get();
125 if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
126 c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
127 else if(str=="NORMAL3")
129 else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
130 c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
131 else if(str=="COLOR4_UBYTE")
133 else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
136 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
137 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
138 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
140 throw LexicalError("Invalid texture unit in VertexComponent conversion");
142 else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
147 n = lexical_cast<unsigned>(str.substr(8));
149 catch(const LexicalError &)
151 throw LexicalError("Invalid attribute in VertexComponent conversion");
153 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
156 throw LexicalError("Invalid input in VertexComponent conversion");
159 // XXX This will go away eventually
160 void operator>>(const LexicalConverter &conv, VertexFormat &f)
162 vector<string> parts = split(conv.get(), '_');
163 for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
167 else if(i->size()==7 && !i->compare(0, 5, "COLOR") && (*i)[6]=='F')
168 *i = i->substr(0, 6)+"_FLOAT";
169 else if(i->size()>=10 && !i->compare(0, 8, "TEXCOORD"))
170 *i = i->substr(0, 9)+"_"+i->substr(9);
171 else if(i->size()>=8 && !i->compare(0, 6, "ATTRIB"))
172 *i = i->substr(0, 7)+"_"+i->substr(7);
175 for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
176 f = (f, lexical_cast<VertexComponent>(*i));