Alpha blending OpenGL

hi all ,
i have one question please as below , please help

Suppose that we render two triangles, the opaque red (alpha=1) with z
value=0.6, and the translucent blue (alpha=0.5) with z value=0.3. Let’s say
we rendered the blue first then the red was rendered. The new incoming
red fragment has coordinates (23,46), and the blue pixel located in the
frame buffer at (23,46). In this case, What is the result (the final color value
of the pixel (23,46) in the frame buffer) if alpha blending is enabled?

If depth testing is enabled, the depth function is GL_LESS or GL_LEQUAL, and “z” is depth (or NDC Z), then the red triangle will fail the depth test wherever it overlaps with the blue triangle.

Ultimately, Z is irrelevant for blending. Blending is only relevant for fragments which are actually rendered; for fragments which aren’t rendered, it doesn’t matter whether they aren’t rendered because they failed the pixel ownership test (i.e. lie outside the primitive) or because they failed the depth test or because they failed some other test.

It depends upon the blending mode. If you’re rendering from front to back, the framebuffer needs to have an alpha channel, the blending mode would be glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE), and the incoming fragment colours need to be pre-multiplied by alpha, i.e. if the blue colour is (0,0,1,1) when solid, the translucent blue colour would be (0,0,0.5,0.5). The clear colour needs to be (0,0,0,0) (i.e. have zero alpha), and the process needs to end with rendering an opaque background (when rendering front-to-back, you can’t just use the background colour as the clear colour as the clear colour is the first colour in the process but the background has to be the last colour).

This would give a final colour of (0.5,0.0,0.5,1.0), the same as if you rendered them from back to front with glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA).

Are you aware that when using blending, you can’t just render polygons in an arbitrary order and leave the ordering to the depth test? Polygons have to be rendered either back-to-front or front-to-back, and front-to-back is the more involved option (although potentially more efficient due to the early depth test optimisation). The ordering is typically determined either by a topological sort or using BSP trees. For geometry where neither approach is feasible, the solution is to use order-independent transparency techniques such as depth peeling or storing a linked list of fragments which are then sorted.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.