//--------------------------------------------------------------------------------------
// File: KuwaLighting.fx
//
// The effect file for the Fetion 3DShow
// 
// Copyright (c) Fetion. All rights reserved.
//
// Create by shenyizhong@gmail.com
//
// Last modify 2010.06.19
//--------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
float4 g_MaterialAmbientColor;      // Material's ambient color
float4 g_MaterialDiffuseColor;      // Material's diffuse color
int g_nNumLights;

float3 g_LightDir[3];               // Light's direction in world space
float4 g_LightDiffuse[3];           // Light's diffuse color
float4 g_LightAmbient;              // Light's ambient color

texture g_MeshTexture;              // Color texture for mesh

float    g_fTime;                   // App's time in seconds
float4x4 g_mWorld;                  // World matrix for object
float4x4 g_mWorldViewProjection;    // World * View * Projection matrix

//--------------------------------------------------------------------------------------
// Texture samplers
//--------------------------------------------------------------------------------------
sampler MeshTextureSampler = 
sampler_state
{
    Texture = <g_MeshTexture>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};


//--------------------------------------------------------------------------------------
// Vertex shader output structure
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Position   : POSITION;
    float4 Diffuse    : COLOR0;
    float2 TextureUV  : TEXCOORD0;
};

//--------------------------------------------------------------------------------------
// This shader computes standard transform and lighting
//--------------------------------------------------------------------------------------
VS_OUTPUT RenderSceneVS( float4 vPos : POSITION, 
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD0,
                         uniform int nNumLights)
{
    VS_OUTPUT Output;
    float3 vNormalWorldSpace;

    Output.Position = mul(vPos, g_mWorldViewProjection);
    
    vNormalWorldSpace = normalize(mul(vNormal, (float3x3)g_mWorld));

    float3 vTotalLightDiffuse = float3(0.2,0.2,0.2); //We keep a little self-diffuse here
    for(int i = 0; i < nNumLights; i++ )
        vTotalLightDiffuse += g_LightDiffuse[i] * max(0,dot(vNormalWorldSpace, g_LightDir[i]));//取灯光和法向的点积

    Output.Diffuse.rgb = g_MaterialDiffuseColor * vTotalLightDiffuse + g_MaterialAmbientColor * g_LightAmbient;
    Output.Diffuse.a = 1.0f;
    Output.TextureUV = vTexCoord0;
    return Output;
}

struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;
};

PS_OUTPUT RenderScenePS( VS_OUTPUT In) 
{ 
    PS_OUTPUT Output;
    Output.RGBColor = tex2D(MeshTextureSampler, In.TextureUV) * In.Diffuse;
    return Output;
}

technique RenderSceneWithTexture1Light
{
    pass P0
    {          
        VertexShader = compile vs_1_1 RenderSceneVS(1);
        PixelShader  = compile ps_1_1 RenderScenePS();
    }
}

technique RenderSceneWithTexture2Light
{
    pass P0
    {          
        VertexShader = compile vs_1_1 RenderSceneVS(2);
        PixelShader  = compile ps_1_1 RenderScenePS();
    }
}

technique RenderSceneWithTexture3Light
{
    pass P0
    {          
        VertexShader = compile vs_1_1 RenderSceneVS(3);
        PixelShader  = compile ps_1_1 RenderScenePS();
    }
}

technique RenderSceneNoTexture
{
    pass P0
    {          
        VertexShader = compile vs_1_1 RenderSceneVS(1);
        PixelShader  = compile ps_1_1 RenderScenePS();
    }
}