uniform sampler2D cvTexture;
uniform sampler2D cvTexture2;
uniform sampler2D cvTexture3;
uniform sampler2D cvTexture_RESULT;
uniform sampler2D cvTexture_RESULT_2;
uniform sampler2D cvTexture_First;
uniform sampler2D cvOrigin;
uniform sampler1D colorTable;
uniform int SHADER_STATE;

uniform float noise;
uniform float gen;
//float noise = 6.0;	

#define mask 3
int m_value = ( (mask - 1) / 2 );
//float gen = 6.0;//乘上 gen 值

float mask_f = 3.0;

vec4 origin;
vec4 color;
vec4 color2;
vec4 color3;

float GetGreyValue( float pos_x, float pos_y, float origin_g )
{
	float pixel;

	color = texture2D( cvTexture, vec2( pos_x, pos_y ) );
	color3 = texture2D( cvTexture3, vec2( pos_x, pos_y ) );
	color2 = texture2D( cvTexture2, vec2( pos_x, pos_y ) );

	pixel = color.g;
	if( pixel > color2.g )
		pixel = color2.g;
	if( pixel > color3.g )
		pixel = color3.g;
	
	pixel = origin_g - pixel;
	//return origin_g - color.g;
	return pixel;
}

float RemoveNoise( float value )
{
	value = value - ( noise / 255.0 );
	value = value / (255.0/gen - noise)*255.0;

	return value;
}

void main()
{	
	
	
	vec4 color_result;

	vec2 pos = gl_TexCoord[0].st;
	float x = pos.s;
	float y = pos.t;

	//color3 = texture2D( cvTexture3, vec2( x, y ) );
	//color2 = texture2D( cvTexture2, vec2( x, y ) );
	//color = texture2D( cvTexture, vec2( x, y ) );
	origin = texture2D( cvOrigin, vec2( x, y ) );
	if( SHADER_STATE == 0 )
	{
		color_result = texture2D( cvTexture_RESULT_2, vec2( x, y ) );
		color = texture1D( colorTable, color_result.g );
	}
	else if( SHADER_STATE == 2 )	//draw textureId in FBO second time
	{
		color_result = texture2D( cvTexture_RESULT, vec2( x, y ) );

		if( true )
		{
			float sum = 0.0;
			float tmp_x;
			float tmp_y;
			float result;

			for( float i=-m_value; i<=m_value; i++ )
			{
				for( float j=-m_value; j<=m_value; j++ )
				{
					tmp_x = x + i/588.0;
					tmp_y = y + j/504.0;
					//tmp_x = x + i/420.0;
					//tmp_y = y + j/360.0;

					if( tmp_x < 0.0 || tmp_x > 1.0 || tmp_y < 0.0 || tmp_y > 1.0 )
						continue;
							
					//sum += GetGreyValue( tmp_x, tmp_y, origin.g );
					//sum += RemoveNoise ( texture2D( cvTexture_RESULT, vec2( tmp_x, tmp_y ) ).g );
					sum +=  texture2D( cvTexture_RESULT, vec2( tmp_x, tmp_y ) ).g ;

				}
			}
			color_result.g = sum / (pow( mask_f, 2.0 ));
	
		}
		color_result.g = RemoveNoise ( color_result.g );

		color.r = color.b = color.g = color_result.g;
	}
	else if( SHADER_STATE == 1 )	//draw textureId in FBO first time
	{
		color.g = GetGreyValue( x, y, origin.g );

		//if( x > 0.33 && x < 0.6325 )	//中間的遮色片
		//	color.g = 0.0;
		//else if( x > 0.97 )	//右邊的遮色片
		//	color.g = 0.0;
		//else if( x < 0.051 )	//左邊的遮色片
		//	color.g = 0.0;
		//else			
		//	color.g = GetGreyValue( x, y, origin.g );
		
		color.r = color.b = color.g;

    }
	else if( SHADER_STATE == 3 )	//640*480的原始影像材質
	{
		if( x > 1.0 || x < 0.0 || y > 1.0 || y < 0.0 )
			color.r = color.g = color.b = 1.0;
		else
			color = texture2D( cvTexture_First, vec2( x, y ) );

			//color.g = 1.0;
	}
	

	gl_FragColor = color;
}
