Panda3D
|
00001 // Filename: load_egg_file.cxx 00002 // Created by: drose (26Feb02) 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_egg_file.h" 00016 #include "eggLoader.h" 00017 #include "config_egg2pg.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(EggLoader &loader) { 00025 loader._data->load_externals(DSearchPath(), loader._record); 00026 00027 loader.build_graph(); 00028 00029 if (loader._error && !egg_accept_errors) { 00030 egg2pg_cat.error() 00031 << "Errors in egg file.\n"; 00032 return NULL; 00033 } 00034 00035 if (loader._root != (PandaNode *)NULL && egg_flatten) { 00036 SceneGraphReducer gr; 00037 00038 int combine_siblings_bits = 0; 00039 if (egg_combine_geoms) { 00040 combine_siblings_bits |= SceneGraphReducer::CS_geom_node; 00041 } 00042 if (egg_flatten_radius > 0.0) { 00043 combine_siblings_bits |= SceneGraphReducer::CS_within_radius; 00044 gr.set_combine_radius(egg_flatten_radius); 00045 } 00046 00047 int num_reduced = gr.flatten(loader._root, combine_siblings_bits); 00048 egg2pg_cat.info() << "Flattened " << num_reduced << " nodes.\n"; 00049 00050 if (egg_unify) { 00051 // We want to premunge before unifying, since otherwise we risk 00052 // needlessly duplicating vertices. 00053 if (premunge_data) { 00054 gr.premunge(loader._root, RenderState::make_empty()); 00055 } 00056 gr.collect_vertex_data(loader._root); 00057 gr.unify(loader._root, true); 00058 if (egg2pg_cat.is_debug()) { 00059 egg2pg_cat.debug() << "Unified.\n"; 00060 } 00061 } 00062 } 00063 00064 return loader._root; 00065 } 00066 00067 //////////////////////////////////////////////////////////////////// 00068 // Function: load_egg_file 00069 // Description: A convenience function. Loads up the indicated egg 00070 // file, and returns the root of a scene graph. Returns 00071 // NULL if the file cannot be read for some reason. 00072 // Does not search along the egg path for the filename 00073 // first; use EggData::resolve_egg_filename() if this is 00074 // required. 00075 //////////////////////////////////////////////////////////////////// 00076 PT(PandaNode) 00077 load_egg_file(const Filename &filename, CoordinateSystem cs, 00078 BamCacheRecord *record) { 00079 Filename egg_filename = filename; 00080 egg_filename.set_text(); 00081 VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); 00082 00083 if (record != (BamCacheRecord *)NULL) { 00084 record->add_dependent_file(egg_filename); 00085 } 00086 00087 EggLoader loader; 00088 loader._data->set_egg_filename(egg_filename); 00089 loader._data->set_auto_resolve_externals(true); 00090 loader._data->set_coordinate_system(cs); 00091 loader._record = record; 00092 00093 bool okflag; 00094 istream *istr = vfs->open_read_file(egg_filename, true); 00095 if (istr == (istream *)NULL) { 00096 return NULL; 00097 } 00098 00099 egg2pg_cat.info() 00100 << "Reading " << egg_filename << "\n"; 00101 00102 okflag = loader._data->read(*istr); 00103 vfs->close_read_file(istr); 00104 00105 if (!okflag) { 00106 egg2pg_cat.error() 00107 << "Error reading " << egg_filename << "\n"; 00108 return NULL; 00109 } 00110 00111 return load_from_loader(loader); 00112 } 00113 00114 //////////////////////////////////////////////////////////////////// 00115 // Function: load_egg_data 00116 // Description: Another convenience function; works like 00117 // load_egg_file() but starts from an already-filled 00118 // EggData structure. The structure is destroyed in the 00119 // loading. 00120 //////////////////////////////////////////////////////////////////// 00121 PT(PandaNode) 00122 load_egg_data(EggData *data, CoordinateSystem cs) { 00123 // We temporarily shuttle the children to a holding node so we can 00124 // copy them into the EggLoader's structure without it complaining. 00125 EggGroupNode children_holder; 00126 children_holder.steal_children(*data); 00127 00128 EggLoader loader(data); 00129 loader._data->steal_children(children_holder); 00130 00131 loader._data->set_auto_resolve_externals(true); 00132 loader._data->set_coordinate_system(cs); 00133 00134 return load_from_loader(loader); 00135 }