// force vc7 to group all shader related stuff into the Shader namespace
#ifdef WIN32
namespace Shader {
#endif

// define inputs from vertex program
struct in_fragment
{
  float4 color     : COLOR0;
  float4 texCoord0 : TEXCOORD0;
  float4 texCoord1 : TEXCOORD1;
  float4 texCoord2 : TEXCOORD2;
};

// define outputs from fragment program
struct out_fragment
{
	float4 color : COLOR0;
};

// fragment program
out_fragment main
(
  // varying parameters
	in_fragment f_in,

	// uniform samplers (textures)
  uniform sampler2D diffuseMap : TEXUNIT0,
  uniform sampler2D lightMap   : TEXUNIT1,
  uniform sampler2D reflMap   : TEXUNIT2,
  uniform sampler2D bumpMap : TEXUNIT3,
		
	// modulate value for the final color output
	uniform float modulate_value,
  uniform float uTime
)
{
	out_fragment f_out;

  f_out.color    = tex2D(diffuseMap, f_in.texCoord0.xy);
  float4 lmColor = tex2D(lightMap,   f_in.texCoord1.xy);

  modulate_value = 1.35;
  lmColor       *= modulate_value;
  f_out.color   *= lmColor;
  float2 final = f_in.texCoord2.xy / f_in.texCoord2.w;

  // apply distortion/ripple
  // first try: apply ripple to x only
  //f_in.texCoord0.y += uTime;
  float2 offset = tex2D(bumpMap, f_in.texCoord0.xy).rb;
  offset -= 0.5.xx;
  //offset *= 3.0;

  // offset in range -1,1
  offset *= 0.1;
  final.xy += offset;
  final.y += uTime;

  f_out.color += tex2D(reflMap, final) * 2.0*lmColor;
  //f_out.color = 0.0.xxxx;
  //f_out.color.a = 1.0;
  //f_out.color += tex2D(reflMap, final);
  //f_out.color += tex2D(reflMap, final)*0.25*lmColor;

	return f_out;
}

#ifdef WIN32
}
#endif

