alecjacobson / computer-graphics-shader-pipeline

Computer Graphics Assignment about the Shader Pipeline
14 stars 22 forks source link

normal_fs_in value changed? #48

Open aiwen324 opened 4 years ago

aiwen324 commented 4 years ago

Hi, I just encountered a weird thing. I normalize the normal_fs_in in the snap_to_sphere.vs, which is the normal vector of the transformed interpolated vertex in view coordinate system. Then when I was doing lit.fs, I am using blinn-phong in the view coordinate system, and I assumed the normal vector which is normal_fs_in should've been already normalized and it's good to go. However, the result has obvious artificial effect similar as #14 , #24 . And if I do normalize(normal_fs_in) in lit.fs again, I could get a smooth lighting as we expected. Why this happens? Here is the result with normalize in lit.fs image Here is the result without normalize in lit.fs image Both of them have normal_fs_in normalized in snap_to_sphere.vs Thanks

abhimadan commented 4 years ago

OpenGL linearly interpolates fragment shader inputs using the barycentric coordinates of the point being rendered. The normalized values you set in the tessellation shader are only at the vertices, and the linear interpolation isn't guaranteed to preserve its length (and generally won't except for flat surfaces).

aiwen324 commented 4 years ago

Wait, so interpolate(gl_TessCoord, v1, v2, v3 only calculates those tessellated points and fragment shader can actually deal with the points that are not even in tessellated points? The fragment shader interpolates those points again?

abhimadan commented 4 years ago

The tessellation shader subdivides your input data and creates new vertices. On the other hand, the fragment shader computes the actual colour at a pixel based on the geometry visible from the eye. In general, this won't be exactly at a vertex, but rather at a point in the middle of a face.

To understand why we do this interpolation, think back to a5. The whole motivation behind assigning normals to vertices was to ensure that we could smoothly assign a normal across a flat face in order to make it appear round when we computed the colour.