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是混合得到的结果。


相关推荐
蜉蝣之翼❉22 天前
games101 hw1
笔记·图形渲染
很甜的西瓜24 天前
typescript软渲染实现类似canvas的2d矢量图形引擎
前端·javascript·typescript·图形渲染·canvas
小草帽学编程1 个月前
camera功能真的那么难用吗
数码相机·图形渲染
EtpBot-萧阳1 个月前
SDL2常用函数:SDL_Texture 数据结构及使用介绍
图形渲染·sdl2·硬件加速
GISer_Jing1 个月前
工作流长任务处置方案
前端·编辑器·图形渲染
EtpBot-萧阳1 个月前
SDL2常用函数:SDL_BlitSurface&SDL_UpdateWindowSurface 数据结构及使用介绍
算法·ffmpeg·图形渲染·sdl2
冒泡P1 个月前
Unity Shader入门(更新中)
unity·c#·游戏引擎·图形渲染·着色器
平和男人杨争争1 个月前
山东大学计算机图形学期末复习15——CG15
人工智能·算法·计算机视觉·图形渲染
龙湾开发1 个月前
计算机图形学编程(使用OpenGL和C++)(第2版)学习笔记 13.几何着色器(二)爆炸效果&修改图元类型
c++·笔记·学习·3d·图形渲染·着色器
平和男人杨争争1 个月前
山东大学计算机图形学期末复习11——CG13上
算法·图形渲染