本文列出三种片段着色器:
清除片段着色器
void main(void)
{
gl_FragColor=vec4(0.0,0.0,0.0,0.0);
}
流经片段着色器
uniform sampler2D texture
void main(){
vec4 color=texture2D(texture,gl_TexCoord[0].st);
gl_FragColor=color;
}
这个片段着色器相当于具有纹理映射工能的流经着色器,因为如果不使用该着色器,OpenGL也会以相同的效果映射纹理
卷积着色器convolution.flag
#extenson GL_ARB_texture_rectangle : enable
uniform sampler2DRect texture;
uniform float fRadius;
float nWidth=3.0;
float nHeight=3.0;
void main(void)
{
vec2 pos=gl_TexCoord[0].st; //获取当前纹理元坐标值
vec4 fSum=vec4(0.0,0.0,0.0,0.0); //邻近元素取值之和
vec4 fTotal=vec4(0.0,0.0,0.0,0.0); //邻近元素个数
vec4 vec4result=vec4(0.0,0.0,0.0,0.0); //覆盖当前纹理元的输出坐标
//邻近元素求和。坐标值加上0.5,以防计算误差
for(float ii=pos.x-fRadius;ii<pos.x+fRadius+0.5;ii+=1.0)
{
for(float ii=pos.x-fRadius;ii<pos.x+fRadius+0.5;ii+=1.0)
{
if(ii>=0.0&&jj>=0.0&&ii<nWidth&&jj<nHeight)
{
fSum+=texture2DRect(txture,vec2(ii,jj)) ;
fTotal+=vec4(1.0,1.0,1.0,1.0);
}
}
//求得邻近元素的均值
vec4Result=fSum/fTotal;
//将结果写入帧缓存对应像素
gl_FragColor=vec4Result;
}
}
参考文献:
仇德元.《GPGPU编程技术——从GLSL、CDPU到OpenGL》[M].河北省三河市:机械工业出版社,2012年:323.