'About OpenGL texture coordinates

I know that I must call one of the following before each call to glVertex:

glTexCoord(0,0); 
glTexCoord(0,1); 
glTexCoord(1,1); 
glTexCoord(1,0); 

But I have no idea what they mean. I know, however, that if I multiply (or is that divide?) the right side (or is it all the ones?) by two, my texture expands, and if I do the opposite, my texture repeats twice. I've managed to code a texture atlas by applying operations until it worked. But I have no proper idea about what's going on. Why does dividing these coordinates affect the image and why does reversing them mirror it? How do texture coordinates work?



Solution 1:[1]

OpenGL uses inverse texturing. It takes coordinates from world space (X,Y,Z) to texture space (X,Y) to discrete space(U,V), where the discrete space is in the [0,1] domain.

Take a polygon, think of it as a sheet of paper. With this:

glTexCoord(0,0); 
glTexCoord(0,1); 
glTexCoord(1,1); 
glTexCoord(1,0); 

You tell OpenGL to draw on the whole sheet of paper. When you apply modifications your texturing space modifies accordingly to the give coordinates. That is why for example when you divide you get the same texture twice, you tell OpenGL to map half of your sheet, instead of the whole sheet of paper.

Solution 2:[2]

Chapter 9 of the Red Book explains this in detail and is available for free online.

http://www.glprogramming.com/red/chapter09.html

Texture coordinates map x,y to the space 0-1 in width and height texture space. This is then stretched like a rubber sheet over the triangles. It is best explained with pictures and the Red Book does this.

Solution 3:[3]

For 2D image textures, 0,0 in texture coordinates corresponds to the bottom left corner of the image, and 1,1 in texture coordinates corresponds to the top right corner of the image. Note that "bottom left corner of the image" is not at the center of the bottom left pixel, but at the edge of the pixel.

Also interesting when uploading images:

8.5.3 Texture Image Structure

The texture image itself (referred to by data) is a sequence of groups of values. The first group is the lower left back corner of the texture image. Subsequent groups fill out rows of width width from left to right; height rows are stacked from bottom to top forming a single two-dimensional image slice; and depth slices are stacked from back to front.

Note that most image formats have the data start at the top, not at the bottom row.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cristina
Solution 2 dorbie
Solution 3