• Advertisement
Sign in to follow this  

Material groups and alpha blending, how to render?

This topic is 4835 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. This week I'm gonna add material groups and alpha blending to my 3d engine, I'm planning to do the following. Loadscene: ------------------- - load all objects into arrays of objects with a specific material - load all materials with a value translucent or not - sort all objects with the translucent material according to depth (worldspace positions) Renderscene: ------------------- - disable depth buffer - draw all objects with non translucent materials - enable depth buffer - draw all objects with translucent materials Can someone tell me if this is a/the right way to do this? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Why are you disabling and enabling the depth buffer? (It may be obvious and I'm just missing it...)

Share this post


Link to post
Share on other sites
when you draw non-translucent objects they want to be drawn in a rought front to back order, with the z-buffer compares and writes on.

When you draw translucent objects they want to be drawn in a back to front order with z-buffer writes OFF but compares on.

Doing it your way your probably going to end up with an intresting mess...

Share this post


Link to post
Share on other sites
thanks for the replies.

I was planning to do the following:

InitGL:
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

Render the scene:

glEnable(GL_DEPTH_TEST);
*draw all objects with non translucent materials, front to back
glDisable(GL_DEPTH_TEST);
*draw all objects with translucent materials

What would be wrong with the code/working way above?

Can you explain the difference between z-buffer comparing and z-buffer writing?

Thanks in forward.

Share this post


Link to post
Share on other sites
Quote:
Original post by cozzie
thanks for the replies.

I was planning to do the following:

InitGL:
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);


That part looks good

Quote:

Render the scene:

glEnable(GL_DEPTH_TEST);
*draw all objects with non translucent materials, front to back
glDisable(GL_DEPTH_TEST);
*draw all objects with translucent materials



ok this part is the problem. Instead try:

glDepthMask(true); // turn on depth writting
*draw all objects with opaque materials. These do not have to be depth sorted.
glDepthMask(false); // turn off depth writting
*draw all objects with translucent materials. These must be sorted back to front.

Quote:

Can you explain the difference between z-buffer comparing and z-buffer writing?


Normally with opaque objects you do both. When you draw an object you check the z buffer to see if there is a closer object in front of it, and don't draw the part of the object that is covered. As you draw each object you update the z buffer with the new depth values for that object.

However you can turn off the z writting part. In this case you still do the compare, and reject pixels that are occluded by existing geometry, but you do not change the z buffer at all. Think of this as making the z buffer read only.

Quote:

What would be wrong with the code/working way above?


There are two problems here. The first is that the blended (translucent) objects are the ones that must be drawn in order and without depth writting. The opaque objects do not need to be depth sorted. (At least to get a visually correct scene, you might z sort them for performance reasons in some applications).

The second is that you were disabling z test altogether for the non-sorted objects.

Basicly you want to draw the opaque objects first, in any order you want, while updating the z buffer. Then you turn off z writting and draw the translucent objects. Since the z buffer already holds the depth info for the opaque objects, hidden transparent objects will not be drawn.

To get visually correct results you need to draw the translucent objects back to front. Because you are drawing them in this order, you don't actually HAVE to turn off z writting. They are already sorted so none will be rejected anyway. However, since the z writes don't affect the scene you might as well turn off the writes and save a little time.

Share this post


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

  • Advertisement