matrix.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright (C) 2005 Michael Liebscher <johnnycanuck@users.sourceforge.net>
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. /*
  16. * matrix.c: Matrix math routines.
  17. *
  18. * Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
  19. *
  20. */
  21. #include "../wolfiphone.h"
  22. /*
  23. -----------------------------------------------------------------------------
  24. Function: Matrix3x3Multiply -Computes the product of two 3x3 matrices.
  25. Parameters: in1, in2 -[in] 3x3 matrices.
  26. out -[out] result.
  27. Returns: Nothing
  28. Notes:
  29. Product of two 3x3 matrices
  30. ( a b c ) ( r u x ) ( ar + bs + ct au + bv + cw ax + by + cz )
  31. ( d e f ) ( s v y ) = ( dr + es + ft du + ev + fw dx + ey + fz )
  32. ( h i j ) ( t w z ) ( hr + hs + ht iu + iv + iw jx + jy + jz )
  33. -----------------------------------------------------------------------------
  34. */
  35. PUBLIC void Matrix3x3Multiply( mat3_t in1, mat3_t in2, mat3_t out )
  36. {
  37. out[0] = in1[0] * in2[0] + in1[1] * in2[3] + in1[2] * in2[6];
  38. out[1] = in1[0] * in2[1] + in1[1] * in2[4] + in1[2] * in2[7];
  39. out[2] = in1[0] * in2[2] + in1[1] * in2[5] + in1[2] * in2[8];
  40. out[3] = in1[3] * in2[0] + in1[4] * in2[3] + in1[5] * in2[6];
  41. out[4] = in1[3] * in2[1] + in1[4] * in2[4] + in1[5] * in2[7];
  42. out[5] = in1[3] * in2[2] + in1[4] * in2[5] + in1[5] * in2[8];
  43. out[6] = in1[6] * in2[0] + in1[7] * in2[3] + in1[8] * in2[6];
  44. out[7] = in1[6] * in2[1] + in1[7] * in2[4] + in1[8] * in2[7];
  45. out[8] = in1[6] * in2[2] + in1[7] * in2[5] + in1[8] * in2[8];
  46. }
  47. /*
  48. -----------------------------------------------------------------------------
  49. Function: MatrixIdentity -Set matrix to the identity matrix (unit matrix).
  50. Parameters: matrix -[in/out] 4x4 matrix.
  51. Returns: Nothing.
  52. Notes:
  53. -----------------------------------------------------------------------------
  54. */
  55. PUBLIC void MatrixIdentity( mat4_t matrix )
  56. {
  57. matrix[ 0] = 1.0; matrix[ 1] = 0.0; matrix[ 2] = 0.0; matrix[ 3] = 0.0;
  58. matrix[ 4] = 0.0; matrix[ 5] = 1.0; matrix[ 6] = 0.0; matrix[ 7] = 0.0;
  59. matrix[ 8] = 0.0; matrix[ 9] = 0.0; matrix[10] = 1.0; matrix[11] = 0.0;
  60. matrix[12] = 0.0; matrix[13] = 0.0; matrix[14] = 0.0; matrix[15] = 1.0;
  61. }
  62. /*
  63. -----------------------------------------------------------------------------
  64. Function: MatrixInvert -Invert a matrix.
  65. Parameters:
  66. in -[in] Input matrix
  67. out -[out] Output matrix.
  68. Returns: Nothing.
  69. Notes: Matrix MUST be orthonormal
  70. -----------------------------------------------------------------------------
  71. */
  72. PUBLIC void MatrixInvert( mat4_t in, mat4_t out )
  73. {
  74. // Transpose rotation
  75. out[ 0] = in[ 0]; out[ 1] = in[ 4]; out[ 2] = in[ 8];
  76. out[ 4] = in[ 1]; out[ 5] = in[ 5]; out[ 6] = in[ 9];
  77. out[ 8] = in[ 2]; out[ 9] = in[ 6]; out[10] = in[10];
  78. // Clear shearing terms
  79. out[3] = 0.0f; out[7] = 0.0f; out[11] = 0.0f; out[15] = 1.0f;
  80. // Translation is minus the dot of translation and rotations
  81. out[12] = -(in[12] * in[ 0]) - (in[13] * in[ 1]) - (in[14] * in[ 2]);
  82. out[13] = -(in[12] * in[ 4]) - (in[13] * in[ 5]) - (in[14] * in[ 6]);
  83. out[14] = -(in[12] * in[ 8]) - (in[13] * in[ 9]) - (in[14] * in[10]);
  84. }
  85. /*
  86. -----------------------------------------------------------------------------
  87. Function: VectorMatrixMultiply -Multiply a vector by a matrix.
  88. Parameters:
  89. vecIn -[in] Input vector.
  90. m -[in] Input matrix.
  91. vecOut -[out] Output vector.
  92. Returns: Nothing.
  93. Notes:
  94. -----------------------------------------------------------------------------
  95. */
  96. PUBLIC void VectorMatrixMultiply( vec3_t vecIn, mat4_t m, vec3_t vecOut )
  97. {
  98. vecOut[0] = (vecIn[0] * m[ 0]) + (vecIn[1] * m[ 4]) + (vecIn[2] * m[ 8]) + m[12];
  99. vecOut[1] = (vecIn[0] * m[ 1]) + (vecIn[1] * m[ 5]) + (vecIn[2] * m[ 9]) + m[13];
  100. vecOut[2] = (vecIn[0] * m[ 2]) + (vecIn[1] * m[ 6]) + (vecIn[2] * m[10]) + m[14];
  101. }
  102. /*
  103. -----------------------------------------------------------------------------
  104. Function: VectorMatrix3x3Multiply -Multiply a vector by just the 3x3 portion
  105. of a matrix.
  106. Parameters:
  107. in -[in] Input vector.
  108. m -[in] Input matrix.
  109. out -[out] Output vector.
  110. Returns: Nothing.
  111. Notes:
  112. -----------------------------------------------------------------------------
  113. */
  114. PUBLIC void VectorMatrix3x3Multiply( vec3_t in, mat4_t m, vec3_t out )
  115. {
  116. out[0] = (in[0] * m[ 0]) + (in[1] * m[ 4]) + (in[2] * m[ 8]);
  117. out[1] = (in[0] * m[ 1]) + (in[1] * m[ 5]) + (in[2] * m[ 9]);
  118. out[2] = (in[0] * m[ 2]) + (in[1] * m[ 6]) + (in[2] * m[10]);
  119. }