Home | History | Annotate | Download | only in a3dconvert
      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"
     20 
     21 #include <dae.h>
     22 #include <dom/domCOLLADA.h>
     23 
     24 ColladaLoader::ColladaLoader() {
     25 
     26 }
     27 
     28 ColladaLoader::~ColladaLoader() {
     29     if (mDae) {
     30         delete mDae;
     31     }
     32     clearGeometry();
     33 }
     34 
     35 void ColladaLoader::clearGeometry() {
     36     for (uint32_t i = 0; i < mGeometries.size(); i++) {
     37         delete mGeometries[i];
     38     }
     39     mGeometries.clear();
     40 }
     41 
     42 bool ColladaLoader::init(const char *colladaFile) {
     43     if (mDae) {
     44         delete mDae;
     45     }
     46     clearGeometry();
     47 
     48     mDae = new DAE();
     49 
     50     bool convertSuceeded = true;
     51 
     52     domCOLLADA* root = mDae->open(colladaFile);
     53     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;
     60     conditioner.triangulate(mDae);
     61 
     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 
     71 SimpleMesh *ColladaLoader::getMesh(uint32_t meshIndex) {
     72     return mGeometries[meshIndex]->getMesh();
     73 }
     74 
     75 bool 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 
     99 bool 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 }
    111 
    112 bool 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 }
    126