// 
//
// Simple Fragment Shader to Render a TFfunction & including the Alpha of the FrontColor
//
#include "Renderer.glsl"

uniform sampler2D texGrid;
uniform float sizeX;
uniform float sizeY;

void main(){ 
          
 	 vec2 TexPoint = gl_TexCoord[0].xy;
	 vec2 TexCoord = gl_TexCoord[0].xy;//vec2(0.0,0.0);    
	 float ratioX =1.0;
	 float ratioY =1.0;;
     vec2 step = vec2(1.0/255.0, 1.0/255.0); 	                    
 	 vec2 posi = vec2(0.0,0.0);
  
	///do in X direction
	do{
		float xst = (texture2D(texGrid, posi)).x;
		float xsp = (texture2D(texGrid, vec2(posi.x + step.x,posi.y))).x;	
		
		if ( (xst <= TexPoint.x) && ( xsp >= TexPoint.x) ){
			TexCoord.x = posi.x;		
			break;		
		}
		posi  += step;		
	}while(posi.x <= ratioX); 

	///do in Y direction
	posi = vec2(0.0,0.0);
	do{
		float yst = (texture2D(texGrid, posi       )).y;
		float ysp = (texture2D(texGrid, vec2(posi.x,posi.y + step.y))).y;	

		if ((yst <= TexPoint.y) && ( ysp > TexPoint.y)){
			TexCoord.y = posi.y;		
			break;		
		}
		posi  += step;		
	}while(posi.y <= ratioY); 

gl_FragColor = vec4(TexCoord.x,TexCoord.y,0.0,1.0);
	
return;
}