Alex Sakhartchouk | 17bd28b | 2011-02-11 17:51:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Sakhartchouk | 17bd28b | 2011-02-11 17:51:44 -0800 | [diff] [blame] | 20 | |
| 21 | #include <dae.h> |
| 22 | #include <dom/domCOLLADA.h> |
| 23 | |
| 24 | ColladaLoader::ColladaLoader() { |
| 25 | |
| 26 | } |
| 27 | |
| 28 | ColladaLoader::~ColladaLoader() { |
| 29 | clearGeometry(); |
| 30 | } |
| 31 | |
| 32 | void ColladaLoader::clearGeometry() { |
| 33 | for (uint32_t i = 0; i < mGeometries.size(); i++) { |
| 34 | delete mGeometries[i]; |
| 35 | } |
| 36 | mGeometries.clear(); |
| 37 | } |
| 38 | |
| 39 | bool 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 Sakhartchouk | 13ec73c | 2011-05-04 18:40:09 -0700 | [diff] [blame] | 65 | SimpleMesh *ColladaLoader::getMesh(uint32_t meshIndex) { |
| 66 | return mGeometries[meshIndex]->getMesh(); |
Alex Sakhartchouk | 17bd28b | 2011-02-11 17:51:44 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | bool 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 | |
| 93 | bool 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 | } |