# Lambertian Shading of a cube

In theory I can't use a lambertian shader on a cube, because there aren't surface normals on the edges.

Is this correct ?

Why is this always possible in software applications ?

• I'm going to go out on a limb and say that it's because the edges are infinitely thin (which follows from the fact that the flat surfaces on each side of the cube are infinitely thin), therefore they have zero surface area, therefore they don't need normals. – IneQuation Oct 20 '16 at 20:55
• Could you detail what makes you think you cannot use lambertian shading on a cube? – Julien Guertault Oct 21 '16 at 6:04
• @ Julien Guertault I mean that for a mathematical cube there aren't normals on the edge points. The formula of Lambertian shading needs a surface with normals on every point. I know that software applications use some tricks, but with this view pratical computer graphics is not an approximation of theoric computer graphics. – Valerio Oct 21 '16 at 11:56
• Ah ok. So, pixels are infinitely small sample points on a grid. The edge of a cube is infinitely thin - a line segment. Because of this, we don't often see edges in rendering. When we do, they show up as holes in the cube, z fighting at the edge and similar. In practice though, you aren't ever shading the edge, just the faces. – Alan Wolfe Oct 25 '16 at 1:06

Each cube face is made up of a quad, or two triangles. Let's take 3 points from either the quad, or one of the triangles, and call them $A,B,C$.

You can get the normal to this surface by normalizing the cross product of $\overline{AB}$ and $\overline{BC}$. You can then use that surface normal to calculate lighting.

You can do similar calculations to get the tangent and bitangent to then calculate $u,v$ values for texture mapping.

I'm not sure why the edges not having normals makes you think you can't calculate lighting for the cube faces, but you totally can.

You could even use the vertices to calculate a normal for the edges if you wanted, but I'm not sure why you would want to do that :P

In a polygon mesh, all the faces that meet at a point don't have to share the same vertex. They can each have their own vertex, and those vertices can all have the same position but different normals.

In meshes with smooth edges (such as on a sphere, where you want to hide the edges), you want all the vertices at the same point to have the same normal. Generally this will be close to the average of the normals of the faces that share the vertex. That way, Lambertian shading computes the same colour for each, and you don't get a discontinuity on the edge.

In meshes where you want a deliberate sharp edge (such as your cube example), you want the vertices to have their own normals. In the cube case, each face has its own four vertices, and those vertices all have the same normal (the face normal). Each corner of the cube has three (or six, depending on the triangulation) vertices, with three normals perpendicular to each other. This way, Lambertian shading gives each face the same colour (because all the normals for one face point in the same direction), and adjacent faces don't affect each other.

A mesh with some rounded parts and some sharp edges can have both. Some vertices can be merged (i.e. each triangle meeting at the same point has the same vertex normal) and others can be split (i.e. each triangle meeting at the same point has its own normal for that vertex). One triangle can have some smooth vertices (shared with its neighbours) and some sharp vertices (with the face normal, not an averaged normal). It's best if the vertices on one edge are all the same, otherwise the results look unphysical.

Generally, the 3D modelling tool you use to create the mesh will give you the choice: you can make the whole mesh smooth or sharp, or you can select some edges and make them individually smooth or sharp.