/*
   AA + BumpMapping + NV shader
   
   by guest(r) - guest.r@gmail.com
*/

const float glow = 1.25;
const float shde = 0.75;

const vec3 dt = vec3(1.0,1.0,1.0);

const mat3 RGBtoYIQ = mat3(0.299, 0.596, 0.212, 
                           0.587,-0.275,-0.523, 
                           0.114,-0.321, 0.311);

const mat3 YIQtoRGB = mat3(1.0, 1.0, 1.0,
                           0.95568806036115671171,-0.27158179694405859326,-1.1081773266826619523,
                           0.61985809445637075388,-0.64687381613840131330, 1.7050645599191817149);

const vec3 val00 = vec3( 1.2, 1.2, 1.2);

vec3 SoftAA (vec3 c00,vec3 c10,vec3 c20,vec3 c01,vec3 c11,vec3 c21,vec3 c02,vec3 c12,vec3 c22)
{
    float d1=dot(abs(c00-c22),dt)+0.001;
    float d2=dot(abs(c20-c02),dt)+0.001;
    float hl=dot(abs(c01-c21),dt)+0.001;
    float vl=dot(abs(c10-c12),dt)+0.001;

    vec3 t1=(hl*(c10+c12)+vl*(c01+c21)+(hl+vl)*c11)/(6.0*(hl+vl));
    vec3 t2=(d1*(c20+c02)+d2*(c00+c22)+(d1+d2)*c11)/(6.0*(d1+d2));

    return t1+t2;
}

uniform sampler2D OGL2Texture;
uniform vec4 OGL2Param;

void main()
{
        
    vec2 dx = vec2(2.0/4096.0,0.0);
    vec2 dy = vec2(0.0,2.0/2048.0);
    
    vec3 c00=texture2D(OGL2Texture, gl_TexCoord[5].xy      ).xyz; 
    vec3 ca0=texture2D(OGL2Texture, gl_TexCoord[5].xy+dx   ).xyz; 
    vec3 c10=texture2D(OGL2Texture, gl_TexCoord[1].xy      ).xyz; 
    vec3 cb0=texture2D(OGL2Texture, gl_TexCoord[1].xy+dx   ).xyz; 
    vec3 c20=texture2D(OGL2Texture, gl_TexCoord[2].zw      ).xyz; 

    vec3 c0a=texture2D(OGL2Texture, gl_TexCoord[5].xy   +dy).xyz; 
    vec3 caa=texture2D(OGL2Texture, gl_TexCoord[5].xy+dx+dy).xyz; 
    vec3 c1a=texture2D(OGL2Texture, gl_TexCoord[1].xy   +dy).xyz; 
    vec3 cba=texture2D(OGL2Texture, gl_TexCoord[1].xy+dx+dy).xyz; 
    vec3 c2a=texture2D(OGL2Texture, gl_TexCoord[2].zw   +dy).xyz; 

    vec3 c01=texture2D(OGL2Texture, gl_TexCoord[3].xy      ).xyz; 
    vec3 ca1=texture2D(OGL2Texture, gl_TexCoord[3].xy+dx   ).xyz; 
    vec3 c11=texture2D(OGL2Texture, gl_TexCoord[0].xy      ).xyz; 
    vec3 cb1=texture2D(OGL2Texture, gl_TexCoord[0].xy+dx   ).xyz; 
    vec3 c21=texture2D(OGL2Texture, gl_TexCoord[4].xy      ).xyz; 

    vec3 c0b=texture2D(OGL2Texture, gl_TexCoord[3].xy   +dy).xyz; 
    vec3 cab=texture2D(OGL2Texture, gl_TexCoord[3].xy+dx+dy).xyz; 
    vec3 c1b=texture2D(OGL2Texture, gl_TexCoord[0].xy   +dy).xyz; 
    vec3 cbb=texture2D(OGL2Texture, gl_TexCoord[0].xy+dx+dy).xyz; 
    vec3 c2b=texture2D(OGL2Texture, gl_TexCoord[4].xy   +dy).xyz; 

    vec3 c02=texture2D(OGL2Texture, gl_TexCoord[1].zw      ).xyz; 
    vec3 ca2=texture2D(OGL2Texture, gl_TexCoord[1].zw+dx   ).xyz; 
    vec3 c12=texture2D(OGL2Texture, gl_TexCoord[2].xy      ).xyz; 
    vec3 cb2=texture2D(OGL2Texture, gl_TexCoord[2].xy+dx   ).xyz; 
    vec3 c22=texture2D(OGL2Texture, gl_TexCoord[6].xy      ).xyz; 

    
    vec3 d00 = SoftAA(c00, ca0, c10, c0a, caa, c1a, c01, ca1, c11);
    vec3 d10 = SoftAA(ca0, c10, cb0, caa, c1a, cba, ca1, c11, cb1);
    vec3 d20 = SoftAA(c10, cb0, c20, c1a, cba, c2a, c11, cb1, c21);

    vec3 d01 = SoftAA(c0a, caa, c1a, c01, ca1, c11, c0b, cab, c1b);
    vec3 d11 = SoftAA(caa, c1a, cba, ca1, c11, cb1, cab, c1b, cbb);
    vec3 d21 = SoftAA(c1a, cba, c2a, c11, cb1, c21, c1b, cbb, c2b);

    vec3 d02 = SoftAA(c01, ca1, c11, c0b, cab, c1b, c02, ca2, c12);
    vec3 d12 = SoftAA(ca1, c11, cb1, cab, c1b, cbb, ca2, c12, cb2);
    vec3 d22 = SoftAA(c11, cb1, c21, c1b, cbb, c2b, c12, cb2, c22);

    float d1=dot(abs(d00-d22),dt)+0.001;
    float d2=dot(abs(d20-d02),dt)+0.001;
    float hl=dot(abs(d01-d21),dt)+0.001;
    float vl=dot(abs(d10-d12),dt)+0.001;

    vec3 t1=(hl*(d10+d12)+vl*(d01+d21)+(hl+vl)*d11)/(3.0*(hl+vl));
    vec3 t2=(d1*(d20+d02)+d2*(d00+d22)+(d1+d2)*d11)/(3.0*(d1+d2));
    
    d11 =.5*(t1+t2);
    
    float bump_param = 3.33-0.66*OGL2Param.z;
    c11 = (-d00+d22-d01+d21-d10+d12+bump_param*d11)/bump_param;
    c11 = min(c11,glow*d11);
    c11 = max(c11,shde*d11);
    c11=RGBtoYIQ*c11;

    c11=vec3(pow(c11.x,val00.x),c11.yz*val00.yz);

    gl_FragColor.xyz=YIQtoRGB*c11;
}