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]&~7)+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]&~7)+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];
97 if((*c<TEXCOORD1 && i>0) || (*c<ATTRIB1 && i>=8) || i>=53)
98 throw InvalidParameterValue("Vertex component index out of range");
104 istream &operator>>(istream &in, VertexFormat &f)
113 string::size_type underscore = str.find('_', start);
115 if(!str.compare(start, underscore-start, "VERTEX2"))
117 else if(!str.compare(start, underscore-start, "VERTEX3"))
119 else if(!str.compare(start, underscore-start, "VERTEX4"))
121 else if(!str.compare(start, underscore-start, "NORMAL3"))
123 else if(!str.compare(start, underscore-start, "TEXCOORD1"))
125 else if(!str.compare(start, underscore-start, "TEXCOORD2"))
127 else if(!str.compare(start, underscore-start, "TEXCOORD3"))
129 else if(!str.compare(start, underscore-start, "TEXCOORD4"))
131 else if(!str.compare(start, underscore-start, "COLOR4UB"))
132 f = (f,COLOR4_UBYTE);
133 else if(!str.compare(start, underscore-start, "COLOR3F"))
134 f = (f,COLOR3_FLOAT);
135 else if(!str.compare(start, underscore-start, "COLOR4F"))
136 f = (f,COLOR4_FLOAT);
137 else if(underscore>=start+8 && !str.compare(start, 6, "ATTRIB"))
141 char n = str[start+6];
142 unsigned i = lexical_cast<unsigned>(str.substr(start+7, underscore-start-7));
154 catch(const LexicalError &)
164 in.setstate(ios_base::failbit);
168 if(underscore==string::npos)
170 start = underscore+1;