Showing posts with label OpenGL. Show all posts
Showing posts with label OpenGL. Show all posts

Tuesday, January 11, 2011

What are UV / ST texture coordinates and UV mapping ?

I know many of you might be having trouble in understanding what UV/ST are. I too had during beginning of OpenGL texturing. Before eating too much time its just:

(U,V) = (X,Y) of texture.

U = X pixel position of image / image width ;
V = Y pixel position of image / image height ;

And we know how to cap something between 0 to 1 range. Just divide it by the maximum that's why we are dividing pixel position by width and height just to cap it between 0 to 1 range.

Now we do simple maths, multiply U value by 100 and we get pixel position in %age.
suppose U = 0.4 and V = 0.6, it simply means
40% width, and 60% height.

U is pointing to position = 0 + 40% of image width
V is pointing to position = 0 + 60% of image height

That is why texture coordinate system becomes
left = 0 (lowest width), right = 1 (max width)
bottom = 0 (lowest height), top = 1 (max height)


Image Source: = http://paulyg.f2s.com/graphics/uvexp.gif


EDIT:
Now what UV mapping is?
I'll try to explain in lame language.

Assume a triangle as your polygon data and a rectangular, balloon like, nonrigid sheet as texture, here we want to cover the triangle with desired portions of that sheet, what should we do in real world? Of-course we will try to stretch it around triangle in all possible ways,  we can start from wherever we want but limitation is simple, we have got 3 points to staple that sheet. this is exactly what OpenGL does. It asks which part of the texture you want to be mapped into which vertex of polygon. We can't tell OpenGL that hey put this UV in center on the polygon!

That's why the higher the number of vertices we have in polygon the better mapping we can do.

But in actual programming, we can assign almost any UV to any vertex. That would make the triangle with unusual patterns. I haven't tested this :-), try this in 3D modelling programs like Blender if you wanna save your time and experiment quickly on UV mapping. Then export your simple mesh into OBJ format, which you can read on text editors as well.

The best resources for studying OpenGL, OpenGL ES

I learned OpenGL in 2009 and completed my Greh game engine in mid 2010 with my own Game framework for menu, sounds, ai, collision etc.

However i would like to help others in their journey for OpenGL. I strongly recommend reading these resources instead of buying many books. Switching to resources too much without fully giving time to them just wastes time and demoralizes us.:-

1. OpenGL redbook: it's enough for studying OpenGL concepts. Click this link
2. The simplest and nicely described tutorial on OpenGL + shaders (i.e. covers OpenGL ES 2.0 also ).
    Click this link  pointer to http://duriansoftware.com/joe/
3. Nehe (Neon Helium) GL tutorials. Link
4. Going to learn about rendering models, UV mapping etc. Link
5. GLSL, OpenGL ES 2.0. Link
6. Good samples for some of important OpenGL techniques. Link
If you know more links then please let me know.