2 * Mathematical operations specific to D3DX9.
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
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.
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.
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
25 #define NONAMELESSUNION
28 #include "wine/port.h"
32 #include "d3dx9_36_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
38 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl;
40 struct ID3DXMatrixStackImpl
42 ID3DXMatrixStack ID3DXMatrixStack_iface;
46 unsigned int stack_size;
51 /*_________________D3DXColor____________________*/
53 D3DXCOLOR* WINAPI D3DXColorAdjustContrast(D3DXCOLOR *pout, const D3DXCOLOR *pc, FLOAT s)
55 TRACE("pout %p, pc %p, s %f\n", pout, pc, s);
57 pout->r = 0.5f + s * (pc->r - 0.5f);
58 pout->g = 0.5f + s * (pc->g - 0.5f);
59 pout->b = 0.5f + s * (pc->b - 0.5f);
64 D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, const D3DXCOLOR *pc, FLOAT s)
68 TRACE("pout %p, pc %p, s %f\n", pout, pc, s);
70 grey = pc->r * 0.2125f + pc->g * 0.7154f + pc->b * 0.0721f;
71 pout->r = grey + s * (pc->r - grey);
72 pout->g = grey + s * (pc->g - grey);
73 pout->b = grey + s * (pc->b - grey);
78 /*_________________Misc__________________________*/
80 FLOAT WINAPI D3DXFresnelTerm(FLOAT costheta, FLOAT refractionindex)
82 FLOAT a, d, g, result;
84 TRACE("costheta %f, refractionindex %f\n", costheta, refractionindex);
86 g = sqrtf(refractionindex * refractionindex + costheta * costheta - 1.0f);
89 result = (costheta * a - 1.0f) * (costheta * a - 1.0f) / ((costheta * d + 1.0f) * (costheta * d + 1.0f)) + 1.0f;
90 result *= 0.5f * d * d / (a * a);
95 /*_________________D3DXMatrix____________________*/
97 D3DXMATRIX * WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *out, FLOAT scaling, const D3DXVECTOR3 *rotationcenter,
98 const D3DXQUATERNION *rotation, const D3DXVECTOR3 *translation)
100 TRACE("out %p, scaling %f, rotationcenter %p, rotation %p, translation %p\n",
101 out, scaling, rotationcenter, rotation, translation);
103 D3DXMatrixIdentity(out);
107 out->u.m[3][0] = -rotationcenter->x;
108 out->u.m[3][1] = -rotationcenter->y;
109 out->u.m[3][2] = -rotationcenter->z;
114 FLOAT temp00, temp01, temp02, temp10, temp11, temp12, temp20, temp21, temp22;
116 temp00 = 1.0f - 2.0f * (rotation->y * rotation->y + rotation->z * rotation->z);
117 temp01 = 2.0f * (rotation->x * rotation->y + rotation->z * rotation->w);
118 temp02 = 2.0f * (rotation->x * rotation->z - rotation->y * rotation->w);
119 temp10 = 2.0f * (rotation->x * rotation->y - rotation->z * rotation->w);
120 temp11 = 1.0f - 2.0f * (rotation->x * rotation->x + rotation->z * rotation->z);
121 temp12 = 2.0f * (rotation->y * rotation->z + rotation->x * rotation->w);
122 temp20 = 2.0f * (rotation->x * rotation->z + rotation->y * rotation->w);
123 temp21 = 2.0f * (rotation->y * rotation->z - rotation->x * rotation->w);
124 temp22 = 1.0f - 2.0f * (rotation->x * rotation->x + rotation->y * rotation->y);
126 out->u.m[0][0] = scaling * temp00;
127 out->u.m[0][1] = scaling * temp01;
128 out->u.m[0][2] = scaling * temp02;
129 out->u.m[1][0] = scaling * temp10;
130 out->u.m[1][1] = scaling * temp11;
131 out->u.m[1][2] = scaling * temp12;
132 out->u.m[2][0] = scaling * temp20;
133 out->u.m[2][1] = scaling * temp21;
134 out->u.m[2][2] = scaling * temp22;
144 out->u.m[3][0] = x * temp00 + y * temp10 + z * temp20;
145 out->u.m[3][1] = x * temp01 + y * temp11 + z * temp21;
146 out->u.m[3][2] = x * temp02 + y * temp12 + z * temp22;
151 out->u.m[0][0] = scaling;
152 out->u.m[1][1] = scaling;
153 out->u.m[2][2] = scaling;
158 out->u.m[3][0] += rotationcenter->x;
159 out->u.m[3][1] += rotationcenter->y;
160 out->u.m[3][2] += rotationcenter->z;
165 out->u.m[3][0] += translation->x;
166 out->u.m[3][1] += translation->y;
167 out->u.m[3][2] += translation->z;
173 D3DXMATRIX * WINAPI D3DXMatrixAffineTransformation2D(D3DXMATRIX *out, FLOAT scaling,
174 const D3DXVECTOR2 *rotationcenter, FLOAT rotation, const D3DXVECTOR2 *translation)
178 TRACE("out %p, scaling %f, rotationcenter %p, rotation %f, translation %p\n",
179 out, scaling, rotationcenter, rotation, translation);
181 s = sinf(rotation / 2.0f);
182 tmp1 = 1.0f - 2.0f * s * s;
183 tmp2 = 2.0 * s * cosf(rotation / 2.0f);
185 D3DXMatrixIdentity(out);
186 out->u.m[0][0] = scaling * tmp1;
187 out->u.m[0][1] = scaling * tmp2;
188 out->u.m[1][0] = -scaling * tmp2;
189 out->u.m[1][1] = scaling * tmp1;
195 x = rotationcenter->x;
196 y = rotationcenter->y;
198 out->u.m[3][0] = y * tmp2 - x * tmp1 + x;
199 out->u.m[3][1] = -x * tmp2 - y * tmp1 + y;
204 out->u.m[3][0] += translation->x;
205 out->u.m[3][1] += translation->y;
211 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, const D3DXMATRIX *pm)
213 D3DXMATRIX normalized;
216 TRACE("poutscale %p, poutrotation %p, pouttranslation %p, pm %p\n", poutscale, poutrotation, pouttranslation, pm);
218 /*Compute the scaling part.*/
222 poutscale->x=D3DXVec3Length(&vec);
227 poutscale->y=D3DXVec3Length(&vec);
232 poutscale->z=D3DXVec3Length(&vec);
234 /*Compute the translation part.*/
235 pouttranslation->x=pm->u.m[3][0];
236 pouttranslation->y=pm->u.m[3][1];
237 pouttranslation->z=pm->u.m[3][2];
239 /*Let's calculate the rotation now*/
240 if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) ) return D3DERR_INVALIDCALL;
242 normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
243 normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
244 normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
245 normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
246 normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
247 normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
248 normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
249 normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
250 normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
252 D3DXQuaternionRotationMatrix(poutrotation,&normalized);
256 FLOAT WINAPI D3DXMatrixDeterminant(const D3DXMATRIX *pm)
258 D3DXVECTOR4 minor, v1, v2, v3;
261 TRACE("pm %p\n", pm);
263 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];
264 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];
265 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];
266 D3DXVec4Cross(&minor, &v1, &v2, &v3);
267 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);
271 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, const D3DXMATRIX *pm)
275 D3DXVECTOR4 v, vec[3];
278 TRACE("pout %p, pdeterminant %p, pm %p\n", pout, pdeterminant, pm);
280 det = D3DXMatrixDeterminant(pm);
281 if ( !det ) return NULL;
282 if ( pdeterminant ) *pdeterminant = det;
290 if ( j > i ) a = a-1;
291 vec[a].x = pm->u.m[j][0];
292 vec[a].y = pm->u.m[j][1];
293 vec[a].z = pm->u.m[j][2];
294 vec[a].w = pm->u.m[j][3];
297 D3DXVec4Cross(&v, &vec[0], &vec[1], &vec[2]);
298 out.u.m[0][i] = pow(-1.0f, i) * v.x / det;
299 out.u.m[1][i] = pow(-1.0f, i) * v.y / det;
300 out.u.m[2][i] = pow(-1.0f, i) * v.z / det;
301 out.u.m[3][i] = pow(-1.0f, i) * v.w / det;
308 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, const D3DXVECTOR3 *peye, const D3DXVECTOR3 *pat, const D3DXVECTOR3 *pup)
310 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
312 TRACE("pout %p, peye %p, pat %p, pup %p\n", pout, peye, pat, pup);
314 D3DXVec3Subtract(&vec2, pat, peye);
315 D3DXVec3Normalize(&vec, &vec2);
316 D3DXVec3Cross(&right, pup, &vec);
317 D3DXVec3Cross(&up, &vec, &right);
318 D3DXVec3Normalize(&rightn, &right);
319 D3DXVec3Normalize(&upn, &up);
320 pout->u.m[0][0] = rightn.x;
321 pout->u.m[1][0] = rightn.y;
322 pout->u.m[2][0] = rightn.z;
323 pout->u.m[3][0] = -D3DXVec3Dot(&rightn,peye);
324 pout->u.m[0][1] = upn.x;
325 pout->u.m[1][1] = upn.y;
326 pout->u.m[2][1] = upn.z;
327 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
328 pout->u.m[0][2] = vec.x;
329 pout->u.m[1][2] = vec.y;
330 pout->u.m[2][2] = vec.z;
331 pout->u.m[3][2] = -D3DXVec3Dot(&vec, peye);
332 pout->u.m[0][3] = 0.0f;
333 pout->u.m[1][3] = 0.0f;
334 pout->u.m[2][3] = 0.0f;
335 pout->u.m[3][3] = 1.0f;
339 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, const D3DXVECTOR3 *peye, const D3DXVECTOR3 *pat, const D3DXVECTOR3 *pup)
341 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
343 TRACE("pout %p, peye %p, pat %p, pup %p\n", pout, peye, pat, pup);
345 D3DXVec3Subtract(&vec2, pat, peye);
346 D3DXVec3Normalize(&vec, &vec2);
347 D3DXVec3Cross(&right, pup, &vec);
348 D3DXVec3Cross(&up, &vec, &right);
349 D3DXVec3Normalize(&rightn, &right);
350 D3DXVec3Normalize(&upn, &up);
351 pout->u.m[0][0] = -rightn.x;
352 pout->u.m[1][0] = -rightn.y;
353 pout->u.m[2][0] = -rightn.z;
354 pout->u.m[3][0] = D3DXVec3Dot(&rightn,peye);
355 pout->u.m[0][1] = upn.x;
356 pout->u.m[1][1] = upn.y;
357 pout->u.m[2][1] = upn.z;
358 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
359 pout->u.m[0][2] = -vec.x;
360 pout->u.m[1][2] = -vec.y;
361 pout->u.m[2][2] = -vec.z;
362 pout->u.m[3][2] = D3DXVec3Dot(&vec, peye);
363 pout->u.m[0][3] = 0.0f;
364 pout->u.m[1][3] = 0.0f;
365 pout->u.m[2][3] = 0.0f;
366 pout->u.m[3][3] = 1.0f;
370 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, const D3DXMATRIX *pm1, const D3DXMATRIX *pm2)
375 TRACE("pout %p, pm1 %p, pm2 %p\n", pout, pm1, pm2);
381 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];
389 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, const D3DXMATRIX *pm1, const D3DXMATRIX *pm2)
394 TRACE("pout %p, pm1 %p, pm2 %p\n", pout, pm1, pm2);
396 for (i = 0; i < 4; i++)
397 for (j = 0; j < 4; j++)
398 temp.u.m[j][i] = 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];
404 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
406 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
408 D3DXMatrixIdentity(pout);
409 pout->u.m[0][0] = 2.0f / w;
410 pout->u.m[1][1] = 2.0f / h;
411 pout->u.m[2][2] = 1.0f / (zf - zn);
412 pout->u.m[3][2] = zn / (zn - zf);
416 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
418 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
420 D3DXMatrixIdentity(pout);
421 pout->u.m[0][0] = 2.0f / (r - l);
422 pout->u.m[1][1] = 2.0f / (t - b);
423 pout->u.m[2][2] = 1.0f / (zf -zn);
424 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
425 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
426 pout->u.m[3][2] = zn / (zn -zf);
430 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
432 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
434 D3DXMatrixIdentity(pout);
435 pout->u.m[0][0] = 2.0f / (r - l);
436 pout->u.m[1][1] = 2.0f / (t - b);
437 pout->u.m[2][2] = 1.0f / (zn -zf);
438 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
439 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
440 pout->u.m[3][2] = zn / (zn -zf);
444 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
446 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
448 D3DXMatrixIdentity(pout);
449 pout->u.m[0][0] = 2.0f / w;
450 pout->u.m[1][1] = 2.0f / h;
451 pout->u.m[2][2] = 1.0f / (zn - zf);
452 pout->u.m[3][2] = zn / (zn - zf);
456 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
458 TRACE("pout %p, fovy %f, aspect %f, zn %f, zf %f\n", pout, fovy, aspect, zn, zf);
460 D3DXMatrixIdentity(pout);
461 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
462 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
463 pout->u.m[2][2] = zf / (zf - zn);
464 pout->u.m[2][3] = 1.0f;
465 pout->u.m[3][2] = (zf * zn) / (zn - zf);
466 pout->u.m[3][3] = 0.0f;
470 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
472 TRACE("pout %p, fovy %f, aspect %f, zn %f, zf %f\n", pout, fovy, aspect, zn, zf);
474 D3DXMatrixIdentity(pout);
475 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
476 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
477 pout->u.m[2][2] = zf / (zn - zf);
478 pout->u.m[2][3] = -1.0f;
479 pout->u.m[3][2] = (zf * zn) / (zn - zf);
480 pout->u.m[3][3] = 0.0f;
484 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
486 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
488 D3DXMatrixIdentity(pout);
489 pout->u.m[0][0] = 2.0f * zn / w;
490 pout->u.m[1][1] = 2.0f * zn / h;
491 pout->u.m[2][2] = zf / (zf - zn);
492 pout->u.m[3][2] = (zn * zf) / (zn - zf);
493 pout->u.m[2][3] = 1.0f;
494 pout->u.m[3][3] = 0.0f;
498 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
500 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
502 D3DXMatrixIdentity(pout);
503 pout->u.m[0][0] = 2.0f * zn / (r - l);
504 pout->u.m[1][1] = -2.0f * zn / (b - t);
505 pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
506 pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
507 pout->u.m[2][2] = - zf / (zn - zf);
508 pout->u.m[3][2] = (zn * zf) / (zn -zf);
509 pout->u.m[2][3] = 1.0f;
510 pout->u.m[3][3] = 0.0f;
514 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
516 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
518 D3DXMatrixIdentity(pout);
519 pout->u.m[0][0] = 2.0f * zn / (r - l);
520 pout->u.m[1][1] = -2.0f * zn / (b - t);
521 pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
522 pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
523 pout->u.m[2][2] = zf / (zn - zf);
524 pout->u.m[3][2] = (zn * zf) / (zn -zf);
525 pout->u.m[2][3] = -1.0f;
526 pout->u.m[3][3] = 0.0f;
530 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
532 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
534 D3DXMatrixIdentity(pout);
535 pout->u.m[0][0] = 2.0f * zn / w;
536 pout->u.m[1][1] = 2.0f * zn / h;
537 pout->u.m[2][2] = zf / (zn - zf);
538 pout->u.m[3][2] = (zn * zf) / (zn - zf);
539 pout->u.m[2][3] = -1.0f;
540 pout->u.m[3][3] = 0.0f;
544 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, const D3DXPLANE *pplane)
548 TRACE("pout %p, pplane %p\n", pout, pplane);
550 D3DXPlaneNormalize(&Nplane, pplane);
551 D3DXMatrixIdentity(pout);
552 pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
553 pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
554 pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
555 pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
556 pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
557 pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
558 pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
559 pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
560 pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
561 pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
562 pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
563 pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
567 D3DXMATRIX * WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *out, const D3DXVECTOR3 *v, FLOAT angle)
570 FLOAT sangle, cangle, cdiff;
572 TRACE("out %p, v %p, angle %f\n", out, v, angle);
574 D3DXVec3Normalize(&nv, v);
575 sangle = sinf(angle);
576 cangle = cosf(angle);
577 cdiff = 1.0f - cangle;
579 out->u.m[0][0] = cdiff * nv.x * nv.x + cangle;
580 out->u.m[1][0] = cdiff * nv.x * nv.y - sangle * nv.z;
581 out->u.m[2][0] = cdiff * nv.x * nv.z + sangle * nv.y;
582 out->u.m[3][0] = 0.0f;
583 out->u.m[0][1] = cdiff * nv.y * nv.x + sangle * nv.z;
584 out->u.m[1][1] = cdiff * nv.y * nv.y + cangle;
585 out->u.m[2][1] = cdiff * nv.y * nv.z - sangle * nv.x;
586 out->u.m[3][1] = 0.0f;
587 out->u.m[0][2] = cdiff * nv.z * nv.x - sangle * nv.y;
588 out->u.m[1][2] = cdiff * nv.z * nv.y + sangle * nv.x;
589 out->u.m[2][2] = cdiff * nv.z * nv.z + cangle;
590 out->u.m[3][2] = 0.0f;
591 out->u.m[0][3] = 0.0f;
592 out->u.m[1][3] = 0.0f;
593 out->u.m[2][3] = 0.0f;
594 out->u.m[3][3] = 1.0f;
599 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, const D3DXQUATERNION *pq)
601 TRACE("pout %p, pq %p\n", pout, pq);
603 D3DXMatrixIdentity(pout);
604 pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
605 pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
606 pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
607 pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
608 pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
609 pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
610 pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
611 pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
612 pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
616 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
618 TRACE("pout %p, angle %f\n", pout, angle);
620 D3DXMatrixIdentity(pout);
621 pout->u.m[1][1] = cos(angle);
622 pout->u.m[2][2] = cos(angle);
623 pout->u.m[1][2] = sin(angle);
624 pout->u.m[2][1] = -sin(angle);
628 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
630 TRACE("pout %p, angle %f\n", pout, angle);
632 D3DXMatrixIdentity(pout);
633 pout->u.m[0][0] = cos(angle);
634 pout->u.m[2][2] = cos(angle);
635 pout->u.m[0][2] = -sin(angle);
636 pout->u.m[2][0] = sin(angle);
640 D3DXMATRIX * WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *out, FLOAT yaw, FLOAT pitch, FLOAT roll)
642 FLOAT sroll, croll, spitch, cpitch, syaw, cyaw;
644 TRACE("out %p, yaw %f, pitch %f, roll %f\n", out, yaw, pitch, roll);
648 spitch = sinf(pitch);
649 cpitch = cosf(pitch);
653 out->u.m[0][0] = sroll * spitch * syaw + croll * cyaw;
654 out->u.m[0][1] = sroll * cpitch;
655 out->u.m[0][2] = sroll * spitch * cyaw - croll * syaw;
656 out->u.m[0][3] = 0.0f;
657 out->u.m[1][0] = croll * spitch * syaw - sroll * cyaw;
658 out->u.m[1][1] = croll * cpitch;
659 out->u.m[1][2] = croll * spitch * cyaw + sroll * syaw;
660 out->u.m[1][3] = 0.0f;
661 out->u.m[2][0] = cpitch * syaw;
662 out->u.m[2][1] = -spitch;
663 out->u.m[2][2] = cpitch * cyaw;
664 out->u.m[2][3] = 0.0f;
665 out->u.m[3][0] = 0.0f;
666 out->u.m[3][1] = 0.0f;
667 out->u.m[3][2] = 0.0f;
668 out->u.m[3][3] = 1.0f;
673 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
675 TRACE("pout %p, angle %f\n", pout, angle);
677 D3DXMatrixIdentity(pout);
678 pout->u.m[0][0] = cos(angle);
679 pout->u.m[1][1] = cos(angle);
680 pout->u.m[0][1] = sin(angle);
681 pout->u.m[1][0] = -sin(angle);
685 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
687 TRACE("pout %p, sx %f, sy %f, sz %f\n", pout, sx, sy, sz);
689 D3DXMatrixIdentity(pout);
690 pout->u.m[0][0] = sx;
691 pout->u.m[1][1] = sy;
692 pout->u.m[2][2] = sz;
696 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, const D3DXVECTOR4 *plight, const D3DXPLANE *pplane)
701 TRACE("pout %p, plight %p, pplane %p\n", pout, plight, pplane);
703 D3DXPlaneNormalize(&Nplane, pplane);
704 dot = D3DXPlaneDot(&Nplane, plight);
705 pout->u.m[0][0] = dot - Nplane.a * plight->x;
706 pout->u.m[0][1] = -Nplane.a * plight->y;
707 pout->u.m[0][2] = -Nplane.a * plight->z;
708 pout->u.m[0][3] = -Nplane.a * plight->w;
709 pout->u.m[1][0] = -Nplane.b * plight->x;
710 pout->u.m[1][1] = dot - Nplane.b * plight->y;
711 pout->u.m[1][2] = -Nplane.b * plight->z;
712 pout->u.m[1][3] = -Nplane.b * plight->w;
713 pout->u.m[2][0] = -Nplane.c * plight->x;
714 pout->u.m[2][1] = -Nplane.c * plight->y;
715 pout->u.m[2][2] = dot - Nplane.c * plight->z;
716 pout->u.m[2][3] = -Nplane.c * plight->w;
717 pout->u.m[3][0] = -Nplane.d * plight->x;
718 pout->u.m[3][1] = -Nplane.d * plight->y;
719 pout->u.m[3][2] = -Nplane.d * plight->z;
720 pout->u.m[3][3] = dot - Nplane.d * plight->w;
724 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, const D3DXVECTOR3 *pscalingcenter, const D3DXQUATERNION *pscalingrotation, const D3DXVECTOR3 *pscaling, const D3DXVECTOR3 *protationcenter, const D3DXQUATERNION *protation, const D3DXVECTOR3 *ptranslation)
726 D3DXMATRIX m1, m2, m3, m4, m5, m6, m7;
730 TRACE("pout %p, pscalingcenter %p, pscalingrotation %p, pscaling %p, protationcentr %p, protation %p, ptranslation %p\n",
731 pout, pscalingcenter, pscalingrotation, pscaling, protationcenter, protation, ptranslation);
733 if ( !pscalingcenter )
741 psc.x = pscalingcenter->x;
742 psc.y = pscalingcenter->y;
743 psc.z = pscalingcenter->z;
746 if ( !protationcenter )
754 prc.x = protationcenter->x;
755 prc.y = protationcenter->y;
756 prc.z = protationcenter->z;
767 pt.x = ptranslation->x;
768 pt.y = ptranslation->y;
769 pt.z = ptranslation->z;
772 D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
774 if ( !pscalingrotation )
776 D3DXMatrixIdentity(&m2);
777 D3DXMatrixIdentity(&m4);
781 D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
782 D3DXMatrixInverse(&m2, NULL, &m4);
785 if ( !pscaling ) D3DXMatrixIdentity(&m3);
786 else D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
788 if ( !protation ) D3DXMatrixIdentity(&m6);
789 else D3DXMatrixRotationQuaternion(&m6, protation);
791 D3DXMatrixTranslation(&m5, psc.x - prc.x, psc.y - prc.y, psc.z - prc.z);
792 D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
793 D3DXMatrixMultiply(&m1, &m1, &m2);
794 D3DXMatrixMultiply(&m1, &m1, &m3);
795 D3DXMatrixMultiply(&m1, &m1, &m4);
796 D3DXMatrixMultiply(&m1, &m1, &m5);
797 D3DXMatrixMultiply(&m1, &m1, &m6);
798 D3DXMatrixMultiply(pout, &m1, &m7);
802 D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(D3DXMATRIX *pout, const D3DXVECTOR2 *pscalingcenter, FLOAT scalingrotation, const D3DXVECTOR2 *pscaling, const D3DXVECTOR2 *protationcenter, FLOAT rotation, const D3DXVECTOR2 *ptranslation)
804 D3DXQUATERNION rot, sca_rot;
805 D3DXVECTOR3 rot_center, sca, sca_center, trans;
807 TRACE("pout %p, pscalingcenter %p, scalingrotation %f, pscaling %p, protztioncenter %p, rotation %f, ptranslation %p\n",
808 pout, pscalingcenter, scalingrotation, pscaling, protationcenter, rotation, ptranslation);
810 if ( pscalingcenter )
812 sca_center.x=pscalingcenter->x;
813 sca_center.y=pscalingcenter->y;
836 if ( protationcenter )
838 rot_center.x=protationcenter->x;
839 rot_center.y=protationcenter->y;
851 trans.x=ptranslation->x;
852 trans.y=ptranslation->y;
862 rot.w=cos(rotation/2.0f);
865 rot.z=sin(rotation/2.0f);
867 sca_rot.w=cos(scalingrotation/2.0f);
870 sca_rot.z=sin(scalingrotation/2.0f);
872 D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
877 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
879 TRACE("pout %p, x %f, y %f, z%f\n", pout, x, y, z);
881 D3DXMatrixIdentity(pout);
888 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, const D3DXMATRIX *pm)
890 const D3DXMATRIX m = *pm;
893 TRACE("pout %p, pm %p\n", pout, pm);
896 for (j=0; j<4; j++) pout->u.m[i][j] = m.u.m[j][i];
901 /*_________________D3DXMatrixStack____________________*/
903 static const unsigned int INITIAL_STACK_SIZE = 32;
905 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, ID3DXMatrixStack **ppstack)
907 struct ID3DXMatrixStackImpl *object;
909 TRACE("flags %#x, ppstack %p\n", flags, ppstack);
911 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
915 return E_OUTOFMEMORY;
917 object->ID3DXMatrixStack_iface.lpVtbl = &ID3DXMatrixStack_Vtbl;
920 object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(*object->stack));
923 HeapFree(GetProcessHeap(), 0, object);
925 return E_OUTOFMEMORY;
929 object->stack_size = INITIAL_STACK_SIZE;
930 D3DXMatrixIdentity(&object->stack[0]);
932 TRACE("Created matrix stack %p\n", object);
934 *ppstack = &object->ID3DXMatrixStack_iface;
938 static inline struct ID3DXMatrixStackImpl *impl_from_ID3DXMatrixStack(ID3DXMatrixStack *iface)
940 return CONTAINING_RECORD(iface, struct ID3DXMatrixStackImpl, ID3DXMatrixStack_iface);
943 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **out)
945 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
947 if (IsEqualGUID(riid, &IID_ID3DXMatrixStack)
948 || IsEqualGUID(riid, &IID_IUnknown))
950 ID3DXMatrixStack_AddRef(iface);
955 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
958 return E_NOINTERFACE;
961 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
963 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
964 ULONG ref = InterlockedIncrement(&This->ref);
965 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
969 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack *iface)
971 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
972 ULONG ref = InterlockedDecrement(&This->ref);
975 HeapFree(GetProcessHeap(), 0, This->stack);
976 HeapFree(GetProcessHeap(), 0, This);
978 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
982 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
984 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
986 TRACE("iface %p\n", iface);
988 return &This->stack[This->current];
991 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
993 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
995 TRACE("iface %p\n", iface);
997 D3DXMatrixIdentity(&This->stack[This->current]);
1002 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
1004 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1006 TRACE("iface %p, pm %p\n", iface, pm);
1008 This->stack[This->current] = *pm;
1013 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
1015 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1017 TRACE("iface %p, pm %p\n", iface, pm);
1019 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], pm);
1024 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
1026 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1028 TRACE("iface %p, pm %p\n", iface, pm);
1030 D3DXMatrixMultiply(&This->stack[This->current], pm, &This->stack[This->current]);
1035 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
1037 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1039 TRACE("iface %p\n", iface);
1041 /* Popping the last element on the stack returns D3D_OK, but does nothing. */
1042 if (!This->current) return D3D_OK;
1044 if (This->current <= This->stack_size / 4 && This->stack_size >= INITIAL_STACK_SIZE * 2)
1046 unsigned int new_size;
1047 D3DXMATRIX *new_stack;
1049 new_size = This->stack_size / 2;
1050 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1053 This->stack_size = new_size;
1054 This->stack = new_stack;
1063 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
1065 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1067 TRACE("iface %p\n", iface);
1069 if (This->current == This->stack_size - 1)
1071 unsigned int new_size;
1072 D3DXMATRIX *new_stack;
1074 if (This->stack_size > UINT_MAX / 2) return E_OUTOFMEMORY;
1076 new_size = This->stack_size * 2;
1077 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1078 if (!new_stack) return E_OUTOFMEMORY;
1080 This->stack_size = new_size;
1081 This->stack = new_stack;
1085 This->stack[This->current] = This->stack[This->current - 1];
1090 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, const D3DXVECTOR3 *pv, FLOAT angle)
1093 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1095 TRACE("iface %p, pv %p, angle %f\n", iface, pv, angle);
1097 D3DXMatrixRotationAxis(&temp, pv, angle);
1098 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1103 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, const D3DXVECTOR3 *pv, FLOAT angle)
1106 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1108 TRACE("iface %p, pv %p, angle %f\n", iface, pv, angle);
1110 D3DXMatrixRotationAxis(&temp, pv, angle);
1111 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1116 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1119 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1121 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1123 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1124 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1129 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1132 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1134 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1136 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1137 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1142 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1145 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1147 TRACE("iface %p,x %f, y %f, z %f\n", iface, x, y, z);
1149 D3DXMatrixScaling(&temp, x, y, z);
1150 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1155 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1158 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1160 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1162 D3DXMatrixScaling(&temp, x, y, z);
1163 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1168 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1171 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1173 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1175 D3DXMatrixTranslation(&temp, x, y, z);
1176 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1181 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1184 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1186 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1188 D3DXMatrixTranslation(&temp, x, y, z);
1189 D3DXMatrixMultiply(&This->stack[This->current], &temp,&This->stack[This->current]);
1194 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
1196 ID3DXMatrixStackImpl_QueryInterface,
1197 ID3DXMatrixStackImpl_AddRef,
1198 ID3DXMatrixStackImpl_Release,
1199 ID3DXMatrixStackImpl_Pop,
1200 ID3DXMatrixStackImpl_Push,
1201 ID3DXMatrixStackImpl_LoadIdentity,
1202 ID3DXMatrixStackImpl_LoadMatrix,
1203 ID3DXMatrixStackImpl_MultMatrix,
1204 ID3DXMatrixStackImpl_MultMatrixLocal,
1205 ID3DXMatrixStackImpl_RotateAxis,
1206 ID3DXMatrixStackImpl_RotateAxisLocal,
1207 ID3DXMatrixStackImpl_RotateYawPitchRoll,
1208 ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
1209 ID3DXMatrixStackImpl_Scale,
1210 ID3DXMatrixStackImpl_ScaleLocal,
1211 ID3DXMatrixStackImpl_Translate,
1212 ID3DXMatrixStackImpl_TranslateLocal,
1213 ID3DXMatrixStackImpl_GetTop
1216 /*_________________D3DXPLANE________________*/
1218 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, const D3DXVECTOR3 *pvpoint, const D3DXVECTOR3 *pvnormal)
1220 TRACE("pout %p, pvpoint %p, pvnormal %p\n", pout, pvpoint, pvnormal);
1222 pout->a = pvnormal->x;
1223 pout->b = pvnormal->y;
1224 pout->c = pvnormal->z;
1225 pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
1229 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3)
1231 D3DXVECTOR3 edge1, edge2, normal, Nnormal;
1233 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p\n", pout, pv1, pv2, pv3);
1235 edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
1236 edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
1237 D3DXVec3Subtract(&edge1, pv2, pv1);
1238 D3DXVec3Subtract(&edge2, pv3, pv1);
1239 D3DXVec3Cross(&normal, &edge1, &edge2);
1240 D3DXVec3Normalize(&Nnormal, &normal);
1241 D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
1245 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, const D3DXPLANE *pp, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2)
1247 D3DXVECTOR3 direction, normal;
1250 TRACE("pout %p, pp %p, pv1 %p, pv2 %p\n", pout, pp, pv1, pv2);
1255 direction.x = pv2->x - pv1->x;
1256 direction.y = pv2->y - pv1->y;
1257 direction.z = pv2->z - pv1->z;
1258 dot = D3DXVec3Dot(&normal, &direction);
1259 if ( !dot ) return NULL;
1260 temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
1261 pout->x = pv1->x - temp * direction.x;
1262 pout->y = pv1->y - temp * direction.y;
1263 pout->z = pv1->z - temp * direction.z;
1267 D3DXPLANE * WINAPI D3DXPlaneNormalize(D3DXPLANE *out, const D3DXPLANE *p)
1271 TRACE("out %p, p %p\n", out, p);
1273 norm = sqrtf(p->a * p->a + p->b * p->b + p->c * p->c);
1276 out->a = p->a / norm;
1277 out->b = p->b / norm;
1278 out->c = p->c / norm;
1279 out->d = p->d / norm;
1292 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, const D3DXPLANE *pplane, const D3DXMATRIX *pm)
1294 const D3DXPLANE plane = *pplane;
1296 TRACE("pout %p, pplane %p, pm %p\n", pout, pplane, pm);
1298 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;
1299 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;
1300 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;
1301 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;
1305 D3DXPLANE* WINAPI D3DXPlaneTransformArray(D3DXPLANE* out, UINT outstride, const D3DXPLANE* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1309 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1311 for (i = 0; i < elements; ++i) {
1313 (D3DXPLANE*)((char*)out + outstride * i),
1314 (const D3DXPLANE*)((const char*)in + instride * i),
1320 /*_________________D3DXQUATERNION________________*/
1322 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
1324 D3DXQUATERNION temp1, temp2;
1326 TRACE("pout %p, pq1 %p, pq2 %p, pq3 %p, f %f, g %f\n", pout, pq1, pq2, pq3, f, g);
1328 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
1332 D3DXQUATERNION * WINAPI D3DXQuaternionExp(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1336 TRACE("out %p, q %p\n", out, q);
1338 norm = sqrtf(q->x * q->x + q->y * q->y + q->z * q->z);
1341 out->x = sinf(norm) * q->x / norm;
1342 out->y = sinf(norm) * q->y / norm;
1343 out->z = sinf(norm) * q->z / norm;
1344 out->w = cosf(norm);
1357 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, const D3DXQUATERNION *pq)
1362 TRACE("pout %p, pq %p\n", pout, pq);
1364 norm = D3DXQuaternionLengthSq(pq);
1366 out.x = -pq->x / norm;
1367 out.y = -pq->y / norm;
1368 out.z = -pq->z / norm;
1369 out.w = pq->w / norm;
1375 D3DXQUATERNION * WINAPI D3DXQuaternionLn(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1379 TRACE("out %p, q %p\n", out, q);
1381 if ((q->w >= 1.0f) || (q->w == -1.0f))
1384 t = acosf(q->w) / sqrtf(1.0f - q->w * q->w);
1394 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2)
1398 TRACE("pout %p, pq1 %p, pq2 %p\n", pout, pq1, pq2);
1400 out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
1401 out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
1402 out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
1403 out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
1408 D3DXQUATERNION * WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1412 TRACE("out %p, q %p\n", out, q);
1414 norm = D3DXQuaternionLength(q);
1416 out->x = q->x / norm;
1417 out->y = q->y / norm;
1418 out->z = q->z / norm;
1419 out->w = q->w / norm;
1424 D3DXQUATERNION * WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *out, const D3DXVECTOR3 *v, FLOAT angle)
1428 TRACE("out %p, v %p, angle %f\n", out, v, angle);
1430 D3DXVec3Normalize(&temp, v);
1432 out->x = sinf(angle / 2.0f) * temp.x;
1433 out->y = sinf(angle / 2.0f) * temp.y;
1434 out->z = sinf(angle / 2.0f) * temp.z;
1435 out->w = cosf(angle / 2.0f);
1440 D3DXQUATERNION * WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *out, const D3DXMATRIX *m)
1444 TRACE("out %p, m %p\n", out, m);
1446 trace = m->u.m[0][0] + m->u.m[1][1] + m->u.m[2][2] + 1.0f;
1449 s = 2.0f * sqrtf(trace);
1450 out->x = (m->u.m[1][2] - m->u.m[2][1]) / s;
1451 out->y = (m->u.m[2][0] - m->u.m[0][2]) / s;
1452 out->z = (m->u.m[0][1] - m->u.m[1][0]) / s;
1459 for (i = 1; i < 3; i++)
1461 if (m->u.m[i][i] > m->u.m[maxi][maxi])
1468 s = 2.0f * sqrtf(1.0f + m->u.m[0][0] - m->u.m[1][1] - m->u.m[2][2]);
1470 out->y = (m->u.m[0][1] + m->u.m[1][0]) / s;
1471 out->z = (m->u.m[0][2] + m->u.m[2][0]) / s;
1472 out->w = (m->u.m[1][2] - m->u.m[2][1]) / s;
1476 s = 2.0f * sqrtf(1.0f + m->u.m[1][1] - m->u.m[0][0] - m->u.m[2][2]);
1477 out->x = (m->u.m[0][1] + m->u.m[1][0]) / s;
1479 out->z = (m->u.m[1][2] + m->u.m[2][1]) / s;
1480 out->w = (m->u.m[2][0] - m->u.m[0][2]) / s;
1484 s = 2.0f * sqrtf(1.0f + m->u.m[2][2] - m->u.m[0][0] - m->u.m[1][1]);
1485 out->x = (m->u.m[0][2] + m->u.m[2][0]) / s;
1486 out->y = (m->u.m[1][2] + m->u.m[2][1]) / s;
1488 out->w = (m->u.m[0][1] - m->u.m[1][0]) / s;
1496 D3DXQUATERNION * WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *out, FLOAT yaw, FLOAT pitch, FLOAT roll)
1498 FLOAT syaw, cyaw, spitch, cpitch, sroll, croll;
1500 TRACE("out %p, yaw %f, pitch %f, roll %f\n", out, yaw, pitch, roll);
1502 syaw = sinf(yaw / 2.0f);
1503 cyaw = cosf(yaw / 2.0f);
1504 spitch = sinf(pitch / 2.0f);
1505 cpitch = cosf(pitch / 2.0f);
1506 sroll = sinf(roll / 2.0f);
1507 croll = cosf(roll / 2.0f);
1509 out->x = syaw * cpitch * sroll + cyaw * spitch * croll;
1510 out->y = syaw * cpitch * croll - cyaw * spitch * sroll;
1511 out->z = cyaw * cpitch * sroll - syaw * spitch * croll;
1512 out->w = cyaw * cpitch * croll + syaw * spitch * sroll;
1517 D3DXQUATERNION * WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *out, const D3DXQUATERNION *q1,
1518 const D3DXQUATERNION *q2, FLOAT t)
1522 TRACE("out %p, q1 %p, q2 %p, t %f\n", out, q1, q2, t);
1525 dot = D3DXQuaternionDot(q1, q2);
1532 if (1.0f - dot > 0.001f)
1534 FLOAT theta = acosf(dot);
1536 temp = sinf(theta * temp) / sinf(theta);
1537 t = sinf(theta * t) / sinf(theta);
1540 out->x = temp * q1->x + t * q2->x;
1541 out->y = temp * q1->y + t * q2->y;
1542 out->z = temp * q1->z + t * q2->z;
1543 out->w = temp * q1->w + t * q2->w;
1548 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3, const D3DXQUATERNION *pq4, FLOAT t)
1550 D3DXQUATERNION temp1, temp2;
1552 TRACE("pout %p, pq1 %p, pq2 %p, pq3 %p, pq4 %p, t %f\n", pout, pq1, pq2, pq3, pq4, t);
1554 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1558 static D3DXQUATERNION add_diff(const D3DXQUATERNION *q1, const D3DXQUATERNION *q2, const FLOAT add)
1560 D3DXQUATERNION temp;
1562 temp.x = q1->x + add * q2->x;
1563 temp.y = q1->y + add * q2->y;
1564 temp.z = q1->z + add * q2->z;
1565 temp.w = q1->w + add * q2->w;
1570 void WINAPI D3DXQuaternionSquadSetup(D3DXQUATERNION *paout, D3DXQUATERNION *pbout, D3DXQUATERNION *pcout, const D3DXQUATERNION *pq0, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3)
1572 D3DXQUATERNION q, temp1, temp2, temp3, zero;
1574 TRACE("paout %p, pbout %p, pcout %p, pq0 %p, 1 %p, pq2 %p, pq3 %p\n", paout, pbout, pcout, pq0, pq1, pq2, pq3);
1581 if ( D3DXQuaternionDot(pq0, pq1) < 0.0f )
1582 temp2 = add_diff(&zero, pq0, -1.0f);
1586 if ( D3DXQuaternionDot(pq1, pq2) < 0.0f )
1587 *pcout = add_diff(&zero, pq2, -1.0f);
1591 if ( D3DXQuaternionDot(pcout, pq3) < 0.0f )
1592 temp3 = add_diff(&zero, pq3, -1.0f);
1596 D3DXQuaternionInverse(&temp1, pq1);
1597 D3DXQuaternionMultiply(&temp2, &temp1, &temp2);
1598 D3DXQuaternionLn(&temp2, &temp2);
1599 D3DXQuaternionMultiply(&q, &temp1, pcout);
1600 D3DXQuaternionLn(&q, &q);
1601 temp1 = add_diff(&temp2, &q, 1.0f);
1606 D3DXQuaternionExp(&temp1, &temp1);
1607 D3DXQuaternionMultiply(paout, pq1, &temp1);
1609 D3DXQuaternionInverse(&temp1, pcout);
1610 D3DXQuaternionMultiply(&temp2, &temp1, pq1);
1611 D3DXQuaternionLn(&temp2, &temp2);
1612 D3DXQuaternionMultiply(&q, &temp1, &temp3);
1613 D3DXQuaternionLn(&q, &q);
1614 temp1 = add_diff(&temp2, &q, 1.0f);
1619 D3DXQuaternionExp(&temp1, &temp1);
1620 D3DXQuaternionMultiply(pbout, pcout, &temp1);
1625 void WINAPI D3DXQuaternionToAxisAngle(const D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1627 TRACE("pq %p, paxis %p, pangle %p\n", pq, paxis, pangle);
1632 *pangle = 2.0f * acos(pq->w);
1635 /*_________________D3DXVec2_____________________*/
1637 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1639 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
1641 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1642 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1646 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv0, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pv3, FLOAT s)
1648 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
1650 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);
1651 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);
1655 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pt1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pt2, FLOAT s)
1657 FLOAT h1, h2, h3, h4;
1659 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
1661 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1662 h2 = s * s * s - 2.0f * s * s + s;
1663 h3 = -2.0f * s * s * s + 3.0f * s * s;
1664 h4 = s * s * s - s * s;
1666 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1667 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1671 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv)
1675 TRACE("pout %p, pv %p\n", pout, pv);
1677 norm = D3DXVec2Length(pv);
1685 pout->x = pv->x / norm;
1686 pout->y = pv->y / norm;
1692 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1694 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1696 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
1697 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
1698 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
1699 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1703 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR2* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1707 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1709 for (i = 0; i < elements; ++i) {
1711 (D3DXVECTOR4*)((char*)out + outstride * i),
1712 (const D3DXVECTOR2*)((const char*)in + instride * i),
1718 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1723 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1726 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1728 pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1729 pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1734 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, const D3DXVECTOR2* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1738 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1740 for (i = 0; i < elements; ++i) {
1741 D3DXVec2TransformCoord(
1742 (D3DXVECTOR2*)((char*)out + outstride * i),
1743 (const D3DXVECTOR2*)((const char*)in + instride * i),
1749 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1751 const D3DXVECTOR2 v = *pv;
1753 TRACE("pout %p, pv %p, pm %p", pout, pv, pm);
1755 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1756 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1760 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, const D3DXVECTOR2 *in, UINT instride, const D3DXMATRIX *matrix, UINT elements)
1764 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1766 for (i = 0; i < elements; ++i) {
1767 D3DXVec2TransformNormal(
1768 (D3DXVECTOR2*)((char*)out + outstride * i),
1769 (const D3DXVECTOR2*)((const char*)in + instride * i),
1775 /*_________________D3DXVec3_____________________*/
1777 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1779 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
1781 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1782 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1783 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1787 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv0, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3, FLOAT s)
1789 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
1791 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);
1792 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);
1793 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);
1797 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pt1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pt2, FLOAT s)
1799 FLOAT h1, h2, h3, h4;
1801 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
1803 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1804 h2 = s * s * s - 2.0f * s * s + s;
1805 h3 = -2.0f * s * s * s + 3.0f * s * s;
1806 h4 = s * s * s - s * s;
1808 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1809 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1810 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1814 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv)
1818 TRACE("pout %p, pv %p\n", pout, pv);
1820 norm = D3DXVec3Length(pv);
1829 pout->x = pv->x / norm;
1830 pout->y = pv->y / norm;
1831 pout->z = pv->z / norm;
1837 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DVIEWPORT9 *pviewport, const D3DXMATRIX *pprojection, const D3DXMATRIX *pview, const D3DXMATRIX *pworld)
1841 TRACE("pout %p, pv %p, pviewport %p, pprojection %p, pview %p, pworld %p\n", pout, pv, pviewport, pprojection, pview, pworld);
1843 D3DXMatrixIdentity(&m);
1844 if (pworld) D3DXMatrixMultiply(&m, &m, pworld);
1845 if (pview) D3DXMatrixMultiply(&m, &m, pview);
1846 if (pprojection) D3DXMatrixMultiply(&m, &m, pprojection);
1848 D3DXVec3TransformCoord(pout, pv, &m);
1852 pout->x = pviewport->X + ( 1.0f + pout->x ) * pviewport->Width / 2.0f;
1853 pout->y = pviewport->Y + ( 1.0f - pout->y ) * pviewport->Height / 2.0f;
1854 pout->z = pviewport->MinZ + pout->z * ( pviewport->MaxZ - pviewport->MinZ );
1859 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)
1863 TRACE("out %p, outstride %u, in %p, instride %u, viewport %p, projection %p, view %p, world %p, elements %u\n",
1864 out, outstride, in, instride, viewport, projection, view, world, elements);
1866 for (i = 0; i < elements; ++i) {
1868 (D3DXVECTOR3*)((char*)out + outstride * i),
1869 (const D3DXVECTOR3*)((const char*)in + instride * i),
1870 viewport, projection, view, world);
1875 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1877 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1879 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];
1880 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];
1881 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];
1882 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];
1886 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1890 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1892 for (i = 0; i < elements; ++i) {
1894 (D3DXVECTOR4*)((char*)out + outstride * i),
1895 (const D3DXVECTOR3*)((const char*)in + instride * i),
1901 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1906 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1908 norm = 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];
1910 out.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]) / norm;
1911 out.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]) / norm;
1912 out.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]) / norm;
1919 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(D3DXVECTOR3* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1923 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1925 for (i = 0; i < elements; ++i) {
1926 D3DXVec3TransformCoord(
1927 (D3DXVECTOR3*)((char*)out + outstride * i),
1928 (const D3DXVECTOR3*)((const char*)in + instride * i),
1934 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1936 const D3DXVECTOR3 v = *pv;
1938 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1940 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1941 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1942 pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1947 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1951 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1953 for (i = 0; i < elements; ++i) {
1954 D3DXVec3TransformNormal(
1955 (D3DXVECTOR3*)((char*)out + outstride * i),
1956 (const D3DXVECTOR3*)((const char*)in + instride * i),
1962 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DVIEWPORT9 *pviewport, const D3DXMATRIX *pprojection, const D3DXMATRIX *pview, const D3DXMATRIX *pworld)
1966 TRACE("pout %p, pv %p, pviewport %p, pprojection %p, pview %p, pworlds %p\n", pout, pv, pviewport, pprojection, pview, pworld);
1968 D3DXMatrixIdentity(&m);
1969 if (pworld) D3DXMatrixMultiply(&m, &m, pworld);
1970 if (pview) D3DXMatrixMultiply(&m, &m, pview);
1971 if (pprojection) D3DXMatrixMultiply(&m, &m, pprojection);
1972 D3DXMatrixInverse(&m, NULL, &m);
1977 pout->x = 2.0f * ( pout->x - pviewport->X ) / pviewport->Width - 1.0f;
1978 pout->y = 1.0f - 2.0f * ( pout->y - pviewport->Y ) / pviewport->Height;
1979 pout->z = ( pout->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
1981 D3DXVec3TransformCoord(pout, pout, &m);
1985 D3DXVECTOR3* WINAPI D3DXVec3UnprojectArray(D3DXVECTOR3* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DVIEWPORT9* viewport, const D3DXMATRIX* projection, const D3DXMATRIX* view, const D3DXMATRIX* world, UINT elements)
1989 TRACE("out %p, outstride %u, in %p, instride %u, viewport %p, projection %p, view %p, world %p, elements %u\n",
1990 out, outstride, in, instride, viewport, projection, view, world, elements);
1992 for (i = 0; i < elements; ++i) {
1994 (D3DXVECTOR3*)((char*)out + outstride * i),
1995 (const D3DXVECTOR3*)((const char*)in + instride * i),
1996 viewport, projection, view, world);
2001 /*_________________D3DXVec4_____________________*/
2003 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
2005 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
2007 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
2008 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
2009 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
2010 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
2014 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv0, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3, FLOAT s)
2016 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
2018 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);
2019 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);
2020 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);
2021 pout->w = 0.5f * (2.0f * pv1->w + (pv2->w - pv0->w) *s + (2.0f *pv0->w - 5.0f * pv1->w + 4.0f * pv2->w - pv3->w) * s * s + (pv3->w -3.0f * pv2->w + 3.0f * pv1->w - pv0->w) * s * s * s);
2025 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3)
2029 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p\n", pout, pv1, pv2, pv3);
2031 out.x = pv1->y * (pv2->z * pv3->w - pv3->z * pv2->w) - pv1->z * (pv2->y * pv3->w - pv3->y * pv2->w) + pv1->w * (pv2->y * pv3->z - pv2->z *pv3->y);
2032 out.y = -(pv1->x * (pv2->z * pv3->w - pv3->z * pv2->w) - pv1->z * (pv2->x * pv3->w - pv3->x * pv2->w) + pv1->w * (pv2->x * pv3->z - pv3->x * pv2->z));
2033 out.z = pv1->x * (pv2->y * pv3->w - pv3->y * pv2->w) - pv1->y * (pv2->x *pv3->w - pv3->x * pv2->w) + pv1->w * (pv2->x * pv3->y - pv3->x * pv2->y);
2034 out.w = -(pv1->x * (pv2->y * pv3->z - pv3->y * pv2->z) - pv1->y * (pv2->x * pv3->z - pv3->x *pv2->z) + pv1->z * (pv2->x * pv3->y - pv3->x * pv2->y));
2039 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pt1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pt2, FLOAT s)
2041 FLOAT h1, h2, h3, h4;
2043 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
2045 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
2046 h2 = s * s * s - 2.0f * s * s + s;
2047 h3 = -2.0f * s * s * s + 3.0f * s * s;
2048 h4 = s * s * s - s * s;
2050 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
2051 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
2052 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
2053 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
2057 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv)
2061 TRACE("pout %p, pv %p\n", pout, pv);
2063 norm = D3DXVec4Length(pv);
2065 pout->x = pv->x / norm;
2066 pout->y = pv->y / norm;
2067 pout->z = pv->z / norm;
2068 pout->w = pv->w / norm;
2073 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv, const D3DXMATRIX *pm)
2077 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
2079 out.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] * pv->w;
2080 out.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] * pv->w;
2081 out.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] * pv->w;
2082 out.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] * pv->w;
2087 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR4* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
2091 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
2093 for (i = 0; i < elements; ++i) {
2095 (D3DXVECTOR4*)((char*)out + outstride * i),
2096 (const D3DXVECTOR4*)((const char*)in + instride * i),
2102 unsigned short float_32_to_16(const float in)
2104 int exp = 0, origexp;
2105 float tmp = fabs(in);
2106 int sign = (copysignf(1, in) < 0);
2107 unsigned int mantissa;
2110 /* Deal with special numbers */
2111 if (isinf(in)) return (sign ? 0xffff : 0x7fff);
2112 if (isnan(in)) return (sign ? 0xffff : 0x7fff);
2113 if (in == 0.0f) return (sign ? 0x8000 : 0x0000);
2115 if (tmp < powf(2, 10))
2121 } while (tmp < powf(2, 10));
2123 else if (tmp >= powf(2, 11))
2129 } while (tmp >= powf(2, 11));
2132 exp += 10; /* Normalize the mantissa */
2133 exp += 15; /* Exponent is encoded with excess 15 */
2137 mantissa = (unsigned int) tmp;
2138 if ((tmp - mantissa == 0.5f && mantissa % 2 == 1) || /* round half to even */
2139 (tmp - mantissa > 0.5f))
2141 mantissa++; /* round to nearest, away from zero */
2143 if (mantissa == 2048)
2152 ret = 0x7fff; /* INF */
2156 unsigned int rounding = 0;
2158 /* Denormalized half float */
2160 /* return 0x0000 (=0.0) for numbers too small to represent in half floats */
2162 return (sign ? 0x8000 : 0x0000);
2166 /* the 13 extra bits from single precision are used for rounding */
2167 mantissa = (unsigned int)(tmp * powf(2, 13));
2168 mantissa >>= 1 - exp; /* denormalize */
2170 mantissa -= ~(mantissa >> 13) & 1; /* round half to even */
2171 /* remove 13 least significant bits to get half float precision */
2173 rounding = mantissa & 1;
2176 ret = mantissa + rounding;
2180 ret = (exp << 10) | (mantissa & 0x3ff);
2183 ret |= ((sign ? 1 : 0) << 15); /* Add the sign */
2187 D3DXFLOAT16 *WINAPI D3DXFloat32To16Array(D3DXFLOAT16 *pout, const FLOAT *pin, UINT n)
2191 TRACE("pout %p, pin %p, n %u\n", pout, pin, n);
2193 for (i = 0; i < n; ++i)
2195 pout[i].value = float_32_to_16(pin[i]);
2201 /* Native d3dx9's D3DXFloat16to32Array lacks support for NaN and Inf. Specifically, e = 16 is treated as a
2202 * regular number - e.g., 0x7fff is converted to 131008.0 and 0xffff to -131008.0. */
2203 static inline float float_16_to_32(const unsigned short in)
2205 const unsigned short s = (in & 0x8000);
2206 const unsigned short e = (in & 0x7C00) >> 10;
2207 const unsigned short m = in & 0x3FF;
2208 const float sgn = (s ? -1.0f : 1.0f);
2212 if (m == 0) return sgn * 0.0f; /* +0.0 or -0.0 */
2213 else return sgn * powf(2, -14.0f) * (m / 1024.0f);
2217 return sgn * powf(2, e - 15.0f) * (1.0f + (m / 1024.0f));
2221 FLOAT *WINAPI D3DXFloat16To32Array(FLOAT *pout, const D3DXFLOAT16 *pin, UINT n)
2225 TRACE("pout %p, pin %p, n %u\n", pout, pin, n);
2227 for (i = 0; i < n; ++i)
2229 pout[i] = float_16_to_32(pin[i].value);
2235 /*_________________D3DXSH________________*/
2237 FLOAT* WINAPI D3DXSHAdd(FLOAT *out, UINT order, const FLOAT *a, const FLOAT *b)
2241 TRACE("out %p, order %u, a %p, b %p\n", out, order, a, b);
2243 for (i = 0; i < order * order; i++)
2244 out[i] = a[i] + b[i];
2249 FLOAT WINAPI D3DXSHDot(UINT order, const FLOAT *a, const FLOAT *b)
2254 TRACE("order %u, a %p, b %p\n", order, a, b);
2257 for (i = 1; i < order * order; i++)
2263 FLOAT* WINAPI D3DXSHEvalDirection(FLOAT *out, UINT order, const D3DXVECTOR3 *dir)
2266 TRACE("out %p, order %u, dir %p\n", out, order, dir);
2268 if ( (order < D3DXSH_MINORDER) || (order > D3DXSH_MAXORDER) )
2271 out[0] = 0.5f / sqrt(D3DX_PI);
2272 out[1] = -0.5f / sqrt(D3DX_PI / 3.0f) * dir->y;
2273 out[2] = 0.5f / sqrt(D3DX_PI / 3.0f) * dir->z;
2274 out[3] = -0.5f / sqrt(D3DX_PI / 3.0f) * dir->x;
2278 out[4] = 0.5f / sqrt(D3DX_PI / 15.0f) * dir->x * dir->y;
2279 out[5] = -0.5f / sqrt(D3DX_PI / 15.0f) * dir->y * dir->z;
2280 out[6] = 0.25f / sqrt(D3DX_PI / 5.0f) * ( 3.0f * dir->z * dir->z - 1.0f );
2281 out[7] = -0.5f / sqrt(D3DX_PI / 15.0f) * dir->x * dir->z;
2282 out[8] = 0.25f / sqrt(D3DX_PI / 15.0f) * ( dir->x * dir->x - dir->y * dir->y );
2286 out[9] = -sqrt(70.0f / D3DX_PI) / 8.0f * dir->y * (3.0f * dir->x * dir->x - dir->y * dir->y );
2287 out[10] = sqrt(105.0f / D3DX_PI) / 2.0f * dir->x * dir->y * dir->z;
2288 out[11] = -sqrt(42.0 / D3DX_PI) / 8.0f * dir->y * ( -1.0f + 5.0f * dir->z * dir->z );
2289 out[12] = sqrt(7.0f / D3DX_PI) / 4.0f * dir->z * ( 5.0f * dir->z * dir->z - 3.0f );
2290 out[13] = sqrt(42.0 / D3DX_PI) / 8.0f * dir->x * ( 1.0f - 5.0f * dir->z * dir->z );
2291 out[14] = sqrt(105.0f / D3DX_PI) / 4.0f * dir->z * ( dir->x * dir->x - dir->y * dir->y );
2292 out[15] = -sqrt(70.0f / D3DX_PI) / 8.0f * dir->x * ( dir->x * dir->x - 3.0f * dir->y * dir->y );
2296 out[16] = 0.75f * sqrt(35.0f / D3DX_PI) * dir->x * dir->y * (dir->x * dir->x - dir->y * dir->y );
2297 out[17] = 3.0f * dir->z * out[9];
2298 out[18] = 0.75f * sqrt(5.0f / D3DX_PI) * dir->x * dir->y * ( 7.0f * dir->z * dir->z - 1.0f );
2299 out[19] = 0.375f * sqrt(10.0f / D3DX_PI) * dir->y * dir->z * ( 3.0f - 7.0f * dir->z * dir->z );
2300 out[20] = 3.0f / ( 16.0f * sqrt(D3DX_PI) ) * ( 35.0f * dir->z * dir->z * dir->z * dir->z - 30.f * dir->z * dir->z + 3.0f );
2301 out[21] = 0.375f * sqrt(10.0f / D3DX_PI) * dir->x * dir->z * ( 3.0f - 7.0f * dir->z * dir->z );
2302 out[22] = 0.375f * sqrt(5.0f / D3DX_PI) * ( dir->x * dir->x - dir->y * dir->y ) * ( 7.0f * dir->z * dir->z - 1.0f);
2303 out[23] = 3.0 * dir->z * out[15];
2304 out[24] = 3.0f / 16.0f * sqrt(35.0f / D3DX_PI) * ( dir->x * dir->x * dir->x * dir->x- 6.0f * dir->x * dir->x * dir->y * dir->y + dir->y * dir->y * dir->y * dir->y );
2308 out[25] = -3.0f/ 32.0f * sqrt(154.0f / D3DX_PI) * dir->y * ( 5.0f * dir->x * dir->x * dir->x * dir->x - 10.0f * dir->x * dir->x * dir->y * dir->y + dir->y * dir->y * dir->y * dir->y );
2309 out[26] = 0.75f * sqrt(385.0f / D3DX_PI) * dir->x * dir->y * dir->z * ( dir->x * dir->x - dir->y * dir->y );
2310 out[27] = sqrt(770.0f / D3DX_PI) / 32.0f * dir->y * ( 3.0f * dir->x * dir->x - dir->y * dir->y ) * ( 1.0f - 9.0f * dir->z * dir->z );
2311 out[28] = sqrt(1155.0f / D3DX_PI) / 4.0f * dir->x * dir->y * dir->z * ( 3.0f * dir->z * dir->z - 1.0f);
2312 out[29] = sqrt(165.0f / D3DX_PI) / 16.0f * dir->y * ( 14.0f * dir->z * dir->z - 21.0f * dir->z * dir->z * dir->z * dir->z - 1.0f );
2313 out[30] = sqrt(11.0f / D3DX_PI) / 16.0f * dir->z * ( 63.0f * dir->z * dir->z * dir->z * dir->z - 70.0f * dir->z * dir->z + 15.0f );
2314 out[31] = sqrt(165.0f / D3DX_PI) / 16.0f * dir->x * ( 14.0f * dir->z * dir->z - 21.0f * dir->z * dir->z * dir->z * dir->z - 1.0f );
2315 out[32] = sqrt(1155.0f / D3DX_PI) / 8.0f * dir->z * ( dir->x * dir->x - dir->y * dir->y ) * ( 3.0f * dir->z * dir->z - 1.0f );
2316 out[33] = sqrt(770.0f / D3DX_PI) / 32.0f * dir->x * ( dir->x * dir->x - 3.0f * dir->y * dir->y ) * ( 1.0f - 9.0f * dir->z * dir->z );
2317 out[34] = 3.0f / 16.0f * sqrt(385.0f / D3DX_PI) * dir->z * ( dir->x * dir->x * dir->x * dir->x - 6.0 * dir->x * dir->x * dir->y * dir->y + dir->y * dir->y * dir->y * dir->y );
2318 out[35] = -3.0f/ 32.0f * sqrt(154.0f / D3DX_PI) * dir->x * ( dir->x * dir->x * dir->x * dir->x - 10.0f * dir->x * dir->x * dir->y * dir->y + 5.0f * dir->y * dir->y * dir->y * dir->y );
2323 HRESULT WINAPI D3DXSHEvalDirectionalLight(UINT order, const D3DXVECTOR3 *dir, FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *Rout, FLOAT *Gout, FLOAT *Bout)
2328 TRACE("Order %u, Vector %p, Red %f, Green %f, Blue %f, Rout %p, Gout %p, Bout %p\n", order, dir, Rintensity, Gintensity, Bintensity, Rout, Gout, Bout);
2337 D3DXSHEvalDirection(Rout, order, dir);
2338 for (j = 0; j < order * order; j++)
2342 Rout[j] = Rintensity * temp;
2344 Gout[j] = Gintensity * temp;
2346 Bout[j] = Bintensity * temp;
2352 FLOAT * WINAPI D3DXSHMultiply2(FLOAT *out, const FLOAT *a, const FLOAT *b)
2356 TRACE("out %p, a %p, b %p\n", out, a, b);
2358 ta = 0.28209479f * a[0];
2359 tb = 0.28209479f * b[0];
2361 out[0] = 0.28209479f * D3DXSHDot(2, a, b);
2362 out[1] = ta * b[1] + tb * a[1];
2363 out[2] = ta * b[2] + tb * a[2];
2364 out[3] = ta * b[3] + tb * a[3];
2369 FLOAT * WINAPI D3DXSHMultiply3(FLOAT *out, const FLOAT *a, const FLOAT *b)
2373 TRACE("out %p, a %p, b %p\n", out, a, b);
2375 out[0] = 0.28209479f * a[0] * b[0];
2377 ta = 0.28209479f * a[0] - 0.12615662f * a[6] - 0.21850968f * a[8];
2378 tb = 0.28209479f * b[0] - 0.12615662f * b[6] - 0.21850968f * b[8];
2379 out[1] = ta * b[1] + tb * a[1];
2381 out[0] += 0.28209479f * t;
2382 out[6] = -0.12615662f * t;
2383 out[8] = -0.21850968f * t;
2385 ta = 0.21850968f * a[5];
2386 tb = 0.21850968f * b[5];
2387 out[1] += ta * b[2] + tb * a[2];
2388 out[2] = ta * b[1] + tb * a[1];
2389 t = a[1] * b[2] +a[2] * b[1];
2390 out[5] = 0.21850968f * t;
2392 ta = 0.21850968f * a[4];
2393 tb = 0.21850968f * b[4];
2394 out[1] += ta * b[3] + tb * a[3];
2395 out[3] = ta * b[1] + tb * a[1];
2396 t = a[1] * b[3] + a[3] * b[1];
2397 out[4] = 0.21850968f * t;
2399 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2400 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2401 out[2] += ta * b[2] + tb * a[2];
2403 out[0] += 0.28209480f * t;
2404 out[6] += 0.25231326f * t;
2406 ta = 0.21850969f * a[7];
2407 tb = 0.21850969f * b[7];
2408 out[2] += ta * b[3] + tb * a[3];
2409 out[3] += ta * b[2] + tb * a[2];
2410 t = a[2] * b[3] + a[3] * b[2];
2411 out[7] = 0.21850969f * t;
2413 ta = 0.28209479f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2414 tb = 0.28209479f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2415 out[3] += ta * b[3] + tb * a[3];
2417 out[0] += 0.28209479f * t;
2418 out[6] -= 0.12615663f * t;
2419 out[8] += 0.21850969f * t;
2421 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2422 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2423 out[4] += ta * b[4] + tb * a[4];
2425 out[0] += 0.28209479f * t;
2426 out[6] -= 0.18022375f * t;
2428 ta = 0.15607835f * a[7];
2429 tb = 0.15607835f * b[7];
2430 out[4] += ta * b[5] + tb * a[5];
2431 out[5] += ta * b[4] + tb * a[4];
2432 t = a[4] * b[5] + a[5] * b[4];
2433 out[7] += 0.15607834f * t;
2435 ta = 0.28209479f * a[0] + 0.09011186 * a[6] - 0.15607835f * a[8];
2436 tb = 0.28209479f * b[0] + 0.09011186 * b[6] - 0.15607835f * b[8];
2437 out[5] += ta * b[5] + tb * a[5];
2439 out[0] += 0.28209479f * t;
2440 out[6] += 0.09011186f * t;
2441 out[8] -= 0.15607835f * t;
2443 ta = 0.28209480f * a[0];
2444 tb = 0.28209480f * b[0];
2445 out[6] += ta * b[6] + tb * a[6];
2447 out[0] += 0.28209480f * t;
2448 out[6] += 0.18022376f * t;
2450 ta = 0.28209479f * a[0] + 0.09011186 * a[6] + 0.15607835f * a[8];
2451 tb = 0.28209479f * b[0] + 0.09011186 * b[6] + 0.15607835f * b[8];
2452 out[7] += ta * b[7] + tb * a[7];
2454 out[0] += 0.28209479f * t;
2455 out[6] += 0.09011186f * t;
2456 out[8] += 0.15607835f * t;
2458 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2459 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2460 out[8] += ta * b[8] + tb * a[8];
2462 out[0] += 0.28209479f * t;
2463 out[6] -= 0.18022375f * t;
2468 FLOAT * WINAPI D3DXSHMultiply4(FLOAT *out, const FLOAT *a, const FLOAT *b)
2472 TRACE("out %p, a %p, b %p\n", out, a, b);
2474 out[0] = 0.28209479f * a[0] * b[0];
2476 ta = 0.28209479f * a[0] - 0.12615663f * a[6] - 0.21850969f * a[8];
2477 tb = 0.28209479f * b[0] - 0.12615663f * b[6] - 0.21850969f * b[8];
2478 out[1] = ta * b[1] + tb * a[1];
2480 out[0] += 0.28209479f * t;
2481 out[6] = -0.12615663f * t;
2482 out[8] = -0.21850969f * t;
2484 ta = 0.21850969f * a[3] - 0.05839917f * a[13] - 0.22617901f * a[15];
2485 tb = 0.21850969f * b[3] - 0.05839917f * b[13] - 0.22617901f * b[15];
2486 out[1] += ta * b[4] + tb * a[4];
2487 out[4] = ta * b[1] + tb * a[1];
2488 t = a[1] * b[4] + a[4] * b[1];
2489 out[3] = 0.21850969f * t;
2490 out[13] = -0.05839917f * t;
2491 out[15] = -0.22617901f * t;
2493 ta = 0.21850969f * a[2] - 0.14304817f * a[12] - 0.18467439f * a[14];
2494 tb = 0.21850969f * b[2] - 0.14304817f * b[12] - 0.18467439f * b[14];
2495 out[1] += ta * b[5] + tb * a[5];
2496 out[5] = ta * b[1] + tb * a[1];
2497 t = a[1] * b[5] + a[5] * b[1];
2498 out[2] = 0.21850969f * t;
2499 out[12] = -0.14304817f * t;
2500 out[14] = -0.18467439f * t;
2502 ta = 0.20230066f * a[11];
2503 tb = 0.20230066f * b[11];
2504 out[1] += ta * b[6] + tb * a[6];
2505 out[6] += ta * b[1] + tb * a[1];
2506 t = a[1] * b[6] + a[6] * b[1];
2507 out[11] = 0.20230066f * t;
2509 ta = 0.22617901f * a[9] + 0.05839917f * a[11];
2510 tb = 0.22617901f * b[9] + 0.05839917f * b[11];
2511 out[1] += ta * b[8] + tb * a[8];
2512 out[8] += ta * b[1] + tb * a[1];
2513 t = a[1] * b[8] + a[8] * b[1];
2514 out[9] = 0.22617901f * t;
2515 out[11] += 0.05839917f * t;
2517 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2518 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2519 out[2] += ta * b[2] + tb * a[2];
2521 out[0] += 0.28209480f * t;
2522 out[6] += 0.25231326f * t;
2524 ta = 0.24776671f * a[12];
2525 tb = 0.24776671f * b[12];
2526 out[2] += ta * b[6] + tb * a[6];
2527 out[6] += ta * b[2] + tb * a[2];
2528 t = a[2] * b[6] + a[6] * b[2];
2529 out[12] += 0.24776671f * t;
2531 ta = 0.28209480f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2532 tb = 0.28209480f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2533 out[3] += ta * b[3] + tb * a[3];
2535 out[0] += 0.28209480f * t;
2536 out[6] -= 0.12615663f * t;
2537 out[8] += 0.21850969f * t;
2539 ta = 0.20230066f * a[13];
2540 tb = 0.20230066f * b[13];
2541 out[3] += ta * b[6] + tb * a[6];
2542 out[6] += ta * b[3] + tb * a[3];
2543 t = a[3] * b[6] + a[6] * b[3];
2544 out[13] += 0.20230066f * t;
2546 ta = 0.21850969f * a[2] - 0.14304817f * a[12] + 0.18467439f * a[14];
2547 tb = 0.21850969f * b[2] - 0.14304817f * b[12] + 0.18467439f * b[14];
2548 out[3] += ta * b[7] + tb * a[7];
2549 out[7] = ta * b[3] + tb * a[3];
2550 t = a[3] * b[7] + a[7] * b[3];
2551 out[2] += 0.21850969f * t;
2552 out[12] -= 0.14304817f * t;
2553 out[14] += 0.18467439f * t;
2555 ta = -0.05839917f * a[13] + 0.22617901f * a[15];
2556 tb = -0.05839917f * b[13] + 0.22617901f * b[15];
2557 out[3] += ta * b[8] + tb * a[8];
2558 out[8] += ta * b[3] + tb * a[3];
2559 t = a[3] * b[8] + a[8] * b[3];
2560 out[13] -= 0.05839917f * t;
2561 out[15] += 0.22617901f * t;
2563 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2564 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2565 out[4] += ta * b[4] + tb * a[4];
2567 out[0] += 0.28209479f * t;
2568 out[6] -= 0.18022375f * t;
2570 ta = 0.15607835f * a[7];
2571 tb = 0.15607835f * b[7];
2572 out[4] += ta * b[5] + tb * a[5];
2573 out[5] += ta * b[4] + tb * a[4];
2574 t = a[4] * b[5] + a[5] * b[4];
2575 out[7] += 0.15607835f * t;
2577 ta = 0.22617901f * a[3] - 0.09403160f * a[13];
2578 tb = 0.22617901f * b[3] - 0.09403160f * b[13];
2579 out[4] += ta * b[9] + tb * a[9];
2580 out[9] += ta * b[4] + tb * a[4];
2581 t = a[4] * b[9] + a[9] * b[4];
2582 out[3] += 0.22617901f * t;
2583 out[13] -= 0.09403160f * t;
2585 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2586 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2587 out[4] += ta * b[10] + tb * a [10];
2588 out[10] = ta * b[4] + tb * a[4];
2589 t = a[4] * b[10] + a[10] * b[4];
2590 out[2] += 0.18467439f * t;
2591 out[12] -= 0.18806319f * t;
2593 ta = -0.05839917f * a[3] + 0.14567312f * a[13] + 0.09403160f * a[15];
2594 tb = -0.05839917f * b[3] + 0.14567312f * b[13] + 0.09403160f * b[15];
2595 out[4] += ta * b[11] + tb * a[11];
2596 out[11] += ta * b[4] + tb * a[4];
2597 t = a[4] * b[11] + a[11] * b[4];
2598 out[3] -= 0.05839917f * t;
2599 out[13] += 0.14567312f * t;
2600 out[15] += 0.09403160f * t;
2602 ta = 0.28209479f * a[0] + 0.09011186f * a[6] - 0.15607835f * a[8];
2603 tb = 0.28209479f * b[0] + 0.09011186f * b[6] - 0.15607835f * b[8];
2604 out[5] += ta * b[5] + tb * a[5];
2606 out[0] += 0.28209479f * t;
2607 out[6] += 0.09011186f * t;
2608 out[8] -= 0.15607835f * t;
2610 ta = 0.14867701f * a[14];
2611 tb = 0.14867701f * b[14];
2612 out[5] += ta * b[9] + tb * a[9];
2613 out[9] += ta * b[5] + tb * a[5];
2614 t = a[5] * b[9] + a[9] * b[5];
2615 out[14] += 0.14867701f * t;
2617 ta = 0.18467439f * a[3] + 0.11516472f * a[13] - 0.14867701f * a[15];
2618 tb = 0.18467439f * b[3] + 0.11516472f * b[13] - 0.14867701f * b[15];
2619 out[5] += ta * b[10] + tb * a[10];
2620 out[10] += ta * b[5] + tb * a[5];
2621 t = a[5] * b[10] + a[10] * b[5];
2622 out[3] += 0.18467439f * t;
2623 out[13] += 0.11516472f * t;
2624 out[15] -= 0.14867701f * t;
2626 ta = 0.23359668f * a[2] + 0.05947080f * a[12] - 0.11516472f * a[14];
2627 tb = 0.23359668f * b[2] + 0.05947080f * b[12] - 0.11516472f * b[14];
2628 out[5] += ta * b[11] + tb * a[11];
2629 out[11] += ta * b[5] + tb * a[5];
2630 t = a[5] * b[11] + a[11] * b[5];
2631 out[2] += 0.23359668f * t;
2632 out[12] += 0.05947080f * t;
2633 out[14] -= 0.11516472f * t;
2635 ta = 0.28209479f * a[0];
2636 tb = 0.28209479f * b[0];
2637 out[6] += ta * b[6] + tb * a[6];
2639 out[0] += 0.28209479f * t;
2640 out[6] += 0.18022376f * t;
2642 ta = 0.09011186f * a[6] + 0.28209479f * a[0] + 0.15607835f * a[8];
2643 tb = 0.09011186f * b[6] + 0.28209479f * b[0] + 0.15607835f * b[8];
2644 out[7] += ta * b[7] + tb * a[7];
2646 out[6] += 0.09011186f * t;
2647 out[0] += 0.28209479f * t;
2648 out[8] += 0.15607835f * t;
2650 ta = 0.14867701f * a[9] + 0.18467439f * a[1] + 0.11516472f * a[11];
2651 tb = 0.14867701f * b[9] + 0.18467439f * b[1] + 0.11516472f * b[11];
2652 out[7] += ta * b[10] + tb * a[10];
2653 out[10] += ta * b[7] + tb * a[7];
2654 t = a[7] * b[10] + a[10] * b[7];
2655 out[9] += 0.14867701f * t;
2656 out[1] += 0.18467439f * t;
2657 out[11] += 0.11516472f * t;
2659 ta = 0.05947080f * a[12] + 0.23359668f * a[2] + 0.11516472f * a[14];
2660 tb = 0.05947080f * b[12] + 0.23359668f * b[2] + 0.11516472f * b[14];
2661 out[7] += ta * b[13] + tb * a[13];
2662 out[13] += ta * b[7]+ tb * a[7];
2663 t = a[7] * b[13] + a[13] * b[7];
2664 out[12] += 0.05947080f * t;
2665 out[2] += 0.23359668f * t;
2666 out[14] += 0.11516472f * t;
2668 ta = 0.14867701f * a[15];
2669 tb = 0.14867701f * b[15];
2670 out[7] += ta * b[14] + tb * a[14];
2671 out[14] += ta * b[7] + tb * a[7];
2672 t = a[7] * b[14] + a[14] * b[7];
2673 out[15] += 0.14867701f * t;
2675 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2676 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2677 out[8] += ta * b[8] + tb * a[8];
2679 out[0] += 0.28209479f * t;
2680 out[6] -= 0.18022375f * t;
2682 ta = -0.09403160f * a[11];
2683 tb = -0.09403160f * b[11];
2684 out[8] += ta * b[9] + tb * a[9];
2685 out[9] += ta * b[8] + tb * a[8];
2686 t = a[8] * b[9] + a[9] * b[8];
2687 out[11] -= 0.09403160f * t;
2689 ta = -0.09403160f * a[15];
2690 tb = -0.09403160f * b[15];
2691 out[8] += ta * b[13] + tb * a[13];
2692 out[13] += ta * b[8] + tb * a[8];
2693 t = a[8] * b[13] + a[13] * b[8];
2694 out[15] -= 0.09403160f * t;
2696 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2697 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2698 out[8] += ta * b[14] + tb * a[14];
2699 out[14] += ta * b[8] + tb * a[8];
2700 t = a[8] * b[14] + a[14] * b[8];
2701 out[2] += 0.18467439f * t;
2702 out[12] -= 0.18806319f * t;
2704 ta = -0.21026104f * a[6] + 0.28209479f * a[0];
2705 tb = -0.21026104f * b[6] + 0.28209479f * b[0];
2706 out[9] += ta * b[9] + tb * a[9];
2708 out[6] -= 0.21026104f * t;
2709 out[0] += 0.28209479f * t;
2711 ta = 0.28209479f * a[0];
2712 tb = 0.28209479f * b[0];
2713 out[10] += ta * b[10] + tb * a[10];
2715 out[0] += 0.28209479f * t;
2717 ta = 0.28209479f * a[0] + 0.12615663f * a[6] - 0.14567312f * a[8];
2718 tb = 0.28209479f * b[0] + 0.12615663f * b[6] - 0.14567312f * b[8];
2719 out[11] += ta * b[11] + tb * a[11];
2721 out[0] += 0.28209479f * t;
2722 out[6] += 0.12615663f * t;
2723 out[8] -= 0.14567312f * t;
2725 ta = 0.28209479f * a[0] + 0.16820885f * a[6];
2726 tb = 0.28209479f * b[0] + 0.16820885f * b[6];
2727 out[12] += ta * b[12] + tb * a[12];
2729 out[0] += 0.28209479f * t;
2730 out[6] += 0.16820885f * t;
2732 ta =0.28209479f * a[0] + 0.14567312f * a[8] + 0.12615663f * a[6];
2733 tb =0.28209479f * b[0] + 0.14567312f * b[8] + 0.12615663f * b[6];
2734 out[13] += ta * b[13] + tb * a[13];
2736 out[0] += 0.28209479f * t;
2737 out[8] += 0.14567312f * t;
2738 out[6] += 0.12615663f * t;
2740 ta = 0.28209479f * a[0];
2741 tb = 0.28209479f * b[0];
2742 out[14] += ta * b[14] + tb * a[14];
2744 out[0] += 0.28209479f * t;
2746 ta = 0.28209479f * a[0] - 0.21026104f * a[6];
2747 tb = 0.28209479f * b[0] - 0.21026104f * b[6];
2748 out[15] += ta * b[15] + tb * a[15];
2750 out[0] += 0.28209479f * t;
2751 out[6] -= 0.21026104f * t;
2756 static void rotate_X(FLOAT *out, UINT order, FLOAT a, FLOAT *in)
2761 out[2] = -a * in[1];
2766 out[6] = -0.5f * in[6] - 0.8660253882f * in[8];
2767 out[7] = -a * in[4];
2768 out[8] = -0.8660253882f * in[6] + 0.5f * in[8];
2769 out[9] = -a * 0.7905694842f * in[12] + a * 0.6123724580f * in[14];
2772 out[11] = -a * 0.6123724580f * in[12] - a * 0.7905694842f * in[14];
2773 out[12] = a * 0.7905694842f * in[9] + a * 0.6123724580f * in[11];
2774 out[13] = -0.25f * in[13] - 0.9682458639f * in[15];
2775 out[14] = -a * 0.6123724580f * in[9] + a * 0.7905694842f * in[11];
2776 out[15] = -0.9682458639f * in[13] + 0.25f * in[15];
2780 out[16] = -a * 0.9354143739f * in[21] + a * 0.3535533845f * in[23];
2781 out[17] = -0.75f * in[17] + 0.6614378095f * in[19];
2782 out[18] = -a * 0.3535533845f * in[21] - a * 0.9354143739f * in[23];
2783 out[19] = 0.6614378095f * in[17] + 0.75f * in[19];
2784 out[20] = 0.375f * in[20] + 0.5590170026f * in[22] + 0.7395099998f * in[24];
2785 out[21] = a * 0.9354143739f * in[16] + a * 0.3535533845f * in[18];
2786 out[22] = 0.5590170026f * in[20] + 0.5f * in[22] - 0.6614378691f * in[24];
2787 out[23] = -a * 0.3535533845f * in[16] + a * 0.9354143739f * in[18];
2788 out[24] = 0.7395099998f * in[20] - 0.6614378691f * in[22] + 0.125f * in[24];
2792 out[25] = a * 0.7015607357f * in[30] - a * 0.6846531630f * in[32] + a * 0.1976423711f * in[34];
2793 out[26] = -0.5f * in[26] + 0.8660253882f * in[28];
2794 out[27] = a * 0.5229125023f * in[30] + a * 0.3061861992f * in[32] - a * 0.7954951525 * in[34];
2795 out[28] = 0.8660253882f * in[26] + 0.5f * in[28];
2796 out[29] = a * 0.4841229022f * in[30] + a * 0.6614378691f * in[32] + a * 0.5728219748f * in[34];
2797 out[30] = -a * 0.7015607357f * in[25] - a * 0.5229125023f * in[27] - a * 0.4841229022f * in[29];
2798 out[31] = 0.125f * in[31] + 0.4050463140f * in[33] + 0.9057110548f * in[35];
2799 out[32] = a * 0.6846531630f * in[25] - a * 0.3061861992f * in[27] - a * 0.6614378691f * in[29];
2800 out[33] = 0.4050463140f * in[31] + 0.8125f * in[33] - 0.4192627370f * in[35];
2801 out[34] = -a * 0.1976423711f * in[25] + a * 0.7954951525f * in[27] - a * 0.5728219748f * in[29];
2802 out[35] = 0.9057110548f * in[31] - 0.4192627370f * in[33] + 0.0624999329f * in[35];
2805 FLOAT* WINAPI D3DXSHRotate(FLOAT *out, UINT order, const D3DXMATRIX *matrix, const FLOAT *in)
2807 FLOAT alpha, beta, gamma, sinb, temp[36], temp1[36];
2809 TRACE("out %p, order %u, matrix %p, in %p\n", out, order, matrix, in);
2813 if ((order > D3DXSH_MAXORDER) || (order < D3DXSH_MINORDER))
2818 out[1] = matrix->u.m[1][1] * in[1] - matrix->u.m[2][1] * in[2] + matrix->u.m[0][1] * in[3];
2819 out[2] = -matrix->u.m[1][2] * in[1] + matrix->u.m[2][2] * in[2] - matrix->u.m[0][2] * in[3];
2820 out[3] = matrix->u.m[1][0] * in[1] - matrix->u.m[2][0] * in[2] + matrix->u.m[0][0] * in[3];
2825 matrix->u.m[1][0] * matrix->u.m[0][0], matrix->u.m[1][1] * matrix->u.m[0][1],
2826 matrix->u.m[1][1] * matrix->u.m[2][1], matrix->u.m[1][0] * matrix->u.m[2][0],
2827 matrix->u.m[2][0] * matrix->u.m[2][0], matrix->u.m[2][1] * matrix->u.m[2][1],
2828 matrix->u.m[0][0] * matrix->u.m[2][0], matrix->u.m[0][1] * matrix->u.m[2][1],
2829 matrix->u.m[0][1] * matrix->u.m[0][1], matrix->u.m[1][0] * matrix->u.m[1][0],
2830 matrix->u.m[1][1] * matrix->u.m[1][1], matrix->u.m[0][0] * matrix->u.m[0][0], };
2832 out[4] = (matrix->u.m[1][1] * matrix->u.m[0][0] + matrix->u.m[0][1] * matrix->u.m[1][0]) * in[4];
2833 out[4] -= (matrix->u.m[1][0] * matrix->u.m[2][1] + matrix->u.m[1][1] * matrix->u.m[2][0]) * in[5];
2834 out[4] += 1.7320508076f * matrix->u.m[2][0] * matrix->u.m[2][1] * in[6];
2835 out[4] -= (matrix->u.m[0][1] * matrix->u.m[2][0] + matrix->u.m[0][0] * matrix->u.m[2][1]) * in[7];
2836 out[4] += (matrix->u.m[0][0] * matrix->u.m[0][1] - matrix->u.m[1][0] * matrix->u.m[1][1]) * in[8];
2838 out[5] = (matrix->u.m[1][1] * matrix->u.m[2][2] + matrix->u.m[1][2] * matrix->u.m[2][1]) * in[5];
2839 out[5] -= (matrix->u.m[1][1] * matrix->u.m[0][2] + matrix->u.m[1][2] * matrix->u.m[0][1]) * in[4];
2840 out[5] -= 1.7320508076f * matrix->u.m[2][2] * matrix->u.m[2][1] * in[6];
2841 out[5] += (matrix->u.m[0][2] * matrix->u.m[2][1] + matrix->u.m[0][1] * matrix->u.m[2][2]) * in[7];
2842 out[5] -= (matrix->u.m[0][1] * matrix->u.m[0][2] - matrix->u.m[1][1] * matrix->u.m[1][2]) * in[8];
2844 out[6] = (matrix->u.m[2][2] * matrix->u.m[2][2] - 0.5f * (coeff[4] + coeff[5])) * in[6];
2845 out[6] -= (0.5773502692f * (coeff[0] + coeff[1]) - 1.1547005384f * matrix->u.m[1][2] * matrix->u.m[0][2]) * in[4];
2846 out[6] += (0.5773502692f * (coeff[2] + coeff[3]) - 1.1547005384f * matrix->u.m[1][2] * matrix->u.m[2][2]) * in[5];
2847 out[6] += (0.5773502692f * (coeff[6] + coeff[7]) - 1.1547005384f * matrix->u.m[0][2] * matrix->u.m[2][2]) * in[7];
2848 out[6] += (0.2886751347f * (coeff[9] - coeff[8] + coeff[10] - coeff[11]) - 0.5773502692f *
2849 (matrix->u.m[1][2] * matrix->u.m[1][2] - matrix->u.m[0][2] * matrix->u.m[0][2])) * in[8];
2851 out[7] = (matrix->u.m[0][0] * matrix->u.m[2][2] + matrix->u.m[0][2] * matrix->u.m[2][0]) * in[7];
2852 out[7] -= (matrix->u.m[1][0] * matrix->u.m[0][2] + matrix->u.m[1][2] * matrix->u.m[0][0]) * in[4];
2853 out[7] += (matrix->u.m[1][0] * matrix->u.m[2][2] + matrix->u.m[1][2] * matrix->u.m[2][0]) * in[5];
2854 out[7] -= 1.7320508076f * matrix->u.m[2][2] * matrix->u.m[2][0] * in[6];
2855 out[7] -= (matrix->u.m[0][0] * matrix->u.m[0][2] - matrix->u.m[1][0] * matrix->u.m[1][2]) * in[8];
2857 out[8] = 0.5f * (coeff[11] - coeff[8] - coeff[9] + coeff[10]) * in[8];
2858 out[8] += (coeff[0] - coeff[1]) * in[4];
2859 out[8] += (coeff[2] - coeff[3]) * in[5];
2860 out[8] += 0.86602540f * (coeff[4] - coeff[5]) * in[6];
2861 out[8] += (coeff[7] - coeff[6]) * in[7];
2867 if (fabsf(matrix->u.m[2][2]) != 1.0f)
2869 sinb = sqrtf(1.0f - matrix->u.m[2][2] * matrix->u.m[2][2]);
2870 alpha = atan2f(matrix->u.m[2][1] / sinb, matrix->u.m[2][0] / sinb);
2871 beta = atan2f(sinb, matrix->u.m[2][2]);
2872 gamma = atan2f(matrix->u.m[1][2] / sinb, -matrix->u.m[0][2] / sinb);
2876 alpha = atan2f(matrix->u.m[0][1], matrix->u.m[0][0]);
2881 D3DXSHRotateZ(temp, order, gamma, in);
2882 rotate_X(temp1, order, 1.0f, temp);
2883 D3DXSHRotateZ(temp, order, beta, temp1);
2884 rotate_X(temp1, order, -1.0f, temp);
2885 D3DXSHRotateZ(out, order, alpha, temp1);
2890 FLOAT * WINAPI D3DXSHRotateZ(FLOAT *out, UINT order, FLOAT angle, const FLOAT *in)
2895 TRACE("out %p, order %u, angle %f, in %p\n", out, order, angle, in);
2897 order = min(max(order, D3DXSH_MINORDER), D3DXSH_MAXORDER);
2901 for (i = 1; i < order; i++)
2905 c[i - 1] = cosf(i * angle);
2906 s[i - 1] = sinf(i * angle);
2909 out[sum - i] = c[i - 1] * in[sum - i];
2910 out[sum - i] += s[i - 1] * in[sum + i];
2911 for (j = i - 1; j > 0; j--)
2913 out[sum - j] = 0.0f;
2914 out[sum - j] = c[j - 1] * in[sum - j];
2915 out[sum - j] += s[j - 1] * in[sum + j];
2923 for (j = 1; j < i; j++)
2925 out[sum + j] = 0.0f;
2926 out[sum + j] = -s[j - 1] * in[sum - j];
2927 out[sum + j] += c[j - 1] * in[sum + j];
2929 out[sum + i] = -s[i - 1] * in[sum - i];
2930 out[sum + i] += c[i - 1] * in[sum + i];
2936 FLOAT* WINAPI D3DXSHScale(FLOAT *out, UINT order, const FLOAT *a, const FLOAT scale)
2940 TRACE("out %p, order %u, a %p, scale %f\n", out, order, a, scale);
2942 for (i = 0; i < order * order; i++)
2943 out[i] = a[i] * scale;