Friday, November 18, 2011

Twirl filter in GLSL

I had worked on image processing project back in my high school days and so the last memories returned recently when I wanted to implement the twirl filter in GLSL. Mathematically, it is easily given as the following function in polar coordinates,
F(r,theta)=f(r,theta+amt)
In normal world though, we are working in Cartesian coordiates so we need to add that conversion in. Other than that, it is the same function. Here is the output from my shader.
Twirl Filter in GLSL
The Lena image filtered with the twirl filter in GLSL.
And here is the GLSL fragment shader for doing this.

uniform sampler2D textureMap; //image
uniform float amount; //amount of twirl
void main() {
   vec2 uv = gl_TexCoord[0].st-0.5;
   float angle = atan2(uv.y,uv.x);
   float radius = length(uv);
   angle+= radius*amount;
   vec2 shifted = radius*vec2(cos(angle), sin(angle));
   gl_FragColor = texture(textureMap, (shifted+0.5));
}

I hope it is useful for others too.

Enjoy!!!

0 comments:

Popular Posts

Copyright (C) 2011 - Movania Muhammad Mobeen. Awesome Inc. theme. Powered by Blogger.