



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




The “color” of an object or material can have a fourth component, called alpha. This is notated the RGBA system or,
The alpha component has no fixed meaning, but we will see today what meaning it typically has, namely the opacity of the material:
To use RGBA, you have to initialize your OpenGL program as follows:
glutInitDisplayMode( GL_RGBA ...);
But since GL_RGB is an alias for GL_RGBA, we’ve been using it all along.
Given the pipeline model, we understand that at some moment during the rendering process, some of our objects have been drawn and exist only in the frame buffer and some of our objects have not yet been drawn. So, there is a time when the rendering of the next object is being combined with the rendering of some previous object. In the usual case, the new object’s pixels overwrite the old pixels. In general, though, OpenGL allows you to blend the two sets of pixels in the following way. The pixels already in the frame buffer are known as the destination pixels and a particular pixel is colored
. The new pixels are called the source pixels and a particular one is colored
from the following list, most of which we will ignore.
GL_ZERO GL_ONE GL_SRC_COLOR GL_ONE_MINUS_SRC_COLOR GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA GL_DST_ALPHA GL_ONE_MINUS_DST_ALPHA GL_DST_COLOR GL_ONE_MINUS_DST_COLOR GL_SRC_ALPHA_SATURATE GL_CONSTANT_COLOR GL_ONE_MINUS_CONSTANT_COLOR GL_CONSTANT_ALPHA GL_ONE_MINUS_CONSTANT_ALPHA
Note that any of these that need the destination ALPHA will require an ALPHA buffer. You also need to use
glEnable(GL_BLEND); glBlendFunc(source_factor, destination_factor)
The default blend function is
glBlendFunc(GL_ONE, GL_ZERO)
which just replaces (overwrites) the destination with the source. See the man page for glBlendFunc for more information. However, we will quote one important sentence from that man page:
Transparency is best implemented using blend function (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) with primitives sorted from farthest to nearest. Let’s try to understand the first half of that:
object.
darker).
So, we use the alpha of the source object for the source factor and the complement for the destination. In the next section, we’ll try to understand the second half of that sentence. First, let’s look at the ˜cs307/public_html/demos/tutor.cc. This lets you adjust the alpha values for three quads, drawn either furthest to nearest (in keeping with the advice from the man page) or in the reverse order. Another demo is ˜cs307/public_html/demos/TransparentQuads.cc
that it is not important that they add to 100 percent; I just wanted to start out with one that was mostly opaque and the other mostly transparent.)
clearing the framebuffer. (The TW default is to use 70 percent gray.) The second clears the frame buffer and associated buffers. We’ll see about the depth buffer soon.
the background color when they are drawn. Also, the red quad may look different: surrounding colors can affect our psychological perception of color. We’ll can see some of the effect of surrounding color on our perceptions of color by switching to “immerse” mode and comparing.
Suppose we render a scene with surfaces that overlap or even interpenetrate. For example:
How does a graphics system determine which color to use for any pixel? There are two major algorithms: depth sort, which is object -based, and depth buffer, which is pixel -based.
blue quads pass behind the red one and should be invisible, they are still visible. This is the effect of the (lack of the) depth test. We would want to use the depth test for drawing opaque objects, so that only the nearest is visible. Notice that the area where the green and blue quads overlap is a blend of both the green and blue, and the two halves (green behind blue on the right and blue behind green on the left) should look identical. This is because if we ignore depth, we’re just mixing green and blue, and it doesn’t matter which is SRC and which is DEST. Notice that if the depth test is off, the depth mask makes no difference. This makes sense, since the depth buffer is ignored, so it doesn’t matter whether you update it. So, it seems that the depth test should be on.
- If the depth mask is TRUE, the fact that the blue quad passes behind the green one is noted: we only see the right half of the overlap area, because in the left half, the blue quad is behind the green one, and so we don’t see it at all. This is the effect of the
-buffer algorithm. So, even though the green quad is partly transparent, we can’t see any blue through it. That’s bad. Try finding a view where you can compare red through green with red through blue through green. They look the same! Should they?
- If the depth mask is FALSE, the blue and green colors mix where the projections overlap. That’s what we want. However, since the depth buffer isn’t being updated, OpenGL can’t properly interleave this surface with others.
Solutions:
glDepthMask(GL_FALSE);
nearest (switching to the other hidden-surface algorithm). The coding scheme is shown in figure 1.
glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); // this is the default glDisable(GL_BLEND);
// draw all opaque Objects ...
// done
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glDepthMask(GL_FALSE); // draw all transparent Objects
// done glutSwapBuffers();
Figure 1: Code that draws the opaque objects first
Compare these. In particular, when Opaque objects are drawn last, try it with and without the depth test. It just doesn’t work. Demos: ˜cs307/public_html/demos/transparency/tutor With this demo,
- bottom (0.5, 0.5, 0, 1) - middle (0,1,0,0.5) - top (1,0,0,0.5)
Compare. Does this make sense?
You can use this tutor to experiment with different combinations of RGBA values.
There are a limited number of bits in the depth buffer; the actual number depends on the graphics card. Quoting from the OpenGL Reference Manual page for gluPerspective:
Depth buffer precision is affected by the values specified by zNear and zfar. The greater the ratio of zFar to zNear is, the less effective the depth buffer will be at distinguishing between surfaces that are near
zFar
zNear
bits of depth buffer precision are lost. Because
approaches infinity as zNear approaches 0, zNear must never be set to 0.
So, even though it seems realistic to set near to zero and far to infinity, the practical result is that the depth buffer algorithm won’t be easily able to tell which of two surfaces is closer if they are similar in distance. Some of you have discovered that if they are exactly the same distance, the color can be almost random, based on small roundoff errors in the calculation.