Panda3D
Loading...
Searching...
No Matches
pfmBba.cxx
Go to the documentation of this file.
1/**
2 * PANDA 3D SOFTWARE
3 * Copyright (c) Carnegie Mellon University. All rights reserved.
4 *
5 * All use of this software is subject to the terms of the revised BSD
6 * license. You should have received a copy of this license along
7 * with this source code in a file named "LICENSE."
8 *
9 * @file pfmBba.cxx
10 * @author drose
11 * @date 2011-03-02
12 */
13
14#include "pfmBba.h"
15#include "config_pfmprogs.h"
16#include "pfmFile.h"
17
18/**
19 *
20 */
21PfmBba::
22PfmBba() {
23 set_program_brief("generate .bba files from .pfm files");
24 set_program_description
25 ("pfm-bba generates a .bba file from a .pfm file that lists the "
26 "planar bounding volume of the pfm's internal data.");
27
28 add_option
29 ("z", "", 0,
30 "Treats (0,0,0) in the pfm file as a special don't-touch value.",
31 &PfmBba::dispatch_none, &_got_zero_special);
32
33 add_option
34 ("o", "filename", 50,
35 "Specify the filename to which the resulting bba file will be written.",
36 &PfmBba::dispatch_filename, &_got_output_filename, &_output_filename);
37}
38
39
40/**
41 *
42 */
43void PfmBba::
44run() {
45 Filenames::const_iterator fi;
46 for (fi = _input_filenames.begin(); fi != _input_filenames.end(); ++fi) {
47 PfmFile file;
48 if (!file.read(*fi)) {
49 nout << "Cannot read " << *fi << "\n";
50 exit(1);
51 }
52 if (!process_pfm(*fi, file)) {
53 exit(1);
54 }
55 }
56}
57
58/**
59 * Handles a single pfm file.
60 */
62process_pfm(const Filename &input_filename, PfmFile &file) {
63 file.set_zero_special(_got_zero_special);
64
65 Filename bba_filename;
66 if (_got_output_filename) {
67 bba_filename = _output_filename;
68 } else {
69 bba_filename = input_filename;
70 bba_filename.set_extension("bba");
71 }
72
73 if (!bba_filename.empty()) {
74 bba_filename.set_text();
75 PT(BoundingHexahedron) bounds = file.compute_planar_bounds(LPoint2f(0.5, 0.5), pfm_bba_dist[0], pfm_bba_dist[1], false);
76 nassertr(bounds != nullptr, false);
77
78 pofstream out;
79 if (!bba_filename.open_write(out)) {
80 std::cerr << "Unable to open " << bba_filename << "\n";
81 return false;
82 }
83
84 LPoint3 points[8];
85 for (int i = 0; i < 8; ++i) {
86 points[i] = bounds->get_point(i);
87 }
88
89 // Experiment with expanding the back wall backwards.
90 /*
91 LPlane plane(points[0], points[1], points[2]);
92 LVector3 normal = plane.get_normal();
93
94 static const PN_stdfloat scale = 20.0f;
95 normal *= scale;
96 points[0] += normal;
97 points[1] += normal;
98 points[2] += normal;
99 points[3] += normal;
100 */
101
102 for (int i = 0; i < 8; ++i) {
103 const LPoint3 &p = points[i];
104 out << p[0] << "," << p[1] << "," << p[2] << "\n";
105 }
106 }
107
108 return true;
109}
110
111/**
112 * Does something with the additional arguments on the command line (after all
113 * the -options have been parsed). Returns true if the arguments are good,
114 * false otherwise.
115 */
116bool PfmBba::
117handle_args(ProgramBase::Args &args) {
118 if (args.empty()) {
119 nout << "You must specify the pfm file(s) to read on the command line.\n";
120 return false;
121 }
122
123 if (args.size() > 1 && _got_output_filename) {
124 nout << "Cannot use -o when multiple pfm files are specified.\n";
125 return false;
126 }
127
128 Args::const_iterator ai;
129 for (ai = args.begin(); ai != args.end(); ++ai) {
130 _input_filenames.push_back(Filename::from_os_specific(*ai));
131 }
132
133 return true;
134}
135
136
137int main(int argc, char *argv[]) {
138 PfmBba prog;
139 prog.parse_command_line(argc, argv);
140 prog.run();
141 return 0;
142}
This defines a bounding convex hexahedron.
The name of a file, such as a texture file or an Egg file.
Definition filename.h:44
void set_text()
Indicates that the filename represents a text file.
Definition filename.I:424
void set_extension(const std::string &s)
Replaces the file extension.
Definition filename.cxx:804
bool open_write(std::ofstream &stream, bool truncate=true) const
Opens the indicated ifstream for writing the file, if possible.
Generates a bounding-box description of a pfm file.
Definition pfmBba.h:29
bool process_pfm(const Filename &input_filename, PfmFile &file)
Handles a single pfm file.
Definition pfmBba.cxx:62
Defines a pfm file, a 2-d table of floating-point numbers, either 3-component or 1-component,...
Definition pfmFile.h:31
bool read(const Filename &fullpath)
Reads the PFM data from the indicated file, returning true on success, false on failure.
Definition pfmFile.cxx:121
void set_zero_special(bool zero_special)
Sets the zero_special flag.
Definition pfmFile.I:371
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_...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.