NeHe OpenGL Lesson08 - Blending

screen_shot6-300x241 This sample shows us how to use blending operation with OpenGL API. Blending, I guess, may be the only way to access the destination pixels from the current color buffer. In the previous lesson, we care about how to shade the color in the screen, but in this lesson we focus on how our current pixel work with some color already there. Blending used to create some transparency effect. It make us look though something similar to glasses.
Blending also could be used for additive lighting. If one objects lit by many light sources, we could use additive blend operation to collect all lighting information together. Here comes a very interesting topic, the total color value will beyond the range [0, 255] to some range that we even could not expect what the maximum value will be. For such color, we call them HDR (high dynamic range) color. Of course, we could simply clamp them if the value is more than some certain value or use some HDR format to hold them, like RGBAF32. Or we could use find some ways to encode those HDR color into LDR color. And we decode them in some place as need. We could use RGBM, RGBE, LOGUV to do such HDR encode. For the whole hdr frame buffer, we need to do some tone mapping to make the whole picture looks more normal. Oh, it’s beyond this chapter’s title.

 

To make the blending work, you need to  set the blend factors, blend operation and enable the blend feature:

// set up blend parameters
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);

glEnable(GL_BLEND); // switch the blending on

 

One thing that need to mentioned here is that, we use source alpha as the source factor. But how does this alpha value come out? We have vertex color set here, we also provide it with diffuse texture, and lighting also could be enabled. Lighting should not give any impact on the alpha channel at all. And alpha value should be determined by vertex color alpha multiple with texture alpha.

This sample coded on Ubuntu 10.4. To compile and run this program you need to install gcc, cmake and opengl libraries.

 

The full source code could be found here.

posted @ 2012-08-23 22:41  opencoder  阅读(264)  评论(0编辑  收藏  举报