/dlls/d3drm/math.c

https://github.com/YokoZar/wine · C · 278 lines · 205 code · 35 blank · 38 comment · 11 complexity · b46a2d6e4b142e7f7113213244f4b744 MD5 · raw file

  1. /*
  2. * Copyright 2007 David Adam
  3. * Copyright 2007 Vijay Kiran Kamuju
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  18. */
  19. #define NONAMELESSUNION
  20. #include <math.h>
  21. #include <stdarg.h>
  22. #include "windef.h"
  23. #include "winbase.h"
  24. #include "wingdi.h"
  25. #include "d3drmdef.h"
  26. /* Create a RGB color from its components */
  27. D3DCOLOR WINAPI D3DRMCreateColorRGB(D3DVALUE red, D3DVALUE green, D3DVALUE blue)
  28. {
  29. return (D3DRMCreateColorRGBA(red, green, blue, 255.0));
  30. }
  31. /* Create a RGBA color from its components */
  32. D3DCOLOR WINAPI D3DRMCreateColorRGBA(D3DVALUE red, D3DVALUE green, D3DVALUE blue, D3DVALUE alpha)
  33. {
  34. int Red, Green, Blue, Alpha;
  35. Red=floor(red*255);
  36. Green=floor(green*255);
  37. Blue=floor(blue*255);
  38. Alpha=floor(alpha*255);
  39. if (red < 0) Red=0;
  40. if (red > 1) Red=255;
  41. if (green < 0) Green=0;
  42. if (green > 1) Green=255;
  43. if (blue < 0) Blue=0;
  44. if (blue > 1) Blue=255;
  45. if (alpha < 0) Alpha=0;
  46. if (alpha > 1) Alpha=255;
  47. return (RGBA_MAKE(Red, Green, Blue, Alpha));
  48. }
  49. /* Determine the alpha part of a color */
  50. D3DVALUE WINAPI D3DRMColorGetAlpha(D3DCOLOR color)
  51. {
  52. return (RGBA_GETALPHA(color)/255.0);
  53. }
  54. /* Determine the blue part of a color */
  55. D3DVALUE WINAPI D3DRMColorGetBlue(D3DCOLOR color)
  56. {
  57. return (RGBA_GETBLUE(color)/255.0);
  58. }
  59. /* Determine the green part of a color */
  60. D3DVALUE WINAPI D3DRMColorGetGreen(D3DCOLOR color)
  61. {
  62. return (RGBA_GETGREEN(color)/255.0);
  63. }
  64. /* Determine the red part of a color */
  65. D3DVALUE WINAPI D3DRMColorGetRed(D3DCOLOR color)
  66. {
  67. return (RGBA_GETRED(color)/255.0);
  68. }
  69. /* Product of 2 quaternions */
  70. LPD3DRMQUATERNION WINAPI D3DRMQuaternionMultiply(LPD3DRMQUATERNION q, LPD3DRMQUATERNION a, LPD3DRMQUATERNION b)
  71. {
  72. D3DRMQUATERNION temp;
  73. D3DVECTOR cross_product;
  74. D3DRMVectorCrossProduct(&cross_product, &a->v, &b->v);
  75. temp.s = a->s * b->s - D3DRMVectorDotProduct(&a->v, &b->v);
  76. temp.v.u1.x = a->s * b->v.u1.x + b->s * a->v.u1.x + cross_product.u1.x;
  77. temp.v.u2.y = a->s * b->v.u2.y + b->s * a->v.u2.y + cross_product.u2.y;
  78. temp.v.u3.z = a->s * b->v.u3.z + b->s * a->v.u3.z + cross_product.u3.z;
  79. *q = temp;
  80. return q;
  81. }
  82. /* Matrix for the Rotation that a unit quaternion represents */
  83. void WINAPI D3DRMMatrixFromQuaternion(D3DRMMATRIX4D m, LPD3DRMQUATERNION q)
  84. {
  85. D3DVALUE w,x,y,z;
  86. w = q->s;
  87. x = q->v.u1.x;
  88. y = q->v.u2.y;
  89. z = q->v.u3.z;
  90. m[0][0] = 1.0-2.0*(y*y+z*z);
  91. m[1][1] = 1.0-2.0*(x*x+z*z);
  92. m[2][2] = 1.0-2.0*(x*x+y*y);
  93. m[1][0] = 2.0*(x*y+z*w);
  94. m[0][1] = 2.0*(x*y-z*w);
  95. m[2][0] = 2.0*(x*z-y*w);
  96. m[0][2] = 2.0*(x*z+y*w);
  97. m[2][1] = 2.0*(y*z+x*w);
  98. m[1][2] = 2.0*(y*z-x*w);
  99. m[3][0] = 0.0;
  100. m[3][1] = 0.0;
  101. m[3][2] = 0.0;
  102. m[0][3] = 0.0;
  103. m[1][3] = 0.0;
  104. m[2][3] = 0.0;
  105. m[3][3] = 1.0;
  106. }
  107. /* Return a unit quaternion that represents a rotation of an angle around an axis */
  108. LPD3DRMQUATERNION WINAPI D3DRMQuaternionFromRotation(LPD3DRMQUATERNION q, LPD3DVECTOR v, D3DVALUE theta)
  109. {
  110. q->s = cos(theta/2.0);
  111. D3DRMVectorScale(&q->v, D3DRMVectorNormalize(v), sin(theta/2.0));
  112. return q;
  113. }
  114. /* Interpolation between two quaternions */
  115. LPD3DRMQUATERNION WINAPI D3DRMQuaternionSlerp(LPD3DRMQUATERNION q, LPD3DRMQUATERNION a, LPD3DRMQUATERNION b, D3DVALUE alpha)
  116. {
  117. D3DVALUE dot, epsilon, temp, theta, u;
  118. D3DVECTOR v1, v2;
  119. dot = a->s * b->s + D3DRMVectorDotProduct(&a->v, &b->v);
  120. epsilon = 1.0f;
  121. temp = 1.0f - alpha;
  122. u = alpha;
  123. if (dot < 0.0)
  124. {
  125. epsilon = -1.0;
  126. dot = -dot;
  127. }
  128. if( 1.0f - dot > 0.001f )
  129. {
  130. theta = acos(dot);
  131. temp = sin(theta * temp) / sin(theta);
  132. u = sin(theta * alpha) / sin(theta);
  133. }
  134. q->s = temp * a->s + epsilon * u * b->s;
  135. D3DRMVectorScale(&v1, &a->v, temp);
  136. D3DRMVectorScale(&v2, &b->v, epsilon * u);
  137. D3DRMVectorAdd(&q->v, &v1, &v2);
  138. return q;
  139. }
  140. /* Add Two Vectors */
  141. LPD3DVECTOR WINAPI D3DRMVectorAdd(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
  142. {
  143. D3DVECTOR temp;
  144. temp.u1.x=s1->u1.x + s2->u1.x;
  145. temp.u2.y=s1->u2.y + s2->u2.y;
  146. temp.u3.z=s1->u3.z + s2->u3.z;
  147. *d = temp;
  148. return d;
  149. }
  150. /* Subtract Two Vectors */
  151. LPD3DVECTOR WINAPI D3DRMVectorSubtract(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
  152. {
  153. D3DVECTOR temp;
  154. temp.u1.x=s1->u1.x - s2->u1.x;
  155. temp.u2.y=s1->u2.y - s2->u2.y;
  156. temp.u3.z=s1->u3.z - s2->u3.z;
  157. *d = temp;
  158. return d;
  159. }
  160. /* Cross Product of Two Vectors */
  161. LPD3DVECTOR WINAPI D3DRMVectorCrossProduct(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
  162. {
  163. D3DVECTOR temp;
  164. temp.u1.x=s1->u2.y * s2->u3.z - s1->u3.z * s2->u2.y;
  165. temp.u2.y=s1->u3.z * s2->u1.x - s1->u1.x * s2->u3.z;
  166. temp.u3.z=s1->u1.x * s2->u2.y - s1->u2.y * s2->u1.x;
  167. *d = temp;
  168. return d;
  169. }
  170. /* Dot Product of Two vectors */
  171. D3DVALUE WINAPI D3DRMVectorDotProduct(LPD3DVECTOR s1, LPD3DVECTOR s2)
  172. {
  173. D3DVALUE dot_product;
  174. dot_product=s1->u1.x * s2->u1.x + s1->u2.y * s2->u2.y + s1->u3.z * s2->u3.z;
  175. return dot_product;
  176. }
  177. /* Norm of a vector */
  178. D3DVALUE WINAPI D3DRMVectorModulus(LPD3DVECTOR v)
  179. {
  180. D3DVALUE result;
  181. result=sqrt(v->u1.x * v->u1.x + v->u2.y * v->u2.y + v->u3.z * v->u3.z);
  182. return result;
  183. }
  184. /* Normalize a vector. Returns (1,0,0) if INPUT is the NULL vector. */
  185. LPD3DVECTOR WINAPI D3DRMVectorNormalize(LPD3DVECTOR u)
  186. {
  187. D3DVALUE modulus = D3DRMVectorModulus(u);
  188. if(modulus)
  189. {
  190. D3DRMVectorScale(u,u,1.0/modulus);
  191. }
  192. else
  193. {
  194. u->u1.x=1.0;
  195. u->u2.y=0.0;
  196. u->u3.z=0.0;
  197. }
  198. return u;
  199. }
  200. /* Returns a random unit vector */
  201. LPD3DVECTOR WINAPI D3DRMVectorRandom(LPD3DVECTOR d)
  202. {
  203. d->u1.x = rand();
  204. d->u2.y = rand();
  205. d->u3.z = rand();
  206. D3DRMVectorNormalize(d);
  207. return d;
  208. }
  209. /* Reflection of a vector on a surface */
  210. LPD3DVECTOR WINAPI D3DRMVectorReflect(LPD3DVECTOR r, LPD3DVECTOR ray, LPD3DVECTOR norm)
  211. {
  212. D3DVECTOR sca, temp;
  213. D3DRMVectorSubtract(&temp, D3DRMVectorScale(&sca, norm, 2.0*D3DRMVectorDotProduct(ray,norm)), ray);
  214. *r = temp;
  215. return r;
  216. }
  217. /* Rotation of a vector */
  218. LPD3DVECTOR WINAPI D3DRMVectorRotate(LPD3DVECTOR r, LPD3DVECTOR v, LPD3DVECTOR axis, D3DVALUE theta)
  219. {
  220. D3DRMQUATERNION quaternion1, quaternion2, quaternion3;
  221. D3DVECTOR norm;
  222. quaternion1.s = cos(theta * 0.5f);
  223. quaternion2.s = cos(theta * 0.5f);
  224. norm = *D3DRMVectorNormalize(axis);
  225. D3DRMVectorScale(&quaternion1.v, &norm, sin(theta * 0.5f));
  226. D3DRMVectorScale(&quaternion2.v, &norm, -sin(theta * 0.5f));
  227. quaternion3.s = 0.0;
  228. quaternion3.v = *v;
  229. D3DRMQuaternionMultiply(&quaternion1, &quaternion1, &quaternion3);
  230. D3DRMQuaternionMultiply(&quaternion1, &quaternion1, &quaternion2);
  231. *r = *D3DRMVectorNormalize(&quaternion1.v);
  232. return r;
  233. }
  234. /* Scale a vector */
  235. LPD3DVECTOR WINAPI D3DRMVectorScale(LPD3DVECTOR d, LPD3DVECTOR s, D3DVALUE factor)
  236. {
  237. D3DVECTOR temp;
  238. temp.u1.x=factor * s->u1.x;
  239. temp.u2.y=factor * s->u2.y;
  240. temp.u3.z=factor * s->u3.z;
  241. *d = temp;
  242. return d;
  243. }