definability / mfm

MIT License
1 stars 2 forks source link

Process backface lighting properly #51

Closed definability closed 7 years ago

definability commented 7 years ago

When light source looks on back face of Face model, shadows are processed improperly because there's no difference between back and front pixels distance from the light source. shadows_issue

Maybe it's reasonable to attach a plane or hemisphere to back side of the model.

definability commented 7 years ago

Hack gl_FragDepth

gl_FragDepth hacking depending on gl_FrontFacing doesn't work, because depth test looks at gl_FragDepth and places it to appropriate layer (the highest to the viewport).

Use a texture

Separate texture can be used to store each pixel gl_FrontFacing state during shadow map creation procedure and read during final scene rendering.

Add fake fragments

Connect opposite parts of the back side of the model to hide backface of the face with them.

definability commented 7 years ago

Solution: as long as shadow map camera position is the light vector, which is multiplied by fragment normal vector, if it sees back side of a fragment, this means that cosine of angle between normal and light vector is negative so dot product is.

When the dot product of normal and light vector of a vertex is negative, the vertex is shaded as long as only one light source exists.