How do I use face data in an OBJ file ?

Started by
0 comments, last by swiftcoder 11 years, 2 months ago

After watching this tutorial by the coding universe (

">
), I am confused as to what face data (denoted by an f at the start of the line) in a .obj file is, and how it is supposed to be used.

As an exercise to try and find this out for myself, I experimented and wrote a parser for an obj file in java, that only loaded the vertex and normal data from the Stanford bunny model. I rendered it using glDrawArrays, but it came out contorted, with lines connecting triangles that shouldn't be connected.

It looked like this:

http://derp.co.uk/9a0ba

You can just see the basic shape of the bunny under all the stuff that isn't meant to be there.

Do I need the face data to render the model ? Can you do it without it ? If so how do you use it and what does it mean ?

Advertisement
The face data defines which vertices make up each triangle in the model.

For example:
f 1 2 3
Defines a triangle made from the 1st, 2nd and 3rd vertices.

Ideally, you load all the vertices from the obj file into a single vertex array, and then load all the faces into an index array, for use with glDrawElements().

Unfortunately, things get significantly more complicated if you happen to have an obj file with normals and/or texture coordinates, because the face definition will give you an index for each. You pretty much have to build up all combinations of vertex/texcoord/normal, and remove the duplicates afterwards (regenerating the index array as you go).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement