The POV-Ray Cyclopedia

Mike Weber posted a question on povray.general about smooth triangles and normals. This page should explain how normals work in smooth triangles. I will be leaning heavily on an explanation found at Warps Mesh Smoother.

Part One
Setting up a triangle

In this first section we will explore how three different normal vectors at each vertice of the triangle effect the normal vector on different points on each triangle. The next section will explore smoothing several triangles in a mesh.

To the right we see a simple triangle and three individual vectors

#declare p1 = <1.5,0,0>;
#declare p2 = <-1.5,0,-1>;
#declare p3 = <-1,0,1.5>;

#declare v1 = vnormalize(<0.25,1,0>);
#declare v2 = vnormalize(<-0.15,0.75,0.5>);
#declare v3 = vnormalize(<0,0.5,-0.5>);

smooth_triangle { p1,v1,p2,v2,p3,v3 
                  pigment { rgb <1,0,0> } 
                  finish { specular 1 } }

cylinder { p1, (p1+v1) 0.05 pigment { rgb <0,0,1> } finish { specular 1 } }
cylinder { p2, (p2+v2) 0.05 pigment { rgb <0,0,1> } finish { specular 1 } }
cylinder { p3, (p3+v3) 0.05 pigment { rgb <0,0,1> } finish { specular 1 } }

Notice how the triangle is highlighted. The far corner has a normal vector that points towards the light, so it is higlighted, but the closer corner to the left is creating a shadow because it points away from the light. (The light is at <-30,30,-30>.)

Now we need to check random points on the triangle, calculate their normals, and display them. Here is a bit more code:
#declare a = vlength(p1-p2);
#declare b = vlength(p2-p3);
#declare c = vlength(p3-p1);

#declare cnt = 0;
#declare sd = seed(427);

#while (cnt < 30)
  #declare q = p1 + rand(sd)*(p3-p1);
  #declare q = q + rand(sd)*(p2 - q);
  #declare q1 = vlength(p1 - q);
  #declare q2 = vlength(p2 - q);
  #declare q3 = vlength(p3 - q);
  #declare qn = vnormalize(v1/q1 + v2/q2 + v3/q3);

  cylinder { q, (q + qn) 0.03 pigment {rgb <0,1,0>} finish { phong 1 } }

  sphere { q 0.07 pigment { rgb <0,1,0> } finish { phong 1 } }
  #declare cnt = cnt + 1;
#end
This sloppy bit of code is probably not accurate, but it seems to work.
Circular clump
1. A Smooth Triangle
Circular clump
2. Some calculated normals
Circular clump
3. More calculated normals

Send feedback, or head back to the Cyclopedia.

Thanks for watching.