//Cg
//Cg profile gp4vp gp4fp

struct VertexDataOUT{
  float4 o_position  :POSITION;
  float2 o_texcoord0 :TEXCOORD0;
  float3 o_normal    :TEXCOORD1;
  float3 o_objpos    :TEXCOORD2;
};

struct VertexDataIN{
  float4 vtx_position  :POSITION;
  float2 vtx_texcoord0 :TEXCOORD0;
  float3 vtx_normal    :NORMAL0;
  int l_id             :INSTANCEID;
};
//vertex shader
void vshader(VertexDataIN IN,
             out VertexDataOUT OUT,
             uniform float4 instances_position[2],
             uniform float4 Ly[1000],
             uniform float4x4 mat_modelproj)
{	
	float4 vpos = IN.vtx_position + Ly[IN.l_id]; 
	
  OUT.o_position  = mul(mat_modelproj,vpos);
  OUT.o_objpos    = IN.vtx_position;
	OUT.o_texcoord0 = IN.vtx_texcoord0;
	OUT.o_normal    = IN.vtx_normal;
	
}



//fragment shader
struct FragmentDataIN{
  uniform float4 mspos_light;
	uniform float Ka;
	uniform float Kd;
	uniform sampler2D tex_0;
};

void fshader(VertexDataOUT  vIN,
             FragmentDataIN fIN,
             out float4 o_color :COLOR)
{
  float3 P = vIN.o_objpos.xyz;
  float3 N = normalize(vIN.o_normal);

  // compute the ambient term
  float ambient  = fIN.Ka;
  
  // compute the diffuse term
  float3 L = normalize(fIN.mspos_light - P);
  float diffuseLight = max(dot(N,L),0);
  float3 diffuse = fIN.Kd *  diffuseLight;

	o_color = tex2D(fIN.tex_0, vIN.o_texcoord0);
	o_color.xyz = o_color.xyz * (ambient + diffuse);
}
