#version 400

in vec3 vWorldPos;

in vec4 vDiffuse;
in vec4 vAmbient;
in vec4 vSpecular;
in vec4 vCenter;

in vec4 lightPos;
in float linearDepth;

out vec4 fragColor;

uniform sampler2D shadowMap;

uniform sampler2D position;
uniform sampler2D albedo;
uniform sampler2D normals;

uniform vec2 viewportSize;

const vec3 lightVector = vec3(0.0, -1.0, 0.0);
const vec3 lightPosition = vec3(0.0, -500.0, 1000.0);
const vec3 ambientLight = vec3(0.1, 0.1, 0.1);
const vec3 diffuseLight = vec3(0.1, 0.1, 0.1);
const vec3 sunLight = vec3(0.7, 0.7, 0.7);

void main(void) {
  vec3 lightColor = vDiffuse.rgb;
  vec2 coord = gl_FragCoord.xy / viewportSize;
  vec3 pos = texture(position, coord).xyz;
  // vec3 albedo = texture(albedo, coord).xyz;
  vec3 normal = texture(normals, coord).xyz * 2.0 - 1.0;
  float dist = 1.0 - distance(vCenter.xyz, pos) / vCenter.w;
  float flatDist = 1.0 - dot(vCenter.xyz, vWorldPos.xyz);

  float shininess = 1.0;
  vec3 lightDir = normalize(vCenter.xyz - pos);
  float shine = clamp(dot(normalize(lightDir), normal), 0.0, 1.0) * shininess * 0.0;

  fragColor = clamp(vec4(lightColor, lightColor * flatDist + shine * dist * vDiffuse.a), 0.0, 1.0);
  // fragColor = vec4(vec3(dot(lightDir, normal) * 2.0), 1.0);
  // fragColor = min(vec4(1.0, 1.0, 1.0, 1.0), vec4(albedo, dist));
  // fragColor = vec4(spec * vec3(1.0, 1.0, 1.0), 1.0);
}
