How I make obj parser to parse for every vertex point a normal to that vertex?

  .obj, algorithm, c++, graphics, parsing

I have a problem with my parser. I want to make it so that it can parse only one normal per one vertex. But I am struggling to do so because there are different variations of the .obj files.

For example, if I parse cube.obj, it has only 6 normals and 12 indices. But some .obj files do not have the same amount of normals and vertices but have the same amount of indices and vertices, and I want to parse for every vertex as normal.

I already did a parser, here is a code:

void LoadMeshData(mesh_t* Mesh, char* FileName)
{
    FILE* file = NULL;
    fopen_s(&file, FileName, "r");
    
    std::vector<tex2d> TextureCoords;
    char Line[256];
    while(fgets(Line, 256, (FILE*)file) != NULL)
    {
        int32_t CharIdx = 0;
        char PrevChar = 0;
        char Char = Line[CharIdx];

        while(Char)
        {
            Char = Line[CharIdx++];
            if(Char == '#' || Char == 'm' || Char == 'o' && Char == 'u' || Char == 's') break;
            if((PrevChar == 'v' && Char == 't')) 
            {
                v3 Coords = ParseVertex(Line);
                tex2d UVCoords = V3ToUVs(Coords);
                Mesh->TextureCoords.push_back(UVCoords);
                break;
            }
            if((PrevChar == 'v' && Char == 'n'))
            {
                v3 Normals = ParseVertex(Line);
                Mesh->Normals.push_back(Normals);
                break;
            }

            if((PrevChar == 'v' && Char == ' '))
            {
                v3 Vert = ParseVertex(Line);
                Mesh->Vertices.push_back(Vert);
                break;
            } 
            if((PrevChar == 'f' && Char == ' '))
            {
                face_t Face = {};
                parsed_obj Obj = ParseFace(Line);

                //memcpy(Face.E, Obj.VertexIndices, sizeof(int32_t)*3);
                Mesh->Indices.push_back(Obj.VertexIndices[0]);
                Mesh->Indices.push_back(Obj.VertexIndices[1]);
                Mesh->Indices.push_back(Obj.VertexIndices[2]);
                break;
            }
            PrevChar = Char;
        }
    }
}

And here is examples of some of the mine .obj files:

enter image description here

enter image description here

enter image description here

I am still not that skillful at creating good algorithms, as I am still learning to do so. Is there a good way to make that parser parse for every vertex a normal?

Source: Windows Questions C++

LEAVE A COMMENT