diff --git a/pandatool/src/xfile/Sources.pp b/pandatool/src/xfile/Sources.pp index 9c20fd0581..d47f7f6b31 100644 --- a/pandatool/src/xfile/Sources.pp +++ b/pandatool/src/xfile/Sources.pp @@ -14,7 +14,11 @@ d3dxof.lib #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 \ $[SOURCES] \ diff --git a/pandatool/src/xfile/eggToX.cxx b/pandatool/src/xfile/eggToX.cxx index d247d46018..515eb17bfc 100644 --- a/pandatool/src/xfile/eggToX.cxx +++ b/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 -// panda3d@yahoogroups.com . -// -//////////////////////////////////////////////////////////////////// - -#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 +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#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; +} diff --git a/pandatool/src/xfile/eggToX.h b/pandatool/src/xfile/eggToX.h index 17575f86fe..9e7ce08823 100644 --- a/pandatool/src/xfile/eggToX.h +++ b/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 -// panda3d@yahoogroups.com . -// -//////////////////////////////////////////////////////////////////// - -#ifndef EGGTOX_H -#define EGGTOX_H - -#include "pandatoolbase.h" -#include "eggToSomething.h" -#include "xFileMaker.h" - -#include -#include -#include - -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 +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#ifndef EGGTOX_H +#define EGGTOX_H + +#include "pandatoolbase.h" +#include "eggToSomething.h" +#include "xFileMaker.h" + +#include +#include +#include + +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 diff --git a/pandatool/src/xfile/xFileFace.cxx b/pandatool/src/xfile/xFileFace.cxx new file mode 100644 index 0000000000..2bb0e0546b --- /dev/null +++ b/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 +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#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); + } +} diff --git a/pandatool/src/xfile/xFileFace.h b/pandatool/src/xfile/xFileFace.h new file mode 100644 index 0000000000..7f71fdb729 --- /dev/null +++ b/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 +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#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 Vertices; + Vertices _vertices; +}; + +#endif + diff --git a/pandatool/src/xfile/xFileMaker.cxx b/pandatool/src/xfile/xFileMaker.cxx index 6dfd7f382a..bf780ede12 100644 --- a/pandatool/src/xfile/xFileMaker.cxx +++ b/pandatool/src/xfile/xFileMaker.cxx @@ -17,6 +17,7 @@ //////////////////////////////////////////////////////////////////// #include "xFileMaker.h" +#include "xFileMesh.h" #include "notify.h" #include "eggGroupNode.h" #include "eggGroup.h" @@ -29,6 +30,7 @@ #include "pvector.h" #include "vector_int.h" #include "string_utils.h" +#include "datagram.h" // This must be included only in exactly one .cxx file, since // including defines the structure! @@ -91,9 +93,18 @@ open(const Filename &filename) { // Save the templates we will use. static const GUID *temps[] = { + &TID_D3DRMCoords2d, &TID_D3DRMVector, + &TID_D3DRMColorRGBA, + &TID_D3DRMColorRGB, + &TID_D3DRMIndexedColor, + &TID_D3DRMTextureFilename, + &TID_D3DRMMaterial, &TID_D3DRMMeshFace, &TID_D3DRMMesh, + &TID_D3DRMMeshNormals, + &TID_D3DRMMeshTextureCoords, + &TID_D3DRMMeshMaterialList, &TID_D3DRMFrame, }; 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())) { // A grouping node of some kind. 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; } - if (!recurse_nodes(egg_group, data)) { - data->Release(); + if (!recurse_nodes(egg_group, obj)) { + obj->Release(); return false; } - if (!attach_and_release(data, dx_parent)) { + if (!attach_and_release(obj, dx_parent)) { return false; } @@ -187,17 +198,17 @@ add_node(EggNode *egg_node, LPDIRECTXFILEDATA dx_parent) { //////////////////////////////////////////////////////////////////// bool XFileMaker:: 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; } - if (!recurse_nodes(egg_group, data)) { - data->Release(); + if (!recurse_nodes(egg_group, obj)) { + obj->Release(); return false; } - if (!attach_and_release(data, dx_parent)) { + if (!attach_and_release(obj, dx_parent)) { return false; } @@ -232,89 +243,31 @@ add_polyset(EggBin *egg_bin, LPDIRECTXFILEDATA dx_parent) { // Make sure that all our polygons are reasonable. 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; for (ci = egg_bin->begin(); ci != egg_bin->end(); ++ci) { EggPolygon *poly; 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. - HRESULT hr; - LPDIRECTXFILEDATA data; + Datagram raw_data; + mesh.make_mesh_data(raw_data); - string name = "mesh" + format_string(_mesh_index); - _mesh_index++; - - 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; } - if (!attach_and_release(data, dx_parent)) { + if (!attach_and_release(xobj, dx_parent)) { return false; } return true; @@ -339,23 +292,45 @@ recurse_nodes(EggGroupNode *egg_node, LPDIRECTXFILEDATA dx_parent) { return true; } +//////////////////////////////////////////////////////////////////// +// Function: XFileMaker::create_object +// Access: Private +// Description: Creates a DX data object. +//////////////////////////////////////////////////////////////////// +bool XFileMaker:: +create_object(LPDIRECTXFILEDATA &obj, REFGUID template_id, + const string &name, const Datagram &dg) { + HRESULT hr; + + 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 + (template_id, nice_name.c_str(), NULL, + data_size, data_pointer, &obj); + + if (hr != DXFILE_OK) { + nout << "Unable to create data object for " << name << "\n"; + return false; + } + return true; +} + //////////////////////////////////////////////////////////////////// // Function: XFileMaker::create_frame // Access: Private // Description: Creates a "frame" object with the indicated name. //////////////////////////////////////////////////////////////////// bool XFileMaker:: -create_frame(LPDIRECTXFILEDATA &data, const string &name) { - HRESULT hr; - - string nice_name = make_nice_name(name); - hr = _dx_file_save->CreateDataObject - (TID_D3DRMFrame, nice_name.c_str(), NULL, 0, NULL, &data); - if (hr != DXFILE_OK) { - nout << "Unable to create frame object for " << name << "\n"; - return false; - } - return true; +create_frame(LPDIRECTXFILEDATA &obj, const string &name) { + cerr << "Creating frame\n"; + return create_object(obj, TID_D3DRMFrame, name, Datagram()); } //////////////////////////////////////////////////////////////////// @@ -365,28 +340,28 @@ create_frame(LPDIRECTXFILEDATA &data, const string &name) { // parent, and releases the pointer. //////////////////////////////////////////////////////////////////// bool XFileMaker:: -attach_and_release(LPDIRECTXFILEDATA data, LPDIRECTXFILEDATA dx_parent) { +attach_and_release(LPDIRECTXFILEDATA obj, LPDIRECTXFILEDATA dx_parent) { HRESULT hr; if (dx_parent == NULL) { // No parent; it's a toplevel object. - hr = _dx_file_save->SaveData(data); + hr = _dx_file_save->SaveData(obj); if (hr != DXFILE_OK) { nout << "Unable to save data object\n"; - data->Release(); + obj->Release(); return false; } } else { // 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) { nout << "Unable to save data object\n"; - data->Release(); + obj->Release(); return false; } } - data->Release(); + obj->Release(); return true; } diff --git a/pandatool/src/xfile/xFileMaker.h b/pandatool/src/xfile/xFileMaker.h index 274fc6f59d..d7b5fc33e3 100644 --- a/pandatool/src/xfile/xFileMaker.h +++ b/pandatool/src/xfile/xFileMaker.h @@ -35,6 +35,8 @@ class EggGroupNode; class EggGroup; class EggBin; class EggData; +class EggVertexPool; +class Datagram; //////////////////////////////////////////////////////////////////// // Class : XFileMaker @@ -58,8 +60,11 @@ private: bool add_polyset(EggBin *egg_bin, 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); diff --git a/pandatool/src/xfile/xFileMesh.cxx b/pandatool/src/xfile/xFileMesh.cxx new file mode 100644 index 0000000000..88379ed5f8 --- /dev/null +++ b/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 +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#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 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 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); + } + } +} diff --git a/pandatool/src/xfile/xFileMesh.h b/pandatool/src/xfile/xFileMesh.h new file mode 100644 index 0000000000..30b5654393 --- /dev/null +++ b/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 +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#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 Vertices; + typedef pvector Normals; + typedef pvector Faces; + + Vertices _vertices; + Normals _normals; + Faces _faces; + +private: + typedef pmap > UniqueVertices; + typedef pmap > UniqueNormals; + UniqueVertices _unique_vertices; + UniqueNormals _unique_normals; +}; + +#endif + diff --git a/pandatool/src/xfile/xFileNormal.cxx b/pandatool/src/xfile/xFileNormal.cxx new file mode 100644 index 0000000000..aab3a80f4d --- /dev/null +++ b/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 +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#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; +} diff --git a/pandatool/src/xfile/xFileNormal.h b/pandatool/src/xfile/xFileNormal.h new file mode 100644 index 0000000000..104822ac03 --- /dev/null +++ b/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 +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#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 + diff --git a/pandatool/src/xfile/xFileVertex.cxx b/pandatool/src/xfile/xFileVertex.cxx index 52e0348294..f0a8cef9da 100644 --- a/pandatool/src/xfile/xFileVertex.cxx +++ b/pandatool/src/xfile/xFileVertex.cxx @@ -17,6 +17,8 @@ //////////////////////////////////////////////////////////////////// #include "xFileVertex.h" +#include "eggVertex.h" +#include "eggPrimitive.h" //////////////////////////////////////////////////////////////////// // Function: XFileVertex::Constructor @@ -24,61 +26,38 @@ // Description: //////////////////////////////////////////////////////////////////// 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) -{ +XFileVertex(EggVertex *egg_vertex, EggPrimitive *egg_prim) { + _point = LCAST(float, egg_vertex->get_pos3()); + + if (egg_vertex->has_uv()) { + _uv = LCAST(float, egg_vertex->get_uv()); + } else { + _uv.set(0.0, 0.0); + } + + 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::set_normal +// Function: XFileVertex::compare_to // Access: Public // Description: //////////////////////////////////////////////////////////////////// -void XFileVertex:: -set_normal(const Normalf &normal) { - _normal = normal; -} - -//////////////////////////////////////////////////////////////////// -// Function: XFileVertex::set_uv -// Access: Public -// Description: -//////////////////////////////////////////////////////////////////// -void XFileVertex:: -set_uv(const TexCoordf &uv) { - _uv = uv; -} - -//////////////////////////////////////////////////////////////////// -// Function: XFileVertex::set_color -// Access: Public -// Description: -//////////////////////////////////////////////////////////////////// -void XFileVertex:: -set_color(const Colorf &color) { - _color = color; -} - -//////////////////////////////////////////////////////////////////// -// Function: XFileVertex::Ordering Operator -// Access: Public -// Description: -//////////////////////////////////////////////////////////////////// -bool XFileVertex:: -operator < (const XFileVertex &other) const { +int XFileVertex:: +compare_to(const XFileVertex &other) const { int ct; ct = _point.compare_to(other._point); - if (ct == 0) { - ct = _normal.compare_to(other._normal); - } if (ct == 0) { ct = _uv.compare_to(other._uv); } if (ct == 0) { ct = _color.compare_to(other._color); } - return (ct < 0); + return ct; } diff --git a/pandatool/src/xfile/xFileVertex.h b/pandatool/src/xfile/xFileVertex.h index bfa4f51ee0..c2e6c72b4b 100644 --- a/pandatool/src/xfile/xFileVertex.h +++ b/pandatool/src/xfile/xFileVertex.h @@ -22,24 +22,20 @@ #include "pandatoolbase.h" #include "luse.h" +class EggVertex; +class EggPrimitive; + //////////////////////////////////////////////////////////////////// // 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 { public: - XFileVertex(const Vertexf &point); + XFileVertex(EggVertex *egg_vertex, EggPrimitive *egg_poly); + int compare_to(const XFileVertex &other) const; - void set_normal(const Normalf &normal); - void set_uv(const TexCoordf &uv); - void set_color(const Colorf &color); - - bool operator < (const XFileVertex &other) const; - -private: Vertexf _point; - Normalf _normal; TexCoordf _uv; Colorf _color; }; diff --git a/pandatool/src/xfile/xFileVertexPool.h b/pandatool/src/xfile/xFileVertexPool.h index a92d4a2e82..ae058cab57 100644 --- a/pandatool/src/xfile/xFileVertexPool.h +++ b/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 -// panda3d@yahoogroups.com . -// -//////////////////////////////////////////////////////////////////// - -#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 +// panda3d@yahoogroups.com . +// +//////////////////////////////////////////////////////////////////// + +#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 +