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 the mesh displays like this;
That is a perfect-case scenario. No errors within the mesh at all.
However if I close the program and come back to it, it can either look perfect again, or this time look something like this;
That is more of a worst-case scenario, sometimes only a few faces are messed up.
What confuses me the most is how random it is. Even if nothing has changed in the code, it can completely mess up. This leads me to believe that this is some sort of memory or file-access issue?
The following C++ is the code that loads the verticies and triangles:
//uses 3 vectors to store x,y,z then combines them at the end to make the full PxVec3
void ModelLoader::LoadVertex(std::string fileLocation)
{
//clears vector so it doesnt break when loading multiple models
vertexArray.clear();
std::ifstream file2;
file2.open(fileLocation);
if (!file2.is_open())
std::cout << "Failed to load vertex data" << std::endl;
//individual x,y,z values for the vertex arrays
std::vector<float> x;
std::vector<float> y;
std::vector<float> z;
int counter = 0;
//grabs each line add places them into a vector to be used later
while (!file2.eof())
{
if (counter == 3)
counter = 0;
std::string num;
file2 >> num;
switch (counter)
{
case 0:
x.push_back(stof(num)); //converts string to float
break;
case 1:
y.push_back(stof(num));
break;
case 2:
z.push_back(stof(num));
break;
default:
break;
}
counter++;
}
//adds x,y,z values to the vector to complete to full PxVec3
for (int i = 0; i < x.size(); i++)
vertexArray.push_back(PhysicsEngine::PxVec3(x[i], y[i], z[i]));
file2.close();
}
//uses a char array to grab all values within the filelocation selected
void ModelLoader::LoadTriangles(std::string fileLocation)
{
//clears vector so it doesnt break when loading multiple models
triangleArray.clear();
std::ifstream file;
file.open(fileLocation);
if (!file.is_open())
std::cout << "Failed to load triangle data" << std::endl;
//creates char array
char* fileNumber = new char[2];
int arrayCounter = 0;
while (!file.eof())
{
//grabs next char
char num;
file >> num;
//stops last char being repeated
if (file.eof())
break;
if (num != ',')
{
fileNumber[arrayCounter] = num;
arrayCounter++;
}
else
{
//converts the char array to an int
int fullNumber = atoi(fileNumber);
//adds number to triangles vector
//std::cout << fullNumber << std::endl;
triangleArray.push_back((PhysicsEngine::PxU32)fullNumber);
//resets array for next number
fileNumber = new char[2];
arrayCounter = 0;
}
}
//adds the last number to the vector
int fullNumber = atoi(fileNumber);
triangleArray.push_back((PhysicsEngine::PxU32)fullNumber);
delete[] fileNumber;
file.close();
}
EDIT: Just for anyone new, the code for this question has been updated and can be found here: Gist