Panda3D
load_egg_file.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file load_egg_file.cxx
10  * @author drose
11  * @date 2002-02-26
12  */
13 
14 #include "load_egg_file.h"
15 #include "eggLoader.h"
16 #include "config_egg2pg.h"
17 #include "sceneGraphReducer.h"
18 #include "virtualFileSystem.h"
19 #include "config_putil.h"
20 #include "bamCacheRecord.h"
21 
22 static PT(PandaNode)
23 load_from_loader(EggLoader &loader) {
24  loader._data->load_externals(DSearchPath(), loader._record);
25 
26  loader.build_graph();
27 
28  if (loader._error && !egg_accept_errors) {
29  egg2pg_cat.error()
30  << "Errors in egg file.\n";
31  return nullptr;
32  }
33 
34  if (loader._root != nullptr && egg_flatten) {
36 
37  int combine_siblings_bits = 0;
38  if (egg_combine_geoms) {
39  combine_siblings_bits |= SceneGraphReducer::CS_geom_node;
40  }
41  if (egg_flatten_radius > 0.0) {
42  combine_siblings_bits |= SceneGraphReducer::CS_within_radius;
43  gr.set_combine_radius(egg_flatten_radius);
44  }
45 
46  int num_reduced = gr.flatten(loader._root, combine_siblings_bits);
47  egg2pg_cat.info() << "Flattened " << num_reduced << " nodes.\n";
48 
49  if (egg_unify) {
50  // We want to premunge before unifying, since otherwise we risk
51  // needlessly duplicating vertices.
52  if (premunge_data) {
53  gr.premunge(loader._root, RenderState::make_empty());
54  }
55  gr.collect_vertex_data(loader._root);
56  gr.unify(loader._root, true);
57  if (egg2pg_cat.is_debug()) {
58  egg2pg_cat.debug() << "Unified.\n";
59  }
60  }
61  }
62 
63  return loader._root;
64 }
65 
66 /**
67  * A convenience function. Loads up the indicated egg file, and returns the
68  * root of a scene graph. Returns NULL if the file cannot be read for some
69  * reason. Does not search along the egg path for the filename first; use
70  * EggData::resolve_egg_filename() if this is required.
71  */
73 load_egg_file(const Filename &filename, CoordinateSystem cs,
74  BamCacheRecord *record) {
75  Filename egg_filename = filename;
76  egg_filename.set_text();
78 
79  if (record != nullptr) {
80  record->add_dependent_file(egg_filename);
81  }
82 
83  EggLoader loader;
84  loader._data->set_egg_filename(egg_filename);
85  loader._data->set_auto_resolve_externals(true);
86  loader._data->set_coordinate_system(cs);
87  loader._record = record;
88 
89  PT(VirtualFile) vfile = vfs->get_file(egg_filename);
90  if (vfile == nullptr) {
91  return nullptr;
92  }
93 
94  loader._data->set_egg_timestamp(vfile->get_timestamp());
95 
96  bool okflag;
97  std::istream *istr = vfile->open_read_file(true);
98  if (istr == nullptr) {
99  egg2pg_cat.error()
100  << "Couldn't read " << egg_filename << "\n";
101  return nullptr;
102  }
103 
104  egg2pg_cat.info()
105  << "Reading " << egg_filename << "\n";
106 
107  okflag = loader._data->read(*istr);
108  vfile->close_read_file(istr);
109 
110  if (!okflag) {
111  egg2pg_cat.error()
112  << "Error reading " << egg_filename << "\n";
113  return nullptr;
114  }
115 
116  return load_from_loader(loader);
117 }
118 
119 /**
120  * Another convenience function; works like load_egg_file() but starts from an
121  * already-filled EggData structure. The structure is destroyed in the
122  * loading.
123  */
124 PT(PandaNode)
125 load_egg_data(EggData *data, CoordinateSystem cs) {
126  // We temporarily shuttle the children to a holding node so we can copy them
127  // into the EggLoader's structure without it complaining.
128  EggGroupNode children_holder;
129  children_holder.steal_children(*data);
130 
131  EggLoader loader(data);
132  loader._data->steal_children(children_holder);
133 
134  loader._data->set_auto_resolve_externals(true);
135  loader._data->set_coordinate_system(cs);
136 
137  return load_from_loader(loader);
138 }
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
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...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A base class for nodes in the hierarchy that are not leaf nodes.
Definition: eggGroupNode.h:46
A hierarchy of directories and files that appears to be one continuous file system,...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_text()
Indicates that the filename represents a text file.
Definition: filename.I:424
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's...
PT(PandaNode) load_egg_data(EggData *data
Another convenience function; works like load_egg_file() but starts from an already-filled EggData st...
This is the primary interface into all the egg data, and the root of the egg file structure.
Definition: eggData.h:37
The abstract base class for a file or directory within the VirtualFileSystem.
Definition: virtualFile.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class stores a list of directories that can be searched, in order, to locate a particular file.
Definition: dSearchPath.h:28
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,...
Definition: eggLoader.h:67