
uniform mat4 PROJECTION_MATRIX;
uniform mat4 VIEW_MATRIX;
uniform mat4 WORLD_MATRIX;

uniform vec4 COLOUR;

attribute vec4 vertex_position;
attribute vec3 vertex_normal;

varying vec4 colour;

const vec3 SUN_COLOUR = vec3(1.0, 1.0, 1.0);
const vec3 SUN_DIRECTION = vec3(-1.0, -1.0, -1.0);

vec3 getLightColour(vec3 normal, vec3 vertex_view, vec3 light_position)
{
  vec3 vertex_light_offset = normalize(vec3(-light_position.xyz));
  float attenuation = 1.0;

  float ambient_val = 0.2;
  vec3 ambient = ambient_val * COLOUR.rgb * SUN_COLOUR.rgb;

  float diffuse_val = max(0.0, dot(normal, vertex_light_offset));
  vec3 diffuse = diffuse_val * COLOUR.rgb * SUN_COLOUR.rgb;

  vec3 final_colour = ambient + attenuation * (diffuse);
  return final_colour;
}

void main()
{
  mat4 MODELVIEW = VIEW_MATRIX * WORLD_MATRIX;
  mat4 MVP = PROJECTION_MATRIX * MODELVIEW;
  mat3 NORMAL_MATRIX = mat3(WORLD_MATRIX);

  vec4 view_position = WORLD_MATRIX * vertex_position;

  vec3 normal = normalize(NORMAL_MATRIX * vertex_normal);
  vec3 vertex = vec3(view_position);

  vec3 light_colour = vec3(0.0);
  light_colour += getLightColour(normal, vertex, SUN_DIRECTION);

  // Adjust vertex colour based on visibility (AFTER shading to blend right)
  colour = vec4(light_colour, COLOUR.a);

  gl_Position = MVP * vertex_position;
}
