-2

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?

  • 2
    This line should actually never be executed, because `tempIndices.size() == 0` and it doesnt change in the code you show here. Please read about how to create a [mcve] – 463035818_is_not_an_ai Oct 03 '16 at 12:13
  • Apologies @tobi303 I should have included more code to explain. I have edited above to include more,I can confirm that tempIndices is being populated and cout'ing tempIndices.size() on its own prints out 6 as expected. Thanks for your reply – Pearce Byrne Colgan Oct 03 '16 at 12:24

1 Answers1

1

I dont really understand your code. It is still not a MCVE (please read the link!). However, what might be the cause of the error is that you populate each vector only in one particular case of the name:

name == "v"
    tempVertices gets populated
name == "vn" 
    tempNormals gets populated
name == "f" 
    tempIndices gets populated
and then...
    you access all three vectors (if tempIndices.size() >= 2)

Thus at least in the case of name == "f" you access vector indices that are beyond the size of the vectors.

What is the continue supposed to do? continue only makes sense in loops and I dont understand why you put it there. Is this maybe still not the real code?

Community
  • 1
  • 1
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185