#include "Interface.fx"

/*******************************************************************/

struct vsMid
{
	float4	pos				: POSITION;
	float2	diffuseUV		: TEXCOORD0;
	float3	diffuseCube		: TEXCOORD1;
	float	fog				: FOG;
	float3	specularCube	: TEXCOORD2;
	float2	controlUV		: TEXCOORD3;
};

struct vsDst
{
	float4	color	: COLOR0;
};

/*******************************************************************/

vsMid VS(vsPNDT src)
{
	vsMid dst;

// Set position:

	dst.pos	= mul(float4(src.pos, 1), argMVP);

// Set control UV:

	dst.controlUV = src.tex0;

// Set diffuseUV:

	dst.diffuseUV = src.tex0;

// Set diffuseCube:

	float3 normal = mul(src.normal, (float3x3) argMatrix);

	dst.diffuseCube = normal;

// Set fog:

	dst.fog = argFog(dst.pos.z);

// Set specularCube (reflective ray = incoming ray flipped around normal):

	dst.specularCube = argCameraZ - 2.0 * normal * dot(normal, argCameraZ);

	return dst;
}

/*******************************************************************/

vsDst PS(vsMid src)
{
	vsDst dst;

// Read textures:

	float4 diffuseTex		= tex2D  (Sampler0L, src.diffuseUV   );
	float3 controlTex		= tex2D	 (Sampler1L, src.controlUV   ).rgb;
	float3 diffuseLighting	= texCUBE(SamplerDL, src.diffuseCube ).rgb;
	float3 specularLighting	= texCUBE(SamplerSL, src.specularCube).rgb;

// Set color:

	float3 diffuseColor			= diffuseLighting * diffuseTex.rgb;
	float3 specularColor		= (4 * specularLighting) * diffuseTex.rgb;
	float3 illuminationColor	= 2 * (controlTex.x * diffuseTex.rgb);

	dst.color.rgb	= diffuseColor + specularColor + illuminationColor + argColor.rgb;
	dst.color.a		= argColor.a;

	return dst;
}

/*******************************************************************/

technique VS11PS13
{
	pass P0
	{
		FogEnable		= True;
		VertexShader	= compile vs_1_1 VS();
		PixelShader		= compile ps_1_3 PS();
	}
}
