Matrix.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Matrix c
  2. */
  3. #include "3d_all.h"
  4. /*
  5. * Compute inverse of 4x4 transformation matrix.
  6. * Code contributed by Jacques Leroy jle@star.be
  7. * Return GL_TRUE for success, GL_FALSE for failure (singular matrix)
  8. */
  9. GLfloat *glu_invert_matrix(GLfloat * m, GLfloat * out)
  10. {
  11. /* NB. OpenGL Matrices are COLUMN major. */
  12. #define SWAP_ROWS(a, b) { GLfloat *_tmp = a; (a)=(b); (b)=_tmp; }
  13. #define MAT(m,r,c) (m)[(c)*4+(r)]
  14. GLfloat wtmp[4][8];
  15. GLfloat m0, m1, m2, m3, s;
  16. GLfloat *r0, *r1, *r2, *r3;
  17. r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
  18. r0[0] = MAT(m, 0, 0), r0[1] = MAT(m, 0, 1),
  19. r0[2] = MAT(m, 0, 2), r0[3] = MAT(m, 0, 3),
  20. r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
  21. r1[0] = MAT(m, 1, 0), r1[1] = MAT(m, 1, 1),
  22. r1[2] = MAT(m, 1, 2), r1[3] = MAT(m, 1, 3),
  23. r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
  24. r2[0] = MAT(m, 2, 0), r2[1] = MAT(m, 2, 1),
  25. r2[2] = MAT(m, 2, 2), r2[3] = MAT(m, 2, 3),
  26. r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
  27. r3[0] = MAT(m, 3, 0), r3[1] = MAT(m, 3, 1),
  28. r3[2] = MAT(m, 3, 2), r3[3] = MAT(m, 3, 3),
  29. r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
  30. /* choose pivot - or die */
  31. if (fabs(r3[0]) > fabs(r2[0]))
  32. SWAP_ROWS(r3, r2);
  33. if (fabs(r2[0]) > fabs(r1[0]))
  34. SWAP_ROWS(r2, r1);
  35. if (fabs(r1[0]) > fabs(r0[0]))
  36. SWAP_ROWS(r1, r0);
  37. if (0.0 == r0[0])
  38. return (NULL);
  39. /* eliminate first variable */
  40. m1 = r1[0] / r0[0];
  41. m2 = r2[0] / r0[0];
  42. m3 = r3[0] / r0[0];
  43. s = r0[1];
  44. r1[1] -= m1 * s;
  45. r2[1] -= m2 * s;
  46. r3[1] -= m3 * s;
  47. s = r0[2];
  48. r1[2] -= m1 * s;
  49. r2[2] -= m2 * s;
  50. r3[2] -= m3 * s;
  51. s = r0[3];
  52. r1[3] -= m1 * s;
  53. r2[3] -= m2 * s;
  54. r3[3] -= m3 * s;
  55. s = r0[4];
  56. if (s != 0.0) {
  57. r1[4] -= m1 * s;
  58. r2[4] -= m2 * s;
  59. r3[4] -= m3 * s;
  60. }
  61. s = r0[5];
  62. if (s != 0.0) {
  63. r1[5] -= m1 * s;
  64. r2[5] -= m2 * s;
  65. r3[5] -= m3 * s;
  66. }
  67. s = r0[6];
  68. if (s != 0.0) {
  69. r1[6] -= m1 * s;
  70. r2[6] -= m2 * s;
  71. r3[6] -= m3 * s;
  72. }
  73. s = r0[7];
  74. if (s != 0.0) {
  75. r1[7] -= m1 * s;
  76. r2[7] -= m2 * s;
  77. r3[7] -= m3 * s;
  78. }
  79. /* choose pivot - or die */
  80. if (fabs(r3[1]) > fabs(r2[1]))
  81. SWAP_ROWS(r3, r2);
  82. if (fabs(r2[1]) > fabs(r1[1]))
  83. SWAP_ROWS(r2, r1);
  84. if (0.0 == r1[1])
  85. return (NULL);
  86. /* eliminate second variable */
  87. m2 = r2[1] / r1[1];
  88. m3 = r3[1] / r1[1];
  89. r2[2] -= m2 * r1[2];
  90. r3[2] -= m3 * r1[2];
  91. r2[3] -= m2 * r1[3];
  92. r3[3] -= m3 * r1[3];
  93. s = r1[4];
  94. if (0.0 != s) {
  95. r2[4] -= m2 * s;
  96. r3[4] -= m3 * s;
  97. }
  98. s = r1[5];
  99. if (0.0 != s) {
  100. r2[5] -= m2 * s;
  101. r3[5] -= m3 * s;
  102. }
  103. s = r1[6];
  104. if (0.0 != s) {
  105. r2[6] -= m2 * s;
  106. r3[6] -= m3 * s;
  107. }
  108. s = r1[7];
  109. if (0.0 != s) {
  110. r2[7] -= m2 * s;
  111. r3[7] -= m3 * s;
  112. }
  113. /* choose pivot - or die */
  114. if (fabs(r3[2]) > fabs(r2[2]))
  115. SWAP_ROWS(r3, r2);
  116. if (0.0 == r2[2])
  117. return (NULL);
  118. /* eliminate third variable */
  119. m3 = r3[2] / r2[2];
  120. r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
  121. r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6], r3[7] -= m3 * r2[7];
  122. /* last check */
  123. if (0.0 == r3[3])
  124. return (NULL);
  125. s = 1.0f / r3[3]; /* now back substitute row 3 */
  126. r3[4] *= s;
  127. r3[5] *= s;
  128. r3[6] *= s;
  129. r3[7] *= s;
  130. m2 = r2[3]; /* now back substitute row 2 */
  131. s = 1.0f / r2[2];
  132. r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
  133. r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
  134. m1 = r1[3];
  135. r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
  136. r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
  137. m0 = r0[3];
  138. r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
  139. r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
  140. m1 = r1[2]; /* now back substitute row 1 */
  141. s = 1.0f / r1[1];
  142. r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
  143. r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
  144. m0 = r0[2];
  145. r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
  146. r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
  147. m0 = r0[1]; /* now back substitute row 0 */
  148. s = 1.0f / r0[0];
  149. r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
  150. r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
  151. MAT(out, 0, 0) = r0[4];
  152. MAT(out, 0, 1) = r0[5], MAT(out, 0, 2) = r0[6];
  153. MAT(out, 0, 3) = r0[7], MAT(out, 1, 0) = r1[4];
  154. MAT(out, 1, 1) = r1[5], MAT(out, 1, 2) = r1[6];
  155. MAT(out, 1, 3) = r1[7], MAT(out, 2, 0) = r2[4];
  156. MAT(out, 2, 1) = r2[5], MAT(out, 2, 2) = r2[6];
  157. MAT(out, 2, 3) = r2[7], MAT(out, 3, 0) = r3[4];
  158. MAT(out, 3, 1) = r3[5], MAT(out, 3, 2) = r3[6];
  159. MAT(out, 3, 3) = r3[7];
  160. return (out);
  161. #undef MAT
  162. #undef SWAP_ROWS
  163. }
  164. GLMATRIX *matrix_all(GLMATRIX * m, BOD * p_pos, QUAT * p_rot, BOD * p_scs)
  165. {
  166. init_matrix(m);
  167. if (p_pos) {
  168. m->_41 = p_pos->x;
  169. m->_42 = p_pos->y;
  170. m->_43 = p_pos->z;
  171. }
  172. if (p_rot) {
  173. quat_to_matrix(m, p_rot);
  174. }
  175. if (p_scs) {
  176. scale_matrix(m, p_scs->x, p_scs->y, p_scs->z);
  177. }
  178. return (m);
  179. }