1

I've constructed a quick python script that reads a wavefront .obj file and outputs the verticies and faces to files. I have another C++ function that reads these files and creates a mesh based of-of those.

The issue that I'm experiencing is that this works perfectly sometimes, and other time not so much - with the same mesh. E.g. I start my PhysX program, and create 10 of the same objects of the same mesh and some of them look like this:

Normal Mesh

and others look like this:

Broken Mesh

The following C++ is the code that loads the verticies and triangles:

bool ModelLoader::LoadVertexFile(const std::string& fileLocation)
{
    std::ifstream file(fileLocation);
    if (!file) {
        std::cerr << "Failed to load vertex data\n";
        return false;
    }

    float x, y, z;
    vertexArray.clear();

    while (file >> x >> y >> z)
    {
        vertexArray.push_back(PhysicsEngine::PxVec3(x, y, z));
    }
    return true;
}

bool ModelLoader::LoadTrianglesFile(const std::string& fileLocation)
{
    std::ifstream file(fileLocation);
    if (!file) {
        std::cerr << "Failed to load vertex data\n";
        return false;
    }

    int x;
    triangleArray.clear();
    while (file >> x)
    {
        triangleArray.push_back(x);
    }
    return true;
}

This is the code for creating constructing the mesh

   CustomObject(const PxTransform& pose = PxTransform(PxIdentity), string vertfile = "", string trigfile = "") :
        StaticActor(pose)
    {

        modelLoader = new ModelLoader();
        if(!modelLoader->LoadVertexFile(vertfile))
            throw new Exception("Failed to load VertexFile.");
        if(!modelLoader->LoadTrianglesFile(trigfile))
            throw new Exception("Failed to load TrianglesFile.");

        PxTriangleMeshDesc mesh_desc;
        mesh_desc.points.count = (PxU32)modelLoader->vertexArray.size();
        mesh_desc.points.stride = sizeof(PxVec3);
        mesh_desc.points.data = &modelLoader->vertexArray.front();
        mesh_desc.triangles.count = (PxU32)modelLoader->triangleArray.size();
        mesh_desc.triangles.stride = 3 * sizeof(PxU32);
        mesh_desc.triangles.data = &modelLoader->triangleArray.front();

        CreateShape(PxTriangleMeshGeometry(CookMesh(mesh_desc)));
    }

So if anyone can help me figure out whats going wrong it would be really appreciated

Spektre
  • 49,595
  • 11
  • 110
  • 380
Zyie
  • 134
  • 2
  • 12

1 Answers1

0

I think I had a similar issue and fixed by providing contiguous 2D arrays either for vertices and for triangles (got a look to this bunny mesh example in PhysX 5.1 and to the docs)