/* Start File Header -------------------------------------------------------
Copyright (C) 2010 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent of DigiPen Institute of Technology is prohibited.
File Name: animUV.fx
Purpose: The shader for drawing 3D models that have an animated UV coordinate.
Authors: Robert Francis - 100%
- End File Header --------------------------------------------------------*/

texture texture0;
texture normalMap;
float4x4 worldViewProj : WorldViewProjection;
float4x4 world : World;
float uvTime;
float4 ColorOnTop;
float noLighting = 0.0f;

sampler NormalSamp
{
    Texture   = normalMap;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
    AddressU = WRAP;
    AddressV = WRAP;
    AddressW = WRAP;            
};

sampler Sampler
{
    Texture   = texture0;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
    AddressU = WRAP;
    AddressV = WRAP;
    AddressW = WRAP;            
};

struct VS_INPUT
{
	// stream 0
	float3 position	: POSITION;
	float3 normal : NORMAL;
	float2 tex0 : TEXCOORD0;
	float3 tangent : TANGENT;
	float3 binormal : BINORMAL;
	
	// stream 1
	// World Matrix
	float4 matrix0 : TEXCOORD1;
	float4 matrix1 : TEXCOORD2;
    float4 matrix2 : TEXCOORD3;
    float4 matrix3 : TEXCOORD4;
    
    float4 color : TEXCOORD5;
};

struct VS_OUTPUT
{
    float4 pos : POSITION;
	float2 tex0  : TEXCOORD0;
    float3 normal : TEXCOORD1;
    float3 tangent : TEXCOORD2;
	float3 binormal : TEXCOORD3;
	float4 oldPos : TEXCOORD4;
	float3 oldNorm : TEXCOORD5;
	float4 color : TEXCOORD6;
};

VS_OUTPUT SimpleVertex( VS_INPUT IN )
{
    VS_OUTPUT OUT;
    float4x4 modelMatrix = {IN.matrix0, IN.matrix1, IN.matrix2, IN.matrix3};
    OUT.pos = mul( float4(IN.position, 1) , modelMatrix);
    OUT.oldPos = OUT.pos;
	OUT.pos = mul( OUT.pos , worldViewProj);
	
	float4 temp = mul( float4(IN.normal, 0) , modelMatrix ); 
	OUT.oldNorm = temp.xyz;
	temp = mul( temp, world );
	OUT.normal = float3(temp.x, temp.y, temp.z);
	
	temp = mul( float4(IN.tangent, 0) , modelMatrix );
	temp = mul( temp , world );
	OUT.tangent = float3(temp.x, temp.y, temp.z);
	
	temp = mul( float4(IN.binormal, 0) , modelMatrix );
	temp = mul( temp , world );
	OUT.binormal = float3(temp.x, temp.y, temp.z);
	
	OUT.tex0 = IN.tex0;
	OUT.tex0.y += uvTime;
	OUT.color = IN.color;
	return OUT;
}

struct PS_OUT
{
  float4 color0: COLOR0;
  float4 color1: COLOR1;
};

PS_OUT SimplePixel( VS_OUTPUT IN ) : COLOR
{
	float4 texColor = tex2D( Sampler , IN.tex0 );
	if(texColor.w < 0.1f)
	  clip(-1);
	texColor *= IN.color;
	texColor.xyz = ColorOnTop.xyz * ColorOnTop.w + texColor.xyz * (1 - ColorOnTop.w);
	PS_OUT OUT;
	OUT.color0 = texColor;
	OUT.color1 = float4(noLighting, 0, 0, 255);
	return OUT;
}


technique Technique0
{
    pass Pass0
    {
        VertexShader = compile vs_2_0 SimpleVertex();
        PixelShader  = compile ps_2_0 SimplePixel();
        ZEnable = true;
        AlphaBlendEnable = true;
        SrcBlend = SrcAlpha;
        DestBlend = InvSrcAlpha;
        SrcBlendAlpha = ONE;
        DestBlendAlpha = ONE;
    }
}

technique TechniqueVine
{
    pass Pass0
    {
        VertexShader = compile vs_2_0 SimpleVertex();
        PixelShader  = compile ps_2_0 SimplePixel();
        ZEnable = true;
        AlphaBlendEnable = true;
        SrcBlend = SrcAlpha;
        DestBlend = InvSrcAlpha;
        SrcBlendAlpha = ONE;
        DestBlendAlpha = ONE;
    }
}
