This is the first post of what may be a a little series on displacement mapping. This is the demo for this article.
First thing first, what is displacement mapping ?
Simply said, displacement mapping is the act of modifying a bitmap or 3D model (but not only) with the data from another bitmap, also called the ‘displacement map’.
In order to do that, you need to sample the colour channels from each pixel in the displacement map. The colour channels then give you the amount of displacement you need to apply to the corresponding pixel in your source image or to the vertices in the model.
I’m going to show you an example using bitmaps.
Step 1: retrieve colour data
For every pixel in the source image, get the corresponding pixel in the displacement map, and sample the colour channels. The images shows the colour channel values for the pixel at x:10 y:1.

Step 2: using the colour data
The next step is to decide how to use the values. For our example, we will use the red channel for the X axis, and the green channel for the Y axis.
We will also use this simple rule:
for values ranging from 0 to 126, the displacement will be negative along the axis, and for values ranging from 127 to 255, displacements will be positive along the axis.
Now that we have our value (R:193, G:109), we need to find a way to make them “usable”. If we were to use those values as they are, we wouldn’t have much control on the displacement, we would just say: “for pixel at position x:10 y:1 in the source image, replace pixel color by pixel at x:10 + 193 y:1 – 109″. It’s not very flexible.
What we need is a way to “scale” the values.
Step 3: scaling the displacement
The way to do this, is by mapping the values from the colour channels from 0 – 255 to -1 – 1, and then multiply the result by a scaling value.
Note: when I speak about “mapping” here, I’m talking about taking a value contained in on range and “moving it over” to another range.
int r = 193;
int g = 109;
float scale = 8; // this can be any value you want
float mr = -1 + 2 * ( r / 255 ); // map r from 0/255 to -1/1
float mg = -1 + 2 * ( g / 255 ); // map g from 0/255 to -1/1
// rm = 0.513
// mg = -0.14
// multiply both mapped values to get the final displacement amount.
int dx = mr * scale;
int dy = mg * scale;
// dx = 4
// dy = -1
You can now see that by using a scaling factor we have much more control over the amount of displacement we are going to apply to our source image.
The bigger the scale, the bigger the displacement, the smaller the scale, the smaller the displacement… you get the point!
Step 4: moving pixels around
Finally, we get to the last step. Time to apply the displacement.
Get the channel values from the current pixel in the displacement map:

Compute the position of the new pixel:

Get the colour of the pixel at the displaced position:

Apply the new color to the value to the current pixel on the source image:

Here is a demo I built to show you the displacement map in action. I’m not using the AS3 built-in DisplacementMapFilter, and that’s how I got a circular displacement map without using masks or anything.

One thing to note is that you don’t really ever display the displacement map image. It’s basicaly just an image that you will use to get color data from to modify another image. Most of the time displacement maps will be filled with perlin noise, or just anything you want.
And that’s about it. Of course you need to repeat those steps for each pixel in the source image. I didn’t get in to much detail, so just ask google or feel free to leave me a comment or send me an email.