blending and framebuffer

My goal is to draw a triangle that is semi-transparent over another triangle. I’ve just read through the first six chapters of this tutorial: http://www.glprogramming.com/red/, where chapter 6 deals specifically with blending.

I’ve been able to draw a triangle that blends into the background color, but so far, I haven’t be able to find or create code that will blend a triangle into another triangle. I suspect that the problem is with understanding how/when things are written to the framebuffer… Here’s the code I have:

   GL.Enable(GLFeature.Blend);
   GL.Blend(SRC_ALPHA, ONE_MINUS_SRC_ALPHA);

   GL.ClearColor(0f, 0.0f, 0.0f, 1f);
   GL.Clear(Buffer.Color);
   GL.Clear(Buffer.Depth);

//draw red triangle
GL.Begin(DrawMode.Triangles);
GL.Color(1.0f, 0f, 0f, .5f);
GL.Vertex(-2, 2, 13);
GL.Vertex(2, 2, 13);
GL.Vertex(1, 1, 13);
GL.End();

//draw blue triangle
GL.Begin(DrawMode.Triangles);
GL.Color(0f, 0f, 1.0f, .5f);
GL.Vertex(-2, 2, 14);
GL.Vertex(1, 3, 14);
GL.Vertex(2, 1, 14);
GL.End();

The result is that a red triangle is drawn on top of a blue one, but the overlapping region isn’t blended; rather, the red one occludes the blue one where it overlaps it. Note: the blue triangle is farther from the viewer. Whether it’s farther or closer, the red one gets drawn on top. I’ve tried adding glFlush and glFinish between the code for the two triangles, but that makes no difference.

Does anyone know of how to blend one triangle into another one?

Thanks in advance!

Try with depth testing disabled, to verify your near/far assumption is correct.

Dear ZbufferR,

Thanks for your suggestion. I disabled depth testing, and it now works! (I wonder why the tutorial didn’t say that I needed to disable depth testing…)

I’m very grateful for your comment; thanks again!

Depth test breaks blending unless you are sure to draw back to front, because depth buffer only contains 1 depth value for each pixel.

That makes sense; but then I’m not sure why when depth test was enabled that the first polygon drawn was drawn on top whether it was the farther one or not… I’ll look into that.