http://blog.csdn.net/a3070173/archive/2008/11/27/3390477.aspx
- 图像过滤无论是在作图工具还是特效的实现上都时有运用,这里发一些通常会使用到的图像
- 过滤着色器供有需要的朋友参考.
- 备注:这里列出的都是片元着色器,顶点着色器都是一样地简单变换顶点和传递纹理坐标.
- 高斯过滤 - 常用于进行图像模糊
- 横向过滤:
- const int g_iWeightNumber = 17;
-
- uniform sampler2D g_FilterTexture;
- uniform float g_aryWeight[g_iWeightNumber];
- uniform vec2 g_aryVerticalOffset[g_iWeightNumber];
- void main()
- {
- vec4 vec4Sum = vec4(0.0);
- for(int i = 0; i < g_iWeightNumber; ++i)
- {
- vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryVerticalOffset[i])*g_aryWeight[i];
- }
-
- gl_FragColor = vec4Sum;
- }
- 纵向过滤:
- const int g_iWeightNumber = 17;
-
- uniform sampler2D g_FilterTexture;
- uniform float g_aryWeight[g_iWeightNumber];
- uniform vec2 g_aryHorizontalOffset[g_iWeightNumber];
- void main()
- {
- vec4 vec4Sum = vec4(0.0);
- for(int i = 0; i < g_iWeightNumber; ++i)
- {
- vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryHorizontalOffset[i])*g_aryWeight[i];
- }
-
- gl_FragColor = vec4Sum;
- }
- Mean过滤 - 用于纹理抗锯齿
- const int g_iWeightNumber = 9;
-
- uniform sampler2D g_FilterTexture;
- uniform float g_aryWeight[g_iWeightNumber];
- uniform vec2 g_aryOffset[g_iWeightNumber];
- void main()
- {
- vec4 vec4Sum = vec4(0.0);
- if (gl_TexCoord[0].s < 0.495)
- {
- for(int i = 0; i < g_iWeightNumber; ++i)
- {
- vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryOffset[i])*g_aryWeight[i];
- }
- }
- else if (gl_TexCoord[0].s > 0.505)
- {
- vec4Sum = texture2D(g_FilterTexture, gl_TexCoord[0].st);
- }
- else
- {
- vec4Sum = vec4(1.0, 0.0, 0.0, 1.0);
- }
- gl_FragColor = vec4Sum;
- }
- Laplacian过滤 - 用于突显图像轮廓
- const int g_iWeightNumber = 9;
-
- uniform sampler2D g_FilterTexture;
- uniform float g_aryWeight[g_iWeightNumber];
- uniform vec2 g_aryOffset[g_iWeightNumber];
- void main()
- {
- vec4 vec4Sum = vec4(0.0);
- if (gl_TexCoord[0].s < 0.495)
- {
- for(int i = 0; i < g_iWeightNumber; ++i)
- {
- vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryOffset[i])*g_aryWeight[i];
- }
- vec4 vec4BaseColor = texture2D(g_FilterTexture, gl_TexCoord[0].st);
- vec4Sum += vec4BaseColor*0.1;
- }
- else if (gl_TexCoord[0].s > 0.505)
- {
- vec4Sum = texture2D(g_FilterTexture, gl_TexCoord[0].st);
- }
- else
- {
- vec4Sum = vec4(1.0, 0.0, 0.0, 1.0);
- }
- gl_FragColor = vec4Sum;
- }
- Emboss过滤 - used to simulate the relief of an image
- const int g_iWeightNumber = 9;
-
- uniform sampler2D g_FilterTexture;
- uniform float g_aryWeight[g_iWeightNumber];
- uniform vec2 g_aryOffset[g_iWeightNumber];
- void main()
- {
- vec4 vec4Sum = vec4(0.0);
- if (gl_TexCoord[0].s < 0.495)
- {
- for(int i = 0; i < g_iWeightNumber; ++i)
- {
- vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryOffset[i])*g_aryWeight[i];
- }
- vec4Sum += 0.5;
- }
- else if (gl_TexCoord[0].s > 0.505)
- {
- vec4Sum = texture2D(g_FilterTexture, gl_TexCoord[0].st);
- }
- else
- {
- vec4Sum = vec4(1.0, 0.0, 0.0, 1.0);
- }
- gl_FragColor = vec4Sum;
- }
- Sharpness过滤 - 突显图像的细节
- const int g_iWeightNumber = 9;
-
- uniform sampler2D g_FilterTexture;
- uniform float g_aryWeight[g_iWeightNumber];
- uniform vec2 g_aryOffset[g_iWeightNumber];
- void main()
- {
- vec4 vec4Sum = vec4(0.0);
- if (gl_TexCoord[0].s < 0.495)
- {
- for(int i = 0; i < g_iWeightNumber; ++i)
- {
- vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryOffset[i])*g_aryWeight[i];
- }
- }
- else if (gl_TexCoord[0].s > 0.505)
- {
- vec4Sum = texture2D(g_FilterTexture, gl_TexCoord[0].st);
- }
- else
- {
- vec4Sum = vec4(1.0, 0.0, 0.0, 1.0);
- }
- gl_FragColor = vec4Sum;
- }
- Demo效果截图:
- exe文件:http://www.fileupyours.com/view/219112/GLSL/Image%20Filter%20Demo.rar