Karnivaurus
Karnivaurus

Reputation: 24131

OpenGL illuminating opposite faces with the same normal

Using OpenGL shaders, I want to render a solid cube using diffuse lighting. To do this, I compute the normal of each vertex, and then compute the dot product between this normal and the direction of the light. The problem with this is that each pair of flat surfaces on the cube has the same normal direction, and therefore the same lighting intensity. But if the light source is directly above the cube, then the top surface should be bright, and the bottom surface should be dim -- they should not be the same.

What is the solution?

Upvotes: 0

Views: 240

Answers (1)

BDL
BDL

Reputation: 22176

The normal on top of the cube should have the opposite direction than the one on the bottom. Due to this, the result of the dot product will be 1 for the upper side and -1 for the bottom plane. If you clamp the dot-product to the [0, 1] range, the bottom side will be dark.

Upvotes: 1

Related Questions