问题
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.
回答1:
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?
来源:https://stackoverflow.com/questions/4233152/how-to-setup-calculate-texturebuffer-in-gltexcoordpointer-when-importing-from-ob