Panda3D
eggToSomething.cxx
1 // Filename: eggToSomething.cxx
2 // Created by: drose (15Feb00)
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 "eggToSomething.h"
16 
17 ////////////////////////////////////////////////////////////////////
18 // Function: EggToSomething::Constructor
19 // Access: Public
20 // Description: The first parameter to the constructor should be the
21 // one-word name of the file format that is to be read,
22 // for instance "OpenFlight" or "Alias". It's just used
23 // in printing error messages and such.
24 ////////////////////////////////////////////////////////////////////
26 EggToSomething(const string &format_name,
27  const string &preferred_extension,
28  bool allow_last_param, bool allow_stdout) :
29  EggConverter(format_name, preferred_extension, allow_last_param,
30  allow_stdout)
31 {
32  clear_runlines();
33  if (_allow_last_param) {
34  add_runline("[opts] input.egg output" + _preferred_extension);
35  }
36  add_runline("[opts] -o output" + _preferred_extension + " input.egg");
37  if (_allow_stdout) {
38  add_runline("[opts] input.egg >output" + _preferred_extension);
39  }
40 
41  string o_description;
42 
43  if (_allow_stdout) {
44  if (_allow_last_param) {
45  o_description =
46  "Specify the filename to which the resulting " + format_name +
47  " file will be written. "
48  "If this option is omitted, the last parameter name is taken to be the "
49  "name of the output file, or standard output is used if there are no "
50  "other parameters.";
51  } else {
52  o_description =
53  "Specify the filename to which the resulting " + format_name +
54  " file will be written. "
55  "If this option is omitted, the " + format_name +
56  " file is written to standard output.";
57  }
58  } else {
59  if (_allow_last_param) {
60  o_description =
61  "Specify the filename to which the resulting " + format_name +
62  " file will be written. "
63  "If this option is omitted, the last parameter name is taken to be the "
64  "name of the output file.";
65  } else {
66  o_description =
67  "Specify the filename to which the resulting " + format_name +
68  " file will be written.";
69  }
70  }
71 
72  redescribe_option("o", o_description);
73 
74  redescribe_option
75  ("cs",
76  "Specify the coordinate system of the resulting " + _format_name +
77  " file. This may be "
78  "one of 'y-up', 'z-up', 'y-up-left', or 'z-up-left'. The default "
79  "is the same coordinate system as the input egg file. If this is "
80  "different from the input egg file, a conversion will be performed.");
81 
82  _input_units = DU_invalid;
83  _output_units = DU_invalid;
84 }
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function: EggToSomething::add_units_options
88 // Access: Public
89 // Description: Adds -ui and -uo as valid options for this program.
90 // If the user specifies -uo and -ui, or just -uo and
91 // the program specifies -ui by setting _input_units,
92 // the indicated units conversion will be automatically
93 // applied before writing out the egg file.
94 ////////////////////////////////////////////////////////////////////
97  add_option
98  ("ui", "units", 40,
99  "Specify the units of the input egg file. If this is "
100  "specified, the vertices in the egg file will be scaled as "
101  "necessary to make the appropriate units conversion; otherwise, "
102  "the vertices will be left as they are.",
103  &EggToSomething::dispatch_units, NULL, &_input_units);
104 
105  add_option
106  ("uo", "units", 40,
107  "Specify the units of the resulting " + _format_name +
108  " file. Normally, the default units for the format are used.",
109  &EggToSomething::dispatch_units, NULL, &_output_units);
110 }
111 
112 ////////////////////////////////////////////////////////////////////
113 // Function: EggToSomething::apply_units_scale
114 // Access: Protected
115 // Description: Applies the scale indicated by the input and output
116 // units to the indicated egg file. This is normally
117 // done automatically when the file is read in.
118 ////////////////////////////////////////////////////////////////////
119 void EggToSomething::
120 apply_units_scale(EggData *data) {
121 
122  // [gjeon] since maya's internal unit is fixed to cm
123  // and when we can't change UI unit without affecting data
124  // we need to convert data to cm for now
125  // this will be set later to proper output unit user provided
126  // by using MayaApi::set_units() in eggToMaya.cxx
127  DistanceUnit output_units = _output_units;
128  if (_format_name == "Maya")
129  _output_units = DU_centimeters;
130 
131  if (_output_units != DU_invalid && _input_units != DU_invalid &&
132  _input_units != _output_units) {
133  nout << "Converting from " << format_long_unit(_input_units)
134  << " to " << format_long_unit(_output_units) << "\n";
135  double scale = convert_units(_input_units, _output_units);
136  data->transform(LMatrix4d::scale_mat(scale));
137  }
138  _output_units = output_units;
139 }
140 
141 ////////////////////////////////////////////////////////////////////
142 // Function: EggToSomething::pre_process_egg_file
143 // Access: Protected, Virtual
144 // Description: Performs any processing of the egg file that is
145 // appropriate after reading it in.
146 //
147 // Normally, you should not need to call this function
148 // directly; it is called automatically at startup.
149 ////////////////////////////////////////////////////////////////////
150 void EggToSomething::
151 pre_process_egg_file() {
152  apply_units_scale(_data);
154 }
155 
156 ////////////////////////////////////////////////////////////////////
157 // Function: EggToSomething::handle_args
158 // Access: Protected, Virtual
159 // Description: Does something with the additional arguments on the
160 // command line (after all the -options have been
161 // parsed). Returns true if the arguments are good,
162 // false otherwise.
163 ////////////////////////////////////////////////////////////////////
164 bool EggToSomething::
165 handle_args(ProgramBase::Args &args) {
166  if (!check_last_arg(args, 1)) {
167  return false;
168  }
169 
170  return EggConverter::handle_args(args);
171 }
void transform(const LMatrix4d &mat)
Applies the indicated transformation to the node and all of its descendants.
Definition: eggNode.I:313
static LMatrix4d scale_mat(const LVecBase3d &scale)
Returns a matrix that applies the indicated scale in each of the three axes.
Definition: lmatrix.h:6721
This is the primary interface into all the egg data, and the root of the egg file structure...
Definition: eggData.h:41
This is a general base class for programs that convert between egg files and some other format...
Definition: eggConverter.h:28
EggToSomething(const string &format_name, const string &preferred_extension=string(), bool allow_last_param=true, bool allow_stdout=true)
The first parameter to the constructor should be the one-word name of the file format that is to be r...
virtual void pre_process_egg_file()
Performs any processing of the egg file that is appropriate after reading it in.
Definition: eggReader.cxx:164
void add_units_options()
Adds -ui and -uo as valid options for this program.