Browse Source

*** empty log message ***

David Rose 24 years ago
parent
commit
e5a4d3ec81

+ 5 - 1
pandatool/src/xfile/Sources.pp

@@ -14,7 +14,11 @@
     d3dxof.lib
     d3dxof.lib
 
 
   #define SOURCES \
   #define SOURCES \
-    xFileMaker.cxx xFileMaker.h
+    xFileFace.cxx xFileFace.h \
+    xFileMaker.cxx xFileMaker.h \
+    xFileMesh.cxx xFileMesh.h \
+    xFileNormal.cxx xFileNormal.h \
+    xFileVertex.cxx xFileVertex.h
 
 
   #define SOURCES \
   #define SOURCES \
     $[SOURCES] \
     $[SOURCES] \

+ 67 - 67
pandatool/src/xfile/eggToX.cxx

@@ -1,67 +1,67 @@
-// Filename: eggToX.cxx
-// Created by:  drose (19Jun01)
-//
-////////////////////////////////////////////////////////////////////
-//
-// PANDA 3D SOFTWARE
-// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
-//
-// All use of this software is subject to the terms of the Panda 3d
-// Software license.  You should have received a copy of this license
-// along with this source code; you will also find a current copy of
-// the license at http://www.panda3d.org/license.txt .
-//
-// To contact the maintainers of this program write to
-// [email protected] .
-//
-////////////////////////////////////////////////////////////////////
-
-#include "eggToX.h"
-
-////////////////////////////////////////////////////////////////////
-//     Function: EggToX::Constructor
-//       Access: Public
-//  Description:
-////////////////////////////////////////////////////////////////////
-EggToX::
-EggToX() : EggToSomething("DirectX", "x") {
-  set_program_description
-    ("This program reads an Egg file and outputs an equivalent, "
-     "or nearly equivalent, DirectX-style .x file.  Only simple "
-     "hierarchy and polygon meshes are supported; advanced features "
-     "like LOD's, decals, and characters cannot be supported.");
-
-  // X files are always y-up-left.
-  remove_option("cs");
-  _got_coordinate_system = true;
-  _coordinate_system = CS_yup_left;
-}
-
-
-////////////////////////////////////////////////////////////////////
-//     Function: EggToX::run
-//       Access: Public
-//  Description:
-////////////////////////////////////////////////////////////////////
-void EggToX::
-run() {
-  if (!_x.open(get_output_filename())) {
-    nout << "Unable to open " << get_output_filename() << " for output.\n";
-    exit(1);
-  }
-
-  if (!_x.add_tree(_data)) {
-    nout << "Unable to define egg structure.\n";
-    exit(1);
-  }
-
-  _x.close();
-}
-
-
-int main(int argc, char *argv[]) {
-  EggToX prog;
-  prog.parse_command_line(argc, argv);
-  prog.run();
-  return 0;
-}
+// Filename: eggToX.cxx
+// Created by:  drose (19Jun01)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "eggToX.h"
+
+////////////////////////////////////////////////////////////////////
+//     Function: EggToX::Constructor
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+EggToX::
+EggToX() : EggToSomething("DirectX", "x") {
+  set_program_description
+    ("This program reads an Egg file and outputs an equivalent, "
+     "or nearly equivalent, DirectX-style .x file.  Only simple "
+     "hierarchy and polygon meshes are supported; advanced features "
+     "like LOD's, decals, and characters cannot be supported.");
+
+  // X files are always y-up-left.
+  remove_option("cs");
+  _got_coordinate_system = true;
+  _coordinate_system = CS_yup_left;
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: EggToX::run
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+void EggToX::
+run() {
+  if (!_x.open(get_output_filename())) {
+    nout << "Unable to open " << get_output_filename() << " for output.\n";
+    exit(1);
+  }
+
+  if (!_x.add_tree(_data)) {
+    nout << "Unable to define egg structure.\n";
+    exit(1);
+  }
+
+  _x.close();
+}
+
+
+int main(int argc, char *argv[]) {
+  EggToX prog;
+  prog.parse_command_line(argc, argv);
+  prog.run();
+  return 0;
+}

+ 51 - 51
pandatool/src/xfile/eggToX.h

@@ -1,51 +1,51 @@
-// Filename: eggToX.h
-// Created by:  drose (19Jun01)
-//
-////////////////////////////////////////////////////////////////////
-//
-// PANDA 3D SOFTWARE
-// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
-//
-// All use of this software is subject to the terms of the Panda 3d
-// Software license.  You should have received a copy of this license
-// along with this source code; you will also find a current copy of
-// the license at http://www.panda3d.org/license.txt .
-//
-// To contact the maintainers of this program write to
-// [email protected] .
-//
-////////////////////////////////////////////////////////////////////
-
-#ifndef EGGTOX_H
-#define EGGTOX_H
-
-#include "pandatoolbase.h"
-#include "eggToSomething.h"
-#include "xFileMaker.h"
-
-#include <programBase.h>
-#include <withOutputFile.h>
-#include <filename.h>
-
-class Node;
-
-////////////////////////////////////////////////////////////////////
-//       Class : EggToX
-// Description : A program to read in a egg file and write an
-//               equivalent, or nearly equivalent, DirectX-style "x"
-//               file.
-////////////////////////////////////////////////////////////////////
-class EggToX : public EggToSomething {
-public:
-  EggToX();
-
-  void run();
-
-private:
-  void convert_scene_graph(Node *root);
-
-  Filename _input_filename;
-  XFileMaker _x;
-};
-
-#endif
+// Filename: eggToX.h
+// Created by:  drose (19Jun01)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef EGGTOX_H
+#define EGGTOX_H
+
+#include "pandatoolbase.h"
+#include "eggToSomething.h"
+#include "xFileMaker.h"
+
+#include <programBase.h>
+#include <withOutputFile.h>
+#include <filename.h>
+
+class Node;
+
+////////////////////////////////////////////////////////////////////
+//       Class : EggToX
+// Description : A program to read in a egg file and write an
+//               equivalent, or nearly equivalent, DirectX-style "x"
+//               file.
+////////////////////////////////////////////////////////////////////
+class EggToX : public EggToSomething {
+public:
+  EggToX();
+
+  void run();
+
+private:
+  void convert_scene_graph(Node *root);
+
+  Filename _input_filename;
+  XFileMaker _x;
+};
+
+#endif

+ 40 - 0
pandatool/src/xfile/xFileFace.cxx

@@ -0,0 +1,40 @@
+// Filename: xFileFace.cxx
+// Created by:  drose (19Jun01)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "xFileFace.h"
+#include "xFileMesh.h"
+#include "eggPolygon.h"
+
+////////////////////////////////////////////////////////////////////
+//     Function: XFileFace::Constructor
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+XFileFace::
+XFileFace(XFileMesh *mesh, EggPolygon *egg_poly) {
+  // Walk through the polygon's vertices in reverse order, to change
+  // from Egg's counter-clockwise convention to DX's clockwise.
+  EggPolygon::reverse_iterator vi;
+  for (vi = egg_poly->rbegin(); vi != egg_poly->rend(); ++vi) {
+    EggVertex *egg_vertex = (*vi);
+    Vertex v;
+    v._vertex_index = mesh->add_vertex(egg_vertex, egg_poly);
+    v._normal_index = mesh->add_normal(egg_vertex, egg_poly);
+    _vertices.push_back(v);
+  }
+}

+ 46 - 0
pandatool/src/xfile/xFileFace.h

@@ -0,0 +1,46 @@
+// Filename: xFileFace.h
+// Created by:  drose (19Jun01)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef XFILEFACE_H
+#define XFILEFACE_H
+
+#include "pandatoolbase.h"
+#include "pvector.h"
+
+class XFileMesh;
+class EggPolygon;
+
+////////////////////////////////////////////////////////////////////
+//       Class : XFileFace
+// Description : This represents a single face of an XFileMesh.
+////////////////////////////////////////////////////////////////////
+class XFileFace {
+public:
+  XFileFace(XFileMesh *mesh, EggPolygon *egg_poly);
+
+  class Vertex {
+  public:
+    int _vertex_index;
+    int _normal_index;
+  };
+  typedef pvector<Vertex> Vertices;
+  Vertices _vertices;
+};
+
+#endif
+

+ 65 - 90
pandatool/src/xfile/xFileMaker.cxx

@@ -17,6 +17,7 @@
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 
 
 #include "xFileMaker.h"
 #include "xFileMaker.h"
+#include "xFileMesh.h"
 #include "notify.h"
 #include "notify.h"
 #include "eggGroupNode.h"
 #include "eggGroupNode.h"
 #include "eggGroup.h"
 #include "eggGroup.h"
@@ -29,6 +30,7 @@
 #include "pvector.h"
 #include "pvector.h"
 #include "vector_int.h"
 #include "vector_int.h"
 #include "string_utils.h"
 #include "string_utils.h"
+#include "datagram.h"
 
 
 // This must be included only in exactly one .cxx file, since
 // This must be included only in exactly one .cxx file, since
 // including defines the structure!
 // including defines the structure!
@@ -91,9 +93,18 @@ open(const Filename &filename) {
 
 
   // Save the templates we will use.
   // Save the templates we will use.
   static const GUID *temps[] = {
   static const GUID *temps[] = {
+    &TID_D3DRMCoords2d,
     &TID_D3DRMVector,
     &TID_D3DRMVector,
+    &TID_D3DRMColorRGBA,
+    &TID_D3DRMColorRGB,
+    &TID_D3DRMIndexedColor,
+    &TID_D3DRMTextureFilename,
+    &TID_D3DRMMaterial,
     &TID_D3DRMMeshFace,
     &TID_D3DRMMeshFace,
     &TID_D3DRMMesh,
     &TID_D3DRMMesh,
+    &TID_D3DRMMeshNormals,
+    &TID_D3DRMMeshTextureCoords,
+    &TID_D3DRMMeshMaterialList,
     &TID_D3DRMFrame,
     &TID_D3DRMFrame,
   };
   };
   static const int num_temps = sizeof(temps) / sizeof(temps[0]);
   static const int num_temps = sizeof(temps) / sizeof(temps[0]);
@@ -159,17 +170,17 @@ add_node(EggNode *egg_node, LPDIRECTXFILEDATA dx_parent) {
   } else if (egg_node->is_of_type(EggGroupNode::get_class_type())) {
   } else if (egg_node->is_of_type(EggGroupNode::get_class_type())) {
     // A grouping node of some kind.
     // A grouping node of some kind.
     EggGroupNode *egg_group = DCAST(EggGroupNode, egg_node);
     EggGroupNode *egg_group = DCAST(EggGroupNode, egg_node);
-    LPDIRECTXFILEDATA data;
-    if (!create_frame(data, egg_group->get_name())) {
+    LPDIRECTXFILEDATA obj;
+    if (!create_frame(obj, egg_group->get_name())) {
       return false;
       return false;
     }
     }
     
     
-    if (!recurse_nodes(egg_group, data)) {
-      data->Release();
+    if (!recurse_nodes(egg_group, obj)) {
+      obj->Release();
       return false;
       return false;
     }
     }
     
     
-    if (!attach_and_release(data, dx_parent)) {
+    if (!attach_and_release(obj, dx_parent)) {
       return false;
       return false;
     }
     }
     
     
@@ -187,17 +198,17 @@ add_node(EggNode *egg_node, LPDIRECTXFILEDATA dx_parent) {
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 bool XFileMaker::
 bool XFileMaker::
 add_group(EggGroup *egg_group, LPDIRECTXFILEDATA dx_parent) {
 add_group(EggGroup *egg_group, LPDIRECTXFILEDATA dx_parent) {
-  LPDIRECTXFILEDATA data;
-  if (!create_frame(data, egg_group->get_name())) {
+  LPDIRECTXFILEDATA obj;
+  if (!create_frame(obj, egg_group->get_name())) {
     return false;
     return false;
   }
   }
 
 
-  if (!recurse_nodes(egg_group, data)) {
-    data->Release();
+  if (!recurse_nodes(egg_group, obj)) {
+    obj->Release();
     return false;
     return false;
   }
   }
 
 
-  if (!attach_and_release(data, dx_parent)) {
+  if (!attach_and_release(obj, dx_parent)) {
     return false;
     return false;
   }
   }
 
 
@@ -232,89 +243,31 @@ add_polyset(EggBin *egg_bin, LPDIRECTXFILEDATA dx_parent) {
   // Make sure that all our polygons are reasonable.
   // Make sure that all our polygons are reasonable.
   egg_bin->remove_invalid_primitives();
   egg_bin->remove_invalid_primitives();
 
 
-  // First, we need to collect all the common vertices for the
-  // polygons in this polyset.  We can do this fairly easily using a
-  // vertex pool.
-  PT(EggVertexPool) vpool = new EggVertexPool("");
-
-  int num_polys = 0;
+  XFileMesh mesh;
 
 
   EggGroupNode::iterator ci;
   EggGroupNode::iterator ci;
   for (ci = egg_bin->begin(); ci != egg_bin->end(); ++ci) {
   for (ci = egg_bin->begin(); ci != egg_bin->end(); ++ci) {
     EggPolygon *poly;
     EggPolygon *poly;
     DCAST_INTO_R(poly, *ci, false);
     DCAST_INTO_R(poly, *ci, false);
 
 
-    // A temporary holder for the newly converted vertices of the
-    // polygon.
-    PT(EggPolygon) vertex_holder = new EggPolygon;
-
-    num_polys++;
-    EggPolygon::iterator vi;
-    for (vi = poly->begin(); vi != poly->end(); ++vi) {
-      // Make a copy of the polygon's original vertex.
-      PT(EggVertex) vtx_copy = new EggVertex(*(*vi));
-      
-      // Now change the properties on the vertex as appropriate to our
-      // mesh.
-      if (!vtx_copy->has_color()) {
-        vtx_copy->set_color(poly->get_color());
-      }
-      vtx_copy->_dnormals.clear();
-      vtx_copy->_duvs.clear();
-      vtx_copy->_drgbas.clear();
-      vtx_copy->_dxyzs.clear();
-
-      // And create the unique vertex.
-      EggVertex *vtx = vpool->create_unique_vertex(*vtx_copy);
-      vertex_holder->add_vertex(vtx);
-    }
-
-    poly->copy_vertices(*vertex_holder);
+    mesh.add_polygon(poly);
   }
   }
 
 
-  // Now create the raw data for the Mesh object.
-  int highest_index = vpool->get_highest_index();
-  Datagram raw_data;
-  raw_data.add_int32(highest_index);
-  for (int i = 1; i <= highest_index; i++) {
-    EggVertex *vtx = vpool->get_vertex(i);
-    nassertr(vtx != (EggVertex *)NULL, false);
-    Vertexd pos = vtx->get_pos3();
-    raw_data.add_float32(pos[0]);
-    raw_data.add_float32(pos[1]);
-    raw_data.add_float32(pos[2]);
-  }
-
-  raw_data.add_int32(num_polys);
-  for (ci = egg_bin->begin(); ci != egg_bin->end(); ++ci) {
-    EggPolygon *poly;
-    DCAST_INTO_R(poly, *ci, false);
-
-    raw_data.add_int32(poly->size());
-    EggPolygon::reverse_iterator vi;
-    for (vi = poly->rbegin(); vi != poly->rend(); ++vi) {
-      int index = (*vi)->get_index();
-      raw_data.add_int32(index - 1);
-    }
-  }
+  // Get a unique number for each mesh.
+  _mesh_index++;
+  string mesh_index = format_string(_mesh_index);
 
 
   // Finally, create the Mesh object.
   // Finally, create the Mesh object.
-  HRESULT hr;
-  LPDIRECTXFILEDATA data;
-
-  string name = "mesh" + format_string(_mesh_index);
-  _mesh_index++;
+  Datagram raw_data;
+  mesh.make_mesh_data(raw_data);
 
 
-  hr = _dx_file_save->CreateDataObject
-    (TID_D3DRMMesh, name.c_str(), NULL, 
-     raw_data.get_length(), (void *)raw_data.get_data(),
-     &data);
-  if (hr != DXFILE_OK) {
-    nout << "Unable to create Mesh object\n";
+  LPDIRECTXFILEDATA xobj;
+  cerr << "Creating mesh\n";
+  if (!create_object(xobj, TID_D3DRMMesh, "mesh" + mesh_index, raw_data)) {
     return false;
     return false;
   }
   }
 
 
-  if (!attach_and_release(data, dx_parent)) {
+  if (!attach_and_release(xobj, dx_parent)) {
     return false;
     return false;
   }
   }
   return true;
   return true;
@@ -340,24 +293,46 @@ recurse_nodes(EggGroupNode *egg_node, LPDIRECTXFILEDATA dx_parent) {
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: XFileMaker::create_frame
+//     Function: XFileMaker::create_object
 //       Access: Private
 //       Access: Private
-//  Description: Creates a "frame" object with the indicated name.
+//  Description: Creates a DX data object.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 bool XFileMaker::
 bool XFileMaker::
-create_frame(LPDIRECTXFILEDATA &data, const string &name) {
+create_object(LPDIRECTXFILEDATA &obj, REFGUID template_id,
+              const string &name, const Datagram &dg) {
   HRESULT hr;
   HRESULT hr;
 
 
   string nice_name = make_nice_name(name);
   string nice_name = make_nice_name(name);
+
+  int data_size = dg.get_length();
+  void *data_pointer = (void *)dg.get_data();
+
+  if (data_size == 0) {
+    data_pointer = (void *)NULL;
+  }
+
   hr = _dx_file_save->CreateDataObject
   hr = _dx_file_save->CreateDataObject
-    (TID_D3DRMFrame, nice_name.c_str(), NULL, 0, NULL, &data);
+    (template_id, nice_name.c_str(), NULL, 
+     data_size, data_pointer, &obj);
+
   if (hr != DXFILE_OK) {
   if (hr != DXFILE_OK) {
-    nout << "Unable to create frame object for " << name << "\n";
+    nout << "Unable to create data object for " << name << "\n";
     return false;
     return false;
   }
   }
   return true;
   return true;
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: XFileMaker::create_frame
+//       Access: Private
+//  Description: Creates a "frame" object with the indicated name.
+////////////////////////////////////////////////////////////////////
+bool XFileMaker::
+create_frame(LPDIRECTXFILEDATA &obj, const string &name) {
+  cerr << "Creating frame\n";
+  return create_object(obj, TID_D3DRMFrame, name, Datagram());
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: XFileMaker::attach_and_release
 //     Function: XFileMaker::attach_and_release
 //       Access: Private
 //       Access: Private
@@ -365,28 +340,28 @@ create_frame(LPDIRECTXFILEDATA &data, const string &name) {
 //               parent, and releases the pointer.
 //               parent, and releases the pointer.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 bool XFileMaker::
 bool XFileMaker::
-attach_and_release(LPDIRECTXFILEDATA data, LPDIRECTXFILEDATA dx_parent) {
+attach_and_release(LPDIRECTXFILEDATA obj, LPDIRECTXFILEDATA dx_parent) {
   HRESULT hr;
   HRESULT hr;
 
 
   if (dx_parent == NULL) {
   if (dx_parent == NULL) {
     // No parent; it's a toplevel object.
     // No parent; it's a toplevel object.
-    hr = _dx_file_save->SaveData(data);
+    hr = _dx_file_save->SaveData(obj);
     if (hr != DXFILE_OK) {
     if (hr != DXFILE_OK) {
       nout << "Unable to save data object\n";
       nout << "Unable to save data object\n";
-      data->Release();
+      obj->Release();
       return false;
       return false;
     }
     }
   } else {
   } else {
     // Got a parent; it's a child of the indicated object.
     // Got a parent; it's a child of the indicated object.
-    hr = dx_parent->AddDataObject(data);
+    hr = dx_parent->AddDataObject(obj);
     if (hr != DXFILE_OK) {
     if (hr != DXFILE_OK) {
       nout << "Unable to save data object\n";
       nout << "Unable to save data object\n";
-      data->Release();
+      obj->Release();
       return false;
       return false;
     }
     }
   }
   }
 
 
-  data->Release();
+  obj->Release();
   return true;
   return true;
 }
 }
 
 

+ 7 - 2
pandatool/src/xfile/xFileMaker.h

@@ -35,6 +35,8 @@ class EggGroupNode;
 class EggGroup;
 class EggGroup;
 class EggBin;
 class EggBin;
 class EggData;
 class EggData;
+class EggVertexPool;
+class Datagram;
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //       Class : XFileMaker
 //       Class : XFileMaker
@@ -58,8 +60,11 @@ private:
   bool add_polyset(EggBin *egg_bin, LPDIRECTXFILEDATA dx_parent);
   bool add_polyset(EggBin *egg_bin, LPDIRECTXFILEDATA dx_parent);
 
 
   bool recurse_nodes(EggGroupNode *egg_node, LPDIRECTXFILEDATA dx_parent);
   bool recurse_nodes(EggGroupNode *egg_node, LPDIRECTXFILEDATA dx_parent);
-  bool create_frame(LPDIRECTXFILEDATA &data, const string &name);
-  bool attach_and_release(LPDIRECTXFILEDATA data, LPDIRECTXFILEDATA dx_parent);
+
+  bool create_object(LPDIRECTXFILEDATA &obj, REFGUID template_id,
+                     const string &name, const Datagram &dg);
+  bool create_frame(LPDIRECTXFILEDATA &obj, const string &name);
+  bool attach_and_release(LPDIRECTXFILEDATA obj, LPDIRECTXFILEDATA dx_parent);
 
 
   static string make_nice_name(const string &str);
   static string make_nice_name(const string &str);
 
 

+ 139 - 0
pandatool/src/xfile/xFileMesh.cxx

@@ -0,0 +1,139 @@
+// Filename: xFileMesh.cxx
+// Created by:  drose (19Jun01)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "xFileMesh.h"
+#include "xFileFace.h"
+#include "xFileVertex.h"
+#include "xFileNormal.h"
+
+////////////////////////////////////////////////////////////////////
+//     Function: XFileMesh::Constructor
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+XFileMesh::
+XFileMesh() {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: XFileMesh::Destructor
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+XFileMesh::
+~XFileMesh() {
+  // ** Delete stuff.
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: XFileMesh::add_polygon
+//       Access: Public
+//  Description: Adds the indicated polygon to the mesh.
+////////////////////////////////////////////////////////////////////
+void XFileMesh::
+add_polygon(EggPolygon *egg_poly) {
+  XFileFace *face = new XFileFace(this, egg_poly);
+  _faces.push_back(face);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: XFileMesh::add_vertex
+//       Access: Public
+//  Description: Creates a new XFileVertex, if one does not already
+//               exist for the indicated vertex, and returns its
+//               index.
+////////////////////////////////////////////////////////////////////
+int XFileMesh::
+add_vertex(EggVertex *egg_vertex, EggPrimitive *egg_prim) {
+  int next_index = _vertices.size();
+  XFileVertex *vertex = new XFileVertex(egg_vertex, egg_prim);
+  pair<UniqueVertices::iterator, bool> result =
+    _unique_vertices.insert(UniqueVertices::value_type(vertex, next_index));
+
+  if (result.second) {
+    // Successfully added; this is a new vertex.
+    _vertices.push_back(vertex);
+    return next_index;
+  } else {
+    // Not successfully added; there is already a vertex with these
+    // properties.  Return that one instead.
+    delete vertex;
+    return (*result.first).second;
+  }
+}
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: XFileMesh::add_normal
+//       Access: Public
+//  Description: Creates a new XFileNormal, if one does not already
+//               exist for the indicated normal, and returns its
+//               index.
+////////////////////////////////////////////////////////////////////
+int XFileMesh::
+add_normal(EggVertex *egg_vertex, EggPrimitive *egg_prim) {
+  int next_index = _normals.size();
+  XFileNormal *normal = new XFileNormal(egg_vertex, egg_prim);
+  pair<UniqueNormals::iterator, bool> result =
+    _unique_normals.insert(UniqueNormals::value_type(normal, next_index));
+
+  if (result.second) {
+    // Successfully added; this is a new normal.
+    _normals.push_back(normal);
+    return next_index;
+  } else {
+    // Not successfully added; there is already a normal with these
+    // properties.  Return that one instead.
+    delete normal;
+    return (*result.first).second;
+  }
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: XFileMesh::make_mesh_data
+//       Access: Public
+//  Description: Fills the datagram with the raw data for the DX
+//               Mesh template.
+////////////////////////////////////////////////////////////////////
+void XFileMesh::
+make_mesh_data(Datagram &raw_data) {
+  raw_data.add_int32(_vertices.size());
+  
+  Vertices::const_iterator vi;
+  for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) {
+    XFileVertex *vertex = (*vi);
+    const Vertexf &point = vertex->_point;
+    raw_data.add_float32(point[0]);
+    raw_data.add_float32(point[1]);
+    raw_data.add_float32(point[2]);
+  }
+
+  raw_data.add_int32(_faces.size());
+  Faces::const_iterator fi;
+  for (fi = _faces.begin(); fi != _faces.end(); ++fi) {
+    XFileFace *face = (*fi);
+
+    raw_data.add_int32(face->_vertices.size());
+    XFileFace::Vertices::const_iterator fvi;
+    for (fvi = face->_vertices.begin();
+         fvi != face->_vertices.end();
+         ++fvi) {
+      raw_data.add_int32((*fvi)._vertex_index);
+    }
+  }
+}

+ 68 - 0
pandatool/src/xfile/xFileMesh.h

@@ -0,0 +1,68 @@
+// Filename: xFileMesh.h
+// Created by:  drose (19Jun01)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef XFILEMESH_H
+#define XFILEMESH_H
+
+#include "pandatoolbase.h"
+#include "pvector.h"
+#include "pmap.h"
+#include "indirectCompareTo.h"
+
+class XFileMesh;
+class XFileVertex;
+class XFileNormal;
+class XFileFace;
+class EggVertex;
+class EggPolygon;
+class EggPrimitive;
+class Datagram;
+
+////////////////////////////////////////////////////////////////////
+//       Class : XFileMesh
+// Description : This is a collection of polygons; i.e. a polyset.
+////////////////////////////////////////////////////////////////////
+class XFileMesh {
+public:
+  XFileMesh();
+  ~XFileMesh();
+
+  void add_polygon(EggPolygon *egg_poly);
+  int add_vertex(EggVertex *egg_vertex, EggPrimitive *egg_prim);
+  int add_normal(EggVertex *egg_vertex, EggPrimitive *egg_prim);
+
+  void make_mesh_data(Datagram &raw_data);
+
+public:
+  typedef pvector<XFileVertex *> Vertices;
+  typedef pvector<XFileNormal *> Normals;
+  typedef pvector<XFileFace *> Faces;
+
+  Vertices _vertices;
+  Normals _normals;
+  Faces _faces;
+
+private:
+  typedef pmap<XFileVertex *, int, IndirectCompareTo<XFileVertex> > UniqueVertices;
+  typedef pmap<XFileNormal *, int, IndirectCompareTo<XFileNormal> > UniqueNormals;
+  UniqueVertices _unique_vertices;
+  UniqueNormals _unique_normals;
+};
+
+#endif
+

+ 49 - 0
pandatool/src/xfile/xFileNormal.cxx

@@ -0,0 +1,49 @@
+// Filename: xFileNormal.cxx
+// Created by:  drose (19Jun01)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#include "xFileNormal.h"
+#include "eggVertex.h"
+#include "eggPrimitive.h"
+
+////////////////////////////////////////////////////////////////////
+//     Function: XFileNormal::Constructor
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+XFileNormal::
+XFileNormal(EggVertex *egg_vertex, EggPrimitive *egg_prim) {
+  if (egg_vertex->has_normal()) {
+    _normal = LCAST(float, egg_vertex->get_normal());
+  } else if (egg_prim->has_normal()) {
+    _normal = LCAST(float, egg_prim->get_normal());
+  } else {
+    _normal.set(0.0, 0.0, 0.0);
+  }
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: XFileNormal::compare_to
+//       Access: Public
+//  Description:
+////////////////////////////////////////////////////////////////////
+int XFileNormal::
+compare_to(const XFileNormal &other) const {
+  int ct;
+  ct = _normal.compare_to(other._normal);
+  return ct;
+}

+ 44 - 0
pandatool/src/xfile/xFileNormal.h

@@ -0,0 +1,44 @@
+// Filename: xFileNormal.h
+// Created by:  drose (19Jun01)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef XFILENORMAL_H
+#define XFILENORMAL_H
+
+#include "pandatoolbase.h"
+#include "luse.h"
+
+class EggVertex;
+class EggPrimitive;
+
+////////////////////////////////////////////////////////////////////
+//       Class : XFileNormal
+// Description : This represents a single normal associated with an
+//               XFileFace.  It is separate from XFileVertex, because
+//               the X syntax supports a different table of normals
+//               than that of vertices.
+////////////////////////////////////////////////////////////////////
+class XFileNormal {
+public:
+  XFileNormal(EggVertex *egg_vertex, EggPrimitive *egg_prim);
+  int compare_to(const XFileNormal &other) const;
+
+  Normalf _normal;
+};
+
+#endif
+

+ 20 - 41
pandatool/src/xfile/xFileVertex.cxx

@@ -17,6 +17,8 @@
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 
 
 #include "xFileVertex.h"
 #include "xFileVertex.h"
+#include "eggVertex.h"
+#include "eggPrimitive.h"
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: XFileVertex::Constructor
 //     Function: XFileVertex::Constructor
@@ -24,61 +26,38 @@
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 XFileVertex::
 XFileVertex::
-XFileVertex(const Vertexf &point) : 
-  _point(point),
-  _normal(0.0, 0.0, 0.0),
-  _uv(0.0, 0.0),
-  _color(1.0, 1.0, 1.0, 1.0)
-{
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: XFileVertex::set_normal
-//       Access: Public
-//  Description:
-////////////////////////////////////////////////////////////////////
-void XFileVertex::
-set_normal(const Normalf &normal) {
-  _normal = normal;
-}
+XFileVertex(EggVertex *egg_vertex, EggPrimitive *egg_prim) {
+  _point = LCAST(float, egg_vertex->get_pos3());
 
 
-////////////////////////////////////////////////////////////////////
-//     Function: XFileVertex::set_uv
-//       Access: Public
-//  Description:
-////////////////////////////////////////////////////////////////////
-void XFileVertex::
-set_uv(const TexCoordf &uv) {
-  _uv = uv;
-}
+  if (egg_vertex->has_uv()) {
+    _uv = LCAST(float, egg_vertex->get_uv());
+  } else {
+    _uv.set(0.0, 0.0);
+  }
 
 
-////////////////////////////////////////////////////////////////////
-//     Function: XFileVertex::set_color
-//       Access: Public
-//  Description:
-////////////////////////////////////////////////////////////////////
-void XFileVertex::
-set_color(const Colorf &color) {
-  _color = color;
+  if (egg_vertex->has_color()) {
+    _color = egg_vertex->get_color();
+  } else if (egg_prim->has_color()) {
+    _color = egg_prim->get_color();
+  } else {
+    _color.set(1.0, 1.0, 1.0, 1.0);
+  }
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: XFileVertex::Ordering Operator
+//     Function: XFileVertex::compare_to
 //       Access: Public
 //       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-bool XFileVertex::
-operator < (const XFileVertex &other) const {
+int XFileVertex::
+compare_to(const XFileVertex &other) const {
   int ct;
   int ct;
   ct = _point.compare_to(other._point);
   ct = _point.compare_to(other._point);
-  if (ct == 0) {
-    ct = _normal.compare_to(other._normal);
-  }
   if (ct == 0) {
   if (ct == 0) {
     ct = _uv.compare_to(other._uv);
     ct = _uv.compare_to(other._uv);
   }
   }
   if (ct == 0) {
   if (ct == 0) {
     ct = _color.compare_to(other._color);
     ct = _color.compare_to(other._color);
   }
   }
-  return (ct < 0);
+  return ct;
 }
 }

+ 7 - 11
pandatool/src/xfile/xFileVertex.h

@@ -22,24 +22,20 @@
 #include "pandatoolbase.h"
 #include "pandatoolbase.h"
 #include "luse.h"
 #include "luse.h"
 
 
+class EggVertex;
+class EggPrimitive;
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //       Class : XFileVertex
 //       Class : XFileVertex
-// Description : This class represents a single Vertex as extracted
-//               from a Geom and added to the vertex pool.
+// Description : This represents a single vertex associated with an
+//               XFileFace.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 class XFileVertex {
 class XFileVertex {
 public:
 public:
-  XFileVertex(const Vertexf &point);
-
-  void set_normal(const Normalf &normal);
-  void set_uv(const TexCoordf &uv);
-  void set_color(const Colorf &color);
-
-  bool operator < (const XFileVertex &other) const;
+  XFileVertex(EggVertex *egg_vertex, EggPrimitive *egg_poly);
+  int compare_to(const XFileVertex &other) const;
 
 
-private:
   Vertexf _point;
   Vertexf _point;
-  Normalf _normal;
   TexCoordf _uv;
   TexCoordf _uv;
   Colorf _color;
   Colorf _color;
 };
 };

+ 58 - 58
pandatool/src/xfile/xFileVertexPool.h

@@ -1,58 +1,58 @@
-// Filename: xFileVertexPool.h
-// Created by:  drose (19Jun01)
-//
-////////////////////////////////////////////////////////////////////
-//
-// PANDA 3D SOFTWARE
-// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
-//
-// All use of this software is subject to the terms of the Panda 3d
-// Software license.  You should have received a copy of this license
-// along with this source code; you will also find a current copy of
-// the license at http://www.panda3d.org/license.txt .
-//
-// To contact the maintainers of this program write to
-// [email protected] .
-//
-////////////////////////////////////////////////////////////////////
-
-#ifndef XFILEVERTEXPOOL_H
-#define XFILEVERTEXPOOL_H
-
-#include "pandatoolbase.h"
-
-////////////////////////////////////////////////////////////////////
-//       Class : XFileVertexPool
-// Description : This is a collection of unique vertices as extracted
-//               out of a Geom or a series of Geoms.
-////////////////////////////////////////////////////////////////////
-class XFileVertexPool {
-public:
-  XFileVertexPool();
-  ~XFileVertexPool();
-
-  int add_vertex(const XFileVertex &vertex);
-
-  int get_num_vertices();
-  const Vertexf *get_vertices();
-  const Normalf *get_normals();
-  const TexCoordf *get_uvs();
-  const Colorf *get_colors();
-
-  
-
-  void set_normal(const Normalf &normal);
-  void set_uv(const TexCoordf &uv);
-  void set_color(const Colorf &color);
-
-  bool operator < (const XFileVertexPool &other) const;
-
-private:
-  Vertexf _point;
-  Normalf _normal;
-  TexCoordf _uv;
-  Colorf _color;
-};
-
-#endif
-
+// Filename: xFileVertexPool.h
+// Created by:  drose (19Jun01)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
+//
+// All use of this software is subject to the terms of the Panda 3d
+// Software license.  You should have received a copy of this license
+// along with this source code; you will also find a current copy of
+// the license at http://www.panda3d.org/license.txt .
+//
+// To contact the maintainers of this program write to
+// [email protected] .
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef XFILEVERTEXPOOL_H
+#define XFILEVERTEXPOOL_H
+
+#include "pandatoolbase.h"
+
+////////////////////////////////////////////////////////////////////
+//       Class : XFileVertexPool
+// Description : This is a collection of unique vertices as extracted
+//               out of a Geom or a series of Geoms.
+////////////////////////////////////////////////////////////////////
+class XFileVertexPool {
+public:
+  XFileVertexPool();
+  ~XFileVertexPool();
+
+  int add_vertex(const XFileVertex &vertex);
+
+  int get_num_vertices();
+  const Vertexf *get_vertices();
+  const Normalf *get_normals();
+  const TexCoordf *get_uvs();
+  const Colorf *get_colors();
+
+  
+
+  void set_normal(const Normalf &normal);
+  void set_uv(const TexCoordf &uv);
+  void set_color(const Colorf &color);
+
+  bool operator < (const XFileVertexPool &other) const;
+
+private:
+  Vertexf _point;
+  Normalf _normal;
+  TexCoordf _uv;
+  Colorf _color;
+};
+
+#endif
+