struct vertex {
	float4 position  : POSITION;
	float4 color0    : COLOR0;
	float2 texcoord0 : TEXCOORD0;
	float4 normal    : NORMAL;
};

struct fragment {
	float4 position   : POSITION;
	float4 color      : COLOR0;
	float3 normal     : COLOR1;
	float2 texcoord0  : TEXCOORD0;
	float light       : TEXCOORD1;
	float3 lightDir   : TEXCOORD2;
	float2 envcoord   : TEXCOORD3;
	float3 realPos    : TEXCOORD4;
	float2 screenPos  : TEXCOORD5;
};

fragment main(vertex IN,
			  uniform float4x4 modelViewProj,
			  uniform float4x4 modelView,
			  uniform float4x4 modelViewIT,
			  uniform float2x2 textureMatrix,
			  uniform float3 light)
{
	fragment OUT;

	float4 position = mul(modelViewProj, IN.position);
	OUT.position = position;

	OUT.screenPos = 0.5+position.xy;
	OUT.screenPos *= 0.625;
//	OUT.screenPos.y *= 0.9375;
	OUT.screenPos.y -= 0.1;
//	OUT.screenPos.y *= 0.4;
//	OUT.screenPos.y = 1-OUT.screenPos.y;

	OUT.texcoord0 = mul(textureMatrix, IN.texcoord0);
	OUT.color = IN.color0;

	OUT.normal = 0.5+0.5*normalize(mul(modelView, IN.normal).xyz);

	float3 xyz = normalize(light.xyz-IN.position.xyz);
	OUT.light = dot(normalize(IN.normal.rgb), xyz);

	OUT.lightDir = xyz;

	//float2 envcoord = 0.5+0.5*normalize(mul(modelView, IN.normal).xy);
/*	float4 normal = normalize(mul(modelView, IN.normal));
	normal = normalize(float4(xyz,1)*normal);
	float2 envcoord = 0.5+0.5*normalize(mul(modelViewIT, normal).xy);
	envcoord = envcoord*envcoord*envcoord*envcoord;
	OUT.envcoord = envcoord;*/

	OUT.envcoord = 0.5+0.5*normalize(mul(modelView, IN.normal*float4(light,1)).xyz).xy;

	OUT.realPos = mul(modelView, IN.position).xyz;

	return OUT;
}