GLSL get Pixel rgb values as byte

Accname

2D-Graphics enthusiast
Reaction score
1,462
Hi guys, quick question.

Is it possible to get the RGBA values for a pixel in a texture as bytes instead of floats?
I am writing a fragment shader by the way.
I know i can do this:
Code:
vec4 color = texture2D(tex, gl_TexCoord[0].st);
Then i can call:
Code:
float red = color.r;
To get the r value for the pixel as a float between 0 and 1.
But i want to use these as an index in an array and i would like to have them as raw byte data between 0 and 255.
I know i could multiply by 255 but i dont think this will have perfect precision.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
I'm by no means a GLSL expert, but I think texture lookup is always performed via normalized floats.
So multiplying the single values by 255 is probably your best bet. Keep in mind that you don't even need perfect precision.
By treating the float as an unsigned byte you already willingly throw away precision. I doubt that the multiplication will have any more impact on that.
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
I'm by no means a GLSL expert, but I think texture lookup is always performed via normalized floats.
So multiplying the single values by 255 is probably your best bet. Keep in mind that you don't even need perfect precision.
By treating the float as an unsigned byte you already willingly throw away precision. I doubt that the multiplication will have any more impact on that.
But i would like to store data inside the texture instead of using its r,g,b values as actual colors.
I would like to have full control over these values to make some calculations in the shader.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Have you tried it?

Considering OpenGL does the conversion from byte to normalized float in the first place, you should be able to multiply it by 255 to safely get the original value again.
So it should give you the same control over your values as you'd have otherwise.

In case it doesn't, you'll see small inaccuracies in whatever you're projecting onto the screen.
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
I tried it. It worked to some extent, but I was not able to reproduce all values.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top