Finding the Normal on a Cylinder

Once you know the points of intersection, the normal vector is used to help shade the surface appropriately. You’ll only need one scenario to cover this bit.

Test #3: Normal Vector on a Cylinder

Show that the normal vector on the surface of a cylinder is computed correctly.

This scenario chooses four points on the surface of the cylinder, one each at +x, -x, +z and -z, and shows that the normal is the expected value at each point.

 Scenario Outline​: Normal vector on a cylinder
 Given​ cyl ← cylinder()
 When​ n ← local_normal_at(cyl, <point>)
 Then​ n = <normal>
 
 Examples​:
  | point | normal |
  | point(1, 0, 0) | vector(1, 0, 0) |
  | point(0, 5, -1) | vector(0, 0, -1) |
  | point(0, -2, 1) | vector(0, 0, 1) |
  | point(-1, 1, 0) | vector(-1, 0, 0) |

To accomplish this, take the point in question and remove the y component. Treating the result as a vector gives you the normal. In pseudocode, it looks like this:

 function​ local_normal_at(cylinder, point)
 return​ vector(point.x, 0, point.z)
 end​ ​function

With those tests passing, your ray tracer can render cylinders! They’ll be infinitely long, which might be a bit unwieldy, but with a bit of imagination you can do all kinds of interesting things with them. Give it a try! When you come back, we will look at truncating those cylinders to make them easier to use.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.44.23