Results 1 to 4 of 4

Thread: Blending with equal alpha

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    2

    Blending with equal alpha

    Hello!

    Iím playing with the alpha blending example in the Red Book (v1.2, third edition, Example 6.1). In short, it places 2 colored triangles on the screen that overlap, and blends the colors together.

    The example uses blending function:

    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

    which I believe I understand. The original example has the triangles each drawn with 0.75 alpha, making the source triangle (second one drawn) have a higher translucency (since, from the book, source blending is 0.75, and destination blending is 1.0 Ė 0.75 = 0.25).

    So far so good. Now, I thought if I changed the alpha to 0.50 in both triangles, the blended area would be the same color no matter what triangle was drawn first. However, I was wrong! The topmost triangle still has a more dominant affect on the blending. Why is this?

    If the left triangle is the source and has color (1.0, 1.0, 0.0, 0.5), and the right triangle is the destination and has color (0.0, 1.0, 1.0, 0.5), then, if Iím understanding correctly, the final blended color with the aforementioned blend function is:

    R = (1.0 * 0.5) + (0.0 * (1.0-0.5)) = 0.5
    G = (1.0 * 0.5) + (1.0 * (1.0-0.5)) = 1.0
    B = (0.0 * 0.5) + (1.0 * (1.0-0.5)) = 0.5
    A = (0.5 * 0.5) + (0.5 * (1.0-0.5)) = 0.5

    So, RGBA = (0.5, 1.0, 0.5, 0.5)

    If you reverse the source and destination, it turns out the same:

    R = (0.0 * 0.5) + (1.0 * (1.0-0.5)) = 0.5
    G = (1.0 * 0.5) + (1.0 * (1.0-0.5)) = 1.0
    B = (1.0 * 0.5) + (0.0 * (1.0-0.5)) = 0.5
    A = (0.5 * 0.5) + (0.5 * (1.0-0.5)) = 0.5

    So, RGBA = (0.5, 1.0, 0.5, 0.5)

    If the blended colors come up the same in either case, why the color difference when changing the order of drawing?

    Cheers,
    -C

  2. #2
    Member Contributor
    Join Date
    Nov 2001
    Posts
    77

    Re: Blending with equal alpha

    The alpha buffer affects the display color when the triangle is first rendered. Even with blending off, I believe it still gets "adjusted" by the alpha buffer. For instance:

    clear color = (0.0,0.0,0.0,0.0)
    triangle 1 = (1.0 ,1.0 ,0.0 ,0.5)
    triangle 2 = (0.0 ,1.0 ,1.0 ,0.5)

    triangle 1 drawn first = (0.5 ,0.5 ,0.0 ,0.5)
    triangle 2 blended = (0.25,0.75,0.5 ,0.5)

    triangle 2 drawn first = (0.0 ,0.5 ,0.5 ,0.5)
    triangle 1 blended = (0.5 ,0.75,0.25,0.5)

    These are clearly two seperate colors, and may account for the problem.

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Feb 2002
    Location
    Bonn, Germany
    Posts
    1,633

    Re: Blending with equal alpha

    No, that's not it. The blending operation isn't commutative. This is an age old problem, and the solution, known as "order independent transparency" or OIT is something like the holy grail of graphics.

    You're getting this:
    1)framebuffer:=
    0.5*first triangle + 0.5*black

    2)framebuffer:=
    0.5*second triangle + 0.5*framebuffer
    =
    0.5*second triangle + 0.25*first triangle

    Order matters, obviously.

  4. #4
    Junior Member Newbie
    Join Date
    Feb 2004
    Posts
    2

    Re: Blending with equal alpha

    Ah, okay. I think I see now. I wasn't accounting for the fact that the first triangle also needs to blend against the framebuffer (which in this case is all black). Thanks for the replies!

    Cheers,
    -C

Similar Threads

  1. Problem, alpha is always equal to one.
    By laurent7601 in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 01-19-2019, 02:33 PM
  2. Replies: 3
    Last Post: 06-16-2011, 12:51 PM
  3. alpha blending without modifying destination alpha
    By GordAllott in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 09-21-2009, 04:15 AM
  4. Source Alpha Blending without an Alpha Buffer
    By niallm in forum OpenGL ES
    Replies: 1
    Last Post: 06-18-2009, 02:51 AM
  5. alpha blending
    By shalinigilra in forum OpenGL: Linux
    Replies: 0
    Last Post: 09-01-2001, 01:24 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