• Gl_blend concept

    • The color value will be placed in the color buffer, and then the depth value of each fragment will be placed in the depth buffer. When the depth test is not enabled, the color rendered after will overwrite the color stored in the current fragment. However, when the depth test is enabled, the original color fragment will be replaced only when the depth value of the two fragments is compared with the clipping plane close to each other
    • After turning on the mixing of two color segments overlap processing method the source color and the target color in a certain way to generate a special effect technology. Blending is often used to draw transparent or translucent objects. The alpha value that plays a key role in blending is actually mixing the source and target colors at a given ratio to achieve varying degrees of transparency. A value of alpha 0 is completely transparent, and a value of alpha 1 is completely opaque. The blending operation can only be performed in RGBA mode, and the α value cannot be specified in color index mode. The order in which objects are drawn affects the blending process in OpenGL.
    • Mixed open and close methods
      • Open: glEnable (GL_BLEND)
      • Close: glDisable (GL_BLEND)
  • Combining colors

    • Target color: value that already exists in the color buffer (remember to take it literally)
    • Source color: The color value entered into the color buffer as the result of the current render command, that is, the color value to be stored (again, take it literally)
    • Combination mode: The combination mode is controlled by the mixture equation. By default, the combination equation is Cf=(Cs * S)+(Cd * D) Cf: the calculated color Cs: source color Cd: target color S: source mixing factor D: target mixing factor
    • Modify the default blend equation

      The default blend equation is: Cf=(Cs * S)+(Cd * D) via glbBlendEquation(GLenum mode); Method Modify the parameters as follows:
      model function
      GL_FUNC_ADD Cf=(Cs * S)+(Cd * D)
      GL_FUNC_SUBTRACT Cf=(Cs * S)-(Cd * D)
      GL_FUNC_REVERSE_SUBTRACT Cf=(Cd * D)-(Cs * S)
      GL_MIN Cf=min(Cs,Cd)
      GL_MAX Cf=max(Cs,Cd)
  • Mixed factor

    • GlBlendFunc (GLenum S, GLenum D); S: source mixing factor D: target mixing factor

    • OpenGL blending factor mapping table

      function RGB mixing factor Alpha mixing factor
      GL_ZERO (0, 0) 0
      GL_ONE ,1,1 (1) 1
      GL_SRC_COLOR (Rs,Gs,Bs) As
      GL_ONE_MINUS_SRC_COLOR ,1,1 (1) – (Rs, Gs, Bs) 1-As
      GL_DST_COLOR (Rd,Gd,Bd) Ad
      GL_ONE_MINUS_DST_COLOR ,1,1 (1) – (Rd, Gd, Bd) 1-Ad
      GL_SRC_ALPHA (As,As,As) As
      GL_ONE_MINUS_SRC_ALPHA ,1,1 (1) – (As, As, As) 1-As
      GL_DST_ALPHA (Ad,Ad,Ad) Ad
      GL_ONE_MINUS_DST_ALPHA ,1,1 (1) – (Ad, Ad, Ad) 1-Ad
      GL_CONSTANT_COLOR (Rc,Gc,Bc) Ac
      GL_ONE_MINUS_CONSTANT_COLOR ,1,1 (1) – (Rc, Gc, Bc) 1-Ac
      GL_CONSTANT_ALPHA (Ac,Ac,Ac) Ac
      GL_ONE_MINUS_CONSTANT_ALPHA ,1,1 (1) – (Ac, Ac, Ac) 1-Ac
      GL_SRC_ALPHA_SATURATE (f,f,f)*f=min(As,1-Ad) 1

      Note: R, G, B and A in the table represent red, green, blue and alpha respectively. S and D represent the source and target colors respectively. C for constant color (black by default)

  • Case analysis

    The mixed formula is glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA). If the color cache already has a color red (1.0F, 0.0F,0.0f, 0.0F), the target color Cd, If you use a blue (0.0f, 0.0F, 1.0F,0.6f) Cd (target color) = (1.0f,0.0f,0.0f,0.0f); Cs (source color) = (0.0f,0.0f,1.0f,0.6f); The equation Cf = (Cs * S) + (Cd * D) is equivalent to (Blue * 0.6F) + (Red * 0.4F). Conclusion: The final color is a combination of the original red (the target color) and the later blue (the source color). The higher the alpha of the source color, the higher the blue color component is added, and the less the target color will retain. The blending function is often used to achieve the effect of drawing a transparent object in front of some other opaque object

  • Constant blend color

    Constant blend color, initialized to black by default (0.0f,0.0f,0.0f,0.0f), but can still be modified. void glBlendColor(GLclampf red ,GLclampf green ,GLclampf blue ,GLclampf alpha );