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

const float glow = 1.45;
const float shde = 0.60;

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(0.9, 0.9, 0.9,
                           0.85568806036115671171,-0.27158179694405859326,-1.1081773266826619523,
                           0.61985809445637075388,-0.64687381613840131330, 1.7050645599191817149);

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

uniform sampler2D OGL2Texture;
uniform vec4 OGL2Param;

void main()
{
    vec3 c00 = texture2D(OGL2Texture, gl_TexCoord[5].xy).xyz; 
    vec3 c10 = texture2D(OGL2Texture, gl_TexCoord[1].xy).xyz; 
    vec3 c20 = texture2D(OGL2Texture, gl_TexCoord[2].zw).xyz; 
    vec3 c01 = texture2D(OGL2Texture, gl_TexCoord[3].xy).xyz; 
    vec3 c11 = texture2D(OGL2Texture, gl_TexCoord[0].xy).xyz; 
    vec3 c21 = texture2D(OGL2Texture, gl_TexCoord[4].xy).xyz; 
    vec3 c02 = texture2D(OGL2Texture, gl_TexCoord[1].zw).xyz; 
    vec3 c12 = texture2D(OGL2Texture, gl_TexCoord[2].xy).xyz; 
    vec3 c22 = texture2D(OGL2Texture, gl_TexCoord[6].xy).xyz; 
    vec3 dt = vec3(1.0,1.0,1.0); 

    float d1=dot(abs(c00-c22),dt)+0.0001;
    float d2=dot(abs(c20-c02),dt)+0.0001;
    float hl=dot(abs(c01-c21),dt)+0.0001;
    float vl=dot(abs(c10-c12),dt)+0.0001;

    float md = d1+d2;   float mc = hl+vl;
    hl*=  md;vl*= md;   d1*=  mc;d2*= mc;
    
    float ww = d1+d2+hl+vl;
        
    vec3 d11= (hl*(c10+c12)+vl*(c01+c21)+d1*(c20+c02)+d2*(c00+c22)+ww*c11)/(3.0*ww);
    
    float bump_param = 1.59;
    c11 = (-c00+c22-c01+c21-c10+c12+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;

}