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. uniform sampler2D g_FilterTexture;  // 被过滤纹理
  8. uniform float g_aryWeight[g_iWeightNumber]; // Blur权重数组
  9. uniform vec2 g_aryVerticalOffset[g_iWeightNumber];  // 横向Blur偏移数组
  10. void main()
  11. {
  12. vec4 vec4Sum = vec4(0.0);
  13. for(int i = 0; i < g_iWeightNumber; ++i)
  14. {
  15. vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryVerticalOffset[i])*g_aryWeight[i];
  16. }
  17. gl_FragColor = vec4Sum;
  18. }
  19. 纵向过滤:
  20. const int g_iWeightNumber = 17;
  21. uniform sampler2D g_FilterTexture;  // 被过滤纹理
  22. uniform float g_aryWeight[g_iWeightNumber]; // Blur权重数组
  23. uniform vec2 g_aryHorizontalOffset[g_iWeightNumber];    // 横向Blur偏移数组
  24. void main()
  25. {
  26. vec4 vec4Sum = vec4(0.0);
  27. for(int i = 0; i < g_iWeightNumber; ++i)
  28. {
  29. vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryHorizontalOffset[i])*g_aryWeight[i];
  30. }
  31. gl_FragColor = vec4Sum;
  32. }
  33. Mean过滤 - 用于纹理抗锯齿
  34. // Mean kernel
  35. // 1 1 1
  36. // 1 1 1
  37. // 1 1 1
  38. const int g_iWeightNumber = 9;
  39. uniform sampler2D g_FilterTexture;  // 被过滤纹理
  40. uniform float g_aryWeight[g_iWeightNumber]; // Blur权重数组
  41. uniform vec2 g_aryOffset[g_iWeightNumber];  // 横向Blur偏移数组
  42. void main()
  43. {
  44. vec4 vec4Sum = vec4(0.0);
  45. if (gl_TexCoord[0].s < 0.495)
  46. {
  47. for(int i = 0; i < g_iWeightNumber; ++i)
  48. {
  49. vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryOffset[i])*g_aryWeight[i];
  50. }
  51. }
  52. else if (gl_TexCoord[0].s > 0.505)
  53. {
  54. vec4Sum = texture2D(g_FilterTexture, gl_TexCoord[0].st);
  55. }
  56. else
  57. {
  58. vec4Sum = vec4(1.0, 0.0, 0.0, 1.0);
  59. }
  60. gl_FragColor = vec4Sum;
  61. }
  62. Laplacian过滤 - 用于突显图像轮廓
  63. // Edge detection kernel (Laplacian filter).
  64. // 0  -1 0
  65. // -1  4 -1
  66. // 0  -1 0
  67. const int g_iWeightNumber = 9;
  68. uniform sampler2D g_FilterTexture;  // 被过滤纹理
  69. uniform float g_aryWeight[g_iWeightNumber]; // Blur权重数组
  70. uniform vec2 g_aryOffset[g_iWeightNumber];  // 横向Blur偏移数组
  71. void main()
  72. {
  73. vec4 vec4Sum = vec4(0.0);
  74. if (gl_TexCoord[0].s < 0.495)
  75. {
  76. for(int i = 0; i < g_iWeightNumber; ++i)
  77. {
  78. vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryOffset[i])*g_aryWeight[i];
  79. }
  80. vec4 vec4BaseColor = texture2D(g_FilterTexture, gl_TexCoord[0].st);
  81. vec4Sum += vec4BaseColor*0.1;
  82. }
  83. else if (gl_TexCoord[0].s > 0.505)
  84. {
  85. vec4Sum = texture2D(g_FilterTexture, gl_TexCoord[0].st);
  86. }
  87. else
  88. {
  89. vec4Sum = vec4(1.0, 0.0, 0.0, 1.0);
  90. }
  91. gl_FragColor = vec4Sum;
  92. }
  93. Emboss过滤 - used to simulate the relief of an image
  94. // Emboss filter
  95. // 2   0  0
  96. // 0  -1  0
  97. // 0   0  -1
  98. const int g_iWeightNumber = 9;
  99. uniform sampler2D g_FilterTexture;  // 被过滤纹理
  100. uniform float g_aryWeight[g_iWeightNumber]; // Blur权重数组
  101. uniform vec2 g_aryOffset[g_iWeightNumber];  // 横向Blur偏移数组
  102. void main()
  103. {
  104. vec4 vec4Sum = vec4(0.0);
  105. if (gl_TexCoord[0].s < 0.495)
  106. {
  107. for(int i = 0; i < g_iWeightNumber; ++i)
  108. {
  109. vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryOffset[i])*g_aryWeight[i];
  110. }
  111. vec4Sum += 0.5;
  112. }
  113. else if (gl_TexCoord[0].s > 0.505)
  114. {
  115. vec4Sum = texture2D(g_FilterTexture, gl_TexCoord[0].st);
  116. }
  117. else
  118. {
  119. vec4Sum = vec4(1.0, 0.0, 0.0, 1.0);
  120. }
  121. gl_FragColor = vec4Sum;
  122. }
  123. Sharpness过滤 - 突显图像的细节
  124. // Sharpness filter
  125. // -1  -1  -1
  126. // -1   9  -1
  127. // -1  -1  -1
  128. const int g_iWeightNumber = 9;
  129. uniform sampler2D g_FilterTexture;  // 被过滤纹理
  130. uniform float g_aryWeight[g_iWeightNumber]; // Blur权重数组
  131. uniform vec2 g_aryOffset[g_iWeightNumber];  // 横向Blur偏移数组
  132. void main()
  133. {
  134. vec4 vec4Sum = vec4(0.0);
  135. if (gl_TexCoord[0].s < 0.495)
  136. {
  137. for(int i = 0; i < g_iWeightNumber; ++i)
  138. {
  139. vec4Sum += texture2D(g_FilterTexture, gl_TexCoord[0].st + g_aryOffset[i])*g_aryWeight[i];
  140. }
  141. }
  142. else if (gl_TexCoord[0].s > 0.505)
  143. {
  144. vec4Sum = texture2D(g_FilterTexture, gl_TexCoord[0].st);
  145. }
  146. else
  147. {
  148. vec4Sum = vec4(1.0, 0.0, 0.0, 1.0);
  149. }
  150. gl_FragColor = vec4Sum;
  151. }
  152. Demo效果截图:
  153. GLSL实现Image Filter 【转】
  154. GLSL实现Image Filter 【转】
  155. GLSL实现Image Filter 【转】
  156. GLSL实现Image Filter 【转】
  157. GLSL实现Image Filter 【转】
  158. exe文件:http://www.fileupyours.com/view/219112/GLSL/Image%20Filter%20Demo.rar
上一篇:C# RSA加密/解密


下一篇:SqlSugar框架T4模板的使用