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.
本文探讨了使用OpenGL API进行混合操作的方法,重点讲解如何利用混合功能创造透明效果及HDR颜色处理。通过调整混合参数,可以实现像素间的叠加,创建如玻璃般透视效果。此外,文章还介绍了如何处理超过[0,255]范围的颜色值,即HDR颜色,并提供了几种编码和解码HDR颜色至LDR颜色的技术。
581

被折叠的 条评论
为什么被折叠?



