#include "common.h"

//#define USE_SUPER_SPECULAR

#include "lmodel.h"
#include "hmodel.h"

#include "ssao.ps"
struct	_input
{
	float4	tc0	: TEXCOORD0;	// tc.xy, tc.w = tonemap scale
	float2	tcJ	: TEXCOORD1;	// jitter coords
	float4	ttt : SV_Position;
};

struct	_out
{
	float4	low		: SV_Target0;
	float4	high	: SV_Target1;
};

//	TODO:	DX10: Replace Sample with Load
_out main ( _input I )
{
	// Sample the buffers:
	float4	P = s_position.Sample( smp_nofilter, I.tc0 );	// position.(mtl or sun)
	float4	N = s_normal.Sample( smp_nofilter, I.tc0);		// normal.hemi
	float4	D = s_diffuse.Sample( smp_nofilter, I.tc0);		// rgb.gloss
	float4	L = s_accumulator.Sample( smp_nofilter, I.tc0);	// diffuse.specular

#ifdef         USE_SUPER_SPECULAR
	{
		float ds = dot( D.rgb, 1.h/3.h );
		D.w = max( D.w, ds*ds/8.h );
	}
#endif

#ifdef  FORCE_GLOSS
	D.w = FORCE_GLOSS;
#endif

#ifdef         USE_GAMMA_22
	D.rgb = ( D.rgb*D.rgb ); // pow(2.2)
#endif

        // static sun
	float mtl = P.w;

#ifdef        USE_R2_STATIC_SUN
	float sun_occ = P.w*2;

	mtl = xmaterial;
	L += Ldynamic_color * sun_occ * plight_infinity	(mtl, P.xyz, N.xyz, Ldynamic_dir);
#endif

	// hemisphere
	float3 hdiffuse, hspecular;

	//  Calculate SSAO
	float occ = 1.0f; //calc_ssao(P, N, I.tc0, I.tcJ);

        hmodel		(hdiffuse, hspecular, mtl, N.w, D.w, P.xyz, N.xyz);
//      hmodel		(hdiffuse, hspecular, mtl, 1, D.w, P.xyz, N.xyz);
//	hdiffuse*=hdiffuse;			//. high contrast hemi
//	hdiffuse*=(D.rgb*0.8 + 0.2h);	// rise texture contrast for diffuse lighting
//	hdiffuse = 0.8;
//	hdiffuse *= (occ*(D.rgb + .1h));
	hdiffuse *= occ;
	hspecular *= occ;

        float4         light       = float4         (L.rgb + hdiffuse, L.w)        ;
        float4         C           = D*light       ;                             // rgb.gloss * light(diffuse.specular)
//        float3         spec        = (C.rgb*.5h + .5h)*C.w + hspecular + hspecular + hspecular;			// replicated specular
	float3         spec        = C.www         + hspecular;      // replicated specular

#ifdef         USE_SUPER_SPECULAR
                      spec      = (C.rgb*.5h + .5h)*C.w + hspecular        ;
#endif
//		float3       color     = C.rgb + D.rgb*spec     ;
//		float3       color     = C.rgb + (D.rgb*spec+spec)/0.5h;
		float3       color     = C.rgb + spec     ;
//		float3       color     = C.rgb + D.rgb*spec+hspecular+hspecular     ;	//	More realistic and contrast specular - Ugrumiy edition

////////////////////////////////////////////////////////////////////////////////
/// For Test ///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#ifdef         DBG_TEST_NMAP
//. hemi + sun + lighting + specular
					color 	= 	hdiffuse + L.rgb + N;
#endif

#ifdef         DBG_TEST_NMAP_SPEC
//. hemi + sun + lighting + specular
					color 	= 	hdiffuse + L.rgb + N + spec;
#endif

#ifdef         DBG_TEST_LIGHT
//. hemi + sun + lighting + specular
					color 	= 	hdiffuse + L.rgb;
#endif

#ifdef         DBG_TEST_LIGHT_SPEC
//. hemi + sun + lighting + specular
					color 	= 	hdiffuse + L.rgb + spec;
#endif

#ifdef         DBG_TEST_SPEC
//. only lighting and specular
					color 		= spec;
#endif
////////////////////////////////////////////////////////////////////////////////

        // here should be distance fog
        float3        	pos        		= P.xyz;
        float         	distance		= length		(pos);
        float         	fog				= saturate		(distance*fog_params.w + fog_params.x); //
                      	color			= lerp     		(color,fog_color,fog);        			//
        float        	skyblend		= saturate		(fog*fog);

#ifdef         DBG_TMAPPING
        color                        	= D.xyz;
#endif

        float          	tm_scale        = I.tc0.w;                // interpolated from VS

#ifdef        USE_SUPER_SPECULAR
        color        	= spec          - hspecular	;
#endif
//		color 		= N; //show normals
//		color                        	= D.xyz;
//		color		= float3( _input, _input, 0.0f );
		color		= float3( I.ttt.x/1024.0f, I.ttt.y/768.0f, 0.0f);
        _out        	o;
        tonemap        	(o.low, o.high, color, tm_scale )	;
                        o.low.a         = skyblend	;
						o.high.a		= skyblend	;

//		o.low	= skyblend;
//		o.hight	= 0;

		return        	o;
}