Problem with triangles intersection with alpha value. Need Help!


I would like to draw triangles (with the same Y - up direction - value), which ones may intersect each other. When I draw it with alpha value, I see darker blobs.
How can I resolve it?

I have a plane quad (in red colors), and above the quad a lot of randomly generated triangles with the same Y(up direction) value.


Hi !

I would guess that you have messed up the winding order of some of the traiangles, some are clockwise and some are counter clockwise, triangles has two sides…


It looks more like a zfighting problem mixed with blending.
Disable depth test, to see if it gets uniformly dark.
To sort that out properly, you can use the stencil buffer when drawing your grey triangles.
Increment stencil on draw, and set the stencil test to =0 or something like that. So that each pixel of the screen will only be drawn once.

Stencil buffer? Sounds good. How can I use it to eliminate the intersections?

I used stencil like this:

gl.glDepthMask( false );
gl.glEnable( GL.GL_STENCIL_TEST );
gl.glStencilFunc( GL.GL_ALWAYS, 1, 0xFFFFFFFF);
gl.glStencilOp( GL.GL_KEEP, GL.GL_KEEP, GL.GL_KEEP );

        for (Triangle triangle : shadow) {
            gl.glVertex3f(triangle.triples[0].x, triangle.triples[0].y, triangle.triples[0].z);
            gl.glVertex3f(triangle.triples[1].x, triangle.triples[1].y, triangle.triples[1].z);
            gl.glVertex3f(triangle.triples[2].x, triangle.triples[2].y, triangle.triples[2].z);
    gl.glEnd( );

    gl.glDepthMask( true );

    gl.glDisable( GL.GL_STENCIL_TEST );

I couldn’t resolved the alpha mixing problem.
Have I made mistakes using stencil buffer?

mistakes ? yes :

// test passes only if stencil is equal to 0 :
gl.glStencilFunc( GL.GL_EQUAL, 0, 0xFFFFFFFF);
// increment on draw :
gl.glStencilOp( GL.GL_INCR, GL.GL_INCR, GL.GL_INCR );

Post an updated screenshot if you still have troubles.

Edit: and don’t forget to clear stencil between frames : glClear(GL_STENCIL_BUFFER_BIT);