Panda3D
load_egg_file.cxx
1 // Filename: load_egg_file.cxx
2 // Created by: drose (26Feb02)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "load_egg_file.h"
16 #include "eggLoader.h"
17 #include "config_egg2pg.h"
18 #include "sceneGraphReducer.h"
19 #include "virtualFileSystem.h"
20 #include "config_util.h"
21 #include "bamCacheRecord.h"
22 
23 static PT(PandaNode)
24 load_from_loader(EggLoader &loader) {
25  loader._data->load_externals(DSearchPath(), loader._record);
26 
27  loader.build_graph();
28 
29  if (loader._error && !egg_accept_errors) {
30  egg2pg_cat.error()
31  << "Errors in egg file.\n";
32  return NULL;
33  }
34 
35  if (loader._root != (PandaNode *)NULL && egg_flatten) {
37 
38  int combine_siblings_bits = 0;
39  if (egg_combine_geoms) {
40  combine_siblings_bits |= SceneGraphReducer::CS_geom_node;
41  }
42  if (egg_flatten_radius > 0.0) {
43  combine_siblings_bits |= SceneGraphReducer::CS_within_radius;
44  gr.set_combine_radius(egg_flatten_radius);
45  }
46 
47  int num_reduced = gr.flatten(loader._root, combine_siblings_bits);
48  egg2pg_cat.info() << "Flattened " << num_reduced << " nodes.\n";
49 
50  if (egg_unify) {
51  // We want to premunge before unifying, since otherwise we risk
52  // needlessly duplicating vertices.
53  if (premunge_data) {
54  gr.premunge(loader._root, RenderState::make_empty());
55  }
56  gr.collect_vertex_data(loader._root);
57  gr.unify(loader._root, true);
58  if (egg2pg_cat.is_debug()) {
59  egg2pg_cat.debug() << "Unified.\n";
60  }
61  }
62  }
63 
64  return loader._root;
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: load_egg_file
69 // Description: A convenience function. Loads up the indicated egg
70 // file, and returns the root of a scene graph. Returns
71 // NULL if the file cannot be read for some reason.
72 // Does not search along the egg path for the filename
73 // first; use EggData::resolve_egg_filename() if this is
74 // required.
75 ////////////////////////////////////////////////////////////////////
76 PT(PandaNode)
77 load_egg_file(const Filename &filename, CoordinateSystem cs,
78  BamCacheRecord *record) {
79  Filename egg_filename = filename;
80  egg_filename.set_text();
82 
83  if (record != (BamCacheRecord *)NULL) {
84  record->add_dependent_file(egg_filename);
85  }
86 
87  EggLoader loader;
88  loader._data->set_egg_filename(egg_filename);
89  loader._data->set_auto_resolve_externals(true);
90  loader._data->set_coordinate_system(cs);
91  loader._record = record;
92 
93  PT(VirtualFile) vfile = vfs->get_file(egg_filename);
94  if (vfile == NULL) {
95  return NULL;
96  }
97 
98  loader._data->set_egg_timestamp(vfile->get_timestamp());
99 
100  bool okflag;
101  istream *istr = vfile->open_read_file(true);
102  if (istr == (istream *)NULL) {
103  egg2pg_cat.error()
104  << "Couldn't read " << egg_filename << "\n";
105  return NULL;
106  }
107 
108  egg2pg_cat.info()
109  << "Reading " << egg_filename << "\n";
110 
111  okflag = loader._data->read(*istr);
112  vfile->close_read_file(istr);
113 
114  if (!okflag) {
115  egg2pg_cat.error()
116  << "Error reading " << egg_filename << "\n";
117  return NULL;
118  }
119 
120  return load_from_loader(loader);
121 }
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function: load_egg_data
125 // Description: Another convenience function; works like
126 // load_egg_file() but starts from an already-filled
127 // EggData structure. The structure is destroyed in the
128 // loading.
129 ////////////////////////////////////////////////////////////////////
130 PT(PandaNode)
131 load_egg_data(EggData *data, CoordinateSystem cs) {
132  // We temporarily shuttle the children to a holding node so we can
133  // copy them into the EggLoader's structure without it complaining.
134  EggGroupNode children_holder;
135  children_holder.steal_children(*data);
136 
137  EggLoader loader(data);
138  loader._data->steal_children(children_holder);
139 
140  loader._data->set_auto_resolve_externals(true);
141  loader._data->set_coordinate_system(cs);
142 
143  return load_from_loader(loader);
144 }
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
int collect_vertex_data(PandaNode *root, int collect_bits=~0)
Collects all different GeomVertexData blocks that have compatible formats at this node and below into...
A base class for nodes in the hierarchy that are not leaf nodes.
Definition: eggGroupNode.h:51
A hierarchy of directories and files that appears to be one continuous file system, even though the files may originate from several different sources that may not be related to the actual OS&#39;s file system.
void set_text()
Indicates that the filename represents a text file.
Definition: filename.I:507
An interface for simplifying ("flattening") scene graphs by eliminating unneeded nodes and collapsing...
void set_combine_radius(PN_stdfloat combine_radius)
Specifies the radius that is used in conjunction with CS_within_radius to decide whether a subgraph&#39;s...
This is the primary interface into all the egg data, and the root of the egg file structure...
Definition: eggData.h:41
The abstract base class for a file or directory within the VirtualFileSystem.
Definition: virtualFile.h:37
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
void unify(PandaNode *root, bool preserve_order)
Calls unify() on every GeomNode at this level and below.
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
void steal_children(EggGroupNode &other)
Moves all the children from the other node to this one.
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
void premunge(PandaNode *root, const RenderState *initial_state)
Walks the scene graph rooted at this node and below, and uses the indicated GSG to premunge every Geo...
PointerTo< VirtualFile > get_file(const Filename &filename, bool status_only=false) const
Looks up the file by the indicated name in the file system.
int flatten(PandaNode *root, int combine_siblings_bits)
Simplifies the graph by removing unnecessary nodes and nodes.
This class stores a list of directories that can be searched, in order, to locate a particular file...
Definition: dSearchPath.h:32
void add_dependent_file(const Filename &pathname)
Adds the indicated file to the list of files that will be loaded to generate the data in this record...
Converts an egg data structure, possibly read from an egg file but not necessarily, into a scene graph suitable for rendering.
Definition: eggLoader.h:70