how implement own blend function?

687 Views Asked by At

I want to implement the following blend function in my program which isn't using OpenGL.

glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);

In the OpenGL realtime test Application I was able to blend with this function colors on a white background. The blended result should look like http://postimg.org/image/lwr9ossen/. I have a white background and want to blend red points over it. a high density of red points should be get opaque / black.

glClearColor(1.0f, 1.0f, 1.0f, 0.0f);

for(many many many times)
glColor4f(0.75, 0.0, 0.1, 0.85f);
DrawPoint(..)

I tried something, but I had no success. Has anyone the equation for this blend function?

1

There are 1 best solutions below

0
On

The blend function should translate directly to the operations you need if you want to implement the whole thing in your own code.

  • The first argument specifies a scaling factor for your source color, i.e. the color you're drawing the pixel with.
  • The second argument specifies a scaling factor for your destination color, i.e. the current color value at the pixel position in the output image.
  • These two terms are then added, and the result written to the output image.

GL_DST_COLOR corresponds to the color in the destination, which is the output image.

'GL_ONE_MINUS_SRC_ALPHA` is 1.0 minus the alpha component of the pixel you are rendering.

Putting this all together, with (colR, colG, colB, colA) the color of the pixel you are rendering, and (imgR, imgG, imgB) the current color in the output image at the pixel position:

GL_DST_COLOR = (imgR, imgG, imgB)
GL_ONE_MINUS_SRC_ALPHA = (1.0 - colA, 1.0 - colA, 1.0 - colA)
GL_DST_COLOR * (colR, colG, colB) + GL_ONE_MINUS_SRC_ALPHA * (imgR, imgG, imgB)
= (imgR, imgG, imgB) * (colR, colG, colB) +
  (1.0 - colA, 1.0 - colA, 1.0 - colA) * (imgR, imgG, imgB)
= (imgR * colR + (1.0 - colA) * imgR,
   imgG * colG + (1.0 - colA) * imgG,
   imgB * colB + (1.0 - colA) * imgB)

This is the color you write to your image as the result of rendering the pixel.