#version 150
//#extension GL_EXT_gpu_shader4 : enable

in vec3 view_dir;
in vec4 view_pos;

uniform vec3 ambient;
uniform sampler2D t0, t1, t2, t3, t4, t_pp;
uniform sampler2D td;
uniform int gid, lightsCount, shadow_on;
uniform float z_near, z_far;
uniform bool firstPass;
uniform vec2 dt;
uniform vec4 back_color;
uniform mat4 mat_proji, mat_view, mat_viewi, mat_viewproji;

float light_diffuse(int model, vec3 l, vec3 n) {return max(0., dot(l, n));}
float light_specular(int model, vec3 l, vec3 n, vec3 h, vec3 v, float shininess) {return max(0., pow(dot(n, h), shininess));}

vec4 pos, lpos, shp;
vec3 li, si, ldir, halfV, bn, bn2, lwdir;
vec3 vds, vds2;
float sh_pow, sh_mul, dist, NdotL, NdotH, spot, ldist, diff, sdist, shadow;

float getShadow(int light, vec3 view_pos, vec3 dpos) {
	shp = qgl_Light[light].shadowMatrix * vec4(view_pos + dpos, 1);
	shp.z -= z_near / 20.;
	return textureProj(qgl_Light[light].shadow, shp);
}

void calcLight(in int index, in vec3 n, in vec3 v, in vec4 v2) {
	lpos = qgl_Light[index].position;
	ldir = lpos.xyz - (pos.xyz * lpos.w);
	ldist = length(ldir);
	ldir = normalize(ldir);
	halfV = normalize(ldir + v);
	NdotL = max(dot(n, ldir), 0.0001);
	NdotH = max(dot(n, halfV), 0.0001);
	spot = step(0., NdotL) * qgl_Light[index].intensity;
	if (qgl_Light[index].endAngle <= 90.) {
		float scos = max(dot(-ldir, qgl_Light[index].direction.xyz), 0.);
		spot *= scos * step(qgl_Light[index].endAngleCos, scos);
		spot *= smoothstep(qgl_Light[index].endAngleCos, qgl_Light[index].startAngleCos, scos);
		//lwdir = mat3(mat_viewi) * qgl_Light[index].direction.xyz;
		//bn = normalize(cross(lwdir, vec3(1, 0, 0)));
		//bn2 = normalize(cross(lwdir, bn));
		float ds = ldist/200.;//max(abs(sdist) / 5000, 0.02);
		//spot *= clamp(1. - sdist, 0, 1);
		vds = ds * bn.xyz;
		vds2 = ds * bn2.xyz;
		float shadow  = getShadow(index, pos.xyz, vec3(0)) * 3.;
			  shadow += getShadow(index, pos.xyz,   vds                    ) * 2.;
			  shadow += getShadow(index, pos.xyz, - vds                    ) * 2.;
			  shadow += getShadow(index, pos.xyz,             - vds2       ) * 2.;
			  shadow += getShadow(index, pos.xyz,             + vds2       ) * 2.;
			  /*shadow += getShadow(index, pos.xyz,   vds       - vds2       ) * 1.5;
			  shadow += getShadow(index, pos.xyz,   vds       + vds2       ) * 1.5;
			  shadow += getShadow(index, pos.xyz, - vds       - vds2       ) * 1.5;
			  shadow += getShadow(index, pos.xyz, - vds       + vds2       ) * 1.5;
			  shadow += getShadow(index, pos.xyz,   vds + vds              );
			  shadow += getShadow(index, pos.xyz, - vds - vds              );
			  shadow += getShadow(index, pos.xyz,             - vds2 - vds2);
			  shadow += getShadow(index, pos.xyz,             + vds2 + vds2);
			  shadow += getShadow(index, pos.xyz,   vds + vds - vds2       );
			  shadow += getShadow(index, pos.xyz, - vds - vds - vds2       );
			  shadow += getShadow(index, pos.xyz,   vds + vds + vds2       );
			  shadow += getShadow(index, pos.xyz, - vds - vds + vds2       );
			  shadow += getShadow(index, pos.xyz,   vds       - vds2 - vds2);
			  shadow += getShadow(index, pos.xyz,   vds       + vds2 + vds2);
			  shadow += getShadow(index, pos.xyz, - vds       - vds2 - vds2);
			  shadow += getShadow(index, pos.xyz, - vds       + vds2 + vds2);*/
			  //shadow += shadow += getShadow(index, pos.xyz, vds+vds2)*10;
		spot *= mix(1., shadow / 11., shadow_on);
	}
	spot /= (qgl_Light[index].constantAttenuation + ldist * (qgl_Light[index].linearAttenuation + ldist * qgl_Light[index].quadraticAttenuation));
	///li += spot * gl_LightSource[index].diffuse.rgb * light_diffuse(0, ldir, n);
	//si += spot * qgl_Light[index].color.rgb * sh_mul * light_specular(0, ldir, n, halfV, v, sh_pow);
	float NdotLs = NdotL*NdotL;
	float NdotHs = NdotH*NdotH;
	float ndlc = (1. - NdotLs) / NdotLs;
	float der = NdotLs * (sh_mul + ndlc);
	diff = 2. / (1. + sqrt(1. + (1. - sh_mul) * ndlc));
	li += spot * qgl_Light[index].color.rgb * diff;// * light_diffuse(0, ldir, n);
	ndlc = (1. - NdotHs) / NdotHs;
	der = NdotHs * (sh_mul + ndlc);
	si += spot * qgl_Light[index].color.rgb * (sh_mul / (der*der) / 3.1416);
}


const float _pe = 2.4e-7;

void main(void) {
	//if (d == 1.) discard;
	ivec2 tc = ivec2(gl_FragCoord.xy);
	float z = texelFetch(td, tc, 0).r;
	if (z == 1.) {
		qgl_FragData[0] = back_color;
		return;
	}
	vec4 v0 = texelFetch(t0, tc, 0),
		 v1 = texelFetch(t1, tc, 0),
		 v2 = texelFetch(t2, tc, 0),
		 v3 = texelFetch(t3, tc, 0),
		 v4 = texelFetch(t4, tc, 0);

	z = z + z - 1;
	z = ((_pe - 2.) * z_near) / (z + _pe - 1.); // infinite depth

	vec2 sp = gl_FragCoord.xy * dt * 2 - vec2(1, 1);
	vec3 dc = v0.rgb, n = v1.xyz;
	bn = normalize(vec3(v3.w, v4.zw));
	bn2 = normalize(cross(n, bn));
	float height = v2.w;
	li = qgl_AmbientLight.color.rgb * qgl_AmbientLight.intensity;
	si = vec3(0.);

	/*float posz = z_near * z_far / (texelFetch(td, tc, 0).r * (z_far - z_near) - z_far);
	pos = vec4(sp, 0., 1) * mat_proji;
	pos.xy *= v0.z;
	pos.z = posz;*/

	pos.w = 1;
	pos.xyz = view_dir * z;
	//pos.z = -pos.z;

	//pos.z = posz;
	//pos.xyz += n * height;
	//pos.xyz = v3.xyz;
	//pos = v3;
	//pos = vec4(sp, 0, 1.) * mat_proji;
	//pos *= v0.w;
	//pos.z += 1;
	//pos.xy *= 10.;
	//pos.z = v0.w;
	vec3 v = normalize(-pos.xyz);

	float reflectivity = 0.;
	float specularity = modf(v1.w, reflectivity);
	sh_pow = 1. / max((1. - specularity), 0.0001);
	sh_mul = max(1. - specularity, 0.0001);
	for (int i = 0; i < 8; ++i)
		calcLight(i, n, v, v2);
	//	calcLight(0, n, v, v2);

	qgl_FragData[0] = vec4(max(vec3(0), li * dc + si * v2.rgb + v3.rgb + texelFetch(t_pp, tc, 0).rgb), v0.w);
	//qgl_FragData[0].rgb = vec3(length(view_pos.xyz - qgl_Light[1].position.xyz)/3);
	//qgl_FragData[0].rgb = vec3(view_pos.xyz);
	//shd = shd - shp.w;

	/*vec3 fp = pos.xyz;// * lpos.w;
	vec3 _dlp = fp - qgl_Light[0].position.xyz;
	vec3 _ld = qgl_Light[0].direction.xyz;
	float lz = dot(_ld, _dlp);
	vec3 _lt = normalize(cross(_ld, _dlp));
	vec3 _lt2 = normalize(cross(_lt, _dlp));
	float ly = dot(qgl_Light[0].shadowDir0.xyz, normalize(_dlp));
	float lx = dot(qgl_Light[0].shadowDir1.xyz, normalize(_dlp));
	vec3 dd = mat3(mat_viewi)*(normalize(_dlp) - _ld);
	//qgl_FragData[0].rgb = vec3(abs(shp.xy/shp.w)/1,0);
	float Y = dot(_dlp, qgl_Light[0].shadowDir0.xyz) / length(qgl_Light[0].shadowDir0.xyz);
	float X = dot(_dlp, qgl_Light[0].shadowDir1.xyz) / length(qgl_Light[0].shadowDir1.xyz);
	qgl_FragData[0].rgb = vec3(abs((mat3(mat_viewi)*_dlp).x)/100);*/

	//qgl_FragData[0].rgb = vec3(abs(mat3(mat_viewi)* qgl_Light[0].direction.xyz));
	//qgl_FragData[0].rgb = vec3(texture(qgl_Light[0].shadow,shp.xyz/shp.w)/2);
	//qgl_FragData[0].a = 0.;
}
