From 5fc3406d312c40e8af0117ed5a1a3f8a4ce0048f Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 21 Jun 2001 16:35:27 +0000 Subject: [PATCH] *** empty log message *** --- pandatool/src/xfile/xFileMaker.cxx | 26 +++++++++ pandatool/src/xfile/xFileMesh.cxx | 87 ++++++++++++++++++++++++++++- pandatool/src/xfile/xFileMesh.h | 6 ++ pandatool/src/xfile/xFileVertex.cxx | 4 ++ pandatool/src/xfile/xFileVertex.h | 2 + 5 files changed, 124 insertions(+), 1 deletion(-) diff --git a/pandatool/src/xfile/xFileMaker.cxx b/pandatool/src/xfile/xFileMaker.cxx index 8c0353979d..579570ddf8 100644 --- a/pandatool/src/xfile/xFileMaker.cxx +++ b/pandatool/src/xfile/xFileMaker.cxx @@ -279,6 +279,32 @@ add_polyset(EggBin *egg_bin, LPDIRECTXFILEDATA dx_parent) { } } + if (mesh.has_colors()) { + // Tack on colors. + LPDIRECTXFILEDATA xcolors; + mesh.make_color_data(raw_data); + if (!create_object(xcolors, TID_D3DRMMeshVertexColors, + "colors" + mesh_index, raw_data)) { + return false; + } + if (!attach_and_release(xcolors, xobj)) { + return false; + } + } + + if (mesh.has_uvs()) { + // Tack on texture coordinates. + LPDIRECTXFILEDATA xuvs; + mesh.make_uv_data(raw_data); + if (!create_object(xuvs, TID_D3DRMMeshTextureCoords, + "uvs" + mesh_index, raw_data)) { + return false; + } + if (!attach_and_release(xuvs, xobj)) { + return false; + } + } + if (!attach_and_release(xobj, dx_parent)) { return false; } diff --git a/pandatool/src/xfile/xFileMesh.cxx b/pandatool/src/xfile/xFileMesh.cxx index 11a2c0f9c3..71bcc5ea6b 100644 --- a/pandatool/src/xfile/xFileMesh.cxx +++ b/pandatool/src/xfile/xFileMesh.cxx @@ -29,6 +29,8 @@ XFileMesh:: XFileMesh() { _has_normals = false; + _has_colors = false; + _has_uvs = false; } //////////////////////////////////////////////////////////////////// @@ -38,7 +40,16 @@ XFileMesh() { //////////////////////////////////////////////////////////////////// XFileMesh:: ~XFileMesh() { - // ** Delete stuff. + Vertices::iterator vi; + for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) { + XFileVertex *vertex = (*vi); + delete vertex; + } + Normals::iterator ni; + for (ni = _normals.begin(); ni != _normals.end(); ++ni) { + XFileNormal *normal = (*ni); + delete normal; + } } //////////////////////////////////////////////////////////////////// @@ -63,6 +74,13 @@ int XFileMesh:: add_vertex(EggVertex *egg_vertex, EggPrimitive *egg_prim) { int next_index = _vertices.size(); XFileVertex *vertex = new XFileVertex(egg_vertex, egg_prim); + if (vertex->_has_color) { + _has_colors = true; + } + if (vertex->_has_uv) { + _has_uvs = true; + } + pair result = _unique_vertices.insert(UniqueVertices::value_type(vertex, next_index)); @@ -120,6 +138,28 @@ has_normals() const { return _has_normals; } +//////////////////////////////////////////////////////////////////// +// Function: XFileMesh::has_colors +// Access: Public +// Description: Returns true if any of the vertices or faces added to +// this mesh used a color, false otherwise. +//////////////////////////////////////////////////////////////////// +bool XFileMesh:: +has_colors() const { + return _has_colors; +} + +//////////////////////////////////////////////////////////////////// +// Function: XFileMesh::has_uvs +// Access: Public +// Description: Returns true if any of the vertices added to this +// mesh used a texture coordinate, false otherwise. +//////////////////////////////////////////////////////////////////// +bool XFileMesh:: +has_uvs() const { + return _has_uvs; +} + //////////////////////////////////////////////////////////////////// // Function: XFileMesh::make_mesh_data // Access: Public @@ -189,3 +229,48 @@ make_normal_data(Datagram &raw_data) { } } } + +//////////////////////////////////////////////////////////////////// +// Function: XFileMesh::make_color_data +// Access: Public +// Description: Fills the datagram with the raw data for the DX +// MeshVertexColors template. +//////////////////////////////////////////////////////////////////// +void XFileMesh:: +make_color_data(Datagram &raw_data) { + raw_data.clear(); + raw_data.add_int32(_vertices.size()); + + Vertices::const_iterator vi; + int i = 0; + for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) { + XFileVertex *vertex = (*vi); + const Colorf &color = vertex->_color; + raw_data.add_int32(i); + raw_data.add_float32(color[0]); + raw_data.add_float32(color[1]); + raw_data.add_float32(color[2]); + raw_data.add_float32(color[3]); + i++; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: XFileMesh::make_uv_data +// Access: Public +// Description: Fills the datagram with the raw data for the DX +// MeshTextureCoords template. +//////////////////////////////////////////////////////////////////// +void XFileMesh:: +make_uv_data(Datagram &raw_data) { + raw_data.clear(); + raw_data.add_int32(_vertices.size()); + + Vertices::const_iterator vi; + for (vi = _vertices.begin(); vi != _vertices.end(); ++vi) { + XFileVertex *vertex = (*vi); + const TexCoordf &uv = vertex->_uv; + raw_data.add_float32(uv[0]); + raw_data.add_float32(uv[1]); + } +} diff --git a/pandatool/src/xfile/xFileMesh.h b/pandatool/src/xfile/xFileMesh.h index 4c46fc67ca..ce9fad5cfc 100644 --- a/pandatool/src/xfile/xFileMesh.h +++ b/pandatool/src/xfile/xFileMesh.h @@ -47,9 +47,13 @@ public: int add_normal(EggVertex *egg_vertex, EggPrimitive *egg_prim); bool has_normals() const; + bool has_colors() const; + bool has_uvs() const; void make_mesh_data(Datagram &raw_data); void make_normal_data(Datagram &raw_data); + void make_color_data(Datagram &raw_data); + void make_uv_data(Datagram &raw_data); private: typedef pvector Vertices; @@ -67,6 +71,8 @@ private: UniqueNormals _unique_normals; bool _has_normals; + bool _has_colors; + bool _has_uvs; }; #endif diff --git a/pandatool/src/xfile/xFileVertex.cxx b/pandatool/src/xfile/xFileVertex.cxx index 19cc030d5c..b960dd5633 100644 --- a/pandatool/src/xfile/xFileVertex.cxx +++ b/pandatool/src/xfile/xFileVertex.cxx @@ -27,12 +27,15 @@ //////////////////////////////////////////////////////////////////// XFileVertex:: XFileVertex(EggVertex *egg_vertex, EggPrimitive *egg_prim) { + _has_color = true; + _has_uv = true; _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); + _has_uv = false; } if (egg_vertex->has_color()) { @@ -41,6 +44,7 @@ XFileVertex(EggVertex *egg_vertex, EggPrimitive *egg_prim) { _color = egg_prim->get_color(); } else { _color.set(1.0, 1.0, 1.0, 1.0); + _has_color = false; } } diff --git a/pandatool/src/xfile/xFileVertex.h b/pandatool/src/xfile/xFileVertex.h index cb003871d9..a8284c22c4 100644 --- a/pandatool/src/xfile/xFileVertex.h +++ b/pandatool/src/xfile/xFileVertex.h @@ -38,6 +38,8 @@ public: Vertexf _point; TexCoordf _uv; Colorf _color; + bool _has_color; + bool _has_uv; }; #endif