David C. David C. - 1 month ago 6
Java Question

Why does LWJGL have a function that uses floats as coordinates?

I was fixing a problem in my code that I knew had something to do with this:

GL11.glTexCoord2f(x,y);
I changed it of course, but I wondered why it used floats as coordinates. I'm sure there is a reason, but it just seems extremely stupid to me. There is probably something I'm missing :P but whatever.

Answer

This is not specific to Java or LWJGL. As you might know, LWJGL is largely a 1:1 mapping of the original OpenGL API. And the different flavors of the glTexCoord function have been in OpenGL since version 1.0: https://www.opengl.org/sdk/docs/man2/xhtml/glTexCoord.xml

The reason of why texture coordinates are usually given as floating point values is that they refer to an "abstract" image (the texture). The size of this texture is not known. For example, you may have a mesh, and as its texture, you may use a JPG image that is 200x200 pixels large, or or an image that is 800x800 pixels large.

In order to allow arbitrary images to be used as the texture, the texture coordinates are almost always given as values between (0.0, 0.0) and (1.0, 1.0). They refer to the the total size of the image, regardless of which size this is.

Imagine a vertex with texture coordinates (0.5, 0.5). If the texture is 200x200 pixels large, then the actual "pixel" that will be used as this vertex is the pixel at (100,100). If the texture is 800x800 pixels large, then the pixel that will be used at this vertex will be the pixel at (400,400).

Using this sort of "normalized" coordinates (in the range [0.0,1.0]) is very common in graphics and other applications.

Comments