// homo

struct vertex {
	float4 position : POSITION;
	float2 tex0     : TEXCOORD0;
	float4 light    : COLOR0;
	float4 normal   : NORMAL;
};

struct fragment {
	float4 position : POSITION;
	float2 tex0     : TEXCOORD0;
	float4 normal   : COLOR0;
	float4 light    : COLOR1;
	float3 view     : TEXCOORD2;
	float fog       : TEXCOORD3;
	float3 itpos    : TEXCOORD4;
};

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

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

	OUT.tex0 = mul(textureMatrix, IN.tex0);

	OUT.itpos = mul(modelViewIT, IN.position).xyz;

	// set multiplier (=fog density) bigger to bring fog closer
	OUT.fog = 0.45*length(mul(modelViewIT, IN.position).xyz);
//	OUT.fog = 0.11*length(mul(modelViewIT, IN.position).xyz);

	float4 worldpos = mul(modelViewIT, IN.normal);
	OUT.normal.xyz = normalize(worldpos.xyz);

	OUT.light = IN.light;

	// paska shit
	float3 lightDir = IN.light.rgb-IN.position.rgb;
	float3 bi = cross(IN.light.rgb, IN.normal.xyz);
	float3x3 lightMatrix = {IN.light.rgb, bi, IN.normal.rgb};
	lightDir = mul(lightMatrix, lightDir);
	OUT.view = camera-IN.position.xyz;
	OUT.view = mul(lightMatrix, OUT.view);

	return OUT;
}