
struct appdata
{
    float4 position : POSITION;
    float3 normal : NORMAL;
    //float3 color : DIFFUSE;
    float4 diffuse : DIFFUSE;
    float2 uvs :TEXCOORD0;
    float3 tangent :TEXCOORD1;
    //
	//float3 TestColor : SPECULAR;
};





struct vfconn
{

    float4 HPOS : POSITION;
    //oat4 Nrm : NORMAL;
	float4 HCOL : COLOR;
	float4 HCOL2 : COLOR1;
    float4 oPosition : TEXCOORD1;
    float2 oUV : TEXCOORD0;
                      float3 oNormal : TEXCOORD3;

float3 oHalf : TEXCOORD4;
                                                float3 oLight : TEXCOORD2;

float3 oEye : TEXCOORD5;
  
};








vfconn main(appdata IN,
                uniform float3 LightPos,
                uniform float3 CameraPos,
          uniform float3 Reduction,

uniform float4x4 ModelViewLightProj,
uniform float4x4 ModelViewObject,
			uniform float4x4 ModelViewProj)
{
    vfconn OUT;

    
//
	//ModelViewProj=mul(ModelViewProj,ModelViewObject);
    //ModelViewLightProj=mul(ModelViewLightProj,ModelViewObject);
    //ModelViewLightProj=mul(ModelViewObject,ModelViewLightProj);
    
   

    IN.position=mul(ModelViewObject,IN.position);
//IN.position.y*=1.3;
IN.position.xy*=1.4;
float wave=max (0,min (1,IN.position.y*0.07-Reduction.y));
wave=sin(3.14159265352*wave);
wave=wave*wave;

//IN.position.z+=wave*2.2;

float dist=length(IN.position)*1.6;
float dist2=length(IN.position.xz)*1.6;
float dist3=length(IN.position.yz)*1.6;

IN.position.z+=0.4*sin(1*Reduction.x*2+0.4*dist);
IN.position.z+=0.4*sin(1.3*Reduction.x*2+0.32*dist2);
IN.position.z+=0.4*sin(1.3*Reduction.x*1.3+0.42*dist3);

    IN.normal=mul((float3x3   )ModelViewObject,IN.normal);
IN.tangent=mul((float3x3   )ModelViewObject,IN.tangent);



float3 binormal = cross(IN.normal,IN.tangent);


	float4 eyePosition;
eyePosition=CameraPos.xyzz;
//normalize (eyePosition);
eyePosition.w=1;

		float3 eyeVec = normalize(eyePosition -     IN.position);
		//float3 eyeVec = (eyePosition -     IN.position);


		float3 tanEyeVec;

float3 ntangent = float3(1.0, 0.0, 0.0);
float3 nbinormal = cross(IN.normal, ntangent);

        tanEyeVec.x = dot(ntangent, eyeVec);
		tanEyeVec.y = dot(nbinormal, eyeVec);
		tanEyeVec.z = dot(IN.normal, eyeVec);

//		OUT.oEye = 0.5+eyeVec/2;
		OUT.oEye = eyeVec;
		//OUT.oEye = tanEyeVec;


//OUT.oEye=float3 (1,0.5,0);


   
    OUT.HPOS = mul(ModelViewProj, IN.position);
    OUT.oUV=IN.uvs;


    OUT.oPosition=mul (ModelViewLightProj,IN.position);
//OUT.oPosition.xyz*=10.03;
//OUT.oPosition=IN.position;

    //IN.position=mul(IN.position, ModelViewObject);
    //IN.normal=mul(IN.normal,(float3x3   )ModelViewObject);
//IN.tangent=mul(IN.tangent,(float3x3   )ModelViewObject);
//IN.position.w=0;


float3 LightP=LightPos;

float3 L=normalize (LightP-IN.position);
float3 L2=normalize (float3 (0,10,100)-IN.position);
float3 V=normalize (CameraPos-IN.position);
float3 V2=normalize (float3(0,10,100)-IN.position);
float3 H=normalize (L2+V2);







float4 AllWhite=float4 (0.2,0.7,1,0);

float specularL=pow (max(dot (IN.normal,H),0),14);
float diff=max (dot (IN.normal,L),0);
diff*=130;
diff+=0.3;
// spherical mapping
float3 ref=reflect (V,IN.normal);
ref.z+=5
;
float m=0.1*length(ref);
m*=m*3;
m*=m;
m*=m*0.5;

m=max (dot (IN.normal,V),0)*200;

//m*=m*0.6;
m+=0.3;
//float m=2*sqrt(ref.x*ref.x)l

//m = 2(fx^2 + fy^2 + (fz + 1)^2)^(1/2). 

 //OUT.oUV=float2 (m,m);
 //ref.x/m+0.5,ref.y/m+0.5);
 //reflect (V,IN.normal);



OUT.oLight=(-IN.position+LightP*1);
OUT.oNormal=IN.normal;
//OUT.oLight=H;

//normalize(IN.position-LightP);

//OUT.oNormal=
//OUT.oPos=IN.position;

//diff=pow(diff,1);



float3x3 rotation = float3x3(IN.tangent,                               binormal,                               IN.normal);
 rotation = float3x3(IN.tangent,
     binormal,
     IN.normal);
 
//float ff=length(LightP);
float mult=1/(1+0.001*length(CameraPos-IN.position)*length(CameraPos-IN.position));
//mult=IN.position.y;
mult*=mult;mult*=mult;mult*=mult;
OUT.oLight = mul(rotation, OUT.oLight);
OUT.oLight=normalize(OUT.oLight);

//OUT.oLight=float3(0,0,-1);

//OUT.oLight=float3 (1,0,0);
//OUT.oNormal=(UpV*0.5)+0.5;
OUT.oHalf=mul(rotation,H);
OUT.oHalf=normalize(OUT.oHalf);
OUT.oHalf=CameraPos;

//distt=1;
//OUT.oPosition+=OUT.oHalf*0.1;
//distt=0;
OUT.HCOL2=1.7-3*(IN.diffuse*mult*0.29+IN.diffuse*0.9*mult*diff);
//OUT.HCOL2=1/(1+0.002*length(CameraPos-IN.position)*length(CameraPos-IN.position));
OUT.HCOL=1-20*AllWhite*specularL;

OUT.HCOL2=1*(IN.diffuse*mult*0.29+IN.diffuse*0.9*mult*diff);
OUT.HCOL=1*AllWhite*specularL;
OUT.HCOL2=float4(m,m,m,m);
m=diff;
OUT.HCOL2=float4(m,m,m,m)*mult;
OUT.HCOL=float4 (mult,mult,mult,mult);
OUT.HCOL2=mult;


//OUT.HCOL=float4 (1,1,1,1);

//if (LightP.x<111110) OUT.HCOL.x=1;

//float g=IN.color.x;
/*
OUT.HCOL.x=0.1+diff*0.7;
OUT.HCOL.y=0.1+diff*0.9;
OUT.HCOL.z=0.1+diff*1.9;
*/

    return OUT;
} // main




























/*


  VERTEX ENV MAP



  
struct appdata
{
    float4 position : POSITION;
    float3 normal : NORMAL;
    float3 color : DIFFUSE;
    float3 TestColor : SPECULAR;
};

struct vfconn
{
    float4 HPOS : POSITION;
    float2 oPosition : TEXCOORD0;
    float2 oNormal : TEXCOORD1;
};

vfconn main(appdata IN,
            uniform float3 Kd,
            uniform float4x4 ModelViewProj)
{
    vfconn OUT;

    

	OUT.HPOS = mul(ModelViewProj, IN.position);
float3 normal=mul((float3x3)ModelViewProj,IN.normal);
normalize(normal);


float4 normpos=normalize(OUT.HPOS);
float3 refl=reflect (normal,normpos);


float magn=1.5*sqrt (refl.x*refl.x+refl.y*refl.y+(refl.z+1)*(refl.z+1));






float4 normpos2=normalize(IN.position);
float3 refl2=reflect (IN.normal,normpos);


float magn2=1.5*sqrt (refl2.x*refl2.x+refl2.y*refl2.y+(refl2.z+1)*(refl2.z+1));




//diss/=length(diss);


//float cl=dot (normal,diss-OUT.HPOS);
OUT.oNormal.x=refl.x/magn+0.5;
OUT.oNormal.y=refl.y/magn+0.5;


//normalize(diss);

OUT.oPosition.x=IN.position.x*2;
OUT.oPosition.y=IN.position.y*2;

//OUT.oNormal.x=normal.x*0.3;
//OUT.oNormal.y=normal.y*0.3;

    return OUT;
} // main

 */
