Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do opengl texture coordinates work?

Tags:

opengl

I know that I have to do 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 proper idea of what's going on. Why does dividing these affect the image, and why does reversing them mirror it? How do texture coordinates work?

like image 493
mk. Avatar asked Apr 03 '11 21:04

mk.


People also ask

How do texture coordinates work?

Texture coordinates define how an image (or portion of an image) gets mapped to a geometry. A texture coordinate is associated with each vertex on the geometry, and it indicates what point within the texture image should be mapped to that vertex.

How do textures work in OpenGL?

A texture is an OpenGL Object that contains one or more images that all have the same image format. A texture can be used in two ways: it can be the source of a texture access from a Shader, or it can be used as a render target.

How many coordinates are needed when getting a sample from a cube texture?

When specifying your texture coordinates, you will then use sets of 3 coordinates instead of sets of 2. In a simple cube, you point to the 8 corners using normalized vectors.


2 Answers

Texture coordinates specify the point in the texture image that will correspond to the vertex you are specifying them for. Think of a rectangular rubber sheet with your texture image printed on it, where the length of each side is normalized to the range 0-1. Now let's say you wanted to draw a triangle using that texture. You'd take 3 pins and place them in the rubber sheet in the positions of each of your desired texture coordinates. (Say [0, 0], [1, 0] and [1, 1]) then move those pins (without taking them out) to your desired vertex coordinates (Say [0, 0], [0.5, 0] and [1, 1]), so that the rubber sheet is stretched out and the image is distorted. That's basically how texture coordinates work.

If you use texture coordinates greater than 1 and your texture is set to repeat, then it's as if the rubber sheet was infinite in size and the texture was tiled across it. Therefore if your texture coordinates for two vertices were 0, 0 and 4, 0, then the image would have to be repeated 4 times between those vertices.

enter image description here @b1nary.atr0phy Image for all you visual thinkers!

like image 106
geofftnz Avatar answered Oct 04 '22 20:10

geofftnz


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.

like image 36
Cristina Avatar answered Oct 04 '22 20:10

Cristina