uniform bool isFirstPass;
uniform bool isSmooth;

uniform vec2 imagePos;
uniform vec2 imageSize;
uniform vec2 textureSize;

void main()
{
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

    gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

    // gl_TexCoord[0] is the texture coordinate for this pass.
    // gl_TexCoord[1] is the texture coordinate for the unsmoothed pass (warning: could be outside of 0-1 range)
    if (isSmooth && !isFirstPass)
    {
        vec2 normalisedImagePos = imagePos / textureSize;

        // smoothed pass adds 1 pixel each side
        vec2 adjustedNormalisedImagePos = normalisedImagePos + (vec2(-1.0, 1.0) / textureSize);
        vec2 texCoordUnnormalised = gl_TexCoord[0].xy * (imageSize + vec2(2.0, 2.0));

        gl_TexCoord[1].xy = adjustedNormalisedImagePos + (texCoordUnnormalised / textureSize);

        // Invert Y coordinate
        gl_TexCoord[1].y = adjustedNormalisedImagePos.y + (imageSize.y / textureSize.y) - gl_TexCoord[1].y + adjustedNormalisedImagePos.y;
    }
    else
    {
        gl_TexCoord[1] = gl_TexCoord[0];
    }

    gl_FrontColor = gl_Color;
}
