#define GAMMA 1.0 / 2.2

float4x4 gWorldXf;
float4x4 gViewXf;
float4x4 gProjectionXf;
float4x4 gWorldITXf;
float4x4 gViewIXf;
float gExposure;

samplerCUBE gSamplerSpecular	: register( s0 );
samplerCUBE gSamplerDiffuse		: register( s1 );


/*
samplerCUBE gSampler = sampler_state
{
	texture = < gTexture >;
	magfilter = LINEAR;
	minfilter = LINEAR;
	mipfilter = LINEAR;
	AddressU = Wrap;
	AddressV = Wrap; 
	AddressW = Wrap;
};
*/

struct a2v
{
    float4 Position : POSITION0;
    float3 Normal	: NORMAL0;
};

struct v2f
{
    float4 Position : POSITION0;
	float3 Normal	: TEXCOORD0;
	float3 View		: TEXCOORD1;
	float3 WorldPos	: TEXCOORD2;
};

v2f VertexShaderFunction( 
	a2v IN,
	uniform float4x4 WorldXf,
	uniform float4x4 ViewXf,
	uniform float4x4 ProjectionXf,
	uniform float4x4 WorldITXf,
	uniform float4x4 ViewIXf )
{
    v2f OUT = ( v2f ) 0;

    float4 worldPosition = mul( IN.Position, WorldXf );
    float4 viewPosition = mul( worldPosition, ViewXf );
    OUT.Position = mul( viewPosition, ProjectionXf );
	OUT.Normal = mul( IN.Normal, WorldITXf ).xyz;
	OUT.View = ViewIXf[ 3 ].xyz - worldPosition.xyz;
	OUT.WorldPos = worldPosition;

    return OUT;
}

float4 PixelShaderFunction( 
	v2f IN,
	uniform float Exposure ) : COLOR0
{
	float3 N = normalize( IN.Normal );
	float3 V = normalize( IN.View );
	float3 R = reflect( -V, N );
	//float3 L = normalize( float3( 2, -5, 10 ) );
	float3 L = normalize( float3( 0, 0, 180 ) - IN.WorldPos );
	float Diff = max( 0.0,  dot( L, N ) );
	float4 Specular = texCUBE( gSamplerSpecular, R.xyz );
	float4 Diffuse = texCUBE( gSamplerDiffuse, N.xyz );
	
	Specular = pow( Specular, GAMMA );
	Diffuse = pow( Diffuse, GAMMA );
	
	float3 FinalColor = (Diffuse.rgb * 1.0
		+ Specular.rgb * 0.0) * Diff;
	
	return float4( FinalColor * Exposure, 1 );
}

technique Technique1
{
    pass Pass1
    {
        VertexShader = compile vs_3_0 VertexShaderFunction(
			gWorldXf,
			gViewXf,
			gProjectionXf,
			gWorldITXf,
			gViewIXf );
        PixelShader = compile ps_3_0 PixelShaderFunction( 
			gExposure );
    }
}
