Path: utzoo!attcan!uunet!samsung!zaphod.mps.ohio-state.edu!wuarchive!udel!princeton!phoenix!gauss!markv From: markv@gauss.Princeton.EDU (Mark VandeWettering) Newsgroups: comp.graphics Subject: Re: Ray Tracing in 3D space Message-ID: <14440@phoenix.Princeton.EDU> Date: 12 Mar 90 16:08:31 GMT References: <1174@ccadfa.adfa.oz.au> Sender: news@phoenix.Princeton.EDU Reply-To: markv@gauss.Princeton.EDU (Mark VandeWettering) Organization: Princeton University Lines: 44 >I am trying to do ray tracing of light through a >cylinder coming at differenct angle to the axis >of the cylinder. Could some one give me some >pointers? Ray cylinder intersection is (conceptually) just as easy as hitting a sphere. Most of the problems come from clipping the cylinder so it isn't infinite. I can think of several ways to do this, but first let me mention that you should consult Introduction to Ray Tracing edited by Andrew Glassner. Articles by Pat Hanrahan and Eric Haines go over most of this stuff. Its easist to imagine a unit cylinder fromed by rotating the line x = 1 in the xy plane about the y axis. The formula for this cylinder is x^2 + z^2 = 1. If your ray is of the form P + t D, with P and D three tuples, you can insert the components into the original formula and come up with: (px + t dx)^2 + (pz + t dz)^2 - 1 = 0 or px^2 + 2 t dx px + (t dx)^2 + pz^2 + 2 t dz pz + (t dz)^2 or (px^2 + pz^2) + 2 t (dx px + dz pz) + t^2 (dx^2 + dz^2) which you can then solve using the quadratic formula. If there are no roots, then there is no intersection. If there are roots, then these give two t values along the ray. Figure out those points using P + t D. Now, clipping. We wanted to have a finite cylinder, say within the cube two units on a side centered at the origin. Well, gosh, ignore any intersections that occur outside this box. Then take the closest one. Now, to intersect with an arbitrary cylinder, work up a world transformation matrix that transforms points into the objects coordinate system. Transform the ray origin and direction, and voila. You do need to be careful to rescale t appropriately, but its really not all that hard. You might instead want to implement general quadrics as a primitive, or choose any one of a number of different ways of doing the above. Homogenous coordinates might make this simpler actually.... Hmmm.... And there is a geometric argument that can also be used to derive algorithms like this. Think about it. It shouldn't be that difficult. Mark