uniform samplerCube Env;
uniform sampler2D NormalMap;
uniform vec3 CamPos;
uniform float WaterTimer;


varying vec2 texCoord;
varying vec3 vertexPos;
varying vec3 camVec;


void main()
{
	vec3 camVec2 = normalize(camVec);
	
	/*camVec2.y = -camVec2.y;
	
	vec4 reflectColor = textureCube(Env, camVec2);
	
	gl_FragColor = vec4(reflectColor.xyz, 1.0f);
	*/
	
	mat3 rot;

	rot[0] = vec3(0.0,0.0,1.0);
	rot[1] = vec3(1.0,0.0,0.0);
	rot[2] = vec3(0.0,1.0,0.0);
	
	float sinx = 0.025*sin(WaterTimer+texCoord.x*5);
	
	vec3 NVec1 = texture2D(NormalMap, vec2(-WaterTimer*0.04 + texCoord.x + sinx, -WaterTimer*0.04 + texCoord.y - sinx)).rgb;
	vec3 NVec2 = texture2D(NormalMap, vec2( WaterTimer*0.04 + texCoord.y - sinx, -WaterTimer*0.04 + (1.0-texCoord.x) + sinx)).rgb;
	
	NVec1 = (NVec1 -0.5) * 2.0;
	NVec2 = (NVec2 -0.5) * 2.0;
	
	vec3 NVec = NVec1 + NVec2;
	vec3 NVecReflect = normalize(rot * (NVec1 + NVec2) + vec3(0,8,0));
	vec3 NVecRefract = normalize(rot * (NVec1 + NVec2));

	vec3 dir1 = reflect((camVec2),NVecReflect);
	//vec3 dir2 = refract((camVec2),NVecRefract,0.9);
	vec3 dir2 = reflect((camVec2),NVecReflect);
	dir2.y = - dir2.y;
	
	vec4 reflectColor = textureCube(Env, dir1);
	vec4 refractColor = textureCube(Env, dir2);
	
	vec4 texColor = reflectColor*0.5 + refractColor*0.5;
	
	gl_FragColor = vec4(texColor.xyz, 1.0f);

	

}