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>
11 #include "vertexformat.h"
18 VertexFormat::VertexFormat():
22 VertexFormat::VertexFormat(VertexComponent c):
23 data(new unsigned char[8])
29 VertexFormat::VertexFormat(const VertexFormat &f):
34 data=new unsigned char[f.data[0]/8+8];
35 memcpy(data, f.data, f.data[0]+1);
39 VertexFormat &VertexFormat::operator=(const VertexFormat &f)
44 data=new unsigned char[f.data[0]/8+8];
45 memcpy(data, f.data, f.data[0]+1);
53 VertexFormat::~VertexFormat()
58 unsigned VertexFormat::stride() const
61 for(const unsigned char *i=begin(); i!=end(); ++i)
66 VertexFormat operator,(const VertexFormat &f, VertexComponent c)
71 const unsigned char n=++r.data[0];
74 unsigned char *newdt=new unsigned char[n+9];
75 memcpy(newdt, r.data, n);
83 r.data=new unsigned char[8];
91 VertexFormat operator,(const VertexFormat &f, unsigned i)
94 throw InvalidState("VertexFormat has no components");
96 unsigned char *c=r.data+r.data[0];
98 throw InvalidState("Last component is not a generic attribute");
99 // VertexArray uses an unsigned to store flags for enabled arrays
101 throw InvalidParameterValue("Generic attribute index out of range");
107 istream &operator>>(istream &in, VertexFormat &f)
116 string::size_type underscore=str.find('_', start);
118 if(!str.compare(start, underscore-start, "VERTEX2"))
120 else if(!str.compare(start, underscore-start, "VERTEX3"))
122 else if(!str.compare(start, underscore-start, "VERTEX4"))
124 else if(!str.compare(start, underscore-start, "NORMAL3"))
126 else if(!str.compare(start, underscore-start, "TEXCOORD1"))
128 else if(!str.compare(start, underscore-start, "TEXCOORD2"))
130 else if(!str.compare(start, underscore-start, "TEXCOORD3"))
132 else if(!str.compare(start, underscore-start, "TEXCOORD4"))
134 else if(!str.compare(start, underscore-start, "COLOR4UB"))
136 else if(!str.compare(start, underscore-start, "COLOR3F"))
138 else if(!str.compare(start, underscore-start, "COLOR4F"))
140 else if(underscore>=start+8 && !str.compare(start, 6, "ATTRIB"))
145 unsigned i=lexical_cast<unsigned>(str.substr(start+7, underscore-start-7));
157 catch(const LexicalError &)
167 in.setstate(ios_base::failbit);
171 if(underscore==string::npos)