Panda3D
pfmBba.cxx
1 // Filename: pfmBba.cxx
2 // Created by: drose (02Mar11)
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 "pfmBba.h"
16 #include "config_pfm.h"
17 #include "pfmFile.h"
18 #include "pystub.h"
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: PfmBba::Constructor
22 // Access: Public
23 // Description:
24 ////////////////////////////////////////////////////////////////////
25 PfmBba::
26 PfmBba() {
27  set_program_brief("generate .bba files from .pfm files");
28  set_program_description
29  ("pfm-bba generates a .bba file from a .pfm file that lists the "
30  "planar bounding volume of the pfm's internal data.");
31 
32  add_option
33  ("z", "", 0,
34  "Treats (0,0,0) in the pfm file as a special don't-touch value.",
35  &PfmBba::dispatch_none, &_got_zero_special);
36 
37  add_option
38  ("o", "filename", 50,
39  "Specify the filename to which the resulting bba file will be written.",
40  &PfmBba::dispatch_filename, &_got_output_filename, &_output_filename);
41 }
42 
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: PfmBba::run
46 // Access: Public
47 // Description:
48 ////////////////////////////////////////////////////////////////////
49 void PfmBba::
50 run() {
51  Filenames::const_iterator fi;
52  for (fi = _input_filenames.begin(); fi != _input_filenames.end(); ++fi) {
53  PfmFile file;
54  if (!file.read(*fi)) {
55  nout << "Cannot read " << *fi << "\n";
56  exit(1);
57  }
58  if (!process_pfm(*fi, file)) {
59  exit(1);
60  }
61  }
62 }
63 
64 ////////////////////////////////////////////////////////////////////
65 // Function: PfmBba::process_pfm
66 // Access: Public
67 // Description: Handles a single pfm file.
68 ////////////////////////////////////////////////////////////////////
69 bool PfmBba::
70 process_pfm(const Filename &input_filename, PfmFile &file) {
71  file.set_zero_special(_got_zero_special);
72 
73  Filename bba_filename;
74  if (_got_output_filename) {
75  bba_filename = _output_filename;
76  } else {
77  bba_filename = input_filename;
78  bba_filename.set_extension("bba");
79  }
80 
81  if (!bba_filename.empty()) {
82  bba_filename.set_text();
83  PT(BoundingHexahedron) bounds = file.compute_planar_bounds(LPoint2f(0.5, 0.5), pfm_bba_dist[0], pfm_bba_dist[1], false);
84  nassertr(bounds != (BoundingHexahedron *)NULL, false);
85 
86  pofstream out;
87  if (!bba_filename.open_write(out)) {
88  cerr << "Unable to open " << bba_filename << "\n";
89  return false;
90  }
91 
92  LPoint3 points[8];
93  for (int i = 0; i < 8; ++i) {
94  points[i] = bounds->get_point(i);
95  }
96 
97  // Experiment with expanding the back wall backwards.
98  /*
99  LPlane plane(points[0], points[1], points[2]);
100  LVector3 normal = plane.get_normal();
101 
102  static const PN_stdfloat scale = 20.0f;
103  normal *= scale;
104  points[0] += normal;
105  points[1] += normal;
106  points[2] += normal;
107  points[3] += normal;
108  */
109 
110  for (int i = 0; i < 8; ++i) {
111  const LPoint3 &p = points[i];
112  out << p[0] << "," << p[1] << "," << p[2] << "\n";
113  }
114  }
115 
116  return true;
117 }
118 
119 ////////////////////////////////////////////////////////////////////
120 // Function: PfmBba::handle_args
121 // Access: Protected, Virtual
122 // Description: Does something with the additional arguments on the
123 // command line (after all the -options have been
124 // parsed). Returns true if the arguments are good,
125 // false otherwise.
126 ////////////////////////////////////////////////////////////////////
127 bool PfmBba::
128 handle_args(ProgramBase::Args &args) {
129  if (args.empty()) {
130  nout << "You must specify the pfm file(s) to read on the command line.\n";
131  return false;
132  }
133 
134  if (args.size() > 1 && _got_output_filename) {
135  nout << "Cannot use -o when multiple pfm files are specified.\n";
136  return false;
137  }
138 
139  Args::const_iterator ai;
140  for (ai = args.begin(); ai != args.end(); ++ai) {
141  _input_filenames.push_back(Filename::from_os_specific(*ai));
142  }
143 
144  return true;
145 }
146 
147 
148 int main(int argc, char *argv[]) {
149  // A call to pystub() to force libpystub.so to be linked in.
150  pystub();
151 
152  PfmBba prog;
153  prog.parse_command_line(argc, argv);
154  prog.run();
155  return 0;
156 }
void set_extension(const string &s)
Replaces the file extension.
Definition: filename.cxx:837
bool open_write(ofstream &stream, bool truncate=true) const
Opens the indicated ifstream for writing the file, if possible.
Definition: filename.cxx:2045
Generates a bounding-box description of a pfm file.
Definition: pfmBba.h:31
virtual void parse_command_line(int argc, char **argv)
Dispatches on each of the options on the command line, and passes the remaining parameters to handle_...
void set_text()
Indicates that the filename represents a text file.
Definition: filename.I:507
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
bool read(const Filename &fullpath)
Reads the PFM data from the indicated file, returning true on success, false on failure.
Definition: pfmFile.cxx:130
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
Defines a pfm file, a 2-d table of floating-point numbers, either 3-component or 1-component, or with a special extension, 2- or 4-component.
Definition: pfmFile.h:34
void set_zero_special(bool zero_special)
Sets the zero_special flag.
Definition: pfmFile.I:459
This is a two-component point in space.
Definition: lpoint2.h:92
This defines a bounding convex hexahedron.
bool process_pfm(const Filename &input_filename, PfmFile &file)
Handles a single pfm file.
Definition: pfmBba.cxx:70
static Filename from_os_specific(const string &os_specific, Type type=T_general)
This named constructor returns a Panda-style filename (that is, using forward slashes, and no drive letter) based on the supplied filename string that describes a filename in the local system conventions (for instance, on Windows, it may use backslashes or begin with a drive letter and a colon).
Definition: filename.cxx:332