static const int NUM_LIGHTS = 2;

float4x4 gWorld;
float4x4 gWVP;
float3   gLightPosition[NUM_LIGHTS];

// object's ambient material color
float4 gAmbientColor;	// ambient material color
float4 gAmbientLight;	// ambient light color

float4 gDiffuseMtrl;	// the diffuse color of the material
float4 gDiffuseLight[NUM_LIGHTS];	// the color of the diffuse light

float gDiffuseLightMin[NUM_LIGHTS];	// the attenuation scalar of the lights
float gDiffuseLightMax[NUM_LIGHTS];	// the attenuation of the lights

float4 gDirectionalColor;
float3 gDirectionalLightDir;
texture		tex1;
bool		hasTexture;

// texture sampler
sampler		samp1
{
	Texture = <tex1>;
	// texture filtering
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	// texture address modes (available options are WRAP, MIRROR, BORDER, and CLAMP)
	AddressU  = WRAP;
	AddressV  = WRAP;
};
 
struct OutputVS
{
    float4 posH  : POSITION0;
    float2 texCoord0:TEXCOORD0;
    float4 color : COLOR0;
};

OutputVS DiffuseVS(float3 posLocal : POSITION0, float3 normalL : NORMAL0, float2 uv0:TEXCOORD0)
{
    OutputVS Out = (OutputVS)0;
    Out.posH = mul(float4(posLocal, 1.0f), gWVP); // transform Position
    
	float3 color={0,0,0};

    float3 posWorld = mul(float4(posLocal, 1.0f), gWorld);
    
	for(int i = 0; i < NUM_LIGHTS; ++i)
	{
		// get the direction of the light based on the world position
		// of the vertex
		float3 lightDirection = (gLightPosition[i] - posWorld.xyz);

		//att = (lightAttenuation.x) + (lightAttenuation.y * (1/d)) + (lightAttenuation.z * (1 / (d *d)));
		//att = saturate(1.0 – smoothstep(lightAttMinRange, lightMaxRange, lightDis));
		float4 lightDistance = distance(posWorld, gLightPosition[i]);
		float4 att = saturate(1.0f - smoothstep(gDiffuseLightMin[i], gDiffuseLightMax[i], lightDistance));
    
		// normalize the light direction vector
		lightDirection = normalize(lightDirection);
    
		float3 normalW = mul(float4(normalL, 0.0f), gWorld).xyz;
		normalW = normalize(normalW);

		// calculate how much light is hitting this vertex
		float lightAmount = saturate(dot(lightDirection, normalW));
	
		// calculate the diffuse color
		float4 diffuse = att * (lightAmount * (gDiffuseMtrl*gDiffuseLight[i]));
		
		color += saturate(diffuse);
	}

	
    // get the direction of the light based on the world position
    // of the vertex
    float3 lightDirection = gDirectionalLightDir;
    // normalize the light direction vector
	lightDirection = normalize(lightDirection);
    
    float3 normalW = mul(float4(normalL, 0.0f), gWorld).xyz;
	normalW = normalize(normalW);

    // calculate how much light is hitting this vertex
	float lightAmount = saturate(dot(lightDirection, normalW));
	
	// calculate the diffuse color
	float4 diffuse = lightAmount * gDirectionalColor;

	color += saturate(diffuse);
	

	Out.color =float4(gAmbientColor+color,gDiffuseMtrl.a);
	
	// set the alpha to 1.0
	//Out.color.a   = diffuse.a;
	
	// pass through the texture coordinates
	Out.texCoord0 = uv0;

    return Out;
}

float4 DiffusePS(float4 color : COLOR0, float2 uv0:TEXCOORD0) : COLOR
{
	float4 texCol = tex2D(samp1, uv0);
	
	if (hasTexture)
	{
		// return the (ambient+diffuse) multiplied by the texture color
		if(texCol.a <= 0.01 )
		{
		discard;
		
		}
		return  color * texCol;
	}
	else
	{
		
		return color;
	}
}

technique DiffuseTech
{
    pass P0
    {
	
		FillMode = Solid;
        CullMode = CCW;
		
	
        // Specify the vertex and pixel shader associated with this pass.
        vertexShader = compile vs_3_0 DiffuseVS();
        pixelShader  = compile ps_3_0 DiffusePS();
		
		
    }
}
