分享

(Shader Library) Swirl Post Processing Filter in GLSL | Geeks3D

 勤奋不止 2015-09-11

Swirl filter in GLSL



UPDATE (2014.06.25): a Twirl effect demo is also available with GLSL Hacker. Visit this thread for more information.

Twirl filter in GLSL


Today a new pixel hack for our shader library: the popular swirl (or whirl) effect, as usual coded in GLSL. You should be able to use the swirl effect in any OpenGL app with few modification. The principle of the swirl effect is to rotate the texture coordinates.

I quickly coded a small GeeXLab demo to show the whirl post processing filter in action. You can interactively tweak the different parameters (radius, angle and swirl center position). The demo is available in GeeXLab code samples pack (PostFX_Swirl/ folder). You can download the complete code sample pack from this page or here:
Download GeeXLab Code Samples Pack Version 2009.10.02
Webmasters: hotlinking is not allowed (that will cause an error message), please use the post url as download link



You need the latest version of GeeXlab to run this demo. Start GeeXLab and load (or drag and drop) DEMO.xml in GeeXLab. That’s all.

I only tested the swirl GLSL effect with a Radeon card (HD 5850) + Cat 11.4 but it should work fine on NVIDIA boards. If it’s not the case, feel free to leave a comment.

Now the complete post processing swirl / whirl shader in GLSL:

  1. [Vertex_Shader]
  2. void main()
  3. {
  4. gl_Position = ftransform();
  5. gl_TexCoord[0] = gl_MultiTexCoord0;
  6. }
  7.  
  8. [Pixel_Shader]
  9. // Scene buffer
  10. uniform sampler2D tex0;
  11.  
  12. // Currently not used in this demo!
  13. uniform float time;
  14.  
  15. // GeeXLab built-in uniform, width of
  16. // the current render target
  17. uniform float rt_w;
  18. // GeeXLab built-in uniform, height of
  19. // the current render target
  20. uniform float rt_h;
  21.  
  22. // Swirl effect parameters
  23. uniform float radius = 200.0;
  24. uniform float angle = 0.8;
  25. uniform vec2 center = vec2(400.0, 300.0);
  26.  
  27. vec4 PostFX(sampler2D tex, vec2 uv, float time)
  28. {
  29. vec2 texSize = vec2(rt_w, rt_h);
  30. vec2 tc = uv * texSize;
  31. tc -= center;
  32. float dist = length(tc);
  33. if (dist < radius)
  34. {
  35. float percent = (radius - dist) / radius;
  36. float theta = percent * percent * angle * 8.0;
  37. float s = sin(theta);
  38. float c = cos(theta);
  39. tc = vec2(dot(tc, vec2(c, -s)), dot(tc, vec2(s, c)));
  40. }
  41. tc += center;
  42. vec3 color = texture2D(tex0, tc / texSize).rgb;
  43. return vec4(color, 1.0);
  44. }
  45.  
  46. void main (void)
  47. {
  48. vec2 uv = gl_TexCoord[0].st;
  49. gl_FragColor = PostFX(tex0, uv, time);
  50. }

Source of the shader: I adapted this swirl WebGL code (too easy I know…) for GeeXLab.

Swirl / whirl filter in GLSL

Swirl / whirl filter in GLSL

Swirl / whirl filter in GLSL


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多