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

后端 未结 1 1121
北海茫月
北海茫月 2021-01-06 03:37

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/imag

相关标签:
1条回答
  • 2021-01-06 04:07

    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?

    0 讨论(0)
提交回复
热议问题