uniform sampler2D corona;
uniform float time;
uniform float timeOffset;

//Coronal percentages within the image
const float coronaStart = 0.636;
const float coronaEnd = 1.0;

//Coronal temporal width (how 'long' does the corona fade out across the length of the corona?)
const float coronaFadeHeight = 0.025;

//Base corona brightness
const float coronaFlame = 1.0;

//Bright factor (how bright do bright things become?)
const float brightFactor = 1.5;

//Dark factor (how dark do dark things become?)
const float darkFactor = 0.8;

const float pi = 3.14159265;
const float two_pi = 6.28318531;

float toAngle(vec2 v) {
	return atan(v.y, v.x);
}

void main() {	
	//Get our position within the corona (from start (innermost = 0) to end (outermost = 1))
	float l = length(gl_TexCoord[0].xy);
	
	float r = l;
	if(r < coronaStart || r > coronaEnd)
		discard;
	r = (r-coronaStart)/(coronaEnd-coronaStart);
	
	vec2 pos = vec2((8.73 * timeOffset) + (toAngle(gl_TexCoord[0].xy/l) / two_pi), -time + timeOffset + (coronaFadeHeight * r));
	
	float brightness = (texture2D(corona, pos, -3.0).r - (0.4 + r*0.2)) * 2.0;
	
	vec3 color;
	color += coronaFlame * (1.0-r);
	
	if(brightness > 0.0) {
		brightness = pow(brightness,(0.15+(r*1.85)));
		color += brightness * brightFactor;
	}
	else {
		color += brightness * darkFactor;
	}
	
	//Fade out due to corona
	color *= (1.0-r) * (1.0-r);
	
	gl_FragColor.rgb = color * gl_FrontMaterial.diffuse.rgb * gl_Color.rgb;
}