##start
##vertex

#version 150
#extension GL_ARB_explicit_attrib_location : enable


layout(location=0) in vec3 in_Position;
layout(location=1) in vec3 in_Normal;
layout(location=2) in vec2 in_Tc;



uniform mat4 modelViewProjectionMatrix;
uniform mat4 modelMatrix;

uniform mat4 normalMatrix;

uniform mat4 meshMatrix;
uniform mat4 depthBiasMV;


uniform sampler2D diffuseTex;


out vec4 color;
out vec3 normal;
out vec2 tc;
out vec4 shadowCoord2;
out vec4 wpos;
out vec3 wnormal;

void main()
{



   tc = in_Tc;

   normal = normalize(mat3(normalMatrix)*mat3(meshMatrix)*in_Normal);
   wnormal = normalize(mat3(modelMatrix)*mat3(meshMatrix)*in_Normal);

	//normal = vec3(viewMatrix * modelMatrix * vec4(in_Normal,0));


   wpos = modelMatrix* meshMatrix*vec4(in_Position,1);
   gl_Position = modelViewProjectionMatrix * meshMatrix * vec4(in_Position,1);
   shadowCoord2 = depthBiasMV * modelMatrix* meshMatrix*vec4(in_Position,1);

}



##end


##start
##fragment

#version 150 core
#extension GL_EXT_texture_array : enable

in vec4 color;
in vec3 normal;
in vec2 tc;
in vec4 shadowCoord2;
in vec4 wpos;
in vec3 wnormal;

out vec4 out_color;

uniform vec3 sunDir;
uniform vec4 sunColor;


uniform sampler2D diffuseTex;
uniform sampler2DShadow depthTex;

uniform float outsideShadow;

uniform int alwaysBright;

uniform vec3 firePos;
uniform float fireNoise;

uniform int night;
uniform int lightning;

uniform float flashLightIntensity;

uniform int golden;

void main(void)
{

    vec4 shadowCoord = shadowCoord2/shadowCoord2.w;

    //phong lightning

     vec3 n =normalize(wnormal);

    vec3 L,L2;
    vec4 c2 = vec4(0,0,0,0);
    if(night==1){
        L = -normalize( vec3(wpos)- sunDir);
        L2 = -normalize( vec3(wpos)- (sunDir+vec3(0,1,0)));
        c2 = sunColor*max(0,dot(n,L2));
    }else
        L = normalize(sunDir);
    //vec3 l2 = sunDirViewSpace;

     vec4 diff2 = sunColor*max(0,dot(n,L));
     vec4 c = diff2;

//     vec4 ambient = vec4(1,1,1,0);
//     c += ambient*0.1;

     //======== Fire
     //const float maxDis = 30.0f;
     const float minRange2 = 10.0f;
     const float maxRange2 = 25.0f;

     float dist = distance(vec3(wpos),firePos);

     float falloff = dist-minRange2;
     falloff = 1-(falloff/(maxRange2-minRange2));

     dist = clamp(falloff,0,1);

     vec4 fireColor = vec4(226,88,34,1)/vec4(255,255,255,1);

     L = -normalize( vec3(wpos)- firePos);
     vec4 fireDiff = /*diff2**/max(0,dot(normalize(wnormal),L))*fireColor;
     fireDiff = fireDiff*dist*fireNoise*10.0f + /*ambient*/ fireColor*dist/**0.2f*/;

     //==============



     if (alwaysBright == 1 && night == 1){
         //if (night == 1) c = vec4(0.5f);
         //c = max(0,dot(n,l2));
         out_color = (c2 + fireDiff)*texture2D(diffuseTex, tc);
        return;
     }



    if ((shadowCoord.s < 0 || shadowCoord.s > 1 || shadowCoord.t < 0 || shadowCoord.t > 1)){
        //out_color = vec4(1,0,0,0);

        out_color = (fireDiff+outsideShadow*c)*texture2D(diffuseTex, tc);

        return;
    }

    //======== Shadowmapping ============================================================


    float visibility = 1.0;

    float bias =  0.00012;
//bias = 0;
//    if (0.9 > texture(depthTex, vec3(shadowCoord.xy, shadowCoord.z -bias))){
//        visibility = 0.1;
//    }

if(lightning == 0)
visibility = texture(depthTex, vec3(shadowCoord.xy, shadowCoord.z -bias));

//float bias = 0.0005;
//float visibility = 1.0f;

//float sum = 0;
//float x, y;
//float offset = 1.0/1024.0/10.0;
//for (y = -1.5; y <= 1.5; y += 1.0){
//    for (x = -1.5; x <= 1.5; x += 1.0){
//     sum += texture(depthTex, vec3(shadowCoord.x+x*offset, shadowCoord.y+y*offset, shadowCoord.z -bias));
//    }
//}

//visibility = sum / 16.0;




    c *= visibility;
    vec4 ambient = vec4(1,1,1,0);
    c += ambient*0.1;
    //====================================================================

    //======== Flashlight border

    float borderfactor = 1.0f;
     if (night == 1 && lightning == 0){
        const float minRange =0.3f;
        const float maxRange = 0.5f;
        float dist = distance(vec2(0.5,0.5),shadowCoord.st);

        borderfactor = dist-minRange;
        borderfactor = 1-(borderfactor/(maxRange-minRange));


        borderfactor= clamp(borderfactor,0.0,1.0) *flashLightIntensity;
    }
     //==============


//    out_color = vec4(wpos);

  out_color = ( borderfactor*c+fireDiff)*texture2D(diffuseTex, tc);

  if (golden == 1){
      out_color *= vec4(1,1,0,1);
  }
//      out_color = vec4(wnormal ,1);



}


##end
