3 This file is part of libmspgl
4 Copyright © 2007-2009 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include <msp/strings/lexicalcast.h>
11 #include "vertexformat.h"
16 VertexFormat::VertexFormat():
20 VertexFormat::VertexFormat(VertexComponent c):
21 data(new unsigned char[8])
27 VertexFormat::VertexFormat(const VertexFormat &f):
32 data=new unsigned char[f.data[0]/8+8];
33 memcpy(data, f.data, f.data[0]+1);
37 VertexFormat &VertexFormat::operator=(const VertexFormat &f)
42 data=new unsigned char[f.data[0]/8+8];
43 memcpy(data, f.data, f.data[0]+1);
51 VertexFormat::~VertexFormat()
56 VertexFormat operator,(const VertexFormat &f, VertexComponent c)
61 const unsigned char n=++r.data[0];
64 unsigned char *newdt=new unsigned char[n+9];
65 memcpy(newdt, r.data, n);
73 r.data=new unsigned char[8];
81 VertexFormat operator,(const VertexFormat &f, unsigned i)
84 throw InvalidState("VertexFormat has no components");
86 unsigned char *c=r.data+r.data[0];
88 throw InvalidState("Last component is not a generic attribute");
89 // VertexArray uses an unsigned to store flags for enabled arrays
91 throw InvalidParameterValue("Generic attribute index out of range");
97 uint get_stride(const VertexFormat &f)
100 for(const unsigned char *i=f.begin(); i!=f.end(); ++i)
105 std::istream &operator>>(std::istream &in, VertexFormat &f)
114 unsigned underscore=str.find('_', start);
116 if(!str.compare(start, underscore-start, "VERTEX2"))
118 else if(!str.compare(start, underscore-start, "VERTEX3"))
120 else if(!str.compare(start, underscore-start, "VERTEX4"))
122 else if(!str.compare(start, underscore-start, "NORMAL3"))
124 else if(!str.compare(start, underscore-start, "TEXCOORD1"))
126 else if(!str.compare(start, underscore-start, "TEXCOORD2"))
128 else if(!str.compare(start, underscore-start, "TEXCOORD3"))
130 else if(!str.compare(start, underscore-start, "TEXCOORD4"))
132 else if(!str.compare(start, underscore-start, "COLOR4UB"))
134 else if(!str.compare(start, underscore-start, "COLOR3F"))
136 else if(!str.compare(start, underscore-start, "COLOR4F"))
138 else if(underscore>=start+8 && !str.compare(start, 6, "ATTRIB"))
143 unsigned i=lexical_cast<unsigned>(str.substr(start+7, underscore-start-7));
155 catch(const LexicalError &)
165 in.setstate(std::ios_base::failbit);
169 if(underscore==std::string::npos)