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

  1. /*
  2. Efficient Post-Concatenation of Transformation Matrics
  3. by Joseph M. Cychosz
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. #include    <math.h>
  8. #include    "GraphicsGems.h"
  9.  
  10.  
  11. /*       M4xform.c - Basic 4x4 transformation package.
  12.  *                                     
  13.  *    Description:                             
  14.  *        M4xform.c contains a collection of routines used to perform  
  15.  *        direct post-concatenated transformation operations.
  16.  *         
  17.  *                                     
  18.  *    Contents:                             
  19.  *        M4RotateX        Post-concatenate a x-axis rotation.     
  20.  *        M4RotateY        Post-concatenate a y-axis rotation.     
  21.  *        M4RotateZ        Post-concatenate a z-axis rotation.     
  22.  *        M4Scale            Post-concatenate a scaling.         
  23.  *        M4Translate        Post-concatenate a translation.         
  24.  *        M4ZPerspective    Post-concatenate a z-axis perspective     
  25.  *                        transformation.                 
  26.  *                                     
  27.  *    Externals:                             
  28.  *        cos, sin.                                             
  29.  */ 
  30.      
  31.  
  32.  
  33. /*      M4RotateX - Post-concatenate a x-axis rotation matrix.  */
  34.  
  35. Matrix4    *M4RotateX    (m,a)
  36.         Matrix4    *m;            /* Current transformation matrix*/
  37.         double    a;            /* Rotation angle        */
  38. {
  39.         double    c, s;
  40.         double    t;
  41.         int        i;
  42.  
  43.  
  44.         c = cos (a);    s = sin (a);
  45.  
  46.         for (i = 0 ; i < 4 ; i++) {
  47.              t = m->element[i][1];
  48.              m->element[i][1] = t*c - m->element[i][2]*s;
  49.              m->element[i][2] = t*s + m->element[i][2]*c;
  50.         }
  51.         return (m);
  52. }
  53.  
  54.  
  55. /*     M4RotateY - Post-concatenate a y-axis rotation matrix.  */
  56.  
  57. Matrix4        *M4RotateY    (m,a)
  58.         Matrix4    *m;            /* Current transformation matrix*/
  59.         double    a;            /* Rotation angle        */
  60. {
  61.         double    c, s;
  62.         double    t;
  63.         int        i;
  64.  
  65.         c = cos (a);    s = sin (a);
  66.  
  67.         for (i = 0 ; i < 4 ; i++) {
  68.              t = m->element[i][0];
  69.              m->element[i][0] = t*c + m->element[i][2]*s;
  70.              m->element[i][2] = m->element[i][2]*c - t*s;
  71.         }
  72.         return (m);
  73. }
  74.  
  75.  
  76. /*       M4RotateZ - Post-concatenate a z-axis rotation matrix.   */
  77.  
  78. Matrix4    *M4RotateZ    (m,a)
  79.         Matrix4    *m;            /* Current transformation matrix*/
  80.         double    a;            /* Rotation angle        */
  81. {
  82.         double    c, s;
  83.         double    t;
  84.         int        i;
  85.  
  86.         c = cos (a);    s = sin (a);
  87.  
  88.         for (i = 0 ; i < 4 ; i++) {
  89.              t = m->element[i][0];
  90.              m->element[i][0] = t*c - m->element[i][1]*s;
  91.              m->element[i][1] = t*s + m->element[i][1]*c;
  92.         }
  93.         return (m);
  94. }
  95.  
  96.  
  97.  
  98. /*       M4Scale    - Post-concatenate a scaling.   */
  99.  
  100. Matrix4    *M4Scale    (m,sx,sy,sz)
  101.         Matrix4    *m;            /* Current transformation matrix */
  102.         double    sx, sy, sz;    /* Scale factors about x,y,z */
  103. {
  104.         int        i;
  105.  
  106.         for (i = 0 ; i < 4 ; i++) {
  107.              m->element[i][0] *= sx;
  108.              m->element[i][1] *= sy;
  109.              m->element[i][2] *= sz;
  110.         }
  111.         return (m);
  112. }
  113.  
  114.  
  115. /*       M4Translate - Post-concatenate a translation.   */
  116.  
  117. Matrix4    *M4Translate    (m,tx,ty,tz)
  118.         Matrix4    *m;            /* Current transformation matrix */
  119.         double    tx, ty, tz;    /* Translation distance */
  120. {
  121.         int        i;
  122.  
  123.         for (i = 0 ; i < 4 ; i++) {
  124.              m->element[i][0] += m->element[i][3]*tx;
  125.              m->element[i][1] += m->element[i][3]*ty;
  126.              m->element[i][2] += m->element[i][3]*tz;
  127.         }
  128.         return (m);
  129. }
  130.  
  131.  
  132.         /* M4ZPerspective  Post-concatenate a perspective       */        /*transformation.                                    */
  133.         /*                                                    */
  134.         /* Perspective is along the z-axis with the eye at +z.   */
  135.  
  136.  
  137. Matrix4    *M4ZPerspective    (m,d)
  138.         Matrix4    *m;            /* Current transformation matrix  */
  139.         double    d;            /* Perspective distance        */
  140. {
  141.         int        i;
  142.         double    f = 1. / d;
  143.  
  144.         for (i = 0 ; i < 4 ; i++) {
  145.              m->element[i][3] += m->element[i][2]*f;
  146.              m->element[i][2]  = 0.;
  147.         }
  148.         return (m);
  149. }
  150.     
  151.  
  152.  
  153.  
  154.