Jump to content

  • Log In with Google      Sign In   
  • Create Account

OpenGL Alpha problem.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Samurai-X   Members   -  Reputation: 119

Like
0Likes
Like

Posted 01 February 2014 - 04:38 PM

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.



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8632

Like
3Likes
Like

Posted 01 February 2014 - 04:47 PM

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.



#3 Samurai-X   Members   -  Reputation: 119

Like
0Likes
Like

Posted 01 February 2014 - 05:34 PM

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.



#4 Erik Rufelt   Crossbones+   -  Reputation: 3644

Like
2Likes
Like

Posted 01 February 2014 - 05:45 PM

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).



#5 Samurai-X   Members   -  Reputation: 119

Like
0Likes
Like

Posted 01 February 2014 - 06:52 PM

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?  :)



#6 Erik Rufelt   Crossbones+   -  Reputation: 3644

Like
0Likes
Like

Posted 01 February 2014 - 07:03 PM

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.



#7 dpadam450   Members   -  Reputation: 949

Like
1Likes
Like

Posted 01 February 2014 - 09:40 PM

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



#8 Samurai-X   Members   -  Reputation: 119

Like
0Likes
Like

Posted 02 February 2014 - 08:19 AM

 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.


Edited by Samurai-X, 02 February 2014 - 08:23 AM.


#9 Erik Rufelt   Crossbones+   -  Reputation: 3644

Like
5Likes
Like

Posted 02 February 2014 - 10:43 AM

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).


Edited by Erik Rufelt, 02 February 2014 - 10:46 AM.


#10 Samurai-X   Members   -  Reputation: 119

Like
0Likes
Like

Posted 03 February 2014 - 07:18 AM

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.



#11 KoMaXX   Members   -  Reputation: 308

Like
0Likes
Like

Posted 14 February 2014 - 07:43 AM

Hi Samurai-X,

 

sure, rendering to a texture will most likely work but I would expect this to be more complex than using simple depth-buffering!

 

A bit of explanation:

If you turn on depth buffering, pixels will only be painted when they have a z-value that is less than what's already in the depth-buffer. So, when you paint your primitives in such a way that polygons closest to the camera are rendered first, the pixels behind will simply not be rendered, no blending occurs - and you get the desired effect!

 

You can achieve this easily if you give each polygon just a slightly different z-Value or a polygon offset (https://www.opengl.org/archives/resources/faq/technical/polygonoffset.htm)

Since you're in 2D this should not pose any problems.

 

Hope, that helps!


Go on, feed your brain: http://poroba.com/flip/flipz.php





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS