-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColladaLoader.cpp
593 lines (372 loc) · 18.6 KB
/
ColladaLoader.cpp
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include "ColladaLoader.h"
//#include "./pugixml/src/pugixml.hpp"
#include <iostream>
#include <corecrt_math_defines.h>
ColladaLoader::ColladaLoader()
{
}
bool ColladaLoader::load(const char* filename) {
//filename = "data/collada/tree.xml";
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(filename);
//std::cout << "Load result: " << result.description() << ", mesh name: " << doc.child("mesh").attribute("name").value() << std::endl;
std::cout << "Load result: " << result.description() << ", collada: " << doc.child("COLLADA").attribute("xmlns").value() << std::endl; // GOOD !!
//std::cout << "effect_id = " << doc.child("COLLADA").child("library_effects").child("effect").attribute("id").value() << std::endl; // GOOD !!
pugi::xml_node node_COLLADA = doc.child("COLLADA");
pugi::xml_node node_library_effects = node_COLLADA.child("library_effects");
//std::cout << "effect_id = " << library_effects.first_child().attribute("id").value() << std::endl;
//std::cout << "effect_id = " << library_effects.child("effect").attribute("id").value() << std::endl;
/*
pugi::xml_node effect = library_effects.child("effect");
std::cout << "effect_id = " << effect.attribute("id").value() << std::endl;
effect = effect.next_sibling("effect");
std::cout << "effect_id = " << effect.attribute("id").value() << std::endl;
effect = effect.next_sibling("effect");
std::cout << "effect_id3 = " << effect.attribute("id").value() << std::endl;
*/
//pugi::xml_node effect = library_effects_node.child("effect");
load_effects(node_library_effects);
//std::cout << "geometry_id = " << node_COLLADA.child("library_geometries").child("geometry").attribute("id").as_string() << std::endl;
//std::cout << "polygons_material = " << node_COLLADA.child("library_geometries").child("geometry").child("mesh").child("polygons").attribute("material").as_string() << std::endl;
//std::cout << "node_polygons = " << std::endl;
//Geometry geometry;
//load_polygons (node_COLLADA.child("library_geometries").child("geometry").child("mesh").child("polygons"), geometry );
load_geometries( node_COLLADA );
load_visual_scenes(node_COLLADA);
compute_geometry_to_scene_index(); //
// getchar();
/*
FILE * file = fopen(filename, "r");
if (!file) {
printf("Impossible to open the file %s !\n", filename );
return false;
}
while (true) {
char line_header[256];
if (EOF == fscanf(file, "%s", line_header))
break;
if (0 == strcmp(line_header, "<library_effects>")) {
while (true) {
char line_header[256];
if (EOF == fscanf(file, "%s", line_header))
break;
if (0 == strcmp(line_header, "</library_effects>")) break;
else if (0 == strcmp(line_header, "<effect")) {
}
}
}
}
fclose( file );
*/
return true;
};
ColladaLoader::~ColladaLoader()
{
}
//bool ColladaLoader::load_material( FILE * file ) {
//}
bool ColladaLoader::load_effect(pugi::xml_node node_effect, int count) {
library_effect_name_to_index[node_effect.attribute("name").value()] = count;
pugi::xml_node node_technique = node_effect.child("profile_COMMON").child("technique");
//std::cout << "technique_sid = " << node_technique.attribute("sid").as_string() << std::endl;
//getchar();
Effect effect;
pugi::xml_node node_technique_current;
if (node_technique_current = node_technique.child("cook-torrance")) {
effect.technique = Effect::COOK_TORRANCE;
}
else if (node_technique_current = node_technique.child("phong")) {
effect.technique = Effect::PHONG;
}
else {
std::cout << "effect_technique_not_found = " << node_effect.attribute("name").value() << std::endl;
return false;
}
//std::cout << "effect_technique = " << node_technique_current.name() << std::endl;
//std::cout << "effect_technique = " << effect.technique << std::endl;
//pugi::xml_text txt = node_technique_current.child("emission").child("color").text();
//std::cout << "emission_color = " << txt.as_string() << std::endl;
//getchar();
float4 colors[ATTRS_NAMES_LENGTH];
//for (int i = 0; i < 1; ++i) {
for (int i = 0; i < ATTRS_NAMES_LENGTH; ++i) {
//string txt_floats = "0.1 0.2 0.3 1.0";
string txt_floats = node_technique_current.child( ATTRIBUTES_NAMES[i].c_str() ).child( ATTRIBUTES_SUB_NAMES[i].c_str() ).text().as_string();
// std::cout << ATTRIBUTES_NAMES[i] << "_" << ATTRIBUTES_SUB_NAMES[i] << " = " << txt_floats << std::endl;
stof_array(txt_floats, ATTRIBUTES_NUM_FLOATS[i], &colors[i].m[0]);
//printf_floats(4, &colors[i].m[0]);
//getchar();
}
effect.set( effect.technique, colors[0], colors[1], colors[2], colors[3], colors[4].x, colors[5], colors[6].x, colors[7], colors[8].x, colors[9].x);
//effect.emission = float4(0.1f, 0.2f, 0.3f, 1.0f);
//float4 test = float4(0.1f, 0.2f, 0.3f, 1.0f);
//effect.set(effect.technique, test, test, test, test, test, test, test, test, test);
// effect.printf();
// getchar();
library_effects.push_back( effect );
return true;
}
bool ColladaLoader::load_effects(pugi::xml_node node_library_effects) {
if (!node_library_effects) return false;
int count = 0;
for (pugi::xml_node node_effect = node_library_effects.child("effect"); node_effect; node_effect = node_effect.next_sibling("effect"), ++count) {
//std::cout << "effect_id" << count << " = " << effect.attribute("id").value() << std::endl;
load_effect(node_effect, count);
}
//for (int i = 0; i < count; ++i) {
//std::cout << "effect_name #" << count << " = " << effect.attribute("id").value() << std::endl;
//}
/*
for (Mymap::const_iterator it = library_effect_name_to_index.begin(); it != library_effect_name_to_index.end(); ++it) {
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
}
*/
return true;
}
bool ColladaLoader::load_geometries(pugi::xml_node node_collada) {
if (!node_collada) return false;
pugi::xml_node node_library_geometries = node_collada.child("library_geometries");
if (!node_library_geometries) return false;
int count = 0;
for (pugi::xml_node node_geometry = node_library_geometries.child("geometry"); node_geometry; node_geometry = node_geometry.next_sibling("geometry"), ++count) {
//std::cout << "effect_id" << count << " = " << effect.attribute("id").value() << std::endl;
load_geometry(node_geometry, count);
}
return true;
}
bool ColladaLoader::load_geometry(pugi::xml_node node_geometry, int count) {
pugi::xml_node node_polygons = node_geometry.child("mesh").child("polygons");
library_geometry_name_to_index[node_geometry.attribute("id").as_string()] = count;
Geometry geometry;
load_polygons(node_polygons, geometry);
pugi::xml_node node_mesh = node_polygons.parent();
//cout << "imput_source = " << node_polygons.child("input").attribute("source").as_string() << endl;
string node_name_vertex = get_input_source_attr("VERTEX", node_polygons).substr(1);
string node_name_normal = get_input_source_attr("NORMAL", node_polygons).substr(1);
string node_name_uv0 = get_input_source_attr("TEXCOORD", node_polygons).substr(1);
// cout << "imput_VERTEX_source = " << node_name_vertex << endl;
// cout << "imput_NORMAL_source = " << node_name_normal << endl;
// cout << "imput_TEXCOORD_source = " << node_name_uv0 << endl;
string node_name_vertex_2 = find_mesh_vertices_input_source_str(node_name_vertex, node_mesh);
// cout << "imput_VERTEX_2_source = " << node_name_vertex_2 << endl;
pugi::xml_node node_mesh_float_array_vertex2 = find_mesh_float_array(node_name_vertex_2, node_mesh);
pugi::xml_node node_mesh_float_array_normal = find_mesh_float_array(node_name_normal, node_mesh);
pugi::xml_node node_mesh_float_array_uv0 = find_mesh_float_array(node_name_uv0, node_mesh);
// cout << "node_mesh_float_array_VERTEX_2_source_id= " << node_mesh_float_array_vertex2.attribute("id").as_string() << endl;
// cout << "node_mesh_float_array_NORMAL_source_id= " << node_mesh_float_array_normal.attribute("id").as_string() << endl;
// cout << "node_mesh_float_array_TEXCOORD_source_id= " << node_mesh_float_array_uv0.attribute("id").as_string() << endl;
load_vertices(node_mesh_float_array_vertex2, geometry);
load_normals(node_mesh_float_array_normal, geometry);
load_tex_coords(node_mesh_float_array_uv0, geometry);
//printf_floats(geometry.float_array_uv0.size() * 2, &geometry.float_array_uv0[0].m[0]);
// getchar();
//pugi::xml_node node_uv0 = find_node_array(node_mesh, );
//load_texture_coords();
library_geometries.push_back(geometry);
return true;
}
bool ColladaLoader::load_polygons(pugi::xml_node node_polygons, Geometry &geometry) {
//cout << "node_polygons_p = " << node_polygons.child("p").text().as_string() << std::endl;
int num_polygons;
sscanf(node_polygons.attribute("count").value(), "%d", &num_polygons);
//std::cout << "num_polygons = " << num_polygons << std::endl;
string material = node_polygons.attribute("material").as_string();
// std::cout << "material = " << material << std::endl;
int effect_index = library_effect_name_to_index[ material ];
// std::cout << "effect_index = " << effect_index << std::endl;
// getchar();
geometry.polygons.resize(num_polygons);
pugi::xml_node node_polygons_p = node_polygons.child("p");
for (int i = 0; i < num_polygons; ++i) {
geometry.polygons[i].effect_index = effect_index;
//cout << "node_polygons_p = " << node_polygons_p.text().as_string() << std::endl;
load_polygon_triangle( node_polygons_p.text().as_string(), geometry.polygons[i]);
//geometry.polygons[i].printf();
node_polygons_p = node_polygons_p.next_sibling("p");
//getchar();
}
return true;
}
bool ColladaLoader::load_vertices(pugi::xml_node node_mesh_float_array, Geometry &geometry) {
int num_floats = node_mesh_float_array.attribute("count").as_int();
// cout << "vertices_float_array_count = " << num_floats << endl;
int num_verts = num_floats / 3;
geometry.float_array_positions.resize(num_verts);
load_float_array(node_mesh_float_array, &geometry.float_array_positions[0].m[0], num_floats);
return true;
}
bool ColladaLoader::load_normals(pugi::xml_node node_mesh_float_array, Geometry &geometry) {
int num_floats = node_mesh_float_array.attribute("count").as_int();
// cout << "normals_float_array_count = " << num_floats << endl;
int num_verts = num_floats / 3;
geometry.float_array_normals.resize(num_verts);
load_float_array(node_mesh_float_array, &geometry.float_array_normals[0].m[0], num_floats);
return true;
}
bool ColladaLoader::load_tex_coords(pugi::xml_node node_mesh_float_array, Geometry &geometry) {
int num_floats = node_mesh_float_array.attribute("count").as_int();
// cout << "uv0_float_array_count = " << num_floats << endl;
int num_verts = num_floats / 2;
geometry.float_array_uv0.resize(num_verts);
load_float_array(node_mesh_float_array, &geometry.float_array_uv0[0].m[0], num_floats);
return true;
}
bool ColladaLoader::load_float_array(pugi::xml_node node_mesh_float_array, float * pf, int num_floats) {
stof_array(node_mesh_float_array.text().as_string(), num_floats, pf);
return true;
}
string ColladaLoader::find_mesh_vertices_input_source_str(const string mesh_vertices_id, pugi::xml_node node_mesh) {
if (!node_mesh) return "";
int count = 0;
for (pugi::xml_node node_mesh_vertices = node_mesh.child("vertices"); node_mesh_vertices; node_mesh_vertices = node_mesh_vertices.next_sibling("vertices"), ++count) {
//std::cout << "effect_id" << count << " = " << effect.attribute("id").value() << std::endl;
//cout << "mesh_source_id = " << node_mesh_source.attribute("id").as_string() << endl;
//getchar();
if (mesh_vertices_id == node_mesh_vertices.attribute("id").as_string()) {
//cout << "found, mesh_source_id = " << node_mesh_source.attribute("id").as_string() << endl;
//getchar();
return string( node_mesh_vertices.child("input").attribute("source").as_string() ).substr(1);
}
}
return "";
}
pugi::xml_node ColladaLoader::find_mesh_float_array(const string mesh_source_id, pugi::xml_node node_mesh ) {
if (!node_mesh) return pugi::xml_node();
int count = 0;
for (pugi::xml_node node_mesh_source = node_mesh.child("source"); node_mesh_source; node_mesh_source = node_mesh_source.next_sibling("source"), ++count) {
//std::cout << "effect_id" << count << " = " << effect.attribute("id").value() << std::endl;
//cout << "mesh_source_id = " << node_mesh_source.attribute("id").as_string() << endl;
//getchar();
if ( mesh_source_id == node_mesh_source.attribute( "id" ).as_string() ) {
//cout << "found, mesh_source_id = " << node_mesh_source.attribute("id").as_string() << endl;
//getchar();
return node_mesh_source.child("float_array");
}
}
return pugi::xml_node();
}
string ColladaLoader::get_input_source_attr( const string semantic, pugi::xml_node node_polygons ) {
//const static string
pugi::xml_node node_polygons_input = node_polygons.child("input");
for(int i = 0; i < 3; ++i) {
if (semantic == node_polygons_input.attribute("semantic").as_string()) {
return node_polygons_input.attribute("source").as_string();
}
node_polygons_input = node_polygons_input.next_sibling("input");
}
return "";
}
bool ColladaLoader::load_polygon_triangle( string s, PolygonTriangle & poly_tri) {
sscanf( s.c_str(), "%d %d %d %d %d %d %d %d %d",
&poly_tri.vertex_indices.x, &poly_tri.normal_indices.x, &poly_tri.uv0_indices.x,
&poly_tri.vertex_indices.y, &poly_tri.normal_indices.y, &poly_tri.uv0_indices.y,
&poly_tri.vertex_indices.z, &poly_tri.normal_indices.z, &poly_tri.uv0_indices.z );
return true;
}
bool ColladaLoader::load_visual_scenes(pugi::xml_node node_collada) {
if (!node_collada) return false;
pugi::xml_node scene_nodes = node_collada.child("library_visual_scenes").child("visual_scene");
if (!scene_nodes) return false;
int count = 0;
for (pugi::xml_node scene_node = scene_nodes.child("node"); scene_node; scene_node = scene_node.next_sibling("node"), ++count) {
// std::cout << "count = " << count << ", scene_node _id = " << scene_node.attribute("id").as_string() << std::endl;
load_scene_node(scene_node, count);
}
return true;
}
bool ColladaLoader::load_scene_node(pugi::xml_node scene_node, int count) {
if (!scene_node) return false;
string geometry_id = string( scene_node.child("instance_geometry").attribute("url").as_string() ).substr(1);
int geometry_id_index = library_geometry_name_to_index[geometry_id];
// std::cout << "geometry_id" << geometry_id << std::endl;
// std::cout << "geometry_id_index = " << geometry_id_index << std::endl;
SceneNode node;
node.geometry_index = geometry_id_index;
node.matrix = load_scene_node_matrix(scene_node);
library_visual_scenes.push_back(node);
// getchar();
return true;
}
Matrix4x4 ColladaLoader::load_scene_node_matrix(pugi::xml_node scene_node) {
Matrix4x4 matrix;
if (!scene_node) return matrix;
pugi::xml_node node_matrix = scene_node.child("matrix");
if (node_matrix) {
string node_matrix_str = scene_node.child("matrix").text().as_string();
stof_array(node_matrix_str, 4*4, &matrix.m[0] );
//std::cout << "node_matrix = ";
//printf_floats( 4*4, &matrix.m[0] );
//getchar();
matrix.transponse();
return matrix;
}
unordered_map<string, pugi::xml_node> sid_to_node_rotate_map = load_sid_to_rotate_map (scene_node);
const static int NUM_ROTATE_SIDS = 6;
//const static string ROTATE_SID_ARRAY[NUM_ROTATE_SIDS] = { "jointOrientX", "jointOrientY", "jointOrientZ", "rotateX", "rotateY", "rotateZ" };
//const static string ROTATE_SID_ARRAY[NUM_ROTATE_SIDS] = { "jointOrientZ", "jointOrientY", "jointOrientX", "rotateZ", "rotateY", "rotateX" };
//const static string ROTATE_SID_ARRAY[NUM_ROTATE_SIDS] = { "jointOrientX", "jointOrientY", "jointOrientZ", "rotateZ", "rotateY", "rotateX" };
//const static string ROTATE_SID_ARRAY[NUM_ROTATE_SIDS] = { "jointOrientX", "jointOrientY", "jointOrientZ", "rotateY", "rotateZ", "rotateX" }; //
//const static string ROTATE_SID_ARRAY[NUM_ROTATE_SIDS] = { "jointOrientX", "jointOrientY", "jointOrientZ", "rotateY", "rotateX", "rotateZ" };
const static string ROTATE_SID_ARRAY[NUM_ROTATE_SIDS] = { "jointOrientX", "jointOrientY", "jointOrientZ", "rotateX", "rotateZ", "rotateY" };
for (int i = 0; i < NUM_ROTATE_SIDS; i++) {
pugi::xml_node rotate_node = sid_to_node_rotate_map[ ROTATE_SID_ARRAY[i] ];
//pugi::xml_node rotate_node = sid_to_node_rotate_map["jointOrientX"];
if (!rotate_node) {
//std::cout << "not_found : ROTATE_SID_ARRAY[" << i << "] = " << ROTATE_SID_ARRAY[i] << std::endl;
//getchar();
continue;
}
// std::cout << "ROTATE_SID_ARRAY[" << i << "] = " << ROTATE_SID_ARRAY[i] << std::endl;
// std::cout << "node_rotate_sid = " << rotate_node.attribute("sid").as_string() << std::endl;
string angle_str = string(rotate_node.text().as_string()).substr(6);
float angle;
stof_array(angle_str, 1, &angle);
// std::cout << "angle" << angle << std::endl;
float rad_angle = angle * M_PI / 180.0f;
switch ( i%3 ) {
case 0: matrix.rotateX(rad_angle);
break;
case 1: matrix.rotateY(rad_angle);
break;
case 2: matrix.rotateZ(rad_angle);
//matrix.rotateZ(45 * M_PI / 180);
break;
default:
break;
}
}
string translate_str = string(scene_node.child("translate").text().as_string());
float translate[3];
stof_array(translate_str, 3, &translate[0]);
// std::cout << "translate = ";
// printf_floats( 3, &translate[0]);
matrix.translate(translate[0], translate[1], translate[2]);
return matrix;
}
unordered_map<string, pugi::xml_node> ColladaLoader::load_sid_to_rotate_map(pugi::xml_node scene_node) {
unordered_map<string, pugi::xml_node> sid_to_node_rotate_map;
if (!scene_node) return sid_to_node_rotate_map;
int count = 0;
for (pugi::xml_node rotate_node = scene_node.child("rotate"); rotate_node; rotate_node = rotate_node.next_sibling("rotate"), ++count) {
//std::cout << "count = " << count << ", scene_node _id = " << scene_node.attribute("id").as_string() << std::endl;
sid_to_node_rotate_map[rotate_node.attribute("sid").as_string()] = rotate_node;
}
return sid_to_node_rotate_map;
}
float3 ColladaLoader::get_vertex(float3 &v, int geometry_index) {
float4 result = multiply(float4(v.x, v.y, v.z, 1.0f), library_visual_scenes[geometry_to_scene_index[geometry_index]].matrix);
return float3(result.x, result.y, result.z);
}
float3 ColladaLoader::get_normal(float3 &n, int geometry_index) {
float4 result = multiply(float4(n.x, n.y, n.z, 0.0f), library_visual_scenes[geometry_to_scene_index[geometry_index]].matrix);
return float3(result.x, result.y, result.z);
}
void ColladaLoader::compute_geometry_to_scene_index() {
for (int i = 0; i < library_geometries.size(); ++i) {
//std::cout << "library_visual_scenes.size() = " << library_visual_scenes.size() << endl;
//std::cout << "geometry_index = " << library_visual_scenes[i].geometry_index << endl;
geometry_to_scene_index[library_visual_scenes[i].geometry_index] = i;
//getchar();
}
}