LWJGL Forum

Programming => OpenGL => Topic started by: Marat on September 08, 2014, 15:46:30

Title: Signed unsigned byte confusion
Post by: Marat on September 08, 2014, 15:46:30
Hello there.
I'm trying to load a texture and draw a simple textured triangle. Everything works fine, but I don't understand why using GL_UNSIGNED_BYTE type in this call

GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, texture.getWidth(), texture.getHeight(), 0, GL12.GL_BGR, GL11.GL_UNSIGNED_BYTE, texture.getByteBuffer());

produces different colors than when I use GL_BYTE. As far as I know Java uses two's complement byte representation and so does OpenGL.

So for a red component of a pixel it is like this, for example:

Red = 208 (dec unsigned byte) = 11010000(bin) = -48(dec in java). What am I missing?

Thanks for any answer.
Title: Re: Signed unsigned byte confusion
Post by: abcdef on September 15, 2014, 15:32:39
The reason is because opengl clamps the values to the 0-255 range

Signed (Range = -128 to 127)

11010000 = -48 -> Clamps to 0

Unsigned (Range = 0 to 255)

11010000 = 208 -> No clamping necessary as it is in the correct opengl range.