Fragment shader inexplicable bahaviour

假如想象 提交于 2019-12-02 11:50:55

First off all you are mixing the fixed function pipeline with "the newer stuff". This is a real bad practice because it could couse a lot of issues because you don't really know what is going on in background.

If you want to have real lighting with diffuse shaders you have to calculate the diffuse color on your own. It's a long time ago i used the ffp the last time so i searched for some shaders wich use it:

Vertex-Shader

varying vec3 normal;
varying vec3 v;
varying vec3 lightvec;
void main(void)
{
    normal      = normalize(gl_NormalMatrix * gl_Normal);
    v           = vec3(gl_ModelViewMatrix * gl_Vertex);
    lightvec    = normalize(gl_LightSource[0].position.xyz - v);
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

Fragment-Shader

varying vec3 normal;
varying vec3 v;
varying vec3 lightvec;

void main(void)
{
   vec3 Eye          = normalize(-v);
   vec3 Reflected    = normalize( reflect( -lightvec, normal )); 
   vec4 IAmbient     = gl_LightSource[0].ambient * gl_FrontMaterial.ambient;
   vec4 IDiffuse     = gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * max(dot(normal, lightvec), 0.0);
   vec4 ISpecular    = gl_LightSource[0].specular * gl_FrontMaterial.specular * pow(max(dot(Reflected, Eye), 0.0), gl_FrontMaterial.shininess);
   gl_FragColor      = gl_FrontLightModelProduct.sceneColor + IAmbient + IDiffuse + ISpecular;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!