Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

opengl

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?

like image 812
johnbakers Avatar asked Apr 23 '13 12:04

johnbakers


People also ask

What is blending in OpenGL rendering?

Blending is the stage of OpenGL rendering pipeline that takes the fragment color outputs from the Fragment Shader and combines them with the colors in the color buffers that these outputs map to. Blending parameters can allow the source and destination colors for each output to be combined in various ways.

How to render images with different levels of transparency using OpenGL?

To render images with different levels of transparency we have to enable blending. Like most of OpenGL's functionality we can enable blending by enabling GL_BLEND : Now that we've enabled blending we need to tell OpenGL how it should actually blend.

How do I set the source and destination factor in OpenGL?

Well it just so happens that there is a function for this called glBlendFunc . The glBlendFunc (GLenum sfactor, GLenum dfactor) function expects two parameters that set the option for the source and destination factor. OpenGL defined quite a few options for us to set of which we'll list the most common options below.

What is blending in Blender?

Blending operations take place between a source color and a destination color. The source color is the value written by the fragment shader. The destination color is the color from the image in the framebuffer.


1 Answers

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)

like image 182
h4lc0n Avatar answered Nov 15 '22 22:11

h4lc0n