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 VertexFormat operator,(const VertexFormat &f, VertexComponent c)
72 const unsigned char n = ++r.data[0];
75 unsigned char *newdt = new unsigned char[n+9];
76 memcpy(newdt, r.data, n);
84 r.data = new unsigned char[8];
92 VertexFormat operator,(const VertexFormat &f, unsigned i)
95 throw InvalidState("VertexFormat has no components");
97 unsigned char *c = r.data+r.data[0];
98 if((*c<TEXCOORD1 && i>0) || (*c<ATTRIB1 && i>=8) || i>=53)
99 throw InvalidParameterValue("Vertex component index out of range");
105 void operator>>(const LexicalConverter &conv, VertexComponent &c)
107 const string &str = conv.get();
108 if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
109 c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
110 else if(str=="NORMAL3")
112 else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
113 c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
114 else if(str=="COLOR4_UBYTE")
116 else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
119 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
120 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
121 c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
123 throw LexicalError("Invalid texture unit in VertexComponent conversion");
125 else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
130 n = lexical_cast<unsigned>(str.substr(8));
132 catch(const LexicalError &)
134 throw LexicalError("Invalid attribute in VertexComponent conversion");
136 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
139 throw LexicalError("Invalid input in VertexComponent conversion");
142 // XXX This will go away eventually
143 void operator>>(const LexicalConverter &conv, VertexFormat &f)
145 vector<string> parts = split(conv.get(), '_');
146 for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
150 else if(i->size()==7 && !i->compare(0, 5, "COLOR") && (*i)[6]=='F')
151 *i = i->substr(0, 6)+"_FLOAT";
152 else if(i->size()>=10 && !i->compare(0, 8, "TEXCOORD"))
153 *i = i->substr(0, 9)+"_"+i->substr(9);
154 else if(i->size()>=8 && !i->compare(0, 6, "ATTRIB"))
155 *i = i->substr(0, 7)+"_"+i->substr(7);
158 for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
159 f = (f, lexical_cast<VertexComponent>(*i));