Panda3D
|
00001 // Filename: pfmBba.cxx 00002 // Created by: drose (02Mar11) 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 "pfmBba.h" 00016 #include "config_pfm.h" 00017 #include "pfmFile.h" 00018 #include "pystub.h" 00019 00020 //////////////////////////////////////////////////////////////////// 00021 // Function: PfmBba::Constructor 00022 // Access: Public 00023 // Description: 00024 //////////////////////////////////////////////////////////////////// 00025 PfmBba:: 00026 PfmBba() { 00027 set_program_description 00028 ("pfm-bba generates a .bba file from a .pfm file that lists the " 00029 "planar bounding volume of the pfm's internal data."); 00030 00031 add_option 00032 ("z", "", 0, 00033 "Treats (0,0,0) in the pfm file as a special don't-touch value.", 00034 &PfmBba::dispatch_none, &_got_zero_special); 00035 00036 add_option 00037 ("r", "index", 0, 00038 "Selects a reorder index.", 00039 &PfmBba::dispatch_int, NULL, &_reorder_index); 00040 00041 add_option 00042 ("o", "filename", 50, 00043 "Specify the filename to which the resulting bba file will be written.", 00044 &PfmBba::dispatch_filename, &_got_output_filename, &_output_filename); 00045 00046 _reorder_index = 0; 00047 } 00048 00049 00050 //////////////////////////////////////////////////////////////////// 00051 // Function: PfmBba::run 00052 // Access: Public 00053 // Description: 00054 //////////////////////////////////////////////////////////////////// 00055 void PfmBba:: 00056 run() { 00057 Filenames::const_iterator fi; 00058 for (fi = _input_filenames.begin(); fi != _input_filenames.end(); ++fi) { 00059 PfmFile file; 00060 if (!file.read(*fi)) { 00061 nout << "Cannot read " << *fi << "\n"; 00062 exit(1); 00063 } 00064 if (!process_pfm(*fi, file)) { 00065 exit(1); 00066 } 00067 } 00068 } 00069 00070 //////////////////////////////////////////////////////////////////// 00071 // Function: PfmBba::process_pfm 00072 // Access: Public 00073 // Description: Handles a single pfm file. 00074 //////////////////////////////////////////////////////////////////// 00075 bool PfmBba:: 00076 process_pfm(const Filename &input_filename, PfmFile &file) { 00077 file.set_zero_special(_got_zero_special); 00078 00079 Filename bba_filename; 00080 if (_got_output_filename) { 00081 bba_filename = _output_filename; 00082 } else { 00083 bba_filename = input_filename; 00084 bba_filename.set_extension("bba"); 00085 } 00086 00087 if (!bba_filename.empty()) { 00088 bba_filename.set_text(); 00089 PT(BoundingHexahedron) bounds = file.compute_planar_bounds(pfm_bba_dist[0], pfm_bba_dist[1]); 00090 nassertr(bounds != (BoundingHexahedron *)NULL, false); 00091 00092 pofstream out; 00093 if (!bba_filename.open_write(out)) { 00094 pfm_cat.error() 00095 << "Unable to open " << bba_filename << "\n"; 00096 return false; 00097 } 00098 00099 // This is the order expected by our existing bba system. 00100 static const int num_reorder_points = 4; 00101 static const int reorder_points[num_reorder_points][8] = { 00102 { 0, 1, 2, 3, 4, 5, 6, 7 }, // unfiltered 00103 { 7, 5, 1, 3, 6, 4, 0, 2 }, // front, floor 00104 { 4, 6, 2, 0, 5, 7, 3, 1 }, // left 00105 { 7, 5, 1, 3, 2, 0, 4, 6 }, // right 00106 }; 00107 int ri = max(_reorder_index, 0); 00108 ri = min(ri, num_reorder_points - 1); 00109 00110 for (int i = 0; i < bounds->get_num_points(); ++i) { 00111 LPoint3 p = bounds->get_point(reorder_points[ri][i]); 00112 out << p[0] << "," << p[1] << "," << p[2] << "\n"; 00113 } 00114 } 00115 00116 return true; 00117 } 00118 00119 //////////////////////////////////////////////////////////////////// 00120 // Function: PfmBba::handle_args 00121 // Access: Protected, Virtual 00122 // Description: Does something with the additional arguments on the 00123 // command line (after all the -options have been 00124 // parsed). Returns true if the arguments are good, 00125 // false otherwise. 00126 //////////////////////////////////////////////////////////////////// 00127 bool PfmBba:: 00128 handle_args(ProgramBase::Args &args) { 00129 if (args.empty()) { 00130 nout << "You must specify the pfm file(s) to read on the command line.\n"; 00131 return false; 00132 } 00133 00134 if (args.size() > 1 && _got_output_filename) { 00135 nout << "Cannot use -o when multiple pfm files are specified.\n"; 00136 return false; 00137 } 00138 00139 Args::const_iterator ai; 00140 for (ai = args.begin(); ai != args.end(); ++ai) { 00141 _input_filenames.push_back(Filename::from_os_specific(*ai)); 00142 } 00143 00144 return true; 00145 } 00146 00147 00148 int main(int argc, char *argv[]) { 00149 // A call to pystub() to force libpystub.so to be linked in. 00150 pystub(); 00151 00152 PfmBba prog; 00153 prog.parse_command_line(argc, argv); 00154 prog.run(); 00155 return 0; 00156 }