我正在 OpenGL 2.1 中构建一个 3D 场景,并使用带有 Phong 照明模型的定向光来照亮它。
近距离看起来一切正常,但是当相机远离模型时,它们会失去所有照明(环境除外)。
什么能让这一切发生?
这些是顶点着色器:
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;
}
这是片段着色器:
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 );
}
万一有人问;是的,这是一个学校项目
我做错了吗?