Lesson08
TransparencyMany effects in OpenGL depend on some type of mixing. Mixing refers to the operation of drawing a given pixel to an existing pixel on the current screen. The combination of the two colors is based on their Alpha value and implemented through the blending function. Alpha is the fourth component of the color. In OpenGL, gl_rgb can be used to specify only three components of the color, while gl_rgba can specify the Alpha value. In addition, we also use glcolor4f () to replace glcolor3f (). Many people think that the alpha value is the transparency of the material. The material with Alpha 0.0 is completely transparent, and the material with Alpha 1.0 is completely opaque.
Hybrid Equation(RS × Sr + RD × DR, GS × SG + GD × DG, BS × Sb + bd × dB, as × SA + AD × da) openGL calculates the result of mixing two pixels based on the preceding equation. The subscript S and D indicate the source pixel and the given pixel. S and D are mixed factors, indicating how the two pixels will be mixed. S and D usually use the following values, S = (as, as), and D = (,)-(as, ). In this way, the mixed equation becomes: (RS × as + RD × (1-As), GS × as + GD × (1-), BS × as + bd × (1-As), as × as + AD × (1-))
OpenGL MixingUse the same mix in OpenGL as other options. Set the parameter value and disable the depth buffer when creating a transparent object, because we want to make all objects after the transparent object be drawn. However, this is not a good mix of methods, but it is good for a simple project. The correct method is to draw a complete scenario and then draw it in reverse order based on the depth value of the transparent object (that is, the first draw farther from the observer ). This is because mixing two polygon (1 and 2) in different order produces different results. Assuming that Polygon 1 is close to the observer, the correct method is to draw 2 first and then draw 1. In reality, the light from the two polygon must first pass through 2, then pass through 1, and finally reach the observer. Therefore, you need to first sort transparent objects by their depth, and then draw the entire scene in depth buffering mode before processing transparent objects. In initgl (), add the mixed parameter settings: // full brightness, 50% alpha
Glcolor4f (1.0f, 1.0f, 1.0f, 0.3f );
// Blending function transequalcy Based on source Alpha Value
Glblendfunc (gl_src_alpha, gl_one );
Add the B-key response code to control the hybrid switch:
If (Keys ['B'] &! BP)
{
BP = true;
Blend =! Blend;
If (blend)
{
Glable (gl_blend );
Gldisable (gl_depth_test );
} Else
{
Gldisable (gl_blend );
Glenable (gl_depth_test );
}
}
If (! Keys ['B'])
{
BP = false;
}