Blending in OpenGL

This topic is 4995 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Share on other sites
Quote:
 Original post by absriramHere's what I expect the program to do. The two quads should be drawn according to their depths, ie. the hidden surfaces should not be drawn and this should be done in the same way irrespective of the order in which they are drawn.

No. When you draw a quad it gets processed right then, so it depends on whats gone before it.

When you start you've got an empty depth buffer. Drawing an object tests its pixels against the *current* depth buffer and writes the pixels that pass (updating the depth buffer accordingly). Thats why if you draw the near then the far it looks normal.

Buf you draw the far then the near then the far actually gets written to the framebuffer. If you were using solid objects then the near object would then overwrite it correctly and you've get proper depth sorting.

However 'cos you're using blending you're not just replacing pixels but merging them with whats already present. Ideally for proper blending & depth sorting you 1. draw all solid objects. 2. disable depth writing (but leave enabled depth testing) and draw all transparent objects back to front.

Share on other sites
Just some quick advice after looking at your code: perhaps you should be changing the alpha value of glColor4f() to something less than 1.0f?

glColor4f(1.0f,0.0f,0.0f,0.5f);

Share on other sites
If I'm understanding correctly what you want to do then the way to do it is:
glClear(GL_COLOUR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ANY_OTHER_BUFFER_BITS);setUpCamera();glDepthFunc(GL_LESS);glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);glDisable(GL_BLEND);renderScene();glDepthFunc(GL_EQUAL);glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);glEnable(GL_BLEND);renderScene();

This first runs a depth-only pass to fill the depth buffer. The second pass renders the actual scene, providing occlusion because of the previous depth pass and blending where surfaces overlap (but only where they exactly overlap).

Enigma

• 11
• 20
• 12
• 9
• 34
• Forum Statistics

• Total Topics
631399
• Total Posts
2999847
×