I'm working on my own personal game engine and I came across this problem. Trying to load an OBJ file to use with DirectX but the LoadObjFile keeps giving me the error
Run-Time Check Failure #2 - Stack around the variable 'normalIndex' was corrupted.
What can I do? sometimes the variable name changes to 'uvIndex'
Here's my code:
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;
}
It's worth noting that I'm using a file that has something like 300000 vertices. The obj file is here http://tf3dm.com/3d-model/millenium-falcon-82947.html
This is how the function is being called (based on @O'Neil's comment
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 was kind enough to point me in the right direction. I was making the assumption that every face had 3 vertices, so I declared these
unsigned long vertexIndex[3], uvIndex[3], normalIndex[3];
for (int k = 0; k < 3; k++)
{
vertexIndex[k] = 0;
uvIndex[k] = 0;
normalIndex[k] = 0;
}
What I didn't consider is that sometimes I have 4 vertices, which was overflowing those variables.
This is the final code:
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;
}
and this is the function call
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.