Panda3D
|
00001 // Filename: load_dae_file.cxx 00002 // Created by: rdb (16Mar11) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "load_collada_file.h" 00016 #include "colladaLoader.h" 00017 #include "config_collada.h" 00018 #include "sceneGraphReducer.h" 00019 #include "virtualFileSystem.h" 00020 #include "config_util.h" 00021 #include "bamCacheRecord.h" 00022 00023 static PT(PandaNode) 00024 load_from_loader(ColladaLoader &loader) { 00025 loader.build_graph(); 00026 00027 if (loader._error && !collada_accept_errors) { 00028 collada_cat.error() 00029 << "Errors in collada file.\n"; 00030 return NULL; 00031 } 00032 00033 if (loader._root != NULL && collada_flatten) { 00034 SceneGraphReducer gr; 00035 00036 int combine_siblings_bits = 0; 00037 if (collada_combine_geoms) { 00038 combine_siblings_bits |= SceneGraphReducer::CS_geom_node; 00039 } 00040 if (collada_flatten_radius > 0.0) { 00041 combine_siblings_bits |= SceneGraphReducer::CS_within_radius; 00042 gr.set_combine_radius(collada_flatten_radius); 00043 } 00044 00045 int num_reduced = gr.flatten(loader._root, combine_siblings_bits); 00046 collada_cat.info() << "Flattened " << num_reduced << " nodes.\n"; 00047 00048 if (collada_unify) { 00049 // We want to premunge before unifying, since otherwise we risk 00050 // needlessly duplicating vertices. 00051 if (premunge_data) { 00052 gr.premunge(loader._root, RenderState::make_empty()); 00053 } 00054 gr.collect_vertex_data(loader._root); 00055 gr.unify(loader._root, true); 00056 if (collada_cat.is_debug()) { 00057 collada_cat.debug() << "Unified.\n"; 00058 } 00059 } 00060 } 00061 00062 return DCAST(ModelRoot, loader._root); 00063 } 00064 00065 //////////////////////////////////////////////////////////////////// 00066 // Function: load_collada_file 00067 // Description: A convenience function. Loads up the indicated 00068 // dae file, and returns the root of a scene graph. 00069 // Returns NULL if the file cannot be read for some 00070 // reason. Does not search along the model path for 00071 // the filename first. 00072 //////////////////////////////////////////////////////////////////// 00073 PT(PandaNode) 00074 load_collada_file(const Filename &filename, CoordinateSystem cs, 00075 BamCacheRecord *record) { 00076 00077 VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); 00078 00079 if (record != (BamCacheRecord *)NULL) { 00080 record->add_dependent_file(filename); 00081 } 00082 00083 ColladaLoader loader; 00084 loader._filename = filename; 00085 loader._cs = cs; 00086 loader._record = record; 00087 00088 collada_cat.info() 00089 << "Reading " << filename << "\n"; 00090 00091 if (!loader.read(filename)) { 00092 return NULL; 00093 } 00094 00095 return load_from_loader(loader); 00096 }