Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
4.9 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: math.c
  3. *
  4. * Misc. useful math utility functions.
  5. *
  6. * Copyright (c) 1994 Microsoft Corporation
  7. *
  8. \**************************************************************************/
  9. #include <windows.h>
  10. #include <math.h>
  11. #include <d3dx8.h>
  12. #include "d3dsaver.h"
  13. #include "FlyingObjects.h"
  14. #define ZERO_EPS 0.00000001
  15. POINT3D ss_ptZero(0.0f, 0.0f, 0.0f);
  16. void ss_xformPoint(POINT3D *ptOut, POINT3D *ptIn, MATRIX *mat)
  17. {
  18. double x, y, z;
  19. x = (ptIn->x * mat->M[0][0]) + (ptIn->y * mat->M[0][1]) +
  20. (ptIn->z * mat->M[0][2]) + mat->M[0][3];
  21. y = (ptIn->x * mat->M[1][0]) + (ptIn->y * mat->M[1][1]) +
  22. (ptIn->z * mat->M[1][2]) + mat->M[1][3];
  23. z = (ptIn->x * mat->M[2][0]) + (ptIn->y * mat->M[2][1]) +
  24. (ptIn->z * mat->M[2][2]) + mat->M[2][3];
  25. ptOut->x = (float) x;
  26. ptOut->y = (float) y;
  27. ptOut->z = (float) z;
  28. }
  29. void ss_xformNorm(POINT3D *ptOut, POINT3D *ptIn, MATRIX *mat)
  30. {
  31. double x, y, z;
  32. double len;
  33. x = (ptIn->x * mat->M[0][0]) + (ptIn->y * mat->M[0][1]) +
  34. (ptIn->z * mat->M[0][2]);
  35. y = (ptIn->x * mat->M[1][0]) + (ptIn->y * mat->M[1][1]) +
  36. (ptIn->z * mat->M[1][2]);
  37. z = (ptIn->x * mat->M[2][0]) + (ptIn->y * mat->M[2][1]) +
  38. (ptIn->z * mat->M[2][2]);
  39. len = (x * x) + (y * y) + (z * z);
  40. if (len >= ZERO_EPS)
  41. len = 1.0 / sqrt(len);
  42. else
  43. len = 1.0;
  44. ptOut->x = (float) (x * len);
  45. ptOut->y = (float) (y * len);
  46. ptOut->z = (float) (z * len);
  47. return;
  48. }
  49. void ss_matrixIdent(MATRIX *mat)
  50. {
  51. mat->M[0][0] = 1.0f; mat->M[0][1] = 0.0f;
  52. mat->M[0][2] = 0.0f; mat->M[0][3] = 0.0f;
  53. mat->M[1][0] = 0.0f; mat->M[1][1] = 1.0f;
  54. mat->M[1][2] = 0.0f; mat->M[1][3] = 0.0f;
  55. mat->M[2][0] = 0.0f; mat->M[2][1] = 0.0f;
  56. mat->M[2][2] = 1.0f; mat->M[2][3] = 0.0f;
  57. mat->M[3][0] = 0.0f; mat->M[3][1] = 0.0f;
  58. mat->M[3][2] = 0.0f; mat->M[3][3] = 1.0f;
  59. }
  60. void ss_matrixRotate(MATRIX *m, double xTheta, double yTheta, double zTheta)
  61. {
  62. float xScale, yScale, zScale;
  63. float sinX, cosX;
  64. float sinY, cosY;
  65. float sinZ, cosZ;
  66. xScale = m->M[0][0];
  67. yScale = m->M[1][1];
  68. zScale = m->M[2][2];
  69. sinX = (float) sin(xTheta);
  70. cosX = (float) cos(xTheta);
  71. sinY = (float) sin(yTheta);
  72. cosY = (float) cos(yTheta);
  73. sinZ = (float) sin(zTheta);
  74. cosZ = (float) cos(zTheta);
  75. m->M[0][0] = (float) ((cosZ * cosY) * xScale);
  76. m->M[0][1] = (float) ((cosZ * -sinY * -sinX + sinZ * cosX) * yScale);
  77. m->M[0][2] = (float) ((cosZ * -sinY * cosX + sinZ * sinX) * zScale);
  78. m->M[1][0] = (float) (-sinZ * cosY * xScale);
  79. m->M[1][1] = (float) ((-sinZ * -sinY * -sinX + cosZ * cosX) * yScale);
  80. m->M[1][2] = (float) ((-sinZ * -sinY * cosX + cosZ * sinX) * zScale);
  81. m->M[2][0] = (float) (sinY * xScale);
  82. m->M[2][1] = (float) (cosY * -sinX * yScale);
  83. m->M[2][2] = (float) (cosY * cosX * zScale);
  84. }
  85. void ss_matrixTranslate(MATRIX *m, double xTrans, double yTrans,
  86. double zTrans)
  87. {
  88. m->M[0][3] = (float) xTrans;
  89. m->M[1][3] = (float) yTrans;
  90. m->M[2][3] = (float) zTrans;
  91. }
  92. void ss_matrixMult( MATRIX *m1, MATRIX *m2, MATRIX *m3 )
  93. {
  94. int i, j;
  95. for( j = 0; j < 4; j ++ ) {
  96. for( i = 0; i < 4; i ++ ) {
  97. m1->M[j][i] = m2->M[j][0] * m3->M[0][i] +
  98. m2->M[j][1] * m3->M[1][i] +
  99. m2->M[j][2] * m3->M[2][i] +
  100. m2->M[j][3] * m3->M[3][i];
  101. }
  102. }
  103. }
  104. void ss_calcNorm(POINT3D *norm, POINT3D *p1, POINT3D *p2, POINT3D *p3)
  105. {
  106. float crossX, crossY, crossZ;
  107. float abX, abY, abZ;
  108. float acX, acY, acZ;
  109. float sqrLength;
  110. float invLength;
  111. abX = p2->x - p1->x; // calculate p2 - p1
  112. abY = p2->y - p1->y;
  113. abZ = p2->z - p1->z;
  114. acX = p3->x - p1->x; // calculate p3 - p1
  115. acY = p3->y - p1->y;
  116. acZ = p3->z - p1->z;
  117. crossX = (abY * acZ) - (abZ * acY); // get cross product
  118. crossY = (abZ * acX) - (abX * acZ); // (p2 - p1) X (p3 - p1)
  119. crossZ = (abX * acY) - (abY * acX);
  120. sqrLength = (crossX * crossX) + (crossY * crossY) +
  121. (crossZ * crossZ);
  122. if (sqrLength > ZERO_EPS)
  123. invLength = (float) (1.0 / sqrt(sqrLength));
  124. else
  125. invLength = 1.0f;
  126. norm->x = crossX * invLength;
  127. norm->y = crossY * invLength;
  128. norm->z = crossZ * invLength;
  129. }
  130. void ss_normalizeNorm( POINT3D *n )
  131. {
  132. float len;
  133. len = (n->x * n->x) + (n->y * n->y) + (n->z * n->z);
  134. if (len > ZERO_EPS)
  135. len = (float) (1.0 / sqrt(len));
  136. else
  137. len = 1.0f;
  138. n->x *= len;
  139. n->y *= len;
  140. n->z *= len;
  141. }
  142. void ss_normalizeNorms(POINT3D *p, ULONG cPts)
  143. {
  144. float len;
  145. ULONG i;
  146. for (i = 0; i < cPts; i++, p++) {
  147. len = (p->x * p->x) + (p->y * p->y) + (p->z * p->z);
  148. if (len > ZERO_EPS)
  149. len = (float) (1.0 / sqrt(len));
  150. else
  151. len = 1.0f;
  152. p->x *= len;
  153. p->y *= len;
  154. p->z *= len;
  155. }
  156. }