home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / fermiVogle.tar.Z / fermiVogle.tar / devel / src / trans.c < prev    next >
C/C++ Source or Header  |  1996-02-07  |  5KB  |  173 lines

  1. #include "vogle.h"
  2.  
  3. #ifdef    TC
  4.  
  5. extern    double    cos();
  6. extern    double    sin();
  7.  
  8. #else 
  9.  
  10. #include <math.h>
  11.  
  12. #endif
  13.  
  14. /*
  15.  * translate
  16.  * 
  17.  * Set up a translation matrix and premultiply it and 
  18.  * the top matrix on the stack.
  19.  *
  20.  */
  21. void
  22. translate(float x, float y, float z)
  23. {
  24.     Token    *tok;
  25.  
  26.     if (!vdevice.initialised) 
  27.         verror("translate: vogle not initialised");
  28.  
  29.         if (vdevice.inobject) {
  30.         tok = newtokens(4);
  31.  
  32.         tok[0].i = TRANSLATE;
  33.         tok[1].f = x;
  34.         tok[2].f = y;
  35.         tok[3].f = z;
  36.  
  37.                 return;
  38.         }
  39.  
  40.     /*
  41.      * Do the operations directly on the top matrix of
  42.      * the stack to speed things up.
  43.      */
  44.     vdevice.transmat->m[3][0] += x * vdevice.transmat->m[0][0]
  45.                    + y * vdevice.transmat->m[1][0]
  46.                    + z * vdevice.transmat->m[2][0];
  47.  
  48.     vdevice.transmat->m[3][1] += x * vdevice.transmat->m[0][1]
  49.                    + y * vdevice.transmat->m[1][1]
  50.                    + z * vdevice.transmat->m[2][1];
  51.  
  52.     vdevice.transmat->m[3][2] += x * vdevice.transmat->m[0][2]
  53.                    + y * vdevice.transmat->m[1][2]
  54.                    + z * vdevice.transmat->m[2][2];
  55.  
  56.     vdevice.transmat->m[3][3] += x * vdevice.transmat->m[0][3]
  57.                    + y * vdevice.transmat->m[1][3]
  58.                    + z * vdevice.transmat->m[2][3];
  59. }
  60.  
  61. /*
  62.  * rotate
  63.  * 
  64.  * Set up a rotate matrix and premultiply it with 
  65.  * the top matrix on the stack.
  66.  *
  67.  */
  68. void
  69. rotate(float r, char axis)
  70. {
  71.     Token        *tok;
  72.     register float    costheta, sintheta, tmp;
  73.  
  74.     if (!vdevice.initialised)
  75.         verror("rotate: vogle not initialised");
  76.  
  77.         if (vdevice.inobject) {
  78.         tok = newtokens(3);
  79.  
  80.         tok[0].i = ROTATE;
  81.         tok[1].f = r;
  82.         tok[2].i = axis;
  83.  
  84.                 return;
  85.         }
  86.  
  87.     /*
  88.      * Do the operations directly on the top matrix of
  89.      * the stack to speed things up.
  90.      */
  91.     costheta = cos((double)(D2R * r));
  92.     sintheta = sin((double)(D2R * r));
  93.  
  94.     switch(axis) {
  95.     case 'x':
  96.     case 'X':
  97.         tmp = vdevice.transmat->m[1][0];
  98.         vdevice.transmat->m[1][0] = costheta * tmp
  99.                       + sintheta * vdevice.transmat->m[2][0];
  100.         vdevice.transmat->m[2][0] = costheta * vdevice.transmat->m[2][0]
  101.                       - sintheta * tmp;
  102.  
  103.         tmp = vdevice.transmat->m[1][1];
  104.         vdevice.transmat->m[1][1] = costheta * tmp
  105.                       + sintheta * vdevice.transmat->m[2][1];
  106.         vdevice.transmat->m[2][1] = costheta * vdevice.transmat->m[2][1]
  107.                       - sintheta * tmp;
  108.         tmp = vdevice.transmat->m[1][2];
  109.         vdevice.transmat->m[1][2] = costheta * tmp
  110.                       + sintheta * vdevice.transmat->m[2][2];
  111.         vdevice.transmat->m[2][2] = costheta * vdevice.transmat->m[2][2]
  112.                       - sintheta * tmp;
  113.  
  114.         tmp = vdevice.transmat->m[1][3];
  115.         vdevice.transmat->m[1][3] = costheta * tmp
  116.                       + sintheta * vdevice.transmat->m[2][3];
  117.         vdevice.transmat->m[2][3] = costheta * vdevice.transmat->m[2][3]
  118.                       - sintheta * tmp;
  119.         break;
  120.     case 'y':
  121.     case 'Y':
  122.         tmp = vdevice.transmat->m[0][0];
  123.         vdevice.transmat->m[0][0] = costheta * tmp
  124.                       - sintheta * vdevice.transmat->m[2][0];
  125.         vdevice.transmat->m[2][0] = sintheta * tmp
  126.                       + costheta * vdevice.transmat->m[2][0];
  127.         tmp = vdevice.transmat->m[0][1];
  128.         vdevice.transmat->m[0][1] = costheta * tmp
  129.                       - sintheta * vdevice.transmat->m[2][1];
  130.         vdevice.transmat->m[2][1] = sintheta * tmp
  131.                       + costheta * vdevice.transmat->m[2][1];
  132.         tmp = vdevice.transmat->m[0][2];
  133.         vdevice.transmat->m[0][2] = costheta * tmp
  134.                       - sintheta * vdevice.transmat->m[2][2];
  135.         vdevice.transmat->m[2][2] = sintheta * tmp
  136.                       + costheta * vdevice.transmat->m[2][2];
  137.         tmp = vdevice.transmat->m[0][3];
  138.         vdevice.transmat->m[0][3] = costheta * tmp
  139.                       - sintheta * vdevice.transmat->m[2][3];
  140.         vdevice.transmat->m[2][3] = sintheta * tmp
  141.                       + costheta * vdevice.transmat->m[2][3];
  142.         break;
  143.     case 'z':
  144.     case 'Z':
  145.         tmp = vdevice.transmat->m[0][0];
  146.         vdevice.transmat->m[0][0] = costheta * tmp
  147.                       + sintheta * vdevice.transmat->m[1][0];
  148.         vdevice.transmat->m[1][0] = costheta * vdevice.transmat->m[1][0]
  149.                       - sintheta * tmp;
  150.  
  151.         tmp = vdevice.transmat->m[0][1];
  152.         vdevice.transmat->m[0][1] = costheta * tmp
  153.                       + sintheta * vdevice.transmat->m[1][1];
  154.         vdevice.transmat->m[1][1] = costheta * vdevice.transmat->m[1][1]
  155.                       - sintheta * tmp;
  156.  
  157.         tmp = vdevice.transmat->m[0][2];
  158.         vdevice.transmat->m[0][2] = costheta * tmp
  159.                       + sintheta * vdevice.transmat->m[1][2];
  160.         vdevice.transmat->m[1][2] = costheta * vdevice.transmat->m[1][2]
  161.                       - sintheta * tmp;
  162.  
  163.         tmp = vdevice.transmat->m[0][3];
  164.         vdevice.transmat->m[0][3] = costheta * tmp
  165.                       + sintheta * vdevice.transmat->m[1][3];
  166.         vdevice.transmat->m[1][3] = costheta * vdevice.transmat->m[1][3]
  167.                       - sintheta * tmp;
  168.         break;
  169.     default:
  170.         verror("rotate: illegal axis of rotation");
  171.     }
  172. }
  173.