From: sloan@willis.cis.uab.edu (Kenneth Sloan)
Subject: Re: Ordered triplets->homogenized vectors
Organization: CIS, University of Alabama at Birmingham
Date: Tue, 22 Dec 1992 05:16:16 GMT

In article <38000@uflorida.cis.ufl.edu> sdk@reef.cis.ufl.edu (Steven "Scrib" Kuck) writes:
>I have having some problems doing 3d clipping to a canonical view volume.

Your problem is not with clipping - it is with the perspective
transform.  The bottom line is tha you must clip against a "hither"
plane *before* doing the perspective transform.  You have noticed one
classic problem - but have not (yet) run across a more subtle (but
infinitely more amusing) classic problem.

>My points are represented in camera relative points with +Z into the
>screen.  The problem stems that I need to convert these points from
>x,y,z ordered triplets to homogenized vertices.  The equation I'm using
>is from the Texas Instruments 3D Graphics Library documention, and
>looks like this:

Beware of cookbooks.  You need a graphics textbook.  For this
subject, I recommend Newman&Sproull - but any decent text will do.

>
>given an ordered triplet of ( x, y, z, 1 ), convert to a homogenized
>vector of ( x, y, z, w ).
>
>x = D * x / Sx
>y = D * y / Sy
>z = a * z + b
>w = z
>
>D = distance to the viewport
>Sx = viewport half-width
>Sy = viewport half-height
>a  = z-input scaling constant
>b  = z-input translating constant
>
> a = 1 + ( N/F ) 
>     -----------
>     1 - ( N/F )
>
> b = -2 * N
>     -------
>     1 - ( N/F )
>
>where N is near-clipping plane distance and F is the far clipping plane dist.
>
>Okay, now that that is out of the way, the problem becomes rather apparent.
>In order to convert back to an ordered triplet, where the fourth element
>is 1, you divide by "w", i.e.
>
>[ x, y, z, w ] == [ x /w , y/w, z/w, 1 ]
>
>Now, when I convert TO homogenous vectors and I have an initial Z value of
>0,

STOP!  Think! 

Question: What does it mean for a point fo have "an initial Z value
          of 0"?  

Answer: this point is directly alongside the eyepoint.  It is neither in
front of, nor behind the eye.  In order to "see" this point, you would
need a 180deg field of view.

Question: Where in the image plane will this point project?

Answer: either the projected x value, or the projected y value, or both,
will be infinite.  This looks bad.

Extra Credit Question: what about points with an initial Z value *less
than* 0?

>... then W becomes zero, and when I try to divide out by W, I get a divide
>by zero....


Which turns out to be correct, more or less.  Not the answer you want -
but correct nonetheless.  

So...how to avoid this.

  "Doctor, Doctor: it hurst when I do this"

  "Don't do that!"

You cannot allow points with "an initial Z value of 0" to be turned into
homogenous co-ordinates in the first place.  If you do, you lose.
So...you must obviously CLIP BEFORE PERSPECTIVE.  

Back to our Extra Credit Question.  After a little thought (or perhaps
some experimentation) you will discover that you cannot apply the
perspective transformation to points with an initial Z value *less than
or equal* to 0.  You must CLIP BEFORE PERSPECTIVE.

Next, you will decide that it is bad news to divide by even very small
positive values.  Instead of clipping against Z==0.0, you should clip
against Z==hither - where the value of "hither" depends on other
parameters.  All we know so far is that "hither" should be larger than
the smallest number you feel comfortable dividing by.

See Newman&Sproull.



-- 
Kenneth Sloan                   Computer and Information Sciences
sloan@cis.uab.edu               University of Alabama at Birmingham
(205) 934-2213                  115A Campbell Hall, UAB Station 
(205) 934-5473 FAX              Birmingham, AL 35294-1170