Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: Access single bit of a texture...

Hybrid View

  1. #1
    Intern Contributor
    Join Date
    Mar 2005
    Posts
    64

    Access single bit of a texture...

    How can i access single bits of a texture? without too operation (there are not shift or AND-OR operator).
    Thank you

  2. #2
    Intern Contributor
    Join Date
    Sep 2001
    Location
    Marlboro MA
    Posts
    96

    Re: Access single bit of a texture...

    An implementation (there are clearly obvious optimizations here, in particular replacing the divide by pow(2.0,shiftRight) ) for illustration purposes only:

    -mr. bill
    Code :
    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 );
     
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •