/*
==========================================================================================
 Cg Acceleration Research

 Edgar Velázquez Armendáriz - edgar [at] graphics [dot] cornell [dot] edu
------------------------------------------------------------------------------------------
 ageMain.cg

 Fragment program to age the points and just copy the current imageID.
==========================================================================================
*/

/**
 * fp40: # 17 instructions, 1 R-regs, 1 H-regs
 */
void ageMain(in half2 pos                       : WPOS,
                uniform samplerRECT current     : TEXUNIT0,
                out half4 OUT                   : COLOR) {
                
    // STATIC DATA
    const static half INCREMENT = 1;        // [0,255]
                
    // Gets the image id and age
    OUT = texRECT(current, pos);


    // New points are not penalised
    if (OUT.r > 0) {

        // Penalization for points that were not projected to the image
        // Compares the vectors at once. If all elements of this comparison eq 0, means that
        // all of them are filled with ones, and that is an invalid imageID. Using DeMorgan:
        //
        // if (A) -> Penalize
        //  A =  (x eq 0) &&  (y eq 0) &&  (z eq 0)
        // !A = !(x eq 0) || !(y eq 0) || !(z eq 0)
        // !A =  x neq 0  ||  y neq 0  ||  z neq 0      <-- This is like the Cg function !any()
        // !(!A) = A
        if ( !any(OUT.gba < half3(1, 1, 127/255.0)) ) {
            OUT.r += 8/255.0;
            //OUT.r = 0;
        }

        // Color change penalty, using the flag
        if (OUT.a >= 128/255.0) {
            //OUT.r += 28/255.0;
            OUT.r = 1;
        }

    }

    // Finally age the point. Because this is an RGBA8 buffer, values are saturated for free
    OUT.r += INCREMENT/255.0;
                
}