C++ 运行时检查失败#2-围绕变量';正常指数';腐败
我正在开发自己的个人游戏引擎,我遇到了这个问题。试图加载OBJ文件以与DirectX一起使用,但LoadObjFile始终显示错误C++ 运行时检查失败#2-围绕变量';正常指数';腐败,c++,C++,我正在开发自己的个人游戏引擎,我遇到了这个问题。试图加载OBJ文件以与DirectX一起使用,但LoadObjFile始终显示错误 Run-Time Check Failure #2 - Stack around the variable 'normalIndex' was corrupted. 我能做什么?有时变量名会更改为“uvIndex” 这是我的密码: bool Renderer::LoadObjFile( const char* path,
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;
}
bool渲染器::LoadObjFile(
常量字符*路径,
std::vector*顶点,
标准::矢量*纹理,
标准::向量*法线,
标准::向量*顶点索引,
标准::向量*uvindex,
标准::向量<无符号整数>*法线索引)
{
std::ifstream infle(path);//构造对象并打开文件
如果(!infle){
标准:cerr buf)
子字符串。推回(buf);
D3DVECTOR顶点={(float)atof(子字符串[0].c_str()),(float)atof(子字符串[1].c_str()),(float)atof(子字符串[2].c_str())};
顶点->向后推(顶点);
}
否则,如果(行substr(0,3)=“vn”)
{
line=line.substr(3);//删除行标题
std::string buf;//有一个缓冲字符串
std::stringstream ss(line);//将字符串插入流中
std::vector子字符串;//创建vector来保存我们的单词
而(ss>>buf)
子字符串。推回(buf);
D3DVECTOR normal={(float)atof(子字符串[0].c_str()),(float)atof(子字符串[1].c_str()),(float)atof(子字符串[2].c_str())};
法线->推回(法线);
}
否则,如果(行substr(0,3)=“vt”)
{
line=line.substr(3);//删除行标题
std::string buf;//有一个缓冲字符串
std::stringstream ss(line);//将字符串插入流中
std::vector子字符串;//创建vector来保存我们的单词
而(ss>>buf)
子字符串。推回(buf);
D3DXVECTOR2 VertextTexture={(float)std::stod(子字符串[0].c_str()),(float)std::stod(子字符串[1].c_str())};
纹理->推回(垂直纹理);
}
else if(行substr(0,2)=“f”)
{
line=line.substr(2);//删除行标题
std::string buf;//有一个缓冲字符串
std::stringstream ss(line);//将字符串插入流中
std::vector子字符串;//创建vector来保存我们的单词
而(ss>>buf)
子字符串。推回(buf);
无符号长顶点索引[3]、UV索引[3]、法线索引[3];
对于(int k=0;k<3;k++)
{
顶点索引[k]=0;
uvIndex[k]=0;
normalIndex[k]=0;
}
std::字符串分隔符=“/”;
无符号整数pos=0;
字符串标记;
for(无符号int t=0;t向后推(顶点索引[0]);
顶点索引->向后推(顶点索引[1]);
顶点索引->推回(顶点索引[2]);
uvIndex->push_back(uvIndex[0]);
uvIndex->push_back(uvIndex[1]);
uvIndex->push_back(uvIndex[2]);
normalIndex->push_back(normalIndex[0]);
normalIndex->push_back(normalIndex[1]);
normalIndex->push_back(normalIndex[2]);
}
否则,如果(行substr(0,2)=“s”)
{
继续;
}
其他的
{
std::cout-vts;
std::vectorns;
标准::向量顶点索引、UV索引、法线索引;
bool result=LoadObjFile(“millenium falcon.obj”、&vs、&vts、&ns、&vertexIndices、&uvindex、&normalindex);
@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;
}
unsigned long vertexIndex[3]、uvIndex[3]、normalIndex[3];
对于(int k=0;k<3;k++)
{
顶点索引[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;
}
bool渲染器::LoadObjFile(
常量字符*路径,
std::vector&顶点,
标准::矢量和纹理,
标准::向量和法线,
标准::向量<无符号
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);