• Aucun résultat trouvé

The Clip Coordinate System

Dans le document Computer Graphics and Geometric Modeling (Page 138-146)

Once one has transformed objects into camera coordinates, our next problem is to clip points in the camera coordinate system to the truncated pyramid defined by the near and far clipping planes and the window. One could do this directly, but we prefer to transform into a coordinate system, called the clip coordinate systemorclip space, where the clipping volume is the unit cube [0,1] ¥[0,1]¥[0,1]. We denote the transformation that does this by TcamÆclip. There are two reasons for using this transformation:

(1) It is clearly simpler to clip against the unit cube.

(2) The clipping algorithm becomes independent of boundary dimensions.

Actually, rather than using these coordinates we shall use the associated homogeneous coordinates. The latter define what we shall call the homogeneous clip coordinate systemorhomogeneous clip space. Using homogeneous coordinates will enable us to describe maps via matrices and we will also not have to worry about any divisions by zero on our way to the clip stage. The map TcamÆhclip in Figure 4.1 refers to this camera-to-homogeneous-clip coordinates transformation. Let ThcamÆhclip denote the corresponding homogeneous-camera-to-homogeneous-clip coordinates transforma-tion. Figure 4.10 shows the relationships between all these maps. The map Tprojis the standard projection from homogeneous to Euclidean coordinates.

Assume that the view plane and near and far clipping planes are a distance d, dn, and df in front of the camera, respectively. To describe TcamÆhclip, it will suffice to describe ThcamÆhclip.

First of all, translate the camera to (0,0,-d). This translation is represented by the homogeneous matrix

Next, apply the projective transformation with homogeneous matrix Mpersp, where

(4.9)

To see exactly what the map defined by Mpersp does geometrically, consider the lines ax+z= -d and ax -z=d in the plane y =0. Note that (x,y,z,w) Mpersp=(x,y,z,(z/d)+w).

In particular,

This shows that the camera at (0,0,-d) has been mapped to “infinity” and the two lines have been mapped to the lines x¢ = -d/a and x¢ =d/a, respectively, in the plane y =0.

See Figure 4.11. In general, lines through (0,0,-d) are mapped to vertical lines through their intersection with the x-y plane. Furthermore, what was the central projection from the point (0,0,-d) is now an orthogonal projection of R3onto the x-y plane. It follows that the composition of Mtrand Mperspmaps the camera off to “infinity,” the near clipping plane to z = d (1 - d/dn), and the far clipping plane to z = d (1 - d/df).

The perspective projection problem has been transformed into a simple orthographic projection problem (we simply project (x,y,z) to (x,y,0)) with the clip volume now being

[-1 1, ]¥ -[ b b, ]¥[d(1-d df),d(1-d dn)].

Figure 4.11. Mapping the camera to infinity.

To get this new clip volume into the unit cube, we use the composite of the following maps: first, translate to

and then use the radial transformation which multiplies the x, y, and z-coordinates by

respectively. If Mscaleis the homogeneous matrix for the composite of these two maps, then

so that

(4.10)

is the matrix for the map ThcamÆhclip that we are after. It defines the transformation from homogeneous camera to homogeneous clip coordinates. By construction the map TcamÆclip sends the truncated view volume in camera coordinates into the unit cube [0,1] ¥[0,1]¥[0,1] in clip space.

Note that the camera-to-clip-space transformation does not cost us anything because it is computed only once and right away combined with the world-to-camera-space transformation so that points are only transformed once, not twice.

Finally, our camera-to-clip-space transformation maps three-dimensional points to three-dimensional points. In some cases, such as for wireframe displays, the z-coordinate is not needed and we could eliminate a few computations above. However,

M M M M d

if we want to implement visible surface algorithms, then we need the z. Note that the transformation is not a motion and will deform objects. However, and this is the important fact, it preserves relative z-distances from the camera and to determine the visible surfaces we only care about relative and not absolute distances. More pre-cisely, let p1andp2be two points that lie along a ray in front of the camera and assume that they map to p1¢and p2¢, respectively, in clip space. If the z-coordinate of p1 is less than the z-coordinate of p2, then the z-coordinate of p1¢will be less than the z-coordinate of p2¢. In other words, the “in front of” relation is preserved. To see this, letpi=(tix, tiy, tiz), 0 < t1<t2, and pi¢ =(xi¢, yi¢, zi¢). It follows from (4.10) that

from which it is easy to show that t1<t2if and only if z1¢ <z2¢.

4.6 Clipping

In the last section we showed how to transform the clipping problem to a problem of clipping against the unit cube in clip space. The actual clipping against the cube will be done in homogeneous clip space using homogeneous coordinates (x,y,z,w). The advantage of homogeneous coordinates was already alluded to: everypoint of camera space is sent to a well-defined point here because values become undefined only when we try to map down to clip space by dividing by w, which may be zero.

Chapter 3 discussed general clipping algorithms for individualsegments or whole polygons. These have their place, but they are not geared to geometric modeling envi-ronments where one often wants to draw connectedsegments. We shall now describe a very efficient clipping algorithm for such a setting that comes from [Blin91a]. It uses the “best” parts of the Cohen-Sutherland, Cyrus-Beck, and Liang-Barsky algorithms.

In homogeneous coordinates halfplanes can be defined as a set of points that have a nonnegative dot product with a fixed vector. For example, the halfplane ax +by + cz+d≥ 0, is defined by the vector (a,b,c,d) in homogeneous coordinates. Therefore, by lining up vectors appropriately, any convex region bounded by planes can be defined as the set of points that have nonnegative dot products with a fixed finite set of vectors. In our case, we can use the following vectors for the six bounding planes x=0, x =1, y =0, y =1, z =0, and z =1 for the unit cube I3:

Ifp=(x,y,z,w), then let BCi=BCi(p)=p•Bi. We shall call the BCitheboundary coor-dinatesofp. These coordinates are easy to compute:

BC x BC y BC z

A point will be inside the clip volume if and only if all of its boundary coordinates are nonnegative. If the ith boundary coordinate of a point is nonnegative, then we shall call the point i-inside; otherwise, it is i-out. Let BC = BC(p) = (BC1(p),BC2(p), . . . , BC6(p)) denote the vector of boundary coordinates.

Next, let p0andp1be two points and set BC0 =BC(p0) and BC1 = BC(p1). The next table shows the relationship of the segment [p0,p1] with respect to the ith bound-ary:

Sign bit Sign bit

BC0i BC1i Meaning

0 0 Entire segment is i-inside

1 0 Segment straddles boundary, p0is i-out 0 1 Segment straddles boundary, p1is i-out

1 1 Entire segment is i-out

It will be convenient to record the sign information of a point pinto a six-bit word called its outcode and denote it by CODE(p). More precisely, the ith bit of CODE(p) will be the sign bit of BCi(p). Let CODE0 = CODE(p0) and CODE1 = CODE(p1).

Simple logical operations on CODE0 and CODE1 now give us a lot of information about the location of the segment. For example, the segment will be inside the clip volume if (CODE0 or CODE1) is zero. The segment will be entirely outside the clip volume if (CODE0 and CODE1) is nonzero. (Compare this with the Cohen-Sutherland clipping algorithm.)

Whenever the segment crosses the ith clipping plane, we need to find the inter-section. This is easy to do if we parameterize the segment, and we have done this sort of thing before. We need to find the t so that

With our notation, t = BC0i/(BC0i- BC1i). The segment will intersect the plane only if this t lies in [0,1]. The expression shows that this can only happen if BC0iand BC1i

have different signs.

Now, the clipping algorithm we are in the process of describing is intended for situations where we want to do a sequence of “DrawTo” and “MoveTo” commands.

The flag parameter in the “Clip” procedure is used to distinguish between the two cases and will save us having to write a separate “Clip” procedure for both. The abstract programs are given in Algorithm 4.6.1 with the ViewPt procedure represent-ing the next stage of the graphics pipeline after clipprepresent-ing, namely, the clip-space-to-pixel-space map. A more efficient procedure using goto’s, assuming that the trivial rejects are the most common cases, is shown in Algorithm 4.6.2.

Next, we describe the nontrivial stuff that happens when a segment straddles a boundary. We basically use the Liang-Barsky algorithm here. In Algorithm 4.6.3, the variables a0 and a1 keep track of the still-visible part of a segment. MASK is used to select one boundary at a time. Blinn points out that he does the operation CODE0 or CODE1 againon the theory that it will not get done often and we save storing an

p0+ (p1 p0- ) B

( t )∑ i =0.

unneeded value earlier. He also made all tests as much as possible into integer com-parisons to cut down on floating point operations.

There are some limitations to Blinn’s clipping algorithm. Although they tend to be more theoretical than practical, one should be aware of them. The problem is that one is clipping to the infinite inverted pyramid in homogeneous coordinate space shown in Figure 4.12(a) when, in fact, one should be clipping to the double pyramid shown in Figure 4.12(b). The points in the negative pyramid will also project to the visible region. On the other hand, the basic graphics pipeline that we have been describing will not introduce any negative w’s and so this problem will not arise here.

The problem arises only if negative w-coordinates are introduced explicitly or if one wants to represent infinite segments (the complement of a normal segment in a line).

If one does want to handle such cases, the quickest way to do it is to draw the world twice, once as described above and then a second time, where the matrix that maps from shape to clip coordinates is multiplied by -1.

rea

homogeneous point p0, p1;

l BC0,BC1

6-bit word CODE0,CODE1;

Procedure Clip ((move,draw) flag)

{We assume that p0, BC0, and CODE0 have been defined. We clip [p0,p1]}

begin

Calculate BC1, CODE1;

case flag of

move: DoMoveStuff ();

draw : DoDrawStuff ();

end;

{ Update globals }

[p0,BC0,CODE0] := [p1,BC1,CODE1];

end;

Procedure DoMoveStuff ()

if CODE1 = 0 then ViewPt (p1,move);

Procedure DoDrawStuff () if (CODE0 and CODE1) = 0 then

begin

if (CODE0 or CODE1) = 0 then ViewPt (p1,draw) else DoNontrivialStuff () end;

Algorithm 4.6.1. Abstract programs for clipping using homogeneous coordinates.

Procedure Clip ((move,draw) flag) begin

labelmoveit, nontriv, finish;

Calculate BC1, CODE1;

if flag = move then gotomoveit;

if (CODE0 and CODE1)π 0 then goto finish;

if (CODE0 or CODE1)π 0 then goto nontriv;

ViewPt(p1,draw);

finish:

[p0,BC0,CODE0] := [p1,BC1,CODE1];

return;

moveit:

if CODE1 π 0 then goto finish;

ViewPt(p1,move);

goto finish;

nontriv:

DoNontrivialStuff ();

goto finish;

end;

Algorithm 4.6.2. More efficient clipping using homogeneous coordinates.

Figure 4.12. Single- and double-clip pyramid.

Algorithm 4.6.3. The nontrivial part of homogeneous coordinate clipping.

Procedure DoNontrivialStuff () begin

6-bit word BCASE, MASK;

real

integer i;

a0, a1, a;

homogeneous point p;

BCASE := CODE0 or CODE1;

a0 := 0.0; a1 := 1.0; MASK := 1;

for i:=1 to numClipPlanes do begin

if (BCASEand MASK) π 0 then begin

a := BC0[i] / (BC0[i] - BC1[i]);

if (CODE0 and MASK) π 0 then a0 := max (a0,a) else a1 := min (a1,a);

if a1 < a0 then return; { reject } end;

Shift MASK left one bit end;

if CODE0 π 0 then begin

p :=p0+ a0*(p1-p0);

ViewPt (p,move);

end;

if CODE1 π 0 then

begin

p :=p0+ a1*(p1-p0);

ViewPt (p,draw);

end

else ViewPt (p1,draw);

end;

Dans le document Computer Graphics and Geometric Modeling (Page 138-146)