From 195a8d93ad146c3489abe00c12fc2dd0a99689be Mon Sep 17 00:00:00 2001 From: enn0x Date: Sun, 28 Mar 2010 14:44:16 +0000 Subject: [PATCH] Cloth meshes (descriptor, mesh, cooking, pool). --- panda/src/physx/Sources.pp | 6 + panda/src/physx/config_physx.cxx | 2 + panda/src/physx/physxClothMesh.I | 59 +++++++ panda/src/physx/physxClothMesh.cxx | 76 +++++++++ panda/src/physx/physxClothMesh.h | 75 +++++++++ panda/src/physx/physxClothMeshDesc.I | 61 +++++++ panda/src/physx/physxClothMeshDesc.cxx | 223 +++++++++++++++++++++++++ panda/src/physx/physxClothMeshDesc.h | 57 +++++++ panda/src/physx/physxKitchen.cxx | 46 +++++ panda/src/physx/physxKitchen.h | 4 + panda/src/physx/physxManager.cxx | 24 +++ panda/src/physx/physxManager.h | 5 + panda/src/physx/physxMeshPool.cxx | 89 +++++++++- panda/src/physx/physxMeshPool.h | 9 +- panda/src/physx/physx_composite.cxx | 7 +- 15 files changed, 730 insertions(+), 13 deletions(-) create mode 100644 panda/src/physx/physxClothMesh.I create mode 100644 panda/src/physx/physxClothMesh.cxx create mode 100644 panda/src/physx/physxClothMesh.h create mode 100644 panda/src/physx/physxClothMeshDesc.I create mode 100644 panda/src/physx/physxClothMeshDesc.cxx create mode 100644 panda/src/physx/physxClothMeshDesc.h diff --git a/panda/src/physx/Sources.pp b/panda/src/physx/Sources.pp index 2946a650cf..48ef1d968f 100644 --- a/panda/src/physx/Sources.pp +++ b/panda/src/physx/Sources.pp @@ -30,6 +30,8 @@ physxCapsuleForceFieldShapeDesc.I physxCapsuleForceFieldShapeDesc.h \ physxCapsuleShape.I physxCapsuleShape.h \ physxCapsuleShapeDesc.I physxCapsuleShapeDesc.h \ + physxClothMesh.I physxClothMesh.h \ + physxClothMeshDesc.I physxClothMeshDesc.h \ physxConstraintDominance.I physxConstraintDominance.h \ physxContactPair.I physxContactPair.h \ physxContactPoint.I physxContactPoint.h \ @@ -146,6 +148,8 @@ physxCapsuleForceFieldShapeDesc.cxx \ physxCapsuleShape.cxx \ physxCapsuleShapeDesc.cxx \ + physxClothMesh.cxx \ + physxClothMeshDesc.cxx \ physxConstraintDominance.cxx \ physxContactPair.cxx \ physxContactPoint.cxx \ @@ -262,6 +266,8 @@ physxCapsuleForceFieldShapeDesc.I physxCapsuleForceFieldShapeDesc.h \ physxCapsuleShape.I physxCapsuleShape.h \ physxCapsuleShapeDesc.I physxCapsuleShapeDesc.h \ + physxClothMesh.I physxClothMesh.h \ + physxClothMeshDesc.I physxClothMeshDesc.h \ physxConstraintDominance.I physxConstraintDominance.h \ physxContactPair.I physxContactPair.h \ physxContactPoint.I physxContactPoint.h \ diff --git a/panda/src/physx/config_physx.cxx b/panda/src/physx/config_physx.cxx index 7821a73f36..072145ca42 100644 --- a/panda/src/physx/config_physx.cxx +++ b/panda/src/physx/config_physx.cxx @@ -22,6 +22,7 @@ #include "physxCapsuleController.h" #include "physxCapsuleForceFieldShape.h" #include "physxCapsuleShape.h" +#include "physxClothMesh.h" #include "physxContactPair.h" #include "physxContactPoint.h" #include "physxController.h" @@ -117,6 +118,7 @@ init_libphysx() { PhysxCapsuleController::init_type(); PhysxCapsuleForceFieldShape::init_type(); PhysxCapsuleShape::init_type(); + PhysxClothMesh::init_type(); PhysxContactPair::init_type(); PhysxContactPoint::init_type(); PhysxController::init_type(); diff --git a/panda/src/physx/physxClothMesh.I b/panda/src/physx/physxClothMesh.I new file mode 100644 index 0000000000..754114655d --- /dev/null +++ b/panda/src/physx/physxClothMesh.I @@ -0,0 +1,59 @@ +// Filename: physxClothMesh.I +// Created by: enn0x (28Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMesh::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PhysxClothMesh:: +PhysxClothMesh() : PhysxObject() { + +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMesh::Destructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PhysxClothMesh:: +~PhysxClothMesh() { + +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMesh::ls +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void PhysxClothMesh:: +ls() const { + + ls(nout); +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMesh::ls +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void PhysxClothMesh:: +ls(ostream &out, int indent_level) const { + + indent(out, indent_level) << get_type().get_name() + << " (at 0x" << this << ")\n"; +} + diff --git a/panda/src/physx/physxClothMesh.cxx b/panda/src/physx/physxClothMesh.cxx new file mode 100644 index 0000000000..242bf75d5c --- /dev/null +++ b/panda/src/physx/physxClothMesh.cxx @@ -0,0 +1,76 @@ +// Filename: physxClothMesh.cxx +// Created by: enn0x (28Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "physxClothMesh.h" +#include "physxMeshPool.h" + +TypeHandle PhysxClothMesh::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMesh::link +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void PhysxClothMesh:: +link(NxClothMesh *meshPtr) { + + // Link self + PhysxManager::get_global_ptr()->_cloth_meshes.add(this); + _ptr = meshPtr; + _error_type = ET_ok; +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMesh::unlink +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void PhysxClothMesh:: +unlink() { + + // Unlink self + _error_type = ET_released; + PhysxManager::get_global_ptr()->_cloth_meshes.remove(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMesh::release +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void PhysxClothMesh:: +release() { + + nassertv(_error_type == ET_ok); + + unlink(); + NxGetPhysicsSDK()->releaseClothMesh(*_ptr); + _ptr = NULL; + + PhysxMeshPool::release_cloth_mesh(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMesh::get_reference_count +// Access: Published +// Description: Returns the reference count for shared meshes. +//////////////////////////////////////////////////////////////////// +unsigned int PhysxClothMesh:: +get_reference_count() const { + + nassertr(_error_type == ET_ok, 0); + + return _ptr->getReferenceCount(); +} + diff --git a/panda/src/physx/physxClothMesh.h b/panda/src/physx/physxClothMesh.h new file mode 100644 index 0000000000..bf96fa7ab5 --- /dev/null +++ b/panda/src/physx/physxClothMesh.h @@ -0,0 +1,75 @@ +// Filename: physxClothMesh.h +// Created by: enn0x (28Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef PHYSXCLOTHMESH_H +#define PHYSXCLOTHMESH_H + +#include "pandabase.h" + +#include "physxObject.h" +#include "physx_includes.h" + +//////////////////////////////////////////////////////////////////// +// Class : PhysxClothMesh +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDAPHYSX PhysxClothMesh : public PhysxObject { + +PUBLISHED: + unsigned int get_reference_count() const; + +//////////////////////////////////////////////////////////////////// +PUBLISHED: + void release(); + + INLINE void ls() const; + INLINE void ls(ostream &out, int indent_level=0) const; + +public: + INLINE PhysxClothMesh(); + INLINE ~PhysxClothMesh(); + + INLINE NxClothMesh *ptr() const { return _ptr; }; + + void link(NxClothMesh *meshPtr); + void unlink(); + +private: + NxClothMesh *_ptr; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + PhysxObject::init_type(); + register_type(_type_handle, "PhysxClothMesh", + PhysxObject::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "physxClothMesh.I" + +#endif // PHYSXCLOTHMESH_H diff --git a/panda/src/physx/physxClothMeshDesc.I b/panda/src/physx/physxClothMeshDesc.I new file mode 100644 index 0000000000..2c314ed016 --- /dev/null +++ b/panda/src/physx/physxClothMeshDesc.I @@ -0,0 +1,61 @@ +// Filename: physxClothMeshDesc.I +// Created by: enn0x (28Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMeshDesc::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PhysxClothMeshDesc:: +PhysxClothMeshDesc() { + + _desc.flags = 0; + _desc.pointStrideBytes = 5*sizeof(NxReal); + _desc.triangleStrideBytes = 3*sizeof(NxU32); + _desc.points = NULL; + _desc.triangles = NULL; + + _points = NULL; + _triangles = NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMeshDesc::Destructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PhysxClothMeshDesc:: +~PhysxClothMeshDesc() { + + if (_points) { + delete [] _points; + } + + if (_triangles) { + delete [] _triangles; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMeshDesc::is_valid +// Access: Published +// Description: Returns true if the descriptor is valid. +//////////////////////////////////////////////////////////////////// +INLINE bool PhysxClothMeshDesc:: +is_valid() const { + + return _desc.isValid(); +} + diff --git a/panda/src/physx/physxClothMeshDesc.cxx b/panda/src/physx/physxClothMeshDesc.cxx new file mode 100644 index 0000000000..b7aa3c4a91 --- /dev/null +++ b/panda/src/physx/physxClothMeshDesc.cxx @@ -0,0 +1,223 @@ +// Filename: physxClothMeshDesc.cxx +// Created by: enn0x (28Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "physxClothMeshDesc.h" +#include "physxManager.h" + +#include "nodePathCollection.h" +#include "geomNode.h" +#include "geomVertexReader.h" + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMeshDesc::set_num_vertices +// Access: Published +// Description: Sets the number of vertices to be stored within +// this triangle mesh. The function allocates memory +// for the vertices, but it does not set any vertices. +// +// This method must be called before any calls to +// set_vertex are done! +//////////////////////////////////////////////////////////////////// +void PhysxClothMeshDesc:: +set_num_vertices(unsigned int numVertices) { + + if (_desc.points) { + delete [] _points; + } + + _points = new NxReal[5 * numVertices]; + + _desc.numVertices = numVertices; + _desc.points = _points; +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMeshDesc::set_vertex +// Access: Published +// Description: Sets a single vertex. You have to call the function +// set_num_vertices before you can call this function. +//////////////////////////////////////////////////////////////////// +void PhysxClothMeshDesc:: +set_vertex(unsigned int idx, const LPoint3f &vert, const LPoint2f &texcoord) { + + nassertv(_desc.numVertices > idx); + + idx = 5 * idx; + _points[idx] = vert.get_x(); + _points[idx + 1] = vert.get_y(); + _points[idx + 2] = vert.get_z(); + _points[idx + 3] = texcoord.get_x(); + _points[idx + 4] = texcoord.get_y(); +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMeshDesc::set_num_triangles +// Access: Published +// Description: Sets the number of triangles to be stored in this +// triangle mesh. +// +// This method must be called before any calls to +// set_triangle are done! +//////////////////////////////////////////////////////////////////// +void PhysxClothMeshDesc:: +set_num_triangles(unsigned int numTriangles) { + + if (_desc.triangles) { + delete [] _triangles; + } + + _triangles = new NxU32[3 * numTriangles]; + + _desc.numTriangles = numTriangles; + _desc.triangles = _triangles; +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMeshDesc::set_triangles +// Access: Published +// Description: Sets a single triangle, by providing the three +// indices i1, i2, i3. +//////////////////////////////////////////////////////////////////// +void PhysxClothMeshDesc:: +set_triangle(unsigned int idx, + unsigned int i1, unsigned int i2, unsigned int i3) { + + nassertv(_desc.numTriangles > idx); + + idx = 3 * idx; + _triangles[idx] = i1; + _triangles[idx + 1] = i2; + _triangles[idx + 2] = i3; +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMeshDesc::get_desc +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +const NxClothMeshDesc &PhysxClothMeshDesc:: +get_desc() const { + + return _desc; +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxClothMeshDesc::set_from_node_path +// Access: Published +// Description: A convenience method to set the mesh data from +// a NodePath in a single call. The method iterates +// over the NodePath geoms and collects data for +// the triangle mesh. +// +// Do not use the following function when using this +// one: +// - set_num_vertices +// - set_vertex +// - set_num_triangles +// - set_triangle +//////////////////////////////////////////////////////////////////// +void PhysxClothMeshDesc:: +set_from_node_path(const NodePath &np) { + + pvector dataVertices; + pvector dataTexcoords; + pvector dataIndices; + + // Collect data from NodePath + NodePathCollection npc = np.find_all_matches( "**/+GeomNode" ); + for (int i=0; iget_num_geoms(); j++) { + CPT(Geom) geom = gnode->get_geom(j); + CPT(GeomVertexData) vdata = geom->get_vertex_data(); + GeomVertexReader reader; + + // Vertices + reader = GeomVertexReader(vdata, InternalName::get_vertex()); + while (!reader.is_at_end()) { + dataVertices.push_back(reader.get_data3f()); + } + + // Texcoords + reader = GeomVertexReader(vdata, InternalName::get_texcoord()); + while (!reader.is_at_end()) { + dataTexcoords.push_back(reader.get_data2f()); + } + + // Indices + for (int k=0; kget_num_primitives(); k++) { + + CPT(GeomPrimitive) prim = geom->get_primitive(k); + prim = prim->decompose(); + + for (int l=0; lget_num_primitives(); l++) { + + int s = prim->get_primitive_start(l); + int e = prim->get_primitive_end(l); + + for (int l=s; lget_vertex(l)); + } + } + } + } + } + + // Set descriptor members + int i; + + NxU32 numVertices = dataVertices.size(); + NxU32 numTriangles = dataIndices.size() / 3; + + _points = new NxReal[5 * numVertices]; + _triangles = new NxU32[3 * numTriangles]; + + i = 0; + pvector::const_iterator vit; + for (vit=dataVertices.begin(); vit!=dataVertices.end(); vit++) { + LPoint3f v = *vit; + + _points[5*i] = v.get_x(); + _points[5*i+1] = v.get_y(); + _points[5*i+2] = v.get_z(); + i++; + } + + i = 0; + pvector::const_iterator tcit; + for (tcit=dataTexcoords.begin(); tcit!=dataTexcoords.end(); tcit++) { + LPoint2f tc = *tcit; + + _points[5*i+3] = tc.get_x(); + _points[5*i+4] = tc.get_y(); + i++; + } + + i = 0; + pvector::const_iterator iit; + for(iit=dataIndices.begin(); iit!=dataIndices.end(); iit++) { + NxU32 idx = *iit; + + _triangles[i] = idx; + i++; + } + + _desc.numVertices = numVertices; + _desc.points = _points; + _desc.numTriangles = numTriangles; + _desc.triangles = _triangles; +} + diff --git a/panda/src/physx/physxClothMeshDesc.h b/panda/src/physx/physxClothMeshDesc.h new file mode 100644 index 0000000000..536170578a --- /dev/null +++ b/panda/src/physx/physxClothMeshDesc.h @@ -0,0 +1,57 @@ +// Filename: physxClothMeshDesc.h +// Created by: enn0x (28Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef PHYSXCLOTHMESHDESC_H +#define PHYSXCLOTHMESHDESC_H + +#include "pandabase.h" +#include "lpoint3.h" +#include "lpoint2.h" +#include "nodePath.h" + +#include "physx_includes.h" + +//////////////////////////////////////////////////////////////////// +// Class : PhysxClothMeshDesc +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDAPHYSX PhysxClothMeshDesc { + +PUBLISHED: + INLINE PhysxClothMeshDesc(); + INLINE ~PhysxClothMeshDesc(); + + INLINE bool is_valid() const; + + void set_num_vertices(unsigned int n); + void set_vertex(unsigned int idx, const LPoint3f &vert, const LPoint2f &texcoord); + + void set_num_triangles(unsigned int n); + void set_triangle(unsigned int idx, + unsigned int i1, unsigned int i2, unsigned int i3); + + void set_from_node_path(const NodePath &np); + +public: + const NxClothMeshDesc &get_desc() const; + +private: + NxReal *_points; + NxU32 *_triangles; + NxClothMeshDesc _desc; +}; + +#include "physxClothMeshDesc.I" + +#endif // PHYSXCLOTHMESHDESC_H diff --git a/panda/src/physx/physxKitchen.cxx b/panda/src/physx/physxKitchen.cxx index df8afc8752..1472abf1bb 100644 --- a/panda/src/physx/physxKitchen.cxx +++ b/panda/src/physx/physxKitchen.cxx @@ -17,6 +17,8 @@ #include "physxConvexMeshDesc.h" #include "physxTriangleMesh.h" #include "physxTriangleMeshDesc.h" +#include "physxClothMesh.h" +#include "physxClothMeshDesc.h" #include "physxFileStream.h" #include "physxMemoryReadBuffer.h" #include "physxMemoryWriteBuffer.h" @@ -83,6 +85,22 @@ cook_triangle_mesh(const PhysxTriangleMeshDesc &meshDesc, const Filename &filena return _cooking->NxCookTriangleMesh(meshDesc.get_desc(), stream); } +//////////////////////////////////////////////////////////////////// +// Function: PhysxKitchen::cook_cloth_mesh +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool PhysxKitchen:: +cook_cloth_mesh(const PhysxClothMeshDesc &meshDesc, const Filename &filename) { + + nassertr_always(!filename.empty(), false); + nassertr_always(filename.touch(), false); + nassertr_always(meshDesc.is_valid(), false); + + PhysxFileStream stream = PhysxFileStream(filename, false); + return _cooking->NxCookClothMesh(meshDesc.get_desc(), stream); +} + //////////////////////////////////////////////////////////////////// // Function: PhysxKitchen::cook_convex_mesh // Access: Published @@ -139,3 +157,31 @@ cook_triangle_mesh(const PhysxTriangleMeshDesc &meshDesc) { return mesh; } +//////////////////////////////////////////////////////////////////// +// Function: PhysxKitchen::cook_cloth_mesh +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +PhysxClothMesh *PhysxKitchen:: +cook_cloth_mesh(const PhysxClothMeshDesc &meshDesc) { + + nassertr_always(meshDesc.is_valid(), false); + + PhysxMemoryWriteBuffer buffer; + bool status = _cooking->NxCookClothMesh(meshDesc.get_desc(), buffer); + nassertr(status, NULL); + + NxPhysicsSDK *sdk = NxGetPhysicsSDK(); + nassertr(sdk, NULL); + + PhysxClothMesh *mesh = new PhysxClothMesh(); + nassertr(mesh, NULL); + + NxClothMesh *meshPtr = sdk->createClothMesh(PhysxMemoryReadBuffer(buffer.data)); + nassertr(meshPtr, NULL); + + mesh->link(meshPtr); + + return mesh; +} + diff --git a/panda/src/physx/physxKitchen.h b/panda/src/physx/physxKitchen.h index f7090a93ce..bc601f1754 100644 --- a/panda/src/physx/physxKitchen.h +++ b/panda/src/physx/physxKitchen.h @@ -24,6 +24,8 @@ class PhysxConvexMesh; class PhysxConvexMeshDesc; class PhysxTriangleMesh; class PhysxTriangleMeshDesc; +class PhysxClothMesh; +class PhysxClothMeshDesc; //////////////////////////////////////////////////////////////////// // Class : PhysxKitchen @@ -39,9 +41,11 @@ PUBLISHED: bool cook_convex_mesh(const PhysxConvexMeshDesc &meshDesc, const Filename &filename); bool cook_triangle_mesh(const PhysxTriangleMeshDesc &meshDesc, const Filename &filename); + bool cook_cloth_mesh(const PhysxClothMeshDesc &meshDesc, const Filename &filename); PhysxConvexMesh *cook_convex_mesh(const PhysxConvexMeshDesc &meshDesc); PhysxTriangleMesh *cook_triangle_mesh(const PhysxTriangleMeshDesc &meshDesc); + PhysxClothMesh *cook_cloth_mesh(const PhysxClothMeshDesc &meshDesc); private: NxCookingInterface *_cooking; diff --git a/panda/src/physx/physxManager.cxx b/panda/src/physx/physxManager.cxx index 254a10036f..b775962379 100644 --- a/panda/src/physx/physxManager.cxx +++ b/panda/src/physx/physxManager.cxx @@ -271,6 +271,30 @@ get_triangle_mesh(unsigned int idx) { return (PhysxTriangleMesh *)_triangle_meshes[idx]; } +//////////////////////////////////////////////////////////////////// +// Function: PhysxManager::get_num_cloth_meshes +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +unsigned int PhysxManager:: +get_num_cloth_meshes() { + + return _sdk->getNbClothMeshes(); +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxManager::get_cloth_mesh +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +PhysxClothMesh *PhysxManager:: +get_cloth_mesh(unsigned int idx) { + + nassertr_always(idx < _sdk->getNbClothMeshes(), NULL); + + return (PhysxClothMesh *)_cloth_meshes[idx]; +} + //////////////////////////////////////////////////////////////////// // Function: PhysxManager::is_hardware_available // Access: Published diff --git a/panda/src/physx/physxManager.h b/panda/src/physx/physxManager.h index 4eb7bccf03..ae7fb5b936 100644 --- a/panda/src/physx/physxManager.h +++ b/panda/src/physx/physxManager.h @@ -71,6 +71,10 @@ PUBLISHED: PhysxTriangleMesh *get_triangle_mesh(unsigned int idx); MAKE_SEQ(get_triangle_meshes, get_num_triangle_meshes, get_triangle_mesh); + unsigned int get_num_cloth_meshes(); + PhysxClothMesh *get_cloth_mesh(unsigned int idx); + MAKE_SEQ(get_cloth_meshes, get_num_cloth_meshes, get_cloth_mesh); + INLINE void ls() const; INLINE void ls(ostream &out, int indent_level=0) const; @@ -81,6 +85,7 @@ public: PhysxObjectCollection _heightfields; PhysxObjectCollection _convex_meshes; PhysxObjectCollection _triangle_meshes; + PhysxObjectCollection _cloth_meshes; INLINE static NxVec3 vec3_to_nxVec3(const LVector3f &v); INLINE static LVector3f nxVec3_to_vec3(const NxVec3 &v); diff --git a/panda/src/physx/physxMeshPool.cxx b/panda/src/physx/physxMeshPool.cxx index a278ca8686..301fc46573 100644 --- a/panda/src/physx/physxMeshPool.cxx +++ b/panda/src/physx/physxMeshPool.cxx @@ -15,12 +15,13 @@ #include "physxMeshPool.h" #include "physxConvexMesh.h" #include "physxTriangleMesh.h" +#include "physxClothMesh.h" #include "physxFileStream.h" #include "virtualFileSystem.h" PhysxMeshPool::ConvexMeshes PhysxMeshPool::_convex_meshes; PhysxMeshPool::TriangleMeshes PhysxMeshPool::_triangle_meshes; -//PhysxMeshPool::ClothMeshes PhysxMeshPool::_cloth_meshes; +PhysxMeshPool::ClothMeshes PhysxMeshPool::_cloth_meshes; //PhysxMeshPool::SoftbodyMeshes PhysxMeshPool::_softbody_meshes; //////////////////////////////////////////////////////////////////// @@ -122,6 +123,45 @@ load_triangle_mesh(const Filename &fn) { return mesh; } +//////////////////////////////////////////////////////////////////// +// Function: PhysxMeshPool::load_cloth_mesh +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +PhysxClothMesh *PhysxMeshPool:: +load_cloth_mesh(const Filename &fn) { + + if (!check_filename(fn)) return NULL; + + PhysxClothMesh *mesh; + + ClothMeshes::iterator it = _cloth_meshes.find(fn); + if (it == _cloth_meshes.end()) { + // Not found; load mesh. + NxClothMesh *meshPtr; + PhysxFileStream stream = PhysxFileStream(fn, true); + + mesh = new PhysxClothMesh(); + nassertr_always(mesh, NULL); + + NxPhysicsSDK *sdk = NxGetPhysicsSDK(); + nassertr_always(sdk, NULL); + + meshPtr = sdk->createClothMesh(stream); + nassertr_always(meshPtr, NULL); + + mesh->link(meshPtr); + + _cloth_meshes.insert(ClothMeshes::value_type(fn, mesh)); + } + else { + // Found; return previously loaded mesh. + mesh = (*it).second; + } + + return mesh; +} + //////////////////////////////////////////////////////////////////// // Function: PhysxMeshPool::release_convex_mesh // Access: Published @@ -161,13 +201,22 @@ release_triangle_mesh(PhysxTriangleMesh *mesh) { } //////////////////////////////////////////////////////////////////// -// Function: PhysxMeshPool::list_content +// Function: PhysxMeshPool::release_cloth_mesh // Access: Published -// Description: +// Description: //////////////////////////////////////////////////////////////////// -void PhysxMeshPool:: -list_contents() { - list_contents( nout ); +bool PhysxMeshPool:: +release_cloth_mesh(PhysxClothMesh *mesh) { + + ClothMeshes::iterator it; + for (it=_cloth_meshes.begin(); it != _cloth_meshes.end(); ++it) { + if (mesh == (*it).second) { + _cloth_meshes.erase(it); + return true; + } + } + + return false; } //////////////////////////////////////////////////////////////////// @@ -176,7 +225,17 @@ list_contents() { // Description: //////////////////////////////////////////////////////////////////// void PhysxMeshPool:: -list_contents( ostream &out ) { +list_contents() { + list_contents(nout); +} + +//////////////////////////////////////////////////////////////////// +// Function: PhysxMeshPool::list_content +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void PhysxMeshPool:: +list_contents(ostream &out) { out << "PhysX mesh pool contents:\n"; @@ -206,6 +265,19 @@ list_contents( ostream &out ) { } } + // Cloth meshes + { + ClothMeshes::const_iterator it; + for (it=_cloth_meshes.begin(); it != _cloth_meshes.end(); ++it) { + Filename fn = (*it).first; + PhysxClothMesh *mesh = (*it).second; + + out << " " << fn.get_fullpath() + << " (cloth mesh, " << mesh->ptr()->getReferenceCount() + << " references)\n"; + } + } + // Summary NxPhysicsSDK *sdk = NxGetPhysicsSDK(); @@ -214,5 +286,8 @@ list_contents( ostream &out ) { out << " Total number of triangle meshes: " << sdk->getNbTriangleMeshes() << " created, " << _triangle_meshes.size() << " registred\n"; + + out << " Total number of cloth meshes: " << sdk->getNbClothMeshes() + << " created, " << _cloth_meshes.size() << " registred\n"; } diff --git a/panda/src/physx/physxMeshPool.h b/panda/src/physx/physxMeshPool.h index ae27c7fc0e..abd9022405 100644 --- a/panda/src/physx/physxMeshPool.h +++ b/panda/src/physx/physxMeshPool.h @@ -25,6 +25,7 @@ class PhysxConvexMesh; class PhysxTriangleMesh; +class PhysxClothMesh; //////////////////////////////////////////////////////////////////// // Class : PhysxMeshPool @@ -42,9 +43,13 @@ PUBLISHED: static PhysxConvexMesh *load_convex_mesh(const Filename &filename); static PhysxTriangleMesh *load_triangle_mesh(const Filename &filename); + static PhysxClothMesh *load_cloth_mesh(const Filename &filename); + //static PhysxSoftBodyMesh *load_softbody_mesh(const Filename &filename); static bool release_convex_mesh(PhysxConvexMesh *mesh); static bool release_triangle_mesh(PhysxTriangleMesh *mesh); + static bool release_cloth_mesh(PhysxClothMesh *mesh); + //static bool release_softbody_mesh(PhysxSoftBodyMesh *mesh); static void list_contents(); static void list_contents(ostream &out); @@ -54,12 +59,12 @@ private: typedef pmap ConvexMeshes; typedef pmap TriangleMeshes; - //typedef pmap ClothMeshes; + typedef pmap ClothMeshes; //typedef pmap SoftbodyMeshes; static ConvexMeshes _convex_meshes; static TriangleMeshes _triangle_meshes; - //static ClothMeshes _cloth_meshes; + static ClothMeshes _cloth_meshes; //static SoftbodyMeshes _softbody_meshes; }; diff --git a/panda/src/physx/physx_composite.cxx b/panda/src/physx/physx_composite.cxx index c0cd07946a..fd7fa4163d 100644 --- a/panda/src/physx/physx_composite.cxx +++ b/panda/src/physx/physx_composite.cxx @@ -108,11 +108,10 @@ #include "physxUtilLib.cxx" #include "physxVehicle.cxx" #include "physxVehicleDesc.cxx" -//#include "physxVehicleGears.cxx" -//#include "physxVehicleGearsDesc.cxx" -//#include "physxVehicleMotor.cxx" -//#include "physxVehicleMotorDesc.cxx" #include "physxWheel.cxx" #include "physxWheelDesc.cxx" #include "physxWheelShape.cxx" #include "physxWheelShapeDesc.cxx" + +#include "physxClothMesh.cxx" +#include "physxClothMeshDesc.cxx"