#version 330


layout (location=0) in vec3 vsPosition;
layout (location=1) in vec2 vsTexture;


uniform mat4 worldViewProj;
uniform int transitionState;
uniform float transitionProgress=1.0;
uniform int direction=0;
uniform sampler2DRect inputTex;
layout (location=2) in vec2 vsTextureMask;

uniform sampler2DRect maskTex;
uniform vec2 windowSize;
out vec2 fsFragScale;



const vec4 dirMult[9]=vec4[] (
                              vec4(1.,-1.,1.,0.),
                              vec4(0.,-1.,1.,0.),
                              vec4(-1.,-1.,1.,0.),
                              vec4(1.,0.,1.,0.),
                              vec4(0.,0.,1.,0.),
                              vec4(-1.,0.,1.,0.),
                              vec4(1.,1.,1.,0.),
                              vec4(0.,1.,1.,0.),
                              vec4(-1.,1.,1.,0.)
                              );


out vec2 fsTexture;

void main()
{
    vec4 deltaTransIn=vec4(-7.,-5.,-16.,.0)*dirMult[direction];
    vec4 transOffsetIn=vec4(-3.5,-2.5,-8.0,.0)*dirMult[direction];

    vec4 deltaTransOut=vec4(5.5,4.,10.0,0.0)*dirMult[direction];
    vec4 transOffsetOut=vec4(0.0,0.0,0.0,0.0);


#define near .5
#define far 500.0
    mat4 projection = mat4(
                           2.4140,0.0000,0.0000,0.0000,
                           0.0000,2.4140,0.0000,0.0000,
                           0.0000,0.0000,(far+near)/(near-far),(2*near*far)/(near-far),
                           0.0000,0.0000,-1.000,0.0000);

    mat4 translation=mat4(
                          1.0, 0.0, 0.0, 0.00,
                          0.0, 1.0, 0.0, 0.00,
                          0.0, 0.0, 1.0,-2.414,
                          0.0, 0.0, 0.0, 1.00);

    vec4 tmpPos=vec4(vsPosition.x,vsPosition.y,vsPosition.z,1.0)*worldViewProj;
    vec4 worldPos=tmpPos;

    if(transitionState==0)
    {
        float progress=transitionProgress*.5;
        transOffsetIn-=deltaTransIn*progress;
        float offset=(.5-progress)*6.5*transitionProgress;
        vec2 toff=dirMult[direction].xy*offset;
        transOffsetIn.xy+=toff;

        translation[0][3]+=transOffsetIn.x;
        translation[1][3]+=transOffsetIn.y;
        translation[2][3]+=transOffsetIn.z;
    }
    else if(transitionState==2)
    {
        float progress=-transitionProgress*.5;
        transOffsetOut-=deltaTransOut*progress;
        float offset=(.5-progress)*3.*transitionProgress;
        vec2 toff=dirMult[direction].xy*offset;
        transOffsetIn.xy+=toff;

        translation[0][3]+=transOffsetOut.x;
        translation[1][3]+=transOffsetOut.y;
        translation[2][3]+=transOffsetOut.z;
    }


    worldPos*=translation;
    gl_Position = worldPos*projection;

    ivec2 texSize=textureSize(inputTex);
    fsTexture=vsTexture*vec2(texSize);
    fsFragScale=vec2(textureSize(maskTex))/windowSize;
}
