1

I'm attempting to get the alpha value of a texture with glReadPixel(). The problem I have is once the alpha value pixel is below 0.5f, it starts to combine the alpha value of the background object. I can never get an alpha value of 0.0f thru 0.49999f. Is there a way to ignore the alpha value of any background objects and get JUST the 1 pixel alpha value of the top texture or object?

enter image description here

std::vector< float > pixels( 1 * 1 * 4 );
glReadPixels(mouse.x,mouse.y,1,1,GL_RGBA,GL_FLOAT,&pixels[0]);

printf("alpha value = %f", pixels[3]);
aquawicket
  • 524
  • 1
  • 9
  • 27
  • Since you have a texture, you probably have it somewhere in memory. Why not just read directly from it? – E_net4 Sep 07 '12 at 21:36
  • That's what I ended up doing. Works f\perfect. I'll post my answer once i get the code clean up. – aquawicket Sep 07 '12 at 22:27

1 Answers1

4

I'm attempting to get the alpha value of a texture with glReadPixel()

glReadPixels returns data from the framebuffer, not a texture. So you are at the mercy of the product of everything that's been rendered (and whichever blending functions were in use at the time).

I suspect you are on the wrong track for whichever problem you are trying to solve.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
desimusxvii
  • 1,094
  • 1
  • 8
  • 10
  • I simply want to get the true alpha value of a texture or object when I click a pixel. That's all a need. – aquawicket Sep 07 '12 at 19:02
  • Can I do something along the lines of if(pixelIsInObject(x,y,object)){alpha = GetObjectAlpha(x,y,&object);} – aquawicket Sep 07 '12 at 19:11
  • 2
    Once pixels have been rendered and blended into the framebuffer their original values are lost. You'd need to get the position of the click on the rendered triangles and compute the texture coordinates and read the alpha value out of the original texture. Like I said, I think you should tell me what you are trying to solve, not how to fix your solution. – desimusxvii Sep 07 '12 at 19:11
  • OK. so If I could tell you the x,y pixel of the texture, then how do we get the alpha value of that part of the texture. I don't necessarily have 1 problem. I want to use this ability in many parts of my program. – aquawicket Sep 07 '12 at 19:13
  • 1
    "I want to use this ability in many parts of my program." .. WHY? There is probably a better way. – desimusxvii Sep 07 '12 at 19:26