#version 330

precision highp float;

uniform sampler2D texture;
uniform sampler2D textureNormal;

uniform vec3 sunPosition;
uniform vec3 modelPosition;
uniform vec3 color;
uniform bool picked;

in VertexData
{
	vec3 worldPos;
	vec3 normal;
	vec3 tangent;
	vec3 bitangent;
	vec2 texCoord;
	vec4 color;
} inData;

void main()
{           
    vec3 LightDir = normalize(sunPosition - inData.worldPos);
    
    vec3 bumpNormal = texture2D(textureNormal, inData.texCoord.st).rgb;
	bumpNormal = normalize(bumpNormal - 0.5);	
	
	mat3 tangentMatrix = inverse(mat3(inData.tangent, inData.bitangent, inData.normal));
	
	// Convert normal to world space
    bumpNormal = normalize(bumpNormal * tangentMatrix);
        
    //float NdotL = max(dot(inData.normal, LightDir), 0.0);
    float NdotL = max(dot(bumpNormal, LightDir), 0.0);
	
	vec3 diffuse = vec3(1.0, 1.0, 1.0);
	vec3 ambient = vec3(0.4, 0.4, 0.4);
	
	vec3 light = (NdotL * diffuse) + ambient;

	vec4 diffuseColor = max(inData.color, texture2D(texture, inData.texCoord.st));
	diffuseColor.rgb *= light;		
	//diffuseColor = vec4(bumpNormal, 1);
	//diffuseColor = vec4(NdotL, NdotL, NdotL, 1);
	//diffuseColor = vec4(light, 1);
	gl_FragColor = vec4(diffuseColor);
}
