Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: OpenGL shader - Missing pixels

  1. #1
    Intern Contributor
    Join Date
    Mar 2012
    Posts
    50

    OpenGL shader - Missing pixels

    I have recently got my shader working, and since then have looked around for a vert+frag shader to work with my texture.

    This shader has been the closest one i can find thats works close to perfect, except it cuts off nearly half of what it is lighting. I don't know why turns half of what it is lighting into black pixels. I'd kill for an answer.

    Here are the shader files:

    .vert
    Code :
        varying vec3 normal, lightDir, eyeVec; 
        varying float att; 
     
        void main() 
        { 
            normal = gl_NormalMatrix * gl_Normal; 
     
            vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex); 
            lightDir = vec3(gl_LightSource[0].position.xyz - vVertex); 
            eyeVec = vVertex;
     
            float d = length(lightDir); 
     
            att = 1.0 / ( gl_LightSource[0].constantAttenuation +  
            (gl_LightSource[0].linearAttenuation*d) +  
            (gl_LightSource[0].quadraticAttenuation*d*d) ); 
     
            gl_Position = ftransform(); 
     
            gl_FrontColor = gl_Color; 
            gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; 
        }

    .frag
    Code :
        varying vec3 normal, lightDir, eyeVec; 
        varying float att; 
     
        uniform sampler2D tex; 
     
        void main (void) 
        { 
            vec4 final_color =  (gl_FrontLightModelProduct.sceneColor * gl_FrontMaterial.ambient) + (gl_LightSource[0].ambient * gl_FrontMaterial.ambient) * att; 
     
            vec3 N = normalize(normal); 
            vec3 L = normalize(lightDir); 
     
            float lambertTerm = dot(N,L); 
     
            if(lambertTerm > 0.0) 
            { 
                final_color += gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * lambertTerm * att; 
     
                vec3 E = normalize(eyeVec); 
                vec3 R = reflect(-L, N); 
     
                float specular = pow( max(dot(R, E), 0.0), gl_FrontMaterial.shininess ); 
     
                final_color += gl_LightSource[0].specular * gl_FrontMaterial.specular * specular * att;  
            } 
     
            gl_FragColor = final_color; 
            gl_FragColor *= texture2D(tex, gl_TexCoord[0].st); 
        }
    And if i change " eyeVec = vVertex; " to " eyeVec = -vVertex; " in .vert, i magically get the other half of my missing lighting.


  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,169

    Re: OpenGL shader - Missing pixels

    You should be adding the diffuse colour contribution regardless of whether lambert term is > 0. Only the Specular is calculated if NdotL is > 0

  3. #3
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948

    Re: OpenGL shader - Missing pixels

    You should be adding the diffuse colour contribution regardless of whether lambert term is > 0.
    What? If N*L is less than zero, then the normal is pointed away from the light. All lighting directly from that light must therefore be blocked (assuming we're talking about a closed surface).

  4. #4
    Intern Contributor
    Join Date
    Mar 2012
    Posts
    50

    Re: OpenGL shader - Missing pixels

    Does anyone figure how i would solve the missing pixels problem?

  5. #5
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,290

    Re: OpenGL shader - Missing pixels

    change:
    Code :
    vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex); 
    lightDir = vec3(gl_LightSource[0].position.xyz - vVertex); 
    eyeVec = vVertex;

    to:

    Code :
    vec4 vVertex = gl_ModelViewMatrix * gl_Vertex; // naturally, here expect gl_Vertex.w=1.0
    lightDir = vec3(gl_LightSource[0].position.xyz - vVertex.xyz);
     
    vec4 eyePos =  gl_ModelViewMatrix * vec4(0,0,0,1);
    eyeVec = vVertex.xyz - eyePos.xyz; // maybe = vVertex.xyz - gl_ModelViewMatrix[3].xyz;

    A quick hack would be to assume eyeVec = vec3(0,0,-1) always


    Note the circular cutoff you see with your light: that's where your eyeVec.z happens to be 0.0f, and changes its sign.

    Edit:
    On a second thought, this might not be the case (and the above won't fix anything); just make eyeVec = vec3(0,0,-1) always.

  6. #6
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,169

    Re: OpenGL shader - Missing pixels

    Quote:You should be adding the diffuse colour contribution regardless of whether lambert term is > 0.

    What? If N*L is less than zero,
    Ah, yes, of course. The reason I always include the term in all my stock shaders is because NdotL never becomes negative in the first place.
    Code :
     float NdotL = max (0.0, dot (normal, Leye) );
    //calc specular lighting
       float pf;
       if (NdotL == 0.0)
       	pf = 0.0;	//intensity
       else
       {
       	float NdotH = clamp(dot(normal, Heye), 0.0, 1.0);
       	pf = pow (NdotH, specularpower);
       }
       vec3 specularlight = vec3 (lightspecularcolor.rgb * specularintensity * pf);
       vec3 diffuselight = 0.8 * NdotL * lightcolour.rgb
       vec3 revserselight = 0.2 * (1.0-NdotL) * lightreversecolour.rgb;

  7. #7
    Intern Contributor
    Join Date
    Mar 2012
    Posts
    50

    Re: OpenGL shader - Missing pixels

    @IIian Dinev

    Changing the code you suggested leaves this result:
    https://www.youtube.com/watch?v=Fp5HUeGaa-8
    and
    Code :
    // maybe = vVertex.xyz - gl_ModelViewMatrix[3].xyz;
    leaves the same result as
    Code :
    = vVertex.xyz - eyePos.xyz;
    ---------------------------------------------
    And
    Code :
    eyeVec = vec3(0,0,-1);
    Renders just the negative portion:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •