I am trying to make an app which use displacement map for 3d effect. In Flash it can be done using displacement filter like shown here: http://flashflex.com/pixel-bender-displacement-map-fakes-3d-effect-update/. There are few apps who do that on iPhone like one here: http://www.youtube.com/watch?v=NvCHHUN8nnE
I am wondering how do they perform this.
I am trying to use displacement map based technique describe here: http://www.cocos2d-iphone.org/forum/topic/11659 but it seems to be slow to be done on the fly.
Any pointer would be highly appreciated.
Thanks
Perhaps the simplest way to achieve displacement mapping in OpenGL ES 2.0 is also probably the fastest. You need two textures, one to contain color (a regular one), and a displacement map. When sampling the regular texture normally, your fragment shader would look like:
This is pretty simple stuff. Now, to achieve displacement mapping, you need to:
This is also simple. You have another texture where red and green are used as x and y for the displacement. This way, however, you only get static, view-independent displacement. To get real displacement, one would need to generate texture tangents and bitangents which contain the directions of texture axes in object space (a tricky concept, read the article). Once you have them, all you need is object-space eye position (can be calculated by multiplying vertex position by modelview-projection inverse in vertex shader), and by projecting this on the tangent/bitangent vectors, you can modulate the displacement in order to be view dependent:
(that was vertex shader), and fragment shader here:
And that should do the trick … (note i wrote that from head so if there are any errors, don’t hesitate to comment)
EDIT: And there were errors, I swapped order of arguments to texture2D (sampler goes first).