Image blending issue when rendering texture

This is related to my last question . To get this image:

http://img252.imageshack.us/img252/623/picture8z.png

  • I am painting a white background (color = (1, 1, 1, 1)

    ).

  • I render-the-texture two top-left squares with color = (1, 0, 0, .8)

    and blend function (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    , and then paint texture with color = (1, 1, 1, 1)

    and blend function (GL_ONE, GL_ONE_MINUS_SRC_ALPHA)

    .

  • I am drawing the bottom right square with color = (1, 0, 0, .8)

    and blend function (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    .

According to my calculations, the texture rendering squares should have a color

.8 * (1, 0, 0, .8) + (1 - .8) * (0, 0, 0, 0) = (.8, 0, 0, .64)

      

and so after drawing this texture on a white background they should have the color

(.8, 0, 0, .64) + (1 - .8) * (1, 1, 1, 1) = (1, .2, .2, .84)

      

and the bottom right square should have the color

.8 * (1, 0, 0, .8) + (1 - .8) * (1, 1, 1, 1) = (1, .2, .2, .84)

      

which should look the same! Am I wrong in my reasoning? Are my calculations wrong?

Anyway, my goal is to cache part of my scene. How do I render-a texture and then paint that texture so that it is equivalent to just painting the scene?

+2


source to share


3 answers


If you want to render blended content to a texture and compose that texture on screen, the easiest way is to use premultiplexed alpha all over the world. Its relatively simple to show that it works for your case: color your semi-transparent squares in pre-multiplied form (0.8, 0, 0, 0.8), and blend over that (0, 0, 0, 0) with ( GL_ONE

, GL_ONE_MINUS_SRC_ALPHA

) essentially conveys the color of your squares before the texture. Blending (0.8, 0, 0, 0.8) over an opaque white with ( GL_ONE

, GL_ONE_MINUS_SRC_ALPHA

) gives you (1.0, 0.2, 0.2, 1.0). Note that the color channels are the same as in your third calculation, but the alpha channel is still 1.0, which is expected for an opaque object covered by a blended object.



Tom Forsyth has a good article on the premultiplexed viola. These are all worth reading, but see the Composing Semitransparent Layers section for an explanation of why the math works in general.

+4


source


Oops my calculations are wrong! the second line should be

(.8, 0, 0, .64) + (1 - .64) * (1, 1, 1, 1) = (1, .36, .36, .84)

      



which seems to match what I see (when I change the last square to a color (1, .2, .2, .8)

, all three squares have the same color).

0


source


Regarding your last question: Replacing parts of the scene with textures is not trivial. A good starting point is Stefan Jeschke's PhD thesis .

0


source







All Articles