]> git.tdb.fi Git - libs/gl.git/blob - source/vertexformat.cpp
Support multiple sets of texture coordinates in datafiles
[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 VertexFormat operator,(const VertexFormat &f, VertexComponent c)
68 {
69         VertexFormat r = f;
70         if(r.data)
71         {
72                 const unsigned char n = ++r.data[0];
73                 if((n&7)==7)
74                 {
75                         unsigned char *newdt = new unsigned char[n+9];
76                         memcpy(newdt, r.data, n);
77                         delete r.data;
78                         r.data = newdt;
79                 }
80                 r.data[n] = c;
81         }
82         else
83         {
84                 r.data = new unsigned char[8];
85                 r.data[0] = 1;
86                 r.data[1] = c;
87         }
88
89         return r;
90 }
91
92 VertexFormat operator,(const VertexFormat &f, unsigned i)
93 {
94         if(!f.data)
95                 throw InvalidState("VertexFormat has no components");
96         VertexFormat r = f;
97         unsigned char *c = r.data+r.data[0];
98         if((*c<TEXCOORD1 && i>0) || (*c<ATTRIB1 && i>=8) || i>=53)
99                 throw InvalidParameterValue("Vertex component index out of range");
100         *c += i*4;
101
102         return r;
103 }
104
105 void operator>>(const LexicalConverter &conv, VertexComponent &c)
106 {
107         const string &str = conv.get();
108         if(str.size()==7 && !str.compare(0, 6, "VERTEX") && str[6]>='2' && str[6]<='4')
109                 c = static_cast<VertexComponent>(VERTEX2+(str[6]-'2'));
110         else if(str=="NORMAL3")
111                 c = NORMAL3;
112         else if(str.size()==12 && !str.compare(0, 5, "COLOR") && str[5]>='3' && str[5]<='4' && !str.compare(6, 6, "_FLOAT"))
113                 c = static_cast<VertexComponent>(COLOR3_FLOAT+(str[5]-'3'));
114         else if(str=="COLOR4_UBYTE")
115                 c = COLOR4_UBYTE;
116         else if(str.size()>=9 && !str.compare(0, 8, "TEXCOORD") && str[8]>='1' && str[8]<='4')
117         {
118                 if(str.size()==9)
119                         c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1'));
120                 else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7')
121                         c = static_cast<VertexComponent>(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4);
122                 else
123                         throw LexicalError("Invalid texture unit in VertexComponent conversion");
124         }
125         else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_')
126         {
127                 unsigned n;
128                 try
129                 {
130                         n = lexical_cast<unsigned>(str.substr(8));
131                 }
132                 catch(const LexicalError &)
133                 {
134                         throw LexicalError("Invalid attribute in VertexComponent conversion");
135                 }
136                 c = static_cast<VertexComponent>(ATTRIB1+(str[6]-'1')+n*4);
137         }
138         else
139                 throw LexicalError("Invalid input in VertexComponent conversion");
140 }
141
142 // XXX This will go away eventually
143 void operator>>(const LexicalConverter &conv, VertexFormat &f)
144 {
145         vector<string> parts = split(conv.get(), '_');
146         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
147         {
148                 if(*i=="COLOR4UB")
149                         *i = "COLOR4_UBYTE";
150                 else if(i->size()==7 && !i->compare(0, 5, "COLOR") && (*i)[6]=='F')
151                         *i = i->substr(0, 6)+"_FLOAT";
152                 else if(i->size()>=10 && !i->compare(0, 8, "TEXCOORD"))
153                         *i = i->substr(0, 9)+"_"+i->substr(9);
154                 else if(i->size()>=8 && !i->compare(0, 6, "ATTRIB"))
155                         *i = i->substr(0, 7)+"_"+i->substr(7);
156         }
157
158         for(vector<string>::iterator i=parts.begin(); i!=parts.end(); ++i)
159                 f = (f, lexical_cast<VertexComponent>(*i));
160 }
161
162 } // namespace GL
163 } // namespace Msp