sampler2D Tex0;	// basetex
sampler2D Tex2;	// shininessmap
sampler2D Tex7;	// bumpmap

samplerCUBE ReflectionTex;
sampler2D WaterEffectTex;

float3 Half;
float3 FogColor;

float4x4 View;
float4x4 Proj;

struct PS_INPUT{
	float4 Diff				: COLOR0;
	
	float2 BaseTex			: TEXCOORD0;
	float3 ViewDir			: TEXCOORD1;
	
	float2 BumpTex0			: TEXCOORD2;
	float2 BumpTex1			: TEXCOORD3;
	float4 ReflectionTex	: TEXCOORD4;
	
	float FogIntensity		: TEXCOORD5;
};

struct PS_OUTPUT{
    float4 Color			: COLOR;
};

PS_OUTPUT main(const PS_INPUT Input){
	// Compute normal
	float3 N0 = (tex2D(Tex7, Input.BumpTex0) - 0.5f) * 2;
	float3 N1 = (tex2D(Tex7, Input.BumpTex1) - 0.5f) * 2;
	
	float3 N = float3(0, 1, 0);//lerp(normalize(N0 + N1), float3(0, 1, 0), 0.95f);
	N = normalize(N);
	float3 V = normalize(Input.ViewDir);
	V.y *= -1;

	// Compute lighting
	float3 R = 2 * dot(N, V) * N - V;
		
	// Compute result
	float3 ShininessMap = tex2D(Tex2, Input.BaseTex);
	
	R.z *= -1;
	float3 Result = texCUBE(ReflectionTex, R) * ShininessMap;
	
	// Compute real reflection results
	float3 FuckedR = float3(R.x, R.y, -R.z);
	float3 ViewSpaceR = mul(FuckedR, (float3x3)View);
	
	float4 ProjSpaceR = mul(float4(ViewSpaceR, 1), Proj);
	
	float2 ReflectionTex = ProjSpaceR.xy / ProjSpaceR.w;
	ReflectionTex.x = (ReflectionTex.x + 1) / 2;
	ReflectionTex.y = (-ReflectionTex.y + 1) / 2;
	
	/*float2 ReflectionTex = Input.ReflectionTex.xy / Input.ReflectionTex.w;
	ReflectionTex.x = (ReflectionTex.x + 1) / 2;
	ReflectionTex.y = (-ReflectionTex.y + 1) / 2;*/
	
	//ReflectionTex.x += N.x * 0.1f;
	//ReflectionTex.y += N.z * 0.1f;
	
	float3 WaterReflection = tex2D(WaterEffectTex, ReflectionTex);
	
	Result *= WaterReflection;
	
	// Compute basecolor
	float3 BaseTexColor = tex2D(Tex0, Input.BaseTex);
	
	float3 TexColor = BaseTexColor + Result;
	//TexColor += FogColor * Input.FogIntensity;
	//TexColor = lerp(TexColor, FogColor, Input.FogIntensity);

	PS_OUTPUT Out;
	Out.Color.rgb = TexColor;
	Out.Color.a = 1.0f;
	return Out;
}