/
rcbc_xml_minixml_geometries.c
368 lines (305 loc) · 11 KB
/
rcbc_xml_minixml_geometries.c
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
#include <assert.h>
#include "rcbc_xml_minixml_geometries.h"
#include "rcbc_xml_minixml.h"
#include "console.h"
/**
* Reads a float array, such as verticies, normals, text cords, etc...
* @param tempory The ModelTempory struct to hold the array.
* @param mesh The mesh using the floats
* @param xnode The XML node containing the data.
* @return
*/
FloatArray* RCBC_MiniXML_ProcessGeometries_Mesh_FloatArray(ModelTempory *tempory, Mesh *mesh, mxml_node_t *xnode) {
DEBUG_M("Entering function...");
const char* id = mxmlElementGetAttr(xnode, "id");
const char* count_s = mxmlElementGetAttr(xnode, "count");
int count = atoi(count_s);
FloatArray* newarray = NEW(FloatArray, count);
if(!newarray) {
return NULL;
}
GLfloat f = 0.0f;
int i = 0;
char* pch = strtok(xnode->child->value.opaque, " ");
while(pch && i < count) {
sscanf(pch, "%f", &f);
newarray->values[i] = f;
pch = strtok(NULL, " ");
i++;
}
ListAdd(tempory->deleteme, newarray);
DEBUG_M("exiting function");
return newarray;
}
/**
* Reads COLLADA mesh source data.
* @param tempory The ModelTempory struct to hold the data.
* @param mesh The mesh using the data.
* @param xnode The XML node containing the data.
* @return 1 for some reason...
*/
#warning ['TODO']: Check for allocation errors.
int RCBC_MiniXML_ProcessGeometries_Mesh_Source(ModelTempory *tempory, Mesh *mesh, mxml_node_t *xnode) {
DEBUG_M("Entering function...");
const char* id = mxmlElementGetAttr(xnode, "id");
for(xnode = xnode->child; xnode; xnode = xnode->next) {
if(xnode->type == MXML_ELEMENT) {
DumpNodeInfo(xnode);
if(strcasecmp(xnode->value.element.name, "float_array") == 0) {
FloatArray* array = RCBC_MiniXML_ProcessGeometries_Mesh_FloatArray(tempory, mesh, xnode);
Hookup* arrayhookup = NEW(Hookup, (char*)id, (void*)array);
ListAdd(tempory->sources, arrayhookup);
}
}
}
return 1;
}
/**
* Reads COLLADA mesh vertex data.
* @param tempory The ModelTempory struct to hold the vertices.
* @param mesh The mesh using the vertices
* @param xnode The XML node containing the data.
* @return 0 or crash.
*/
#warning ['TODO']: Check for allocation errors. (remember why this returns a 1).
int RCBC_MiniXML_ProcessGeometries_Mesh_Verticies(ModelTempory *tempory, Mesh *mesh, mxml_node_t *xnode) {
DEBUG_M("Entering function...");
assert(mesh);
assert(xnode);
mxml_node_t *node;
char input_id[512];
const char *source;
const char *semantic;
const char *id = mxmlElementGetAttr(xnode, "id");
for(node = xnode->child; node != NULL; node = node->next) {
DumpNodeInfo(node);
if(node->type == MXML_ELEMENT) {
if(strcasecmp(node->value.element.name, "input") == 0) {
semantic = mxmlElementGetAttr(node, "semantic");
source = mxmlElementGetAttr(node, "source");
DEHASH(source);
int input_id_len = strlen(id)+strlen(semantic)+2;
char* input_id = malloc(input_id_len);
snprintf(input_id, input_id_len, "%s_%s", id, semantic);
ListAdd(tempory->freeme, input_id); /* free after hookups */
Hookup* idhookup = NEW(Hookup, (char*)input_id, NULL);
Hookup* sourcehookup = NEW(Hookup, (char*)source, (void*)&idhookup->ptr);
ListAdd(tempory->sources, idhookup);
ListAdd(tempory->sinks, sourcehookup);
}
}
}
return 0;
}
/**
* Reads COLLADA mesh polygon data.
* @param tempory The ModelTempory struct to hold the polygons.
* @param mesh The mesh using the polygons
* @param xnode The XML node containing the data.
* @return NULL because its not implemented yet.
*/
UnsortedPolygons* RCBC_MiniXML_ProcessGeometries_Mesh_Polygons(ModelTempory *tempory, Mesh *mesh, mxml_node_t *xnode) {
DEBUG_M("Entering function...");
assert(tempory);
assert(mesh);
assert(xnode);
//mxml_node_t *node;
#warning ['TODO']: Handle polygon models... or not...
WARNING("Model contains polygon data! Export as triangles. %s", SYMBOL_WARNING);
return NULL;
}
/**
* Reads COLLADA mesh triangle data.
* @param tempory The ModelTempory struct to hold the array.
* @param mesh The mesh using the triangles
* @param xnode The XML node containing the data.
* @return The collada triangles in unsorted form.
*/
UnsortedTriangles* RCBC_MiniXML_ProcessGeometries_Mesh_Triangles(ModelTempory *tempory, Mesh *mesh, mxml_node_t *xnode) {
DEBUG_M("Entering function...");
assert(tempory);
assert(mesh);
assert(xnode);
mxml_node_t *node;
const char* count_s = mxmlElementGetAttr(xnode, "count");
const char* material = mxmlElementGetAttr(xnode, "material");
char* source_id;
int source_id_len;
int count = atoi(count_s);
int inputs = 0;
UnsortedTriangles* triangles = NEW(UnsortedTriangles, count);
for(node = xnode->child; node != NULL; node = node->next) {
DumpNodeInfo(node);
if(node->type == MXML_ELEMENT) {
if(strcasecmp(node->value.element.name, "input") == 0) {
const char *semantic = mxmlElementGetAttr(node, "semantic");
const char *source = mxmlElementGetAttr(node, "source");
const char *offset_s = mxmlElementGetAttr(node, "offset");
int offset = atoi(offset_s);
DEHASH(source);
void* ptr = NULL;
inputs++;
if(strcasecmp(semantic, "VERTEX") == 0) {
ptr = &(triangles->vertices);
triangles->vertices_offset = offset;
triangles->normals_offset = offset;
#warning ['TODO']: Handle Mayas weirdness
/* Maya exports the file in a slightly different format
* where the vertices structure has both position and
* normals rather than them being in trinagles. */
source_id_len = strlen(source)+strlen("NORMAL")+2;
source_id = malloc(source_id_len);
snprintf(source_id, source_id_len, "%s_NORMAL", source);
/* Add the normals hookup now, the position will be done below */
Hookup* hookup_normals = NEW(Hookup, (char*)source_id, &triangles->normals);
ListAdd(tempory->sinks, hookup_normals);
/* We keep track of strings to free later */
ListAdd(tempory->freeme, source_id);
source_id_len = strlen(source)+strlen("POSITION")+2;
source_id = malloc(source_id_len);
snprintf(source_id, source_id_len, "%s_POSITION", source);
ListAdd(tempory->freeme, source_id);
} else if(strcasecmp(semantic, "NORMAL") == 0) {
ptr = &(triangles->normals);
triangles->normals_offset = offset;
source_id = (char*)source;
} else if(strcasecmp(semantic, "TEXCOORD") == 0) {
ptr = &(triangles->texcoords);
triangles->texcoords_offset = offset;
source_id = (char*)source;
}
Hookup* hookup = NEW(Hookup, (char*)source_id, ptr);
ListAdd(tempory->sinks, hookup);
} else if(strcasecmp(node->value.element.name, "p") == 0) {
triangles->inputs = inputs;
UnsortedTrianglesAllocateIndices(triangles);
int value = -1;
int i = 0;
char* pch = strtok(node->child->value.opaque, " ");
while(pch && i < count * inputs * 3) {
sscanf(pch, "%d", &value);
triangles->indices[i] = value;
pch = strtok(NULL, " ");
i++;
}
}
}
}
triangles->inputs = inputs;
triangles->ptr = (void**)&mesh->triangles;
ListAdd(tempory->unsorted, triangles);
if(material != NULL && material[0] != '\0') {
Hookup* material_hookup = NEW(Hookup, (char*)material, (void*)&triangles->image);
ListAdd(tempory->sinks, material_hookup);
}
return triangles;
}
/**
* Reads COLLADA mesh child data.
* @param tempory The ModelTempory struct to hold the data.
* @param mesh The mesh using the data.
* @param xnode The XML node containing the data.
* @return The collada triangles in unsorted form.
*/
int RCBC_MiniXML_ProcessGeometries_Mesh_Children(ModelTempory *tempory, Mesh *mesh, mxml_node_t *xnode) {
DEBUG_M("Entering function...");
if(!mesh) {
ERROR("XML Passed NULL RCBC mesh... %s", SYMBOL_WARNING);
return 1;
}
if(!xnode) {
ERROR("XML Passed NULL XML node... %s", SYMBOL_WARNING);
return 1;
}
DumpNodeInfo(xnode);
if(strcasecmp(xnode->value.element.name, "source") == 0) {
return RCBC_MiniXML_ProcessGeometries_Mesh_Source(tempory, mesh, xnode);
} else if(strcasecmp(xnode->value.element.name, "vertices") == 0) {
return RCBC_MiniXML_ProcessGeometries_Mesh_Verticies(tempory, mesh, xnode);
} else if(strcasecmp(xnode->value.element.name, "triangles") == 0) {
if(!RCBC_MiniXML_ProcessGeometries_Mesh_Triangles(tempory, mesh, xnode)) {
return 0;
} else {
return 1;
}
} else if(strcasecmp(xnode->value.element.name, "polygons") == 0) {
#warning ['TODO']: Convert polygons to triangle strips
//return RCBC_MiniXML_ProcessGeometries_Mesh_Polygons(tempory, mesh, xnode);
ERROR("Model contains polygon data, convert to triangles.");
return 1;
}
}
/**
* Reads a COLLADA mesg
* @param tempory The ModelTempory struct to hold the mesh data.
* @param xnode The XML node containing the mesh.
* @return The Mesh or crash.
*/
Mesh* RCBC_MiniXML_ProcessGeometries_Mesh(ModelTempory *tempory, mxml_node_t *xnode) {
DEBUG_M("Entering function...");
Mesh* last;
assert(tempory);
assert(xnode);
Mesh* mesh = NEW(Mesh);
ListAdd(tempory->model->geometries, mesh);
mxml_node_t *child;
const char *id = mxmlElementGetAttr(xnode, "id");
for(child = xnode->child; child != NULL; child = child->next) {
if(child->type == MXML_ELEMENT) {
RCBC_MiniXML_ProcessGeometries_Mesh_Children(tempory, mesh, child);
}
}
return mesh;
}
/**
* Grabs the mesh from the geometry (COLLADA specs say there can be only one
* although a few nonmesh temporys are supported, they are usless to us and ignored)
* @param tempory The ModelTempory struct to hold throwaway data.
* @param node The XML node containing the mesh.
* @return The mesh or NULL on error.
*/
Mesh* RCBC_MiniXML_ProcessGeometries_Geometry(ModelTempory *tempory, mxml_node_t *node) {
DEBUG(DEBUG_MEDIUM, "%sRCBC_MiniXML_ProcessGeometries_Geometry", COLOUR_LIGHT_BLUE);
assert(tempory);
assert(node);
const char *id = mxmlElementGetAttr(node, "id");
mxml_node_t* mesh_node = mxmlFindElement(node, node, "mesh", NULL, NULL, MXML_DESCEND);
if(!mesh_node) {
ERROR("No mesh node in geometry!");
return NULL;
}
Mesh* mesh = RCBC_MiniXML_ProcessGeometries_Mesh(tempory, mesh_node);
Hookup* hookup = NEW(Hookup, (char*)id, (void*)mesh);
ListAdd(tempory->sources, hookup);
return mesh;
}
/**
* Reads COLLADA geometry information.
* @param tempory The ModelTempory struct to hold throwaway data.
* @param node The XML node containing the geometry infomration.
* @return 1 on error (or success?)
*/
int RCBC_MiniXML_ProcessGeometries(ModelTempory *tempory, mxml_node_t *node) {
const char* id;
DEBUG(DEBUG_MEDIUM, "RCBC_MiniXML_ProcessGeometries");
assert(tempory);
if(!node) {
return 1;
}
#warning ['TODO']: Free the mesh if it already exists for safety... probably not needed...
if(tempory->model->geometries) {
//DEBUG(DEBUG_HIGH, "Freeing empty...");
//RCBC_MeshFree(&(tempory->geometries));
}
for(node = node->child; node != NULL; node = node->next) {
if(node->type == MXML_ELEMENT) {
DumpNodeInfo(node);
if(strcasecmp(node->value.element.name, "geometry") == 0) {
if(!RCBC_MiniXML_ProcessGeometries_Geometry(tempory, node)) {
return 1;
}
}
}
}
return 1;
}