]> git.tdb.fi Git - libs/gl.git/blob - source/vertexformat.cpp
Exception fixes
[libs/gl.git] / source / vertexformat.cpp
1 #include <cstring>
2 #include <msp/strings/format.h>
3 #include <msp/strings/lexicalcast.h>
4 #include <msp/strings/utils.h>
5 #include "error.h"
6 #include "vertexformat.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 VertexFormat::VertexFormat():
14         data(0)
15 { }
16
17 VertexFormat::VertexFormat(VertexComponent c):
18         data(new unsigned char[8])
19 {
20         data[0] = 1;
21         data[1] = c;
22 }
23
24 VertexFormat::VertexFormat(const VertexFormat &f):
25         data(0)
26 {
27         if(f.data)
28         {
29                 data = new unsigned char[(f.data[0]&~7)+8];
30                 memcpy(data, f.data, f.data[0]+1);
31         }
32 }
33
34 VertexFormat &VertexFormat::operator=(const VertexFormat &f)
35 {
36         delete[] data;
37         if(f.data)
38         {
39                 data = new unsigned char[(f.data[0]&~7)+8];
40                 memcpy(data, f.data, f.data[0]+1);
41         }
42         else
43                 data = 0;
44
45         return *this;
46 }
47
48 VertexFormat::~VertexFormat()
49 {
50         delete[] data;
51 }
52
53 unsigned VertexFormat::stride() const
54 {
55         unsigned s = 0;
56         for(const unsigned char *i=begin(); i!=end(); ++i)
57                 s += (*i&3)+1;
58         return s;
59 }
60
61 int VertexFormat::offset(VertexComponent comp, unsigned index) const
62 {
63         if((comp<TEXCOORD1 && index>0) || (comp<ATTRIB1 && index>=8) || index>=53)
64                 throw out_of_range("VertexFormat::offset");
65
66         unsigned type = (comp>>2)+index;
67         unsigned size = comp&3;
68         unsigned offs = 0;
69         for(const unsigned char *i=begin(); i!=end(); ++i)
70         {
71                 if(static_cast<unsigned>(*i>>2)==type)
72                 {
73                         if((*i&3)>=size)
74                                 return offs;
75                         else
76                                 return -1;
77                 }
78                 else
79                         offs += (*i&3)+1;
80         }
81
82         return -1;
83 }
84
85 VertexFormat operator,(const VertexFormat &f, VertexComponent c)
86 {
87         VertexFormat r = f;
88         if(r.data)
89         {
90                 const unsigned char n = ++r.data[0];
91                 if((n&7)==7)
92                 {
93                         unsigned char *newdt = new unsigned char[n+9];
94                         memcpy(newdt, r.data, n);
95                         delete r.data;
96                         r.data = newdt;
97                 }
98                 r.data[n] = c;
99         }
100         else
101         {
102                 r.data = new unsigned char[8];
103                 r.data[0] = 1;
104                 r.data[1] = c;
105         }
106
107         return r;
108 }
109
110 VertexFormat operator,(const VertexFormat &f, unsigned i)
111 {
112         if(!f.data)
113                 throw invalid_operation("VertexFormat::operator,");
114         VertexFormat r = f;
115         unsigned char *c = r.data+r.data[0];
116         if((*c<TEXCOORD1 && i>0) || (*c<ATTRIB1 && i>=8) || i>=53)
117                 throw invalid_argument("VertexFormat::operator,");
118         *c += i*4;
119
120         return r;
121 }
122
123 void operator>>(const LexicalConverter &conv, VertexComponent &c)
124 {
125         const string &str = conv.get();
126         if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
127                 c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
128         else if(str=="NORMAL3")
129                 c = NORMAL3;
130         else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
131                 c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
132         else if(str=="COLOR4_UBYTE")
133                 c = COLOR4_UBYTE;
134         else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
135         {
136                 if(str.size()==9)
137                         c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
138                 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
139                         c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
140                 else
141                         throw lexical_error(format("conversion of '%s' to VertexComponent", str));
142         }
143         else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
144         {
145                 unsigned n;
146                 try
147                 {
148                         n = lexical_cast<unsigned>(str.substr(8));
149                 }
150                 catch(const lexical_error &)
151                 {
152                         throw lexical_error(format("conversion of '%s' to VertexComponent", str));
153                 }
154                 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
155         }
156         else
157                 throw lexical_error(format("conversion of '%s' to VertexComponent", str));
158 }
159
160 // XXX This will go away eventually
161 void operator>>(const LexicalConverter &conv, VertexFormat &f)
162 {
163         vector<string> parts = split(conv.get(), '_');
164         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
165         {
166                 if(*i=="COLOR4UB")
167                         *i = "COLOR4_UBYTE";
168                 else if(i->size()==7 && !i->compare(0, 5, "COLOR") && (*i)[6]=='F')
169                         *i = i->substr(0, 6)+"_FLOAT";
170                 else if(i->size()>=10 && !i->compare(0, 8, "TEXCOORD"))
171                         *i = i->substr(0, 9)+"_"+i->substr(9);
172                 else if(i->size()>=8 && !i->compare(0, 6, "ATTRIB"))
173                         *i = i->substr(0, 7)+"_"+i->substr(7);
174         }
175
176         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
177                 f = (f, lexical_cast<VertexComponent>(*i));
178 }
179
180 } // namespace GL
181 } // namespace Msp