/*
   SimoneT SimoneT Bump Map + TV colors emulation  v.1.0

   Copyright (C) 2016 SimoneT - simone1tarditi@gmail.com

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    
*/

uniform vec4 OGL2Size;
uniform vec4 OGL2InvSize;
uniform sampler2D OGL2Texture;

// NTSC RGB to sRGB

vec3 ntscrgb_2_srgb( vec3 c ) {

	vec3 v = ( pow( c, vec3(2.2) ) *
        mat3(  0.6068909,  0.1735011,  0.2003480,
 	       0.2989164,  0.5865990,  0.1144845,
 	       0.0000000,  0.0660957,  1.1162243 ) ) *
	mat3(  3.2404542, -1.5371385, -0.4985314,
	      -0.9692660,  1.8760108,  0.0415560,
 	       0.0556434, -0.2040259,  1.0572252 );

	v.r = ( v.r > 0.0031308 ) ? (( 1.055 * pow( v.r, ( 1.0 / 2.4 ))) - 0.055 ) : 12.92 * v.r;
	v.g = ( v.g > 0.0031308 ) ? (( 1.055 * pow( v.g, ( 1.0 / 2.4 ))) - 0.055 ) : 12.92 * v.g;
	v.b = ( v.b > 0.0031308 ) ? (( 1.055 * pow( v.b, ( 1.0 / 2.4 ))) - 0.055 ) : 12.92 * v.b;

	return clamp( v, 0.0, 1.0 );
}

float CLength(vec4 c1){

	float rmean = c1.x*0.5;

	c1*= c1;

	return sqrt((2.0+rmean)*c1.x+4.0*c1.y+(3.0-rmean)*c1.z)/6.0;
}

	vec2 OGLParam = 1.0/vec2( 2048.0, 1024.0 );

void main (void)
{     
	vec4 color[16];
	color[0]  = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2(-1.5,-1.5));   
	color[1]  = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2(-0.5,-1.5));   
	color[2]  = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2( 0.5,-1.5));   
	color[3]  = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2( 1.5,-1.5));   

	color[4]  = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2(-1.5,-0.5));   
	color[5]  = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2(-0.5,-0.5));   
	color[6]  = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2( 0.5,-0.5));   
	color[7]  = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2( 1.5,-0.5));   

	color[8]  = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2(-1.5, 0.5));   
	color[9]  = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2(-0.5, 0.5));   
	color[10] = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2( 0.5, 0.5));   
	color[11] = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2( 1.5, 0.5));   

	color[12] = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2(-1.5, 1.5));   
	color[13] = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2(-0.5, 1.5));   
	color[14] = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2( 0.5, 1.5));   
	color[15] = texture2D(OGL2Texture, gl_TexCoord[0].xy + OGLParam * vec2( 1.5, 1.5));   

	float tl = CLength((color[0]  + color[1]  + color[4]  + color[5] ) * 0.25);   // top left
	float  l = CLength((color[4]  + color[5]  + color[8]  + color[9] ) * 0.25);   // left
	float bl = CLength((color[8]  + color[9]  + color[12] + color[13]) * 0.25);   // bottom left
	float  t = CLength((color[1]  + color[2]  + color[5]  + color[6] ) * 0.25);   // top
	vec4   c =        ((color[5]  + color[6]  + color[9]  + color[10]) * 0.25);   // center
	float  b = CLength((color[9]  + color[10] + color[13] + color[14]) * 0.25);   // bottom
	float tr = CLength((color[2]  + color[3]  + color[6]  + color[7] ) * 0.25);   // top right
	float  r = CLength((color[6]  + color[7]  + color[10] + color[11]) * 0.25);   // right
	float br = CLength((color[10] + color[11] + color[14] + color[15]) * 0.25);   // bottom right

// Compute dx using Sobel:
//           -1 0 1 
//           -2 0 2
//           -1 0 1

	float dX = (tr + 2.0*r + br -tl - 2.0*l - bl);

// Compute dy using Sobel:
//          -1 -2 -1 
//           0  0  0
//           1  2  1

	float dY = (bl + 2.0*b + br -tl - 2.0*t - tr);

// Build the normalized normal

	float normalStrength = 8.0;

	vec3 normal = normalize(vec3(dX, 1.0 / normalStrength, dY)) * 0.5 + 0.5;

// Determine where the light is positioned (this can be set however you like)  

	vec3 light_pos = normalize(vec3(0.5, 1.5, 0.5));  
	
// Calculate the lighting diffuse value  

	float diffuse = max(dot(normal, light_pos),0.0);  
 
// Set the output color of our current pixel  

	gl_FragColor.rgb = ntscrgb_2_srgb( diffuse * c.rgb );
}

