Graphics Reference
In-Depth Information
out vec4 fFragColor;
void main( )
{
ivec2 ires = textureSize( uImageUnit, 0 );
float Res = float( ires.s ); // assume it's a square
// texture image
vec2 st = vST;
float Radius = Res * uR;
vec2 xy = Res * st; // pixel coordinates from
// texture coords
vec2 dxy = xy - Res/2.; // twirl center is (Res/2, Res/2)
float r = length( dxy );
float beta = atan(dxy.y,dxy.x) + radians(uD)*
(Radius-r)/Radius;
vec2 xy1 = xy;
if (r <= Radius)
{
xy1 = Res/2. + r * vec2( cos(beta), sin(beta) );
}
st = xy1/Res; // restore coordinates
vec3 irgb = texture( uImageUnit, st ).rgb;
fFragColor = vec4( irgb, 1. );
}
Image warping need not be uniform, of course, and you can readily use
noise functions, as described in the previous chapter, to modify the address
of a source pixel in an image. Some code for a fragment shader to do this is
below, and the result is shown in Figure 11.18.
Figure 11.18.
The grid (left) and cherry blossom image (right) with noise as pixel offset.