/*
   New Bump Shader v 1.0

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

   4xGLSoft lite - Copyright (C) 2016 guest(r) - guest.r@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 sampler2D OGL2Texture;
uniform vec4 OGL2Size;
uniform vec4 OGL2InvSize;
uniform vec4 OGL2Param;

const   vec2 Size2D    = vec2( 2048.0, 1024.0 );
const   vec2 InvSize2D = 1.0/vec2( 2048.0, 1024.0 );
const   vec2 Size3D    = vec2( 4096.0, 2048.0 );
const   vec2 InvSize3D = 1.0/vec2( 4096.0, 2048.0 );
const   vec3 ones = vec3(1.0);
const	vec4 yx = vec4(1.0/4096,1.0/2048,-1.0/4096,-1.0/2048);//4xGLSoft lite constant 
const	vec4 xy = vec4(2.0/4096,2.0/2048,-2.0/4096,-2.0/2048);//4xGLSoft lite constant

//	4xGLSoft lite code

vec3 lite4xGLSoft(vec2 texcoord) {

	vec3 c11 = texture2D(OGL2Texture, texcoord        ).xyz; 
	vec3 c00 = texture2D(OGL2Texture, texcoord + xy.zw).xyz; 
	vec3 c20 = texture2D(OGL2Texture, texcoord + xy.xw).xyz; 
	vec3 c22 = texture2D(OGL2Texture, texcoord + xy.xy).xyz; 
	vec3 c02 = texture2D(OGL2Texture, texcoord + xy.zy).xyz; 
	vec3 s00 = texture2D(OGL2Texture, texcoord + yx.zw).xyz; 
	vec3 s20 = texture2D(OGL2Texture, texcoord + yx.xw).xyz; 
	vec3 s22 = texture2D(OGL2Texture, texcoord + yx.xy).xyz; 
	vec3 s02 = texture2D(OGL2Texture, texcoord + yx.zy).xyz; 

	float d1=dot(abs(c00-c22),ones)+0.001;
	float d2=dot(abs(c20-c02),ones)+0.001;
	float m1=dot(abs(s00-s22),ones)+0.001;
	float m2=dot(abs(s02-s20),ones)+0.001;

	vec3 t2=(d1*(c20+c02)+d2*(c00+c22))/(2.0*(d1+d2));
	
	return .25*(c11+t2+(m2*(s00+s22)+m1*(s02+s20))/(m1+m2));

}

vec3 Bump( vec2 texCoords, vec2 InvSize ){     

	float tl  = dot(lite4xGLSoft( texCoords + InvSize * vec2(-1.0,-1.0) ),ones);   
	float t   = dot(lite4xGLSoft( texCoords + InvSize * vec2( 0.0,-1.0) ),ones);   
	float tr  = dot(lite4xGLSoft( texCoords + InvSize * vec2( 1.0,-1.0) ),ones);   

	float l   = dot(lite4xGLSoft( texCoords + InvSize * vec2(-1.0, 0.0) ),ones);   
	vec3  c   =     lite4xGLSoft( texCoords );   
	float r   = dot(lite4xGLSoft( texCoords + InvSize * vec2( 1.0, 0.0) ),ones);   

	float bl  = dot(lite4xGLSoft( texCoords + InvSize * vec2(-1.0, 1.0) ),ones);   
	float b   = dot(lite4xGLSoft( texCoords + InvSize * vec2( 0.0, 1.0) ),ones);   
	float br  = dot(lite4xGLSoft( texCoords + InvSize * vec2( 1.0, 1.0) ),ones);

	// 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 = 1.0 + OGL2Param.z;

	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.25, 0.5, 0.25));  
	
	// Calculate the lighting diffuse value  

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

	return diffuse * c.rgb ;

}

void main()
{

	gl_FragColor.rgb = Bump( gl_TexCoord[0].xy, OGL2InvSize.xy );

}
