Panda3D

eggWriter.cxx

00001 // Filename: eggWriter.cxx
00002 // Created by:  drose (14Feb00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "eggWriter.h"
00016 
00017 #include "string_utils.h"
00018 #include "compose_matrix.h"
00019 #include "globPattern.h"
00020 
00021 ////////////////////////////////////////////////////////////////////
00022 //     Function: EggWriter::Constructor
00023 //       Access: Public
00024 //  Description: Egg-writing type programs may specify their output
00025 //               file using either the last-filename convention, the
00026 //               -o convention, and/or implicitly writing the result
00027 //               to standard output.  Not all interfaces are
00028 //               appropriate for all applications; some may be
00029 //               confusing or dangerous.
00030 //
00031 //               The calling application should pass allow_last_param
00032 //               true to allow the user to specify the output filename
00033 //               as the last parameter on the command line (the most
00034 //               dangerous, but convenient, method), and allow_stdout
00035 //               true to allow the user to omit the output filename
00036 //               altogether and have the output implicitly go to
00037 //               standard output (not terribly dangerous, but
00038 //               inappropriate when writing binary file formats).
00039 ////////////////////////////////////////////////////////////////////
00040 EggWriter::
00041 EggWriter(bool allow_last_param, bool allow_stdout) :
00042   WithOutputFile(allow_last_param, allow_stdout, false)
00043 {
00044   // Indicate the extension name we expect the user to supply for
00045   // output files.
00046   _preferred_extension = ".egg";
00047 
00048   clear_runlines();
00049   if (_allow_last_param) {
00050     add_runline("[opts] output.egg");
00051   }
00052   add_runline("[opts] -o output.egg");
00053   if (_allow_stdout) {
00054     add_runline("[opts] >output.egg");
00055   }
00056 
00057   string o_description;
00058 
00059   if (_allow_stdout) {
00060     if (_allow_last_param) {
00061       o_description =
00062         "Specify the filename to which the resulting egg file will be written.  "
00063         "If this option is omitted, the last parameter name is taken to be the "
00064         "name of the output file, or standard output is used if there are no "
00065         "other parameters.";
00066     } else {
00067       o_description =
00068         "Specify the filename to which the resulting egg file will be written.  "
00069         "If this option is omitted, the egg file is written to standard output.";
00070     }
00071   } else {
00072     if (_allow_last_param) {
00073       o_description =
00074         "Specify the filename to which the resulting egg file will be written.  "
00075         "If this option is omitted, the last parameter name is taken to be the "
00076         "name of the output file.";
00077     } else {
00078       o_description =
00079         "Specify the filename to which the resulting egg file will be written.";
00080     }
00081   }
00082 
00083   add_option
00084     ("o", "filename", 50, o_description,
00085      &EggWriter::dispatch_filename, &_got_output_filename, &_output_filename);
00086 
00087   redescribe_option
00088     ("cs",
00089      "Specify the coordinate system of the resulting egg file.  This may be "
00090      "one of 'y-up', 'z-up', 'y-up-left', or 'z-up-left'.  The default is "
00091      "y-up.");
00092 }
00093 
00094 
00095 ////////////////////////////////////////////////////////////////////
00096 //     Function: EggWriter::as_writer
00097 //       Access: Public, Virtual
00098 //  Description: Returns this object as an EggWriter pointer, if it is
00099 //               in fact an EggWriter, or NULL if it is not.
00100 //
00101 //               This is intended to work around the C++ limitation
00102 //               that prevents downcasts past virtual inheritance.
00103 //               Since both EggReader and EggWriter inherit virtually
00104 //               from EggSingleBase, we need functions like this to downcast
00105 //               to the appropriate pointer.
00106 ////////////////////////////////////////////////////////////////////
00107 EggWriter *EggWriter::
00108 as_writer() {
00109   return this;
00110 }
00111 
00112 ////////////////////////////////////////////////////////////////////
00113 //     Function: EggWriter::post_process_egg_file
00114 //       Access: Public, Virtual
00115 //  Description: Performs any processing of the egg file that is
00116 //               appropriate before writing it out.  This includes any
00117 //               normal adjustments the user requested via -np, etc.
00118 //
00119 //               Normally, you should not need to call this function
00120 //               directly; write_egg_file() calls it for you.  You
00121 //               should call this only if you do not use
00122 //               write_egg_file() to write out the resulting egg file.
00123 ////////////////////////////////////////////////////////////////////
00124 void EggWriter::
00125 post_process_egg_file() {
00126   if (_got_transform) {
00127     nout << "Applying transform matrix:\n";
00128     _transform.write(nout, 2);
00129     LVecBase3d scale, hpr, translate;
00130     if (decompose_matrix(_transform, scale, hpr, translate,
00131                          _data->get_coordinate_system())) {
00132       nout << "(scale " << scale << ", hpr " << hpr << ", translate "
00133            << translate << ")\n";
00134     }
00135     _data->transform(_transform);
00136   }
00137 
00138   bool needs_remove = false;
00139 
00140   switch (_normals_mode) {
00141   case NM_strip:
00142     nout << "Stripping normals.\n";
00143     _data->strip_normals();
00144     needs_remove = true;
00145     break;
00146 
00147   case NM_polygon:
00148     nout << "Recomputing polygon normals.\n";
00149     _data->recompute_polygon_normals();
00150     needs_remove = true;
00151     break;
00152 
00153   case NM_vertex:
00154     nout << "Recomputing vertex normals.\n";
00155     _data->recompute_vertex_normals(_normals_threshold);
00156     needs_remove = true;
00157     break;
00158 
00159   case NM_preserve:
00160     // Do nothing.
00161     break;
00162   }
00163 
00164   if (_got_tbnall) {
00165     needs_remove |= _data->recompute_tangent_binormal(GlobPattern("*"));
00166   } else {
00167     if (_got_tbnauto) {
00168       needs_remove |= _data->recompute_tangent_binormal_auto();
00169     }
00170     needs_remove |= _data->recompute_tangent_binormal(_tbn_names);
00171   }
00172 
00173   if (needs_remove) {
00174     _data->remove_unused_vertices(true);
00175   }
00176 }
00177 
00178 ////////////////////////////////////////////////////////////////////
00179 //     Function: EggWriter::write_egg_file
00180 //       Access: Public
00181 //  Description: Writes out the egg file as the normal result of the
00182 //               program.  This calls post_process_egg_file() to
00183 //               perform any last minute processing (like normal
00184 //               computation) and then writes out the file to the
00185 //               output stream returned by get_output().
00186 ////////////////////////////////////////////////////////////////////
00187 void EggWriter::
00188 write_egg_file() {
00189   post_process_egg_file();
00190   _data->write_egg(get_output());
00191 }
00192 
00193 ////////////////////////////////////////////////////////////////////
00194 //     Function: EggWriter::handle_args
00195 //       Access: Protected, Virtual
00196 //  Description: Does something with the additional arguments on the
00197 //               command line (after all the -options have been
00198 //               parsed).  Returns true if the arguments are good,
00199 //               false otherwise.
00200 ////////////////////////////////////////////////////////////////////
00201 bool EggWriter::
00202 handle_args(ProgramBase::Args &args) {
00203   if (!check_last_arg(args, 0)) {
00204     return false;
00205   }
00206 
00207   if (!args.empty()) {
00208     nout << "Unexpected arguments on command line:\n";
00209     Args::const_iterator ai;
00210     for (ai = args.begin(); ai != args.end(); ++ai) {
00211       nout << (*ai) << " ";
00212     }
00213     nout << "\r";
00214     return false;
00215   }
00216 
00217   if (!_got_path_directory && _got_output_filename) {
00218     // Put in the name of the output directory.
00219     _path_replace->_path_directory = _output_filename.get_dirname();
00220   }
00221 
00222   return true;
00223 }
00224 
00225 ////////////////////////////////////////////////////////////////////
00226 //     Function: EggWriter::post_command_line
00227 //       Access: Protected, Virtual
00228 //  Description:
00229 ////////////////////////////////////////////////////////////////////
00230 bool EggWriter::
00231 post_command_line() {
00232   if (!_allow_stdout && !_got_output_filename) {
00233     nout << "You must specify the filename to write with -o.\n";
00234     return false;
00235   }
00236 
00237   append_command_comment(_data);
00238 
00239   return EggSingleBase::post_command_line();
00240 }
 All Classes Functions Variables Enumerations