/*
	frame 1 algorithm - XPeng
*/

#version 130


uniform sampler2D 	inText;		// input sampler
uniform sampler2D 	frameText0;	// input frame sampler
uniform sampler2D 	frameText1;	// input frame sampler
//uniform sampler2D 	frameText2;	// input frame sampler
//uniform sampler2D 	frameText3;	// input frame sampler
//uniform sampler2D 	frameText4;	// input frame sampler
//uniform sampler2D 	frameText5;	// input frame sampler
//uniform sampler2D 	frameText6;	// input frame sampler
//uniform sampler2D 	frameText7;	// input frame sampler
out     vec4 		outText;	// output sampler

uniform float preOffsetX, preOffsetY, scaleW, scaleH;
uniform float rangeX, rangeY, rangeW, rangeH;

uniform  float scaleX_frame;  // let Ws = source width, Wf = conor frame width, scaleX_frame = Ws/Wf
uniform  float scaleY_frame;  // let Hs = source height, Hf = conor frame height, scaleY_frame = Hs/Hf
uniform  float scaleW_frame;  // let Ws = source width, Wf = hori frame width, scaleX_frame = Ws/Wf
uniform  float scaleH_frame;  // let Hs = source height, Hf = vert frame height, scaleY_frame = Hs/Hf
uniform  float sizeX_frame;
uniform  float sizeY_frame;

/*
    _______________________
    |_0_|______1______|_2_|
    |   |             |   |
    |   |             |   |
    | 7 |             | 3 |
    |   |             |   |
    |___|_____________|___|
    |_6_|______5______|_4_|
*/
void main()
{
	 vec2 v_texCoord = gl_TexCoord[0].st;
	vec3 col = texture2D(inText, v_texCoord).rgb;
    
    float fac;
	 vec2 v_texCoordFrame = vec2((v_texCoord.x*rangeW+rangeX-preOffsetX)/scaleW, (v_texCoord.y*rangeH+rangeY-preOffsetY)/scaleH);

	vec4 colFrame0;
     vec2 texCoordFrame0 = vec2(v_texCoordFrame.s * scaleX_frame, v_texCoordFrame.t * scaleY_frame);
	colFrame0.abgr = texture2D(frameText0, texCoordFrame0).rgba; // rgba ==> abgr

	vec4 colFrame1;
     vec2 texCoordFrame1 = vec2((v_texCoordFrame.s-sizeX_frame) * scaleW_frame, texCoordFrame0.t);
    fac = min(sign(v_texCoordFrame.s - sizeX_frame)+1, 1)*min(1-sign(v_texCoordFrame.s - (1-sizeX_frame)), 1);
    colFrame1.abgr = fac * texture2D(frameText1, texCoordFrame1).rgba; // rgba ==> abgr
/*
	vec4 colFrame2;
     vec2 texCoordFrame2 = vec2(1- (1-v_texCoordFrame.s) * scaleX_frame, texCoordFrame0.t);
    colFrame2.abgr = texture2D(frameText2, texCoordFrame2).rgba; // rgba ==> argb

	vec4 colFrame3;
     vec2 texCoordFrame3 = vec2(texCoordFrame2.s, (v_texCoordFrame.t-sizeY_frame) * scaleH_frame);
    fac = min(sign(v_texCoordFrame.t - sizeY_frame)+1, 1)*min(1-sign(v_texCoordFrame.t - (1-sizeY_frame)), 1);
    colFrame3.abgr = fac * texture2D(frameText3, texCoordFrame3).rgba; // rgba ==> argb

	vec4 colFrame4;
     vec2 texCoordFrame4 = vec2(texCoordFrame2.s, 1- (1-v_texCoordFrame.t) * scaleY_frame);
    colFrame4.abgr = texture2D(frameText4, texCoordFrame4).rgba; // rgba ==> argb

	vec4 colFrame5;
     vec2 texCoordFrame5 = vec2(texCoordFrame1.s, texCoordFrame4.t);
    fac = min(sign(v_texCoordFrame.s - sizeX_frame)+1, 1)*min(1-sign(v_texCoordFrame.s - (1-sizeX_frame)), 1);
    colFrame5.abgr = fac * texture2D(frameText5, texCoordFrame5).rgba; // rgba ==> argb

	vec4 colFrame6;
     vec2 texCoordFrame6 = vec2(texCoordFrame0.s, texCoordFrame4.t);
    colFrame6.abgr = texture2D(frameText6, texCoordFrame6).rgba; // rgba ==> argb

	vec4 colFrame7;
     vec2 texCoordFrame7 = vec2(texCoordFrame0.s, texCoordFrame3.t);
    fac = min(sign(v_texCoordFrame.t - sizeY_frame)+1, 1)*min(1-sign(v_texCoordFrame.t - (1-sizeY_frame)), 1);
    colFrame7.abgr = fac * texture2D(frameText7, texCoordFrame7).rgba; // rgba ==> argb
*/

    col = colFrame0.rgb*colFrame0.a + col*(1-colFrame0.a);
    col = colFrame1.rgb*colFrame1.a + col*(1-colFrame1.a);
/*
    col = colFrame2.rgb*colFrame2.a + col*(1-colFrame2.a);
    col = colFrame3.rgb*colFrame3.a + col*(1-colFrame3.a);
    col = colFrame4.rgb*colFrame4.a + col*(1-colFrame4.a);
    col = colFrame5.rgb*colFrame5.a + col*(1-colFrame5.a);
    col = colFrame6.rgb*colFrame6.a + col*(1-colFrame6.a);
    col = colFrame7.rgb*colFrame7.a + col*(1-colFrame7.a);
*/

	outText = vec4(col, 1.0);
}

