NeHe OpenGL Lesson20 – Masking
This sample shows us how to implement masking effect with OpenGL using blend operation instead of alpha testing. Masking is an important effect, that could be used build tree leaves, some complicated windows or fence, those objects will be shined though them.
Main Idea
1) Draw the scene texture normally;
2) with glBlendFunc(GL_DST_COLOR,GL_ZERO) setup to draw the white and black texture, this texture will make those area that will be fully overlapped be totally black;
3) with glBlendFunc(GL_ONE, GL_ONE) setup, draw the mask diffuse texture. Those areas that fully black will be filled by the mask diffuse texture color.
The above workflow could be represented with following diagram:
Of course, we could get the same masking effect with the alpha testing feature. I already wrote an alpha testing sample here, you could check them in the source code.
There is one thing that need to take special care is that when you use alpha testing for some mip-maps, you need to care about how those mip-maps are generated. Most of time, the masking texture alpha value should be 1 or 0. No middle valued such as 0.5, 0.6 created. But this situation will happen if you create mip-maps with linear sampling method.
A project named “AlphaRef” added, this project will load some TGA images with alpha channels. An OpenGL texture object will be created with alpha channel, that will be used for alpha testing.
The full source code could be downloaded from 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:你的「微服务管家」又秀新绝活了