基于Qt的OpenGL可编程管线学习(11)- 高斯模糊
下图是使用高斯模糊和未使用高斯模糊的效果图对比
正常图片
高斯模糊后
1、标准高斯模糊
原理:
每个像素周围对应的像素乘以对应的算子,然后除以算子的综合
算子为
121
242
121
fragment shader
varyingvec2M_coord;varyingvec3M_normal;varyingvec3M_WordPos;uniformsampler2DU_MainTexture;uniformsampler2DU_SubTexture;voidmain(){//121//242//121intcoreSize=3;inthalfCoreSize=coreSize/2;floattexelOffset=1/100.0;vec4color=vec4(1.0);floatnGaussionCore[9]=float[](1.0,2.0,1.0,2.0,4.0,2.0,6.0,2.0,1.0);intindex=0;for(inty=0;y<coreSize;y++){for(intx=0;x<coreSize;x++){vec4currentColor=texture2D(U_MainTexture,M_coord+vec2((-halfCoreSize+x)*texelOffset,(-halfCoreSize+y)*texelOffset));color+=currentColor*nGaussionCore[index++];}}color/=16.0;gl_FragColor=color;}
2、横向模糊
与高斯模糊类似 不过只是模糊横向分量
//水平高斯模糊varyingvec2M_coord;varyingvec3M_normal;varyingvec3M_WordPos;uniformsampler2DU_MainTexture;uniformsampler2DU_SubTexture;voidmain(){intcoreSize=3;inthalfCoreSize=coreSize/2;floattexelOffset=1/100.0;vec4color=vec4(0.0);floatnGaussionCore[5]=float[](0.22,0.19,0.12,0.08,0.01);color=texture2D(U_MainTexture,M_coord)*nGaussionCore[0];for(inti=1;i<5;++i){color+=texture2D(U_MainTexture,vec2(M_coord.x+i*texelOffset,M_coord.y))*nGaussionCore[i];color+=texture2D(U_MainTexture,vec2(M_coord.x+i*texelOffset,M_coord.y))*nGaussionCore[i];}gl_FragColor=color;}
3、纵向模糊
与高斯模糊类似 不过只是模糊纵向分量
varyingvec2M_coord;varyingvec3M_normal;varyingvec3M_WordPos;uniformsampler2DU_MainTexture;uniformsampler2DU_SubTexture;voidmain(){intcoreSize=3;inthalfCoreSize=coreSize/2;floattexelOffset=1/100.0;vec4color=vec4(0.0);floatnGaussionCore[5]=float[](0.22,0.19,0.12,0.08,0.01);color=texture2D(U_MainTexture,M_coord)*nGaussionCore[0];for(inti=1;i<5;++i){color+=texture2D(U_MainTexture,vec2(M_coord.x,i*texelOffset+M_coord.y))*nGaussionCore[i];color+=texture2D(U_MainTexture,vec2(M_coord.x,i*texelOffset+M_coord.y))*nGaussionCore[i];}gl_FragColor=color;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。