Jump to content
  • Advertisement
Sign in to follow this  
oblivion_ocean

UV Mapping

This topic is 2468 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

Hoping to get a detailed response!

I'm wondering how I can read in a UV texture map that looks something like this:
http://oi43.tinypic.com/osc2tu.jpg

The above was saved from a very simple sphere and saved as an object file.
I am using OpenGL and want to import it using an object loader.

Here are some snippets from that file if this offers a clue to how faces are organized.

v 0.788558 0.182857 -0.583303
v 0.763021 -0.552948 -0.325232
v 0.000000 0.441661 -0.871224
v 0.000000 0.947624 -0.233366
v 0.000000 0.172146 0.960696

vt 0.527612 0.590604
vt 0.619266 0.558969
vt 0.620496 0.648223
vt 0.507932 0.493025
vt 0.578067 0.505968

f 1/4 17/5 39/1
f 16/2 39/1 17/5
f 3/13 13/14 6/15
f 6/15 13/14 35/16
f 3/13 14/17 13/14

Wondering if glTexCoord2f() is all I need with the vt values?

Any help would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Your question is a bit unclear. The texture map as an image has to be read as-is, besides that it needs to be converted from its file format (e.g. JPEG, PNG, ...) into a bitmap that can then be passed to create an OpenGL texture. Any UV mapping happens on the GPU when interpreting the given UV co-ordinates. Hence the magic, if any, would happen when reading the OBJ file and constructing OpenGL vertices from it.

Readers for OBJ files are discussed several times here on GDnet. The last recent seems to be this thread, which also provides full C++ source code for reading an OBJ file. It ends up with invoking D3D, but reading and baking the vertices as done in that sample should be useable for OpenGL as well.

Share this post


Link to post
Share on other sites
I've already created the loader and everything, my question has to do with the vt values specifically.

Is it as easy as plugging them into glTexCoord2f()? Just wanted to ask before I spent the time.
Not sure if there are variations on how UV Mapping is done so, thought I'd ask here first.

Share this post


Link to post
Share on other sites

... my question has to do with the vt values specifically.

Is it as easy as plugging them into glTexCoord2f()?...

In OpenGL each single vertex is required to have all of its attributes, and only such a compound can be addressed (either directly or by an index). I.e. if you work with the attributes "vertex position" and "texture co-ordinate" and you have N vertices, then you must provide N vertex positions and N texture co-ordinates. But OBJ isn't defined that way. In an OBJ file you may have N positions (i.e. N lines starting with 'v') and M texture co-ordinates (i.e. M lines starting with 'vt'), and that in an unrelated order. Hence you need to pair the positions and texture co-ordiantes as prescribed by the 'f' lines, and have to pass those pairs to OpenGL. This pairing usually means to duplicate some of the texture-cordinates. However, in the end you need to have exactly 1 texture co-ordinate tuple for each vertex position you pass to OpenGL.

Share this post


Link to post
Share on other sites
"However, in the end you need to have exactly 1 texture co-ordinate tuple for each vertex position you pass to OpenGL."

That's all I'm looking for; just wanted to be sure there was nothing else going on before I jumped in to finish this object loader.
I'll follow up here if required thanks.

Share this post


Link to post
Share on other sites
Each face has uv-coords assigned to vertices. Some faces that share the same vertices, might be mapped differently (such as seams and discontinuities in the texture). Now if there are no discontinuities, then both faces with have the same exact uv value for the shared vertex. Since a vertex can only hold 1 single UV value, then you have to decide if this vertex which is shared by multiple faces, has the same uv value for all shared faces or not. If it is the same across shared faces, then it only has 1 UV. If the vertex has different uv-values depending on which face is sharing it, then you have to duplicate that vertex and give it the proper uv value for each face.

Share this post


Link to post
Share on other sites

Each face has uv-coords assigned to vertices. Some faces that share the same vertices, might be mapped differently (such as seams and discontinuities in the texture). Now if there are no discontinuities, then both faces with have the same exact uv value for the shared vertex. Since a vertex can only hold 1 single UV value, then you have to decide if this vertex which is shared by multiple faces, has the same uv value for all shared faces or not. If it is the same across shared faces, then it only has 1 UV. If the vertex has different uv-values depending on which face is sharing it, then you have to duplicate that vertex and give it the proper uv value for each face.

Since the vertices result from the OBJ file, and the OBJ file defines vertices (notice that I don't speak of pure vertex positions) as an index pair of vertex position and texture co-ordinates (in this case at least), you don't have the problem of "finding" a suitable texture co-ordinate for a given position; it is already defined by the model, be it with or without discontinuities.

A thing you can do, however, is to look up duplicate pairs of vertex positions and texture co-ordinates. Usually there are duplicates in an OBJ file. So you can optimize the mesh by not duplicating the vertices in OpenGL as well but re-using the formerly created ones. This way requires to use vertex indexing, of course.

Share this post


Link to post
Share on other sites
Is there a sample someone can post so I can try this out because I'm running into issues,
even though I believe I am doing everything correctly and passing the correct values to
glTexCoord2f() for all verts.

It has to be something dumb or some variation I'm not aware of but I would like a reliable
sample to be sure or if someone would like to try importing this, I will post it somewhere.

Object File : http://www.text-uplo...02841&c=4587487
Texture Image : http://oi43.tinypic.com/2d2g7xd.jpg

Here it is, take a look.
Got the shape but the texture is just off.

Share this post


Link to post
Share on other sites
E.g. in this post you can see a C++ sample that groups vertex positions with texture co-ordinates to vertices. It also avoids duplicate vertices.

What about posting your relevant code snippets here? It may be the easiest way to find the solution...

Share this post


Link to post
Share on other sites

E.g. in this post you can see a C++ sample that groups vertex positions with texture co-ordinates to vertices. It also avoids duplicate vertices.

What about posting your relevant code snippets here? It may be the easiest way to find the solution...


Never mind, figured it out.
Turns out, the image was vertically flipped (likely from saving to bmp).

Thanks for your time!

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!