uniform sampler2D 	clouds;
varying vec2 		texcoord;
varying float 		depth;

uniform float 		overcast;
uniform vec4		skycolor;
uniform vec3		sundir;
uniform vec3		camdir;
varying vec3		pos;
uniform vec3 		sunpos;
/*varying float		h;
varying vec3		cnormal;
varying float		depth;	*/
//uniform vec4		SUN;

// costanti passate allo shader
uniform vec3  SUN; 	// = colore del sole
uniform vec3  cBr; 	// = RayLeigh
uniform vec3  cBm; 	// = Mie	
uniform	vec3  C0; 	// = cBr+cBm;	
uniform vec3  C1; 	// = 1.0/(cBr+cBm);

uniform float C2;	// = 3/16*PIGRECO
uniform float C3;	// = 1/4*PIGRECO
uniform float C4;	// = (1-g)^2
uniform float C5;	// = 1+g^2
uniform float C6;	// = 2*g
uniform float C7;	// = 3/2

////////////////////////////
void main()
{
	// compute normal - sundir angle
	/*float OM=clamp(dot(sundir.xyz,cnormal.xyz),0.0,1.0);
	float Fr=0.75+0.75*OM*OM;
	float Fm=step(1.0,OM)+0.75;
	//vec4 CV=(vec4(0.5,0.6,0.6,1.0)*Fr + vec4(0.5,0.7,0.9,1.0)*Fm)*suncolor*h;
	
	vec4 CV=(vec4(0.5,0.6,0.6,1.0)*Fr + vec4(0.7,0.7,0.7,1.0)*Fm)*suncolor*h;
	
	gl_FragColor=CV;*/
	
	/*cnormal=normalize(cnormal);
	float cos=dot(sundir,cnormal);
	float cos2=pow(clamp(cos,0.0,1.0),500.0);

	float F1=C2*(1.0 + cos*cos);
	float F2=C3*(C4/pow(C5-C6*cos,C7));

	vec3 Br = cBr*F1;
	vec3 Bm = cBm*F2;

//	vec3 Fex = exp(-C0*depth*0.0001);
	vec3 Lin = (Br+Bm)*C1*SUN*h; 
		
	gl_FragColor.xyz=skycolor + Lin*5.0 + cos2*SUN;
	gl_FragColor.w=1.0;*/
	
	vec3 dir=normalize(sunpos-pos);
	vec3 clouddir=normalize(pos);
	vec4 cloudcolor=texture2D(clouds,texcoord.st);
	vec3 normal=(cloudcolor.xyz*2.0-1.0);
	vec3 N=normal;
	N.y=normal.z;
	N.z=-normal.y;
	N.x=-normal.x;
	float cos=clamp(dot(dir,N/*-sundir,clouddir*/),0.0,1.0);
	float cos3=pow(clamp(dot(-sundir,clouddir),0.9,1.0),2.0);
	float cos2=pow(clamp(dot(-sundir,clouddir),0.0,1.0),30.0)*3.0;
	
	float d=clamp(depth,0.0,2500.0);
	d/=2500.0;
	
	//cos=cos2;
	//cos2=pow(cos,10.0);
	
	float density=cloudcolor.w;//-overcast,0.0,1.0);
	//f=pow(0.5,f);
	//f=1.0-f;
	
	gl_FragColor.xyz=(vec3(0.4,0.4,0.4)*SUN+vec3(0.5,0.5,0.5)*(1.0-cos*cos3)*SUN)+pow(1.0-cos,15.0)*SUN+(1.0-cloudcolor.w)*SUN*cos2;//+SUN*cos2*(1.0-(cloudcolor.w*(1.0-clamp(cos,0.1,0.2))));//*Fex;
	gl_FragColor.w=density*(1.0-d);
}
