OpenGL Transparency with glColor4b?

Started by
2 comments, last by sniper227 9 years, 3 months ago

Hey guys, so I got transparency working using glColor4f and a couple of other functions but glColor4f is used to set how transparent an object is and i'm curious on how I could use glColor4b instead because I need it since i generate different colors from 0-255 and I cant figure out how to get transparency working with it like in glColor4f. Thanks

Advertisement

Internally OpenGL uses floating point numbers in the range [0.0,1.0] for RGB colors and Alpha. For transparency you have used a number less than 1.0 in the A channel.

Any other number representation for color channels, like an unsigned 8 bit integer value as in glColor4ub, is scaled and rounded to fit naturally into its value space. For example, an unsigned byte can store a minimum value of 0 and a maximum value of 255. To yield this from the internal color value range from 0.0 to 1.0, you need to scale by 255 and round to an integer value.

So: If you previously used glColor4f( 0.2f, 0.3f, 1.0f, 0.5f ) you have the equivalent glColor4ub( 0.2f*255, 0.3f*255, 1.0f*255, 0.5f*255 ) where you can, of course, calculate and use the final values as well. (The rounding to an integer value is done automatically here because the compiler inserts castings.)

Although i've written "the equivalent" above, it is not exactly equivalent. This is because one cannot store as much information in 8 bits (of an unsigned integer number) as one can store in 32 bits (of a floating point number). That's the price you have to pay for the advantage to use less memory.

BTW: The glColor routines are deprecated for a long time now.

EDIT: Oops: Brother Bob has noticed a mistake I made. See below. I've corrected the above writing to use glColor4ub, since in the OP the value range [0,255] is mentioned explicitly.

What haegarr said, except that the b suffix is for signed bytes; thus, the range is likely 0 to 127 instead. The suffix for unsigned byte is ub.

Thanks a lot guys ! It works :D

This topic is closed to new replies.

Advertisement