//Vertex Shader
VS_OUTPUT vs_block(VS_INPUT IN)
{
	VS_OUTPUT OUT = (VS_OUTPUT)0;

    //getting the position of the vertex in the world
	float p = (IN.position.y + 128.0f) / 256.0f;
    
    float4x4 world = matW * p + matLastBlock * (1.0f - p);
    
    float4 posWorld2 = mul(IN.position, world);
    float4 posWorld = mul(IN.position, matW);
	posWorld.y = p * posWorld.y + (1.0f - p) * (posWorld2.y);
      	
	float4 normal = normalize(mul(IN.normal, matW));
	
	//getting to position to object space
	float4 pos = mul(posWorld, matVP);
	OUT.position = pos;
	 
	// Shade
    OUT.params.x = max(dot(normal, normalize(lightDir)) * 1.4f, ambientLight);
	

    //Reflection coordinate
    OUT.envUV = reflect(-normalize(camPos - posWorld), normal);

    OUT.tex0 = IN.tex0;
    OUT.tex1 = IN.tex0;
    
    return OUT;
}
VS_LIGHTING_OUTPUT vs_block_2_0( VS_INPUT Input )
{
   VS_LIGHTING_OUTPUT Output = (VS_LIGHTING_OUTPUT)0;

     //getting the position of the vertex in the world
	float p = (Input.position.y + 128.0f) / 256.0f;
    
    float4x4 world = matW * p + matLastBlock * (1.0f - p);
    
    float4 posWorld2 = mul(Input.position, world);
    float4 posWorld = mul(Input.position, matW);
	posWorld.y = p * posWorld.y + (1.0f - p) * (posWorld2.y);
    float4 normal = normalize(mul(Input.normal, matW));
	Output.Normal  = normal;
	
	//getting to position to object space
	float4 pos = mul(posWorld, matVP);
	
   Output.Position         = pos;
   
   
    float3 fvLightPosition 		   = float3(1000000, 1000000, 1000000);//cameraPos.xyz;
   
   Output.Texcoord         = Input.tex0;
   
   float3 fvObjectPosition = mul( Input.position, matV );
   
   Output.ViewDirection    = cameraPos - fvObjectPosition;
   Output.LightDirection   = fvLightPosition - fvObjectPosition;

	//Reflection coordinate
    Output.envUV = reflect(-normalize(camPos - posWorld), normal);	
	
   return( Output );
}



//Lighting Technique
technique Block_2_0
{
	pass P0
	{        
		Lighting = false;
		ZWriteEnable = true;
		//AlphaBlendEnable = false;
		VertexShader = compile vs_2_0 vs_block_2_0();
		PixelShader  = compile ps_2_0 ps_lighting_2_0();
	}
	pass P1
	{        
		Lighting = false;
		ZWriteEnable = true;
		AlphaBlendEnable = true;
		SrcBlend = SrcAlpha;
		DestBlend = InvSrcAlpha;		
		VertexShader = compile vs_2_0 vs_block_2_0();
		PixelShader  = compile ps_2_0 ps_lighting_2_0();
	}	
}

technique Block_1_1
{
	pass P0
	{        
		Lighting = false;	
		ZWriteEnable = true;	
		AlphaBlendEnable = false;
		VertexShader = compile vs_1_1 vs_block();
		PixelShader  = compile ps_1_1 ps_lighting();
	}
	pass P1
	{        
		Lighting = false;
		ZWriteEnable = true;
		AlphaBlendEnable = true;
		SrcBlend = SrcAlpha;
		DestBlend = InvSrcAlpha;		
		VertexShader = compile vs_1_1 vs_block();
		PixelShader  = compile ps_1_1 ps_lighting();	
	}	
}

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