I'm practicing the Phong lighting model with glsl, and here's my shaders: vertex shader:
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
layout (location = 2) in vec3 aNormal;
out vec2 TexCoord;
out vec3 LightColor;
uniform mat4 model;
layout (std140) uniform coord_mats {
mat4 view;
mat4 projection;
vec3 lightPos;
vec3 lightColor;
};
void main()
{
vec3 vertex_position = vec3(model * vec4(aPos, 1.0));
// vec3 vertex_position = vec3(view * model * vec4(aPos, 1.0));
vec3 vertex_normal = normalize(vec3(model * vec4 (aNormal, 1.0)));
// vec3 vertex_normal = normalize(vec3(view * model * vec4 (aNormal, 1.0)));
vec3 light_position = vec3(vec4(lightPos, 1.0));
// vec3 light_position = vec3(view * vec4(lightPos, 1.0));
vec3 light_dir = normalize(light_position - vertex_position);
float diff = max(dot( light_dir,vertex_normal), 0.0);
vec3 diffuse = diff * lightColor;
vec3 ambient = 0.1 * lightColor;
LightColor = diffuse + ambient;
gl_Position = projection * view * model * vec4(aPos, 1.0);
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}
fragment shader:
#version 330 core
out vec4 FragColor;
in vec2 TexCoord;
in vec3 LightColor;
// texture sampler
uniform sampler2D texture1;
void main()
{
FragColor = vec4(LightColor,1.0) * texture(texture1, TexCoord);
}
The diffuse light works right, that is, faces which are facing to the light source, are bright, when backing the light source, they are dark(basically ambient color).
Now I want to calculate the specular light, in order to get the camera position for free, I changes the coordinate to camera view coordinate(using the commented code, just multiply the view matrix to the vertex position, normal and light position), but weird things happened: all faces which are facing the light is dark, all faces which are backing to the light is bright.
How could this happened?