PDA

View Full Version : Doing basic distortion in a fragment shader



Cubby208
02-13-2016, 11:36 PM
HI I have a program that currently renders a bunch of stuff onto a texture. I was hoping to be able to re render that texture with various distortion effects based on user interaction. I can figure out the whole user interaction thing by myself but I do not know any distortion techniques. An ideal situation is if I would be able to make a texture that is a map for distortion (sorta like a bump map) and then have the shader do it.

GClements
02-14-2016, 06:14 AM
You can use a texture to map texture coordinates, e.g.


in vec2 texcoord;
uniform sampler2D u_coord_map; // GL_RG
uniform sampler2D u_texture;

void main() {
vec2 t = texture(u_coord_map, texcoord).st;
vec4 color = texture(u_texture, t);
....
}


How much use that is depends upon how easy it is to generate the coordinate mapping.

When you say "like a bump map", it seems that you want to be able to generate texture coordinates based upon local distortion. That typically requires deconvolution, for which this technique (http://stannum.co.il/blog/1/reconstructing-a-height-map-from-a-normal-map) may be useful.

Another technique which may be useful (it's hard to say; "distortion" is a rather vague concept) is a radial basis network. This allows you to define a N-dimensional mapping using (source,destination) pairs of vertices such that each source vertex maps exactly to the corresponding destination vertex and the mapping is continuous. WebGL example (http://www.zen87603.zen.co.uk/webgl/warp/warp.html).