1

The OpenGL model header file I'm working with contains definitions along the following:

static const float modelVertices[NUM_OBJECT_VERTEX * 3] = {}

static const float modelTexCoords[NUM_OBJECT_VERTEX * 2] = {}

static const float modelNormals[NUM_OBJECT_VERTEX * 3] = {}

static const unsigned short modelIndices[NUM_OBJECT_INDEX] = {}

Where there are a bunch of numbers (floats and integers, as fitting) separated by comma's inbetween the brackets.

It seems straight-forward to convert an .obj file's v, vt, vn to the above format. My .obj file also has a bunch of f's which include triplets separated by /. I'm not sure what these parameters are exactly...

Which parameters do I need to convert to get the fourth - the modelIndices?

(I need to admit in advance that I am a newbie to OpenGL, so apologies if this seems too elementary!)

1 Answer 1

1

Triplets are just a face definition.

If you have f 1 2 3

This means you have a triangle that is made of vertex of indices 1 2 and 3.

If all entries are like that, that means you can directly fill your modelIndices with those indices, and draw them using GL_TRIANGLES.

Now if they are separated by / this means you have different mapping between vertices position and texture coordinates and/or normals.

This is something OpenGL can't handle directly and the way to go for you is to explode the texcoord and normal data into arrays of the same size than vertice position array.

To do this is trivial: here's pseudo code:

read face data (triplets)
for each triplet
   read vertex indice
   read texcoord and normal indices
   fetch texcoord @ texcoord indice from your vt array
   store texcoord @ vertex indice in your modelTexCoords array
   fetch normal @ normal indice from your vn array
   store normal @ vertex indice in your modelTexCoords array

   etc

See also wikipedia's doc, which explain well .obj format: http://en.wikipedia.org/wiki/Obj

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

6 Comments

hmm, so you're saying that texcoord (for usage in modelTexCoords) is actually repeated in each /-triplet as vertex index/texcoord/normal index?
hmm, so basically vn is repeated in the normal index of modelTexCoords as well as in modelNormals ... also, in the case where you have f's with a bunch of 1/2/3's, the modelIndices is just the first element (1 in this case) of each f triplet?
yes that's right, modelIndices are just the first element of each f triplet. I don't understand what do you mean by 'vn is repeated in the normal index of modelTexCoords' though.
I had thought the vn's from the obj mapped directly to the modelNormals, but it seems the vn's are also duplicated again in the modelTexCoords
the other thing is each line of v and vn show triplets (separated by spaces), so do the index mapping refer to line numb
|

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.