Panda3D
 All Classes Functions Variables Enumerations
eggMultiBase.cxx
1 // Filename: eggMultiBase.cxx
2 // Created by: drose (02Nov00)
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 "eggMultiBase.h"
16 #include "eggBase.h"
17 #include "eggData.h"
18 #include "eggComment.h"
19 #include "filename.h"
20 #include "dSearchPath.h"
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: EggMultiBase::Constructor
24 // Access: Public
25 // Description:
26 ////////////////////////////////////////////////////////////////////
27 EggMultiBase::
28 EggMultiBase() {
29  add_option
30  ("f", "", 80,
31  "Force complete loading: load up the egg file along with all of its "
32  "external references.",
33  &EggMultiBase::dispatch_none, &_force_complete);
34 
35  add_option
36  ("noabs", "", 0,
37  "Don't allow any of the named egg files to have absolute pathnames. "
38  "If any do, abort with an error. This option is designed to help "
39  "detect errors when populating or building a standalone model tree, "
40  "which should be self-contained and include only relative pathnames.",
41  &EggMultiBase::dispatch_none, &_noabs);
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: EggMultiBase::post_process_egg_files
46 // Access: Public
47 // Description: Performs any processing of the egg file(s) that is
48 // appropriate before writing them out. This includes any
49 // normal adjustments the user requested via -np, etc.
50 //
51 // Normally, you should not need to call this function
52 // directly; write_egg_files() calls it for you. You
53 // should call this only if you do not use
54 // write_egg_files() to write out the resulting egg
55 // files.
56 ////////////////////////////////////////////////////////////////////
57 void EggMultiBase::
59  if (_eggs.empty()) {
60  return;
61  }
62 
63  Eggs::iterator ei;
64  if (_got_transform) {
65  nout << "Applying transform matrix:\n";
66  _transform.write(nout, 2);
67  LVecBase3d scale, hpr, translate;
68  if (decompose_matrix(_transform, scale, hpr, translate,
69  _eggs[0]->get_coordinate_system())) {
70  nout << "(scale " << scale << ", hpr " << hpr << ", translate "
71  << translate << ")\n";
72  }
73  for (ei = _eggs.begin(); ei != _eggs.end(); ++ei) {
74  (*ei)->transform(_transform);
75  }
76  }
77 
78  if (_make_points) {
79  nout << "Making points\n";
80  for (ei = _eggs.begin(); ei != _eggs.end(); ++ei) {
81  (*ei)->make_point_primitives();
82  }
83  }
84 
85  switch (_normals_mode) {
86  case NM_strip:
87  nout << "Stripping normals.\n";
88  for (ei = _eggs.begin(); ei != _eggs.end(); ++ei) {
89  (*ei)->strip_normals();
90  (*ei)->remove_unused_vertices(true);
91  }
92  break;
93 
94  case NM_polygon:
95  nout << "Recomputing polygon normals.\n";
96  for (ei = _eggs.begin(); ei != _eggs.end(); ++ei) {
97  (*ei)->recompute_polygon_normals();
98  (*ei)->remove_unused_vertices(true);
99  }
100  break;
101 
102  case NM_vertex:
103  nout << "Recomputing vertex normals.\n";
104  for (ei = _eggs.begin(); ei != _eggs.end(); ++ei) {
105  (*ei)->recompute_vertex_normals(_normals_threshold);
106  (*ei)->remove_unused_vertices(true);
107  }
108  break;
109 
110  case NM_preserve:
111  // Do nothing.
112  break;
113  }
114 }
115 
116 
117 ////////////////////////////////////////////////////////////////////
118 // Function: EggMultiBase::read_egg
119 // Access: Protected, Virtual
120 // Description: Allocates and returns a new EggData structure that
121 // represents the indicated egg file. If the egg file
122 // cannot be read for some reason, returns NULL.
123 //
124 // This can be overridden by derived classes to control
125 // how the egg files are read, or to extend the
126 // information stored with each egg structure, by
127 // deriving from EggData.
128 ////////////////////////////////////////////////////////////////////
129 PT(EggData) EggMultiBase::
130 read_egg(const Filename &filename) {
131  PT(EggData) data = new EggData;
132 
133  if (!data->read(filename)) {
134  // Failure reading.
135  return (EggData *)NULL;
136  }
137 
138  if (_noabs && data->original_had_absolute_pathnames()) {
139  nout << filename.get_basename()
140  << " includes absolute pathnames!\n";
141  return (EggData *)NULL;
142  }
143 
144  DSearchPath file_path;
145  file_path.append_directory(filename.get_dirname());
146 
147  // We always resolve filenames first based on the source egg
148  // filename, since egg files almost always store relative paths.
149  // This is a temporary kludge around integrating the path_replace
150  // system with the EggData better.
151  //
152  // Update: I believe this kludge is obsolete. Commenting out. - Josh.
153  // data->resolve_filenames(file_path);
154 
155  if (_force_complete) {
156  if (!data->load_externals()) {
157  return (EggData *)NULL;
158  }
159  }
160 
161  // Now resolve the filenames again according to the user's
162  // specified _path_replace.
163  EggBase::convert_paths(data, _path_replace, file_path);
164 
165  if (_got_coordinate_system) {
166  data->set_coordinate_system(_coordinate_system);
167  } else {
168  _coordinate_system = data->get_coordinate_system();
169  _got_coordinate_system = true;
170  }
171 
172  return data;
173 }
This specialization of ProgramBase is intended for programs that read and/or write multiple egg files...
Definition: eggMultiBase.h:36
void append_directory(const Filename &directory)
Adds a new directory to the end of the search list.
This is the primary interface into all the egg data, and the root of the egg file structure...
Definition: eggData.h:41
static void convert_paths(EggNode *node, PathReplace *path_replace, const DSearchPath &additional_path)
Recursively walks the egg hierarchy.
Definition: eggBase.cxx:170
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:1455
void post_process_egg_files()
Performs any processing of the egg file(s) that is appropriate before writing them out...
This class stores a list of directories that can be searched, in order, to locate a particular file...
Definition: dSearchPath.h:32