首页 > 代码库 > GLSL实现Image Filter 【转】

GLSL实现Image Filter 【转】

http://blog.csdn.net/a3070173/archive/2008/11/27/3390477.aspx

 

  1. 图像过滤无论是在作图工具还是特效的实现上都时有运用,这里发一些通常会使用到的图像
  2. 过滤着色器供有需要的朋友参考.
  3. 备注:这里列出的都是片元着色器,顶点着色器都是一样地简单变换顶点和传递纹理坐标.
  4. 高斯过滤 - 常用于进行图像模糊
  5. 横向过滤:
  6. const int g_iWeightNumber = 17;
  7.                                     
  8. uniform sampler2D g_FilterTexture;  // 被过滤纹理
  9. uniform float g_aryWeight[g_iWeightNumber]; // Blur权重数组
  10. uniform vec2 g_aryVerticalOffset[g_iWeightNumber];  // 横向Blur偏移数组
  11. void main()
  12. {
  13.     vec4 vec4Sum = vec4(0.0);
  14.     for(int i = 0; i < g_iWeightNumber; ++i)
  15.     {
  16.         vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryVerticalOffset[i])*g_aryWeight[i];
  17.     }
  18.         
  19.     gl_FragColor = vec4Sum;
  20. }
  21. 纵向过滤:
  22. const int g_iWeightNumber = 17;
  23.                                     
  24. uniform sampler2D g_FilterTexture;  // 被过滤纹理
  25. uniform float g_aryWeight[g_iWeightNumber]; // Blur权重数组
  26. uniform vec2 g_aryHorizontalOffset[g_iWeightNumber];    // 横向Blur偏移数组
  27. void main()
  28. {
  29.     vec4 vec4Sum = vec4(0.0);
  30.     for(int i = 0; i < g_iWeightNumber; ++i)
  31.     {
  32.         vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryHorizontalOffset[i])*g_aryWeight[i];
  33.     }
  34.         
  35.     gl_FragColor = vec4Sum;
  36. }
  37. Mean过滤 - 用于纹理抗锯齿
  38. // Mean kernel
  39. // 1 1 1
  40. // 1 1 1
  41. // 1 1 1    
  42. const int g_iWeightNumber = 9;
  43.                                     
  44. uniform sampler2D g_FilterTexture;  // 被过滤纹理
  45. uniform float g_aryWeight[g_iWeightNumber]; // Blur权重数组
  46. uniform vec2 g_aryOffset[g_iWeightNumber];  // 横向Blur偏移数组
  47. void main()
  48. {
  49.     vec4 vec4Sum = vec4(0.0);
  50.     if (gl_TexCoord[0].s < 0.495)
  51.     {
  52.         for(int i = 0; i < g_iWeightNumber; ++i)
  53.         {
  54.             vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryOffset[i])*g_aryWeight[i];
  55.         }
  56.     }
  57.     else if (gl_TexCoord[0].s > 0.505)
  58.     {
  59.         vec4Sum = texture2D(g_FilterTexture, gl_TexCoord[0].st);
  60.     }
  61.     else
  62.     {
  63.         vec4Sum = vec4(1.0, 0.0, 0.0, 1.0);
  64.     }
  65.     gl_FragColor = vec4Sum;
  66. }
  67. Laplacian过滤 - 用于突显图像轮廓
  68. // Edge detection kernel (Laplacian filter).
  69. // 0  -1 0
  70. // -1  4 -1
  71. // 0  -1 0      
  72. const int g_iWeightNumber = 9;
  73.                                     
  74. uniform sampler2D g_FilterTexture;  // 被过滤纹理
  75. uniform float g_aryWeight[g_iWeightNumber]; // Blur权重数组
  76. uniform vec2 g_aryOffset[g_iWeightNumber];  // 横向Blur偏移数组
  77. void main()
  78. {
  79.     vec4 vec4Sum = vec4(0.0);
  80.     if (gl_TexCoord[0].s < 0.495)
  81.     {
  82.         for(int i = 0; i < g_iWeightNumber; ++i)
  83.         {
  84.             vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryOffset[i])*g_aryWeight[i];
  85.         }
  86.         vec4 vec4BaseColor = texture2D(g_FilterTexture, gl_TexCoord[0].st);
  87.         vec4Sum += vec4BaseColor*0.1;
  88.     }
  89.     else if (gl_TexCoord[0].s > 0.505)
  90.     {
  91.         vec4Sum = texture2D(g_FilterTexture, gl_TexCoord[0].st);
  92.     }
  93.     else
  94.     {
  95.         vec4Sum = vec4(1.0, 0.0, 0.0, 1.0);
  96.     }
  97.     gl_FragColor = vec4Sum;
  98. }
  99. Emboss过滤 - used to simulate the relief of an image
  100. // Emboss filter
  101. // 2   0  0
  102. // 0  -1  0
  103. // 0   0  -1        
  104. const int g_iWeightNumber = 9;
  105.                                     
  106. uniform sampler2D g_FilterTexture;  // 被过滤纹理
  107. uniform float g_aryWeight[g_iWeightNumber]; // Blur权重数组
  108. uniform vec2 g_aryOffset[g_iWeightNumber];  // 横向Blur偏移数组
  109. void main()
  110. {
  111.     vec4 vec4Sum = vec4(0.0);
  112.     if (gl_TexCoord[0].s < 0.495)
  113.     {
  114.         for(int i = 0; i < g_iWeightNumber; ++i)
  115.         {
  116.             vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryOffset[i])*g_aryWeight[i];
  117.         }
  118.         vec4Sum += 0.5;
  119.     }
  120.     else if (gl_TexCoord[0].s > 0.505)
  121.     {
  122.         vec4Sum = texture2D(g_FilterTexture, gl_TexCoord[0].st);
  123.     }
  124.     else
  125.     {
  126.         vec4Sum = vec4(1.0, 0.0, 0.0, 1.0);
  127.     }
  128.     gl_FragColor = vec4Sum;
  129. }
  130. Sharpness过滤 - 突显图像的细节
  131. // Sharpness filter
  132. // -1  -1  -1
  133. // -1   9  -1
  134. // -1  -1  -1       
  135. const int g_iWeightNumber = 9;
  136.                                     
  137. uniform sampler2D g_FilterTexture;  // 被过滤纹理
  138. uniform float g_aryWeight[g_iWeightNumber]; // Blur权重数组
  139. uniform vec2 g_aryOffset[g_iWeightNumber];  // 横向Blur偏移数组
  140. void main()
  141. {
  142.     vec4 vec4Sum = vec4(0.0);
  143.     if (gl_TexCoord[0].s < 0.495)
  144.     {
  145.         for(int i = 0; i < g_iWeightNumber; ++i)
  146.         {
  147.             vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryOffset[i])*g_aryWeight[i];
  148.         }
  149.     }
  150.     else if (gl_TexCoord[0].s > 0.505)
  151.     {
  152.         vec4Sum = texture2D(g_FilterTexture, gl_TexCoord[0].st);
  153.     }
  154.     else
  155.     {
  156.         vec4Sum = vec4(1.0, 0.0, 0.0, 1.0);
  157.     }
  158.     gl_FragColor = vec4Sum;
  159. }
  160. Demo效果截图:
  161. exe文件:http://www.fileupyours.com/view/219112/GLSL/Image%20Filter%20Demo.rar