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 @   opencoder  阅读(267)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示