I am trying to parse an .obj file and I have my file parsed and stored in vec3's and int vectors respectively.
However when I try to create my aitVertex
struct by linking everything up using the faces to correspond to the vertex and normal's I am getting the following "std::out_of_range at memory location".
Here is the relevant code:
bool aitMesh::loadFromObj(std::string path)
{
//Setup vars etc...
//std::vector<float> tempVertices;
//std::vector<float> tempNormals;
float Vertex[3];
float Normal[3];
int Index[6];
std::vector<glm::vec3> tempVertices;
std::vector<glm::vec3> tempNormals;
std::vector<int> tempIndices;
if (name == "v")
{
//sscanf_s matches a sequence of non-whitespace characters
//line.c_str returns a pointer to an array that contains a string..
//in this case and stores in Vertex[1,2,3]
sscanf_s(line.c_str(), "%*s %f %f %f", &Vertex[0], &Vertex[1], &Vertex[2]);
std::cout << "Line: " << line << "\n";
std::cout << "Name: " << name << "\n";
//Adds to tempVertices Array
//tempVertices.push_back(Vertex[0]);
//tempVertices.push_back(Vertex[1]);
//tempVertices.push_back(Vertex[2]);
tempVertices.push_back(glm::vec3(Vertex[0], Vertex[1], Vertex[2]));
continue;
}
if (name == "vn")
{
sscanf_s(line.c_str(), "%*s %f %f %f", &Normal[0], &Normal[1], &Normal[2]);
std::cout << "Line: " << line << "\n";
std::cout << "Name: " << name << "\n";
//tempNormals.push_back(Normal[0]);
//tempNormals.push_back(Normal[1]);
//tempNormals.push_back(Normal[2]);
tempNormals.push_back(glm::vec3(Normal[0], Normal[1], Normal[2]));
continue;
}
if (name == "s")
{
continue;
}
if (name == "f")
{
sscanf_s(line.c_str(), "%*s %d//%d %d//%d %d//%d", &Index[0], &Index[1], &Index[2], &Index[3], &Index[4], &Index[5]);
std::cout << "Line: " << line << "\n";
std::cout << "Name: " << name << "\n";
tempIndices.push_back(Index[0]);
tempIndices.push_back(Index[1]);
tempIndices.push_back(Index[2]);
tempIndices.push_back(Index[3]);
tempIndices.push_back(Index[4]);
tempIndices.push_back(Index[5]);
}
for (int i = 2; i <= tempIndices.size(); i++)
{
if (i % 2 == 0)
{
vertices.push_back(aitVertex(tempVertices.at(tempIndices[i-2]), tempNormals.at(tempIndices[i-1])));
}
}
The line my program is breaking on is
vertices.push_back(aitVertex(tempVertices.at(tempIndices[i-2]), tempNormals.at(tempIndices[i-1])));
I think the problem lies when I reference tempIndices
as when I try print out tempIndices[i-2]
on its on it prints out as it should..
Am I just referencing it incorrectly?