Panda3D
bamToEgg.cxx
1 // Filename: bamToEgg.cxx
2 // Created by: drose (25Jun01)
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 "bamToEgg.h"
16 #include "save_egg_file.h"
17 #include "pystub.h"
18 #include "string_utils.h"
19 #include "bamFile.h"
20 #include "bamCacheRecord.h"
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: BamToEgg::Constructor
24 // Access: Public
25 // Description:
26 ////////////////////////////////////////////////////////////////////
27 BamToEgg::
28 BamToEgg() :
29  SomethingToEgg("bam", ".bam")
30 {
31  set_program_brief("convert a native Panda .bam file to an .egg file");
32  set_program_description
33  ("This program converts native Panda bam files to egg. The conversion "
34  "is somewhat incomplete; running egg2bam followed by bam2egg should not "
35  "be expected to yield the same egg file you started with.");
36 
37  redescribe_option
38  ("cs",
39  "Specify the coordinate system of the input " + _format_name +
40  " file. By default, this is taken from the Config.prc file, which "
41  "is currently " + format_string(get_default_coordinate_system()) + ".");
42 
43  _coordinate_system = get_default_coordinate_system();
44 }
45 
46 ////////////////////////////////////////////////////////////////////
47 // Function: BamToEgg::run
48 // Access: Public
49 // Description:
50 ////////////////////////////////////////////////////////////////////
51 void BamToEgg::
52 run() {
53  BamFile bam_file;
54 
55  if (!bam_file.open_read(_input_filename)) {
56  nout << "Unable to read " << _input_filename << "\n";
57  exit(1);
58  }
59 
60  nout << _input_filename << " : Bam version "
61  << bam_file.get_file_major_ver() << "."
62  << bam_file.get_file_minor_ver() << "\n";
63 
64  typedef pvector<TypedWritable *> Objects;
65  Objects objects;
66  TypedWritable *object = bam_file.read_object();
67 
68  if (object != (TypedWritable *)NULL &&
69  object->is_exact_type(BamCacheRecord::get_class_type())) {
70  // Here's a special case: if the first object in the file is a
71  // BamCacheRecord, it's really a cache data file and not a true
72  // bam file; but skip over the cache data record and let the user
73  // treat it like an ordinary bam file.
74  object = bam_file.read_object();
75  }
76 
77  while (object != (TypedWritable *)NULL || !bam_file.is_eof()) {
78  if (object != (TypedWritable *)NULL) {
79  ReferenceCount *ref_ptr = object->as_reference_count();
80  if (ref_ptr != NULL) {
81  ref_ptr->ref();
82  }
83  objects.push_back(object);
84  }
85  object = bam_file.read_object();
86  }
87  bam_file.resolve();
88  bam_file.close();
89 
90  _data->set_coordinate_system(_coordinate_system);
91 
92  if (objects.size() == 1 &&
93  objects[0]->is_of_type(PandaNode::get_class_type())) {
94  PandaNode *node = DCAST(PandaNode, objects[0]);
95  save_egg_data(_data, node);
96 
97  } else {
98  nout << "File does not contain a scene graph.\n";
99  exit(1);
100  }
101 
102  write_egg_file();
103 }
104 
105 int main(int argc, char *argv[]) {
106  // A call to pystub() to force libpystub.so to be linked in.
107  pystub();
108 
109  BamToEgg prog;
110  prog.parse_command_line(argc, argv);
111  prog.run();
112  return 0;
113 }
The principle public interface to reading and writing Bam disk files.
Definition: bamFile.h:45
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
bool open_read(const Filename &bam_filename, bool report_errors=true)
Attempts to open the indicated filename for reading.
Definition: bamFile.cxx:56
bool is_exact_type(TypeHandle handle) const
Returns true if the current object is the indicated type exactly.
Definition: typedObject.I:74
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:128
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:301
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:37
void close()
Closes the input or output stream.
Definition: bamFile.cxx:277
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
bool is_eof() const
Returns true if the reader has reached end-of-file, false otherwise.
Definition: bamFile.cxx:112
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:28
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:317
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:96