PDA

View Full Version : Access single bit of a texture...



capedica
04-28-2005, 01:40 AM
How can i access single bits of a texture? without too operation (there are not shift or AND-OR operator).
Thank you

mrbill
04-28-2005, 04:58 AM
An implementation (there are clearly obvious optimizations here, in particular replacing the divide by pow(2.0,shiftRight) ) for illustration purposes only:

-mr. bill

uniform sampler2D Texture0;
uniform int shiftRight; // shiftRight=0 for LSBit, shiftRight=7 for MSBit
varying vec2 texCoord;

void main(void)
{

float test = texture2D( Texture0, texCoord ).r;
float testBit = floor( test*255.0 ); // Map from [0.0,1.0] to [0.0,255.0]
testBit /= pow( 2.0, float( shiftRight ) ); // Shift right by shiftRight bits
// This puts bit of interest just left of fixed point
testBit = floor( testBit ); // Discard fractional bits
testBit *= 0.5; // Shift bit of interest right one
testBit = fract( testBit ); // Will now be 0.0 or 0.5
testBit *= 2.0; // Map from [0.0,0.5] to [0.0,1.0]
gl_FragColor = vec4( testBit );

}