00001 // Filename: load_egg_file.cxx 00002 // Created by: drose (26Feb02) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #include "load_egg_file.h" 00020 #include "eggLoader.h" 00021 #include "config_egg2pg.h" 00022 #include "sceneGraphReducer.h" 00023 #include "virtualFileSystem.h" 00024 #include "config_util.h" 00025 #include "config_express.h" 00026 00027 static PT(PandaNode) 00028 load_from_loader(EggLoader &loader) { 00029 loader._data.load_externals(); 00030 00031 loader.build_graph(); 00032 00033 if (loader._error && !egg_accept_errors) { 00034 egg2pg_cat.error() 00035 << "Errors in egg file.\n"; 00036 return NULL; 00037 } 00038 00039 if (loader._root != (PandaNode *)NULL && egg_flatten) { 00040 SceneGraphReducer gr; 00041 int num_reduced = gr.flatten(loader._root, egg_flatten_siblings); 00042 egg2pg_cat.info() << "Flattened " << num_reduced << " nodes.\n"; 00043 } 00044 00045 return loader._root; 00046 } 00047 00048 //////////////////////////////////////////////////////////////////// 00049 // Function: load_egg_file 00050 // Description: A convenience function. Loads up the indicated egg 00051 // file, and returns the root of a scene graph. Returns 00052 // NULL if the file cannot be read for some reason. 00053 // Does not search along the egg path for the filename 00054 // first; use EggData::resolve_egg_filename() if this is 00055 // required. 00056 //////////////////////////////////////////////////////////////////// 00057 PT(PandaNode) 00058 load_egg_file(const string &filename, CoordinateSystem cs) { 00059 Filename egg_filename = Filename::text_filename(filename); 00060 if (use_vfs) { 00061 VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); 00062 if (!vfs->exists(egg_filename)) { 00063 egg2pg_cat.error() 00064 << "Could not find " << egg_filename << "\n"; 00065 return NULL; 00066 } 00067 00068 } else { 00069 if (!egg_filename.exists()) { 00070 egg2pg_cat.error() 00071 << "Could not find " << egg_filename << "\n"; 00072 return NULL; 00073 } 00074 } 00075 00076 egg2pg_cat.info() 00077 << "Reading " << egg_filename << "\n"; 00078 00079 00080 EggLoader loader; 00081 loader._data.set_egg_filename(egg_filename); 00082 loader._data.set_auto_resolve_externals(true); 00083 if (cs != CS_default) { 00084 loader._data.set_coordinate_system(cs); 00085 } 00086 bool okflag; 00087 00088 if (use_vfs) { 00089 VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr(); 00090 istream *istr = vfs->open_read_file(egg_filename); 00091 if (istr == (istream *)NULL) { 00092 egg2pg_cat.error() 00093 << "Could not open " << egg_filename << " for reading.\n"; 00094 return NULL; 00095 } 00096 okflag = loader._data.read(*istr); 00097 delete istr; 00098 00099 } else { 00100 ifstream file; 00101 00102 if (!egg_filename.open_read(file)) { 00103 egg2pg_cat.error() 00104 << "Could not open " << egg_filename << " for reading.\n"; 00105 return NULL; 00106 } 00107 okflag = loader._data.read(file); 00108 } 00109 00110 if (!okflag) { 00111 egg2pg_cat.error() 00112 << "Error reading " << egg_filename << "\n"; 00113 return NULL; 00114 } 00115 00116 return load_from_loader(loader); 00117 } 00118 00119 //////////////////////////////////////////////////////////////////// 00120 // Function: load_egg_data 00121 // Description: Another convenience function; works like 00122 // load_egg_file() but starts from an already-filled 00123 // EggData structure. The structure is destroyed in the 00124 // loading. 00125 //////////////////////////////////////////////////////////////////// 00126 PT(PandaNode) 00127 load_egg_data(EggData &data) { 00128 // We temporarily shuttle the children to a holding node so we can 00129 // copy them into the EggLoader's structure without it complaining. 00130 EggGroupNode children_holder; 00131 children_holder.steal_children(data); 00132 00133 EggLoader loader(data); 00134 loader._data.steal_children(children_holder); 00135 00136 return load_from_loader(loader); 00137 }