3 This file is part of libmspgl
4 Copyright © 2007-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include <msp/strings/lexicalcast.h>
10 #include <msp/strings/utils.h>
12 #include "vertexformat.h"
19 VertexFormat::VertexFormat():
23 VertexFormat::VertexFormat(VertexComponent c):
24 data(new unsigned char[8])
30 VertexFormat::VertexFormat(const VertexFormat &f):
35 data = new unsigned char[(f.data[0]&~7)+8];
36 memcpy(data, f.data, f.data[0]+1);
40 VertexFormat &VertexFormat::operator=(const VertexFormat &f)
45 data = new unsigned char[(f.data[0]&~7)+8];
46 memcpy(data, f.data, f.data[0]+1);
54 VertexFormat::~VertexFormat()
59 unsigned VertexFormat::stride() const
62 for(const unsigned char *i=begin(); i!=end(); ++i)
67 int VertexFormat::offset(VertexComponent comp, unsigned index) const
69 if((comp<TEXCOORD1 && index>0) || (comp<ATTRIB1 && index>=8) || index>=53)
70 throw InvalidParameterValue("Vertex component index out of range");
72 unsigned type = (comp>>2)+index;
73 unsigned size = comp&3;
75 for(const unsigned char *i=begin(); i!=end(); ++i)
91 VertexFormat operator,(const VertexFormat &f, VertexComponent c)
96 const unsigned char n = ++r.data[0];
99 unsigned char *newdt = new unsigned char[n+9];
100 memcpy(newdt, r.data, n);
108 r.data = new unsigned char[8];
116 VertexFormat operator,(const VertexFormat &f, unsigned i)
119 throw InvalidState("VertexFormat has no components");
121 unsigned char *c = r.data+r.data[0];
122 if((*c<TEXCOORD1 && i>0) || (*c<ATTRIB1 && i>=8) || i>=53)
123 throw InvalidParameterValue("Vertex component index out of range");
129 void operator>>(const LexicalConverter &conv, VertexComponent &c)
131 const string &str = conv.get();
132 if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
133 c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
134 else if(str=="NORMAL3")
136 else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
137 c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
138 else if(str=="COLOR4_UBYTE")
140 else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
143 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
144 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
145 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
147 throw LexicalError("Invalid texture unit in VertexComponent conversion");
149 else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
154 n = lexical_cast<unsigned>(str.substr(8));
156 catch(const LexicalError &)
158 throw LexicalError("Invalid attribute in VertexComponent conversion");
160 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
163 throw LexicalError("Invalid input in VertexComponent conversion");
166 // XXX This will go away eventually
167 void operator>>(const LexicalConverter &conv, VertexFormat &f)
169 vector<string> parts = split(conv.get(), '_');
170 for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
174 else if(i->size()==7 && !i->compare(0, 5, "COLOR") && (*i)[6]=='F')
175 *i = i->substr(0, 6)+"_FLOAT";
176 else if(i->size()>=10 && !i->compare(0, 8, "TEXCOORD"))
177 *i = i->substr(0, 9)+"_"+i->substr(9);
178 else if(i->size()>=8 && !i->compare(0, 6, "ATTRIB"))
179 *i = i->substr(0, 7)+"_"+i->substr(7);
182 for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
183 f = (f, lexical_cast<VertexComponent>(*i));