How to configure / calculate texture buffer in glTexCoordPointer when importing from OBJ file

I am parsing an OBJ file in Android, and my goal is to render and display the object. Everything works fine, except for the correct display of the texture (importing a resource / image into opengl, etc. Works great).

I don't know how to fill texture related data from an obj file into a texturebuffer object.

In the OBJ file, I have 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 drawing procedure 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 counters 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 advice is appreciated.

+4
source share
1 answer

Sorry, not sure how much this patronizes, but from above:

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

Defines a triangle between a vertex with the 127th position specified in the file, the 73rd position of the texture in the file and the 62nd normal and two other vertices indicated in the same way.

In OpenGL, you only point one index to the top (since it all goes through the transformation as a whole), so you need to find out all the different combinations of position, texture and normal coordinates and arrange the buffers go to glVertexPointer , glNormalPointer and glTexCoordPointer respectively. For example, the above side can be specified via GL_TRIANGLES as between vertices 0, 1 and 2, where you copied the 127th position to the first in your internal vertex buffer, you copied the 73rd texture coordinate to the first in your internal texture coordinate buffer, and you copied the 62nd normal number to the first in your internal regular list.

Most likely, you want HashMap to take a combination of key vertices / coordinate / normal textures and comparisons with the position in internal arrays. When you encounter every face in the incoming OBJ, you can check to see if you have this combination highlighted in your internal buffers and give it the next available one if not.

vt represents texture coordination, e.g. texture coordinate:

 vt 0.495011 0.389417 

means that in the texture space x = 0.495011, y = 0.389417. From memory, the OBJ and OpenGL file formats have y axes in opposite directions. So in terms of OpenGL you need s = 0.495011, t = 1 - 0.389417 = 0.610583.

If your object uses a suitable complex atlas or texture skinning, I suggest that it would be far from obvious that the y coordinates are inverted; if you have something that looks like a cube with the same texture that repeats completely on each face, then you will probably just see that it is flipped vertically.

Is this a source of confusion?

+3
source

All Articles