home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gems / graphics / viewtran.c < prev   
C/C++ Source or Header  |  1992-04-09  |  2KB  |  73 lines

  1. /*
  2. 3D Viewing and Rotation Using Orthonormal Bases
  3. by Steve Cunningham
  4. from "Grahics Gems", Academic Press, 1990
  5. */
  6.  
  7. /*
  8.  * Transformations are presented as 4 by 3 matrices, omitting the
  9.  * fourth column to save memory.
  10.  *
  11.  * Functions are used from the Graphics Gems vector C library
  12.  */
  13.  
  14.  
  15. #include "GraphicsGems.h"        
  16.  
  17. typedef float Transform[4][3];
  18.  
  19. void BuildViewTransform( VRP, EP, UP, T )
  20.      Point3 VRP, EP, UP;
  21.      Transform T;
  22. {
  23.     Vector3    U, V, N;
  24.     float    dot;
  25.  
  26.     /*
  27.      * Compute vector  N = EP - VRP  and normalize  N
  28.      */
  29.     N.x = EP.x - VRP.x; N.y = EP.y - VRP.y; N.z = EP.z - VRP.z;
  30.     V3Normalize(&N);
  31.  
  32.     /*
  33.      * Compute vector  V = VP - VRP
  34.      * Make vector  V  orthogonal to  N  and normalize  V
  35.      */
  36.     V.x = UP.x - VRP.x; V.y = UP.y - VRP.y; V.z = UP.z - VRP.z;
  37.     dot = V3Dot(&V,&N);
  38.     V.x -= dot * N.x; V.y -= dot * N.y; V.z -= dot * N.z;
  39.     V3Normalize(&V);
  40.  
  41.  
  42.     /*
  43.      * Compute vector  U = V x N  (cross product)
  44.      */
  45.     V3Cross(&V,&N,&U);
  46.  
  47.     /*
  48.      * Write the vectors U, V, and N as the first three rows of the
  49.      *       first, second, and third columns of  T, respectively
  50.      */
  51.     T[0][0] = U.x;        /* column 1 , vector U */
  52.     T[1][0] = U.y;
  53.     T[2][0] = U.z;
  54.     T[0][1] = V.x;        /* column 2 , vector V */
  55.     T[1][1] = V.y;
  56.     T[2][1] = V.z;
  57.     T[0][2] = N.x;        /* column 3 , vector N */
  58.     T[1][2] = N.y;
  59.     T[2][2] = N.z;
  60.  
  61.     /*
  62.      * Compute the fourth row of  T  to include the translation of
  63.      *       VRP  to the origin
  64.      */
  65.     T[3][0] = - U.x * VRP.x - U.y * VRP.y - U.z * VRP.z;
  66.     T[3][1] = - V.x * VRP.x - V.y * VRP.y - V.z * VRP.z;
  67.     T[3][2] = - N.x * VRP.x - N.y * VRP.y - N.z * VRP.z;
  68.  
  69.     return;
  70. }
  71.  
  72.  
  73.