#version 130

uniform sampler2D texture;
uniform sampler2D heightmap;
const vec2 size = vec2(2.0, 0.0);
const ivec3 offset = ivec3(-1, 0, 1);

void main(void)
{
	vec2 coord = gl_TexCoord[0].xy;
	float s01 = textureOffset(heightmap, coord, offset.xy).x;
	float s21 = textureOffset(heightmap, coord, offset.zy).x;
	float s10 = textureOffset(heightmap, coord, offset.yx).x;
	float s12 = textureOffset(heightmap, coord, offset.yz).x;
	vec3 va = normalize(vec3(size.xy, s21 - s01));
	vec3 vb = normalize(vec3(size.yx, s12 - s10));
	vec2 dudv = cross(va, vb).rg;
	gl_FragColor = texture2D(texture, coord + dudv * 0.2);
}