blob: 32d6250510f51e66520d40e4f8167494a78308b1 [file] [log] [blame]
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ColladaLoader.h"
18#include "ColladaConditioner.h"
19#include "ColladaGeometry.h"
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080020
21#include <dae.h>
22#include <dom/domCOLLADA.h>
23
24ColladaLoader::ColladaLoader() {
25
26}
27
28ColladaLoader::~ColladaLoader() {
29 clearGeometry();
30}
31
32void ColladaLoader::clearGeometry() {
33 for (uint32_t i = 0; i < mGeometries.size(); i++) {
34 delete mGeometries[i];
35 }
36 mGeometries.clear();
37}
38
39bool ColladaLoader::init(const char *colladaFile) {
40 DAE dae;
41
42 clearGeometry();
43
44 bool convertSuceeded = true;
45
46 domCOLLADA* root = dae.open(colladaFile);
47 if (!root) {
48 fprintf(stderr, "Failed to read file %s.\n", colladaFile);
49 return false;
50 }
51
52 // We only want to deal with triangulated meshes since rendering complex polygons is not feasible
53 ColladaConditioner conditioner;
54 conditioner.triangulate(&dae);
55
56 domLibrary_geometries *allGeometry = daeSafeCast<domLibrary_geometries>(root->getDescendant("library_geometries"));
57
58 if (allGeometry) {
59 convertSuceeded = convertAllGeometry(allGeometry) && convertSuceeded;
60 }
61
62 return convertSuceeded;
63}
64
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -070065SimpleMesh *ColladaLoader::getMesh(uint32_t meshIndex) {
66 return mGeometries[meshIndex]->getMesh();
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080067}
68
69bool ColladaLoader::convertAllGeometry(domLibrary_geometries *allGeometry) {
70
71 bool convertSuceeded = true;
72 domGeometry_Array &geo_array = allGeometry->getGeometry_array();
73 for (size_t i = 0; i < geo_array.getCount(); i++) {
74 domGeometry *geometry = geo_array[i];
75 const char *geometryName = geometry->getName();
76 if (geometryName == NULL) {
77 geometryName = geometry->getId();
78 }
79
80 domMeshRef mesh = geometry->getMesh();
81 if (mesh != NULL) {
82 printf("Converting geometry: %s\n", geometryName);
83 convertSuceeded = convertGeometry(geometry) && convertSuceeded;
84 } else {
85 printf("Skipping geometry: %s, unsupported type\n", geometryName);
86 }
87
88 }
89
90 return convertSuceeded;
91}
92
93bool ColladaLoader::convertGeometry(domGeometry *geometry) {
94 bool convertSuceeded = true;
95
96 domMeshRef mesh = geometry->getMesh();
97
98 ColladaGeometry *convertedGeo = new ColladaGeometry();
99 convertedGeo->init(geometry);
100
101 mGeometries.push_back(convertedGeo);
102
103 return convertSuceeded;
104}