How does Texture Mapping work?

I read the answers from What exactly is UV and UVW Mapping? and How does UVW texture mapping work?, which are quite nice, but am still not 100% sure, if I understand correctly.

Lets start with a 2D example. So, say I have a triangle (obviously described by 3 vertices). Now my question is, how do I convert a (x,y)-coordinate to the (u,v)-coordinate of my texture? Since {x,y} could be any value between [0,n] with n being all real numbers, considering that it is in object space. But my texture coordinates are between [0,1]. How do I know how to map lets say (3,4) to (u,v)? If I know how to map the object coordinates to the texture coordinate it is easy to interpolate the values, I assume (either using bilinear interpolation or barycentric interpolation).

And then how would this work for 3D? Lets say in this case we would have a pyramide with 5 vertices (4 bottom, 1 tip). I guess the procedure would be similar, with the exception that I know have an additional depth value. But how does the mapping of a 2D texture work on a 3D object, when I don't have nice flat surfaces like on a pyramide, but instead have a circular surface, like a tea pot?

I hope I'm clear in my questions. I'm still little confused myself. I just don't quite get the mathematical background of texture mapping.

It would be enough, if you could point me to some website with good explanation, maybe with clear graphics and step by step description.

How do I know how to map lets say (3,4) to (u,v)?

First of all, your assumption is wrong. You don't transform position coordinates to uv coordinates, your vertice already has a uv coordinate. Like the following:

Where (100,125) is the position of the first vertex (in pixels) and (0,0) is the UV coordinate. Mapping that point to the texture is a simply finding which pixel in the texture is the pixel (0,0). This is usually done for you in the shader when you use texture lookup functions. Finding the uv coordinate of any point in the triangle is done by using the 3 uv coordinates and finding out the average in that specific point.

The same happens when you have a 3D object, you will map your vertices to a 2D uv map (usually done by whoever is makes the model) and then use the UV coordinates for the 2D lookup.

• What if the model-maker does not give the (u,v) coordinates? Wikipedia says that you can also calculate the coordinates. Or do I just manually assign the UV values?
– Odie
Jul 7, 2014 at 14:57
• If the model-maker does not give (u,v) coordinates you can pick other methods to calculate it (like sphere/cube projection/etc0, but in the end you will still end up using uv coordinates for each vertex. Jul 7, 2014 at 15:07
• @Odie you can ask a new question about generating uvs in runtime, its an extensive topic and I don't know enough about it answer here. Jul 7, 2014 at 15:15

Each of the vertexes of the triangle will have a u,v coordinate assigned. When the triangle is projected onto the screen each point that is displayed on screen will have a set of barycentric coordinates.

Then to get the texture coordinate you just take the average of the texture coordinates weighted by the barycentric coordinates.

In openGL this is done automatically when you select varying as output of the vertex shader and input of the fragment shader.