Order of translucent object rendering

2020-04-17 05:50发布

问题:

When rendering a translucent object (using glBlend), it is advised to sort objects from back to front z order.

From opengl docs: When using depth buffering in an application, you need to be careful about the order in which you render primitives. Fully opaque primitives need to be rendered first, followed by partially opaque primitives in back-to-front order. If you don't render primitives in this order, the primitives, which would otherwise be visible through a partially opaque primitive, might lose the depth test entirely.

So, whenever I'm rendering translucent objects after rendering opaque triangles, if I render translucent triangles from back to front in relation to the "view direction", is this correct? What exactly does back to front mean here? I'm trying to understand, if I have a scene objects that go through a mvp transformation, do I need to specifically look at the order in world coordinates, and in relation to the camera center?

Also, additionally does this change depending on the blend type?

回答1:

Whenever you render translucent triangles, you have to draw them back-to-front. Back-to-front here means, that you have to draw triangles first that are further away from the screen (that have a higher depth value).

The reason why this is necessary is that most blending equations yield different results depending on the order. The equations as implemented in OpenGL are usually such that they give "correct" results when rendering back-to-front.



回答2:

I think the order matters not only because of the blending function you are using but one important aspect of pipeline you have to keep in mind. The z test happens before blending operation down the line in pipeline. Consider scenario where we are drawing two triangles t1 and t2 t1 is back of t2.

So If z test rejects all fragments belonging to t1 so no color value for t1 is written to color buffer. Now later on t2 fragments are being drawn which is exactly in front of t1 which z test rejected in last step. So ideally the colors of t1 would have been blended with colors of t2 but because of failed z test of t1 and blending operation being after z test t2 colors wont be blended.

Hence If you are not keeping order then disable z test and then draw your object with proper blending function. This is not so good in performance as lots of z tests will fail.

So you maintain order or polygons but this solution also doesnt scale with number of polygons increasing and when there is mixture of opaque and transparent objects.

Hence new solution is proposed of using order independent trasparency. Different ways are there to implement this and you dont have to worry about order of objects.

I have implemented solution using per pixel linked list in fragment shader. You can find link below. This is not best solution but should give you fair idea.

Order Independent Transparency