0
\$\begingroup\$

It's the first time I'm trying to implement the Phong lighting model. I'm pretty sure everything is working fine.

I was experimenting using different materials, meaning I played with Kd,Ks,Ka and Ns values when I came across this problem.

Whenever I use a material with alpha value less than one I get this weird result :

enter image description here

At first I thought it has to do with the normals of the model since they are all supposed to point away from the model but it doesn't seem to be the case.

Any ideas?

\$\endgroup\$
1
\$\begingroup\$

This looks like what happens when you have depth writing enabled on a translucent material.

Sometimes a polygon in front (from our camera's perspective) renders first, and writes its depth into the depth buffer. Then when a polygon behind tries to render later, it detects there's already something at a closer depth and doesn't draw anything. That's where you get the faint/translucent parts.

Other times, one or more polygons further back draw first, then later the frontmost polygon draws on top of them. Stacking up multiple translucent polygons like this makes the result more opaque, leading to the darker regions in your image.

Since polygons are not depth-sorted by default, you end up falling into either of these two cases basically at random, leading to the patchy/inconsistent appearance you see here.

You can make this more consistent in a couple of ways:

  1. Turn off depth writes. Then every front-facing polygon will be drawn, even if a closer polygon already got drawn there. This works for order independent blend modes like Add or Multiply. If your blending function is not order-indepent though (like Over/Layer/standard alpha blending), you'll still see inconsistent blending results where the order changes.

  2. Sort your polygons — either front to back to render only the frontmost skin, or back to front to layer everything in the correct order. Note that not every possible arrangement of 3D polygons is sortable though.

  3. Render your object fully opaque onto an off-screen buffer, then blend that buffer into your scene with translucent blending. This ensures you only see the frontmost surfacr, and get consistent blending everywhere.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.