Skip to main content
GameDev.net gamedev.net
🔒 Locked

glBlend not working as expected

Started by Alex Dem Mar 2, 2016 at 8:31 AM 6 replies 2.8k views
Original Post
Alex Dem
Alex Dem

Hi there. I'm new to OpenGL and I'm having some difficulties with the blending of drawn objects. I have read much resources around the internet but so far nothing helps. I'm just trying to enable simple blend with no sorting of objects.

Here is what I'm doing to reach the result of the attached picture :

i call :

glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

glDepthMask( GL_FALSE );

//draw transparent objects

glDepthMask( GL_TRUE);

glDisable( GL_BLEND );

On this picture the purple figure have to be blended but it is not. It goes through the silver box, however the color is not blended, but only the color of the box is shown. The alpha factor is 0.1f.

_WeirdCat_
_WeirdCat_

glDisable(GL_DEPTH_TEST);

draw pink object;

glEnable(GL_DEPTH_TEST);

the reason you dont see it is because depth test (initially set to GL_LEQUAL => less or equal) eee tested pixel of pink object is further away from camera than grays box front face, thus even when blending enabled it becomes invisible.

also glDepthMask( GL_FALSE ); disables writing to z buffer but z_test is still working.

Simmie
Simmie
In which order are you drawing the objects and what is the alpha value of the box? It looks like you draw the transparent purple object first with z-writes disabled and then the opaque box with zu writes enabled. In this case the box would be simply drawn on top of the purple object, like in your pictures.

The correct way would be:
1. Render alls opaque objects with z-writes and reads enabled.
2. Render all transparent objects from back to front with z-reads enabled, but z-writes disabled.

You need to sort the transparent objects from back to front, because the z-buffer does not handle transparent objects very well. If two transparent objects intersect, you need to split them in order to render them properly.
Alex Dem
Alex Dem

I have the problem that if i set transparent alphas to all the colors when i draw i still get this picture.
As long as I understood there are two ways in which i can get some (not perfect transparency).
I either have to disable z test and z write or i have to sort opaque from transparent objects.

Is it mandatory to sort objects in every situation because i think there will be quite slowing when more than 200 objects are transparent and are sorted on each re-rendering?

Simmie
Simmie
You need both, because if you disable the z-test, the order in which you render the objects is important. AFAIK the only way to avoid the sorting is either approximate translucent surfaces using premultiplied alpha or using alpha to coverage. If you search for that terms you should find decent articles/tutorials about both and you can check if one of them meets your requirements.

Edit:
Keep in mind that you probably do not have to sort the objects in every frame, if the camera position does not change much. Also, you could do the sorting in a separate thread so it does not block the rendering thread.
Alex Dem
Alex Dem

Thanks a lot! And just a final question: Why are not all objects translucent when i set alpha value for each color to be 0.5?

Simmie
Simmie
Make sure that no translucent object writes to the z-buffer, but from the code you posted above this should be the case.
How do you set the alpha value?
Can you show the exact code and the fragment shader?
Alex Dem
Alex Dem

The whole process consist of a loop that loop through my shapes and pass their vertexes to OpenGl along with a color specified.

for(int i = 0; i< m_MyObjects.GetSize(); i++)
{

m_MyObjects.GetAt(i).DrawMe();
}

DrawMe() function consists of setting color with

glColor4f( color.red, color.green, color.blue, color.alfa ); // where these values are float values

and after that i call functions relate to the type of polygons i draw in my scene.

If I hard code the alfa value, it stil does not blend the colors in any way. It only draws them under or over another colors lie opaque object.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.