OpenGL Alpha problem.

Started by
9 comments, last by KoMaXX 10 years, 2 months ago

Hi, I am trying to render two partially transparent 2D sprites, with the same alpha value, one on top of the other, the transparency is achieved by setting the alpha value in the ColorArray.

My question is this: Is it possible that the intersection area of the two partially transparent objects to have the same initial alpha value? What I have now is something like the sum of both objects alpha value...

Please take a look at the attached image, which I think it describes the problem in a better way.

16bipuq.png

Your help would be much appreciated.
Thanks.

Advertisement

If the two overlapping object have the same alpha, then just write the alpha channel to the frame buffer without any kind of blending at all. The last object you draw already contains exactly the alpha channel you expect as a final result.

I am not sure if I did correctly understand what you do mean, but I want to state that those two overlapping objects are rendered to the screen among many other objects, each with it's own alpha. So I guess setting the frame buffer alpha would affect every rendered object(...) Also, disabling blending will affect my 2D textured objects area that is fully transparent, resulting the sprites to be rendered into "black box". Well I hope this makes any sense at all...

Could be there any possible solution to such issue? Thank you.

You can draw them with depth-testing, so that they never overdraw each other. Draw the one that is supposed to be in front first, then with depth-testing enabled the one that is supposed to be behind it will not be drawn, if it's drawn at an appropriate Z. (For example same Z and a depth-test that is GL_NOTEQUAL).

Thanks for the advice, I will do a quick search about the depth-testing, even though the depth thingy has been disabled (that's because the game is purely 2D, which helps avoiding any unecessary hassle) if I recall correctly.

Perhaps another solution that doesn't involve depth testing? :)

You can render the sprites without blending like Brother Bob suggests, writing the alpha, and then blend the background in with GL_ONE_MINUS_DST_ALPHA. So instead of blending sprites on top of the background, blend the background on top of the sprites.

Or draw into a separate render-target and then blit the result on top of the background.

It's rather difficult to understand exactly the effect you want to achieve. Perhaps you can post a screen-shot of how it looks now with real sprites, and draw an image of how it's supposed to look?

If I understand correctly (each pixel only drawn once) then depth-testing is likely the easiest solution.

This is how blending of any object in 3d rendering or the real world works. What are you specifically trying to do? What effect?

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

What are you specifically trying to do? What effect?

Thanks for asking... I've got a big 2D sprite, which is composed with 5 small separate textured 2D sprites, What I need is to create a shadow effect for the whole big sprite by stetting the R,G, and B values of every small sprite composing the big one to 0 (to give it a nice black color) and setting each one's alpha value to 50% in order to have a nice transparent black shadow effect.... The problem is that the intersection areas of the overlapping sprites look draker than the non-intersection areas.... Here is a small image that would better decribe all of this.

2ik8ww3.png

My question is, how can I get rid of that issue that's affecting my shadow effect? Is there any other way to achieve a nice shadow effect for 2D sprites without involving alphas (And unecessary 3d effects such lightings and cameras)?

Thanks.

Depth or stencil testing is an easy way to achieve that, especially if there's no such testing used for other things, as they can be dedicated to providing non-overlapping shadows.

Another way is to use a back-buffer with an alpha channel, and draw everything with alpha zero except for shadows which are drawn with color-masking to only draw alpha at 50%. Then when all shadows and sprites are drawn, draw a black square over the entire screen with (GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA) to fill in the shadows.

EDIT: (It can also be done the other way around with everything else at alpha 1.0, which is probably easier).

Thank you all for your kind help... I have done some quick research regarding the depth testing and the other solutions that were kindly proposed, and have realised implementing such solutions will make things even more complex. The graphic engine I have written is mainly for rendering 2D sprites, with some 2D capabilities such 2D rotation.... Thus I have opted to look else where and do some research about rendering into texture.

I think rendering into texture is the best and simplest way for me to render those shadows, which doesn't need too many alteration to the render engine. The idea is to render all the objects into a texture in runtime with 100% alpha value, then rendering this new texture to the screen, whith the desired 50% value.

This topic is closed to new replies.

Advertisement