Panda3D
 All Classes Functions Variables Enumerations
eggWriter.cxx
1 // Filename: eggWriter.cxx
2 // Created by: drose (14Feb00)
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 "eggWriter.h"
16 
17 #include "string_utils.h"
18 #include "compose_matrix.h"
19 #include "globPattern.h"
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: EggWriter::Constructor
23 // Access: Public
24 // Description: Egg-writing type programs may specify their output
25 // file using either the last-filename convention, the
26 // -o convention, and/or implicitly writing the result
27 // to standard output. Not all interfaces are
28 // appropriate for all applications; some may be
29 // confusing or dangerous.
30 //
31 // The calling application should pass allow_last_param
32 // true to allow the user to specify the output filename
33 // as the last parameter on the command line (the most
34 // dangerous, but convenient, method), and allow_stdout
35 // true to allow the user to omit the output filename
36 // altogether and have the output implicitly go to
37 // standard output (not terribly dangerous, but
38 // inappropriate when writing binary file formats).
39 ////////////////////////////////////////////////////////////////////
41 EggWriter(bool allow_last_param, bool allow_stdout) :
42  WithOutputFile(allow_last_param, allow_stdout, false)
43 {
44  // Indicate the extension name we expect the user to supply for
45  // output files.
46  _preferred_extension = ".egg";
47 
48  clear_runlines();
49  if (_allow_last_param) {
50  add_runline("[opts] output.egg");
51  }
52  add_runline("[opts] -o output.egg");
53  if (_allow_stdout) {
54  add_runline("[opts] >output.egg");
55  }
56 
57  string o_description;
58 
59  if (_allow_stdout) {
60  if (_allow_last_param) {
61  o_description =
62  "Specify the filename to which the resulting egg 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, or standard output is used if there are no "
65  "other parameters.";
66  } else {
67  o_description =
68  "Specify the filename to which the resulting egg file will be written. "
69  "If this option is omitted, the egg file is written to standard output.";
70  }
71  } else {
72  if (_allow_last_param) {
73  o_description =
74  "Specify the filename to which the resulting egg file will be written. "
75  "If this option is omitted, the last parameter name is taken to be the "
76  "name of the output file.";
77  } else {
78  o_description =
79  "Specify the filename to which the resulting egg file will be written.";
80  }
81  }
82 
83  add_option
84  ("o", "filename", 50, o_description,
85  &EggWriter::dispatch_filename, &_got_output_filename, &_output_filename);
86 
87  redescribe_option
88  ("cs",
89  "Specify the coordinate system of the resulting egg file. This may be "
90  "one of 'y-up', 'z-up', 'y-up-left', or 'z-up-left'. The default is "
91  "y-up.");
92 }
93 
94 
95 ////////////////////////////////////////////////////////////////////
96 // Function: EggWriter::as_writer
97 // Access: Public, Virtual
98 // Description: Returns this object as an EggWriter pointer, if it is
99 // in fact an EggWriter, or NULL if it is not.
100 //
101 // This is intended to work around the C++ limitation
102 // that prevents downcasts past virtual inheritance.
103 // Since both EggReader and EggWriter inherit virtually
104 // from EggSingleBase, we need functions like this to downcast
105 // to the appropriate pointer.
106 ////////////////////////////////////////////////////////////////////
109  return this;
110 }
111 
112 ////////////////////////////////////////////////////////////////////
113 // Function: EggWriter::post_process_egg_file
114 // Access: Public, Virtual
115 // Description: Performs any processing of the egg file that is
116 // appropriate before writing it out. This includes any
117 // normal adjustments the user requested via -np, etc.
118 //
119 // Normally, you should not need to call this function
120 // directly; write_egg_file() calls it for you. You
121 // should call this only if you do not use
122 // write_egg_file() to write out the resulting egg file.
123 ////////////////////////////////////////////////////////////////////
124 void EggWriter::
126  if (_got_transform) {
127  nout << "Applying transform matrix:\n";
128  _transform.write(nout, 2);
129  LVecBase3d scale, hpr, translate;
130  if (decompose_matrix(_transform, scale, hpr, translate,
131  _data->get_coordinate_system())) {
132  nout << "(scale " << scale << ", hpr " << hpr << ", translate "
133  << translate << ")\n";
134  }
135  _data->transform(_transform);
136  }
137 
138  if (_make_points) {
139  nout << "Making points\n";
140  _data->make_point_primitives();
141  }
142 
143  bool needs_remove = false;
144 
145  switch (_normals_mode) {
146  case NM_strip:
147  nout << "Stripping normals.\n";
148  _data->strip_normals();
149  needs_remove = true;
150  break;
151 
152  case NM_polygon:
153  nout << "Recomputing polygon normals.\n";
154  _data->recompute_polygon_normals();
155  needs_remove = true;
156  break;
157 
158  case NM_vertex:
159  nout << "Recomputing vertex normals.\n";
160  _data->recompute_vertex_normals(_normals_threshold);
161  needs_remove = true;
162  break;
163 
164  case NM_preserve:
165  // Do nothing.
166  break;
167  }
168 
169  if (_got_tbnall) {
170  needs_remove |= _data->recompute_tangent_binormal(GlobPattern("*"));
171  } else {
172  if (_got_tbnauto) {
173  needs_remove |= _data->recompute_tangent_binormal_auto();
174  }
175  needs_remove |= _data->recompute_tangent_binormal(_tbn_names);
176  }
177 
178  if (needs_remove) {
179  _data->remove_unused_vertices(true);
180  }
181 }
182 
183 ////////////////////////////////////////////////////////////////////
184 // Function: EggWriter::write_egg_file
185 // Access: Public
186 // Description: Writes out the egg file as the normal result of the
187 // program. This calls post_process_egg_file() to
188 // perform any last minute processing (like normal
189 // computation) and then writes out the file to the
190 // output stream returned by get_output().
191 ////////////////////////////////////////////////////////////////////
192 void EggWriter::
195  _data->write_egg(get_output());
196 }
197 
198 ////////////////////////////////////////////////////////////////////
199 // Function: EggWriter::handle_args
200 // Access: Protected, Virtual
201 // Description: Does something with the additional arguments on the
202 // command line (after all the -options have been
203 // parsed). Returns true if the arguments are good,
204 // false otherwise.
205 ////////////////////////////////////////////////////////////////////
206 bool EggWriter::
207 handle_args(ProgramBase::Args &args) {
208  if (!check_last_arg(args, 0)) {
209  return false;
210  }
211 
212  if (!args.empty()) {
213  nout << "Unexpected arguments on command line:\n";
214  Args::const_iterator ai;
215  for (ai = args.begin(); ai != args.end(); ++ai) {
216  nout << (*ai) << " ";
217  }
218  nout << "\r";
219  return false;
220  }
221 
222  if (!_got_path_directory && _got_output_filename) {
223  // Put in the name of the output directory.
224  _path_replace->_path_directory = _output_filename.get_dirname();
225  }
226 
227  return true;
228 }
229 
230 ////////////////////////////////////////////////////////////////////
231 // Function: EggWriter::post_command_line
232 // Access: Protected, Virtual
233 // Description:
234 ////////////////////////////////////////////////////////////////////
235 bool EggWriter::
236 post_command_line() {
237  if (!_allow_stdout && !_got_output_filename) {
238  nout << "You must specify the filename to write with -o.\n";
239  return false;
240  }
241 
242  append_command_comment(_data);
243 
244  return EggSingleBase::post_command_line();
245 }
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:1455
virtual void post_process_egg_file()
Performs any processing of the egg file that is appropriate before writing it out.
Definition: eggWriter.cxx:125
void write_egg_file()
Writes out the egg file as the normal result of the program.
Definition: eggWriter.cxx:193
virtual EggWriter * as_writer()
Returns this object as an EggWriter pointer, if it is in fact an EggWriter, or NULL if it is not...
Definition: eggWriter.cxx:108
This is the bare functionality (intended to be inherited from along with ProgramBase or some derivati...
ostream & get_output()
Returns an output stream that corresponds to the user&#39;s intended egg file output–either stdout...
This is the base class for a program that generates an egg file output, but doesn&#39;t read any for inpu...
Definition: eggWriter.h:30
EggWriter(bool allow_last_param=false, bool allow_stdout=true)
Egg-writing type programs may specify their output file using either the last-filename convention...
Definition: eggWriter.cxx:41
This class can be used to test for string matches against standard Unix-shell filename globbing conve...
Definition: globPattern.h:37