Panda3D
 All Classes Functions Variables Enumerations
eggToSomething.cxx
00001 // Filename: eggToSomething.cxx
00002 // Created by:  drose (15Feb00)
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 "eggToSomething.h"
00016 
00017 ////////////////////////////////////////////////////////////////////
00018 //     Function: EggToSomething::Constructor
00019 //       Access: Public
00020 //  Description: The first parameter to the constructor should be the
00021 //               one-word name of the file format that is to be read,
00022 //               for instance "OpenFlight" or "Alias".  It's just used
00023 //               in printing error messages and such.
00024 ////////////////////////////////////////////////////////////////////
00025 EggToSomething::
00026 EggToSomething(const string &format_name,
00027                const string &preferred_extension,
00028                bool allow_last_param, bool allow_stdout) :
00029   EggConverter(format_name, preferred_extension, allow_last_param,
00030                allow_stdout)
00031 {
00032   clear_runlines();
00033   if (_allow_last_param) {
00034     add_runline("[opts] input.egg output" + _preferred_extension);
00035   }
00036   add_runline("[opts] -o output" + _preferred_extension + " input.egg");
00037   if (_allow_stdout) {
00038     add_runline("[opts] input.egg >output" + _preferred_extension);
00039   }
00040 
00041   string o_description;
00042 
00043   if (_allow_stdout) {
00044     if (_allow_last_param) {
00045       o_description =
00046         "Specify the filename to which the resulting " + format_name +
00047         " file will be written.  "
00048         "If this option is omitted, the last parameter name is taken to be the "
00049         "name of the output file, or standard output is used if there are no "
00050         "other parameters.";
00051     } else {
00052       o_description =
00053         "Specify the filename to which the resulting " + format_name +
00054         " file will be written.  "
00055         "If this option is omitted, the " + format_name +
00056         " file is written to standard output.";
00057     }
00058   } else {
00059     if (_allow_last_param) {
00060       o_description =
00061         "Specify the filename to which the resulting " + format_name +
00062         " 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.";
00065     } else {
00066       o_description =
00067         "Specify the filename to which the resulting " + format_name +
00068         " file will be written.";
00069     }
00070   }
00071 
00072   redescribe_option("o", o_description);
00073 
00074   redescribe_option
00075     ("cs",
00076      "Specify the coordinate system of the resulting " + _format_name +
00077      " file.  This may be "
00078      "one of 'y-up', 'z-up', 'y-up-left', or 'z-up-left'.  The default "
00079      "is the same coordinate system as the input egg file.  If this is "
00080      "different from the input egg file, a conversion will be performed.");
00081 
00082   _input_units = DU_invalid;
00083   _output_units = DU_invalid;
00084 }
00085 
00086 ////////////////////////////////////////////////////////////////////
00087 //     Function: EggToSomething::add_units_options
00088 //       Access: Public
00089 //  Description: Adds -ui and -uo as valid options for this program.
00090 //               If the user specifies -uo and -ui, or just -uo and
00091 //               the program specifies -ui by setting _input_units,
00092 //               the indicated units conversion will be automatically
00093 //               applied before writing out the egg file.
00094 ////////////////////////////////////////////////////////////////////
00095 void EggToSomething::
00096 add_units_options() {
00097   add_option
00098     ("ui", "units", 40,
00099      "Specify the units of the input egg file.  If this is "
00100      "specified, the vertices in the egg file will be scaled as "
00101      "necessary to make the appropriate units conversion; otherwise, "
00102      "the vertices will be left as they are.",
00103      &EggToSomething::dispatch_units, NULL, &_input_units);
00104 
00105   add_option
00106     ("uo", "units", 40,
00107      "Specify the units of the resulting " + _format_name +
00108      " file.  Normally, the default units for the format are used.",
00109      &EggToSomething::dispatch_units, NULL, &_output_units);
00110 }
00111 
00112 ////////////////////////////////////////////////////////////////////
00113 //     Function: EggToSomething::apply_units_scale
00114 //       Access: Protected
00115 //  Description: Applies the scale indicated by the input and output
00116 //               units to the indicated egg file.  This is normally
00117 //               done automatically when the file is read in.
00118 ////////////////////////////////////////////////////////////////////
00119 void EggToSomething::
00120 apply_units_scale(EggData *data) {
00121 
00122   // [gjeon] since maya's internal unit is fixed to cm
00123   // and when we can't change UI unit without affecting data
00124   // we need to convert data to cm for now
00125   // this will be set later to proper output unit user provided
00126   // by using MayaApi::set_units() in eggToMaya.cxx
00127   DistanceUnit output_units = _output_units;
00128   if (_format_name == "Maya")
00129     _output_units = DU_centimeters;
00130 
00131   if (_output_units != DU_invalid && _input_units != DU_invalid &&
00132       _input_units != _output_units) {
00133     nout << "Converting from " << format_long_unit(_input_units)
00134          << " to " << format_long_unit(_output_units) << "\n";
00135     double scale = convert_units(_input_units, _output_units);
00136     data->transform(LMatrix4d::scale_mat(scale));
00137   }
00138   _output_units = output_units;
00139 }
00140 
00141 ////////////////////////////////////////////////////////////////////
00142 //     Function: EggToSomething::pre_process_egg_file
00143 //       Access: Protected, Virtual
00144 //  Description: Performs any processing of the egg file that is
00145 //               appropriate after reading it in.
00146 //
00147 //               Normally, you should not need to call this function
00148 //               directly; it is called automatically at startup.
00149 ////////////////////////////////////////////////////////////////////
00150 void EggToSomething::
00151 pre_process_egg_file() {
00152   apply_units_scale(_data);
00153   EggConverter::pre_process_egg_file();
00154 }
00155 
00156 ////////////////////////////////////////////////////////////////////
00157 //     Function: EggToSomething::handle_args
00158 //       Access: Protected, Virtual
00159 //  Description: Does something with the additional arguments on the
00160 //               command line (after all the -options have been
00161 //               parsed).  Returns true if the arguments are good,
00162 //               false otherwise.
00163 ////////////////////////////////////////////////////////////////////
00164 bool EggToSomething::
00165 handle_args(ProgramBase::Args &args) {
00166   if (!check_last_arg(args, 1)) {
00167     return false;
00168   }
00169 
00170   return EggConverter::handle_args(args);
00171 }
 All Classes Functions Variables Enumerations