varying vec2 		texcoord;

uniform sampler2D 	bmp;
uniform sampler2D 	color;

#ifdef LAYERMASK

	uniform sampler2D 	mask;

#endif

uniform samplerCube	reflCube;
/*varying vec3		w0,
					w1,
					w2;*/
varying mat3		TBN;

varying vec3		viewDir;
uniform float		reflScale;
uniform vec4		sunColor;
uniform float 		reflColorMul;
uniform vec3 		reflColor;
uniform vec2		fresnel;
uniform float		reflPow;
uniform float		specPow;

void main()
{
	vec4 refl=vec4(0.0,0.0,0.0,0.0);
	vec4 normalColor=texture2D(bmp,texcoord.st);
	vec4 diffuseColor=texture2D(color,texcoord.st);
	float alpha=/*normalColor.a**/specPow;
	
	#ifdef LAYERMASK
	
		vec3 maskColor=texture2D(mask,texcoord.st).rgb;
		alpha*=(1.0-min(maskColor.r+maskColor.g+maskColor.b,1.0));
	
	#endif
	
	vec3 normal=(normalColor.xyz*2.0)-1.0;
	normalize(normal);
	
	vec3 N;
	/*N.x=dot(w0,normal);
	N.y=dot(w1,normal);
	N.z=dot(w2,normal);*/
	N=TBN*normal;
	//N=normalize(N);
	
	vec3 vdir=normalize(viewDir);
//	vec3 H = ldir+E;
//	H=normalize(H);
//	float NdotH = dot(H,normal.xyz);
//	float EdotH = dot(E,H);
	//float NdotE = dot(N,vdir);
	//float NdotL = dot(ldir,normal.xyz);
	vec3 reflection = reflect(vdir,N);
	//float F=0.1+0.9*pow((1.0-NdotE),5.0);
	//float iFresnel=max((F*pow(NdotH,mat.y)),0.0);
	//float fresnelPow=clamp(pow(1.0-clamp(dot(N,vdir),0.0,1.0),fresnel.x),0.0,0.9999)*reflPow;
	//fresnelPow=max(0.0,fresnelPow);
	refl=textureCubeLod(reflCube,reflection.xyz,(1.0-alpha)*7.0)*diffuseColor;//*reflColorMul*reflScale*F;//*max(0.0,reflColorMul*shininess/*fresnel.y*/*fresnelPow);
	
	/*#ifdef LAYERMASK
	
		fresnelPow*=alpha;
	
	#endif*/
	
	gl_FragColor.rgb=max(refl.rgb*reflColor,0.0);//*alpha;
	gl_FragColor.a=1.0;//alpha;//1.0-clamp(iFresnel,0.0,1.0);//max(fresnelPow,0.0);//fresnelPow;//clamp(fresnelPow,0.0,1.0);
}
