TheTrueJard
TheTrueJard

Reputation: 481

Use a texture's alpha channel as a border map

Okay, so this is a bit complicated (at least for me), and I haven't been able to find any kind of solution anywhere. I'm making a 2D golf game in OpenGL. The background is a texture that I made that will have a map of the course. The problem is that the borders, where the ball can't safely land (out of bounds, sand traps, etc.) is not geometric, so I cant just compare the x and y coordinates of the ball to a few values to see if it's out. One idea I have as a solution is to use the alpha channel of the texture to mark what pixels are out of bounds vs. in bounds. I would have my shader draw the RGB values regardless of the alpha. Then I would mark in-bounds as alpha==1.0f and out-of-bounds as alpha==0.0f, or something similar at least. Then I could sample the texture at the ball's x and y coordinates to see whether it's in or out. Most of this I can accomplish myself, except for one thing: How do I sample the texture from the CPU? I know that shaders on the graphics card can use the texture() function with a sampler2D and vec2, and that's all fine, but if the fragment shader's only output(s) is to color attachments (not counting gl_FragDepth or anything similar), then how do I get the returned alpha value back to the CPU? Or sample on the CPU somehow rather than using another shader? Or is this whole concept flawed? Any help is appreciated.

P.S. I'm using OpenGL 4, if that wasn't clear.

Upvotes: 0

Views: 68

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473537

OpenGL exists to do rendering. Giving OpenGL information for the sole purpose of later reading it back is wrong-headed.

You shouldn't pass this boundary information to OpenGL at all unless you intend to use it to draw pictures. And even if you do, keep a copy of it around on the CPU, so that you can query it without hassle. On the CPU, you simply compute the pixel coordinate you need to read from, and read from it.

It's no different from reading from a 2D array.

Upvotes: 2

Related Questions