views:

62

answers:

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  A: 

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

Stringer Bell
hmm, so you're saying that `texcoord` (for usage in `modelTexCoords`) is actually *repeated* in each `/`-triplet as `vertex index/texcoord/normal index`?
ina
yes, kind of...
Stringer Bell
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?
ina
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.
Stringer Bell
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`
ina
the other thing is each line of `v` and `vn` show triplets (separated by spaces), so do the index mapping refer to line numb
ina
To reformulate the question, what is the size of `v`, `vt` and `vn`? If they differ from `v`, that means you don't have same mapping for `vt` and `vn` and that's problematic for the reasons I've enumerated in my answer. About your last comment, that's right. Index mapping refer to line number!
Stringer Bell