iDevGames Forums
Reading texture colors values - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Reading texture colors values (/thread-3150.html)



Reading texture colors values - Leroy - Jul 24, 2007 02:14 PM

I'm working on enabling the player to select objects(textured quads) in my 2d game. So first, I check if a mousedown has occurred in the bounding rect of the quad, and if it has, I want to check the corresponding color value of the texture's pixel which I clicked, for transparency(the alpha value). How do I do this(I'm using tga)?


Reading texture colors values - ThemsAllTook - Jul 24, 2007 08:02 PM

The file format doesn't make a difference. If you keep the buffer you pass to glTexImage2D around, you can simply examing the alpha value at the appropriate pixel location.


Reading texture colors values - Leroy - Jul 24, 2007 08:54 PM

ThemsAllTook Wrote:The file format doesn't make a difference. If you keep the buffer you pass to glTexImage2D around, you can simply examing the alpha value at the appropriate pixel location.

I'm not sure I understand quite what you're saying(my understanding of opengl is terrible). Are you saying for me to use the coord on the selected quad as an index to it's texture's image data? And if so, how do I parse through that data to retrieve the alpha values?


Reading texture colors values - unknown - Jul 24, 2007 09:09 PM

you can just call glReadPixels to get the color of a pixel on screen.


Reading texture colors values - Leroy - Jul 24, 2007 09:29 PM

unknown Wrote:you can just call glReadPixels to get the color of a pixel on screen.

Yeah but when Im trying to get the alpha value of a sprite, ill end up getting the alpha value of the tile under the sprite, right?


Reading texture colors values - Skorche - Jul 24, 2007 09:47 PM

Yeah, you don't want to call glReadPixels. You want to read it out of your original image.

Assuming that your texture is a vanilla 32bit RGBA image, then you would do the following.

Code:
unsigned char *pixels; // pointer to your texture data
unsigned char alpha = pixels[4*(y*width + x) + 3];



Reading texture colors values - Leroy - Jul 24, 2007 10:06 PM

Skorche Wrote:
Code:
unsigned char *pixels; // pointer to your texture data
unsigned char alpha = pixels[4*(y*width + x) + 3];

That's what I was looking for, thanks.