PageRenderTime 31ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/dlls/d3dx9_36/math.c

https://github.com/aragaer/wine
C | 1596 lines | 1316 code | 245 blank | 35 comment | 68 complexity | 078ced061ac059ac1bbc52b5bdccf1db MD5 | raw file
  1. /*
  2. * Mathematical operations specific to D3DX9.
  3. *
  4. * Copyright (C) 2008 David Adam
  5. * Copyright (C) 2008 Luis Busquets
  6. * Copyright (C) 2008 Jérôme Gardou
  7. * Copyright (C) 2008 Philip Nilsson
  8. * Copyright (C) 2008 Henri Verbeet
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #define NONAMELESSUNION
  25. #include "config.h"
  26. #include "wine/port.h"
  27. #include "windef.h"
  28. #include "wingdi.h"
  29. #include "d3dx9_36_private.h"
  30. #include "wine/debug.h"
  31. WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
  32. static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl;
  33. typedef struct ID3DXMatrixStackImpl
  34. {
  35. ID3DXMatrixStack ID3DXMatrixStack_iface;
  36. LONG ref;
  37. unsigned int current;
  38. unsigned int stack_size;
  39. D3DXMATRIX *stack;
  40. } ID3DXMatrixStackImpl;
  41. /*_________________D3DXColor____________________*/
  42. D3DXCOLOR* WINAPI D3DXColorAdjustContrast(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
  43. {
  44. pout->r = 0.5f + s * (pc->r - 0.5f);
  45. pout->g = 0.5f + s * (pc->g - 0.5f);
  46. pout->b = 0.5f + s * (pc->b - 0.5f);
  47. pout->a = pc->a;
  48. return pout;
  49. }
  50. D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
  51. {
  52. FLOAT grey;
  53. grey = pc->r * 0.2125f + pc->g * 0.7154f + pc->b * 0.0721f;
  54. pout->r = grey + s * (pc->r - grey);
  55. pout->g = grey + s * (pc->g - grey);
  56. pout->b = grey + s * (pc->b - grey);
  57. pout->a = pc->a;
  58. return pout;
  59. }
  60. /*_________________Misc__________________________*/
  61. FLOAT WINAPI D3DXFresnelTerm(FLOAT costheta, FLOAT refractionindex)
  62. {
  63. FLOAT a, d, g, result;
  64. g = sqrt(refractionindex * refractionindex + costheta * costheta - 1.0f);
  65. a = g + costheta;
  66. d = g - costheta;
  67. result = ( costheta * a - 1.0f ) * ( costheta * a - 1.0f ) / ( ( costheta * d + 1.0f ) * ( costheta * d + 1.0f ) ) + 1.0f;
  68. result = result * 0.5f * d * d / ( a * a );
  69. return result;
  70. }
  71. /*_________________D3DXMatrix____________________*/
  72. D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR3 *rotationcenter, CONST D3DXQUATERNION *rotation, CONST D3DXVECTOR3 *translation)
  73. {
  74. D3DXMATRIX m1, m2, m3, m4, m5;
  75. D3DXMatrixScaling(&m1, scaling, scaling, scaling);
  76. if ( !rotationcenter )
  77. {
  78. D3DXMatrixIdentity(&m2);
  79. D3DXMatrixIdentity(&m4);
  80. }
  81. else
  82. {
  83. D3DXMatrixTranslation(&m2, -rotationcenter->x, -rotationcenter->y, -rotationcenter->z);
  84. D3DXMatrixTranslation(&m4, rotationcenter->x, rotationcenter->y, rotationcenter->z);
  85. }
  86. if ( !rotation ) D3DXMatrixIdentity(&m3);
  87. else D3DXMatrixRotationQuaternion(&m3, rotation);
  88. if ( !translation ) D3DXMatrixIdentity(&m5);
  89. else D3DXMatrixTranslation(&m5, translation->x, translation->y, translation->z);
  90. D3DXMatrixMultiply(&m1, &m1, &m2);
  91. D3DXMatrixMultiply(&m1, &m1, &m3);
  92. D3DXMatrixMultiply(&m1, &m1, &m4);
  93. D3DXMatrixMultiply(pout, &m1, &m5);
  94. return pout;
  95. }
  96. D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation2D(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR2 *protationcenter, FLOAT rotation, CONST D3DXVECTOR2 *ptranslation)
  97. {
  98. D3DXMATRIX m1, m2, m3, m4, m5;
  99. D3DXQUATERNION rot;
  100. D3DXVECTOR3 rot_center, trans;
  101. rot.w=cos(rotation/2.0f);
  102. rot.x=0.0f;
  103. rot.y=0.0f;
  104. rot.z=sin(rotation/2.0f);
  105. if ( protationcenter )
  106. {
  107. rot_center.x=protationcenter->x;
  108. rot_center.y=protationcenter->y;
  109. rot_center.z=0.0f;
  110. }
  111. else
  112. {
  113. rot_center.x=0.0f;
  114. rot_center.y=0.0f;
  115. rot_center.z=0.0f;
  116. }
  117. if ( ptranslation )
  118. {
  119. trans.x=ptranslation->x;
  120. trans.y=ptranslation->y;
  121. trans.z=0.0f;
  122. }
  123. else
  124. {
  125. trans.x=0.0f;
  126. trans.y=0.0f;
  127. trans.z=0.0f;
  128. }
  129. D3DXMatrixScaling(&m1, scaling, scaling, 1.0f);
  130. D3DXMatrixTranslation(&m2, -rot_center.x, -rot_center.y, -rot_center.z);
  131. D3DXMatrixTranslation(&m4, rot_center.x, rot_center.y, rot_center.z);
  132. D3DXMatrixRotationQuaternion(&m3, &rot);
  133. D3DXMatrixTranslation(&m5, trans.x, trans.y, trans.z);
  134. D3DXMatrixMultiply(&m1, &m1, &m2);
  135. D3DXMatrixMultiply(&m1, &m1, &m3);
  136. D3DXMatrixMultiply(&m1, &m1, &m4);
  137. D3DXMatrixMultiply(pout, &m1, &m5);
  138. return pout;
  139. }
  140. HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, CONST D3DXMATRIX *pm)
  141. {
  142. D3DXMATRIX normalized;
  143. D3DXVECTOR3 vec;
  144. /*Compute the scaling part.*/
  145. vec.x=pm->u.m[0][0];
  146. vec.y=pm->u.m[0][1];
  147. vec.z=pm->u.m[0][2];
  148. poutscale->x=D3DXVec3Length(&vec);
  149. vec.x=pm->u.m[1][0];
  150. vec.y=pm->u.m[1][1];
  151. vec.z=pm->u.m[1][2];
  152. poutscale->y=D3DXVec3Length(&vec);
  153. vec.x=pm->u.m[2][0];
  154. vec.y=pm->u.m[2][1];
  155. vec.z=pm->u.m[2][2];
  156. poutscale->z=D3DXVec3Length(&vec);
  157. /*Compute the translation part.*/
  158. pouttranslation->x=pm->u.m[3][0];
  159. pouttranslation->y=pm->u.m[3][1];
  160. pouttranslation->z=pm->u.m[3][2];
  161. /*Let's calculate the rotation now*/
  162. if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) ) return D3DERR_INVALIDCALL;
  163. normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
  164. normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
  165. normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
  166. normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
  167. normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
  168. normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
  169. normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
  170. normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
  171. normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
  172. D3DXQuaternionRotationMatrix(poutrotation,&normalized);
  173. return S_OK;
  174. }
  175. FLOAT WINAPI D3DXMatrixDeterminant(CONST D3DXMATRIX *pm)
  176. {
  177. D3DXVECTOR4 minor, v1, v2, v3;
  178. FLOAT det;
  179. v1.x = pm->u.m[0][0]; v1.y = pm->u.m[1][0]; v1.z = pm->u.m[2][0]; v1.w = pm->u.m[3][0];
  180. v2.x = pm->u.m[0][1]; v2.y = pm->u.m[1][1]; v2.z = pm->u.m[2][1]; v2.w = pm->u.m[3][1];
  181. v3.x = pm->u.m[0][2]; v3.y = pm->u.m[1][2]; v3.z = pm->u.m[2][2]; v3.w = pm->u.m[3][2];
  182. D3DXVec4Cross(&minor, &v1, &v2, &v3);
  183. det = - (pm->u.m[0][3] * minor.x + pm->u.m[1][3] * minor.y + pm->u.m[2][3] * minor.z + pm->u.m[3][3] * minor.w);
  184. return det;
  185. }
  186. D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, CONST D3DXMATRIX *pm)
  187. {
  188. int a, i, j;
  189. D3DXMATRIX out;
  190. D3DXVECTOR4 v, vec[3];
  191. FLOAT det;
  192. det = D3DXMatrixDeterminant(pm);
  193. if ( !det ) return NULL;
  194. if ( pdeterminant ) *pdeterminant = det;
  195. for (i=0; i<4; i++)
  196. {
  197. for (j=0; j<4; j++)
  198. {
  199. if (j != i )
  200. {
  201. a = j;
  202. if ( j > i ) a = a-1;
  203. vec[a].x = pm->u.m[j][0];
  204. vec[a].y = pm->u.m[j][1];
  205. vec[a].z = pm->u.m[j][2];
  206. vec[a].w = pm->u.m[j][3];
  207. }
  208. }
  209. D3DXVec4Cross(&v, &vec[0], &vec[1], &vec[2]);
  210. out.u.m[0][i] = pow(-1.0f, i) * v.x / det;
  211. out.u.m[1][i] = pow(-1.0f, i) * v.y / det;
  212. out.u.m[2][i] = pow(-1.0f, i) * v.z / det;
  213. out.u.m[3][i] = pow(-1.0f, i) * v.w / det;
  214. }
  215. *pout = out;
  216. return pout;
  217. }
  218. D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
  219. {
  220. D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
  221. D3DXVec3Subtract(&vec2, pat, peye);
  222. D3DXVec3Normalize(&vec, &vec2);
  223. D3DXVec3Cross(&right, pup, &vec);
  224. D3DXVec3Cross(&up, &vec, &right);
  225. D3DXVec3Normalize(&rightn, &right);
  226. D3DXVec3Normalize(&upn, &up);
  227. pout->u.m[0][0] = rightn.x;
  228. pout->u.m[1][0] = rightn.y;
  229. pout->u.m[2][0] = rightn.z;
  230. pout->u.m[3][0] = -D3DXVec3Dot(&rightn,peye);
  231. pout->u.m[0][1] = upn.x;
  232. pout->u.m[1][1] = upn.y;
  233. pout->u.m[2][1] = upn.z;
  234. pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
  235. pout->u.m[0][2] = vec.x;
  236. pout->u.m[1][2] = vec.y;
  237. pout->u.m[2][2] = vec.z;
  238. pout->u.m[3][2] = -D3DXVec3Dot(&vec, peye);
  239. pout->u.m[0][3] = 0.0f;
  240. pout->u.m[1][3] = 0.0f;
  241. pout->u.m[2][3] = 0.0f;
  242. pout->u.m[3][3] = 1.0f;
  243. return pout;
  244. }
  245. D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
  246. {
  247. D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
  248. D3DXVec3Subtract(&vec2, pat, peye);
  249. D3DXVec3Normalize(&vec, &vec2);
  250. D3DXVec3Cross(&right, pup, &vec);
  251. D3DXVec3Cross(&up, &vec, &right);
  252. D3DXVec3Normalize(&rightn, &right);
  253. D3DXVec3Normalize(&upn, &up);
  254. pout->u.m[0][0] = -rightn.x;
  255. pout->u.m[1][0] = -rightn.y;
  256. pout->u.m[2][0] = -rightn.z;
  257. pout->u.m[3][0] = D3DXVec3Dot(&rightn,peye);
  258. pout->u.m[0][1] = upn.x;
  259. pout->u.m[1][1] = upn.y;
  260. pout->u.m[2][1] = upn.z;
  261. pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
  262. pout->u.m[0][2] = -vec.x;
  263. pout->u.m[1][2] = -vec.y;
  264. pout->u.m[2][2] = -vec.z;
  265. pout->u.m[3][2] = D3DXVec3Dot(&vec, peye);
  266. pout->u.m[0][3] = 0.0f;
  267. pout->u.m[1][3] = 0.0f;
  268. pout->u.m[2][3] = 0.0f;
  269. pout->u.m[3][3] = 1.0f;
  270. return pout;
  271. }
  272. D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
  273. {
  274. D3DXMATRIX out;
  275. int i,j;
  276. for (i=0; i<4; i++)
  277. {
  278. for (j=0; j<4; j++)
  279. {
  280. out.u.m[i][j] = pm1->u.m[i][0] * pm2->u.m[0][j] + pm1->u.m[i][1] * pm2->u.m[1][j] + pm1->u.m[i][2] * pm2->u.m[2][j] + pm1->u.m[i][3] * pm2->u.m[3][j];
  281. }
  282. }
  283. *pout = out;
  284. return pout;
  285. }
  286. D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
  287. {
  288. D3DXMatrixMultiply(pout, pm1, pm2);
  289. D3DXMatrixTranspose(pout, pout);
  290. return pout;
  291. }
  292. D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
  293. {
  294. D3DXMatrixIdentity(pout);
  295. pout->u.m[0][0] = 2.0f / w;
  296. pout->u.m[1][1] = 2.0f / h;
  297. pout->u.m[2][2] = 1.0f / (zf - zn);
  298. pout->u.m[3][2] = zn / (zn - zf);
  299. return pout;
  300. }
  301. D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
  302. {
  303. D3DXMatrixIdentity(pout);
  304. pout->u.m[0][0] = 2.0f / (r - l);
  305. pout->u.m[1][1] = 2.0f / (t - b);
  306. pout->u.m[2][2] = 1.0f / (zf -zn);
  307. pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
  308. pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
  309. pout->u.m[3][2] = zn / (zn -zf);
  310. return pout;
  311. }
  312. D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
  313. {
  314. D3DXMatrixIdentity(pout);
  315. pout->u.m[0][0] = 2.0f / (r - l);
  316. pout->u.m[1][1] = 2.0f / (t - b);
  317. pout->u.m[2][2] = 1.0f / (zn -zf);
  318. pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
  319. pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
  320. pout->u.m[3][2] = zn / (zn -zf);
  321. return pout;
  322. }
  323. D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
  324. {
  325. D3DXMatrixIdentity(pout);
  326. pout->u.m[0][0] = 2.0f / w;
  327. pout->u.m[1][1] = 2.0f / h;
  328. pout->u.m[2][2] = 1.0f / (zn - zf);
  329. pout->u.m[3][2] = zn / (zn - zf);
  330. return pout;
  331. }
  332. D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
  333. {
  334. D3DXMatrixIdentity(pout);
  335. pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
  336. pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
  337. pout->u.m[2][2] = zf / (zf - zn);
  338. pout->u.m[2][3] = 1.0f;
  339. pout->u.m[3][2] = (zf * zn) / (zn - zf);
  340. pout->u.m[3][3] = 0.0f;
  341. return pout;
  342. }
  343. D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
  344. {
  345. D3DXMatrixIdentity(pout);
  346. pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
  347. pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
  348. pout->u.m[2][2] = zf / (zn - zf);
  349. pout->u.m[2][3] = -1.0f;
  350. pout->u.m[3][2] = (zf * zn) / (zn - zf);
  351. pout->u.m[3][3] = 0.0f;
  352. return pout;
  353. }
  354. D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
  355. {
  356. D3DXMatrixIdentity(pout);
  357. pout->u.m[0][0] = 2.0f * zn / w;
  358. pout->u.m[1][1] = 2.0f * zn / h;
  359. pout->u.m[2][2] = zf / (zf - zn);
  360. pout->u.m[3][2] = (zn * zf) / (zn - zf);
  361. pout->u.m[2][3] = 1.0f;
  362. pout->u.m[3][3] = 0.0f;
  363. return pout;
  364. }
  365. D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
  366. {
  367. D3DXMatrixIdentity(pout);
  368. pout->u.m[0][0] = 2.0f * zn / (r - l);
  369. pout->u.m[1][1] = -2.0f * zn / (b - t);
  370. pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
  371. pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
  372. pout->u.m[2][2] = - zf / (zn - zf);
  373. pout->u.m[3][2] = (zn * zf) / (zn -zf);
  374. pout->u.m[2][3] = 1.0f;
  375. pout->u.m[3][3] = 0.0f;
  376. return pout;
  377. }
  378. D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
  379. {
  380. D3DXMatrixIdentity(pout);
  381. pout->u.m[0][0] = 2.0f * zn / (r - l);
  382. pout->u.m[1][1] = -2.0f * zn / (b - t);
  383. pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
  384. pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
  385. pout->u.m[2][2] = zf / (zn - zf);
  386. pout->u.m[3][2] = (zn * zf) / (zn -zf);
  387. pout->u.m[2][3] = -1.0f;
  388. pout->u.m[3][3] = 0.0f;
  389. return pout;
  390. }
  391. D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
  392. {
  393. D3DXMatrixIdentity(pout);
  394. pout->u.m[0][0] = 2.0f * zn / w;
  395. pout->u.m[1][1] = 2.0f * zn / h;
  396. pout->u.m[2][2] = zf / (zn - zf);
  397. pout->u.m[3][2] = (zn * zf) / (zn - zf);
  398. pout->u.m[2][3] = -1.0f;
  399. pout->u.m[3][3] = 0.0f;
  400. return pout;
  401. }
  402. D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, CONST D3DXPLANE *pplane)
  403. {
  404. D3DXPLANE Nplane;
  405. D3DXPlaneNormalize(&Nplane, pplane);
  406. D3DXMatrixIdentity(pout);
  407. pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
  408. pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
  409. pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
  410. pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
  411. pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
  412. pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
  413. pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
  414. pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
  415. pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
  416. pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
  417. pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
  418. pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
  419. return pout;
  420. }
  421. D3DXMATRIX* WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
  422. {
  423. D3DXVECTOR3 v;
  424. D3DXVec3Normalize(&v,pv);
  425. D3DXMatrixIdentity(pout);
  426. pout->u.m[0][0] = (1.0f - cos(angle)) * v.x * v.x + cos(angle);
  427. pout->u.m[1][0] = (1.0f - cos(angle)) * v.x * v.y - sin(angle) * v.z;
  428. pout->u.m[2][0] = (1.0f - cos(angle)) * v.x * v.z + sin(angle) * v.y;
  429. pout->u.m[0][1] = (1.0f - cos(angle)) * v.y * v.x + sin(angle) * v.z;
  430. pout->u.m[1][1] = (1.0f - cos(angle)) * v.y * v.y + cos(angle);
  431. pout->u.m[2][1] = (1.0f - cos(angle)) * v.y * v.z - sin(angle) * v.x;
  432. pout->u.m[0][2] = (1.0f - cos(angle)) * v.z * v.x - sin(angle) * v.y;
  433. pout->u.m[1][2] = (1.0f - cos(angle)) * v.z * v.y + sin(angle) * v.x;
  434. pout->u.m[2][2] = (1.0f - cos(angle)) * v.z * v.z + cos(angle);
  435. return pout;
  436. }
  437. D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, CONST D3DXQUATERNION *pq)
  438. {
  439. D3DXMatrixIdentity(pout);
  440. pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
  441. pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
  442. pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
  443. pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
  444. pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
  445. pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
  446. pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
  447. pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
  448. pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
  449. return pout;
  450. }
  451. D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
  452. {
  453. D3DXMatrixIdentity(pout);
  454. pout->u.m[1][1] = cos(angle);
  455. pout->u.m[2][2] = cos(angle);
  456. pout->u.m[1][2] = sin(angle);
  457. pout->u.m[2][1] = -sin(angle);
  458. return pout;
  459. }
  460. D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
  461. {
  462. D3DXMatrixIdentity(pout);
  463. pout->u.m[0][0] = cos(angle);
  464. pout->u.m[2][2] = cos(angle);
  465. pout->u.m[0][2] = -sin(angle);
  466. pout->u.m[2][0] = sin(angle);
  467. return pout;
  468. }
  469. D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
  470. {
  471. D3DXMATRIX m;
  472. D3DXMatrixIdentity(pout);
  473. D3DXMatrixRotationZ(&m, roll);
  474. D3DXMatrixMultiply(pout, pout, &m);
  475. D3DXMatrixRotationX(&m, pitch);
  476. D3DXMatrixMultiply(pout, pout, &m);
  477. D3DXMatrixRotationY(&m, yaw);
  478. D3DXMatrixMultiply(pout, pout, &m);
  479. return pout;
  480. }
  481. D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
  482. {
  483. D3DXMatrixIdentity(pout);
  484. pout->u.m[0][0] = cos(angle);
  485. pout->u.m[1][1] = cos(angle);
  486. pout->u.m[0][1] = sin(angle);
  487. pout->u.m[1][0] = -sin(angle);
  488. return pout;
  489. }
  490. D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
  491. {
  492. D3DXMatrixIdentity(pout);
  493. pout->u.m[0][0] = sx;
  494. pout->u.m[1][1] = sy;
  495. pout->u.m[2][2] = sz;
  496. return pout;
  497. }
  498. D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, CONST D3DXVECTOR4 *plight, CONST D3DXPLANE *pplane)
  499. {
  500. D3DXPLANE Nplane;
  501. FLOAT dot;
  502. D3DXPlaneNormalize(&Nplane, pplane);
  503. dot = D3DXPlaneDot(&Nplane, plight);
  504. pout->u.m[0][0] = dot - Nplane.a * plight->x;
  505. pout->u.m[0][1] = -Nplane.a * plight->y;
  506. pout->u.m[0][2] = -Nplane.a * plight->z;
  507. pout->u.m[0][3] = -Nplane.a * plight->w;
  508. pout->u.m[1][0] = -Nplane.b * plight->x;
  509. pout->u.m[1][1] = dot - Nplane.b * plight->y;
  510. pout->u.m[1][2] = -Nplane.b * plight->z;
  511. pout->u.m[1][3] = -Nplane.b * plight->w;
  512. pout->u.m[2][0] = -Nplane.c * plight->x;
  513. pout->u.m[2][1] = -Nplane.c * plight->y;
  514. pout->u.m[2][2] = dot - Nplane.c * plight->z;
  515. pout->u.m[2][3] = -Nplane.c * plight->w;
  516. pout->u.m[3][0] = -Nplane.d * plight->x;
  517. pout->u.m[3][1] = -Nplane.d * plight->y;
  518. pout->u.m[3][2] = -Nplane.d * plight->z;
  519. pout->u.m[3][3] = dot - Nplane.d * plight->w;
  520. return pout;
  521. }
  522. D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pscalingcenter, CONST D3DXQUATERNION *pscalingrotation, CONST D3DXVECTOR3 *pscaling, CONST D3DXVECTOR3 *protationcenter, CONST D3DXQUATERNION *protation, CONST D3DXVECTOR3 *ptranslation)
  523. {
  524. D3DXMATRIX m1, m2, m3, m4, m5, m6, m7;
  525. D3DXQUATERNION prc;
  526. D3DXVECTOR3 psc, pt;
  527. if ( !pscalingcenter )
  528. {
  529. psc.x = 0.0f;
  530. psc.y = 0.0f;
  531. psc.z = 0.0f;
  532. }
  533. else
  534. {
  535. psc.x = pscalingcenter->x;
  536. psc.y = pscalingcenter->y;
  537. psc.z = pscalingcenter->z;
  538. }
  539. if ( !protationcenter )
  540. {
  541. prc.x = 0.0f;
  542. prc.y = 0.0f;
  543. prc.z = 0.0f;
  544. }
  545. else
  546. {
  547. prc.x = protationcenter->x;
  548. prc.y = protationcenter->y;
  549. prc.z = protationcenter->z;
  550. }
  551. if ( !ptranslation )
  552. {
  553. pt.x = 0.0f;
  554. pt.y = 0.0f;
  555. pt.z = 0.0f;
  556. }
  557. else
  558. {
  559. pt.x = ptranslation->x;
  560. pt.y = ptranslation->y;
  561. pt.z = ptranslation->z;
  562. }
  563. D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
  564. if ( !pscalingrotation )
  565. {
  566. D3DXMatrixIdentity(&m2);
  567. D3DXMatrixIdentity(&m4);
  568. }
  569. else
  570. {
  571. D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
  572. D3DXMatrixInverse(&m2, NULL, &m4);
  573. }
  574. if ( !pscaling ) D3DXMatrixIdentity(&m3);
  575. else D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
  576. if ( !protation ) D3DXMatrixIdentity(&m6);
  577. else D3DXMatrixRotationQuaternion(&m6, protation);
  578. D3DXMatrixTranslation(&m5, psc.x - prc.x, psc.y - prc.y, psc.z - prc.z);
  579. D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
  580. D3DXMatrixMultiply(&m1, &m1, &m2);
  581. D3DXMatrixMultiply(&m1, &m1, &m3);
  582. D3DXMatrixMultiply(&m1, &m1, &m4);
  583. D3DXMatrixMultiply(&m1, &m1, &m5);
  584. D3DXMatrixMultiply(&m1, &m1, &m6);
  585. D3DXMatrixMultiply(pout, &m1, &m7);
  586. return pout;
  587. }
  588. D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(D3DXMATRIX *pout, CONST D3DXVECTOR2 *pscalingcenter, FLOAT scalingrotation, CONST D3DXVECTOR2 *pscaling, CONST D3DXVECTOR2 *protationcenter, FLOAT rotation, CONST D3DXVECTOR2 *ptranslation)
  589. {
  590. D3DXQUATERNION rot, sca_rot;
  591. D3DXVECTOR3 rot_center, sca, sca_center, trans;
  592. if ( pscalingcenter )
  593. {
  594. sca_center.x=pscalingcenter->x;
  595. sca_center.y=pscalingcenter->y;
  596. sca_center.z=0.0f;
  597. }
  598. else
  599. {
  600. sca_center.x=0.0f;
  601. sca_center.y=0.0f;
  602. sca_center.z=0.0f;
  603. }
  604. if ( pscaling )
  605. {
  606. sca.x=pscaling->x;
  607. sca.y=pscaling->y;
  608. sca.z=1.0f;
  609. }
  610. else
  611. {
  612. sca.x=1.0f;
  613. sca.y=1.0f;
  614. sca.z=1.0f;
  615. }
  616. if ( protationcenter )
  617. {
  618. rot_center.x=protationcenter->x;
  619. rot_center.y=protationcenter->y;
  620. rot_center.z=0.0f;
  621. }
  622. else
  623. {
  624. rot_center.x=0.0f;
  625. rot_center.y=0.0f;
  626. rot_center.z=0.0f;
  627. }
  628. if ( ptranslation )
  629. {
  630. trans.x=ptranslation->x;
  631. trans.y=ptranslation->y;
  632. trans.z=0.0f;
  633. }
  634. else
  635. {
  636. trans.x=0.0f;
  637. trans.y=0.0f;
  638. trans.z=0.0f;
  639. }
  640. rot.w=cos(rotation/2.0f);
  641. rot.x=0.0f;
  642. rot.y=0.0f;
  643. rot.z=sin(rotation/2.0f);
  644. sca_rot.w=cos(scalingrotation/2.0f);
  645. sca_rot.x=0.0f;
  646. sca_rot.y=0.0f;
  647. sca_rot.z=sin(scalingrotation/2.0f);
  648. D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
  649. return pout;
  650. }
  651. D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
  652. {
  653. D3DXMatrixIdentity(pout);
  654. pout->u.m[3][0] = x;
  655. pout->u.m[3][1] = y;
  656. pout->u.m[3][2] = z;
  657. return pout;
  658. }
  659. D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
  660. {
  661. CONST D3DXMATRIX m = *pm;
  662. int i,j;
  663. for (i=0; i<4; i++)
  664. for (j=0; j<4; j++) pout->u.m[i][j] = m.u.m[j][i];
  665. return pout;
  666. }
  667. /*_________________D3DXMatrixStack____________________*/
  668. static const unsigned int INITIAL_STACK_SIZE = 32;
  669. HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, LPD3DXMATRIXSTACK* ppstack)
  670. {
  671. ID3DXMatrixStackImpl* object;
  672. TRACE("flags %#x, ppstack %p\n", flags, ppstack);
  673. object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXMatrixStackImpl));
  674. if ( object == NULL )
  675. {
  676. *ppstack = NULL;
  677. return E_OUTOFMEMORY;
  678. }
  679. object->ID3DXMatrixStack_iface.lpVtbl = &ID3DXMatrixStack_Vtbl;
  680. object->ref = 1;
  681. object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(D3DXMATRIX));
  682. if (!object->stack)
  683. {
  684. HeapFree(GetProcessHeap(), 0, object);
  685. *ppstack = NULL;
  686. return E_OUTOFMEMORY;
  687. }
  688. object->current = 0;
  689. object->stack_size = INITIAL_STACK_SIZE;
  690. D3DXMatrixIdentity(&object->stack[0]);
  691. TRACE("Created matrix stack %p\n", object);
  692. *ppstack = &object->ID3DXMatrixStack_iface;
  693. return D3D_OK;
  694. }
  695. static inline ID3DXMatrixStackImpl *impl_from_ID3DXMatrixStack(ID3DXMatrixStack *iface)
  696. {
  697. return CONTAINING_RECORD(iface, ID3DXMatrixStackImpl, ID3DXMatrixStack_iface);
  698. }
  699. static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **ppobj)
  700. {
  701. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  702. if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXMatrixStack))
  703. {
  704. ID3DXMatrixStack_AddRef(iface);
  705. *ppobj = This;
  706. return S_OK;
  707. }
  708. *ppobj = NULL;
  709. WARN("(%p)->(%s,%p), not found\n", This, debugstr_guid(riid), ppobj);
  710. return E_NOINTERFACE;
  711. }
  712. static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
  713. {
  714. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  715. ULONG ref = InterlockedIncrement(&This->ref);
  716. TRACE("(%p) : AddRef from %d\n", This, ref - 1);
  717. return ref;
  718. }
  719. static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack* iface)
  720. {
  721. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  722. ULONG ref = InterlockedDecrement(&This->ref);
  723. if (!ref)
  724. {
  725. HeapFree(GetProcessHeap(), 0, This->stack);
  726. HeapFree(GetProcessHeap(), 0, This);
  727. }
  728. TRACE("(%p) : ReleaseRef to %d\n", This, ref);
  729. return ref;
  730. }
  731. static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
  732. {
  733. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  734. TRACE("iface %p\n", iface);
  735. return &This->stack[This->current];
  736. }
  737. static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
  738. {
  739. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  740. TRACE("iface %p\n", iface);
  741. D3DXMatrixIdentity(&This->stack[This->current]);
  742. return D3D_OK;
  743. }
  744. static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
  745. {
  746. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  747. TRACE("iface %p\n", iface);
  748. This->stack[This->current] = *pm;
  749. return D3D_OK;
  750. }
  751. static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
  752. {
  753. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  754. TRACE("iface %p\n", iface);
  755. D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], pm);
  756. return D3D_OK;
  757. }
  758. static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
  759. {
  760. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  761. TRACE("iface %p\n", iface);
  762. D3DXMatrixMultiply(&This->stack[This->current], pm, &This->stack[This->current]);
  763. return D3D_OK;
  764. }
  765. static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
  766. {
  767. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  768. TRACE("iface %p\n", iface);
  769. /* Popping the last element on the stack returns D3D_OK, but does nothing. */
  770. if (!This->current) return D3D_OK;
  771. if (This->current <= This->stack_size / 4 && This->stack_size >= INITIAL_STACK_SIZE * 2)
  772. {
  773. unsigned int new_size;
  774. D3DXMATRIX *new_stack;
  775. new_size = This->stack_size / 2;
  776. new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(D3DXMATRIX));
  777. if (new_stack)
  778. {
  779. This->stack_size = new_size;
  780. This->stack = new_stack;
  781. }
  782. }
  783. --This->current;
  784. return D3D_OK;
  785. }
  786. static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
  787. {
  788. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  789. TRACE("iface %p\n", iface);
  790. if (This->current == This->stack_size - 1)
  791. {
  792. unsigned int new_size;
  793. D3DXMATRIX *new_stack;
  794. if (This->stack_size > UINT_MAX / 2) return E_OUTOFMEMORY;
  795. new_size = This->stack_size * 2;
  796. new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(D3DXMATRIX));
  797. if (!new_stack) return E_OUTOFMEMORY;
  798. This->stack_size = new_size;
  799. This->stack = new_stack;
  800. }
  801. ++This->current;
  802. This->stack[This->current] = This->stack[This->current - 1];
  803. return D3D_OK;
  804. }
  805. static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
  806. {
  807. D3DXMATRIX temp;
  808. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  809. TRACE("iface %p\n", iface);
  810. D3DXMatrixRotationAxis(&temp, pv, angle);
  811. D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
  812. return D3D_OK;
  813. }
  814. static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
  815. {
  816. D3DXMATRIX temp;
  817. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  818. TRACE("iface %p\n", iface);
  819. D3DXMatrixRotationAxis(&temp, pv, angle);
  820. D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
  821. return D3D_OK;
  822. }
  823. static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
  824. {
  825. D3DXMATRIX temp;
  826. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  827. TRACE("iface %p\n", iface);
  828. D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
  829. D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
  830. return D3D_OK;
  831. }
  832. static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
  833. {
  834. D3DXMATRIX temp;
  835. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  836. TRACE("iface %p\n", iface);
  837. D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
  838. D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
  839. return D3D_OK;
  840. }
  841. static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
  842. {
  843. D3DXMATRIX temp;
  844. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  845. TRACE("iface %p\n", iface);
  846. D3DXMatrixScaling(&temp, x, y, z);
  847. D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
  848. return D3D_OK;
  849. }
  850. static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
  851. {
  852. D3DXMATRIX temp;
  853. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  854. TRACE("iface %p\n", iface);
  855. D3DXMatrixScaling(&temp, x, y, z);
  856. D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
  857. return D3D_OK;
  858. }
  859. static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
  860. {
  861. D3DXMATRIX temp;
  862. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  863. TRACE("iface %p\n", iface);
  864. D3DXMatrixTranslation(&temp, x, y, z);
  865. D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
  866. return D3D_OK;
  867. }
  868. static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
  869. {
  870. D3DXMATRIX temp;
  871. ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
  872. TRACE("iface %p\n", iface);
  873. D3DXMatrixTranslation(&temp, x, y, z);
  874. D3DXMatrixMultiply(&This->stack[This->current], &temp,&This->stack[This->current]);
  875. return D3D_OK;
  876. }
  877. static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
  878. {
  879. ID3DXMatrixStackImpl_QueryInterface,
  880. ID3DXMatrixStackImpl_AddRef,
  881. ID3DXMatrixStackImpl_Release,
  882. ID3DXMatrixStackImpl_Pop,
  883. ID3DXMatrixStackImpl_Push,
  884. ID3DXMatrixStackImpl_LoadIdentity,
  885. ID3DXMatrixStackImpl_LoadMatrix,
  886. ID3DXMatrixStackImpl_MultMatrix,
  887. ID3DXMatrixStackImpl_MultMatrixLocal,
  888. ID3DXMatrixStackImpl_RotateAxis,
  889. ID3DXMatrixStackImpl_RotateAxisLocal,
  890. ID3DXMatrixStackImpl_RotateYawPitchRoll,
  891. ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
  892. ID3DXMatrixStackImpl_Scale,
  893. ID3DXMatrixStackImpl_ScaleLocal,
  894. ID3DXMatrixStackImpl_Translate,
  895. ID3DXMatrixStackImpl_TranslateLocal,
  896. ID3DXMatrixStackImpl_GetTop
  897. };
  898. /*_________________D3DXPLANE________________*/
  899. D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, CONST D3DXVECTOR3 *pvpoint, CONST D3DXVECTOR3 *pvnormal)
  900. {
  901. pout->a = pvnormal->x;
  902. pout->b = pvnormal->y;
  903. pout->c = pvnormal->z;
  904. pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
  905. return pout;
  906. }
  907. D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3)
  908. {
  909. D3DXVECTOR3 edge1, edge2, normal, Nnormal;
  910. edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
  911. edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
  912. D3DXVec3Subtract(&edge1, pv2, pv1);
  913. D3DXVec3Subtract(&edge2, pv3, pv1);
  914. D3DXVec3Cross(&normal, &edge1, &edge2);
  915. D3DXVec3Normalize(&Nnormal, &normal);
  916. D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
  917. return pout;
  918. }
  919. D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, CONST D3DXPLANE *pp, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
  920. {
  921. D3DXVECTOR3 direction, normal;
  922. FLOAT dot, temp;
  923. normal.x = pp->a;
  924. normal.y = pp->b;
  925. normal.z = pp->c;
  926. direction.x = pv2->x - pv1->x;
  927. direction.y = pv2->y - pv1->y;
  928. direction.z = pv2->z - pv1->z;
  929. dot = D3DXVec3Dot(&normal, &direction);
  930. if ( !dot ) return NULL;
  931. temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
  932. pout->x = pv1->x - temp * direction.x;
  933. pout->y = pv1->y - temp * direction.y;
  934. pout->z = pv1->z - temp * direction.z;
  935. return pout;
  936. }
  937. D3DXPLANE* WINAPI D3DXPlaneNormalize(D3DXPLANE *pout, CONST D3DXPLANE *pp)
  938. {
  939. D3DXPLANE out;
  940. FLOAT norm;
  941. norm = sqrt(pp->a * pp->a + pp->b * pp->b + pp->c * pp->c);
  942. if ( norm )
  943. {
  944. out.a = pp->a / norm;
  945. out.b = pp->b / norm;
  946. out.c = pp->c / norm;
  947. out.d = pp->d / norm;
  948. }
  949. else
  950. {
  951. out.a = 0.0f;
  952. out.b = 0.0f;
  953. out.c = 0.0f;
  954. out.d = 0.0f;
  955. }
  956. *pout = out;
  957. return pout;
  958. }
  959. D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, CONST D3DXPLANE *pplane, CONST D3DXMATRIX *pm)
  960. {
  961. CONST D3DXPLANE plane = *pplane;
  962. pout->a = pm->u.m[0][0] * plane.a + pm->u.m[1][0] * plane.b + pm->u.m[2][0] * plane.c + pm->u.m[3][0] * plane.d;
  963. pout->b = pm->u.m[0][1] * plane.a + pm->u.m[1][1] * plane.b + pm->u.m[2][1] * plane.c + pm->u.m[3][1] * plane.d;
  964. pout->c = pm->u.m[0][2] * plane.a + pm->u.m[1][2] * plane.b + pm->u.m[2][2] * plane.c + pm->u.m[3][2] * plane.d;
  965. pout->d = pm->u.m[0][3] * plane.a + pm->u.m[1][3] * plane.b + pm->u.m[2][3] * plane.c + pm->u.m[3][3] * plane.d;
  966. return pout;
  967. }
  968. D3DXPLANE* WINAPI D3DXPlaneTransformArray(D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
  969. {
  970. UINT i;
  971. for (i = 0; i < elements; ++i) {
  972. D3DXPlaneTransform(
  973. (D3DXPLANE*)((char*)out + outstride * i),
  974. (CONST D3DXPLANE*)((const char*)in + instride * i),
  975. matrix);
  976. }
  977. return out;
  978. }
  979. /*_________________D3DXQUATERNION________________*/
  980. D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
  981. {
  982. D3DXQUATERNION temp1, temp2;
  983. D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
  984. return pout;
  985. }
  986. D3DXQUATERNION* WINAPI D3DXQuaternionExp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
  987. {
  988. FLOAT norm;
  989. norm = sqrt(pq->x * pq->x + pq->y * pq->y + pq->z * pq->z);
  990. if (norm )
  991. {
  992. pout->x = sin(norm) * pq->x / norm;
  993. pout->y = sin(norm) * pq->y / norm;
  994. pout->z = sin(norm) * pq->z / norm;
  995. pout->w = cos(norm);
  996. }
  997. else
  998. {
  999. pout->x = 0.0f;
  1000. pout->y = 0.0f;
  1001. pout->z = 0.0f;
  1002. pout->w = 1.0f;
  1003. }
  1004. return pout;
  1005. }
  1006. D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
  1007. {
  1008. D3DXQUATERNION out;
  1009. FLOAT norm;
  1010. norm = D3DXQuaternionLengthSq(pq);
  1011. out.x = -pq->x / norm;
  1012. out.y = -pq->y / norm;
  1013. out.z = -pq->z / norm;
  1014. out.w = pq->w / norm;
  1015. *pout =out;
  1016. return pout;
  1017. }
  1018. D3DXQUATERNION* WINAPI D3DXQuaternionLn(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
  1019. {
  1020. FLOAT norm, normvec, theta;
  1021. norm = D3DXQuaternionLengthSq(pq);
  1022. if ( norm > 1.0001f )
  1023. {
  1024. pout->x = pq->x;
  1025. pout->y = pq->y;
  1026. pout->z = pq->z;
  1027. pout->w = 0.0f;
  1028. }
  1029. else if( norm > 0.99999f)
  1030. {
  1031. normvec = sqrt( pq->x * pq->x + pq->y * pq->y + pq->z * pq->z );
  1032. theta = atan2(normvec, pq->w) / normvec;
  1033. pout->x = theta * pq->x;
  1034. pout->y = theta * pq->y;
  1035. pout->z = theta * pq->z;
  1036. pout->w = 0.0f;
  1037. }
  1038. else
  1039. {
  1040. FIXME("The quaternion (%f, %f, %f, %f) has a norm <1. This should not happen. Windows returns a result anyway. This case is not implemented yet.\n", pq->x, pq->y, pq->z, pq->w);
  1041. }
  1042. return pout;
  1043. }
  1044. D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
  1045. {
  1046. D3DXQUATERNION out;
  1047. out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
  1048. out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
  1049. out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
  1050. out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
  1051. *pout = out;
  1052. return pout;
  1053. }
  1054. D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
  1055. {
  1056. D3DXQUATERNION out;
  1057. FLOAT norm;
  1058. norm = D3DXQuaternionLength(pq);
  1059. out.x = pq->x / norm;
  1060. out.y = pq->y / norm;
  1061. out.z = pq->z / norm;
  1062. out.w = pq->w / norm;
  1063. *pout=out;
  1064. return pout;
  1065. }
  1066. D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
  1067. {
  1068. D3DXVECTOR3 temp;
  1069. D3DXVec3Normalize(&temp, pv);
  1070. pout->x = sin( angle / 2.0f ) * temp.x;
  1071. pout->y = sin( angle / 2.0f ) * temp.y;
  1072. pout->z = sin( angle / 2.0f ) * temp.z;
  1073. pout->w = cos( angle / 2.0f );
  1074. return pout;
  1075. }
  1076. D3DXQUATERNION* WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *pout, CONST D3DXMATRIX *pm)
  1077. {
  1078. int i, maxi;
  1079. FLOAT maxdiag, S, trace;
  1080. trace = pm->u.m[0][0] + pm->u.m[1][1] + pm->u.m[2][2] + 1.0f;
  1081. if ( trace > 1.0f)
  1082. {
  1083. pout->x = ( pm->u.m[1][2] - pm->u.m[2][1] ) / ( 2.0f * sqrt(trace) );
  1084. pout->y = ( pm->u.m[2][0] - pm->u.m[0][2] ) / ( 2.0f * sqrt(trace) );
  1085. pout->z = ( pm->u.m[0][1] - pm->u.m[1][0] ) / ( 2.0f * sqrt(trace) );
  1086. pout->w = sqrt(trace) / 2.0f;
  1087. return pout;
  1088. }
  1089. maxi = 0;
  1090. maxdiag = pm->u.m[0][0];
  1091. for (i=1; i<3; i++)
  1092. {
  1093. if ( pm->u.m[i][i] > maxdiag )
  1094. {
  1095. maxi = i;
  1096. maxdiag = pm->u.m[i][i];
  1097. }
  1098. }
  1099. switch( maxi )
  1100. {
  1101. case 0:
  1102. S = 2.0f * sqrt(1.0f + pm->u.m[0][0] - pm->u.m[1][1] - pm->u.m[2][2]);
  1103. pout->x = 0.25f * S;
  1104. pout->y = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
  1105. pout->z = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
  1106. pout->w = ( pm->u.m[1][2] - pm->u.m[2][1] ) / S;
  1107. break;
  1108. case 1:
  1109. S = 2.0f * sqrt(1.0f + pm->u.m[1][1] - pm->u.m[0][0] - pm->u.m[2][2]);
  1110. pout->x = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
  1111. pout->y = 0.25f * S;
  1112. pout->z = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
  1113. pout->w = ( pm->u.m[2][0] - pm->u.m[0][2] ) / S;
  1114. break;
  1115. case 2:
  1116. S = 2.0f * sqrt(1.0f + pm->u.m[2][2] - pm->u.m[0][0] - pm->u.m[1][1]);
  1117. pout->x = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
  1118. pout->y = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
  1119. pout->z = 0.25f * S;
  1120. pout->w = ( pm->u.m[0][1] - pm->u.m[1][0] ) / S;
  1121. break;
  1122. }
  1123. return pout;
  1124. }
  1125. D3DXQUATERNION* WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
  1126. {
  1127. pout->x = sin( yaw / 2.0f) * cos(pitch / 2.0f) * sin(roll / 2.0f) + cos(yaw / 2.0f) * sin(pitch / 2.0f) * cos(roll / 2.0f);
  1128. pout->y = sin( yaw / 2.0f) * cos(pitch / 2.0f) * cos(roll / 2.0f) - cos(yaw / 2.0f) * sin(pitch / 2.0f) * sin(roll / 2.0f);
  1129. pout->z = cos(yaw / 2.0f) * cos(pitch / 2.0f) * sin(roll / 2.0f) - sin( yaw / 2.0f) * sin(pitch / 2.0f) * cos(roll / 2.0f);
  1130. pout->w = cos( yaw / 2.0f) * cos(pitch / 2.0f) * cos(roll / 2.0f) + sin(yaw / 2.0f) * sin(pitch / 2.0f) * sin(roll / 2.0f);
  1131. return pout;
  1132. }
  1133. D3DXQUATERNION* WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, FLOAT t)
  1134. {
  1135. FLOAT dot, epsilon, temp, theta, u;
  1136. epsilon = 1.0f;
  1137. temp = 1.0f - t;
  1138. u = t;
  1139. dot = D3DXQuaternionDot(pq1, pq2);
  1140. if ( dot < 0.0f )
  1141. {
  1142. epsilon = -1.0f;
  1143. dot = -dot;
  1144. }
  1145. if( 1.0f - dot > 0.001f )
  1146. {
  1147. theta = acos(dot);
  1148. temp = sin(theta * temp) / sin(theta);
  1149. u = sin(theta * u) / sin(theta);
  1150. }
  1151. pout->x = temp * pq1->x + epsilon * u * pq2->x;
  1152. pout->y = temp * pq1->y + epsilon * u * pq2->y;
  1153. pout->z = temp * pq1->z + epsilon * u * pq2->z;
  1154. pout->w = temp * pq1->w + epsilon * u * pq2->w;
  1155. return pout;
  1156. }
  1157. D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, CONST D3DXQUATERNION *pq4, FLOAT t)
  1158. {
  1159. D3DXQUATERNION temp1, temp2;
  1160. D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
  1161. return pout;
  1162. }
  1163. void WINAPI D3DXQuaternionToAxisAngle(CONST D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
  1164. {
  1165. paxis->x = pq->x;
  1166. paxis->y = pq->y;
  1167. paxis->z = pq->z;
  1168. *pangle = 2.0f * acos(pq->w);
  1169. }
  1170. /*_________________D3DXVec2_____________________*/
  1171. D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
  1172. {
  1173. pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
  1174. pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
  1175. return pout;
  1176. }
  1177. D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv0, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT s)
  1178. {
  1179. pout->x = 0.5f * (2.0f * pv1->x + (pv2->x - pv0->x) *s + (2.0f *pv0->x - 5.0f * pv1->x + 4.0f * pv2->x - pv3->x) * s * s + (pv3->x -3.0f * pv2->x + 3.0f * pv1->x - pv0->x) * s * s * s);
  1180. pout->y = 0.5f * (2.0f * pv1->y + (pv2->y - pv0->y) *s + (2.0f *pv0->y - 5.0f * pv1->y + 4.0f * pv2->y - pv3->y) * s * s + (pv3->y -3.0f * pv2->y + 3.0f * pv1->y - pv0->y) * s * s * s);
  1181. return pout;
  1182. }
  1183. D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pt1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pt2, FLOAT s)
  1184. {
  1185. FLOAT h1, h2, h3, h4;
  1186. h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
  1187. h2 = s * s * s - 2.0f * s * s + s;
  1188. h3 = -2.0f * s * s * s + 3.0f * s * s;
  1189. h4 = s * s * s - s * s;
  1190. pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
  1191. pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
  1192. return pout;
  1193. }
  1194. D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
  1195. {
  1196. D3DXVECTOR2 out;
  1197. FLOAT norm;
  1198. norm = D3DXVec2Length(pv);
  1199. if ( !norm )
  1200. {
  1201. out.x = 0.0f;
  1202. out.y = 0.0f;
  1203. }
  1204. else
  1205. {
  1206. out.x = pv->x / norm;
  1207. out.y = pv->y / norm;
  1208. }
  1209. *pout=out;
  1210. return pout;
  1211. }
  1212. D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
  1213. {
  1214. pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
  1215. pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
  1216. pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
  1217. pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
  1218. return pout;
  1219. }
  1220. D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
  1221. {
  1222. UINT i;
  1223. for (i = 0; i < elements; ++i) {
  1224. D3DXVec2Transform(
  1225. (D3DXVECTOR4*)((char*)out + outstride * i),
  1226. (CONST D3DXVECTOR2*)((const char*)in + instride * i),
  1227. matrix);
  1228. }
  1229. return out;
  1230. }
  1231. D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
  1232. {
  1233. D3DXVECTOR2 v;
  1234. FLOAT norm;
  1235. v = *pv;
  1236. norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
  1237. pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
  1238. pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
  1239. return pout;
  1240. }
  1241. D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
  1242. {
  1243. UINT i;
  1244. for (i = 0; i < elements; ++i) {
  1245. D3DXVec2TransformCoord(
  1246. (D3DXVECTOR2*)((char*)out + outstride * i),
  1247. (CONST D3DXVECTOR2*)((const char*)in + instride * i),
  1248. matrix);
  1249. }
  1250. return out;
  1251. }
  1252. D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
  1253. {
  1254. CONST D3DXVECTOR2 v = *pv;
  1255. pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
  1256. pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
  1257. return pout;
  1258. }
  1259. D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride, CONST D3DXMATRIX *matrix, UINT elements)
  1260. {
  1261. UINT i;
  1262. for (i = 0; i < elements; ++i) {
  1263. D3DXVec2TransformNormal(
  1264. (D3DXVECTOR2*)((char*)out + outstride * i),
  1265. (CONST D3DXVECTOR2*)((const char*)in + instride * i),
  1266. matrix);
  1267. }
  1268. return out;
  1269. }
  1270. /*_________________D3DXVec3_____________________*/
  1271. D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
  1272. {
  1273. pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
  1274. pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
  1275. pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
  1276. return pout;
  1277. }
  1278. D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
  1279. {
  1280. pout->x = 0.5f * (2.0f * pv1->x + (pv2->x - pv0->x) *s + (2.0f *pv0->x - 5.0f * pv1->x + 4.0f * pv2->x - pv3->x) * s * s + (pv3->x -3.0f * pv2->x + 3.0f * pv1->x - pv0->x) * s * s * s);
  1281. pout->y = 0.5f * (2.0f * pv1->y + (pv2->y - pv0->y) *s + (2.0f *pv0->y - 5.0f * pv1->y + 4.0f * pv2->y - pv3->y) * s * s + (pv3->y -3.0f * pv2->y + 3.0f * pv1->y - pv0->y) * s * s * s);
  1282. pout->z = 0.5f * (2.0f * pv1->z + (pv2->z - pv0->z) *s + (2.0f *pv0->z - 5.0f * pv1->z + 4.0f * pv2->z - pv3->z) * s * s + (pv3->z -3.0f * pv2->z + 3.0f * pv1->z - pv0->z) * s * s * s);
  1283. return pout;
  1284. }
  1285. D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
  1286. {
  1287. FLOAT h1, h2, h3, h4;
  1288. h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
  1289. h2 = s * s * s - 2.0f * s * s + s;
  1290. h3 = -2.0f * s * s * s + 3.0f * s * s;
  1291. h4 = s * s * s - s * s;
  1292. pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
  1293. pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
  1294. pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
  1295. return pout;
  1296. }
  1297. D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
  1298. {
  1299. D3DXVECTOR3 out;
  1300. FLOAT norm;
  1301. norm = D3DXVec3Length(pv);
  1302. if ( !norm )
  1303. {
  1304. out.x = 0.0f;
  1305. out.y = 0.0f;
  1306. out.z = 0.0f;
  1307. }
  1308. else
  1309. {
  1310. out.x = pv->x / norm;
  1311. out.y = pv->y / norm;
  1312. out.z = pv->z / norm;
  1313. }
  1314. *pout = out;
  1315. return pout;
  1316. }
  1317. D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
  1318. {
  1319. D3DXMATRIX m;
  1320. D3DXVECTOR3 out;
  1321. D3DXMatrixMultiply(&m, pworld, pview);
  1322. D3DXMatrixMultiply(&m, &m, pprojection);
  1323. D3DXVec3TransformCoord(&out, pv, &m);
  1324. out.x = pviewport->X + ( 1.0f + out.x ) * pviewport->Width / 2.0f;
  1325. out.y = pviewport->Y + ( 1.0f - out.y ) * pviewport->Height / 2.0f;
  1326. out.z = pviewport->MinZ + out.z * ( pviewport->MaxZ - pviewport->MinZ );
  1327. *pout = out;
  1328. return pout;
  1329. }
  1330. D3DXVECTOR3* WINAPI D3DXVec3ProjectArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DVIEWPORT9* viewport, CONST D3DXMATRIX* projection, CONST D3DXMATRIX* view, CONST D3DXMATRIX* world, UINT elements)
  1331. {
  1332. UINT i;
  1333. for (i = 0; i < elements; ++i) {
  1334. D3DXVec3Project(
  1335. (D3DXVECTOR3*)((char*)out + outstride * i),
  1336. (CONST D3DXVECTOR3*)((const char*)in + instride * i),
  1337. viewport, projection, view, world);
  1338. }
  1339. return out;
  1340. }
  1341. D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
  1342. {
  1343. pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z + pm->u.m[3][0];
  1344. pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z + pm->u.m[3][1];
  1345. pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z + pm->u.m[3][2];
  1346. pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[2][3] * pv->z + pm->u.m[3][3];
  1347. return pout;
  1348. }
  1349. D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
  1350. {
  1351. UIN