home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / sys / sgi / graphics / 51 < prev    next >
Encoding:
Text File  |  1992-12-22  |  3.5 KB  |  147 lines

  1. Path: sparky!uunet!olivea!sgigate!sgi!fido!krypton!gavin
  2. From: gavin@krypton.asd.sgi.com (Gavin Bell)
  3. Newsgroups: comp.sys.sgi.graphics
  4. Subject: Re: eye point/eye direction/up vector -> lookat (FAQ?)
  5. Message-ID: <1h7k07INNf03@fido.asd.sgi.com>
  6. Date: 22 Dec 92 17:41:27 GMT
  7. References: <Bzn4v1.F7r@world.std.com>
  8. Organization: Silicon Graphics, Inc.  Mountain View, CA
  9. Lines: 135
  10. NNTP-Posting-Host: krypton.asd.sgi.com
  11.  
  12. In <Bzn4v1.F7r@world.std.com> tjh@world.std.com (Tim Hall) writes:
  13.  
  14. >Given the eye point, eye direction and up vector how do I compute the
  15. >twist value given to lookat?
  16.  
  17. Lookat.... evil.... bad....
  18.  
  19. Mr Thant Tessman came up with an 'upat' replacement for lookat that is
  20. exactly what you are looking for.  It doesn't bother computing the
  21. correct lookat paramaters (which is impossible since lookat has
  22. singularities when looking straight up or down).
  23.  
  24. You can find it at:
  25.  
  26. /usr/people/4Dgifts/examples/grafix/upat.c
  27.  
  28. If you can't get your system administrator to load 4Dgifts from eoe2
  29. for you (there is some great stuff in there), here it is:
  30.  
  31. /*
  32.  *   upat.c:
  33.  *
  34.  *    This module contains all the code necessary to hook the routine upat()
  35.  *  into any application wanting to take advantage of "user-defined up-axis"
  36.  *  functionality.  Excluding its seventh parameter--twist--upat() can be 
  37.  *  thought of as a superset of the LOOKAT(3G) function (the first six 
  38.  *  parameters are identical to the first six parameters of LOOKAT(3G)).  
  39.  *
  40.  * C SPECIFICATION
  41.  *     void upat(vx, vy, vz, px, py, pz, ux, uy, uz)
  42.  *     float vx, vy, vz, px, py, pz, ux, uy, uz;
  43.  *
  44.  * PARAMETERS
  45.  *     vx, vy, vz   is the viewpoint, the point the eye is looking from
  46.  *     px, py, pz   is the reference point, the point the eye is looking at
  47.  *     ux, uy, uz   is the up vector
  48.  */
  49.  
  50. #include <stdio.h>
  51. #include <math.h>
  52. #include <gl/gl.h>
  53.  
  54. #define X 0
  55. #define Y 1
  56. #define Z 2
  57.  
  58. void normalize(float *);
  59. void cross(float *result, float *v1, float *v2);
  60.  
  61. void upat(float vx, float vy, float vz, 
  62.           float px, float py, float pz, 
  63.           float ux, float uy, float uz) 
  64. {
  65.     int i;
  66.     float forward[3], side[3], up[3];
  67.     float m[4][4];
  68.  
  69.  
  70.     forward[X] = px - vx;
  71.     forward[Y] = py - vy;
  72.     forward[Z] = pz - vz;
  73.  
  74.     /* temporarily use view-up to hold world-up */
  75.     up[X] = ux;        
  76.     up[Y] = uy;
  77.     up[Z] = uz;
  78.  
  79.     normalize(forward);
  80.  
  81.     /* generate the vector side from the
  82.      * 2 vectors view forward and world up 
  83.      */
  84.     cross(side, forward, up);
  85.     normalize(side);
  86.  
  87.     /* generate the view up vector from 
  88.      * the 2 vectors view forward and view side 
  89.      */
  90.     cross(up, side, forward);
  91.  
  92.     m[0][0] = side[X];
  93.     m[1][0] = side[Y];
  94.     m[2][0] = side[Z];
  95.     m[3][0] = 0.0;
  96.  
  97.     m[0][1] = up[X];
  98.     m[1][1] = up[Y];
  99.     m[2][1] = up[Z];
  100.     m[3][1] = 0.0;
  101.  
  102.     m[0][2] = -forward[X];
  103.     m[1][2] = -forward[Y];
  104.     m[2][2] = -forward[Z];
  105.     m[3][2] = 0.0;
  106.  
  107.     m[0][3] = 0.0;
  108.     m[1][3] = 0.0;
  109.     m[2][3] = 0.0;
  110.     m[3][3] = 1.0;
  111.  
  112.     multmatrix(m);
  113.     translate(-vx, -vy, -vz);
  114. }
  115.  
  116.  
  117.  
  118. /* 
  119.  *   generate a normalized vector of length 1.0
  120.  */
  121. void normalize(float *v) 
  122. {
  123.     float r;
  124.  
  125.  
  126.     r = sqrt( v[X]*v[X] + v[Y]*v[Y] + v[Z]*v[Z] );
  127.  
  128.     v[X] /= r;
  129.     v[Y] /= r;
  130.     v[Z] /= r;
  131. }
  132.  
  133.  
  134.  
  135. /*
  136.  *   make a cross product of the two vectors passed in via v1 and v2,
  137.  *   and return the resulting perpendicular-to-both vector in result
  138.  */
  139. void cross(float *result, float *v1, float *v2) 
  140. {
  141.     result[X] = v1[Y]*v2[Z] - v1[Z]*v2[Y];
  142.     result[Y] = v1[Z]*v2[X] - v1[X]*v2[Z];
  143.     result[Z] = v1[X]*v2[Y] - v1[Y]*v2[X];
  144. }
  145. --
  146. --gavin     (gavin@sgi.com,  (415)390-1024)
  147.