[comp.graphics] How do I draw a perspective view?

u906400@BRUNY.CC.UTAS.EDU.AU (Roger Scott) (06/01/91)

Could somebody please explain to me how to take a point (x,y,z) in space and
map it onto a screen as a single point perspective view in (x,y)?  I've had
a look through the books, but I can't follow what's happening.  It is for an
assignment and so far I can get it to draw a cube properly, but when I throw
anything more difficult at the program  the lines seem to go everywhere. I
am currently using
	xp = (x/((z/d)+1)) & yp = (y/((z/d)+1)) where d is the distance between
						the point and the origin.
Is this right?  I don't think it is or otherwise I would have figured it out
by now.  Any help appreciated.

	Roger.

*****************************************************************
*   Roger Scott             3rd Year Computer Science           *
*                           University Of Tasmania, AUSTRALIA   *
* Internet/AARnet  u906400@bruny.cc.utas.edu.au  (Try here 1st) *
*             or   ap351@cleveland.freenet.edu                  *
*             or   Roger.Scott@p15.f206.n670.z3.fidonet.org     *
*****************************************************************

mgobbi@cs.ubc.ca (Mike Gobbi) (06/04/91)

In article <9106010027.AA01356@bruny.cc.utas.edu.au> u906400@BRUNY.CC.UTAS.EDU.AU (Roger Scott) writes:
>Could somebody please explain to me how to take a point (x,y,z) in space and
>map it onto a screen as a single point perspective view in (x,y)?  I've had
> ...
>by now.  Any help appreciated.
>
>	Roger.

Okay. Assuming that you are using a left-handed coordinate system:
  X -> distance to right
  Y -> distance up
  Z -> distance forward

I don't know if it is correct (I just came up with it myself after doing
some geometry), but this method works for me:

  px = x / z * tan(FOV)
  py = y / z * tan(FOV)

where FOV is the desired field of view (humans see 180 degrees, most
rendering works best if FOV is between 30 and 60).  This maps x&y into
values from -1 to 1, which you translate & scale to position in your
window nicely.

Note that you MUST clip any polygon which has ANY vertex behind the eye
(negative or 0 z-value) or else really wierd stuff will happen.



--
    __     In Quest of Knowledge...
   /..\
--mm--mm--     Mike Gobbi
               (mgobbi@cs.ubc.ca)

ttobler@unislc.uucp (Trent Tobler) (06/05/91)

From article <9106010027.AA01356@bruny.cc.utas.edu.au>, by u906400@BRUNY.CC.UTAS.EDU.AU (Roger Scott):
> Could somebody please explain to me how to take a point (x,y,z) in space and
> map it onto a screen as a single point perspective view in (x,y)?  I've had
> a look through the books, but I can't follow what's happening.  It is for an
> assignment and so far I can get it to draw a cube properly, but when I throw
> anything more difficult at the program  the lines seem to go everywhere. I
> am currently using
> 	xp = (x/((z/d)+1)) & yp = (y/((z/d)+1)) where d is the distance between
> 						the point and the origin.

Well, I assume you only want to look one direction (adequete if you can do
rotation.)

If that is the case, then the point will be (x/z,y/z), or xp = x/z and yp = y/z.
If z is negative, then the point is behind you, and you should therefore
consider it a virtual point (point at infinite).  These points are the most
difficult to draw lines with.  Example:
________________________
|                      |
|      p-------p       | no points are behind you.
|                      |
|      v       p-------| one point is behind you.
|                      |
|------p       v       | one point is behind you.
|                      |
|      v       v       | both points are behind you. (no line)
|                      |
|                      |
|______________________|

As you can see, virtual points cause the line to head AWAY from it, starting
from the second point (which must be a real point, otherwise the line is
behind you as well).

Other than that, there is not much more to perspective.


--
  Trent Tobler - ttobler@csulx.weber.edu

ron@vicorp.com (Ron Peterson) (06/06/91)

In article <1991Jun3.213012.7393@cs.ubc.ca> mgobbi@cs.ubc.ca (Mike Gobbi) writes:
[text on how to do perspective deleted]
>
>Note that you MUST clip any polygon which has ANY vertex behind the eye
>(negative or 0 z-value) or else really wierd stuff will happen.
>
Is there a simple general method for doing perspective calculations that 
works for ANY orientation of object, screen, and POV including situations
where:
   * part of the object is behind the POV and part in front or 
   * if the object crosses between octants of the coordinate system or 
   * the object resides at the origin or
   * the POV is inside the object being rendered
or does it have to be broken into cases?
I implemented something in my ray tracer that works for
most situations but when dealing with very long thin objects that
pass close to the POV (like the Star Wars battleship scene where the
mile long ship goes past the camera) weird things start to happen at
the edges of the picture.  Also, I know DBWRender (the version that
source code is available for) has trouble with objects or the POV
crossing octants (textures change and the scene sometimes flips upside
down.)
ron@vicorp.com or uunet!vicorp!ron

pmartz@undies.dsd.es.com (Paul Martz) (06/07/91)

In article <1991Jun6.002233.5415@vicorp.com>, ron@vicorp.com (Ron Peterson) writes:
> In article <1991Jun3.213012.7393@cs.ubc.ca> mgobbi@cs.ubc.ca (Mike Gobbi) writes:
> [text on how to do perspective deleted]
> >
> >Note that you MUST clip any polygon which has ANY vertex behind the eye
> >(negative or 0 z-value) or else really wierd stuff will happen.
> >
> Is there a simple general method for doing perspective calculations that 
> works for ANY orientation of object, screen, and POV including situations
> where:
>    * part of the object is behind the POV and part in front or 
>    * if the object crosses between octants of the coordinate system or 
>    * the object resides at the origin or
>    * the POV is inside the object being rendered
> or does it have to be broken into cases?

It does not have to be broken down into cases, as long as you:
	- Use a generalized viewing pipe, a la Foley & Van Dam;
	- Send every primitive through a clipping function after it
	  has been transformed to NPC space.
-- 

   -paul	pmartz@dsd.es.com
		Evans & Sutherland