[Vertex shader]

float4x4 mvp;
float3 lightPos;
float3 camPos;
float invRad;

struct VS_OUTPUT {
	float4 Pos:      POSITION;
	float2 texCoord: TEXCOORD0;
	float3 lightVec: TEXCOORD1;
	float3 viewVec:  TEXCOORD2;
};

VS_OUTPUT main(float4 pos: POSITION, float2 texCoord: TEXCOORD0, float3 tangent: TEXCOORD1, float3 binormal: TEXCOORD2, float3 normal: TEXCOORD3){
	VS_OUTPUT Out;

	Out.Pos = mul(mvp, pos);
	Out.texCoord = texCoord;

	float3 lightVec = invRad * (lightPos - pos);
	float3 viewVec = camPos - pos;

	Out.lightVec.x = dot(lightVec, tangent);
	Out.lightVec.y = dot(lightVec, binormal);
	Out.lightVec.z = dot(lightVec, normal);

	Out.viewVec.x = dot(viewVec, tangent);
	Out.viewVec.y = dot(viewVec, binormal);
	Out.viewVec.z = dot(viewVec, normal);
	return Out;
}

[Fragment shader]

sampler Bump;
sampler Base;

float4 lightColor;
float2 pSize;

float4 main(float2 texCoord: TEXCOORD0, float3 lightVec: TEXCOORD1, float3 viewVec: TEXCOORD2) : COLOR {
	float atten = saturate(1 - dot(lightVec, lightVec));
	lightVec = normalize(lightVec);
	viewVec = normalize(viewVec);
	
	float height = tex2D(Bump, texCoord).a;
	float2 newTexCoord = texCoord + viewVec.xy * (height * pSize.x + pSize.y);

	float4 base = tex2D(Base, newTexCoord);
	float3 bump = tex2D(Bump, newTexCoord) * 2 - 1;

//	return base;
	bump = normalize(bump);

	float diffuse = saturate(dot(lightVec, bump));
	float specular = pow(saturate(dot(reflect(-viewVec, bump), lightVec)), 16);

	return lightColor * (diffuse * base + 0.7 * specular) * atten;
}
