Jump to content
  • Advertisement
Sign in to follow this  
matthewHughes

"correct" blending

This topic is 4233 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 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!