/**
 * Copyright (C) 2014 Stellar Jockeys.
 * All Rights Reserved.
 *
 * NOTICE: All information contained herein is, and remains the property of
 * Stellar Jockeys and its suppliers, if any.  The intellectual and technical
 * concepts contained herein are proprietary to Stellar Jockeys and its suppliers.
 * Dissemination of this information or reproduction of this material is
 * strictly forbidden unless prior written permission is obtained from
 * Stellar Jockeys.
 *
 * @file transfer.frag
 * @author Harry Hsiao
 * @date 03/18/2014
 */
#version 120
uniform sampler2D uTexture;
uniform vec2 uOffset;
uniform vec2 uPlayerPosition;
uniform vec4 uBorderColor;
uniform float uBorderPenumbra;
uniform float uBorderUmbra;
varying vec2 vTextureCoord;

/*
 * Controls the amount of sharpening. Larger values for more sharpening.
 */
const float SHARPENING = 0.375;

/*
 * Controls the blending of the unshifted texel and the shifted texel.
 *
 * 0 is no subpixel shifting. 1 is only the shifted texel.
 */
const float SMOOTHING = 0.525;

void main()
{
    /*
     * This is a complete hack.
     * Approximates an unsharp filter by tapping in the opposite direction.
     * Blended with original aligned pixel for greater lighting stability.
     *
     * May want to do a proper unsharp filter or try a cubic interpolation with
     * with overshoot.
     */
    vec4 original = texture2D(uTexture, vTextureCoord);
    vec4 shifted = texture2D(uTexture, vTextureCoord + uOffset);
    vec4 unsharp = texture2D(uTexture, vTextureCoord - uOffset);
    vec4 sharpened = (shifted - unsharp) * SHARPENING + shifted;

    vec2 position = vTextureCoord * 2.0 - 1.0;
    position -= uPlayerPosition;
    float distance = length(position);

    // NOTE: smoothstep is glsl 130...
    float border = clamp((distance - uBorderPenumbra) / (uBorderUmbra - uBorderPenumbra), 0.0, 1.0);
    border = ((border * -2.0) + 3.0) * border * border * uBorderColor.a;

    gl_FragColor = vec4(mix(mix(original.rgb, sharpened.rgb, SMOOTHING), uBorderColor.rgb, border), 1.0);
}
