How to setup/calculate texturebuffer in glTexCoordPointer when importing from OBJ-file
Asked Answered
D

1

4

I'm parsing an OBJ-file in Android and my goal is to render & display the object. Everything works fine except the correct texture mapping (importing the resource/image into opengl etc works fine).

I don't know how to populate the texture related data from the obj-file into an texturebuffer-object.

In the OBJ-file I've vt-lines:

vt 0.495011 0.389417 
vt 0.500686 0.561346

and face-lines:

f 127/73/62 98/72/62 125/75/62

My draw-routine looks like (only relevant parts):

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glNormalPointer(GL10.GL_FLOAT, 0, normalsBuffer);

gl.glTexCoordPointer(2, GL10.GL_SHORT, 0, t.getvtBuffer());

gl.glDrawElements(GL10.GL_TRIANGLES, t.getFacesCount(), GL10.GL_UNSIGNED_SHORT, t.getFaceBuffer());

Output of the counts of the OBJ-file:

Vertex-count: 1023
Vns-count: 1752
Vts-count: 524
/////////////////////////
Part 0
Material name:default
Number of faces:2037
Number of vnPointers:2037
Number of vtPointers:2037

Any advise is welcome.

Daglock answered 20/11, 2010 at 14:18 Comment(0)
R
3

Sorry, not sure to what extent this is patronising, but from the top:

f 127/73/62 98/72/62 125/75/62

Means a triangle between the vertex with the 127th position given in the file, the 73rd texture position in the file and the 62nd normal and the two other vertices specified in a similar way.

In OpenGL you specify only a single index per vertex (as the whole thing goes through the transform as a single unit), so you need to figure out all the different combinations of position, texture coordinate and normal and arrange the buffers you pass to glVertexPointer, glNormalPointer and glTexCoordPointer accordingly. For example, the above face might end up being specified via GL_TRIANGLES as between vertices 0, 1 and 2, where you'd copied the 127th position to the first in your internal vertex buffer, you'd copied the 73rd texture coordinate to the first in your internal texture coordinate buffer and you'd copied the 62nd normal to the first in your internal normal list.

Quite probably you want a HashMap taking the combination key vertex/texture coordinate/normal and mapping to a position in your internal arrays. As you come across each face in the incoming OBJ, you can check whether you've already got that combination allocated to a spot in your internal buffers and give it the next available one if not.

vt is a texture coord, so e.g. the texture coordinate:

vt 0.495011 0.389417

Means that, within texture space, x = 0.495011, y = 0.389417. From memory, the OBJ file format and OpenGL have the y axes in opposite directions. So in OpenGL terms you'd want s = 0.495011, t = 1 - 0.389417 = 0.610583.

If your object is using a suitably complicated texture atlas or skinning, I imagine it would be far from obvious that the y coordinates are flipped; if you just have something like a cube with the same texture repeated in its entirety on each face then you'd probably just see it as being flipped along the vertical.

Does that cover the source of confusion?

Refer answered 22/11, 2010 at 18:30 Comment(3)
Hi Tommy,thanks for the explanation/hints. However I'm not 100% enlightened. I'm still not sure how to build the hashmap (or list,vector..) from the different combinations of v,vn and vt ? Are there constraints which you must follow (this might help me in understanding the context)? For example: Must the size/count of vertice-,normal- and texture-Hashmap always be the same?Daglock
I can't really do Android specifics, just conceptuals. A HashMap maps a key to a value. So you could have the string '127/73/62' as your key and the array position in memory as the value. Look up 127/73/62 and if it returns a number, put that wherever you put things to pass to glDrawElements. If it doesn't then take the next slot in your memory arrays for vertex/texcoord/normal and write the combo there. Then put the slot number into the HashMap as the value for that key and use it in what you're building for glDrawElements.Refer
Hi Tommy, finally found a solution. I had to duplicate vertices according to the amount of normals in the obj-face list (like you said). thanks. I'll mark question as solvedDaglock

© 2022 - 2024 — McMap. All rights reserved.