home *** CD-ROM | disk | FTP | other *** search
/ gondwana.ecr.mu.oz.au/pub/ / Graphics.tar / Graphics / voglw.zip / trans.c < prev    next >
C/C++ Source or Header  |  1997-02-13  |  5KB  |  191 lines

  1. #include "vogl.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(x, y, z)
  23.     float     x, y, z;
  24. {
  25.     Token    *tok;
  26.  
  27.     if (!vdevice.initialised) 
  28.         verror("translate: vogl not initialised");
  29.  
  30.         if (vdevice.inobject) {
  31.         tok = newtokens(4);
  32.  
  33.         tok[0].i = TRANSLATE;
  34.         tok[1].f = x;
  35.         tok[2].f = y;
  36.         tok[3].f = z;
  37.  
  38.                 return;
  39.         }
  40.  
  41.     /*
  42.      * Do the operations directly on the top matrix of
  43.      * the stack to speed things up.
  44.      */
  45.     vdevice.transmat->m[3][0] += x * vdevice.transmat->m[0][0]
  46.                    + y * vdevice.transmat->m[1][0]
  47.                    + z * vdevice.transmat->m[2][0];
  48.  
  49.     vdevice.transmat->m[3][1] += x * vdevice.transmat->m[0][1]
  50.                    + y * vdevice.transmat->m[1][1]
  51.                    + z * vdevice.transmat->m[2][1];
  52.  
  53.     vdevice.transmat->m[3][2] += x * vdevice.transmat->m[0][2]
  54.                    + y * vdevice.transmat->m[1][2]
  55.                    + z * vdevice.transmat->m[2][2];
  56.  
  57.     vdevice.transmat->m[3][3] += x * vdevice.transmat->m[0][3]
  58.                    + y * vdevice.transmat->m[1][3]
  59.                    + z * vdevice.transmat->m[2][3];
  60. }
  61.  
  62. /*
  63.  * rot
  64.  * 
  65.  * Set up a rotate matrix and premultiply it with 
  66.  * the top matrix on the stack.
  67.  *
  68.  */
  69. void
  70. rot(r, axis)
  71.     float    r;
  72.     char    axis;
  73. {
  74.     Token        *tok;
  75.     register float    costheta, sintheta, tmp;
  76.  
  77.     if (!vdevice.initialised)
  78.         verror("rot: vogl not initialised");
  79.  
  80.         if (vdevice.inobject) {
  81.         tok = newtokens(3);
  82.  
  83.         tok[0].i = ROTATE;
  84.         tok[1].f = r;
  85.         tok[2].i = axis;
  86.  
  87.                 return;
  88.         }
  89.  
  90.     /*
  91.      * Do the operations directly on the top matrix of
  92.      * the stack to speed things up.
  93.      */
  94.     costheta = cos((double)(D2R * r));
  95.     sintheta = sin((double)(D2R * r));
  96.  
  97.     switch(axis) {
  98.     case 'x':
  99.     case 'X':
  100.         tmp = vdevice.transmat->m[1][0];
  101.         vdevice.transmat->m[1][0] = costheta * tmp
  102.                       + sintheta * vdevice.transmat->m[2][0];
  103.         vdevice.transmat->m[2][0] = costheta * vdevice.transmat->m[2][0]
  104.                       - sintheta * tmp;
  105.  
  106.         tmp = vdevice.transmat->m[1][1];
  107.         vdevice.transmat->m[1][1] = costheta * tmp
  108.                       + sintheta * vdevice.transmat->m[2][1];
  109.         vdevice.transmat->m[2][1] = costheta * vdevice.transmat->m[2][1]
  110.                       - sintheta * tmp;
  111.         tmp = vdevice.transmat->m[1][2];
  112.         vdevice.transmat->m[1][2] = costheta * tmp
  113.                       + sintheta * vdevice.transmat->m[2][2];
  114.         vdevice.transmat->m[2][2] = costheta * vdevice.transmat->m[2][2]
  115.                       - sintheta * tmp;
  116.  
  117.         tmp = vdevice.transmat->m[1][3];
  118.         vdevice.transmat->m[1][3] = costheta * tmp
  119.                       + sintheta * vdevice.transmat->m[2][3];
  120.         vdevice.transmat->m[2][3] = costheta * vdevice.transmat->m[2][3]
  121.                       - sintheta * tmp;
  122.         break;
  123.     case 'y':
  124.     case 'Y':
  125.         tmp = vdevice.transmat->m[0][0];
  126.         vdevice.transmat->m[0][0] = costheta * tmp
  127.                       - sintheta * vdevice.transmat->m[2][0];
  128.         vdevice.transmat->m[2][0] = sintheta * tmp
  129.                       + costheta * vdevice.transmat->m[2][0];
  130.         tmp = vdevice.transmat->m[0][1];
  131.         vdevice.transmat->m[0][1] = costheta * tmp
  132.                       - sintheta * vdevice.transmat->m[2][1];
  133.         vdevice.transmat->m[2][1] = sintheta * tmp
  134.                       + costheta * vdevice.transmat->m[2][1];
  135.         tmp = vdevice.transmat->m[0][2];
  136.         vdevice.transmat->m[0][2] = costheta * tmp
  137.                       - sintheta * vdevice.transmat->m[2][2];
  138.         vdevice.transmat->m[2][2] = sintheta * tmp
  139.                       + costheta * vdevice.transmat->m[2][2];
  140.         tmp = vdevice.transmat->m[0][3];
  141.         vdevice.transmat->m[0][3] = costheta * tmp
  142.                       - sintheta * vdevice.transmat->m[2][3];
  143.         vdevice.transmat->m[2][3] = sintheta * tmp
  144.                       + costheta * vdevice.transmat->m[2][3];
  145.         break;
  146.     case 'z':
  147.     case 'Z':
  148.         tmp = vdevice.transmat->m[0][0];
  149.         vdevice.transmat->m[0][0] = costheta * tmp
  150.                       + sintheta * vdevice.transmat->m[1][0];
  151.         vdevice.transmat->m[1][0] = costheta * vdevice.transmat->m[1][0]
  152.                       - sintheta * tmp;
  153.  
  154.         tmp = vdevice.transmat->m[0][1];
  155.         vdevice.transmat->m[0][1] = costheta * tmp
  156.                       + sintheta * vdevice.transmat->m[1][1];
  157.         vdevice.transmat->m[1][1] = costheta * vdevice.transmat->m[1][1]
  158.                       - sintheta * tmp;
  159.  
  160.         tmp = vdevice.transmat->m[0][2];
  161.         vdevice.transmat->m[0][2] = costheta * tmp
  162.                       + sintheta * vdevice.transmat->m[1][2];
  163.         vdevice.transmat->m[1][2] = costheta * vdevice.transmat->m[1][2]
  164.                       - sintheta * tmp;
  165.  
  166.         tmp = vdevice.transmat->m[0][3];
  167.         vdevice.transmat->m[0][3] = costheta * tmp
  168.                       + sintheta * vdevice.transmat->m[1][3];
  169.         vdevice.transmat->m[1][3] = costheta * vdevice.transmat->m[1][3]
  170.                       - sintheta * tmp;
  171.         break;
  172.     default:
  173.         verror("rot: illegal axis of rotation");
  174.     }
  175. }
  176.  
  177. /*
  178.  * rotate
  179.  * 
  180.  * Set up an old style, I've got this real fast way of doing
  181.  * it providing I use ints, rotate.
  182.  *
  183.  */
  184. void
  185. rotate(r, axis)
  186.     Angle    r;
  187.     char    axis;
  188. {
  189.     rot(r / (float)10, axis);
  190. }
  191.