/*
   Cartoon shader I
   
   Copyright (C) 2006 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.

*/


const float th = 0.10;      // outlines sensitivity, recommended from 0.00...0.50
const float bb = 0.55;      // outlines strength,    recommended from 0.10...2.00
const float pp = 1.25;      // outlines blackening,  recommended from 0.25...2.00

uniform sampler2D OGL2Texture;

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);
	float d2=dot(abs(c20-c02),dt);
	float hl=dot(abs(c01-c21),dt);
	float vl=dot(abs(c10-c12),dt);
	c11=(c11+c01+c10+c12+c21)/5.0;	
	
	float d=bb*pow(max(d1+d2+hl+vl-th,0.0),pp)/(dot(c11,dt)+0.5);

	gl_FragColor.xyz = (1.1-d)*c11;
}
