![](/img/trans.png)
[英]Run-Time Check Failure #2 - Stack around the variable ''z" was corrupted
[英]Run-Time Check Failure #2 - Stack around the variable 'normalIndex' was corrupted
我正在开发自己的个人游戏引擎,但遇到了这个问题。 尝试加载要与 DirectX 一起使用的 OBJ 文件,但 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.