Jump to content
  • Advertisement
Sign in to follow this  
leiavoia

OpenGL Easy way to do "alpha masking"?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is what i want to do: first, i'm working in 2D, but the concept shouldn't change any. I have a sprite (textured quad) and i want the sprite to "dissolve" away. What i want to do is apply a "dissolvy looking" alpha mask texture to the sprite so that the alpha image eats away at the sprite data, but doesn't effect anything already underneath it (the background). Like so: Can this be easily done in OpenGL? I've never done multitexturing, but i think the answer lies there. Any help would be appreciated. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Sorry if I'm not completely understanding what you're trying to do, but your image and description seem to say different things, from my perspective. ;)

Could you try to describe your problem in another way? Do you mean removing pixels from the sprite's image to dissolve it? I don't see how this alpha mask will help you dissolve the sprite. Doh.

Share this post


Link to post
Share on other sites
bluntly:

I want to modify a texture's transparency by another texture.

Texture #1 is the sprite. Texture #2 is the "alpha mask" (a fairly common layering concept in photoshop, et al). Typically the alpha mask is a greyscale image.

Did that make better sense?

The actual effect i want to achieve is to have a bad guy "dissolve" when he gets zapped by a ray-gun. I could simply alter the textured quad's whole alpha value and "fade" him out, but i want to apply a looping series of alpha masks (that look like TV "snow") over the original graphic while it's also fading it out.

Share this post


Link to post
Share on other sites
Hmm, I haven't actually tried this, but it should work..

First render all your background.

Then turn off your R, G and B color channels, so that only Alpha is write enabled:
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE)
Draw your alpha mask then, making sure that the alpha values you want are in the alpha channel (duh, that sounds redundant!)

Re-enable RGB channels, then enable blending, and set your blend function to the following:

glBlendFunc(GL_DEST_ALPHA, GL_ONE_MINUS_DEST_ALPHA);

that should blend whatever you draw with the background, according to the alpha value you set with your alpha mask.

You'll have to switch the order of the 2 blend func arguements depending on if you want white to be all background or all foreground.

That's it! No multitexture/shader need...should be pretty fast.

Once again, haven't actually tried this, but if you follow the logic, it makes sense.

Share this post


Link to post
Share on other sites
The way you can do it with multitexturing is: Set your RGB texture to texture unit 0 and set the texture environment setting to GL_MODULATE with glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) (This is actually the default texenv setting for a texture unit, so really no change is necessary unless you previously modify this setting earlier in the render process without setting it back). Set the alpha texture (must be of type GL_ALPHA) to texture unit 1 and change the texenv mode to GL_REPLACE. You can do this by setting the active texture to stage 1, then calling glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE). This has the effect of taking RGB data from stage 0, and alpha data from stage 1.

Then, set your blend func to glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) to perform 'standard' masking, where parts with alpha=0 are drawn transparent and parts with alpha=1 are drawn opaque. Parts with fractional alpha are drawn blended.

This is how I used to do terrain transitional blends. Works pretty well, as long as you are careful with your texture state changes, since multi-texturing introduces a second texture stage parameter and thus an additional possible source of slowdown if state changes are not handled well. Just at a glance it looks like renderer's method should probably work pretty well as well (thanks, renderer), so I'd try both and see which you prefer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!