/* 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: OmniLight.fx
Purpose: The shader for handling the light calculations for omni/point lights.
Authors: Robert Francis - 100%
- End File Header --------------------------------------------------------*/

#include "LightInclude.fx"

float4 OmniLightPixel( PS_INPUT IN) : COLOR
{
	float4 G_buffer = tex2D(DepthSamp, IN.tex0);
	
	//compute pixel position
	float depth = G_buffer.z;
	float3 PixelPos = normalize(float3(IN.Pos, 1)) * depth;
	
	// compute normal                                                        
	float3 N;
	N.xy = (G_buffer.xy*2.f-1.f);
	N.z = -sqrt(1 - dot(N.xy, N.xy));
	
	// compute light direction
	float3 L = (lightPos.xyz - PixelPos);
	float dist = length(L);
	L = normalize(L);
	
	float atten = clamp((maxAtten - dist) / (maxAtten - minAtten), 0.0, 1.0);
	
	float NL = max(dot(L, N), 0.0);  //multiply with distance attenuation

	//shadowmap
  float shadCoef = GetShadowCoef(float4(PixelPos, 1), N);
	
	float4 result = Diffuse * NL * atten * shadCoef;
	
	//Specular
	//will always be 0 or 1, gets rid of specular from lights behind normals
	float SpecCoef = min(NL / (max(dot(L,N), 0.00001)), 1.0); 
	float3 R = 2.0 * N * dot(N, L) - L;
	R = normalize(R);
	float3 V = -normalize(PixelPos);
	result.w = SpecCoef * max(dot(R, V), 0.0) * atten;
	return result;
}

technique Technique0
{
    pass Pass0
    {
        VertexShader = compile vs_2_0 Vertex();
        PixelShader  = compile ps_3_0 OmniLightPixel();
        ZEnable = true;
        AlphaBlendEnable = true;
        SRCBLEND = ONE;
        DESTBLEND = ONE;
    }
}