]> git.tdb.fi Git - libs/gl.git/blob - source/vertexformat.cpp
16cf28c7c468483bf319705b4ea8d0e30d02e9cf
[libs/gl.git] / source / vertexformat.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007-2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <cstring>
9 #include <msp/strings/lexicalcast.h>
10 #include <msp/strings/utils.h>
11 #include "except.h"
12 #include "vertexformat.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace GL {
18
19 VertexFormat::VertexFormat():
20         data(0)
21 { }
22
23 VertexFormat::VertexFormat(VertexComponent c):
24         data(new unsigned char[8])
25 {
26         data[0] = 1;
27         data[1] = c;
28 }
29
30 VertexFormat::VertexFormat(const VertexFormat &f):
31         data(0)
32 {
33         if(f.data)
34         {
35                 data = new unsigned char[(f.data[0]&~7)+8];
36                 memcpy(data, f.data, f.data[0]+1);
37         }
38 }
39
40 VertexFormat &VertexFormat::operator=(const VertexFormat &f)
41 {
42         delete[] data;
43         if(f.data)
44         {
45                 data = new unsigned char[(f.data[0]&~7)+8];
46                 memcpy(data, f.data, f.data[0]+1);
47         }
48         else
49                 data = 0;
50
51         return *this;
52 }
53
54 VertexFormat::~VertexFormat()
55 {
56         delete[] data;
57 }
58
59 unsigned VertexFormat::stride() const
60 {
61         unsigned s = 0;
62         for(const unsigned char *i=begin(); i!=end(); ++i)
63                 s += (*i&3)+1;
64         return s;
65 }
66
67 int VertexFormat::offset(VertexComponent comp, unsigned index) const
68 {
69         if((comp<TEXCOORD1 && index>0) || (comp<ATTRIB1 && index>=8) || index>=53)
70                 throw InvalidParameterValue("Vertex component index out of range");
71
72         unsigned type = (comp>>2)+index;
73         unsigned size = comp&3;
74         unsigned offs = 0;
75         for(const unsigned char *i=begin(); i!=end(); ++i)
76         {
77                 if(static_cast<unsigned>(*i>>2)==type)
78                 {
79                         if((*i&3)>=size)
80                                 return offs;
81                         else
82                                 return -1;
83                 }
84                 else
85                         offs += (*i&3)+1;
86         }
87
88         return -1;
89 }
90
91 VertexFormat operator,(const VertexFormat &f, VertexComponent c)
92 {
93         VertexFormat r = f;
94         if(r.data)
95         {
96                 const unsigned char n = ++r.data[0];
97                 if((n&7)==7)
98                 {
99                         unsigned char *newdt = new unsigned char[n+9];
100                         memcpy(newdt, r.data, n);
101                         delete r.data;
102                         r.data = newdt;
103                 }
104                 r.data[n] = c;
105         }
106         else
107         {
108                 r.data = new unsigned char[8];
109                 r.data[0] = 1;
110                 r.data[1] = c;
111         }
112
113         return r;
114 }
115
116 VertexFormat operator,(const VertexFormat &f, unsigned i)
117 {
118         if(!f.data)
119                 throw InvalidState("VertexFormat has no components");
120         VertexFormat r = f;
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");
124         *c += i*4;
125
126         return r;
127 }
128
129 void operator>>(const LexicalConverter &conv, VertexComponent &c)
130 {
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")
135                 c = 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")
139                 c = COLOR4_UBYTE;
140         else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
141         {
142                 if(str.size()==9)
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);
146                 else
147                         throw LexicalError("Invalid texture unit in VertexComponent conversion");
148         }
149         else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
150         {
151                 unsigned n;
152                 try
153                 {
154                         n = lexical_cast<unsigned>(str.substr(8));
155                 }
156                 catch(const LexicalError &)
157                 {
158                         throw LexicalError("Invalid attribute in VertexComponent conversion");
159                 }
160                 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
161         }
162         else
163                 throw LexicalError("Invalid input in VertexComponent conversion");
164 }
165
166 // XXX This will go away eventually
167 void operator>>(const LexicalConverter &conv, VertexFormat &f)
168 {
169         vector<string> parts = split(conv.get(), '_');
170         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
171         {
172                 if(*i=="COLOR4UB")
173                         *i = "COLOR4_UBYTE";
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);
180         }
181
182         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
183                 f = (f, lexical_cast<VertexComponent>(*i));
184 }
185
186 } // namespace GL
187 } // namespace Msp