This is what I'm doing:
vertex shader:
#version 400
layout ( location = 0 ) in vec3 vertex_position;
layout ( location = 1 ) in vec3 vertex_normal;
uniform mat4 ModelViewMatrix; //view*model
uniform mat3 NormalMatrix; //transpose(inverse(modelviewmatrix))) //top 3x3 row of model*view
uniform mat4 priv_mat; //projection*view*model
out vec3 normal;
out vec3 position;
void main()
{
normal=normalize(NormalMatrix*vertex_normal);
position=vec3(ModelViewMatrix*vec4(vertex_position,1.0));
gl_Position=priv_mat*vec4(vertex_position,1.0);
}
fragment shader:
#version 400
in vec3 normal;
in vec3 position;
uniform vec4 LightPosition; // *needs to be in view-space* (view*lightpos)
uniform vec3 LightIntensity;
uniform vec3 Kd; // Diffuse reflectivity
uniform vec3 Ka; // Ambient reflectivity
uniform vec3 Ks; // Specular reflectivity
uniform float Shininess; // Shininess
vec3 ads( ){
vec3 n = normal;
vec3 s = normalize( vec3(LightPosition) - position );
vec3 v = normalize( vec3(-position));
vec3 r = reflect( -s, n );
return
LightIntensity *
( Ka +
Kd * max( dot(s, n), 0.0 ) +
Ks * pow( max( dot(r,v), 0.0 ), Shininess ) );
}
void main()
{
gl_FragColor = vec4(ads(), 1.0);
}
But when I run it, I get this:
Where the light doesn't just come from the wrong spot, but it also doesn't change when I move the planet around, like so
Where the diffuse value somehow gets the wrong direction? The math is beyond me however.
So, the diffuse value is calculated by getting the dot(s,n), where s is (lightposition - position), and position is (view * model * vertex_position). But like you can see, the results are not as expected