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