//#version 330
uniform samplerCube texturec;
uniform sampler2D texturee;
uniform sampler2D noise_texture;
uniform vec4 lightpos;

in vec4 ShadowCoord;
in vec4 TexCoord1;
in vec3 direction;
in vec3 place;
in vec3 norm;
in float sshadow;

void main()
{

    vec3 norm_shadowDir = normalize(ShadowCoord.xyz);
    float distanceFromLight = texture(texturec, norm_shadowDir).x;
    float dist = distanceFromLight*100.0;
    float shadow = 0.0;

    int value = 5;
    float size = 7.10/(value*4*value+1);
    float shadows = 1.0/(value*4*value+1);
    for(int i=-value;i<value;i++){
    for(int a=-value;a<value;a++){
    vec3 asd = vec3(ShadowCoord.x+i*size, ShadowCoord.y+a*size, ShadowCoord.z);
    norm_shadowDir = normalize(asd);
    distanceFromLight = texture(texturec, norm_shadowDir).x;
    dist = distanceFromLight*1000.0;
    shadow = dist < (length(asd)- 0.00055) ? shadow+shadows/10.0 : shadow+shadows;
    }}

    float airlight = 0.0;
    vec3 jea = place;
    for(int i=0;i<99;i++)
    {
        jea += direction;
        float diist = length(jea-lightpos.xyz);
        if(texture(texturec, normalize(jea-lightpos.xyz)).x*1000.0>diist)
        {
            airlight += (2.1+texture(noise_texture, normalize(jea-lightpos.xyz).yz/10+lightpos.xy/20.0).x/1.0)/(diist*diist);
        }
    }


	gl_FragData[0] = gl_Color * shadow * texture(texturee,TexCoord1.xy) + 0.1*texture(texturee,TexCoord1.xy);
	gl_FragData[1] = airlight/vec4((1.0/length(direction)))*vec4(lightpos.w);
	gl_FragData[2] = vec4(gl_FragCoord.z, 1.0, TexCoord1.z, 1.0);
}
