Understanding OpenGL blending and alpha

Started by
4 comments, last by p1p1 8 years, 8 months ago

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):
[attachment=28358:explosion-black.png]
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.

Advertisement

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

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.


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!

http://www.andersriggelsen.dk/glblendfunc.php

note: order of drawing is important

http://www.andersriggelsen.dk/glblendfunc.php

note: order of drawing is important

Yes, when I saw this previously it didn't clarify things for me regarding the order / z-depth. Now eveything seems straightforward.

This topic is closed to new replies.

Advertisement