/*********************************************************************NVMH3****

Sky
******************************************************************************/
#include "..\mrt.fx"

float4x4 viewProj : ViewProjection;
float4x4 world : World;
float3 campos : CAMERAPOS;

float FinalColorModulate : FinalColorModulate;

float TaggedBloom : TaggedBloom;

float4 ColorModulate : ColorModulate;
float Opacity : Opacity;

float2 NormalScale : NormalScale;
float2 NormalTranslate : NormalTranslate;
float  Distortion : Distortion;
float2 NormalModulate : NormalModulate;

texture BumpTexture : BumpTexture;


texture MyDiffuseTexture : MyDiffuseTexture;
sampler DiffuseTextureSampler = sampler_state 
{
    texture = <MyDiffuseTexture>;
    AddressU  = WRAP;        
    AddressV  = WRAP;
    AddressW  = WRAP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};


sampler BumpTextureSampler = sampler_state 
{
    texture = <BumpTexture>;
    AddressU  = WRAP;        
    AddressV  = WRAP;
    AddressW  = WRAP;
    MIPFILTER = LINEAR;
    MINFILTER = LINEAR;
    MAGFILTER = LINEAR;
};

struct vertexInput{
    float4 position				: POSITION;
    float2 texCoordDiffuse		: TEXCOORD0;
    float2 texCoordDiffuse2		: TEXCOORD1;
};

struct vertexOutput {
    float4 hPosition		: POSITION;
    float2 texCoordDiffuse	: TEXCOORD0;
    float2 texCoordDiffuse2	: TEXCOORD1;
    float3 posWorld         : TEXCOORD2;
};

vertexOutput VS_Transform(vertexInput IN) 
{
    float4 pos;
    vertexOutput OUT;
    
    pos = mul(IN.position, world);    
    OUT.posWorld = pos;
    OUT.hPosition = mul( pos , viewProj);       
     
    OUT.texCoordDiffuse = IN.texCoordDiffuse;   
    OUT.texCoordDiffuse2 = IN.texCoordDiffuse2;   
    return OUT;
}


//-----------------------------------
PS_OUTPUT PS_TexturedBumpmap( vertexOutput IN)
{    
    float3x3 tanmat;
    
    float4 tanWorld =  mul(float3( 1,1, 0), world);   
	tanmat[0] = normalize(tanWorld.xyz);
	tanmat[2] = normalize(float3( 0, 1, 0));
	tanmat[1] = normalize(cross(tanmat[2], tanmat[0])) * 1;

    float4 outColor;
    
    //float4 normalTexture = tex2D( BumpTextureSampler, IN.texCoordDiffuse + NormalTranslate ) * 2 - 1;
    //normalTexture.xy *= NormalScale;
    
    float4 normalTexture = tex2D( BumpTextureSampler, (IN.texCoordDiffuse + NormalTranslate) * NormalScale ) * 2 - 1;
    normalTexture.xy *= NormalModulate;
    
    float3 normalWorld = mul(normalTexture.xyz, tanmat);
    normalWorld = normalize(normalWorld);

	float2 uvs = IN.texCoordDiffuse + (normalTexture.xy) * 0.02 * Distortion;
           
    outColor = tex2D( DiffuseTextureSampler, uvs );

	outColor.xyz *= ColorModulate * FinalColorModulate;
	outColor.a *= Opacity;
	
	//outColor.xyz = normalTexture.xyz;
		
	PS_OUTPUT Out;	
	SetPSOutput( Out, outColor, float4( campos - IN.posWorld.xyz, TaggedBloom * MRT_FORCE_BLOOM ) );
	return Out;
}


//-----------------------------------
PS_OUTPUT PS_Textured( vertexOutput IN)
{    
    float4 outColor;
    
    outColor = tex2D( DiffuseTextureSampler, IN.texCoordDiffuse );

	outColor.xyz *= ColorModulate * FinalColorModulate;
	outColor.a *= Opacity;
	
	PS_OUTPUT Out;	
	SetPSOutput( Out, outColor, float4( campos - IN.posWorld.xyz, TaggedBloom * MRT_FORCE_BLOOM ) );
	return Out;
}



//-----------------------------------
technique texturedBumpmap
{
    pass p0 
    {	
        VertexShader = compile vs_2_0 VS_Transform();
		PixelShader  = compile ps_2_0 PS_TexturedBumpmap();
    }
}

technique textured
{
    pass p0 
    {	
        VertexShader = compile vs_2_0 VS_Transform();
		PixelShader  = compile ps_2_0 PS_Textured();
    }
}
