コード例 #1
0
ファイル: physXWorld.cpp プロジェクト: david-leal/nau
void
PhsXWorld::_addCloth(float mass, std::shared_ptr<nau::scene::IScene> &aScene, std::string name, nau::math::vec3 aVec) {
    PxPhysics *gPhysics = &(m_pDynamicsWorld->getPhysics());

    std::shared_ptr<nau::scene::SceneObject> &aObject = aScene->getSceneObject(0);
    std::shared_ptr<VertexData> &vd = aObject->getRenderable()->getVertexData();
    std::vector<std::shared_ptr<MaterialGroup>> &matGroups = aObject->getRenderable()->getMaterialGroups();
    std::vector<std::shared_ptr<MaterialGroup>>::iterator matGroupsIter;

    PxDefaultMemoryOutputStream writeBuffer;

    int count = static_cast<int> (vd->getDataOf(VertexData::GetAttribIndex(std::string("position")))->size());
    PxClothParticle *particles = new PxClothParticle[count];

    PxClothMeshDesc meshDesc;
    matGroupsIter = matGroups.begin();
    for (; matGroupsIter != matGroups.end(); matGroupsIter++) {
        if ((*matGroupsIter)->getIndexData()->getIndexSize()) {
            std::shared_ptr<std::vector<unsigned int>> &indexes = (*matGroupsIter)->getIndexData()->getIndexData();
            PxClothParticle *ptls = particles;
            std::shared_ptr<std::vector<VertexData::Attr>> points = vd->getDataOf(VertexData::GetAttribIndex(std::string("position")));
            for (int i = 0; i < count; i++) {
                VertexData::Attr tempPoint = points->at(i);
                //ptls[i] = PxClothParticle(PxVec3(tempPoint.x, tempPoint.y, tempPoint.z), (i==0 || i==1) ? 0.0f : 1.0f);
                ptls[i] = PxClothParticle(PxVec3(tempPoint.x, tempPoint.y, tempPoint.z), i == 0 ? 0.0f : 0.1f);
            }

            meshDesc.points.data = reinterpret_cast<const unsigned char *>(&(vd->getDataOf(VertexData::GetAttribIndex(std::string("position")))->at(0)));
            meshDesc.points.count = count;
            meshDesc.points.stride = 4 * sizeof(float);

            meshDesc.invMasses.data = &particles->invWeight;
            meshDesc.invMasses.count = count;
            meshDesc.invMasses.stride = sizeof(PxClothParticle);

            meshDesc.triangles.data = reinterpret_cast<const unsigned char *>(&((*indexes)[0]));
            meshDesc.triangles.count = static_cast<int> (indexes->size() / 3);
            meshDesc.triangles.stride = 3 * sizeof(unsigned int);

        }
    }
    //float * points = reinterpret_cast<float *>(&(vd->getDataOf(VertexData::GetAttribIndex(std::string("position")))->at(0)));
    //PxU32 numParticles = static_cast<int> (vd->getDataOf(VertexData::GetAttribIndex(std::string("position")))->size());
    //PxU32 stride = 4 * sizeof(float);
    //// create particles
    //PxClothParticle* particles = new PxClothParticle[numParticles];
    //PxClothParticle* pIt = particles;
    //for (PxU32 i = 0; i<numParticles; ++i) {
    //	pIt->invWeight = i==0 ? 0.0f : 1.0f;
    //	int tempStride = i*stride;
    //	pIt->pos = PxVec3(points[tempStride], points[tempStride + 1], points[tempStride + 2]);
    //}

    PxClothFabric* fabric = PxClothFabricCreate(*gPhysics, meshDesc, PxVec3(0, -1, 0));
    PxTransform pose = PxTransform(PxMat44(const_cast<float*> (aScene->getTransform().getMatrix())));
    PxClothFlags flags = PxClothFlags();
    /*if(!flags.isSet(PxClothFlag::eSCENE_COLLISION))
    	flags.set(PxClothFlag::eSCENE_COLLISION);
    if (!flags.isSet(PxClothFlag::eGPU))
    	flags.set(PxClothFlag::eGPU);
    if (!flags.isSet(PxClothFlag::eSWEPT_CONTACT))
    	flags.set(PxClothFlag::eSWEPT_CONTACT);*/
    cloth = gPhysics->createCloth(pose, *fabric, particles, flags);
    cloth->userData = aScene.get();
    cloth->setClothFlag(PxClothFlag::eSCENE_COLLISION, true);
    cloth->setClothFlag(PxClothFlag::eGPU, true);
    cloth->setClothFlag(PxClothFlag::eSWEPT_CONTACT, true);
    cloth->setSolverFrequency(300.0f);
    cloth->setInertiaScale(0.9f);

    cloth->setStretchConfig(PxClothFabricPhaseType::eVERTICAL, PxClothStretchConfig(0.2f));
    cloth->setStretchConfig(PxClothFabricPhaseType::eHORIZONTAL, PxClothStretchConfig(0.2f));
    cloth->setStretchConfig(PxClothFabricPhaseType::eSHEARING, PxClothStretchConfig(0.75f));
    cloth->setStretchConfig(PxClothFabricPhaseType::eBENDING, PxClothStretchConfig(0.2f));
    m_pDynamicsWorld->addActor(*cloth);

}