static void CreatePhysicsTerrain() { if(gTerrainData!=NULL) { delete gTerrainData; gTerrainData=NULL; } gTerrainData = new TerrainData; gTerrainData->init(TERRAIN_SIZE, TERRAIN_OFFSET, TERRAIN_WIDTH, TERRAIN_CHAOS); // Build physical model NxTriangleMeshDesc terrainDesc; terrainDesc.numVertices = gTerrainData->nbVerts; terrainDesc.numTriangles = gTerrainData->nbFaces; terrainDesc.pointStrideBytes = sizeof(NxVec3); terrainDesc.triangleStrideBytes = 3*sizeof(NxU32); terrainDesc.points = gTerrainData->verts; terrainDesc.triangles = gTerrainData->faces; terrainDesc.flags = 0; terrainDesc.heightFieldVerticalAxis = NX_Y; terrainDesc.heightFieldVerticalExtent = -1000.0f; bool status = InitCooking(); if (!status) { printf("Unable to initialize the cooking library. Please make sure that you have correctly installed the latest version of the NVIDIA PhysX SDK."); exit(1); } MemoryWriteBuffer buf; status = CookTriangleMesh(terrainDesc, buf); if (!status) { printf("Unable to cook a triangle mesh."); exit(1); } MemoryReadBuffer readBuffer(buf.data); terrainMesh = gPhysicsSDK->createTriangleMesh(readBuffer); // // Please note about the created Triangle Mesh, user needs to release it when no one uses it to save memory. It can be detected // by API "meshData->getReferenceCount() == 0". And, the release API is "gPhysicsSDK->releaseTriangleMesh(*meshData);" // NxTriangleMeshShapeDesc terrainShapeDesc; terrainShapeDesc.meshData = terrainMesh; terrainShapeDesc.shapeFlags = NX_SF_FEATURE_INDICES; NxActorDesc ActorDesc; ActorDesc.shapes.pushBack(&terrainShapeDesc); gTerrain = gScene->createActor(ActorDesc); gTerrain->userData = (void*)0; CloseCooking(); }
//------------------------------------------------------------------------------------ // Constructor //------------------------------------------------------------------------------------ TileTerrainShapeX::TileTerrainShapeX( PhysXManager *pMan , sn::TileTerrain* terrain ) { gPhAllocator = new UserAllocator(); NxU32* ii = terrain->GetIndices(); NxTriangleMeshDesc terrainDesc; terrainDesc.numVertices = terrain->GetVertexCount(); terrainDesc.numTriangles = terrain->GetIndexesCount() / 3; terrainDesc.pointStrideBytes = sizeof(gfx::Vertex3); terrainDesc.triangleStrideBytes = 3*sizeof(NxU32); terrainDesc.points = terrain->GetPositions(); terrainDesc.triangles = ii; terrainDesc.flags = 0; terrainDesc.heightFieldVerticalAxis = NX_Y; terrainDesc.heightFieldVerticalExtent = -1000.0f; bool status = InitCooking(/*gPhAllocator, &gPhErrorStream*/); if (!status) kge::io::Logger::Log("Unable to initialize the cooking library. Please make sure that you have correctly installed the latest version of the AGEIA PhysX SDK."); MemoryWriteBuffer buf; status = CookTriangleMesh(terrainDesc, buf); if (!status) printf("Unable to cook a triangle mesh."); MemoryReadBuffer readBuffer(buf.data); terrainMesh = pMan->getPhysxSDK()->createTriangleMesh(readBuffer); NxTriangleMeshShapeDesc terrainShapeDesc; terrainShapeDesc.meshData = terrainMesh; terrainShapeDesc.shapeFlags = NX_SF_FEATURE_INDICES; NxActorDesc ActorDesc; ActorDesc.shapes.pushBack(&terrainShapeDesc); gLocalActor = pMan->getPhysxScene()->createActor(ActorDesc); gLocalActor->userData = (void*)0; CloseCooking(); } // Constructor