#version 140

uniform samplerCube texCube;
uniform sampler2D u_normalTexture;

uniform float u_reflective;
uniform float u_RGTC;

in mat3 var_TangentBinormalNormalMatrix;  
in vec3 var_viewDir;
in vec4 var_TexCoord0;

out vec4 FragColor;

void main() {
	vec3 texCoord = var_TexCoord0.xyz;
	if(u_reflective==1.0) {
		vec4 bumpTexel = texture ( u_normalTexture, var_TexCoord0.st ) * 2. - 1.;
		vec3 localNormal = u_RGTC == 1. 
			? vec3(bumpTexel.x, bumpTexel.y, sqrt(max(1.-bumpTexel.x*bumpTexel.x-bumpTexel.y*bumpTexel.y, 0)))
			: normalize( bumpTexel.wyz ); 
		vec3 N = var_TangentBinormalNormalMatrix * localNormal;
		N = normalize(N);
		vec3 nViewDir = normalize(var_viewDir);
		vec3 reflect = - (nViewDir - 2*N*dot(N, nViewDir));
		texCoord = reflect;
		//gl_FragColor.xyz = abs(reflect); return;
	}
	vec4 tex = texture(texCube, texCoord);
	FragColor = tex;	
}