Panda3D
 All Classes Functions Variables Enumerations
eggToDAE.cxx
1 // Filename: eggToDAE.cxx
2 // Created by: pro-rsoft (04Oct08)
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 "eggToDAE.h"
16 #include "dcast.h"
17 #include "pystub.h"
18 #include "pandaVersion.h"
19 
20 #include "FCDocument/FCDocument.h"
21 #include "FCDocument/FCDAsset.h"
22 #include "FCDocument/FCDTransform.h"
23 
24 // Useful conversion stuff
25 #define TO_VEC3(v) (LVecBase3d(v[0], v[1], v[2]))
26 #define TO_VEC4(v) (LVecBase4d(v[0], v[1], v[2], v[3]))
27 #define TO_COLOR(v) (LColor(v[0], v[1], v[2], v[3]))
28 #define FROM_VEC3(v) (FMVector3(v[0], v[1], v[2]))
29 #define FROM_VEC4(v) (FMVector4(v[0], v[1], v[2], v[3]))
30 #define FROM_MAT4(v) (FMMatrix44(v.get_data()))
31 #define FROM_FSTRING(fs) (fs.c_str())
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: EggToDAE::Constructor
35 // Access: Public
36 // Description:
37 ////////////////////////////////////////////////////////////////////
38 EggToDAE::
39 EggToDAE() :
40  EggToSomething("COLLADA", ".dae", true, false)
41 {
42  set_binary_output(false);
43  set_program_brief("convert .egg files into COLLADA asset files");
44  set_program_description
45  ("This program converts files from the egg format to the COLLADA "
46  ".dae (Digital Asset Exchange) format.");
47 
48  _document = NULL;
49 }
50 
51 ////////////////////////////////////////////////////////////////////
52 // Function: EggToDAE::run
53 // Access: Public
54 // Description:
55 ////////////////////////////////////////////////////////////////////
56 void EggToDAE::
57 run() {
58  nassertv(has_output_filename());
59  nassertv(_data != NULL);
60 
61  FCollada::Initialize();
62  _document = FCollada::NewTopDocument();
63 
64  // Add the contributor part to the asset
65  FCDAssetContributor* contributor = _document->GetAsset()->AddContributor();
66  const char* user_name = getenv("USER");
67  if (user_name == NULL) user_name = getenv("USERNAME");
68  if (user_name != NULL) contributor->SetAuthor(TO_FSTRING(user_name));
69  //contributor->SetSourceData();
70  char authoring_tool[1024];
71  snprintf(authoring_tool, 1024, "Panda3D %s eggToDAE converter | FCollada v%d.%02d", PANDA_VERSION_STR, FCOLLADA_VERSION >> 16, FCOLLADA_VERSION & 0xFFFF);
72  authoring_tool[1023] = 0;
73  contributor->SetAuthoringTool(TO_FSTRING(authoring_tool));
74 
75  // Set coordinate system
76  switch (_data->get_coordinate_system()) {
77  case CS_zup_right:
78  _document->GetAsset()->SetUpAxis(FMVector3::ZAxis);
79  break;
80  case CS_yup_right:
81  _document->GetAsset()->SetUpAxis(FMVector3::YAxis);
82  break;
83  }
84 
85  // Now actually start processing the data.
86  FCDSceneNode* visual_scene = _document->AddVisualScene();
87  for (EggGroupNode::iterator it = _data->begin(); it != _data->end(); ++it) {
88  if ((*it)->is_of_type(EggGroup::get_class_type())) {
89  process_node(visual_scene, DCAST(EggGroup, *it));
90  }
91  }
92 
93  // We're done here.
94  FCollada::SaveDocument(_document, get_output_filename().to_os_specific().c_str());
95  SAFE_DELETE(_document);
96  FCollada::Release();
97 
98  //if (!out) {
99  // nout << "An error occurred while writing.\n";
100  // exit(1);
101  //}
102 }
103 
104 void EggToDAE::process_node(FCDSceneNode* parent, const PT(EggGroup) node) {
105  assert(node != NULL);
106  FCDSceneNode* scene_node = parent->AddChildNode();
107  // Set the parameters
108  scene_node->SetDaeId(node->get_name().c_str());
109  scene_node->SetJointFlag(node->is_joint());
110  // Apply the transforms
111  apply_transform(scene_node, node);
112  // Recursively process sub-nodes
113  for (EggGroupNode::iterator it = node->begin(); it != node->end(); ++it) {
114  if ((*it)->is_of_type(EggGroup::get_class_type())) {
115  process_node(scene_node, DCAST(EggGroup, *it));
116  }
117  }
118 }
119 
120 void EggToDAE::apply_transform(FCDSceneNode* to, const PT(EggGroup) from) {
121  assert(to != NULL);
122  assert(from != NULL);
123  for (int co = 0; co < from->get_num_components(); ++co) {
124  switch (from->get_component_type(co)) {
125  case EggTransform::CT_translate2d:
126  cerr << "Warning: ignoring non-supported 2d translation\n";
127  break;
128  case EggTransform::CT_rotate2d:
129  cerr << "Warning: ignoring non-supported 2d rotation\n";
130  break;
131  case EggTransform::CT_scale2d:
132  cerr << "Warning: ignoring non-supported 2d scaling\n";
133  break;
134  case EggTransform::CT_matrix3:
135  cerr << "Warning: ignoring non-supported 2d matrix\n";
136  break;
137  case EggTransform::CT_translate3d: {
138  FCDTTranslation* new_transform = (FCDTTranslation*) to->AddTransform(FCDTransform::TRANSLATION);
139  new_transform->SetTranslation(FROM_VEC3(from->get_component_vec3(co)));
140  break; }
141  case EggTransform::CT_rotate3d: {
142  FCDTRotation* new_transform = (FCDTRotation*) to->AddTransform(FCDTransform::ROTATION);
143  new_transform->SetRotation(FROM_VEC3(from->get_component_vec3(co)), from->get_component_number(co));
144  break; }
145  case EggTransform::CT_scale3d: {
146  FCDTScale* new_transform = (FCDTScale*) to->AddTransform(FCDTransform::SCALE);
147  new_transform->SetScale(FROM_VEC3(from->get_component_vec3(co)));
148  break; }
149  case EggTransform::CT_matrix4: {
150  FCDTMatrix* new_transform = (FCDTMatrix*) to->AddTransform(FCDTransform::MATRIX);
151  new_transform->SetTransform(FROM_MAT4(from->get_component_mat4(co)));
152  break; }
153  case EggTransform::CT_rotx: {
154  FCDTRotation* new_transform = (FCDTRotation*) to->AddTransform(FCDTransform::ROTATION);
155  new_transform->SetRotation(FMVector3::XAxis, from->get_component_number(co));
156  break; }
157  case EggTransform::CT_roty: {
158  FCDTRotation* new_transform = (FCDTRotation*) to->AddTransform(FCDTransform::ROTATION);
159  new_transform->SetRotation(FMVector3::YAxis, from->get_component_number(co));
160  break; }
161  case EggTransform::CT_rotz: {
162  FCDTRotation* new_transform = (FCDTRotation*) to->AddTransform(FCDTransform::ROTATION);
163  new_transform->SetRotation(FMVector3::ZAxis, from->get_component_number(co));
164  break; }
165  case EggTransform::CT_uniform_scale: {
166  FCDTScale* new_transform = (FCDTScale*) to->AddTransform(FCDTransform::SCALE);
167  new_transform->SetScale(from->get_component_number(co), from->get_component_number(co), from->get_component_number(co));
168  break; }
169  default:
170  cerr << "Warning: ignoring invalid transform\n";
171  }
172  }
173 }
174 
175 int main(int argc, char *argv[]) {
176  // A call to pystub() to force libpystub.so to be linked in.
177  pystub();
178 
179  EggToDAE prog;
180  prog.parse_command_line(argc, argv);
181  prog.run();
182  return 0;
183 }
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_...
virtual bool is_joint() const
Returns true if this particular node represents a &lt;Joint&gt; entry or not.
Definition: eggGroup.cxx:508
Filename get_output_filename() const
If has_output_filename() returns true, this is the filename that the user specified.
bool has_output_filename() const
Returns true if the user specified an output filename, false otherwise (e.g.
The main glue of the egg hierarchy, this corresponds to the &lt;Group&gt;, &lt;Instance&gt;, and &lt;Joint&gt; type nod...
Definition: eggGroup.h:36
A program to read an egg file and write a DAE file.
Definition: eggToDAE.h:31
const LMatrix4d & get_component_mat4(int n) const
Returns the 4x4 matrix associated with the nth component.
Definition: eggTransform.I:358
const LVecBase3d & get_component_vec3(int n) const
Returns the 3-component vector associated with the nth component.
Definition: eggTransform.I:330
double get_component_number(int n) const
Returns the solitary number associated with the nth component.
Definition: eggTransform.I:299
int get_num_components() const
Returns the number of components that make up the transform.
Definition: eggTransform.I:274
This is the general base class for a file-converter program that reads some model file format and gen...
ComponentType get_component_type(int n) const
Returns the type of the nth component.
Definition: eggTransform.I:284