/**
 * iZ3D frontScreen fragment shader.
 * Copyright © 2007-2008 iZ3D LLC
 * Copyright © 2007-2010 Kirill Gavrilov <kirill@sview.ru>
 *
 * Distributed under the Boost Software License, Version 1.0.
 * See accompanying file license-boost.txt or copy at
 * http://www.boost.org/LICENSE_1_0.txt
 */

vec4 vecOne   = vec4(    1.0,     1.0,     1.0,     1.0);
vec4 vecMinC  = vec4(  0.003,   0.003,   0.003,   0.003);
vec4 vecHalfG = vec4(0.72974, 0.72974, 0.72974, 0.72974);

// normalize multipliers
float multUp = 2.2;
float multDw = 1.0 / multUp;

// process power operation for rgb components
vec4 POW3(vec4 v, float m) {
    return vec4(pow(abs(v.r), m), pow(abs(v.g), m), pow(abs(v.b), m), 0.0);
}

uniform sampler2D texL, texR;
varying vec2 fTexCoord;
void main(void) {
    vec4 colorL = texture2D(texL, fTexCoord); // got texture Left
    vec4 colorR = texture2D(texR, fTexCoord); // got texture Right

    colorL = POW3(colorL, multUp); // firstly process normalize
    colorR = POW3(colorR, multUp);

    colorL = vec4(dot(colorL, vecOne)); // DP3
    colorR = vec4(dot(colorR, vecOne)); // DP3

    colorL += colorR; // ADD

    vec4 vecDelta = colorL - vecMinC; // SUB

    colorL = vec4(1.0 / colorL.r);    // RCP
    colorL *= colorR;                 // MUL
    colorL = POW3(colorL, multDw);

    gl_FragColor.r = (vecDelta.r < 0.0) ? vecHalfG.r : colorL.r; // CMP
    gl_FragColor.g = (vecDelta.g < 0.0) ? vecHalfG.g : colorL.g;
    gl_FragColor.b = (vecDelta.b < 0.0) ? vecHalfG.b : colorL.b;
}
