From de91e1c3a8f2c8f3ca102c69c6c39b18737bd70f Mon Sep 17 00:00:00 2001 From: Uwe Woessner Date: Tue, 14 Dec 2021 13:29:20 +0100 Subject: [PATCH] compile with physx 4 (no support for particles any more) --- physics/Engine.cpp | 7 +++++++ physics/Engine.h | 5 ++++- physics/ParticleUpdater.cpp | 3 ++- physics/ParticleUpdater.h | 3 ++- physics/PhysicsUtil.cpp | 14 +++++++++++++- physics/PhysicsUtil.h | 14 ++++++++++++-- physics/Vehicle.cpp | 12 ++++++++++++ physics/VehicleManager.cpp | 2 ++ tests/physics_vehicle_test.cpp | 6 ++++++ 9 files changed, 60 insertions(+), 6 deletions(-) diff --git a/physics/Engine.cpp b/physics/Engine.cpp index 6b74e5e..ea3f61e 100644 --- a/physics/Engine.cpp +++ b/physics/Engine.cpp @@ -29,7 +29,12 @@ Engine* Engine::instance() Engine::Engine() : _cooking(NULL), _cudaManager(NULL), _pvdTransport(NULL), _pvd(NULL) { +#if (PX_PHYSICS_VERSION_MAJOR > 3) + PxFoundation* foundation = PxCreateFoundation(PX_PHYSICS_VERSION, defaultAllocator, errorHandler); +#else PxFoundation* foundation = PxCreateFoundation(PX_FOUNDATION_VERSION, defaultAllocator, errorHandler); +#endif + if (!foundation) { OSG_WARN << "Unable to initialize PhysX foundation." << std::endl; @@ -127,6 +132,7 @@ bool Engine::addActor(const std::string& s, PxRigidActor* actor, const PxFilterD return false; } +#if !(PX_PHYSICS_VERSION_MAJOR > 3) bool Engine::addActor(const std::string& s, physx::PxParticleBase* ps, const physx::PxFilterData& filter) { if (addActor(s, ps)) @@ -137,6 +143,7 @@ bool Engine::addActor(const std::string& s, physx::PxParticleBase* ps, const phy else return false; } +#endif bool Engine::removeActor(const std::string& s, PxActor* actor) { diff --git a/physics/Engine.h b/physics/Engine.h index a25ed6a..5d3166a 100644 --- a/physics/Engine.h +++ b/physics/Engine.h @@ -5,9 +5,10 @@ #include #include #include +#include #include -#if PX_PHYSICS_VERSION_MAJOR==3 && PX_PHYSICS_VERSION_MINOR==4 +#if PX_PHYSICS_VERSION_MAJOR==3 && PX_PHYSICS_VERSION_MINOR==4 || PX_PHYSICS_VERSION_MAJOR > 3 // #else # error "Unsupport PhysX version" @@ -43,7 +44,9 @@ namespace osgPhysics /** Add actor object to specified scene */ bool addActor(const std::string& scene, physx::PxActor* actor); bool addActor(const std::string& scene, physx::PxRigidActor* actor, const physx::PxFilterData& filter); +#if !(PX_PHYSICS_VERSION_MAJOR > 3) bool addActor(const std::string& scene, physx::PxParticleBase* ps, const physx::PxFilterData& filter); +#endif bool removeActor(const std::string& scene, physx::PxActor* actor); typedef std::vector ActorList; diff --git a/physics/ParticleUpdater.cpp b/physics/ParticleUpdater.cpp index 15f4098..1c04c88 100644 --- a/physics/ParticleUpdater.cpp +++ b/physics/ParticleUpdater.cpp @@ -4,6 +4,7 @@ #include #include +#if !(PX_PHYSICS_VERSION_MAJOR > 3) #include #pragma intrinsic(_BitScanForward) #pragma intrinsic(_BitScanReverse) @@ -420,5 +421,5 @@ void NativeParticleOperator::operateParticles(osgParticle::ParticleSystem* ps, d // FIXME: are unhandled particles all dead? } } - +#endif #endif diff --git a/physics/ParticleUpdater.h b/physics/ParticleUpdater.h index 6896b74..95306af 100644 --- a/physics/ParticleUpdater.h +++ b/physics/ParticleUpdater.h @@ -11,6 +11,7 @@ #include #include "Engine.h" +#if !(PX_PHYSICS_VERSION_MAJOR > 3) namespace osgPhysics { @@ -187,5 +188,5 @@ namespace osgPhysics #endif } - +#endif #endif diff --git a/physics/PhysicsUtil.cpp b/physics/PhysicsUtil.cpp index b94a1ec..bb60b9b 100644 --- a/physics/PhysicsUtil.cpp +++ b/physics/PhysicsUtil.cpp @@ -224,7 +224,9 @@ namespace osgPhysics heightFieldDesc.nbColumns = numColumns; heightFieldDesc.nbRows = numRows; heightFieldDesc.format = PxHeightFieldFormat::eS16_TM; +#if !(PX_PHYSICS_VERSION_MAJOR > 3) heightFieldDesc.thickness = thickness; +#endif PxHeightFieldSample* samplesData = (PxHeightFieldSample*)malloc( sizeof(PxHeightFieldSample) * numColumns * numRows); @@ -322,6 +324,7 @@ namespace osgPhysics return createTriangleMesh(verts, indices); } +#if !(PX_PHYSICS_VERSION_MAJOR > 3) PxClothFabric* createClothFabric(const std::vector& verts, const std::vector& indices, const osg::Vec3& gravity) { @@ -336,7 +339,7 @@ namespace osgPhysics PxVec3 g(gravity[0], gravity[1], gravity[2]); return PxClothFabricCreate(*SDK_OBJ, meshDesc, g); } - +#endif PxScene* createScene(const osg::Vec3& gravity, const PxSimulationFilterShader& filter, physx::PxSceneFlags flags, unsigned int numThreads, bool useGPU) { @@ -348,10 +351,19 @@ namespace osgPhysics if (useGPU) { PxCudaContextManager* cudaManager = Engine::instance()->getOrCreateCudaContextManager(); +#if (PX_PHYSICS_VERSION_MAJOR > 3) + if (cudaManager) sceneDesc.cudaContextManager = cudaManager; +#else if (cudaManager) sceneDesc.gpuDispatcher = cudaManager->getGpuDispatcher(); +#endif + } +#if (PX_PHYSICS_VERSION_MAJOR > 3) + if (!sceneDesc.cudaContextManager && !sceneDesc.cpuDispatcher) +#else if (!sceneDesc.gpuDispatcher && !sceneDesc.cpuDispatcher) +#endif { PxDefaultCpuDispatcher* defCpuDispatcher = PxDefaultCpuDispatcherCreate(numThreads); if (!defCpuDispatcher) diff --git a/physics/PhysicsUtil.h b/physics/PhysicsUtil.h index b191dc4..6b32ff8 100644 --- a/physics/PhysicsUtil.h +++ b/physics/PhysicsUtil.h @@ -25,12 +25,20 @@ namespace osgPhysics /** Cook and create new convex mesh */ extern physx::PxConvexMesh* createConvexMesh( const std::vector& verts, - physx::PxConvexFlags flags = physx::PxConvexFlag::eCOMPUTE_CONVEX | physx::PxConvexFlag::eINFLATE_CONVEX); + physx::PxConvexFlags flags = physx::PxConvexFlag::eCOMPUTE_CONVEX +#if !(PX_PHYSICS_VERSION_MAJOR > 3) + | physx::PxConvexFlag::eINFLATE_CONVEX +#endif + ); /** Cook and create new convex mesh from node */ extern physx::PxConvexMesh* createConvexMesh( osg::Node& node, - physx::PxConvexFlags flags = physx::PxConvexFlag::eCOMPUTE_CONVEX | physx::PxConvexFlag::eINFLATE_CONVEX); + physx::PxConvexFlags flags = physx::PxConvexFlag::eCOMPUTE_CONVEX +#if !(PX_PHYSICS_VERSION_MAJOR > 3) + | physx::PxConvexFlag::eINFLATE_CONVEX +#endif + ); /** Cook and create new height field, where lowerTriangleData & upperTriangleData contain material index data and hole flag of the lower/upper triangle of each height field cell @@ -53,9 +61,11 @@ namespace osgPhysics extern physx::PxTriangleMesh* createTriangleMesh(osg::Node& node); /** Cook and create new physics cloth fabric */ +#if !(PX_PHYSICS_VERSION_MAJOR > 3) extern physx::PxClothFabric* createClothFabric(const std::vector& verts, const std::vector& indices, const osg::Vec3& gravity); +#endif /** Create a physics scene */ extern physx::PxScene* createScene(const osg::Vec3& gravity, diff --git a/physics/Vehicle.cpp b/physics/Vehicle.cpp index 2557b07..4b43f9b 100644 --- a/physics/Vehicle.cpp +++ b/physics/Vehicle.cpp @@ -262,7 +262,14 @@ physx::PxRigidDynamic* WheeledVehicle::createActorAndFilters( PxMaterial* defMaterial = VehicleManager::instance()->getSurfaceMaterial(VehicleManager::SURFACE_TARMAC); for (int i = 0; i < numWheels; ++i) { + +#if (PX_PHYSICS_VERSION_MAJOR > 3) + _wheelShapes[i] = PxRigidActorExt::createExclusiveShape(*actor, *(wheelGeometries[i]), wheelMtl ? *wheelMtl : *defMaterial); + actor->attachShape(*_wheelShapes[i]); +#else _wheelShapes[i] = actor->createShape(*(wheelGeometries[i]), wheelMtl ? *wheelMtl : *defMaterial); +#endif + _wheelShapes[i]->setLocalPose(PxTransform(PxIdentity)); // Create a query filter data for the car to ensure that cars do not attempt to drive on themselves @@ -271,7 +278,12 @@ physx::PxRigidDynamic* WheeledVehicle::createActorAndFilters( } // Must first create wheels and then chassis because PhysX will treat the first shape as the wheel +#if (PX_PHYSICS_VERSION_MAJOR > 3) + _chassisShape = PxRigidActorExt::createExclusiveShape(*actor, PxConvexMeshGeometry(chassis.mesh), chassisMtl ? *chassisMtl : *defMaterial); + actor->attachShape(*_chassisShape); +#else _chassisShape = actor->createShape(PxConvexMeshGeometry(chassis.mesh), chassisMtl ? *chassisMtl : *defMaterial); +#endif _chassisShape->setLocalPose(PxTransform(PxIdentity)); VehicleManager::createFilter(VehicleManager::FILTER_UNDRIVABLE_SURFACE, _chassisShape); VehicleManager::createFilter(VehicleManager::FILTER_CHASSIS, _chassisShape); diff --git a/physics/VehicleManager.cpp b/physics/VehicleManager.cpp index 7c254b7..2d0d9c3 100644 --- a/physics/VehicleManager.cpp +++ b/physics/VehicleManager.cpp @@ -171,6 +171,7 @@ bool VehicleManager::addActor(const std::string& scene, PxActor* actor, } else if (actor) { +#if !(PX_PHYSICS_VERSION_MAJOR > 3) PxParticleBase* particleBase = actor->is(); if (particleBase) { @@ -179,6 +180,7 @@ bool VehicleManager::addActor(const std::string& scene, PxActor* actor, filter.word1 = COLLISION_FLAG_OBSTACLE_AGAINST; particleBase->setSimulationFilterData(filter); } +#endif } return osgPhysics::Engine::instance()->addActor(scene, actor); } diff --git a/tests/physics_vehicle_test.cpp b/tests/physics_vehicle_test.cpp index 5d3edda..fc21751 100644 --- a/tests/physics_vehicle_test.cpp +++ b/tests/physics_vehicle_test.cpp @@ -182,6 +182,7 @@ osg::Group* createCar(osgPhysicsUtils::InputManager* manager, osgPhysics::Update return car.release(); } +#if !(PX_PHYSICS_VERSION_MAJOR > 3) osgParticle::ParticleSystem* createParticleSystem(osg::Group* parent) { // Set up the particle appearance @@ -241,6 +242,7 @@ osgParticle::ParticleSystem* createParticleSystem(osg::Group* parent) parent->addChild(geode.get()); return ps.get(); } +#endif int main(int argc, char** argv) { @@ -289,9 +291,11 @@ int main(int argc, char** argv) osg::ref_ptr particleGroup = new osg::MatrixTransform; particleGroup->setMatrix(osg::Matrix::translate(initialPosition + osg::Z_AXIS * 5.0f)); +#if !(PX_PHYSICS_VERSION_MAJOR > 3) osgParticle::ParticleSystem* ps = createParticleSystem(particleGroup.get()); osg::ref_ptr particleUpdater = new osgParticle::ParticleSystemUpdater; particleUpdater->addParticleSystem(ps); +#endif // Build the scene graph osg::ref_ptr root = new osg::Group; @@ -299,7 +303,9 @@ int main(int argc, char** argv) root->addChild(terrain.get()); root->addChild(vehicle.get()); root->addChild(ball.get()); +#if !(PX_PHYSICS_VERSION_MAJOR > 3) root->addChild(particleUpdater.get()); +#endif root->addChild(particleGroup.get()); // Start the viewer