0

I am trying to write a really simple obj file reader, that writes all vertice values in order from the obj file into one vector (already done that), and also writes vertice values referred by face values from the obj file in another vector, for example:

v1 = 0.0 0.0 0.0

v2 = 1.0 0.0 0.0

v3 = 1.0 1.0 0.0

f1 = 3 2 1

My program would write all vertice values in order in the first vector and then would write vertice values making up a face as following in the second vector: 1.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0

Everything worked fine until I tried to get values from the first vector(verticesCoordsXYZ) and push them back into a the second vector(faceCoords). Code that I commented out breaks the program, it seems like the location I am searching for in the first vector is bigger than the vector size (when the program breaks I get out_of_range at memory loc error, and it says that the size of verticesCoordsXYZ is 0), even though I filled the vector with all the vertice values.

What am I doing wrong? Am I not understanding how vectors work, am I going out of vector scope? How can I fix this problem?

    string line;
    while (getline(modelfile, line))
    {

        string s;
        istringstream iss(line);
        iss >> s;
        vector <float> verticesCoordsXYZ;
        vector <float> faceCoords;

        if (s == "v")
        {

            float x, y ,z;
            iss >> x >> y >> z;
            verticesCoordsXYZ.push_back(x);
            verticesCoordsXYZ.push_back(y);
            verticesCoordsXYZ.push_back(z);

            for (int i = 0; i < (int)verticesCoordsXYZ.size(); i++)
            {
                cout << verticesCoordsXYZ.at(i);
            }
            cout << endl;

        }

        if (s == "f")
        {
            int a, b, c;

            iss >> a >> b >> c;

            int aLocation = a * 3 - 3; //vertice locations in verticeCoordsXYZ that would make up faces
            int bLocation = b * 3 - 3;
            int cLocation = c * 3 - 3;

            for (int i = 0; i < 2; i++)
            {
                //faceCoords.push_back(verticesCoordsXYZ.at(aLocation+i));
            }
            for (int i = 0; i < 2; i++)
            {
                //faceCoords.push_back(verticesCoordsXYZ.at(bLocation+i));
            }
            for (int i = 0; i < 2; i++)
            {
                //faceCoords.push_back(verticesCoordsXYZ.at(cLocation+i));
            }

            for (int i = 0; i < (int)faceCoords.size(); i++)
            {
                cout << faceCoords.at(i) << "f";
            }



            cout << endl << a << b << c;
        }

    }

    modelfile.close();

2 Answers 2

1

You're recreating the vectors every time through the while loop because they're declared inside it. This means that when s == "f" they are all empty.

You should declare them outside the while loop, where you declare line.

Sign up to request clarification or add additional context in comments.

Comments

0

You declare vector inside the while loop so that it get reseted every time the while go loop again.

Try declare the vector before the while loop

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.