// vertex programm to set up per-pixel parameters for phong shading

// constants
float4x4 transform; // c0  View+Projection matrix
float4   lightpos;  // c4  obj-space light position, 1/(radius^2)
float4   lightcol;  // c5  light color
float4   speccol;   // c6  specular color
float4   campos;    // c7  obj-space camera position

struct Input
{
	float3 pos    : POSITION;
	float2 uv     : TEXCOORD0;
	float3 normal : NORMAL;
};

struct Output
{
	float4 pos      : POSITION;
	float2 uv       : TEXCOORD0;
	float3 normal   : TEXCOORD1;
	float3 light    : TEXCOORD2;
	float3 view     : TEXCOORD3;
	float4 diffuse  : COLOR0;
	float4 specular : COLOR1;
};

Output main( Input src )
{
    Output dst;
    
    float4 pos= float4(src.pos, 1.0f);
	float4 light= lightpos - pos;
	float4 view= campos - pos;
    
	// transform vertex position to view-space
	dst.pos= mul(pos, transform);

	// texture interpolators
	dst.uv= src.uv;         // uv for diffuse texture
	dst.normal= src.normal; // normal vector
	dst.light= light;
	dst.view= view;

	// distance attenuation
	float distance= dot(light, light);
	float intensity= saturate( 1.0f - (distance * lightpos.w) );
	
	// self shadowing term
	intensity*= saturate( dot(normalize(light), src.normal)*4-0.25f );

	dst.diffuse= lightcol * intensity;
	dst.specular= speccol * intensity;

    return dst;    
}
