NeHe OpenGL Lesson08 - Blending
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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了