Panda3D
bamToEgg.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 bamToEgg.cxx
10  * @author drose
11  * @date 2001-06-25
12  */
13 
14 #include "bamToEgg.h"
15 #include "save_egg_file.h"
16 #include "string_utils.h"
17 #include "bamFile.h"
18 #include "bamCacheRecord.h"
19 
20 /**
21  *
22  */
23 BamToEgg::
24 BamToEgg() :
25  SomethingToEgg("bam", ".bam")
26 {
27  set_program_brief("convert a native Panda .bam file to an .egg file");
28  set_program_description
29  ("This program converts native Panda bam files to egg. The conversion "
30  "is somewhat incomplete; running egg2bam followed by bam2egg should not "
31  "be expected to yield the same egg file you started with.");
32 
33  redescribe_option
34  ("cs",
35  "Specify the coordinate system of the input " + _format_name +
36  " file. By default, this is taken from the Config.prc file, which "
37  "is currently " + format_string(get_default_coordinate_system()) + ".");
38 
39  _coordinate_system = get_default_coordinate_system();
40 }
41 
42 /**
43  *
44  */
45 void BamToEgg::
46 run() {
47  BamFile bam_file;
48 
49  if (!bam_file.open_read(_input_filename)) {
50  nout << "Unable to read " << _input_filename << "\n";
51  exit(1);
52  }
53 
54  nout << _input_filename << " : Bam version "
55  << bam_file.get_file_major_ver() << "."
56  << bam_file.get_file_minor_ver() << "\n";
57 
58  typedef pvector<TypedWritable *> Objects;
59  Objects objects;
60  TypedWritable *object = bam_file.read_object();
61 
62  if (object != nullptr &&
63  object->is_exact_type(BamCacheRecord::get_class_type())) {
64  // Here's a special case: if the first object in the file is a
65  // BamCacheRecord, it's really a cache data file and not a true bam file;
66  // but skip over the cache data record and let the user treat it like an
67  // ordinary bam file.
68  object = bam_file.read_object();
69  }
70 
71  while (object != nullptr || !bam_file.is_eof()) {
72  if (object != nullptr) {
73  ReferenceCount *ref_ptr = object->as_reference_count();
74  if (ref_ptr != nullptr) {
75  ref_ptr->ref();
76  }
77  objects.push_back(object);
78  }
79  object = bam_file.read_object();
80  }
81  bam_file.resolve();
82  bam_file.close();
83 
84  _data->set_coordinate_system(_coordinate_system);
85 
86  if (objects.size() == 1 &&
87  objects[0]->is_of_type(PandaNode::get_class_type())) {
88  PandaNode *node = DCAST(PandaNode, objects[0]);
89  save_egg_data(_data, node);
90 
91  } else {
92  nout << "File does not contain a scene graph.\n";
93  exit(1);
94  }
95 
97 }
98 
99 int main(int argc, char *argv[]) {
100  BamToEgg prog;
101  prog.parse_command_line(argc, argv);
102  prog.run();
103  return 0;
104 }
The principle public interface to reading and writing Bam disk files.
Definition: bamFile.h:41
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
bool open_read(const Filename &bam_filename, bool report_errors=true)
Attempts to open the indicated filename for reading.
Definition: bamFile.cxx:51
bool is_exact_type(TypeHandle handle) const
Returns true if the current object is the indicated type exactly.
Definition: typedObject.I:38
bool resolve()
This must be called after one or more objects have been read via calls to read_object() in order to r...
Definition: bamFile.cxx:110
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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_...
int get_file_major_ver()
Returns the major version number of the file currently being read, or the system current major versio...
Definition: bamFile.cxx:264
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void close()
Closes the input or output stream.
Definition: bamFile.cxx:243
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
bool save_egg_data(EggData *data, PandaNode *node)
Another convenience function; works like save_egg_file() but populates an EggData instead of writing ...
bool is_eof() const
Returns true if the reader has reached end-of-file, false otherwise.
Definition: bamFile.cxx:98
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void write_egg_file()
Writes out the egg file as the normal result of the program.
Definition: eggWriter.cxx:177
void ref() const
Explicitly increments the reference count.
A base class for all things that want to be reference-counted.
This program reads a bam file, for instance as written out from a real-time interaction session,...
Definition: bamToEgg.h:25
This is the general base class for a file-converter program that reads some model file format and gen...
int get_file_minor_ver()
Returns the minor version number of the file currently being read, or the system current minor versio...
Definition: bamFile.cxx:277
TypedWritable * read_object()
Reads and returns the next object from the Bam file, or NULL if the end of the file has been reached,...
Definition: bamFile.cxx:85