我构建了一个快速的 python 脚本,它读取波前 .obj 文件并将顶点和面输出到文件。我有另一个 C++ 函数,它读取这些文件并创建基于这些文件的网格。
我遇到的问题是,有时这可以完美地工作,而其他时候却不是那么好 - 使用相同的网格。例如,我启动我的 PhysX 程序,网格显示如下;
这是一个完美的案例。网格内完全没有错误。
但是,如果我关闭程序并返回它,它可以再次看起来很完美,或者这次看起来像这样;
那更像是最坏的情况,有时只有几张脸被搞砸了。
最让我困惑的是它的随机性。即使代码没有任何变化,它也可能完全搞砸。这让我相信这是某种内存或文件访问问题?
以下 C++ 是加载顶点和三角形的代码:
//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();
}
编辑:对于任何新人来说,这个问题的代码已经更新,可以在这里找到:Gist