Per-pixel shading, but still artifacts

I have implemented a simple per-pixel shading but I get surprising (for me) shading artifacts.

The rendered objects show shading artifacts I would expect in per-vertex shading but not per-pixel shading. Or are these artifacts normal for PP-shading? The vertex-normals look good to me.

The the vertex shader:


varying vec3 v_normal;
void main(void)
{
    v_normal = normalize(gl_NormalMatrix * normalize(gl_Normal));
    gl_FrontColor = gl_Color;
    gl_Position = ftransform();
}

The relevant fragment shader code:


varying vec3 v_normal
...
vec3 normal = normalize(v_normal);
float nDotVP = max(0.0, dot(normal, gl_LightSource[light].position.xyz));    
vec4 ldiffuse = gl_LightSource[light].diffuse * nDotVP;
gl_FragColor = color * ldiffuse; // + lambient + lspecular;

Indeed, this is quite surprising.

Try with gl_FrontColor = vec4(1.0,1.0,1.0,1.0); to rule out any problem with the vertex color.

And does it looks very different if you skip re-normalization of linearly interpolated normal in FS :
vec3 normal = v_normal;

No that doesn’t help.

Do your normals look discontinuous if you display them in false colors ?

gl_FragColor = vec4( normal*0.5+vec3(0.5), 1.0 );

Output looks perfectly normal (!) to me, especially if you are dealing with directional lights. Per pixel renormalization is not a valid solution in this case, you’d better go for mesh decimation and normal mapping.

Looks correct to me. Take a look at the tallest normal that doesn’t touch the top of the image. It’s almost perpendicular to the triangles it’s shading so even if you interpolate the normal, it’s going to be dark around that area. It’s not your lighting.

strattonbrazil, nullptr - the more I think about it, the more I agree with you. It is probably caused by the asymmetry of the normals. I wonder how I can get smooth shading under such conditions. Maybe I should weight the face normals before I average them to vertex normals. I’ll try that.

There are two things wrong here

  1. it’s mostly the geometry of the object that messes things up, if it’s supposed to be smooth then you have to make sure it is in you modeler application.

  2. when averaging normals together you also have to selectively discard certain normals based on the angle, like if lets say the normal points more than 90 degrees away from the original vector.
    It will create sharp edges but it does create more sane lighting.