home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / geometry / transform3 / tm3persp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-25  |  2.0 KB  |  62 lines

  1. /* Copyright (c) 1992 The Geometry Center; University of Minnesota
  2.    1300 South Second Street;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of geomview/OOGL. geomview/OOGL is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9.  
  10. /* Authors: Charlie Gunn, Pat Hanrahan, Stuart Levy, Tamara Munzner, Mark Phillips */
  11.  
  12. #include <stdio.h>
  13. #include <math.h>
  14. #include "transform3.h"
  15. #include "ooglutil.h"    /* For OOGLError */
  16.  
  17. /*
  18.  *     (  2*n/(r-l)      0             0          0 )
  19.  *     (     0        2*n/(t-b)        0          0 )
  20.  * T = ( (r+l)/(r-l) (t+b)/(t-b)  (f+n)/(n-f)    -1 )
  21.  *     (     0         0      2*f*n/(n-f)     0 )
  22.  *
  23.  * Transform a row vector {xw,yw,zw,w} * T => {X,Y,Z,-z}
  24.  * mapping the given viewing frustum into the cube -1 <= {X,Y,Z} <= 1,
  25.  * with Z = -1 at near plane (z = -n), +1 at far plane (z = -f)
  26.  * since the camera is looking in its -Z direction.
  27.  * Here l and r are the x coordinates, and b and t the y coordinates,
  28.  * of the edges of the viewing frustum at the near plane, z = -n.
  29.  *
  30.  * Note that n and f should be the positive distances to the near & far planes,
  31.  * not the negative Z coordinates of those planes.
  32.  */
  33. void
  34. Tm3Perspective( T, l, r, b, t, n, f )
  35.     Transform3 T;
  36.     float l,r,b,t,n,f;
  37. {
  38.     Tm3Identity( T );
  39.  
  40.     if( l == r ) {
  41.     OOGLError(1/*UserError*/, "Tm3Perspective: l and r must be different." );
  42.     return;
  43.     }
  44.     if( b == t ) {
  45.     OOGLError(1/*UserError*/, "Tm3Perspective: b and t must be different." );
  46.     return;
  47.     }
  48.     if( n == f ) {
  49.     OOGLError(1/*UserError*/, "Tm3Perspective: n and f must be different." );
  50.     return;
  51.     }
  52.  
  53.     T[X][X] = 2*n/(r-l);
  54.     T[Y][Y] = 2*n/(t-b);
  55.     T[Z][Z] = -(f+n)/(f-n);
  56.     T[W][W] = 0.;
  57.     T[Z][W] = -1;
  58.     T[Z][X] = (r+l)/(r-l);
  59.     T[Z][Y] = (t+b)/(t-b);
  60.     T[W][Z] = 2*n*f/(n-f);
  61. }
  62.