Question

I have some GDI code that's drawing semi-transparent triangles using System.Drawing.SolidBrush. I'm trying to reimplement the code using a proper 3D rendering API (OpenGL/Direct3D11), but I'm not sure what blend equation to use for these triangles to get the same output as the original GDI code.

I assume it's something relatively simple like additive blending (func=GL_FUNC_ADD, eq=GL_ONE,GL_ONE) or interpolation (func=GL_FUNC_ADD, eq=GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA), but neither seems to look quite right. This is almost certainly to a bug in my new code, but I want to make sure I'm working towards the correct target before I continue. Does anybody know the appropriate blend equation?

EDIT: Here's the relevant C# code, stripped of context:

using System.Drawing;
SolidBrush b = new SolidBrush(Color.FromArgb(alpha,red,green,blue));
Point[] points = ...;
Graphics g;
g.FillPolygon(b,points);

My question is, what color will actually be written, in terms of the brush's RGBA and the destination pixel's RGBA? All the docs I can find just say "use alpha to control the Brush's transparency" or something equally vague; I'm curious what the actual blend equation is.

Was it helpful?

Solution

According to MSDN, System.Drawing.Graphics has a property named CompositingMode which can be either SourceOver or SourceCopy.

The default one is SourceOver, which

Specifies that when a color is rendered, it is blended with the background color. The blend is determined by the alpha component of the color being rendered.

As I have tested, the blend strategy used here is alpha composition, which determines the pixel value in the area image A over image B by the following equation:

Alpha composition equation

Here αa and αb are the normalized alpha value of image A and image B; Ca and Cb are the RGBA values of image A and image B; Co is the RGBA value of the output image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top