# What coordinates are we passing to pixel shader from vertex shader?

I have read articles about shader programing and understood the very basic knowledge of shader programing. One thing I always get confused is about the texture mapping. What I pass(output) from VS to PS is the vertex position and Texture Coordinates. I understood that in VS we can simply choose to pass the vertex position as it is or manipulate vertex position here (and may be something more). So what we ultimately pass is the position of the vetex. But... it is still not clear to me that we are passing which we are calling as texture coordinates or UV?

If I understood correctly, if my model is a simple triangle consist of 3 vertices, VS will be run three times for each of the vertix and I am passing the position of the vertex. Its straight forward. But what is with the texture? Say, I am using 50x50 jpg image as texture. How are the 3 vertices are mapped to this 50x50 pixel texture?

From the book I have started reading couple of days ago explained that the rasterizer is the one which groups the vertices to form triangle and calculates the number of pixel in the triangle. But again its not clear to me that what we are passing as texture coordinates to VS?

## 1 Answer

Texture Coordinates are usually expressed in the range between [0,1]. Each (textured) vertex will have these coordinates. These coordinates are mapped to texels in the actual texture. [0,0] is the top left corner, [1,1] the bottom-right corner. When the coordinates are in a range that is multiple of 1, the texture will repeat itself.

For example, for a pixel halfway between v0 having t[0,0] and v1 having t[1,1], the rasterizer will interpolate the texture coordinates value, resulting in t[0.5,0.5]. The texture lookup would then sample the texel in middle of the texture.

• Sorry this not yet clear to me. Can you please give example how a 20x20 image is interpolated/mapped to a triangle with vertices v1(0,0,0) v2(10,0,0) and v3(0,10,0)? – Noob Game Developer Jul 6 '14 at 15:42
• It depends on the actual texture coordinates assigned to those vertices. A texture mapped to one triangle is an extreme case. If we assume that they have respectively t0(0,1) t1(1,1) t2(0,0) only one (triangular) half of the square texture would be shown. Since there is no upper-right vertex t3(1,0) no texels from the other half of the square will be sampled. – TheWanderer Jul 7 '14 at 14:04