OpenGL学习笔记-Blending

混合方程中,Csource是片段着色器输出的颜色向量(the color output of the fragment shader),其权重为Fsource。Cdestination是当前存储在color buffer中的颜色向量(the color vector that is currently stored in the color buffer),其权重为Fdestination。Csource和Cdestination由OpenGL自动设置,但Fsource和Fdestination需要由我们进行设置(The source and destination colors will automatically be set by OpenGL, but the source and destination factor can be set to a value of our choosing)。当OpenGL渲染一个片段时,会根据混合方程对上述两个颜色向量进行加权组合(whenever OpenGL renders a fragment, it combines the current fragment's color with the fragment color currently in the color buffer),Cresult是加权组合后的结果,将保存到color buffer中,并且会将Cdestination覆盖掉。

要想实现混合的效果,需要先使能混合:

cpp 复制代码
glEnable(GL_BLEND);

然后设置混合方程中的Fsource和Fdestination,一种常用的设置如下:

cpp 复制代码
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glBlendFunc函数的原型如下,上述设置的含义是:Fsource取Csource的alpha分量,Fdestination取(1-Csource的alpha分量)。

cpp 复制代码
void glBlendFunc(GLenum sfactor, GLenum dfactor);

完成渲染后,需要将混合关闭:

cpp 复制代码
glDisable(GL_BLEND);

一个简单的例子:图1是待混合的两个片段,图2是混合方程,图3是混合得到的结果。


相关推荐
Alaso_shuang1 天前
Raylib贴图
c语言·图形渲染·贴图·raylib库·c语言项目
3DVisionary3 天前
DIC技术在汽车风洞试验中的革新应用:流场与形变的全场测量
汽车·图形渲染·非接触测量·数字图像相关·汽车动力学·风洞试验·形变与应变
炫云云渲染7 天前
2025年效果图云渲染带来效率革命
图形渲染·效果图成本控制·效果图可控效率提升·云渲染效率革命·效果图精准化成本控制
还下着雨ZG15 天前
2 VTK的基本概念
图形渲染
魏无忌15 天前
BIM+GIS尝试
图形渲染·webgpu·bim引擎·bim+gis
Renderbus瑞云渲染农场16 天前
跨平台渲染不再难_瑞云渲染跨平台转移+克隆双功能上线,效率升级
3d·图形渲染
水星梦月19 天前
跨平台开发中的图形渲染:Canvas与View+CSS的性能分析与决策路径
前端·css·图形渲染·canvas
郝学胜-神的一滴20 天前
Cesium绘制线:从基础到高级技巧
前端·javascript·程序人生·线性代数·算法·矩阵·图形渲染
WaWaJie_Ngen21 天前
【OpenGL】模板测试(StencilTest)
c++·算法·游戏·游戏引擎·游戏程序·图形渲染
郝学胜-神的一滴23 天前
矩阵的奇异值分解(SVD)及其在计算机图形学中的应用
程序人生·线性代数·算法·矩阵·图形渲染