home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter52 / xsharp14.exe / L4.C < prev    next >
Text File  |  1991-12-01  |  1KB  |  40 lines

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