// Bump Map | Gloss | Bloom
// Version III
// by Zodav

const vec3 gray = vec3(0.299, 0.587, 0.114);
float bump;
uniform sampler2D OGL2Texture;
uniform vec4 OGL2Param;

//Samples -------------------------------------------------------	|\\
//|  		mK08			|\\
//| 		mK04			|\\
//| mK06	mK02	mK00	mK03	mK07	|\\
//| 		mK01			|\\
//| 		mK05			|\\
//| ------------------------------------------------------------------	|\\

void main(){

	vec3 mK00 = texture2D(OGL2Texture, gl_TexCoord[0].xy).rgb;
	vec3 mK01 = texture2D(OGL2Texture, gl_TexCoord[1].xy).rgb;
	vec3 mK02 = texture2D(OGL2Texture, gl_TexCoord[2].xy).rgb;
	vec3 mK03 = texture2D(OGL2Texture, gl_TexCoord[3].xy).rgb;
	vec3 mK04 = texture2D(OGL2Texture, gl_TexCoord[4].xy).rgb;
	vec3 mK05 = texture2D(OGL2Texture, gl_TexCoord[1].zw).rgb;
	vec3 mK06 = texture2D(OGL2Texture, gl_TexCoord[2].zw).rgb;
	vec3 mK07 = texture2D(OGL2Texture, gl_TexCoord[3].zw).rgb;
	vec3 mK08 = texture2D(OGL2Texture, gl_TexCoord[4].zw).rgb;
		
	if (OGL2Param.z == 0){ bump = 2.0; }
	if (OGL2Param.z == 1){ bump = 4.0; }
	if (OGL2Param.z == 2){ bump = 6.0; }
	if (OGL2Param.z == 3){ bump = 8.0; }
	
	//Normal Map Creation
		float bp1 = 1.0-(dot(((mK01*-1)+(mK00*-1)+(mK04*2)), gray)*bump+0.5);
		float bp2 = 1.0-(dot(((mK03*-1)+(mK00*-1)+(mK02*2)), gray)*bump+0.5);
		float bp3 = dot(((mK04*-1)+(mK00*-1)+(mK01*2)), gray)*bump+0.5;
		float bp4 = dot(((mK02*-1)+(mK00*-1)+(mK03*2)), gray)*bump+0.5;
		vec3 tmp1 = vec3(bp1*bp3, bp2*bp4, 1.0);
		vec3 tmp2 = normalize(tmp1*2.0-1.0);
	
	//Pseudo HDR Bloom
		float pHDR = smoothstep(0.2, 1.0, dot((mK05*2.0+mK06*2.0+mK00*4.0+mK07*2.0+mK08*2.0)/12.0, gray));
		
	//Bump Map
		float bumpMap = ((min(min(tmp2.r, tmp2.g), tmp2.b)+max(max(tmp2.r, tmp2.g), tmp2.b))/2);

	//Gloss Map
		float glossMap = pow(dot(reflect(vec3(bumpMap), -vec3(0.0, 0.0, 1.0)), vec3(0.75)), 0.5);
					
	//Color Map
		vec3 colorMap = mK00;
		colorMap += 1.0-((1.0-(mK00+bumpMap*glossMap))*(1.0-pHDR));
		float cMR = smoothstep(0.0, 2.0, colorMap.r);
		float cMG = smoothstep(0.0, 2.0, colorMap.g);
		float cMB = smoothstep(0.0, 2.0, colorMap.b);
				
	gl_FragColor.rgb = vec3(cMR, cMG, cMB);
}