johnbakers
johnbakers

Reputation: 24771

OpenGL blending; what exactly do Source Color and Destination Color refer to, with examples

I'm confused by exactly what this means:

When doing blending for a particular buffer, the color from the fragment output is called the source color. The color currently in the buffer is called the destination color.

(from the OpenGL wiki)

I understand what the blending equation itself is, but I do not understand quite the distinction between source color and destination color.

Can anyone provide an example or more specific definition?

Upvotes: 11

Views: 5411

Answers (2)

h4lc0n
h4lc0n

Reputation: 2770

To keep it short and simple:

  • Source color: This is the color you're currently using. For example, when you use glColor4f(...) you set the source color of the operation.
  • Destination color: This is the color of the fragment (pixel if you prefer to think it this way, though it's not necessarily the same) in a certain coordinate in the video buffer.

Usually, the reason why one uses:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Is because you want to use the alpha value you just supplied. You use it to multiply your current color, and then use (1 - alpha) and use it to multiply the buffer's current color in that coordinate.

Therefore, if you paint a quad with glColor4f(1.0f, 1.0f, 1.0f, 0.6f) and the buffer is filled with glColor4f(1.0f, 0.0f, 0.0f, 1.0f), the final operation will be:

(1.0f, 1.0f, 1.0f) * ALPHA + (1.0f, 0.0f, 0.0f) * (1 - ALPHA)
(0.6f, 0.6f, 0.6f) + (0.4f, 0.0f, 0.0f)

So the final color is (1.0f, 0.6f, 0.6f)

Upvotes: 8

Nicol Bolas
Nicol Bolas

Reputation: 474076

In desktop OpenGL, the fragment shader can output multiple colors, not just one. Each color is numbered, and each such number is mapped to an output buffer in the framebuffer, based on glDrawBuffers.

Blending takes place independently between each fragment shader output color and the color currently stored in the destination image. So if you write two colors, blending would happen between the first color and its image, and then between the second color and its image.

For each blending operation, the source color is the color the fragment shader wrote to the output variable. The destination color is the color from the framebuffer image that corresponds to the particular fragment shader output (based on the glDrawBuffers mapping). So the source color(s) come from the fragment shader, and the destination colors come from the framebuffer image(s).

Upvotes: 1

Related Questions