Jump to content
  • Advertisement
Sign in to follow this  
Marusu

OpenGL Rendering textures from obj file

This topic is 2517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
It is a very common question so it went into the FAQ
http://www.opengl.org/wiki/FAQ#Multi_indexed_rendering

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!