/**
 * Optimized Anaglyphs (StereoVision) fragment shader.
 * Copyright © 2007-2010 Kirill Gavrilov <kirill@sview.ru>
 *
 * This code is licensed under MIT license (see docs/license-mit.txt for details).
 */

// http://research.csc.ncsu.edu/stereographics/LS.pdf
const mat4 multL = mat4(     0.4154,    -0.0458, -0.0547,      0,
                             0.4710,    -0.0484, -0.0615,      0,
                         0.16666667,    -0.0257,  0.0128,      0,
                                  0,          0,       0,      0);
const mat4 multR = mat4(-0.01090909,     0.3756, -0.0651,      0,
                        -0.03636364, 0.73333333, -0.1287,      0,
                        -0.00606061, 0.01111111,  1.2971,      0,
                                  0,          0,       0,      0);

// normalize multipliers
const vec4 multUp =       vec4(2.2, 2.2, 2.2, 1.0);
const vec4 multDw = 1.0 / vec4(2.2, 2.2, 2.2, 1.0);

uniform sampler2D texL, texR;
varying vec2 fTexCoord;
void main(void) {
    vec4 colorL = texture2D(texL, fTexCoord);
    vec4 colorR = texture2D(texR, fTexCoord);

    // normalize
    colorL = pow(colorL, multUp);
    colorR = pow(colorR, multUp);

    // summ
    vec4 color = multR * colorR + multL * colorL;

    // return to normal
    gl_FragColor = pow(color, multDw);
}
