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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
|
#include <string.h>
#include <kalmia.h>
#include "util/util.h"
#include "xml/xml.h"
#include "geometry.h"
/* macro to ensure tag type matches what we expect */
#define CHECK_TAG_TYPE(expected) \
if (strcmp(src->type, expected) != 0) { \
return -1; \
}
int kai_read_float_array(struct ka_float_array_t *dest, struct kai_tag_t *src)
{
CHECK_TAG_TYPE("float_array");
long count = kai_tag_attr_to_long(src, "count", -1);
if (count == -1) {
/* count not present */
return -1;
}
dest->count = count;
dest->id = kai_tag_attr_to_dup(src, "id");
dest->digits = kai_tag_attr_to_long(src, "digits", 6);
dest->magnitude = kai_tag_attr_to_long(src, "magnitude", 38);
/* allocate & fill buffer */
dest->buf = kai_alloc(
dest->count * sizeof(ka_real_t),
"float_array buffer"
);
if (dest->buf == NULL) {
free(dest->id);
return -1;
}
kai_text_to_reals(dest->buf, src->content, dest->count);
return 0;
}
void kai_release_float_array(struct ka_float_array_t fa)
{
free(fa.id);
free(fa.buf);
}
int kai_read_param(struct ka_param_t *dest, struct kai_tag_t *src)
{
CHECK_TAG_TYPE("param");
char *type = kai_tag_attr_to_dup(src, "type");
if (type == NULL) {
return -1;
}
dest->name = kai_tag_attr_to_dup(src, "name");
dest->sid = kai_tag_attr_to_dup(src, "sid");
dest->type = type;
dest->semantic = kai_tag_attr_to_dup(src, "semantic");
return 0;
}
void kai_release_param(struct ka_param_t p)
{
free(p.name);
free(p.sid);
free(p.type);
free(p.semantic);
}
int kai_read_accessor(struct ka_accessor_t *dest, struct kai_tag_t *src)
{
CHECK_TAG_TYPE("accessor");
long count = kai_tag_attr_to_long(src, "count", -1);
if (count == -1) { return -1; }
char *source = kai_tag_attr_to_dup(src, "source");
if (source == NULL) { return -1; }
dest->count = count;
dest->offset = kai_tag_attr_to_long(src, "offset", 0);
dest->source = source;
dest->stride = kai_tag_attr_to_long(src, "stride", 1);
int result;
KAI_FILL_ARRAY_FROM_TAGS(
result,
dest->param, dest->param_count, struct ka_param_t,
src, "param",
kai_read_param, kai_release_param
);
if (result != 0) {
free(source);
return -1;
}
return 0;
}
void kai_release_accessor(struct ka_accessor_t a)
{
free(a.source);
int i;
for (i=0; i<a.param_count; i++) {
kai_release_param(a.param[i]);
}
free(a.param);
}
int kai_read_source(struct ka_source_t *dest, struct kai_tag_t *src)
{
CHECK_TAG_TYPE("source");
char *id = kai_tag_attr_to_dup(src, "id");
if (id == NULL) { return -1; }
dest->id = id;
dest->name = kai_tag_attr_to_dup(src, "name");
struct kai_tag_t *child;
int result;
child = kai_tag_get_first_child_with_type(src, "float_array");
result = kai_read_float_array(&(dest->float_array), child);
if (result != 0) {
free(dest->id);
free(dest->name);
return -1;
}
child = kai_tag_get_first_child_with_type(src, "technique_common");
if (child == NULL) {
free(dest->id);
free(dest->name);
kai_release_float_array(dest->float_array);
return -1;
}
child = kai_tag_get_first_child_with_type(child, "accessor");
if (child == NULL) {
free(dest->id);
free(dest->name);
kai_release_float_array(dest->float_array);
return -1;
}
result = kai_read_accessor(&(dest->accessor), child);
if (result != 0) {
free(dest->id);
free(dest->name);
kai_release_float_array(dest->float_array);
return -1;
}
return 0;
}
void kai_release_source(struct ka_source_t s)
{
free(s.id);
free(s.name);
kai_release_float_array(s.float_array);
kai_release_accessor(s.accessor);
}
int kai_read_input_unshared(struct ka_input_t *dest, struct kai_tag_t *src)
{
CHECK_TAG_TYPE("input");
char *semantic = kai_tag_attr_to_dup(src, "semantic");
if (semantic == NULL) { return -1; }
char *source = kai_tag_attr_to_dup(src, "source");
if (source == NULL) { free(semantic); return -1; }
dest->shared = false;
dest->offset = 0;
dest->semantic = semantic;
dest->source = source;
dest->set = 0;
return 0;
}
int kai_read_input_shared(struct ka_input_t *dest, struct kai_tag_t *src)
{
int result = kai_read_input_unshared(dest, src);
if (result != 0) { return result; }
long offset = kai_tag_attr_to_long(src, "offset", -1);
if (offset == -1) { return -1; }
dest->shared = true;
dest->offset = offset;
long set = kai_tag_attr_to_long(src, "set", -1);
if (set != -1) {
dest->use_set = true;
dest->set = set;
}
else {
dest->use_set = false;
}
return 0;
}
void kai_release_input(struct ka_input_t i)
{
free(i.semantic);
free(i.source);
}
int kai_read_vertices(struct ka_vertices_t *dest, struct kai_tag_t *src)
{
CHECK_TAG_TYPE("vertices");
char *id = kai_tag_attr_to_dup(src, "id");
if (id == NULL) { return -1; }
dest->id = id;
dest->name = kai_tag_attr_to_dup(src, "name");
int result;
KAI_FILL_ARRAY_FROM_TAGS(
result,
dest->input, dest->input_count, struct ka_input_t,
src, "input",
kai_read_input_unshared, kai_release_input
);
if (result != 0) {
kai_release_vertices(*dest);
return -1;
}
return 0;
}
void kai_release_vertices(struct ka_vertices_t v)
{
free(v.id);
free(v.name);
int i;
for (i=0; i<v.input_count; i++) {
kai_release_input(v.input[i]);
}
free(v.input);
}
int kai_read_triangles(struct ka_triangles_t *dest, struct kai_tag_t *src)
{
CHECK_TAG_TYPE("triangles");
long count = kai_tag_attr_to_long(src, "count", -1);
if (count == -1) { return -1; }
dest->name = kai_tag_attr_to_dup(src, "name");
dest->count = count;
dest->material = kai_tag_attr_to_dup(src, "material");
/* zero these out so that any error-related calls to kai_release_triangles
* won't cause an invalid free
*/
dest->input_count = 0;
dest->input = NULL;
dest->p_count = 0;
dest->p = NULL;
/* load the child inputs into the structure */
int result;
KAI_FILL_ARRAY_FROM_TAGS(
result,
dest->input, dest->input_count, struct ka_input_t,
src, "input",
kai_read_input_shared, kai_release_input
);
if (result != 0) {
kai_release_triangles(*dest);
return -1;
}
/* load the child p tag */
struct kai_tag_t *p = kai_tag_get_first_child_with_type(src, "p");
if (p != NULL) {
dest->p_count = 3 * dest->count * dest->input_count;
dest->p = kai_alloc(dest->p_count * sizeof(unsigned int), "triangles primitive array");
if (dest->p == NULL) {
dest->p_count = 0;
kai_release_triangles(*dest);
return -1;
}
kai_text_to_uints(dest->p, p->content, dest->p_count);
}
return 0;
}
void kai_release_triangles(struct ka_triangles_t t)
{
free(t.name);
free(t.material);
int i;
for (i=0; i<t.input_count; i++) {
kai_release_input(t.input[i]);
}
free(t.input);
free(t.p);
}
int kai_read_mesh(struct ka_mesh_t *dest, struct kai_tag_t *src)
{
CHECK_TAG_TYPE("mesh");
dest->source_count = 0;
dest->source = NULL;
dest->triangles_count = 0;
dest->triangles = NULL;
/* load vertices first bc if we don't kai_release_mesh will do bad things if we hit an error */
struct kai_tag_t *child;
int result;
child = kai_tag_get_first_child_with_type(src, "vertices");
if (child == NULL) { return -1; }
result = kai_read_vertices(&(dest->vertices), child);
if (result != 0) { return -1; }
/* load sources */
KAI_FILL_ARRAY_FROM_TAGS(
result,
dest->source, dest->source_count, struct ka_source_t,
src, "source",
kai_read_source, kai_release_source
);
if (result != 0) {
kai_release_mesh(*dest);
return -1;
}
/* load triangles */
KAI_FILL_ARRAY_FROM_TAGS(
result,
dest->triangles, dest->triangles_count, struct ka_triangles_t,
src, "triangles",
kai_read_triangles, kai_release_triangles
);
if (result != 0) {
kai_release_mesh(*dest);
return -1;
}
return 0;
}
void kai_release_mesh(struct ka_mesh_t m)
{
int i;
for (i=0; i<m.source_count; i++) {
kai_release_source(m.source[i]);
}
free(m.source);
kai_release_vertices(m.vertices);
for (i=0; i<m.triangles_count; i++) {
kai_release_triangles(m.triangles[i]);
}
free(m.triangles);
}
int kai_read_geometry(struct ka_geometry_t *dest, struct kai_tag_t *src)
{
CHECK_TAG_TYPE("geometry");
dest->id = kai_tag_attr_to_dup(src, "id");
dest->name = kai_tag_attr_to_dup(src, "name");
struct kai_tag_t *mesh = kai_tag_get_first_child_with_type(src, "mesh");
if (mesh == NULL) {
free(dest->id);
free(dest->name);
return -1;
}
int result = kai_read_mesh(&(dest->mesh), mesh);
if (result != 0) {
free(dest->id);
free(dest->name);
return -1;
}
return 0;
}
void kai_release_geometry(struct ka_geometry_t g)
{
free(g.id);
free(g.name);
kai_release_mesh(g.mesh);
}
int kai_read_library_geometries(struct ka_library_geometries_t *dest, struct kai_tag_t *src)
{
CHECK_TAG_TYPE("library_geometries");
dest->id = kai_tag_attr_to_dup(src, "id");
dest->name = kai_tag_attr_to_dup(src, "name");
int result;
KAI_FILL_ARRAY_FROM_TAGS(
result,
dest->geometry, dest->geometry_count, struct ka_geometry_t,
src, "geometry",
kai_read_geometry, kai_release_geometry
);
if (result != 0) {
kai_release_library_geometries(*dest);
return -1;
}
return 0;
}
void kai_release_library_geometries(struct ka_library_geometries_t l)
{
free(l.id);
free(l.name);
int i;
for (i=0; i<l.geometry_count; i++) {
kai_release_geometry(l.geometry[i]);
}
free(l.geometry);
}
|