View Full Version : Changing alpha of pixels on a texture?

05-23-2011, 02:05 PM
So here's the deal... I'm making a 2D game and am implementing an x-ray effect to allow the player to see through walls. The way the effect currently is implemented is to draw the area behind the wall on screen, then read this to a texture, then draw the wall, and then finally drawing the texture back onto the screen on top of the wall with a circular gradual alpha, meaning that the middle of the texture will have no alpha and the closer you get to the edge, the higher the alpha will be. This creates a gradual transparency effect -- a circular "x-ray" -- that lets the player see part of the area behind the wall.

This is all working fine and dandy. I read the pixels from a 128x128 area of the screen where the x-ray effect should be into an array of bytes. I can then just change the relevant bytes of the array to set the gradual circular alpha values... and then set this array of bytes as an image onto a texture. Works great. However, reading from the screen into the array with glReadPixels() is slow on older hardware, so I'm going to implement an FBO for graphics cards that support that and draw the part of the screen that needs the x-ray effect directly to a texture with render-to-texture instead of reading from the screen. But when I've rendered the image to the texture, I can't figure out how to set the alpha values of the pixels on the texture to get the same circular x-ray effect when drawing the texture to the screen.

Is it possible to change the alpha values of individual pixels on a texture/FBO? Or can I somehow draw another texture that has the correct alpha values to the FBO so that only the alpha values get "drawn" to the FBO, like transferring the alpha values of one texture onto another?

05-27-2011, 03:35 PM
Aha! I guess to get the alpha value of another texture onto the FBO, I just set glColorMask(0, 0, 0, 1) to only draw the alpha channel to the FBO?

05-28-2011, 05:21 AM
yes, correct!