// ReflectionMap / SphereMap Shader
//
// Copyright Outerspace Software, all rights reserved

string description = "A reflectionmapping (spheremap) effect. It uses the first texture as a reflectionmap (spheremap). The second texture is not used.";

float4x4	World;
float4x4	ViewProjection;
float4x4	WorldInverseTranspose;
float4		EyePos;
float4		DiffuseCol;

struct VS_INPUT{
	float4 position:	POSITION;
	float3 normal:		NORMAL;
	float3 diffuse:		COLOR;
	float2 tex:			TEXCOORD0;
};

struct VS_OUTPUT{
	float4 position:	POSITION;
	float4 diffuse:		COLOR;
	float2 tex:			TEXCOORD0;
};

VS_OUTPUT VS(VS_INPUT input){
	VS_OUTPUT output;
	float4 Pos=mul(input.position, World);
	output.position = mul(Pos, ViewProjection);

	float3 NormalVector		= normalize(mul(input.normal,WorldInverseTranspose));
	float3 EyeVector		= normalize(EyePos - Pos);

	float3 ReflectionVector = reflect(EyeVector,NormalVector);
	ReflectionVector.z=abs(ReflectionVector.z);

	ReflectionVector.z+=1;
	float f=2*length(ReflectionVector);
	output.tex = float2(0.5,0.5) + ReflectionVector.xy/f;

	output.diffuse = DiffuseCol;
	return output;
}

technique Shader
{
    pass P0
    {
        VertexShader = compile vs_1_1 VS();
        PixelShader  = NULL;
    }
}

technique AlphaChannel
{
    pass P0
    {
        VertexShader = NULL;
        PixelShader  = NULL;
    }
}
