home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter52 / l52-4.c < prev    next >
C/C++ Source or Header  |  1997-06-18  |  2KB  |  42 lines

  1. /* Fixed point matrix arithmetic functions
  2. Tested with Borland C++ 4.02 in small model by Jim Mischel 12/16/94.
  3. */
  4.  
  5. #include "polygon.h"
  6.  
  7. /* Matrix multiplies Xform by SourceVec, and stores the result in DestVec. 
  8. Multiplies a 4x4 matrix times a 4x1 matrix; the result is a 4x1 matrix. Cheats 
  9. by assuming the W coord is 1 and bottom row of matrix is 0 0 0 1, and doesn't 
  10. bother to set the W coordinate of the destination */
  11. void XformVec(Xform WorkingXform, Fixedpoint *SourceVec,
  12.    Fixedpoint *DestVec)
  13. {
  14.    int i;
  15.  
  16.    for (i=0; i<3; i++)
  17.       DestVec[i] = FixedMul(WorkingXform[i][0], SourceVec[0]) +
  18.             FixedMul(WorkingXform[i][1], SourceVec[1]) +
  19.             FixedMul(WorkingXform[i][2], SourceVec[2]) +
  20.             WorkingXform[i][3];   /* no need to multiply by W = 1 */
  21. }
  22.  
  23. /* Matrix multiplies SourceXform1 by SourceXform2 and stores result in 
  24.  DestXform. Multiplies a 4x4 matrix times a 4x4 matrix; result is a 4x4 matrix.
  25.  Cheats by assuming bottom row of each matrix is 0 0 0 1, and doesn't bother 
  26.  to set the bottom row of the destination */
  27. void ConcatXforms(Xform SourceXform1, Xform SourceXform2,
  28.    Xform DestXform)
  29. {
  30.    int i, j;
  31.  
  32.    for (i=0; i<3; i++) {
  33.       for (j=0; j<4; j++)
  34.          DestXform[i][j] =
  35.                FixedMul(SourceXform1[i][0], SourceXform2[0][j]) +
  36.                FixedMul(SourceXform1[i][1], SourceXform2[1][j]) +
  37.                FixedMul(SourceXform1[i][2], SourceXform2[2][j]) +
  38.                SourceXform1[i][3];
  39.    }
  40. }
  41.  
  42.