Search code examples
c++iostreamstringstreamglm-mathwavefront

Reading obj files! (Indices)


What I want to do is read the faces(f), but they have "/" that I don't know how to remove? Basically what I want, is to get the faces number stored in a temporary variable. I have no idea how to do it in c++ fstream.

std::string text;
    std::istringstream iss(line);

    iss >> text;

    // Easy part!
    if (text == "v")
    {
        glm::vec3 currectPos;

        iss >> currectPos.x;
        iss >> currectPos.y;
        iss >> currectPos.z;

        filePos.push_back(currectPos);
    }
    if (text == "vt")
    {
        glm::vec2 currectUV;

        iss >> currectUV.x;
        iss >> currectUV.y;

        fileUV.push_back(currectUV);
    }
    if (text == "vn")
    {
        glm::vec3 currectNorm;

        iss >> currectNorm.x;
        iss >> currectNorm.y;
        iss >> currectNorm.z;

        fileNorm.push_back(currectNorm);
    }


    // Last part, hard part!
    if (text == "f")
    {
        // Face!!! :O
        // This one here I am having trouble with
        // How do I read it?
    }

v -1.000000 -1.000000 1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
vt 0.625000 0.000000
vt 0.375000 0.250000
vt 0.375000 0.000000
vt 0.625000 0.250000
vt 0.375000 0.500000
vt 0.625000 0.500000
vt 0.375000 0.750000
vt 0.625000 0.750000
vt 0.375000 1.000000
vt 0.125000 0.750000
vt 0.125000 0.500000
vt 0.875000 0.500000
vt 0.625000 1.000000
vt 0.875000 0.750000
vn -1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
f 2/1/1 3/2/1 1/3/1
f 4/4/2 7/5/2 3/2/2
f 8/6/3 5/7/3 7/5/3
f 6/8/4 1/9/4 5/7/4
f 7/5/5 1/10/5 3/11/5
f 4/12/6 6/8/6 8/6/6
f 2/1/1 4/4/1 3/2/1
f 4/4/2 8/6/2 7/5/2
f 8/6/3 6/8/3 5/7/3
f 6/8/4 2/13/4 1/9/4
f 7/5/5 5/7/5 1/10/5
f 4/12/6 2/14/6 6/8/6

Solution

  • int i, j, k;
    char c;
    iss >> i;
    iss >> c; // remove the /
    iss >> j;
    iss >> c; // remove the next /
    iss >> k;
    

    store the i,j,k somewhere and so on, then repeat as many times as needed.