diff options
| author | sschirm <[email protected]> | 2016-12-23 14:20:36 +0100 |
|---|---|---|
| committer | sschirm <[email protected]> | 2016-12-23 14:56:17 +0100 |
| commit | ef6937e69e8ee3f409cf9d460d5ad300a65d5924 (patch) | |
| tree | 710426e8daa605551ce3f34b581897011101c30f /PhysX_3.4/Snippets/SnippetTriangleMeshCreate/SnippetTriangleMeshCreate.cpp | |
| parent | Initial commit: (diff) | |
| download | physx-3.4-ef6937e69e8ee3f409cf9d460d5ad300a65d5924.tar.xz physx-3.4-ef6937e69e8ee3f409cf9d460d5ad300a65d5924.zip | |
PhysX 3.4 / APEX 1.4 release candidate @21506124
Diffstat (limited to 'PhysX_3.4/Snippets/SnippetTriangleMeshCreate/SnippetTriangleMeshCreate.cpp')
| -rw-r--r-- | PhysX_3.4/Snippets/SnippetTriangleMeshCreate/SnippetTriangleMeshCreate.cpp | 199 |
1 files changed, 165 insertions, 34 deletions
diff --git a/PhysX_3.4/Snippets/SnippetTriangleMeshCreate/SnippetTriangleMeshCreate.cpp b/PhysX_3.4/Snippets/SnippetTriangleMeshCreate/SnippetTriangleMeshCreate.cpp index c9ab3ee5..3c96bd24 100644 --- a/PhysX_3.4/Snippets/SnippetTriangleMeshCreate/SnippetTriangleMeshCreate.cpp +++ b/PhysX_3.4/Snippets/SnippetTriangleMeshCreate/SnippetTriangleMeshCreate.cpp @@ -112,23 +112,10 @@ void createRandomTerrain(const PxVec3& origin, PxU32 numRows, PxU32 numColumns, } } -// Creates a triangle mesh with different settings. -template <bool skipMeshCleanup, bool skipEdgeData, bool inserted, bool cookingPerformance> -void createTriangleMesh(PxU32 numVertices, const PxVec3* vertices, PxU32 numTriangles, const PxU32* indices) +// Setup common cooking params +void setupCommonCookingParams(PxCookingParams& params, bool skipMeshCleanup, bool skipEdgeData) { - PxU64 startTime = SnippetUtils::getCurrentTimeCounterValue(); - - PxTriangleMeshDesc meshDesc; - meshDesc.points.count = numVertices; - meshDesc.points.data = vertices; - meshDesc.points.stride = sizeof(PxVec3); - meshDesc.triangles.count = numTriangles; - meshDesc.triangles.data = indices; - meshDesc.triangles.stride = 3 * sizeof(PxU32); - - PxCookingParams params = gCooking->getParams(); - params.midphaseDesc = PxMeshMidPhase::eBVH33; - // we suppress the triangle mesh remap table computation to gain some speed, as we will not need it + // we suppress the triangle mesh remap table computation to gain some speed, as we will not need it // in this snippet params.suppressTriangleMeshRemapTable = true; @@ -150,14 +137,46 @@ void createTriangleMesh(PxU32 numVertices, const PxVec3* vertices, PxU32 numTria params.meshPreprocessParams &= ~static_cast<PxMeshPreprocessingFlags>(PxMeshPreprocessingFlag::eDISABLE_ACTIVE_EDGES_PRECOMPUTE); else params.meshPreprocessParams |= PxMeshPreprocessingFlag::eDISABLE_ACTIVE_EDGES_PRECOMPUTE; +} + +// Creates a triangle mesh using BVH33 midphase with different settings. +void createBV33TriangleMesh(PxU32 numVertices, const PxVec3* vertices, PxU32 numTriangles, const PxU32* indices, + bool skipMeshCleanup, bool skipEdgeData, bool inserted, bool cookingPerformance, bool meshSizePerfTradeoff) +{ + PxU64 startTime = SnippetUtils::getCurrentTimeCounterValue(); + + PxTriangleMeshDesc meshDesc; + meshDesc.points.count = numVertices; + meshDesc.points.data = vertices; + meshDesc.points.stride = sizeof(PxVec3); + meshDesc.triangles.count = numTriangles; + meshDesc.triangles.data = indices; + meshDesc.triangles.stride = 3 * sizeof(PxU32); + + PxCookingParams params = gCooking->getParams(); + + // Create BVH33 midphase + params.midphaseDesc = PxMeshMidPhase::eBVH33; - // The COOKING_PERFORMANCE flag for BVH33 midphase enables a fast cooking path at the expense of somewhat lower quality BVH construction. - if(params.midphaseDesc.getType() == PxMeshMidPhase::eBVH33) - { - if (cookingPerformance) - params.midphaseDesc.mBVH33Desc.meshCookingHint = PxMeshCookingHint::eCOOKING_PERFORMANCE; - else - params.midphaseDesc.mBVH33Desc.meshCookingHint = PxMeshCookingHint::eSIM_PERFORMANCE; + // setup common cooking params + setupCommonCookingParams(params, skipMeshCleanup, skipEdgeData); + + // The COOKING_PERFORMANCE flag for BVH33 midphase enables a fast cooking path at the expense of somewhat lower quality BVH construction. + if (cookingPerformance) + params.midphaseDesc.mBVH33Desc.meshCookingHint = PxMeshCookingHint::eCOOKING_PERFORMANCE; + else + params.midphaseDesc.mBVH33Desc.meshCookingHint = PxMeshCookingHint::eSIM_PERFORMANCE; + + // If meshSizePerfTradeoff is set to true, smaller mesh cooked mesh is produced. The mesh size/performance trade-off + // is controlled by setting the meshSizePerformanceTradeOff from 0.0f (smaller mesh) to 1.0f (larger mesh). + if(meshSizePerfTradeoff) + { + params.midphaseDesc.mBVH33Desc.meshSizePerformanceTradeOff = 0.0f; + } + else + { + // using the default value + params.midphaseDesc.mBVH33Desc.meshSizePerformanceTradeOff = 0.55f; } gCooking->setParams(params); @@ -173,6 +192,7 @@ void createTriangleMesh(PxU32 numVertices, const PxVec3* vertices, PxU32 numTria PxTriangleMesh* triMesh = NULL; + PxU32 meshSize = 0; // The cooked mesh may either be saved to a stream for later loading, or inserted directly into PxPhysics. if (inserted) @@ -186,18 +206,100 @@ void createTriangleMesh(PxU32 numVertices, const PxVec3* vertices, PxU32 numTria PxDefaultMemoryInputData stream(outBuffer.getData(), outBuffer.getSize()); triMesh = gPhysics->createTriangleMesh(stream); + + meshSize = outBuffer.getSize(); } // Print the elapsed time for comparison PxU64 stopTime = SnippetUtils::getCurrentTimeCounterValue(); float elapsedTime = SnippetUtils::getElapsedTimeInMilliseconds(stopTime - startTime); - printf("-----------------------------------------------\n"); - printf("Create triangle mesh with %d triangles: \n",numTriangles); - cookingPerformance ? printf("\t Cooking performance on\n") : printf("\t Cooking performance off\n"); - inserted ? printf("\t Mesh inserted on\n") : printf("\t Mesh inserted off\n"); - !skipEdgeData ? printf("\t Precompute edge data on\n") : printf("\t Precompute edge data off\n"); - !skipMeshCleanup ? printf("\t Mesh cleanup on\n") : printf("\t Mesh cleanup off\n"); - printf("Elapsed time in ms: %f \n", double(elapsedTime)); + printf("\t -----------------------------------------------\n"); + printf("\t Create triangle mesh with %d triangles: \n",numTriangles); + cookingPerformance ? printf("\t\t Cooking performance on\n") : printf("\t\t Cooking performance off\n"); + inserted ? printf("\t\t Mesh inserted on\n") : printf("\t\t Mesh inserted off\n"); + !skipEdgeData ? printf("\t\t Precompute edge data on\n") : printf("\t\t Precompute edge data off\n"); + !skipMeshCleanup ? printf("\t\t Mesh cleanup on\n") : printf("\t\t Mesh cleanup off\n"); + printf("\t\t Mesh size/performance trade-off: %f \n", double(params.midphaseDesc.mBVH33Desc.meshSizePerformanceTradeOff)); + printf("\t Elapsed time in ms: %f \n", double(elapsedTime)); + if(!inserted) + { + printf("\t Mesh size: %d \n", meshSize); + } + + triMesh->release(); +} + +// Creates a triangle mesh using BVH34 midphase with different settings. +void createBV34TriangleMesh(PxU32 numVertices, const PxVec3* vertices, PxU32 numTriangles, const PxU32* indices, + bool skipMeshCleanup, bool skipEdgeData, bool inserted, const PxU32 numTrisPerLeaf) +{ + PxU64 startTime = SnippetUtils::getCurrentTimeCounterValue(); + + PxTriangleMeshDesc meshDesc; + meshDesc.points.count = numVertices; + meshDesc.points.data = vertices; + meshDesc.points.stride = sizeof(PxVec3); + meshDesc.triangles.count = numTriangles; + meshDesc.triangles.data = indices; + meshDesc.triangles.stride = 3 * sizeof(PxU32); + + PxCookingParams params = gCooking->getParams(); + + // Create BVH34 midphase + params.midphaseDesc = PxMeshMidPhase::eBVH34; + + // setup common cooking params + setupCommonCookingParams(params, skipMeshCleanup, skipEdgeData); + + // Cooking mesh with less triangles per leaf produces larger meshes with better runtime performance + // and worse cooking performance. Cooking time is better when more triangles per leaf are used. + params.midphaseDesc.mBVH34Desc.numTrisPerLeaf = numTrisPerLeaf; + + gCooking->setParams(params); + +#if defined(PX_CHECKED) || defined(PX_DEBUG) + // If DISABLE_CLEAN_MESH is set, the mesh is not cleaned during the cooking. + // We should check the validity of provided triangles in debug/checked builds though. + if (skipMeshCleanup) + { + PX_ASSERT(gCooking->validateTriangleMesh(meshDesc)); + } +#endif // DEBUG + + + PxTriangleMesh* triMesh = NULL; + PxU32 meshSize = 0; + + // The cooked mesh may either be saved to a stream for later loading, or inserted directly into PxPhysics. + if (inserted) + { + triMesh = gCooking->createTriangleMesh(meshDesc, gPhysics->getPhysicsInsertionCallback()); + } + else + { + PxDefaultMemoryOutputStream outBuffer; + gCooking->cookTriangleMesh(meshDesc, outBuffer); + + PxDefaultMemoryInputData stream(outBuffer.getData(), outBuffer.getSize()); + triMesh = gPhysics->createTriangleMesh(stream); + + meshSize = outBuffer.getSize(); + } + + // Print the elapsed time for comparison + PxU64 stopTime = SnippetUtils::getCurrentTimeCounterValue(); + float elapsedTime = SnippetUtils::getElapsedTimeInMilliseconds(stopTime - startTime); + printf("\t -----------------------------------------------\n"); + printf("\t Create triangle mesh with %d triangles: \n", numTriangles); + inserted ? printf("\t\t Mesh inserted on\n") : printf("\t\t Mesh inserted off\n"); + !skipEdgeData ? printf("\t\t Precompute edge data on\n") : printf("\t\t Precompute edge data off\n"); + !skipMeshCleanup ? printf("\t\t Mesh cleanup on\n") : printf("\t\t Mesh cleanup off\n"); + printf("\t\t Num triangles per leaf: %d \n", numTrisPerLeaf); + printf("\t Elapsed time in ms: %f \n", double(elapsedTime)); + if (!inserted) + { + printf("\t Mesh size: %d \n", meshSize); + } triMesh->release(); } @@ -212,24 +314,53 @@ void createTriangleMeshes() PxVec3* vertices = new PxVec3[numVertices]; PxU32* indices = new PxU32[numTriangles * 3]; + srand(50); + createRandomTerrain(PxVec3(0.0f, 0.0f, 0.0f), numRows, numColumns, 1.0f, 1.0f, 1.f, vertices, indices); - // Create triangle mesh with different settings + // Create triangle mesh using BVH33 midphase with different settings + printf("-----------------------------------------------\n"); + printf("Create triangles mesh using BVH33 midphase: \n\n"); // Favor runtime speed, cleaning the mesh and precomputing active edges. Store the mesh in a stream. // These are the default settings, suitable for offline cooking. - createTriangleMesh<false,false,false,false>(numVertices,vertices,numTriangles,indices); + createBV33TriangleMesh(numVertices,vertices,numTriangles,indices, false, false, false, false, false); + + // Favor mesh size, cleaning the mesh and precomputing active edges. Store the mesh in a stream. + createBV33TriangleMesh(numVertices, vertices, numTriangles, indices, false, false, false, false, true); // Favor cooking speed, skip mesh cleanup, but precompute active edges. Insert into PxPhysics. // These settings are suitable for runtime cooking, although selecting fast cooking may reduce // runtime performance of simulation and queries. We still need to ensure the triangles // are valid, so we perform a validation check in debug/checked builds. - createTriangleMesh<true,false,true,true>(numVertices,vertices,numTriangles,indices); + createBV33TriangleMesh(numVertices,vertices,numTriangles,indices, true, false, true, true, false); + + // Favor cooking speed, skip mesh cleanup, and don't precompute the active edges. Insert into PxPhysics. + // This is the fastest possible solution for runtime cooking, but all edges are marked as active, which can + // further reduce runtime performance, and also affect behavior. + createBV33TriangleMesh(numVertices,vertices,numTriangles,indices, false, true, true, true, false); + + // Create triangle mesh using BVH34 midphase with different settings + printf("-----------------------------------------------\n"); + printf("Create triangles mesh using BVH34 midphase: \n\n"); + + // Favor runtime speed, cleaning the mesh and precomputing active edges. Store the mesh in a stream. + // These are the default settings, suitable for offline cooking. + createBV34TriangleMesh(numVertices, vertices, numTriangles, indices, false, false, false, 4); + + // Favor mesh size, cleaning the mesh and precomputing active edges. Store the mesh in a stream. + createBV34TriangleMesh(numVertices, vertices, numTriangles, indices, false, false, false, 15); + + // Favor cooking speed, skip mesh cleanup, but precompute active edges. Insert into PxPhysics. + // These settings are suitable for runtime cooking, although selecting more triangles per leaf may reduce + // runtime performance of simulation and queries. We still need to ensure the triangles + // are valid, so we perform a validation check in debug/checked builds. + createBV34TriangleMesh(numVertices, vertices, numTriangles, indices, true, false, true, 15); // Favor cooking speed, skip mesh cleanup, and don't precompute the active edges. Insert into PxPhysics. // This is the fastest possible solution for runtime cooking, but all edges are marked as active, which can // further reduce runtime performance, and also affect behavior. - createTriangleMesh<false,true,true,true>(numVertices,vertices,numTriangles,indices); + createBV34TriangleMesh(numVertices, vertices, numTriangles, indices, false, true, true, 15); delete [] vertices; delete [] indices; |