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

  1. /*
  2. Using Quaternions for Coding 3D Transformations
  3. Patrick-Gilles Maillot
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. extern double P[3], Q[4], M[4][4];
  8.  
  9. set_obs_position(x,y,z)
  10. float    x, y, z;
  11. {
  12. int    i;
  13.  
  14. /*
  15.  * Set the values of the eye's position.
  16.  * The position here represents the position of the orthonormal base
  17.  * in respect to the observer.
  18.  */
  19.        P[0] = -x;
  20.        P[1] = -y;
  21.        P[2] = -z;
  22. /*
  23.  * Set the visualization to be in the decreasing x axis
  24.  */
  25.     Q[0] = 1.;
  26.       for (i = 1; i < 4; i++) Q[i] = 0.;
  27. }
  28.  
  29. translate_quaternion(x,i,w)
  30. float    x;
  31. int    i, w;
  32. {
  33. int    j, k;
  34. float    A, B, D, E, F;
  35.     
  36.     if (w < 0) {
  37. /*
  38.  * The observer moves in respect to the scene.
  39.  */
  40.     P[i - 1] -= x;
  41.   } else {
  42.  
  43. /* 
  44.  * The scene moves in respect to the observer.
  45.  * Compute the successor axis of i [1,2,3];
  46.  * and then the successor axis of j [1,2,3];
  47.  */
  48.     if ((j = i + 1) > 3) j = 1;
  49.     if ((k = j + 1) > 3) k = 1;
  50.     A = Q[j]; B = Q[k]; F = Q[0]; E = Q[i];
  51.     P[i - 1] += x * (E * E + F * F - A * A - B * B);
  52.     D = x + x;
  53.     P[j - 1] += D * (E * A + F * B);
  54.     P[k - 1] += D * (E * B + F * A);
  55.   }
  56. }
  57.  
  58. rotate_quaternion(x,y,i,w)
  59. float    x, y;
  60. int    i, w;
  61. {
  62. int    j, k;
  63. float    E, F, R1;
  64. /*
  65.  * Compute the successor axis of i [1,2,3] and  j [1,2,3];
  66.  */
  67.     if ((j = i + 1) > 3) j = 1;
  68.     if ((k = j + 1) > 3) k = 1;
  69.     E = Q[i];
  70.     Q[i] = E * x + w * y * Q[0];
  71.     Q[0] = Q[0] * x - w * y * E;
  72.     E = Q[j];
  73.     Q[j] = E * x + y * Q[k];
  74.     Q[k] = Q[k] * x - y * E;
  75.       if (w < 0) {
  76. /* Compute a new position if the observer moves in respect to the scene. */
  77.         j -= 1; k -= 1;
  78.         R1 = x * x - y * y;
  79.         F = 2. * x * y;
  80.         E = P[j];
  81.         P[j] = E * R1 + F * P[k];
  82.         P[k] = P[k] * R1 - F * E;
  83.       }
  84. }
  85.  
  86.  
  87. Evaluate_matrix()
  88. {
  89. float    e, f, r[4];
  90. int    i, j, k;
  91. /*
  92.  * We will need some square values!
  93.  */
  94.     for (i = 0; i < 4; i++) r[i] = Q[i] * Q[i];
  95. /*
  96.  * Compute each element of the matrix.
  97.  * j is the successor of i (in 1,2,3), while k is the successor of j.
  98.  */
  99.       for (i = 1; i < 4; i++) {
  100.         if ((j = i + 1) > 3) j = 1;
  101.         if ((k = j + 1) > 3) k = 1;
  102.         e = 2. * Q[i] * Q[j];
  103.         f = 2. * Q[k] * Q[0];
  104.         M[j][i] = e - f;
  105.         M[i][j] = e + f;
  106.         M[i][i] = r[i] + r[0] - r[j] - r[k];
  107.         M[0][i] = P[i - 1];
  108.         M[i][0] = 0.;
  109.       }
  110.     M[0][0] = 1.;
  111. }
  112.  
  113.  
  114.  
  115.  
  116.