#version 430
#define PI 3.1415926535897932384626433832795
uniform vec3 lightColor;
uniform sampler2D tex;
uniform vec3 ambientLight;
uniform vec3 lightPosition;
uniform vec3 cameraPosition;
in Vertex{
vec4 rawPosition; // Vertex Position only multiplied by the transformationmatrix. ( gl_Position is also multiplied by the projection and view matrices)
vec2 uv;
vec4 normal; // the normals have also been multiplied by the transformationmatrix ( is this correct? my normals have a w-value of 0.0f but the transformationmatrix makes a difference. without it the reflections dont seem be in the right place.
}vertexIn;
out vec4 color;
void main(){
vec3 normal = normalize(vec3(vertexIn.normal.x, vertexIn.normal.y, vertexIn.normal.z));
vec3 worldPosition = vec3(vertexIn.rawPosition.x, vertexIn.rawPosition.y, vertexIn.rawPosition.z);
vec3 lightVector = normalize(lightPosition - worldPosition);
vec3 reflection = normalize(reflect((-1.0)*lightVector, normal));
vec3 cameraVector = normalize(cameraPosition - worldPosition);
float lightAngle = clamp(dot(lightVector, normal), 0.0, 1.0);
float cameraAngle = clamp(dot(cameraVector, reflection), 0.0, 1.0);
float specular = pow(cameraAngle, 100);
vec4 actColor = texture(tex, vertexIn.uv);
vec4 diffuseLight = vec4(lightColor, 1.0) * lightAngle;
vec4 specularLight = vec4(lightColor, 1.0) * specular;
color = actColor * clamp(diffuseLight, vec4(ambientLight, 1.0), vec4(1.0, 1.0, 1.0, 1.0)) + specularLight;
}