This topic is 4161 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
It's not a GL problem.
You need to know where on the polygon it was clicked and use barycentric coordinates to figure which st value is present where the user clicked.
If you have a 2D game, it will be easy to calculate this stuff.

##### Share on other sites
I must say, that was a particularly vague and unhelpful comment. I should have been more specific I guess. Here is my pseudo-code up until the point:

point world_click = get_world_click();

Now that I have quad_click, how do I figure out the alpha value of the corresponding texel for the point on the quad which the player clicked?

##### Share on other sites
The answer is in my previous post : barycentric coordinates
You need to know on which triangle the user clicked on, where exactly it was on the triangle. The mathematics involved with barycentric coordinates can be used to get the st coordinates.

Convert the st coordinates : just multiply by your texture dimension.
So if your texture is in RAM, you'll get the RGBA of the texture.

Or you could keep it simple if you are doing a 2D application and you just have rectangles. No need to worry about the complex math with barycentric coordinates.

##### Share on other sites
I undertsand how to get thebarycentric coordinates, my only question is(my grasp of opengl is extremely limited) how do I get the rgba value? Do I call some opengl function or what? Could you please show me some pseudo-code or something?

Thanks.

float pixel[4];

##### Share on other sites
glReadixels will work if the framebuffer has an alpha channel but why depend on system settings?
It's more reliable to store your textures in RAM. If you don't have it, use glGetTexImage

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 26
• 10
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633717
• Total Posts
3013509
×