View Full Version : Per-pixel shading, but still artifacts

03-24-2010, 07:32 AM
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.
http://www.imagebanana.com/img/6eg5xv3u/shading_w_normals.PNG http://www.imagebanana.com/img/gocvzi8/shading_w_normals4.PNG

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;

03-24-2010, 10:27 AM
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;

03-24-2010, 01:57 PM
No that doesn't help.

Nicolas Lelong
03-24-2010, 03:50 PM
Do your normals look discontinuous if you display them in false colors ?

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

03-24-2010, 05:05 PM
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.

03-24-2010, 08:44 PM
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.

03-25-2010, 03:09 AM
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.

03-25-2010, 04:16 AM
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.