const float dx = 1.0 / 1024.0;
const float dy = 1.0 / 768.0;

uniform sampler2D wbuffer_disp;
uniform sampler2D ibuffer;

uniform float lpx, lpy;
uniform bool water_bump;

void main()
{
	vec2 tc = gl_TexCoord[0].st;

	tc.s = tc.s - dx;
	vec4 hl = texture2D(wbuffer_disp, tc);

	tc.s = tc.s + 2.0 * dx;
	vec4 hr = texture2D(wbuffer_disp, tc);

	tc.s = tc.s - dx;
	tc.t = tc.t + dy;
	vec4 hu = texture2D(wbuffer_disp, tc);

	tc.t = tc.t - 2.0 * dy;
	vec4 hd = texture2D(wbuffer_disp, tc);

	tc.t = tc.t + dy;

	float dispx = (hl.r - hr.r) * 0.125;
	float dispy = (hd.r - hu.r) * 0.125;

	tc.s = tc.s + dispx;
	tc.t = tc.t + dispy;

	vec4 color = texture2D(ibuffer, tc);


	vec2 ttc = gl_TexCoord[0].st;

	float shade = 1.0;
	if (water_bump==true)
		shade = 1.0 / distance(vec2((ttc.s - 0.5 + lpx) + dispx * 6.0, (ttc.t - 0.5 + lpy) + dispy * 6.0), vec2(0.0, 0.0));

	gl_FragColor = color * shade;
}
