dilevin / computer-graphics-shader-pipeline

Computer Graphics Assignment about the Shader Pipeline
2 stars 5 forks source link

Phong shading reflecting vertices #6

Closed ACalza closed 5 years ago

ACalza commented 5 years ago

This was asked during office hours (and a few others had a similar issue) and it's in regards to the shading for 'snap to vertices.' The shading as the image depicted below appears to look like Gourand shading, and want to know if this is a common issue, and if it's fine to leave it as is. Gavin had that same issue too lol.

image
MallocPower commented 5 years ago

I also noticed the same issue. However, after messing around with the code, I discovered that if you set the tessellation level to a higher number (eg. 50 instead of 5), the result would be a smooth appearance.

tess

Would the issue be caused by the fact that the stated hard code tessellation level is too low to produce a smooth surface?

rin-23 commented 5 years ago

Using tesselation level 5 should produce smooth shading. Make sure you are normalizing your vectors if you expect them to be unit length.

ACalza commented 5 years ago

The vectors are normizalized. Can we see a screenshot ?

rin-23 commented 5 years ago

The screenshot(gif) you see in your assignment was made with level 5.

MallocPower commented 5 years ago

I just noticed that I normalized the normal vector yet I passed in the raw value (d'oh!). Confirmed to have worked at level 5.

ACalza commented 5 years ago

I normalized the vector in the tessellation step, but I had to normalize it in the lit.fs file for it to work....Weird....

rin-23 commented 5 years ago

That's expected. When vectors are normalized in vertex/tesselation shader and then interpolated to generate new vectors in the fragment shader(per-pixel), those new vectors don't necessarily stay unit length. It's one of those gotcha things of shader programming. Rule of thumb is to always normalize vectors in fs if they are suppose to be unit length.

rarora7777 commented 5 years ago

Consider an extreme example: if the normals at your vertices were (1, 0, 0), (0, 1, 0), and (0, 0, 1), what would be the interpolated normal at the centroid?

One might question why OpenGL does not automatically take care of the normalization? And the answer to that question is very simple: OpenGL does not know that your variable is actually a normal vector (or any other direction vector for that matter).