#version 410 core

layout(location = 0, index = 0) out vec4 fragColor;

in vec3 vObjPos;
//in vec3 N;
uniform vec4 uOverallColor;

DECLARE_VERTEX_PROPERTIES

vec4 LightFragment(vec3 v, vec3 N, vec4 cShaderColor, vec3 sceneColor, vec3 materialAmbient, vec3 materialDiffuse, vec3 materialSpecular, float materialShininess)
{
vec4 ambient = vec4(0.0);
vec4 diffuse = vec4(0.0);
vec4 specular = vec4(0.0);
vec3 eye = normalize(-v);
{
  const int i =0;
  vec3 VP = - v;
  float dd = dot(VP, VP);
  float d = sqrt(dd);
  VP = VP / d;
  vec3 halfVector = normalize(VP + eye);
  float nDotVP = max(0.0, dot(N, VP));
  float nDotHV = max(0.0, dot(N, halfVector));
  float pf = 0.0;
  if(nDotVP > 0.0) pf = pow(nDotHV, materialShininess);
  float attenuation = 1.0;
  diffuse.rgb  += vec3(1.0) * (nDotVP * attenuation);
  specular.rgb += vec3(1.0) * (pf * attenuation);
}
  return vec4(
    (sceneColor +
    ambient.rgb * materialAmbient +
    diffuse.rgb * materialDiffuse) * cShaderColor.rgb +
    specular.rgb * materialSpecular, cShaderColor.a);
}

void main(void)
{
  vec3 N = normalize(cross(dFdx(vObjPos), dFdy(vObjPos)));

  fragColor = uOverallColor;
  SET_VERTEX_PROPERTIES
  fragColor = LightFragment(vObjPos, normalize(N), fragColor,vec3(0.1), vec3(0.0), vec3(0.98), vec3(0.25), 90.0);
}