uniform sampler2D tex_entry;
uniform sampler2D tex_exit;
uniform sampler3D tex_intensity;
uniform sampler3D tex_gradient;
uniform sampler1D tex_transfer;
uniform float dz;

varying vec3 normal;
varying vec3 v;
varying vec3 lightvec;
varying mat3 normalMatrix;

void main(){
	
	vec4 entry_point = texture2D(tex_entry, vec2(gl_TexCoord[0]));
	
	vec4 Result = {0.0,0.0,0.0,0.0};
	float maxintensity = 0.0;
	
	if (entry_point.w == 0.0) discard;
	else {
		vec4 exit_point = texture2D(tex_exit, vec2(gl_TexCoord[0]));
	
		float dist = distance(entry_point, exit_point)/dz;
		int maxiter = int(floor(dist));
		vec4 diff = (exit_point - entry_point)/dist;
		
		vec4 point = entry_point;
		
		bool work = true;
		int i = 0;
		
		vec4 max = {0.0,0.0,0.0,0.0};
		while(i < maxiter){
			vec4 intensity = texture3D(tex_intensity, point.xyz);
			
			if (intensity.x > max.x) max = intensity;
			
			i++;
			point += diff;
		}
			
		Result = max;	
		

		gl_FragColor = Result;
	}
}