/*
 * render_mode
 * 0: ArrowPlot 1: StreamLine 2: ColorCoding
 */
uniform int render_mode;

uniform float opacity;

uniform sampler1D tex_transfer;
uniform sampler2D tex_arrow;
uniform sampler2D tex_channel0;
uniform sampler2D tex_channel1;
uniform sampler2D tex_channel2;

uniform float channel0_min;
uniform float channel0_max;
uniform float channel1_min;
uniform float channel1_max;
uniform float channel2_min;
uniform float channel2_max;

// parameters for arrow plot
uniform vec4 arrow_color_toning;

void main() {
	if (render_mode == 0 || render_mode == 1) {
		// ArrowPlot
		float pi = 2.0 * asin(1.0);
		
		// Pfeil zeigt nach oben
		vec2 vArrow = vec2(0.0, 1.0);
		vec2 vPoint = vec2(texture2D(tex_channel0, vec2(gl_TexCoord[1])).x, texture2D(tex_channel1, vec2(gl_TexCoord[1])).x);

		vPoint.x = vPoint.x * (abs(channel0_min) + channel0_max) - abs(channel0_min);
		vPoint.y = vPoint.y * (abs(channel1_min) + channel1_max) - abs(channel1_min);

		vPoint = normalize(vPoint);
		
		//float angle = pi / 2.0; // 90° nach rechts
		//float angle = pi / 4.0; // 45° nach rechts
		//float angle = 0.0;		// keine Drehung

		float angle = acos(dot(vArrow, vPoint) / (length(vArrow) * length(vPoint)));
		
		if (vPoint.x < 0.0)
			angle *= -1.0;
		
		vec4 coord = gl_TexCoord[0];
		
		mat4 rotMat =    mat4(cos(angle), -sin(angle), 0.0, 0.0,
							  sin(angle),  cos(angle), 0.0, 0.0,
							  0.0,        0.0,         1.0, 0.0,
							  0.0,        0.0,         0.0, 1.0);
							  
		coord -= 0.5;
		coord = rotMat * coord;
		coord += 0.5;
	
		vec4 color = texture2D(tex_arrow, coord.xy) * arrow_color_toning;
		color.a *= opacity;
		gl_FragColor = color;
	} else if (render_mode == 2) {
		// ColorCoding
		vec4 intensity = texture2D(tex_channel0, gl_TexCoord[0].xy);
		vec4 color = texture1D(tex_transfer, intensity.x);
		
		//vec4 color = texture2D(tex_channel0, gl_TexCoord[0].xy);
		color.a *= opacity;
		gl_FragColor = color;
	} else {
		// OOOOPPS where are we now? OK draw a warning red...
		gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
	}
}
