Texture coordinates from OBJ file for use in Android
I have this OBJ file for a cube with texture:
# Max2Obj Version 4.0 Mar 10th, 2001
#
mtllib cube2.mtl
g
# object Cube_1 to come ...
#
v -5.500000 0.000000 -1.000000
v -5.500000 0.000000 1.000000
v -7.500000 0.000000 1.000000
v -7.500000 0.000000 -1.000000
v -5.500000 2.000000 -1.000000
v -5.500000 2.000000 1.000001
v -7.500000 2.000000 1.000000
v -7.500000 2.000000 -1.000000
v -5.500000 0.000000 -1.000000
v -5.500000 2.000000 -1.000000
v -5.500000 2.000000 1.000001
v -5.500000 0.000000 -1.000000
v -5.500000 2.000000 1.000001
v -5.500000 0.000000 1.000000
v -5.500000 0.000000 1.000000
v -5.500000 2.000000 1.000001
v -7.500000 2.000000 1.000000
v -5.500000 0.000000 1.000000
v -7.500000 2.000000 1.000000
v -7.500000 0.000000 1.000000
v -7.500000 0.000000 1.000000
v -7.500000 2.000000 1.000000
v -7.500000 2.000000 -1.000000
v -7.500000 0.000000 1.000000
v -7.500000 2.000000 -1.000000
v -7.500000 0.000000 -1.000000
v -5.500000 2.000000 -1.000000
v -5.500000 0.000000 -1.000000
v -7.500000 0.000000 -1.000000
v -5.500000 2.000000 -1.000000
v -7.500000 0.000000 -1.000000
v -7.500000 2.000000 -1.000000
# 32 vertices
vt 0.000500 0.999500 0.000500
vt 0.000500 0.000500 0.000500
vt 0.999501 0.000500 0.000500
vt 0.999501 0.999500 0.000500
vt 0.999500 0.999500 0.999501
vt 0.999500 0.000500 0.999501
vt 0.000499 0.000500 0.999501
vt 0.000499 0.999500 0.999501
vt 0.999500 0.000500 0.999500
vt 0.999500 0.999501 0.999500
vt 0.000500 0.999501 0.999500
vt 0.999500 0.000500 0.999500
vt 0.000500 0.999501 0.999500
vt 0.000500 0.000500 0.999500
vt 0.999500 0.000500 0.000500
vt 0.999500 0.999501 0.000500
vt 0.000499 0.999501 0.000500
vt 0.999500 0.000500 0.000500
vt 0.000499 0.999501 0.000500
vt 0.000499 0.000500 0.000500
vt 0.999500 0.000500 0.000499
vt 0.999500 0.999501 0.000499
vt 0.000500 0.999501 0.000499
vt 0.999500 0.000500 0.000499
vt 0.000500 0.999501 0.000499
vt 0.000500 0.000500 0.000499
vt 0.000500 0.999501 0.999500
vt 0.000500 0.000500 0.999500
vt 0.999501 0.000500 0.999500
vt 0.000500 0.999501 0.999500
vt 0.999501 0.000500 0.999500
vt 0.999501 0.999501 0.999500
vt 0.000500 0.999500 0.000500
vt 0.999501 0.000500 0.000500
vt 0.999500 0.999500 0.999501
vt 0.000499 0.000500 0.999501
# 36 texture vertices
vn 0.000000 -1.000000 -0.000000
vn 0.000000 -1.000000 -0.000000
vn 0.000000 -1.000000 -0.000000
vn 0.000000 -1.000000 -0.000000
vn 0.000000 1.000000 -0.000000
vn 0.000000 1.000000 -0.000000
vn 0.000000 1.000000 -0.000000
vn 0.000000 1.000000 -0.000000
vn 1.000000 0.000000 -0.000000
vn 1.000000 0.000000 -0.000000
vn 1.000000 0.000000 -0.000000
vn 1.000000 0.000000 -0.000000
vn 1.000000 0.000000 -0.000000
vn 1.000000 0.000000 -0.000000
vn -0.000000 -0.000000 开发者_Python百科1.000000
vn -0.000000 -0.000000 1.000000
vn -0.000000 -0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn -1.000000 0.000000 -0.000000
vn -1.000000 0.000000 -0.000000
vn -1.000000 0.000000 -0.000000
vn -1.000000 0.000000 -0.000000
vn -1.000000 0.000000 -0.000000
vn -1.000000 0.000000 -0.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
# 32 vertex normals
g Cube_1
usemtl 01_-_Default_1
s 0
f 1/33/1 2/2/2 3/34/3
f 1/1/1 3/3/3 4/4/4
f 5/35/5 8/8/8 7/36/7
f 5/5/5 7/7/7 6/6/6
f 9/9/9 10/10/10 11/11/11
f 12/12/12 13/13/13 14/14/14
f 15/15/15 16/16/16 17/17/17
f 18/18/18 19/19/19 20/20/20
f 21/21/21 22/22/22 23/23/23
f 24/24/24 25/25/25 26/26/26
f 27/27/27 28/28/28 29/29/29
f 30/30/30 31/31/31 32/32/32
# 12 faces
g
As you can see vt have 3 entries per row. Does this mean is it 3d texture and not 2d? Android supports only 2d textures. So does it mean above OBJ file will not work in Android? I can parse OBJ and get float[] textcoordinates = [0.000500, 0.999500, 0.000500,0.000500, 0.000500, 0.000500, .....] Are these the correct first 2 tex coordinates that needs to be fed to OpenGL-ES? Or do I need to figure the texture coordinates from the f numbers in the OBJ file which AFAIK are indexes into texture coordinates to compute the correct tex coodinates. So I am not sure which is the right way. Somebody with experience with OpenGL-ES and OBJ file format can clear all this up.
This model is in fact using a 3d texture. The details of the obj file format are described at http://paulbourke.net/dataformats/obj/, here is the part about texture coordinates:
vt u v w
Vertex statement for both polygonal and free-form geometry.
Specifies a texture vertex and its coordinates. A 1D texture requires only u texture coordinates, a 2D texture requires both u and v texture coordinates, and a 3D texture requires all three coordinates.
The vertex data from the obj file can not be passed directly to opengl. You have to setup your arrays so that the corresponding indices in each array belong to the same vertex. As an example, the first vertex of the first face in your model (1/33/1) consists of
- the vertex coordinates from the first "v" line.
- the texture coordinates from the 33. "vt" line.
- the normal vector from the first "vn" line.
The easiest way to set this up for opengl would be to iterate over all faces, get the values for vertex, texture and normal and append these to new arrays.
精彩评论