Panda3D
loaderFileTypePandatool.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 loaderFileTypePandatool.cxx
10  * @author drose
11  * @date 2001-04-26
12  */
13 
15 #include "config_ptloader.h"
18 #include "config_putil.h"
19 #include "load_egg_file.h"
20 #include "save_egg_file.h"
21 #include "eggData.h"
22 #include "loaderOptions.h"
23 #include "bamCacheRecord.h"
24 
25 TypeHandle LoaderFileTypePandatool::_type_handle;
26 
27 /**
28  *
29  */
30 LoaderFileTypePandatool::
31 LoaderFileTypePandatool(SomethingToEggConverter *loader,
32  EggToSomethingConverter *saver) :
33  _loader(loader), _saver(saver)
34 {
35  if (_loader != nullptr) {
36  _loader->set_merge_externals(true);
37  }
38 }
39 
40 /**
41  *
42  */
43 LoaderFileTypePandatool::
44 ~LoaderFileTypePandatool() {
45 }
46 
47 /**
48  *
49  */
50 std::string LoaderFileTypePandatool::
51 get_name() const {
52  if (_loader != nullptr) {
53  return _loader->get_name();
54  }
55  return _saver->get_name();
56 }
57 
58 /**
59  *
60  */
61 std::string LoaderFileTypePandatool::
62 get_extension() const {
63  if (_loader != nullptr) {
64  return _loader->get_extension();
65  }
66  return _saver->get_extension();
67 }
68 
69 /**
70  * Returns a space-separated list of extension, in addition to the one
71  * returned by get_extension(), that are recognized by this converter.
72  */
73 std::string LoaderFileTypePandatool::
75  if (_loader != nullptr) {
76  return _loader->get_additional_extensions();
77  }
78  return _saver->get_additional_extensions();
79 }
80 
81 /**
82  * Returns true if this file type can transparently load compressed files
83  * (with a .pz or .gz extension), false otherwise.
84  */
87  if (_loader != nullptr) {
88  return _loader->supports_compressed();
89  }
90  return _saver->supports_compressed();
91 }
92 
93 /**
94  * Returns true if the file type can be used to load files, and load_file() is
95  * supported. Returns false if load_file() is unimplemented and will always
96  * fail.
97  */
99 supports_load() const {
100  return (_loader != nullptr);
101 }
102 
103 /**
104  * Returns true if the file type can be used to save files, and save_file() is
105  * supported. Returns false if save_file() is unimplemented and will always
106  * fail.
107  */
109 supports_save() const {
110  return (_saver != nullptr);
111 }
112 
113 /**
114  * Searches for the indicated filename on whatever paths are appropriate to
115  * this file type, and updates it if it is found.
116  */
119  path.resolve_filename(get_model_path(), get_extension());
120 }
121 
122 /**
123  *
124  */
125 PT(PandaNode) LoaderFileTypePandatool::
126 load_file(const Filename &path, const LoaderOptions &options,
127  BamCacheRecord *record) const {
128  if (_loader == nullptr) {
129  return nullptr;
130  }
131 
132  if (record != nullptr) {
133  record->add_dependent_file(path);
134  }
135 
136  PT(PandaNode) result;
137 
138  SomethingToEggConverter *loader = _loader->make_copy();
139 
140  DSearchPath file_path;
141  file_path.append_directory(path.get_dirname());
142  loader->get_path_replace()->_path = file_path;
143 
144  // Convert animation, if the converter supports it.
145  switch (options.get_flags() & LoaderOptions::LF_convert_anim) {
146  case LoaderOptions::LF_convert_anim:
147  loader->set_animation_convert(AC_both);
148  break;
149 
150  case LoaderOptions::LF_convert_skeleton:
151  loader->set_animation_convert(AC_model);
152  break;
153 
154  case LoaderOptions::LF_convert_channels:
155  loader->set_animation_convert(AC_chan);
156  break;
157 
158  default:
159  break;
160  }
161 
162  // Try to convert directly to PandaNode first, if the converter type
163  // supports it.
164  if (ptloader_load_node && loader->supports_convert_to_node(options)) {
165  result = loader->convert_to_node(options, path);
166  if (!result.is_null()) {
167  return result;
168  }
169  }
170 
171  // If the converter type doesn't support the direct PandaNode conversion,
172  // take the slower route through egg instead.
173  PT(EggData) egg_data = new EggData;
174  loader->set_egg_data(egg_data);
175 
176  if (loader->convert_file(path)) {
177  DistanceUnit input_units = loader->get_input_units();
178  if (input_units != DU_invalid && ptloader_units != DU_invalid &&
179  input_units != ptloader_units) {
180  // Convert the file to the units specified by the ptloader-units
181  // Configrc variable.
182  ptloader_cat.info()
183  << "Converting from " << format_long_unit(input_units)
184  << " to " << format_long_unit(ptloader_units) << "\n";
185  double scale = convert_units(input_units, ptloader_units);
186  egg_data->transform(LMatrix4d::scale_mat(scale));
187  }
188 
189  if (!egg_data->has_primitives()) {
190  egg_data->make_point_primitives();
191  } else if (!egg_data->has_normals()) {
192  egg_data->recompute_polygon_normals();
193  }
194 
195  result = load_egg_data(egg_data);
196  }
197  delete loader;
198 
199  return result;
200 }
201 
202 /**
203  *
204  */
205 bool LoaderFileTypePandatool::
206 save_file(const Filename &path, const LoaderOptions &options,
207  PandaNode *node) const {
208  if (_saver == nullptr) {
209  return false;
210  }
211 
212  PT(EggData) egg_data = new EggData;
213  if (!save_egg_data(egg_data, node)) {
214  return false;
215  }
216 
217  EggToSomethingConverter *saver = _saver->make_copy();
218  saver->set_egg_data(egg_data);
219 
220  bool result = saver->write_file(path);
221  delete saver;
222  return result;
223 }
std::string get_dirname() const
Returns the directory part of the filename.
Definition: filename.I:358
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
string format_long_unit(DistanceUnit unit)
Returns the string representing the full name (plural) for the given unit.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Specifies parameters that may be passed to the loader.
Definition: loaderOptions.h:23
double convert_units(DistanceUnit from, DistanceUnit to)
Returns the scaling factor that must be applied to convert from units of "from" to "to".
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool resolve_filename(const DSearchPath &searchpath, const std::string &default_extension=std::string())
Searches the given search path for the filename.
Definition: filename.cxx:1581
virtual std::string get_additional_extensions() const
Returns a space-separated list of extension, in addition to the one returned by get_extension(),...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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:37
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is a base class for a family of converter classes that manage a conversion from egg format to so...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
DistanceUnit
This enumerated type lists all the kinds of units we're likely to come across in model conversion pro...
Definition: distanceUnit.h:23
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void set_egg_data(EggData *egg_data)
Sets the egg data that will be filled in when convert_file() is called.
bool save_egg_data(EggData *data, PandaNode *node)
Another convenience function; works like save_egg_file() but populates an EggData instead of writing ...
virtual bool supports_load() const
Returns true if the file type can be used to load files, and load_file() is supported.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual bool supports_compressed() const
Returns true if this file type can transparently load compressed files (with a .pz or ....
An instance of this class is written to the front of a Bam or Txo file to make the file a cached inst...
virtual std::string get_additional_extensions() const
Returns a space-separated list of extension, in addition to the one returned by get_extension(),...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual bool supports_save() const
Returns true if the file type can be used to save files, and save_file() is supported.
virtual bool supports_compressed() const
Returns true if this file type can transparently save compressed files (with a .pz extension),...
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.
virtual std::string get_additional_extensions() const
Returns a space-separated list of extension, in addition to the one returned by get_extension(),...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
virtual void resolve_filename(Filename &path) const
Searches for the indicated filename on whatever paths are appropriate to this file type,...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is a base class for a family of converter classes that manage a conversion from some file type t...
virtual bool supports_compressed() const
Returns true if this file type can transparently load compressed files (with a .pz extension),...