1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
/*
* Copyright 2011, Blender Foundation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "mesh.h"
#include "attribute.h"
#include "util_debug.h"
#include "util_foreach.h"
CCL_NAMESPACE_BEGIN
/* Attribute */
void Attribute::set(ustring name_, TypeDesc type_, Element element_)
{
name = name_;
type = type_;
element = element_;
std = STD_NONE;
/* string and matrix not supported! */
assert(type == TypeDesc::TypeFloat || type == TypeDesc::TypeColor ||
type == TypeDesc::TypePoint || type == TypeDesc::TypeVector ||
type == TypeDesc::TypeNormal);
}
void Attribute::reserve(int numverts, int numtris)
{
buffer.resize(buffer_size(numverts, numtris), 0);
}
size_t Attribute::data_sizeof()
{
if(type == TypeDesc::TypeFloat)
return sizeof(float);
else
return sizeof(float3);
}
size_t Attribute::element_size(int numverts, int numtris)
{
if(element == VERTEX)
return numverts;
else if(element == FACE)
return numtris;
else
return numtris*3;
}
size_t Attribute::buffer_size(int numverts, int numtris)
{
return element_size(numverts, numtris)*data_sizeof();
}
bool Attribute::same_storage(TypeDesc a, TypeDesc b)
{
if(a == b)
return true;
if(a == TypeDesc::TypeColor || a == TypeDesc::TypePoint ||
a == TypeDesc::TypeVector || a == TypeDesc::TypeNormal)
if(b == TypeDesc::TypeColor || b == TypeDesc::TypePoint ||
b == TypeDesc::TypeVector || b == TypeDesc::TypeNormal)
return true;
return false;
}
ustring Attribute::standard_name(Attribute::Standard std)
{
if(std == Attribute::STD_VERTEX_NORMAL)
return ustring("N");
else if(std == Attribute::STD_FACE_NORMAL)
return ustring("Ng");
else if(std == Attribute::STD_UV)
return ustring("uv");
else if(std == Attribute::STD_GENERATED)
return ustring("generated");
else if(std == Attribute::STD_POSITION_UNDEFORMED)
return ustring("undeformed");
else if(std == Attribute::STD_POSITION_UNDISPLACED)
return ustring("undisplaced");
return ustring();
}
/* Attribute Set */
AttributeSet::AttributeSet()
{
mesh = NULL;
}
AttributeSet::~AttributeSet()
{
}
Attribute *AttributeSet::add(ustring name, TypeDesc type, Attribute::Element element)
{
Attribute *attr = find(name);
if(attr) {
/* return if same already exists */
if(attr->type == type && attr->element == element)
return attr;
/* overwrite attribute with same name but different type/element */
remove(name);
}
attributes.push_back(Attribute());
attr = &attributes.back();
if(element == Attribute::VERTEX)
attr->set(name, type, element);
else if(element == Attribute::FACE)
attr->set(name, type, element);
else if(element == Attribute::CORNER)
attr->set(name, type, element);
if(mesh)
attr->reserve(mesh->verts.size(), mesh->triangles.size());
return attr;
}
Attribute *AttributeSet::find(ustring name)
{
foreach(Attribute& attr, attributes)
if(attr.name == name)
return &attr;
return NULL;
}
void AttributeSet::remove(ustring name)
{
Attribute *attr = find(name);
if(attr) {
list<Attribute>::iterator it;
for(it = attributes.begin(); it != attributes.end(); it++) {
if(&*it == attr) {
attributes.erase(it);
return;
}
}
}
}
Attribute *AttributeSet::add(Attribute::Standard std, ustring name)
{
Attribute *attr = NULL;
if(name == ustring())
name = Attribute::standard_name(std);
if(std == Attribute::STD_VERTEX_NORMAL)
attr = add(name, TypeDesc::TypeNormal, Attribute::VERTEX);
else if(std == Attribute::STD_FACE_NORMAL)
attr = add(name, TypeDesc::TypeNormal, Attribute::FACE);
else if(std == Attribute::STD_UV)
attr = add(name, TypeDesc::TypePoint, Attribute::CORNER);
else if(std == Attribute::STD_GENERATED)
attr = add(name, TypeDesc::TypePoint, Attribute::VERTEX);
else if(std == Attribute::STD_POSITION_UNDEFORMED)
attr = add(name, TypeDesc::TypePoint, Attribute::VERTEX);
else if(std == Attribute::STD_POSITION_UNDISPLACED)
attr = add(name, TypeDesc::TypePoint, Attribute::VERTEX);
else
assert(0);
attr->std = std;
return attr;
}
Attribute *AttributeSet::find(Attribute::Standard std)
{
foreach(Attribute& attr, attributes)
if(attr.std == std)
return &attr;
return NULL;
}
void AttributeSet::remove(Attribute::Standard std)
{
Attribute *attr = find(std);
if(attr) {
list<Attribute>::iterator it;
for(it = attributes.begin(); it != attributes.end(); it++) {
if(&*it == attr) {
attributes.erase(it);
return;
}
}
}
}
Attribute *AttributeSet::find(AttributeRequest& req)
{
if(req.std == Attribute::STD_NONE)
return find(req.name);
else
return find(req.std);
}
void AttributeSet::reserve(int numverts, int numtris)
{
foreach(Attribute& attr, attributes)
attr.reserve(numverts, numtris);
}
void AttributeSet::clear()
{
attributes.clear();
}
/* AttributeRequest */
AttributeRequest::AttributeRequest(ustring name_)
{
name = name_;
std = Attribute::STD_NONE;
type = TypeDesc::TypeFloat;
element = ATTR_ELEMENT_NONE;
offset = 0;
}
AttributeRequest::AttributeRequest(Attribute::Standard std_)
{
name = ustring();
std = std_;
type = TypeDesc::TypeFloat;
element = ATTR_ELEMENT_NONE;
offset = 0;
}
/* AttributeRequestSet */
AttributeRequestSet::AttributeRequestSet()
{
}
AttributeRequestSet::~AttributeRequestSet()
{
}
bool AttributeRequestSet::modified(const AttributeRequestSet& other)
{
if(requests.size() != other.requests.size())
return true;
for(size_t i = 0; i < requests.size(); i++) {
bool found = false;
for(size_t j = 0; j < requests.size() && !found; j++)
if(requests[i].name == other.requests[j].name &&
requests[i].std == other.requests[j].std)
found = true;
if(!found)
return true;
}
return false;
}
void AttributeRequestSet::add(ustring name)
{
foreach(AttributeRequest& req, requests)
if(req.name == name)
return;
requests.push_back(AttributeRequest(name));
}
void AttributeRequestSet::add(Attribute::Standard std)
{
foreach(AttributeRequest& req, requests)
if(req.std == std)
return;
requests.push_back(AttributeRequest(std));
}
void AttributeRequestSet::add(AttributeRequestSet& reqs)
{
foreach(AttributeRequest& req, reqs.requests) {
if(req.std == Attribute::STD_NONE)
add(req.name);
else
add(req.std);
}
}
bool AttributeRequestSet::find(ustring name)
{
foreach(AttributeRequest& req, requests)
if(req.name == name)
return true;
return false;
}
bool AttributeRequestSet::find(Attribute::Standard std)
{
foreach(AttributeRequest& req, requests)
if(req.std == std)
return true;
return false;
}
size_t AttributeRequestSet::size()
{
return requests.size();
}
void AttributeRequestSet::clear()
{
requests.clear();
}
CCL_NAMESPACE_END
|