运行时检查失败 #2 - 变量周围的堆栈'normalIndex'已损坏

Run-Time Check Failure #2 - Stack around the variable 'normalIndex' was corrupted

本文关键字:堆栈 已损坏 normalIndex 周围 失败 检查 变量 运行时      更新时间:2023-10-16

我正在开发自己的个人游戏引擎,遇到了这个问题。试图加载OBJ文件以与DirectX一起使用,但LoadObjFile一直给我错误

Run-Time Check Failure #2 - Stack around the variable 'normalIndex' was corrupted.

我能做什么?有时变量名称会更改为"uvIndex">

这是我的代码:

bool Renderer::LoadObjFile(
const char* path, 
std::vector < D3DXVECTOR3 > *vertices,
std::vector < D3DXVECTOR2 > *textureVertices,
std::vector < D3DXVECTOR3 > *normals,
std::vector< unsigned int > *vertexIndices,
std::vector< unsigned int > *uvIndices,
std::vector< unsigned int > *normalIndices)
{
std::ifstream infile(path);  // construct object and open file
if (!infile) { 
std::cerr << "Error opening file!" << std::endl; 
return false;
}
std::string line;
while (std::getline(infile, line))
{
if (line.substr(0, 2) == "v ")
{
line = line.substr(2);                  // Eliminate line header
std::string buf;                        // Have a buffer string
std::stringstream ss(line);             // Insert the string into a stream
std::vector<std::string> substrings;    // Create vector to hold our words
while (ss >> buf)
substrings.push_back(buf);
D3DVECTOR vertex = { (float)atof(substrings[0].c_str()), (float)atof(substrings[1].c_str()), (float)atof(substrings[2].c_str()) };
vertices->push_back(vertex);
}
else if (line.substr(0, 3) == "vn ")
{
line = line.substr(3);                  // Eliminate line header
std::string buf;                        // Have a buffer string
std::stringstream ss(line);             // Insert the string into a stream
std::vector<std::string> substrings;    // Create vector to hold our words
while (ss >> buf)
substrings.push_back(buf);
D3DVECTOR normal = { (float)atof(substrings[0].c_str()), (float)atof(substrings[1].c_str()), (float)atof(substrings[2].c_str()) };
normals->push_back(normal);
}
else if (line.substr(0, 3) == "vt ")
{
line = line.substr(3);                  // Eliminate line header
std::string buf;                        // Have a buffer string
std::stringstream ss(line);             // Insert the string into a stream
std::vector<std::string> substrings;    // Create vector to hold our words
while (ss >> buf)
substrings.push_back(buf);
D3DXVECTOR2 vertexTexture = { (float)std::stod(substrings[0].c_str()), (float)std::stod(substrings[1].c_str()) };
textureVertices->push_back(vertexTexture);
}
else if (line.substr(0, 2) == "f ")
{
line = line.substr(2);                  // Eliminate line header
std::string buf;                        // Have a buffer string
std::stringstream ss(line);             // Insert the string into a stream
std::vector<std::string> substrings;    // Create vector to hold our words
while (ss >> buf)
substrings.push_back(buf);
unsigned long vertexIndex[3], uvIndex[3], normalIndex[3];
for (int k = 0; k < 3; k++)
{
vertexIndex[k] = 0;
uvIndex[k] = 0;
normalIndex[k] = 0;
}
std::string delimiter = "/";
unsigned int pos = 0;
std::string token;
for (unsigned int t = 0; t < substrings.size(); t++)
{
if ((pos = substrings[t].find(delimiter)) != std::string::npos)
{
token.clear();
token = substrings[t].substr(0, pos);
vertexIndex[t] = atol(token.c_str()) - 1;
substrings[t].erase(0, pos + delimiter.length());
}
if ((pos = substrings[t].find(delimiter)) != std::string::npos)
{
token.clear();
token = substrings[t].substr(0, pos);
uvIndex[t] = atol(token.c_str()) - 1;
substrings[t].erase(0, pos + delimiter.length());
}
token.clear();
token = substrings[t];
normalIndex[t] = atol(token.c_str()) - 1;
substrings[t].clear();
}
vertexIndices->push_back(vertexIndex[0]);
vertexIndices->push_back(vertexIndex[1]);
vertexIndices->push_back(vertexIndex[2]);
uvIndices->push_back(uvIndex[0]);
uvIndices->push_back(uvIndex[1]);
uvIndices->push_back(uvIndex[2]);
normalIndices->push_back(normalIndex[0]);
normalIndices->push_back(normalIndex[1]);
normalIndices->push_back(normalIndex[2]);
}
else if (line.substr(0, 2) == "s ")
{
continue;
}
else 
{
std::cout << line << std::endl;
}
}
return true;
}

值得注意的是,我使用的文件大约有300000个顶点。obj文件在这里http://tf3dm.com/3d-model/millenium-falcon-82947.html

这就是函数的调用方式(基于@O'Neil的评论

std::vector< D3DXVECTOR3 > vs;
std::vector< D3DXVECTOR2 > vts;
std::vector< D3DXVECTOR3 > ns;
std::vector< unsigned int > vertexIndices, uvIndices, normalIndices;
bool result = LoadObjFile("millenium-falcon.obj", &vs, &vts, &ns, &vertexIndices, &uvIndices, &normalIndices);

@O'Neil好心地为我指明了正确的方向。我假设每个面都有3个顶点,所以我声明了这些

unsigned long vertexIndex[3], uvIndex[3], normalIndex[3];
for (int k = 0; k < 3; k++)
{
vertexIndex[k] = 0;
uvIndex[k] = 0;
normalIndex[k] = 0;
}

我没有考虑的是,有时我有4个顶点,它们溢出了这些变量。

这是最后一个代码:

bool Renderer::LoadObjFile(
const char* path, 
std::vector < D3DXVECTOR3 > &vertices,
std::vector < D3DXVECTOR2 > &textureVertices,
std::vector < D3DXVECTOR3 > &normals,
std::vector< unsigned int > &vertexIndices,
std::vector< unsigned int > &uvIndices,
std::vector< unsigned int > &normalIndices)
{
std::ifstream infile(path);  // construct object and open file
if (!infile) { 
std::cerr << "Error opening file!" << std::endl; 
return false;
}
std::string line;
std::string buf;                        // Have a buffer string
std::string delimiter = "/";
unsigned int pos = 0;
std::string token;
while (std::getline(infile, line))
{
if (line.substr(0, 2) == "v ")
{
line = line.substr(2);                  // Eliminate line header
std::stringstream ss(line);             // Insert the string into a stream
buf.clear();
std::vector<std::string> substrings;    // Create vector to hold our words
while (ss >> buf)
substrings.push_back(buf);
D3DVECTOR vertex = { (float)atof(substrings[0].c_str()), (float)atof(substrings[1].c_str()), (float)atof(substrings[2].c_str()) };
vertices.push_back(vertex);
}
else if (line.substr(0, 3) == "vn ")
{
line = line.substr(3);                  // Eliminate line header
std::stringstream ss(line);             // Insert the string into a stream
buf.clear();
std::vector<std::string> substrings;    // Create vector to hold our words
while (ss >> buf)
substrings.push_back(buf);
D3DVECTOR normal = { (float)atof(substrings[0].c_str()), (float)atof(substrings[1].c_str()), (float)atof(substrings[2].c_str()) };
normals.push_back(normal);
}
else if (line.substr(0, 3) == "vt ")
{
line = line.substr(3);                  // Eliminate line header
std::stringstream ss(line);             // Insert the string into a stream
buf.clear();
std::vector<std::string> substrings;    // Create vector to hold our words
while (ss >> buf)
substrings.push_back(buf);
D3DXVECTOR2 vertexTexture = { (float)std::stod(substrings[0].c_str()), (float)std::stod(substrings[1].c_str()) };
textureVertices.push_back(vertexTexture);
}
else if (line.substr(0, 2) == "f ")
{
line = line.substr(2);                  // Eliminate line header
std::stringstream ss(line);             // Insert the string into a stream
buf.clear();
std::vector<std::string> substrings;    // Create vector to hold our words
while (ss >> buf)
substrings.push_back(buf);
std::vector<unsigned int> vertexIndex, uvIndex, normalIndex;
for (unsigned int t = 0; t < substrings.size(); t++)
{
if ((pos = substrings[t].find(delimiter)) != std::string::npos)
{
token.clear();
token = substrings[t].substr(0, pos);
vertexIndex.push_back(atol(token.c_str()) - 1);
substrings[t].erase(0, pos + delimiter.length());
}
if ((pos = substrings[t].find(delimiter)) != std::string::npos)
{
token.clear();
token = substrings[t].substr(0, pos);
uvIndex.push_back(atol(token.c_str()) - 1);
substrings[t].erase(0, pos + delimiter.length());
}
token.clear();
token = substrings[t];
normalIndex.push_back(atol(token.c_str()) - 1);
substrings[t].clear();
}
for (unsigned int m = 0; m < vertexIndex.size(); m++)
vertexIndices.push_back(vertexIndex[m]);
for (unsigned int m = 0; m < uvIndex.size(); m++)
uvIndices.push_back(uvIndex[m]);
for (unsigned int m = 0; m < normalIndex.size(); m++)
normalIndices.push_back(normalIndex[m]);
}
else if (line.substr(0, 2) == "s ")
{
continue;
}
else 
{
std::cout << line << std::endl;
}
}
return true;
}

这就是函数调用

std::vector< D3DXVECTOR3 > vs;
std::vector< D3DXVECTOR2 > vts;
std::vector< D3DXVECTOR3 > ns;
std::vector< unsigned int > vertexIndices, uvIndices, normalIndices;
bool result = LoadObjFile("millenium-falcon.obj", vs, vts, ns, vertexIndices, uvIndices, normalIndices);