Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

pandatool/src/eggbase/somethingToEgg.cxx

Go to the documentation of this file.
00001 // Filename: somethingToEgg.cxx
00002 // Created by:  drose (15Feb00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "somethingToEgg.h"
00020 #include "somethingToEggConverter.h"
00021 
00022 #include "config_util.h"
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //     Function: SomethingToEgg::Constructor
00026 //       Access: Public
00027 //  Description: The first parameter to the constructor should be the
00028 //               one-word name of the file format that is to be read,
00029 //               for instance "OpenFlight" or "Alias".  It's just used
00030 //               in printing error messages and such.
00031 ////////////////////////////////////////////////////////////////////
00032 SomethingToEgg::
00033 SomethingToEgg(const string &format_name,
00034                const string &preferred_extension,
00035                bool allow_last_param, bool allow_stdout) :
00036   EggConverter(format_name, preferred_extension, allow_last_param, allow_stdout)
00037 {
00038   clear_runlines();
00039   if (_allow_last_param) {
00040     add_runline("[opts] input" + _preferred_extension + " output.egg");
00041   }
00042   add_runline("[opts] -o output.egg input" + _preferred_extension);
00043   if (_allow_stdout) {
00044     add_runline("[opts] input" + _preferred_extension + " >output.egg");
00045   }
00046 
00047   // -f doesn't make sense if we aren't reading egg files.
00048   remove_option("f");
00049 
00050   redescribe_option
00051     ("cs",
00052      "Specify the coordinate system of the input " + _format_name +
00053      " file.  Normally, this can inferred from the file itself.");
00054 
00055   add_option
00056     ("ignore", "", 0,
00057      "Ignore non-fatal errors and generate an egg file anyway.",
00058      &SomethingToEgg::dispatch_none, &_allow_errors);
00059 
00060   _input_units = DU_invalid;
00061   _output_units = DU_invalid;
00062   _animation_convert = AC_none;
00063   _got_start_frame = false;
00064   _got_end_frame = false;
00065   _got_frame_inc = false;
00066   _got_neutral_frame = false;
00067   _got_input_frame_rate = false;
00068   _got_output_frame_rate = false;
00069   _merge_externals = false;
00070 }
00071 
00072 ////////////////////////////////////////////////////////////////////
00073 //     Function: SomethingToEgg::add_units_options
00074 //       Access: Public
00075 //  Description: Adds -ui and -uo as valid options for this program.
00076 //               If the user specifies -uo and -ui, or just -uo and
00077 //               the program specifies -ui by setting _input_units,
00078 //               the indicated units conversion will be automatically
00079 //               applied before writing out the egg file.
00080 ////////////////////////////////////////////////////////////////////
00081 void SomethingToEgg::
00082 add_units_options() {
00083   add_option
00084     ("ui", "units", 40,
00085      "Specify the units of the input " + _format_name +
00086      " file.  Normally, this can be inferred from the file itself.",
00087      &SomethingToEgg::dispatch_units, NULL, &_input_units);
00088 
00089   add_option
00090     ("uo", "units", 40,
00091      "Specify the units of the resulting egg file.  If this is "
00092      "specified, the vertices in the egg file will be scaled as "
00093      "necessary to make the appropriate units conversion; otherwise, "
00094      "the vertices will be left as they are.",
00095      &SomethingToEgg::dispatch_units, NULL, &_output_units);
00096 }
00097 
00098 ////////////////////////////////////////////////////////////////////
00099 //     Function: SomethingToEgg::add_animation_options
00100 //       Access: Public
00101 //  Description: Adds options appropriate to animation packages.
00102 ////////////////////////////////////////////////////////////////////
00103 void SomethingToEgg::
00104 add_animation_options() {
00105   add_option
00106     ("a", "animation-mode", 40,
00107      "Specifies how animation from the " + _format_name + " file is "
00108      "converted to egg, if at all.  At present, the following keywords "
00109      "are supported: none, pose, flip, model, chan, or both.  The default "
00110      "is none, which means not to convert animation.",
00111      &SomethingToEgg::dispatch_animation_convert, NULL, &_animation_convert);
00112 
00113   add_option
00114     ("cn", "name", 40,
00115      "Specifies the name of the animation character.  This should match "
00116      "between all of the model files and all of the channel files for a "
00117      "particular model and its associated channels.",
00118      &SomethingToEgg::dispatch_string, NULL, &_character_name);
00119 
00120   add_option
00121     ("sf", "start-frame", 40,
00122      "Specifies the starting frame of animation to extract.  If omitted, "
00123      "the first frame of the time slider will be used.  For -a pose, this "
00124      "is the one frame of animation to extract.",
00125      &SomethingToEgg::dispatch_double, &_got_start_frame, &_start_frame);
00126 
00127   add_option
00128     ("ef", "end-frame", 40,
00129      "Specifies the ending frame of animation to extract.  If omitted, "
00130      "the last frame of the time slider will be used.",
00131      &SomethingToEgg::dispatch_double, &_got_end_frame, &_end_frame);
00132 
00133   add_option
00134     ("if", "frame-inc", 40,
00135      "Specifies the increment between successive frames.  If omitted, "
00136      "this is taken from the time slider settings, or 1.0 if the time "
00137      "slider does not specify.",
00138      &SomethingToEgg::dispatch_double, &_got_frame_inc, &_frame_inc);
00139 
00140   add_option
00141     ("nf", "neutral-frame", 40,
00142      "Specifies the frame number to use for the neutral pose.  The model "
00143      "will be set to this frame before extracting out the neutral character.  "
00144      "If omitted, the current frame of the model is used.  This is only "
00145      "relevant for -a model or -a both.",
00146      &SomethingToEgg::dispatch_double, &_got_neutral_frame, &_neutral_frame);
00147 
00148   add_option
00149     ("fri", "fps", 40,
00150      "Specify the frame rate (frames per second) of the input " + _format_name +
00151      " file.  Normally, this can be inferred from the file itself.",
00152      &SomethingToEgg::dispatch_double, &_got_input_frame_rate, &_input_frame_rate);
00153 
00154   add_option
00155     ("fro", "fps", 40,
00156      "Specify the frame rate (frames per second) of the generated animation.  "
00157      "If this is specified, the animation speed is scaled by the appropriate "
00158      "factor based on the frame rate of the input file (see -ri).",
00159      &SomethingToEgg::dispatch_double, &_got_output_frame_rate, &_output_frame_rate);
00160 }
00161 
00162 ////////////////////////////////////////////////////////////////////
00163 //     Function: SomethingToEgg::add_merge_externals_options
00164 //       Access: Public
00165 //  Description: Adds -f.
00166 ////////////////////////////////////////////////////////////////////
00167 void SomethingToEgg::
00168 add_merge_externals_options() {
00169   add_option
00170     ("f", "", 40,
00171      "Follow and convert all external references in the source file.",
00172      &SomethingToEgg::dispatch_none, &_merge_externals);
00173 }
00174 
00175 ////////////////////////////////////////////////////////////////////
00176 //     Function: SomethingToEgg::apply_units_scale
00177 //       Access: Protected
00178 //  Description: Applies the scale indicated by the input and output
00179 //               units to the indicated egg file.  This is normally
00180 //               done automatically when the file is written out.
00181 ////////////////////////////////////////////////////////////////////
00182 void SomethingToEgg::
00183 apply_units_scale(EggData &data) {
00184   if (_output_units != DU_invalid && _input_units != DU_invalid &&
00185       _input_units != _output_units) {
00186     nout << "Converting from " << format_long_unit(_input_units)
00187          << " to " << format_long_unit(_output_units) << "\n";
00188     double scale = convert_units(_input_units, _output_units);
00189     data.transform(LMatrix4d::scale_mat(scale));
00190   }
00191 }
00192 
00193 ////////////////////////////////////////////////////////////////////
00194 //     Function: SomethingToEgg::apply_parameters
00195 //       Access: Protected
00196 //  Description: Copies the relevant parameters specified by the user
00197 //               on the command line (if add_path_replace_options(),
00198 //               add_path_store_options(), or add_animation_options()
00199 //               was used) to the converter.
00200 ////////////////////////////////////////////////////////////////////
00201 void SomethingToEgg::
00202 apply_parameters(SomethingToEggConverter &converter) {
00203   converter.set_path_replace(_path_replace);
00204 
00205   converter.set_animation_convert(_animation_convert);
00206   converter.set_character_name(_character_name);
00207   if (_got_start_frame) {
00208     converter.set_start_frame(_start_frame);
00209   }
00210   if (_got_end_frame) {
00211     converter.set_end_frame(_end_frame);
00212   }
00213   if (_got_frame_inc) {
00214     converter.set_frame_inc(_frame_inc);
00215   }
00216   if (_got_neutral_frame) {
00217     converter.set_neutral_frame(_neutral_frame);
00218   }
00219   if (_got_input_frame_rate) {
00220     converter.set_input_frame_rate(_input_frame_rate);
00221   }
00222   if (_got_output_frame_rate) {
00223     converter.set_output_frame_rate(_output_frame_rate);
00224   }
00225 }
00226 
00227 ////////////////////////////////////////////////////////////////////
00228 //     Function: SomethingToEgg::handle_args
00229 //       Access: Protected
00230 //  Description:
00231 ////////////////////////////////////////////////////////////////////
00232 bool SomethingToEgg::
00233 handle_args(Args &args) {
00234   if (_allow_last_param && !_got_output_filename && args.size() > 1) {
00235     _got_output_filename = true;
00236     _output_filename = Filename::from_os_specific(args.back());
00237     args.pop_back();
00238 
00239     if (!(_output_filename.get_extension() == "egg")) {
00240       nout << "Output filename " << _output_filename
00241            << " does not end in .egg.  If this is really what you intended, "
00242         "use the -o output_file syntax.\n";
00243       return false;
00244     }
00245 
00246     if (!verify_output_file_safe()) {
00247       return false;
00248     }
00249   }
00250 
00251   if (args.empty()) {
00252     nout << "You must specify the " << _format_name
00253           << " file to read on the command line.\n";
00254     return false;
00255   }
00256 
00257   if (args.size() != 1) {
00258     nout << "You may only specify one " << _format_name
00259          << " file to read on the command line.  "
00260          << "You specified: ";
00261     Args::const_iterator ai;
00262     for (ai = args.begin(); ai != args.end(); ++ai) {
00263       nout << (*ai) << " ";
00264     }
00265     nout << "\n";
00266     return false;
00267   }
00268 
00269   _input_filename = Filename::from_os_specific(args[0]);
00270 
00271   if (!_input_filename.exists()) {
00272     nout << "Cannot find input file " << _input_filename << "\n";
00273     return false;
00274   }
00275 
00276   return true;
00277 }
00278 
00279 ////////////////////////////////////////////////////////////////////
00280 //     Function: SomethingToEgg::post_command_line
00281 //       Access: Protected, Virtual
00282 //  Description: This is called after the command line has been
00283 //               completely processed, and it gives the program a
00284 //               chance to do some last-minute processing and
00285 //               validation of the options and arguments.  It should
00286 //               return true if everything is fine, false if there is
00287 //               an error.
00288 ////////////////////////////////////////////////////////////////////
00289 bool SomethingToEgg::
00290 post_command_line() {
00291   // Prepend the source filename to the model path.
00292   DSearchPath &model_path = get_model_path();
00293   Filename directory = _input_filename.get_dirname();
00294   if (directory.empty()) {
00295     directory = ".";
00296   }
00297   model_path.prepend_directory(directory);
00298 
00299   return EggConverter::post_command_line();
00300 }
00301 
00302 ////////////////////////////////////////////////////////////////////
00303 //     Function: SomethingToEgg::post_process_egg_file
00304 //       Access: Protected, Virtual
00305 //  Description: Performs any processing of the egg file that is
00306 //               appropriate before writing it out.  This includes any
00307 //               normal adjustments the user requested via -np, etc.
00308 //
00309 //               Normally, you should not need to call this function
00310 //               directly; write_egg_file() calls it for you.  You
00311 //               should call this only if you do not use
00312 //               write_egg_file() to write out the resulting egg file.
00313 ////////////////////////////////////////////////////////////////////
00314 void SomethingToEgg::
00315 post_process_egg_file() {
00316   apply_units_scale(_data);
00317   EggConverter::post_process_egg_file();
00318 }
00319 
00320 ////////////////////////////////////////////////////////////////////
00321 //     Function: SomethingToEgg::dispatch_animation_convert
00322 //       Access: Protected, Static
00323 //  Description: Dispatch function to set the given animation convert mode
00324 //               according to the specified parameter.  var is a
00325 //               pointer to an AnimationConvert variable.
00326 ////////////////////////////////////////////////////////////////////
00327 bool SomethingToEgg::
00328 dispatch_animation_convert(const string &opt, const string &arg, void *var) {
00329   AnimationConvert *ip = (AnimationConvert *)var;
00330   (*ip) = string_animation_convert(arg);
00331   if ((*ip) == AC_invalid) {
00332     nout << "Invalid keyword for -" << opt << ": " << arg << "\n";
00333     return false;
00334   }    
00335 
00336   return true;
00337 }

Generated on Fri May 2 03:18:37 2003 for Panda-Tool by doxygen1.3