 
 out vec4 color;
 
-void main() {
-  vec3 normal = normalize(fragmentNormal); // normal should be corrected after interpolation
-  vec4 gamma = vec4(vec3(1.0/2.2), 1);
-  vec3 fragmentToCamera = normalize(cameraPos - fragmentPos);
-
-  // direct light TODO: make procedure
-
+vec4 calcDirectionalLight(vec3 normal, vec3 fragmentToCamera) {
   vec4 directAmbientColor = vec4(directionalLight.color, 1) * directionalLight.ambientIntensity;
 
   float directDiffuseFactor = max(0.0, dot(normal, -directionalLight.direction));

  float directSpecularFactor = pow(max(0.0, dot(fragmentToCamera, directLightReflect)), materialSpecularFactor);
  vec4 directSpecularColor = directionalLight.specularIntensity * vec4(directionalLight.color, 1) * materialSpecularIntensity * directSpecularFactor;

  vec4 directColor = directAmbientColor + directDiffuseColor + directSpecularColor;

  // point light TODO: make procedure

  float pointLightDistance = length(fragmentPos - pointLight.position);
  float pointLightDistance2 = 1.0 + pow(pointLightDistance, 2); // 1.0 constant prevents division by zero
  vec3 pointLightDirection = normalize(fragmentPos - pointLight.position);

  float spotSpecularFactor = pow(max(0.0, dot(fragmentToCamera, spotLightReflect)), materialSpecularFactor);
  vec4 spotSpecularColor = spotLight.specularIntensity * vec4(spotLight.color, 1) * materialSpecularIntensity * spotSpecularFactor / spotLightDistance2;

  return spotFactor*(spotAmbientColor + spotDiffuseColor + spotSpecularColor);
}

void main() {
  vec3 normal = normalize(fragmentNormal); // normal should be corrected after interpolation
  vec3 fragmentToCamera = normalize(cameraPos - fragmentPos);

  vec4 directColor = calcDirectionalLight(normal, fragmentToCamera);
  vec4 pointColor = calcPointLight(normal, fragmentToCamera);
  vec4 spotColor = calcSpotLight(normal, fragmentToCamera);
  vec4 linearColor = texture(textureSampler, fragmentUV) * (directColor + pointColor + spotColor);

  vec4 gamma = vec4(vec3(1.0/2.2), 1);
  color = pow(linearColor, gamma); // gamma-corrected color
}