# Alpha Blending and Intersecting Triangles

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

## Recommended Posts

Is there any way to get 2 intersecting triangles to blend and look correct if both have textures with alphas? Example 2 tris that intersect each other and have a leaf pattern on them.

##### Share on other sites
I believe the only way to get 'correct' alpha blending in this situation is to split one or both of the triangles into pieces along the line formed by the intersection of the triangles, then render the pieces in correct back to front order. This only applies if alpha blending is being performed. If the alpha of the textures is either 1 or 0 (ie, 0 simply defining areas of transparency) and no blending is being done--which I gather might be the case, since you are doing leaves--you can simply enable alpha testing so that pixels where alpha=0 do not modify the depth buffer; in this case, you can simply draw the two triangles as-is without splitting.

##### Share on other sites
Quote:
 Original post by VertexNormal...you can simply enable alpha testing so that pixels where alpha=0 do not modify the depth buffer...

How exactly do you do this? You can either enable or disable writing to the depth buffer using the glDepthMask function. But once you start drawing the polygon, away it goes. How can you put a condition depending on the alpha of each pixel?

##### Share on other sites
Yeah I am interested too. I've never heard of alpha test being used to test away the depth buffer.

##### Share on other sites
You can use alpha testing becuase it comes before depth testing in the order of operations. As Alphatesting is a boolean operation it will either pass, and update the zbuffer, or fail and not touch it (if you have depth writes active of course).

The draw back to this approach is clearly that you'll get alaised results, instead of nice alpha blending. If this is for the case of rendering tree leaves or other folliage then I believe a method sometimes emplloyed is to render the alpha texture sveral times with a different alphatest and blend value to simulate smoother blending.

##### Share on other sites
Quote:
 Original post by Ryback...How can you put a condition depending on the alpha of each pixel?...

glEnable(GL_ALPHA_TEST);glAlphaFunc(GL_GREATER, 0.0f);

With that, all fragments with an alpha value greater than 0.0 will fail the alpha test and will not be written to the framebuffer (color/depth/stencil).

##### Share on other sites
Quote:
 Original post by KalidorglEnable(GL_ALPHA_TEST);glAlphaFunc(GL_GREATER, 0.0f);With that, all fragments with an alpha value greater than 0.0 will fail the alpha test and will not be written to the framebuffer (color/depth/stencil).

Don't you mean the opposite? With that code, all fragments with an alpha value greater than 0.0 will PASS the alpha test and will be written to the framebuffer.

Anyways, thanks for the info, I will try it tonight!

##### Share on other sites
Quote:
 Original post by RybackDon't you mean the opposite? With that code, all fragments with an alpha value greater than 0.0 will PASS the alpha test and will be written to the framebuffer.Anyways, thanks for the info, I will try it tonight!

Err... yeah that's what I meant. :P

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632164
• Total Posts
3004520

×