Clipping
We want to portions of objects outside the view frustrum (boundaries of the image plane projected into 3D). This is defined in terms of 6 planes (including a near & far plane). Clipping avoids degeneracy and improves efficiency.
1. When to Clip?
- Before perspective transform in 3D space. This is natural & not too degenerate.
- In homogeneous coordinates after perspective transform (clip space), before perspective divide. This is canonical (independent of camera) & simplest to implement.
- In transformed 3D screen space (problem: objects in the plane of the camera).
2. Halfspaces
A halfspace is defined by a plane. The plane divides space into two halfspaces. A point is inside the halfspace if it is on the plane or on the "inside" side of the plane.
A plane equation is
2.1 Point to Plane Distance
We can scale
When clipping, we don't need to normalize, but we will get an incorrect magnitude for
3. Clipping with View Frustrum
So, we test
4. Clipping Lines
To intersect a line & plane, we use the parametric line euqation
- For any vector
in the plane , let the intersection point be . - A vector in that plane is given by
. - So,
. - Solve for
and find the point of intersection.
Take two points either side of the plane that make a line,
- If
and then clip to plane. - If
and then clip to plane. - If
and then discard line. - If
and then keep line.
These operations are implemented in hardware, so we don't need to worry about efficiency.
5. Clipping Objects
Clipping can be carried out against any objects. We can develop containment tests for convex objects (common problem, e.g. convex polyhedra) and concave objects.
5.1 Convex Objects
A convex object satisfies:
- A line joining any two points on the boundary lies inside the object.
- The object is the intersection of planar halfspaces.
To test if an object is convex:
1convex = true; 2for each face F of object { 3 find plane equation of face: F(x,y,z) = 0; 4 choose point (x_i,y_i,z_i) not on the face; 5 6 for all other points of the object { 7 if (sign(F(x_i,y_i,z_i)) != sign(F(x,y,z))) { 8 convex = false; 9 break; 10 } 11 } 12}
The same test can be expressed in vector form, avoiding the need to calculate the Cartesian equation of our plane. To do this, we store normal vector
To find the normal vector of a face, we can take the cross prouct of two edges of the face (or any vector in the plane).
We must take care to ensure the normal vector points outwards from the object. We can do this by ensuring the vertices of the face are ordered counter-clockwise when viewed from outside the object.
To check this direction, we need to look at the rest of the object.
5.2 Concave Objects
Containment and clipping for concave objects uses the ray containment test. This works by finding all intersections between a ray and polygon edges (faces). If the number of intersections is odd, the point is contained.
To calculate intersections, we define a ray as
- Compute the intersection of the ray with the plane of each face.
- If
, check whether the intersection point is contained in the face (not trivial, but can be reduced to 2D under orthographic projection).
To clip to concave volumes, we may need to split the lines & volumes into segments & concave parts. Usually, concave volumes are unused.