# 【转】3D引擎中的剪切算法

摘自： ttp://www.cs.berkeley.edu/~ug/slide/pipeline/assignments/as6/discussion.shtml

**University of California EECS Dept, CS Division **

TA: Laura Downs

TA: Jordan Smith **CS184: Foundations of Computer Graphics **

Prof. Carlo H. Séquin

Spring 1998

### CS 184: Discussion Section 7 Notes

#### Transforms in the Hierarchy (an Example)

Suppose I have a scene hierarchy as shown at the right. The top level node, or world node, is a translated, scaled box. The bottom level node, or object node, is a box with sides of length and one corner at the origin.

The **Q _{world<-object}** transform is the transformation that computes the

**world**coordinates of a point given in

**object**coordinates, i.e. (in column vectors)

**Q**

_{world<-object}**p**=

_{object}**p**. In this example, if I take the point,

_{world}**p**=(0 1 1), on one corner of the original box, and apply the two modeling transforms, the world coordinates of that point,

_{object}**p**, should be (1 4 3).

_{world}In the figure, the **world** coordinate system is system **A** and the **object** coordinate system is system **C**. Therefore, our **world<-object** transform will be the same as the **A<-C** transform, i.e. **Q _{world<-object}**=

**Q**,

_{A<-C}**p**=

_{object}**p**, and

_{C}**p**=

_{world}**p**.

_{A} **Q _{A<-B}**

`T(1 2 1)`

<-----

**Q _{B<-A}**

`T(-1 -2 -1)`

----->

**Q _{B<-C}**

`S(2 2 2)`

<-----

**Q _{C<-B}**

`S(1/2 1/2 1/2)`

----->

We can't get the **Q _{A<-C}** transform directly, but we can construct it from the individual transforms in the hierarchy. We know

**Q**and

_{A<-B}**Q**;

_{B<-C}**Q**is the translation,

_{A<-B}`T(1 2 1)`

and **Q**is the scale,

_{B<-C}`S(2 2 2)`

. Now we have enough information to compute the complete **world<-object**transform,

**Q _{world<-object}**

**p**=

_{object}**Q**

_{A<-C}**p**=

_{C}**Q**

_{A<-B}**Q**

_{B<-C}**p**=

_{C}`T(1 2 1)`

`S(2 2 2)`

**p**

_{C}**Q _{world<-object}** =

`T(1 2 1)`

`S(2 2 2)`

=1

0

0

1

0

1

0

2

0

0

1

1

0

0

0

1

2

0

0

0

0

2

0

0

0

0

2

0

0

0

0

1

=

2

0

0

1

0

2

0

2

0

0

2

1

0

0

0

1

We also need to know the inverse to that matrix, the **Q _{object<-world}** matrix. Rather than invert the

**Q**matrix explicitly, we can keep track of the inverse as we move down the hierarchy.

_{world<-object}**Q _{world<-object}**

^{-1}=

**Q**=

_{object<-world}**Q**=

_{C<-A}**Q**

_{C<-B}**Q**=

_{B<-A}`S(1/2 1/2 1/2)`

`T(-1 -2 -1)`

. #### Camera Transform

The *camera transformation* computes the coordinates of a point in space relative to the camera. This coordinate system is called the View Reference Coordinates (VRC). This transformation is desirable because camera projections are very easy to compute in this coordinate system.

In the example shown on the right, we have a scene hierarchy containing an object and a camera. Each occurs deep within the hierarchy and is affected by a sequence of transformations. We want to find the **camera<-object** transformation.

We already know how to find the **world<-object** transformation that transforms the points in an **object**'s coordinate system into points in the **world** coordinate system. This is just the concatenation of all of the transforms along the path from the **world** to the **object**.

**p _{world}** =

**Q**

_{world<-object}**p**=

_{object}**O**

_{1}O_{2}...O_{m}**p**

_{object}**Q**=

_{world<-object}**O**

_{1}O_{2}...O_{m}We also know how to compute the inverse of this matrix.

**Q _{object<-world}** =

**Q**

_{world<-object}^{-1}= [

**O**]

_{1}O_{2}...O_{m}^{-1}=

**O**

_{m}^{-1}...O_{2}^{-1}O_{1}^{-1}

We keep track of both of these matrices as we traverse the scene hierarchy. Each time a transformation is encountered in the traversal, we concatenate it with the **Q _{world<-object}** matrix and we concatenate its inverse with the

**Q**matrix.

_{object<-world}The **camera<-object** transform is computed from the **camera<-world** and **world<-object** transforms.

**p _{camera}** =

**Q**

_{camera<-object}**p**=

_{object}**Q**

_{camera<-world}**Q**

_{world<-object}**p**

_{object}When we want to render the scene from a particular camera, we first find the **world<-camera** and **camera<-world** transforms by traversing the path from the **world** node to the **camera** node.

**Q _{world<-camera}** =

**C**

_{1}C_{2}...C_{n}**Q**=

_{camera<-world}**Q**

_{world<-camera}^{-1}= [

**C**]

_{1}C_{2}...C_{n}^{-1}=

**C**

_{n}^{-1}...C_{2}^{-1}C_{1}^{-1}

Then, we start at the root node of the scene graph and use the **camera<-world** transform as the initial modeling transform. Now, all points will be automatically transformed into the camera's frame of reference by the modeling transform.

#### The **lookat** transformation

The standard camera placement transformation is represented in

*GLIDE*by the

**lookat**transformation. Although the most common use for this transformation is to place a camera at one point in space looking at another point in space, it can also be used to place any object or light in the scene. The

**lookat**transformation is particularly useful for pointing a spotlight at something in the scene.

The **lookat** transformation specifies a new coordinate system, **(u,v,n)** within the current system by

- the origin of the new system (
**eye**), - a point that will lie on the -
*n*-axis (**vrp**), - and an up vector that should project onto the
*v*-axis in the*uv*-plane (**up**).

In order to compute the transformation matrix for the **lookat** transform, we must first find the orthonormal coordinate axes **(u,v,n)** and the origin **c**.

**c**=**eye****n**=**c**-**vrp**/ |**c**-**vrp**|**u**=**up**x**n**/ |**up**x**n**|**v**=**n**x**u**

**up**vector is parallel to the vector between the

**eye**and the

**vrp**, then the

**u**and

**v**axes will be degenerate. In this case, do whatever you like with

**u**and

**v**, so long as

**(u,v,n)**is orthonormal, since the operation is not well defined.

Now that we have the necessary information: the origin, **c**, and the coordinate axes, **(u,v,n)**, we can compute the **lookat** transformation, **Q _{lookat}**. The matrix,

**Q**, will transform points in the camera's or object's system into the world system just like any other modeling transform.

_{lookat}Let's call **p _{uvn}**=(p

_{u}p

_{v}p

_{n}) and

**p**=(p

_{xyz}_{x}p

_{y}p

_{z})

**p _{xyz}**=

**Q**

_{lookat}**p**

_{uvn}**p**=

_{xyz}**c**+ (p

_{u}

**u**+ p

_{v}

**v**+ p

_{n}

**n**)

**p _{xyz}** =

`T(`**c**)

**Q**

_{xyz<-uvn}**p**=

_{uvn}`T(`**c**)

u_{x}

v_{x}

n_{x}

0

u_{y}

v_{y}

n_{y}

0

u_{z}

v_{z}

n_{z}

0

0

0

0

1

p_{u}

p_{v}

p_{n}

1

Note that **Q _{xyz<-uvn}** is an orthogonal matrix (

**Q**

_{xyz<-uvn}^{-1}=

**Q**

_{xyz<-uvn}^{T}). So it is easy to find

**Q**=

_{uvn<-xyz}**Q**

_{xyz<-uvn}^{-1}.

so we get **Q _{lookat}** =

`T(`**c**)

**Q**where

_{xyz<-uvn}**Q**=

_{xyz<-uvn}u_{x}

v_{x}

n_{x}

0

u_{y}

v_{y}

n_{y}

0

u_{z}

v_{z}

n_{z}

0

0

0

0

1

and **Q _{uvn<-xyz}** =

u_{x}

u_{y}

u_{z}

0

v_{x}

v_{y}

v_{z}

0

n_{x}

n_{y}

n_{z}

0

0

0

0

1

We also need to find **Q _{lookat}**

^{-1}, which we can do in a straightforward fashion:

**Q _{lookat}**

^{-1}= (

`T(`**c**)

**Q**)

_{xyz<-uvn}^{-1}=

**Q**

_{xyz<-uvn}^{-1}

`T(`**c**)

^{-1}=

**Q**

_{xyz<-uvn}^{T}

`T(`**c**)

^{-1}=

**Q**

_{uvn<-xyz}`T(-`**c**)

#### Normalizing Transform

We start out with some arbitrary view frustum or parallelepiped as defined by the **frustum** parameter in the *GLIDE* **camera** statement. We need to transform this volume into a canonical volume so that we can clip our polygons against standard clipping bounds and apply a standard projection to their vertices. The normalizing operation is slightly different for the parallel and perspective projections.

##### Parallelepiped Normalization

We start with a parallelepiped as defined by the **frustum**, *(x _{min}, y_{min}, z_{min})*

*(x*. The

_{max}, y_{max}, z_{max})*(x*and

_{min}, y_{min})*(x*pairs describe the rectangular cross-section in the

_{max}, y_{max})*z*=-1 plane. The

*z*and

_{min}*z*values describe the back and front planes. The direction of projection (

_{max}**dop**) is from the origin to the midpoint of that rectangle.

First we have to shear the volume so that all of the boundaries are axially aligned and the **dop** lies along the -*z*-axis. Let *y _{m}* = (

*y*+

_{min}*y*)/2 and

_{max}*x*= (

_{m}*x*+

_{min}*x*)/2. Then our shear matrix is:

_{max}1

0 *x _{m}*

0

0

1 *y _{m}*

0

0

0

1

0

0

0

0

1

Next, we translate the volume so that the front clipping plane corresponds to the *xy*-plane. This involves just a translation by -*z _{max}* in

*z*,

`T(0,0,-`*z*_{max})

Finally, we scale the volume non-uniformly to match the bounds:

-1 <= *x* <= 1,

-1 <= *y* <= 1,

-1 <= *z* <= 0.

with the scale transformation: `S(1/(`

*x _{max}*-

*x*), 1/(

_{m}*y*-

_{max}*y*), 1/(

_{m}*z*-

_{max}*z*))

_{min}##### Frustum Normalization

We start with a frustum as defined by the **frustum**, *(x _{min}, y_{min}, z_{min})*

*(x*. The

_{max}, y_{max}, z_{max})*(x*and

_{min}, y_{min})*(x*pairs describe the rectangular cross-section in the

_{max}, y_{max})*z*=-1 plane. The

*z*and

_{min}*z*values describe the back and front planes.

_{max}First we have to shear the volume so that the center of the frustum lies along the -*z*-axis. Let *y _{m}* = (

*y*+

_{min}*y*)/2 and

_{max}*x*= (

_{m}*x*+

_{min}*x*)/2. Then our shear matrix is:

_{max}1

0 *x _{m}*

0

0

1 *y _{m}*

0

0

0

1

0

0

0

0

1

Then, we scale the volume non-uniformly to match the bounds:

*z* <= *x* <= -*z*, *z* <= *y* <= -*z*,

-1 <= *z* <= *a*,

for some value of *a*<0. First we scale *x* and *y* so that the bounds of the frustum lie in the *x=z*, *x=-z*, *y=z*, *y=-z* planes: `S(1/(`

*x _{max}*-

*x*), 1/(

_{m}*y*-

_{max}*y*), 1)

_{m}Then we scale the whole frustum uniformly by -1/

*z*so that the back clipping plane lies in the

_{min}*z*=-1 plane.

`S(-1/`*z*_{min}, -1/*z*_{min}, -1/*z*_{min})

which gives us a value for

*a*of

*a*= -

*z*/

_{max}*z*.

_{min}#### Perspective Projection

Now, we are in a normalized space oriented with the camera sitting at the origin, pointing down the -*z*-axis. If we take any point in the scene, say **p**=(*p _{x}*,

*p*,

_{,y}*p*), we want to find the projection of that point onto the image plane,

_{z}*z*=-1. This projection is the intersection of the ray starting at the origin and passing through the point,

**p**.

We can find the coordinates of that point using an old trick from geometry, similar triangles. The triangle shown in dashed lines in the picture has height=*p _{y}* and width=-

*p*. The triangle formed by the projection ray, the image plane and the -

_{z}*z*-axis has width=1. So, by similar triangles, the height of that triangle is

*p*/(-

_{y}*p*).

_{z}This value, *p _{y}*/(-

*p*), is the projected

_{z}*y*-value of the point. Similarly, the projected

*x*-value is

*p*/(-

_{x}*p*). This non-linear operation can be performed in homogeneous coordinates by allowing a

_{z}*w*-value that is not 1. We simply apply this operation:

*w' = -z*

Unfortunately, this operation is not invertible because the new *z* and *w* values are linearly dependent. Also, after the homogeneous normalization (division by *w*) has been applied, all of the *z* values will be -1. In order to maintain *z* ordering, we need to transform *z* to some other value. We will use the value:

*z' = (z-a) / (1+a)*

where *a* is the value of the front *z* clipping plane after the frustum normalization. We pick this value because this transformation will map our canonical view volume into the normalized half cube after the homogeneous division has been applied

-1 <= *x* <= 1,

-1 <= *y* <= 1,

-1 <= *z* <= 0.

The transformation matrix that applies this perspective transformation is:

**Q _{proj<-vrc}** =

1

0

0

0

0

1

0

0

0

0

1/(1+a)

-a/(1+a)

0

0

-1

0

(as given in Foley and vanDam on p. 275) and its inverse is

**Q _{proj<-vrc}**

^{-1}=

1

0

0

0

0

1

0

0

0

0

0

-1

0

0

-(1+a)/a

-1/a

##### Projections in FlatLand

Visualizing the effect of a 4D transformation is pretty tough, so here is an analogous 3D transformation. The*x*-coordinate in our regular space is transformed just like the

*y*-coordinate is here.

Here we have a homogeneous 2D space with no *x*-coordinate. The figure to the right shows the normalized frustum in the *w*=1 plane just before the perspective transformation (warp). The figures below show the *(y,z)* view volume after its perspective warp.

The transformed homogeneous plane viewed along the *y*-axis

The transformed homogeneous plane viewed along the *z*-axis

The transformed homogeneous plane viewed along the *w*-axis

The transformed homogeneous *(y,z,w)* plane

#### Back-Face Culling in Homogeneous Coordinates

We started out with a plane equation*N*in the object coordinate system, with

^{.}P = 0*N = [a b c d]*.

For a parallel projection: Once we are in the normalized half-space, a plane is front facing if the *z*-component of the normal is positive, i.e. *c > 0*.

For a perspective projection: Once we are in the normalized frustum, a plane is front facing (its normal points towards the camera/origin) if *d > 0*. After we have applied the perspective warp to the plane equation (**Q _{proj<-vrc}**

^{-1})

^{T}

*N*, we have a new value

*c' = -(1+a)d/a*. So, if this value is positive, (

*c' > 0*), then we have

*-(1+a)d/a > 0*and thus

*d > 0*.

Therefore, we only have to check the value for *c* in the final plane equation after perspective warp to determine whether a plane faces the camera and we do *not* have to homogenize the plane equation. Homogenizing the plane equation can reverse the direction of the normal and lead to incorrect culling.

#### Clipping in Homogeneous Coordinates

Once we have performed the above normalizations and the perspective projection, we have this set of clipping bounds:-w <= x <= w

-w <= y <= w

-w <= z <= 0

This set of bounds will work for both the parallel and the perspective projection cases.

In the parallel case, *w*=1, so the bounds become:

-1 <= x <= 1

-1 <= y <= 1

-1 <= z <= 0

In the perspective case, *w*=-*z*, so the *x* and *y* bounds become:

z <= x <= -z

z <= y <= -z

The *z* bounds come from the new *z* value: *z' = (z-a) / (1+a)*. At the front clipping plane, *z=a*, we now have *z'=0* and at the back clipping plane, *z=-1*, we now have *z'=-1* but we also have *w*=1 so *z'*=*-w*

Clipping against these planes is nearly as easy as clipping against the axially aligned planes. For example, suppose we are clipping two points, **p _{1}**=(

*x*,

_{1}*y*,

_{1}*z*) and

_{1}**p**=(

_{2}*x*,

_{2}*y*,

_{2}*z*) against the

_{2}*x=w*plane. We have already determined that the two points lie on opposite sides of the plane (i.e. for one of them

*x>w*and for the other

*x*).

We want to find the intersection point:

This gives us the formula

We want to find the intersection point:

**p**=

_{t}**p**+ t(

_{1}**p**-

_{2}**p**) with

_{1}*x*=

_{t}*w*.

_{t}This gives us the formula

*x _{1}* + t(

*x*-

_{2}*x*) =

_{1}*w*+ t(

_{1}*w*-

_{2}*w*)

_{1}*x*-

_{1}*w*= t[(

_{1}*w*-

_{2}*w*) - (

_{1}*x*-

_{2}*x*)]

_{1}t = (

*x*-

_{1}*w*)/ [(

_{1}*w*-

_{2}*w*) - (

_{1}*x*-

_{2}*x*)]

_{1}Because this parameter, *t* is computed from coordinates that are all linearly derived from the initial, object space, coordinates of the two points, it can be used to correctly compute interpolated values along the edges of the polygon. If the points were normalized (divided through by *w*) before the clip, the parameter value would *not* be the correct value. For example, consider the midpoint in screen space of a line that starts very close to the viewer and ends very far away. The visual midpoint will be much closer to the near end of the line than to the far end of the line in object space although the homogenized parameter value would be *t*=0.5.

#### Mental Gymnastics

Consider the following scene: You are sitting on a ping pong table that is 8 feet long (in y) and 4 feet wide (in x). This table defines the*xy*-plane. Your eye is exactly 3 feet above the center of the table (at [0 0 3]) and you are staring directly parallel to the length of the table (along the

*y*-axis). Use a canonical [-1,1]x[-1,1] viewing angle and an image plane one foot in front of your eye.

**Questions:**

- What are the coordinates of the four corners of the ping pong table?
- How would you describe, in
*GLIDE*, the orientation of your eye as a camera? - Where are the projections of the four table points in the
*xy*-image plane? Draw the four projected points connected in order. - Where are the correct projections of the edges of the table? Draw the four projected points and describe where the edges should be.
- What does the clipped table polygon look like with a near clipping plane of
*z*=-1 and a far clipping plane of*z*=-10

Last modified: Tue Mar 31 19:51:50 1998