Because OBJ is a file format that is completely unrelated to OpenGL.
Face statements in OBJ files can contain any number of vertices, not only 3 or 4. And it's not unusual for them to contain more than 4. So if you want to be able to read a reasonable variety of OBJ files, you need to be prepared to read any number of vertices per face.
This really does not add much difficulty. Unless you use an algorithm to analyze connectivity, and build meshes, you get a separate triangle per face statement anyway, even for files with only 3 vertices per face. So in that case, you create one triangle per face, and draw them with primitive type GL_TRIANGLES
.
So all that changes with more than 3 vertices per face is that you generate multiple triangles per face. You will need n - 2
triangles for a face with n
vertices. My answer to this question explains in more detail how this is done: Converting quadriladerals in an OBJ file into triangles?.
Another option is that you treat each face as a triangle fan, and render them with the GL_TRIANGLE_FAN
primitive type. A convex polygon (and all of this would break down if faces in the file are non-convex polygons) can always be drawn as a triangle fan, while keeping the vertices exactly in their original order. To treat each face as a separate triangle fan, you can use the primitive restart feature that has been available since OpenGL 3.1, using calls like:
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(...);
It's easy to tell that OBJ is not targeting OpenGL rendering. The much more cumbersome aspect is that OBJ uses separate indices for positions, normals, and texture coordinates, while OpenGL only supports a single set of indices which are used for all vertex attributes. This tends to cause much more difficulties for people writing parsers. See for example here: OpenGL - Index buffers difficulties.