/**
 * iZ3D frontScreen fragment shader.
 * Copyright © 2007-2009 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
 */

uniform sampler2D texL, texR, textT;
varying vec2 fTexCoord;

// returns coordinates in table-texture for input colors
vec2 color2Table(in float cX, in float cY) {
    return vec2((cX * (255.0 / 512.0) + 1.0 / 1024.0), (cY * (255.0 / 256.0) + 1.0 / 512.0));
}

// compute back color
vec4 getBackColor(in vec4 cL, in vec4 cR) {
   vec2 tCoordR = color2Table(cL.r, cR.r);
   vec2 tCoordG = color2Table(cL.g, cR.g);
   vec2 tCoordB = color2Table(cL.b, cR.b);
   vec4 cB;
   cB.r = texture2D(textT, tCoordR).r;
   cB.g = texture2D(textT, tCoordG).g;
   cB.b = texture2D(textT, tCoordB).b;
   cB.a = cL.a;
   return cB;
}

void main(void) {
    vec4 cL = texture2D(texL, fTexCoord);
    vec4 cR = texture2D(texR, fTexCoord);
    vec4 backColor = getBackColor(cL, cR);
    gl_FragColor = backColor;
}
