views:

701

answers:

2

Given two vectors A and B which form the line segment L = A-B. Furthermore given a view frustum F which is defined by its left, right, bottom, top, near and far planes.

How do I clip L against F?

That is, test for an intersection and where on L that intersection occurs? (Keep in mind that a line segment can have more than one intersection with the frustum if it intersects two sides at a corner.)

If possible, provide a code example please (C++ or Python preferred).

+2  A: 

I don't want to get into writing code for this now but if I understand "frustum" correctly the following should work.

  1. Intersect the Line with all given planes
  2. If you have two intersections you're done.
  3. If you have only one intersection calculate the front plane and intersect.
  4. If you still have only one intersection calculate the back plane and intersect.

But I may have completely misunderstood. In that case please elaborate :)

Corporal Touchy
Hmm. Actually what I want, is for the line segment to __not__ be outside the view frustum.So if I have two intersections, I have to move both A and B to match the intersecting boundaries.Alas I have no idea how that works.
MHOOO
A: 

Adding to what Corporal Touchy said above, you'll need to know how to intersect a line segment with a plane. In the description on that page, u represents the parameter in the parametric definition of your line. First, calculate u using one of the 2 methods described. If the value of u falls in the range of 0.0 to 1.0, then the plane clips the line somewhere on your segment. Plugging u back into your line equation gives you the point where that intersection occurs.

Another approach is to find the directed distance of each point to a plane. If the distance of one point is positive and the other is negative, then they lie on opposite sides of the plane. You then know which point is outside your frustum (based on which way your plane normal points). Using this approach, finding the intersection point can be done faster by doing a linear interpolation based on the ratio of the directed distances. E.g. if the distance of one point is +12 and the other is -12, you know the plane cuts the segment in half, and your u parameter is 0.5.

Hope this helps.