Jump to content
  • Advertisement
Sign in to follow this  
Alex Dem

OpenGL glBlend not working as expected

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

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.

Share this post


Link to post
Share on other sites
Advertisement

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.

Edited by WiredCat

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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?

Edited by Alex Dem

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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.

 

Edited by Alex Dem

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net 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!