I'm building a 3D scene in OpenGL 2.1 and illuminating it using a directional light with the Phong lighting model.
Up close everything seems to work fine but, when the camera gets away from the models they lose all the lighting (except ambient).
What could make this happen?
These is the vertex shader:
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;
uniform mat4 projectionMatrix;
uniform mat3 normalMatrix;
uniform vec3 lightDir;
out vec3 normal;
out vec3 lightDir_viewSpace;
out vec3 vertexPos_viewSpace;
void main(){
normal = normalize( normalMatrix * gl_Normal );
gl_Position = projectionMatrix * viewMatrix * modelMatrix * gl_Vertex;
vertexPos_viewSpace = - ( viewMatrix * modelMatrix * gl_Vertex ).xyz;;
lightDir_viewSpace = normalize( viewMatrix * vec4(lightDir, 1) ).xyz;
}
And here is the fragment shader:
uniform vec3 Ka;
uniform vec3 Kd;
uniform vec3 Ks;
uniform float Shininess;
in vec3 normal;
in vec3 lightDir_viewSpace;
in vec3 vertexPos_viewSpace;
float getdiffuseIntensity( vec3 N, vec3 L ){
float intensity = clamp(dot(L , N), 0.0, 1.0);
return intensity;
}
float getSpecularIntensity( vec3 N, vec3 L, vec3 vertexPos, float shine ){
vec3 R = normalize( reflect( -L, N ) );
vec3 V = normalize( vertexPos );
float intensity = 0.0;
if ( dot(N, L) > 0.0 ){
float cosVR = clamp( dot(V, R), 0.0, 1.0 );
intensity = pow( cosVR, shine );
}
return intensity;
}
void main(){
vec3 normalNorm = normalize( normal );
vec3 lightDirNorm = normalize( lightDir_viewSpace );
vec3 vertexDirNorm = normalize( vertexPos_viewSpace );
vec3 ilumAmbi = Ka;
vec3 ilumDiff = Kd * getdiffuseIntensity( normalNorm, lightDirNorm );
vec3 ilumEspec = Ks * getSpecularIntensity( normalNorm, lightDirNorm, vertexDirNorm, Shininess );
gl_FragColor = vec4( ilumAmbi + ilumDiff + ilumEspec , 1.0 );
}
Also in case someone asks; Yes, this is a school project
Am I doing something wrong?