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.

转载于:https://www.cnblogs.com/open-coder/archive/2012/08/23/2653398.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值