PDA

View Full Version : blending and framebuffer



Jakeop132
01-23-2011, 08:05 AM
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!

ZbuffeR
01-23-2011, 08:58 AM
Try with depth testing disabled, to verify your near/far assumption is correct.

Jakeop132
01-23-2011, 11:59 AM
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!

ZbuffeR
01-23-2011, 02:10 PM
Depth test breaks blending unless you are sure to draw back to front, because depth buffer only contains 1 depth value for each pixel.

Jakeop132
01-24-2011, 05:43 AM
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.