Advertisement Jump to content
Sign in to follow this  

OpenGL Understanding OpenGL blending and alpha

This topic is 1269 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

Hello, everyone!

I've been working a simple game, and right now I'm stuck with a blending problem while experimenting with particles (I am still learning to code OpenGL and stuff).

What I am trying to achieve seems very simple to me, though I don't know whether I am doing it the right way.
I am trying to draw several particles, where each particle is a simple textured quad of two triangles.

Here's the texture, I am using (it is the standard square 'hotspot' with alpha (png with transparency), a radial gradient from non-transparent center to transparent edges):
My actual color of the blurred circle is white, I just modified the image so that everyone could see it here on white background in this post.
Each quad-particle exists on the same plane (has same Z coordinate with others), I suppose this is somehow relevant to my problem.
Now, I want to draw a stream of those particles with color tinted to my desired color (or animated, whatever). Say, I want multiple particles with a blurred texture with green tint, so I need my own color (green) over that blurred texture's alpha, and that, overall over background mixed with other particles' colors.
So I am using the standard combination of glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) and I have blending enabled.

Whenever I draw a single particle everything seems to be ok. I have a green particle with blurred edges over my background without any artifacts.
But when I draw multiple particles in a stream, they somehow 'overlap' (I don't know the actual term), and this is the result I am getting:
[attachment=28360:Screen Shot 2015-07-31 at 23.18.46.png]
So instead of mixing the alpha of the texture with color of other particles and background color, there are edges of the quads visible in the stream.
In my fragment shader i define fragmentColor = myColor * texture (tex, texcoord), and I am using it with the mentioned blending function...

The question is: how do I make the quads lay one next to the other with 'mixing neighbouring colors' but without 'overdrawing those white texture edges'? Thanks in advance, sorry for my english.

Share this post

Link to post
Share on other sites

Ok, I think I got it now.
[attachment=28361:Screen Shot 2015-08-01 at 03.26.54.png]
The problem was indeed the overlapping z coordinate and the 'order' of particles with respect to depth buffer.
Both of the following solutions worked: either add z-displacement (each particle should have its own 'depth' and lay on its own plane), or disable depth buffer testing while drawing particles on a common z-plane (the latter is better).

Share this post

Link to post
Share on other sites

well those solutions work, but for 3D rendering it's most common to have the depth test on while setting the depth mask to 0.


this way you will have a depth test just to see if transparent objects are not occluded by opaque objects (transparent objects contribute to scene), while transparent objects cant occluded each other.

Share this post

Link to post
Share on other sites

it's most common to have the depth test on while setting the depth mask to 0

I didn't know about depth mask, thanks a lot for the hint!

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!