layout( std140 ) uniform view {
	mat4 V;
	mat4 P;
};

layout( std140 ) uniform sun {
	vec3 sun_dir;
};

struct VSOut {
	vec3 view_position;
	vec3 normal;
	vec3 colour;
};

#ifdef VERTEX_SHADER

in vec3 position;
in vec3 normal;
in vec3 colour;
in mat4 model_to_world;
out VSOut v2f;

void main() {
	vec4 view_position = V * model_to_world * vec4( position, 1.0 );
	v2f.view_position = vec3( view_position );
	v2f.normal = mat3( model_to_world ) * normal;
	v2f.colour = colour;
	gl_Position = P * view_position;
}

#else

in VSOut v2f;
out vec4 screen_colour;

float sq( float x ) {
	return x * x;
}

void main() {
	// sunlight
	float lambert = dot( sun_dir, v2f.normal );
	float half_lambert = sq( lambert * 0.5 + 0.5 );
	vec3 c = v2f.colour * half_lambert;

	// fog
	vec3 fog_colour = vec3( 0.5, 0.6, 0.7 );
	float fog_strength = 0.0005;
	float fog_distance_bias = 100.0;
	float fog_t = 1.0 - exp( -fog_strength * max( ( length( v2f.view_position ) - fog_distance_bias ), 0.0 ) );

	screen_colour = vec4( mix( c, fog_colour, fog_t ), 1.0 );
}

#endif
