
struct PLANET_VS_INPUT
{
     float4 position : POSITION0;
     float3 normal   : NORMAL;
     float2 tex0     : TEXCOORD0;
};

struct PLANET_VS_OUTPUT 
{
   float4 Position :        POSITION0;
   float3 envUV   :         TEXCOORD4;   
   float2 Texcoord :        TEXCOORD0;
   float3 ViewDirection :   TEXCOORD1;
   float3 LightDirection :  TEXCOORD2;
   float3 Normal :          TEXCOORD3;
   float4 params	 	:   TEXCOORD5;
   
};


PLANET_VS_OUTPUT vs_planet (PLANET_VS_INPUT Input)
{
	PLANET_VS_OUTPUT Output = (PLANET_VS_OUTPUT)0;

	//getting the position of the vertex in the world
	float4 posWorld = mul(Input.position, matW);
	//float4 normal = normalize(mul(Input.normal, matW));
	float4 normal = normalize(mul(Input.normal, matW));
	//getting to position to object space
	float4 pos = mul(posWorld, matVP);

	Output.Position = pos;
	Output.Normal  = normal;

	float3 fvLightPosition  = lightDir;//cameraPos.xyz;
	Output.Texcoord         = Input.tex0;

	float3 fvObjectPosition = mul( Input.position, matW );
	Output.ViewDirection    = cameraPos - fvObjectPosition;
	Output.LightDirection   = fvLightPosition - fvObjectPosition;
	//Output.envUV            = normalize(mul( Input.normal, matV ));
	Output.envUV			= normal;
	return( Output );

}


float4 ps_planet(PLANET_VS_OUTPUT  Input) : COLOR0
{
	float4 color = tex2D(DiffuseSampler, Input.Texcoord);

	// Lighting
	float lighting = 1.0f;
	
	float4 param = tex2D(ParamSampler, Input.Texcoord);

	float3 fvLightDirection = normalize( Input.LightDirection );
	float3 fvNormal         = normalize( Input.Normal );

	float  fNDotL = dot( fvNormal, fvLightDirection );

	// Self illumination + ambient
	lighting = max(0.3f+param.b, fNDotL);
	
	color *= lighting;
	
	float4 output = color;
	
	// Env texture coordinates
	float2 envTC = float2(Input.envUV.xy)*0.5+0.5;
	
	// Env
	output = lerp(output, tex2D(EnvironmentSampler2D, envTC), param.r);
	
	// Opacity
	output.a = 1.0-param.g;
	

	return output;

}

technique Planet_2_0
{
    pass P0
    {
		Lighting = false;		
		ZWriteEnable = true;	
		
        VertexShader = compile vs_2_0 vs_planet();
        PixelShader  = compile ps_2_0 ps_planet();        
    }
}

technique Planet_1_1
{
	pass P0
	{        
		Lighting = false;	
		ZWriteEnable = true;	
		StencilEnable = false;
		VertexShader = compile vs_1_1 vs_lighting();
		PixelShader  = compile ps_1_1 ps_lighting();
	}
}

technique Planet_FFP
{
	pass P0
	{        
		Lighting = false;
		ZWriteEnable = true;
		StencilEnable = false;
		WorldTransform[0] = <matW>;
		ViewTransform = <matV>;
		ProjectionTransform = <matP>;
		Texture[0] = <texDiffuse>;
	}
}
