//-----------------------------------------------------------------------------
// File: Flyer.fx
//
// Desc: The effect file for the EffectMesh sample.  The technique implements:
//
//       Texture mapping
//       Diffuse lighting
//       Specular lighting
//       Environment mapping
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
texture tex0;  // texture for scene rendering
texture tex1; // texture for environment map
//texture tex2; // texture for environment map

float4x4 g_mWorldView : WORLDVIEW;               // View matrix for object
float4x4 g_mWorldViewProjection : WORLDVIEWPROJECTION; // World * View * Projection matrix
float4x4 g_vWorld : WORLD; // World * View * Projection matrix
//float4x4 g_mProj;                                       // Projection matrix for object
shared float4x4 g_mViewInv;                             // Inverse of view matrix
shared float4 g_vLightColor = {1.0f, 1.0f, 1.0f, 1.0f}; // Light value
shared float3 g_vLight;                                 // Light position in view space
shared float3 _g_vLight= {-1.0f, -1.0f, -1.0f}; // Light value
shared float3 g_vCameraPos;                                 // Light position in view space
shared float4 g_vFogColor = {1.0f, 0.0f, 1.0f, 1.0f};                                 // Light position in view space
shared float g_fFogMinDistance = 20;                                 // Light position in view space
shared float g_fFogMaxDistance = 100;                                 // Light position in view space
shared float4 g_vAmbient = {0.1f, 0.1f, 0.1f, 1.0f};;      // Ambient color
float  g_fSizeMul = 1.0f;                               // A size multiplier
float  g_fAnimSpeed;                                    // Animation speed

// Object material attributes
float4 Diffuse = {1.0f, 1.0f, 1.0f, 1.0f};      // Diffuse color of the material
float4 Ambient = {0.7f, 0.7f, 0.7f, 1.0f};      // Ambient color of the material
float4 Specular = {0.95f, 0.95f, 0.95f, 1.0f};  // Specular color of the material
float  Reflectivity = 0.8f; // Reflectivity of the material
float  Power = 15.0f;


//-----------------------------------------------------------------------------
// Texture samplers
//-----------------------------------------------------------------------------
sampler g_samScene =
sampler_state
{
    Texture = <tex0>;
    MinFilter = Linear;
    MagFilter = Linear;
    MipFilter = None;
};

sampler g_samEnv =
sampler_state
{
    Texture = <tex1>;
    MipFilter = ANISOTROPIC;
    MinFilter = ANISOTROPIC;
    MagFilter = ANISOTROPIC;
};

/*
sampler g_samEnv2 =
sampler_state
{
    Texture = <tex2>;
    MipFilter = Linear;
    MinFilter = Linear;
    MagFilter = Linear;
};
*/

//-----------------------------------------------------------------------------
// Name: VertScene
// Type: Vertex shader
// Desc: This shader computes standard transform and lighting
//-----------------------------------------------------------------------------
void VertScene( float4 vPos : POSITION,
                float3 vNormal : NORMAL,
                float2 vTex0 : TEXCOORD0,
                float4 vDiffuse : COLOR0,
                out float4 oPos : POSITION,
                out float4 oDiffuse : COLOR0,
                out float2 oTex0 : TEXCOORD0,
                out float3 oViewPos : TEXCOORD1,
                out float3 oViewNormal : TEXCOORD2,
		out float oFog : FOG)
{
	oPos = mul( vPos, g_mWorldViewProjection );
	oViewPos = mul( vPos, g_mWorldView );
	oViewNormal = normalize( mul( vNormal, (float3x3)g_mWorldViewProjection ) );
	vNormal = normalize( mul( vNormal, (float3x3)g_vWorld ) );
	float3 oRealPos  =  mul( vPos, g_vWorld ) ;
	oDiffuse = dot(vNormal, -_g_vLight);
	oTex0 = vTex0;
	float dist = distance(g_vCameraPos, oRealPos);
	float _fog = (g_fFogMaxDistance - dist) / (g_fFogMaxDistance - g_fFogMinDistance);
	oFog = _fog;
}


//-----------------------------------------------------------------------------
// Name: PixScene
// Type: Pixel shader
// Desc: This shader outputs the pixel's color by modulating the texture's
//		 color with diffuse material color
//-----------------------------------------------------------------------------
float4 PixScene( float4 MatDiffuse : COLOR0,
                 float2 Tex0 : TEXCOORD0,
                 float3 ViewPos : TEXCOORD1,
                 float3 ViewNormal : TEXCOORD2) : COLOR0
{
//	float3 vHalf = normalize( normalize( -ViewPos ) - normalize( _g_vLight - ViewPos ) );
//	float3  vNormal = mul( ViewNormal, (float3x3)g_mViewInv );

//	float4 fSpecular =  Specular * pow( saturate( dot( vHalf, normalize( vNormal ) ) ), Power );

    float3 vHalf = normalize( normalize( -ViewPos ) + normalize( _g_vLight - ViewPos ) );
//   float3 vHalf = normalize( normalize( g_vCameraPos - ViewPos ) + normalize( _g_vLight - ViewPos ) );

    // Compute normal dot half for specular light
    float4 fSpecular = pow( saturate( dot( vHalf, normalize( ViewNormal ) ) ) * Specular, Power );


//    float3 R = normalize(reflect(_g_vLight, ViewNormal));
//    float4 fSpecular = pow( saturate( dot( R, normalize( -ViewNormal ) ) ) * Specular, Power );
 //   return MaterialSpecularColor * LightSpecularColor * pow(max(0, dot(R, -EyeToVertex)), MaterialSpecularPower/4);
	

	float tex_alpha = tex2D( g_samScene, Tex0 )[3];
	
    float3 R = normalize(reflect(ViewPos, ViewNormal));
    float2 _Tex;
    _Tex[0] = R[0];
    _Tex[1] = R[1];

    float4 ret_color =  float4( (float3)( g_vLightColor * ( tex2D( g_samScene, Tex0 ) * (MatDiffuse * Diffuse  + g_vAmbient * Ambient) + fSpecular * tex_alpha * MatDiffuse)
			+ tex2D( g_samEnv, _Tex ) * Reflectivity * tex_alpha * (MatDiffuse + Ambient / 2.0f)
			), 1.0f );

    return ret_color;
}

technique tec0
{
    pass P0
    {
        VertexShader = compile vs_2_0 VertScene();
        PixelShader  = compile ps_2_0 PixScene();
        ZEnable = true;
	ZWRITEENABLE = true;
        AlphaBlendEnable = false;
	FogEnable = false;
    }
}

