Results 1 to 5 of 5

Thread: blending and framebuffer

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    20

    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!

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: blending and framebuffer

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

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    20

    Re: blending and framebuffer

    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!

  4. #4
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: blending and framebuffer

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

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    20

    Re: blending and framebuffer

    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.

Similar Threads

  1. Replies: 4
    Last Post: 11-04-2013, 08:12 AM
  2. Replies: 1
    Last Post: 05-01-2013, 09:37 PM
  3. Sample Independent Blending onto Multisampled Framebuffer
    By Kastenfrosch2 in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 09-05-2012, 04:47 AM
  4. Blending operations on integer framebuffer
    By soconne in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 04-03-2009, 02:09 AM
  5. Saving areas of the framebuffer and blending them
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 05-18-2005, 12:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean