Rendering textures from obj file

Started by
3 comments, last by Brother Bob 12 years, 3 months ago
Hi,
I made a simple textured box in blender and exported to .obj file.
Content looks like this:

# Blender v2.61 (sub 0) OBJ File: 'skybox.blend'
# www.blender.org
mtllib skybox.mtl
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 0.744716 0.379594
vt 0.503906 0.379595
vt 0.503906 0.138785
vt 0.744716 0.138784
vt 0.744717 0.620404
vt 0.744717 0.861216
vt 0.503906 0.861216
vt 0.503906 0.620405
vt 0.263096 0.620405
vt 0.263096 0.379595
vt 0.022287 0.620405
vt 0.022287 0.379595
vt 0.985527 0.379594
vt 0.985527 0.620404
usemtl skybox_mat_skybox.png
s off
f 1/1 2/2 3/3
f 1/1 3/3 4/4
f 5/5 8/6 7/7
f 5/5 7/7 6/8
f 1/1 5/5 6/8
f 1/1 6/8 2/2
f 2/2 6/8 7/9
f 2/2 7/9 3/10
f 3/10 7/9 8/11
f 3/10 8/11 4/12
f 5/5 1/1 4/13
f 5/5 4/13 8/14


The problem is, that I have no idea how to correctly map textures.
As for now I only use vt for textures (ignore f).
I thought that opengl es requires vt to be exact same amount as v. But there we have more texture coordinates than vertices.
Can someone tell me or give me some urls on how to make sense of numbers after the slash signs in faces? Not what they mean, but how to use them.

Thank you,
Martin
Advertisement
"I thought that opengl es requires vt to be exact same amount as v. But there we have more texture coordinates than vertices."
Yes, OpenGL does, but obj does not require that. You can however expand the obj to have the same number of UVs and vertices.

To load the obj correctly you can't simply use the vertices supplied in the file, as lots of them usually need to be duplicated (or triplicated) in order to allow different UVs and normals per vertex.
For example, if you have a cube, and want each face of the cube to have different UVs (and in most cases, you'll want this), it means that each cube vertex needs to be duplicated 3 times.
So in the obj, you actually create your mesh vertices when you read the 'f' stuff, not when you read the 'v' (but of course, you need to read those to use them in the 'f')
The numbers after the slashes are the index of the UVs and normals.

For example:
f 5/5 4/13 8/14

This means to create a new face that uses vertex positions 5, 4 and 8, but uses UVs 5, 13 and 14.
You have to check if you already created a vertex that contains those attributes, otherwise create a new one.
It is a very common question so it went into the FAQ
http://www.opengl.org/wiki/FAQ#Multi_indexed_rendering
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Thank you both, Relfos explanation and V-man link were very useful, I finnaly managed to properly load a cube with texture, just like 3ds max does. smile.png
One notice though, I had to multiply each v coordinate by -1.0f and set texture wrap to repeat to properly map texture, otherwise it would map everything upside down. Is there an explanation to this?
You have probably loaded your texture upside down, or your modeling software uses another origin for the texture coordinates. If, for example, you load the image with the origin in the lower left corner, but your modeling software uses the origin in the upper right corner, the Y-axis will be inverted. Ensure that your application uses the same convention as the modeling software, that the image is loaded in the correct way, or change the model when you load it to transform from one convention to the other.

This topic is closed to new replies.

Advertisement