//uniform sampler2D 	tex;
uniform sampler2D 	bump;
varying vec2 		texcoord;
//varying vec4		pos;
varying float		fadeAlpha;

////////////////////////////
vec2 encode(vec3 n)
{
	n=normalize(n);
    float f = sqrt(8.0*n.z+8.0);
    return n.xy / f + 0.5;
}

void main()
{
	//float color=texture2D(tex,texcoord.st*5.0).r;
	//float alpha=texture2D(tex,texcoord.st).a;
	vec4 wavesNormal=texture2D(bump,texcoord.st);
	//float spume=texture2D(bump,texcoord.st*50.0).a;
	vec3 normal=wavesNormal.xyz*2.0-1.0;
	
	vec3 N=normal;
	N.y=normal.z;
	N.z=-normal.x;
	N.x=-normal.y;
	N=normalize(N);
	N*=vec3(10.0,10.0,10.0);
	N.xy=encode(N/*alpha*fadeAlpha*/);	
	
	// normal
	gl_FragColor.xy=N.xy;
	gl_FragColor.z=min((wavesNormal.a*fadeAlpha-0.6)*2.0,1.0);
	gl_FragColor.a=clamp(wavesNormal.a/*color*/*fadeAlpha,0.0,1.0);//*color.r;//0.0;//alpha*coordMod.x;
}
