Sign in to follow this  
matthewHughes

"correct" blending

Recommended Posts

Hi all, I tried doing blending the "correct" way, that is, drawing transparent polys after drawing the rest of the scene, and I got a rather disappointing/confusing result. In the scene below, you can see on the right that the transparent (green) cube renders properly. You can see the pink cube through it. But when the transparent cube is behind the opaque cube, you can still see it! Is there a way to prevent the transparent cube from showing through? I tried turning depth testing back on but it creates problems with the transparent cube. float angle=0.0f; void render() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // draw the opaque cube glLoadIdentity(); glTranslatef(0, 0, -8); glColor4f(1, 1, 0.4f, 1.0f); glutSolidCube(1); //draw the transparent cube glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE); glLoadIdentity(); glColor4f(0.4f, 0.4f, 1.0f, 0.3f); glTranslatef(cos(angle) * 2, 0, -8 + sin(angle+=0.0005) * 2); glutSolidCube(1); glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); glutSwapBuffers(); }

Share this post


Link to post
Share on other sites
What about enabling back face culling so the back faces of the green cube aren't blended. If that doesn't work for you then you can also first render the cube to the depth buffer only (disable color writes) and then render it to the color buffer only (disable depth writes) with GL_EQUAL depth test.

For a much more advanced solution you can also check out Depth Peeling.

Share this post


Link to post
Share on other sites
The reason the transparent green cube is drawn behind the solid pink cube is because you disabled the depth test. A two pass approach as nts described is not necessary, just keep depth testing enabled.

Share this post


Link to post
Share on other sites
Simply put, instead of disabling the depth test (glDisable(GL_DEPTH_TEST)), disable depth writing, as has been said (glDepthMask(FALSE)).

You want, I think, to keep the test, so that transparent items behind solid ones are not rendered where they render behind a solid item, but rather diable the depth writing, so that they don't occlude anything themselves.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this