blob: 3fc954da50c90950d3351b3b6fa5e27949968e23 [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() {
Alex Sakhartchouk8f6a3312011-08-17 16:43:47 -070029 if (mDae) {
30 delete mDae;
31 }
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080032 clearGeometry();
33}
34
35void ColladaLoader::clearGeometry() {
36 for (uint32_t i = 0; i < mGeometries.size(); i++) {
37 delete mGeometries[i];
38 }
39 mGeometries.clear();
40}
41
42bool ColladaLoader::init(const char *colladaFile) {
Alex Sakhartchouk8f6a3312011-08-17 16:43:47 -070043 if (mDae) {
44 delete mDae;
45 }
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080046 clearGeometry();
47
Alex Sakhartchouk8f6a3312011-08-17 16:43:47 -070048 mDae = new DAE();
49
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080050 bool convertSuceeded = true;
51
Alex Sakhartchouk8f6a3312011-08-17 16:43:47 -070052 domCOLLADA* root = mDae->open(colladaFile);
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080053 if (!root) {
54 fprintf(stderr, "Failed to read file %s.\n", colladaFile);
55 return false;
56 }
57
58 // We only want to deal with triangulated meshes since rendering complex polygons is not feasible
59 ColladaConditioner conditioner;
Alex Sakhartchouk8f6a3312011-08-17 16:43:47 -070060 conditioner.triangulate(mDae);
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080061
62 domLibrary_geometries *allGeometry = daeSafeCast<domLibrary_geometries>(root->getDescendant("library_geometries"));
63
64 if (allGeometry) {
65 convertSuceeded = convertAllGeometry(allGeometry) && convertSuceeded;
66 }
67
68 return convertSuceeded;
69}
70
Alex Sakhartchouk13ec73c2011-05-04 18:40:09 -070071SimpleMesh *ColladaLoader::getMesh(uint32_t meshIndex) {
72 return mGeometries[meshIndex]->getMesh();
Alex Sakhartchouk17bd28b2011-02-11 17:51:44 -080073}
74
75bool ColladaLoader::convertAllGeometry(domLibrary_geometries *allGeometry) {
76
77 bool convertSuceeded = true;
78 domGeometry_Array &geo_array = allGeometry->getGeometry_array();
79 for (size_t i = 0; i < geo_array.getCount(); i++) {
80 domGeometry *geometry = geo_array[i];
81 const char *geometryName = geometry->getName();
82 if (geometryName == NULL) {
83 geometryName = geometry->getId();
84 }
85
86 domMeshRef mesh = geometry->getMesh();
87 if (mesh != NULL) {
88 printf("Converting geometry: %s\n", geometryName);
89 convertSuceeded = convertGeometry(geometry) && convertSuceeded;
90 } else {
91 printf("Skipping geometry: %s, unsupported type\n", geometryName);
92 }
93
94 }
95
96 return convertSuceeded;
97}
98
99bool ColladaLoader::convertGeometry(domGeometry *geometry) {
100 bool convertSuceeded = true;
101
102 domMeshRef mesh = geometry->getMesh();
103
104 ColladaGeometry *convertedGeo = new ColladaGeometry();
105 convertedGeo->init(geometry);
106
107 mGeometries.push_back(convertedGeo);
108
109 return convertSuceeded;
110}
Alex Sakhartchouk8f6a3312011-08-17 16:43:47 -0700111
112bool ColladaLoader::stripGeometryAndSave() {
113
114 ColladaConditioner conditioner;
115 bool convertSuceeded = conditioner.stripGeometry(mDae);
116
117 mDae->writeAll();
118 if(!convertSuceeded) {
119 printf("Encountered errors\n");
120 } else {
121 printf("Stripped geometry data from collada file\n");
122 }
123
124 return convertSuceeded;
125}