Fragment shader inexplicable bahaviour

前端 未结 1 2039
无人及你
无人及你 2021-01-26 04:02

I have written a C++ program where I draw a teapot and apply lighting. It is itself simple, but I also use shaders. Simple I\'m new with GLSL I just tried a simple fragment shad

相关标签:
1条回答
  • 2021-01-26 04:51

    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;
    }
    
    0 讨论(0)
提交回复
热议问题