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)
260 TRACE("pm %p\n", pm);
262 t[0] = pm->u.m[2][2] * pm->u.m[3][3] - pm->u.m[2][3] * pm->u.m[3][2];
263 t[1] = pm->u.m[1][2] * pm->u.m[3][3] - pm->u.m[1][3] * pm->u.m[3][2];
264 t[2] = pm->u.m[1][2] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][2];
265 v[0] = pm->u.m[1][1] * t[0] - pm->u.m[2][1] * t[1] + pm->u.m[3][1] * t[2];
266 v[1] = -pm->u.m[1][0] * t[0] + pm->u.m[2][0] * t[1] - pm->u.m[3][0] * t[2];
268 t[0] = pm->u.m[1][0] * pm->u.m[2][1] - pm->u.m[2][0] * pm->u.m[1][1];
269 t[1] = pm->u.m[1][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[1][1];
270 t[2] = pm->u.m[2][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[2][1];
271 v[2] = pm->u.m[3][3] * t[0] - pm->u.m[2][3] * t[1] + pm->u.m[1][3] * t[2];
272 v[3] = -pm->u.m[3][2] * t[0] + pm->u.m[2][2] * t[1] - pm->u.m[1][2] * t[2];
274 return pm->u.m[0][0] * v[0] + pm->u.m[0][1] * v[1] +
275 pm->u.m[0][2] * v[2] + pm->u.m[0][3] * v[3];
278 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, const D3DXMATRIX *pm)
280 FLOAT det, t[3], v[16];
283 TRACE("pout %p, pdeterminant %p, pm %p\n", pout, pdeterminant, pm);
285 t[0] = pm->u.m[2][2] * pm->u.m[3][3] - pm->u.m[2][3] * pm->u.m[3][2];
286 t[1] = pm->u.m[1][2] * pm->u.m[3][3] - pm->u.m[1][3] * pm->u.m[3][2];
287 t[2] = pm->u.m[1][2] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][2];
288 v[0] = pm->u.m[1][1] * t[0] - pm->u.m[2][1] * t[1] + pm->u.m[3][1] * t[2];
289 v[4] = -pm->u.m[1][0] * t[0] + pm->u.m[2][0] * t[1] - pm->u.m[3][0] * t[2];
291 t[0] = pm->u.m[1][0] * pm->u.m[2][1] - pm->u.m[2][0] * pm->u.m[1][1];
292 t[1] = pm->u.m[1][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[1][1];
293 t[2] = pm->u.m[2][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[2][1];
294 v[8] = pm->u.m[3][3] * t[0] - pm->u.m[2][3] * t[1] + pm->u.m[1][3] * t[2];
295 v[12] = -pm->u.m[3][2] * t[0] + pm->u.m[2][2] * t[1] - pm->u.m[1][2] * t[2];
297 det = pm->u.m[0][0] * v[0] + pm->u.m[0][1] * v[4] +
298 pm->u.m[0][2] * v[8] + pm->u.m[0][3] * v[12];
304 t[0] = pm->u.m[2][2] * pm->u.m[3][3] - pm->u.m[2][3] * pm->u.m[3][2];
305 t[1] = pm->u.m[0][2] * pm->u.m[3][3] - pm->u.m[0][3] * pm->u.m[3][2];
306 t[2] = pm->u.m[0][2] * pm->u.m[2][3] - pm->u.m[0][3] * pm->u.m[2][2];
307 v[1] = -pm->u.m[0][1] * t[0] + pm->u.m[2][1] * t[1] - pm->u.m[3][1] * t[2];
308 v[5] = pm->u.m[0][0] * t[0] - pm->u.m[2][0] * t[1] + pm->u.m[3][0] * t[2];
310 t[0] = pm->u.m[0][0] * pm->u.m[2][1] - pm->u.m[2][0] * pm->u.m[0][1];
311 t[1] = pm->u.m[3][0] * pm->u.m[0][1] - pm->u.m[0][0] * pm->u.m[3][1];
312 t[2] = pm->u.m[2][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[2][1];
313 v[9] = -pm->u.m[3][3] * t[0] - pm->u.m[2][3] * t[1]- pm->u.m[0][3] * t[2];
314 v[13] = pm->u.m[3][2] * t[0] + pm->u.m[2][2] * t[1] + pm->u.m[0][2] * t[2];
316 t[0] = pm->u.m[1][2] * pm->u.m[3][3] - pm->u.m[1][3] * pm->u.m[3][2];
317 t[1] = pm->u.m[0][2] * pm->u.m[3][3] - pm->u.m[0][3] * pm->u.m[3][2];
318 t[2] = pm->u.m[0][2] * pm->u.m[1][3] - pm->u.m[0][3] * pm->u.m[1][2];
319 v[2] = pm->u.m[0][1] * t[0] - pm->u.m[1][1] * t[1] + pm->u.m[3][1] * t[2];
320 v[6] = -pm->u.m[0][0] * t[0] + pm->u.m[1][0] * t[1] - pm->u.m[3][0] * t[2];
322 t[0] = pm->u.m[0][0] * pm->u.m[1][1] - pm->u.m[1][0] * pm->u.m[0][1];
323 t[1] = pm->u.m[3][0] * pm->u.m[0][1] - pm->u.m[0][0] * pm->u.m[3][1];
324 t[2] = pm->u.m[1][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[1][1];
325 v[10] = pm->u.m[3][3] * t[0] + pm->u.m[1][3] * t[1] + pm->u.m[0][3] * t[2];
326 v[14] = -pm->u.m[3][2] * t[0] - pm->u.m[1][2] * t[1] - pm->u.m[0][2] * t[2];
328 t[0] = pm->u.m[1][2] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][2];
329 t[1] = pm->u.m[0][2] * pm->u.m[2][3] - pm->u.m[0][3] * pm->u.m[2][2];
330 t[2] = pm->u.m[0][2] * pm->u.m[1][3] - pm->u.m[0][3] * pm->u.m[1][2];
331 v[3] = -pm->u.m[0][1] * t[0] + pm->u.m[1][1] * t[1] - pm->u.m[2][1] * t[2];
332 v[7] = pm->u.m[0][0] * t[0] - pm->u.m[1][0] * t[1] + pm->u.m[2][0] * t[2];
334 v[11] = -pm->u.m[0][0] * (pm->u.m[1][1] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][1]) +
335 pm->u.m[1][0] * (pm->u.m[0][1] * pm->u.m[2][3] - pm->u.m[0][3] * pm->u.m[2][1]) -
336 pm->u.m[2][0] * (pm->u.m[0][1] * pm->u.m[1][3] - pm->u.m[0][3] * pm->u.m[1][1]);
338 v[15] = pm->u.m[0][0] * (pm->u.m[1][1] * pm->u.m[2][2] - pm->u.m[1][2] * pm->u.m[2][1]) -
339 pm->u.m[1][0] * (pm->u.m[0][1] * pm->u.m[2][2] - pm->u.m[0][2] * pm->u.m[2][1]) +
340 pm->u.m[2][0] * (pm->u.m[0][1] * pm->u.m[1][2] - pm->u.m[0][2] * pm->u.m[1][1]);
344 for (i = 0; i < 4; i++)
345 for (j = 0; j < 4; j++)
346 pout->u.m[i][j] = v[4 * i + j] * det;
351 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, const D3DXVECTOR3 *peye, const D3DXVECTOR3 *pat, const D3DXVECTOR3 *pup)
353 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
355 TRACE("pout %p, peye %p, pat %p, pup %p\n", pout, peye, pat, pup);
357 D3DXVec3Subtract(&vec2, pat, peye);
358 D3DXVec3Normalize(&vec, &vec2);
359 D3DXVec3Cross(&right, pup, &vec);
360 D3DXVec3Cross(&up, &vec, &right);
361 D3DXVec3Normalize(&rightn, &right);
362 D3DXVec3Normalize(&upn, &up);
363 pout->u.m[0][0] = rightn.x;
364 pout->u.m[1][0] = rightn.y;
365 pout->u.m[2][0] = rightn.z;
366 pout->u.m[3][0] = -D3DXVec3Dot(&rightn,peye);
367 pout->u.m[0][1] = upn.x;
368 pout->u.m[1][1] = upn.y;
369 pout->u.m[2][1] = upn.z;
370 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
371 pout->u.m[0][2] = vec.x;
372 pout->u.m[1][2] = vec.y;
373 pout->u.m[2][2] = vec.z;
374 pout->u.m[3][2] = -D3DXVec3Dot(&vec, peye);
375 pout->u.m[0][3] = 0.0f;
376 pout->u.m[1][3] = 0.0f;
377 pout->u.m[2][3] = 0.0f;
378 pout->u.m[3][3] = 1.0f;
382 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, const D3DXVECTOR3 *peye, const D3DXVECTOR3 *pat, const D3DXVECTOR3 *pup)
384 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
386 TRACE("pout %p, peye %p, pat %p, pup %p\n", pout, peye, pat, pup);
388 D3DXVec3Subtract(&vec2, pat, peye);
389 D3DXVec3Normalize(&vec, &vec2);
390 D3DXVec3Cross(&right, pup, &vec);
391 D3DXVec3Cross(&up, &vec, &right);
392 D3DXVec3Normalize(&rightn, &right);
393 D3DXVec3Normalize(&upn, &up);
394 pout->u.m[0][0] = -rightn.x;
395 pout->u.m[1][0] = -rightn.y;
396 pout->u.m[2][0] = -rightn.z;
397 pout->u.m[3][0] = D3DXVec3Dot(&rightn,peye);
398 pout->u.m[0][1] = upn.x;
399 pout->u.m[1][1] = upn.y;
400 pout->u.m[2][1] = upn.z;
401 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
402 pout->u.m[0][2] = -vec.x;
403 pout->u.m[1][2] = -vec.y;
404 pout->u.m[2][2] = -vec.z;
405 pout->u.m[3][2] = D3DXVec3Dot(&vec, peye);
406 pout->u.m[0][3] = 0.0f;
407 pout->u.m[1][3] = 0.0f;
408 pout->u.m[2][3] = 0.0f;
409 pout->u.m[3][3] = 1.0f;
413 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, const D3DXMATRIX *pm1, const D3DXMATRIX *pm2)
418 TRACE("pout %p, pm1 %p, pm2 %p\n", pout, pm1, pm2);
424 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];
432 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, const D3DXMATRIX *pm1, const D3DXMATRIX *pm2)
437 TRACE("pout %p, pm1 %p, pm2 %p\n", pout, pm1, pm2);
439 for (i = 0; i < 4; i++)
440 for (j = 0; j < 4; j++)
441 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];
447 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
449 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
451 D3DXMatrixIdentity(pout);
452 pout->u.m[0][0] = 2.0f / w;
453 pout->u.m[1][1] = 2.0f / h;
454 pout->u.m[2][2] = 1.0f / (zf - zn);
455 pout->u.m[3][2] = zn / (zn - zf);
459 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
461 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
463 D3DXMatrixIdentity(pout);
464 pout->u.m[0][0] = 2.0f / (r - l);
465 pout->u.m[1][1] = 2.0f / (t - b);
466 pout->u.m[2][2] = 1.0f / (zf -zn);
467 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
468 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
469 pout->u.m[3][2] = zn / (zn -zf);
473 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
475 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
477 D3DXMatrixIdentity(pout);
478 pout->u.m[0][0] = 2.0f / (r - l);
479 pout->u.m[1][1] = 2.0f / (t - b);
480 pout->u.m[2][2] = 1.0f / (zn -zf);
481 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
482 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
483 pout->u.m[3][2] = zn / (zn -zf);
487 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
489 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
491 D3DXMatrixIdentity(pout);
492 pout->u.m[0][0] = 2.0f / w;
493 pout->u.m[1][1] = 2.0f / h;
494 pout->u.m[2][2] = 1.0f / (zn - zf);
495 pout->u.m[3][2] = zn / (zn - zf);
499 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
501 TRACE("pout %p, fovy %f, aspect %f, zn %f, zf %f\n", pout, fovy, aspect, zn, zf);
503 D3DXMatrixIdentity(pout);
504 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
505 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
506 pout->u.m[2][2] = zf / (zf - zn);
507 pout->u.m[2][3] = 1.0f;
508 pout->u.m[3][2] = (zf * zn) / (zn - zf);
509 pout->u.m[3][3] = 0.0f;
513 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
515 TRACE("pout %p, fovy %f, aspect %f, zn %f, zf %f\n", pout, fovy, aspect, zn, zf);
517 D3DXMatrixIdentity(pout);
518 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
519 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
520 pout->u.m[2][2] = zf / (zn - zf);
521 pout->u.m[2][3] = -1.0f;
522 pout->u.m[3][2] = (zf * zn) / (zn - zf);
523 pout->u.m[3][3] = 0.0f;
527 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
529 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
531 D3DXMatrixIdentity(pout);
532 pout->u.m[0][0] = 2.0f * zn / w;
533 pout->u.m[1][1] = 2.0f * zn / h;
534 pout->u.m[2][2] = zf / (zf - zn);
535 pout->u.m[3][2] = (zn * zf) / (zn - zf);
536 pout->u.m[2][3] = 1.0f;
537 pout->u.m[3][3] = 0.0f;
541 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
543 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
545 D3DXMatrixIdentity(pout);
546 pout->u.m[0][0] = 2.0f * zn / (r - l);
547 pout->u.m[1][1] = -2.0f * zn / (b - t);
548 pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
549 pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
550 pout->u.m[2][2] = - zf / (zn - zf);
551 pout->u.m[3][2] = (zn * zf) / (zn -zf);
552 pout->u.m[2][3] = 1.0f;
553 pout->u.m[3][3] = 0.0f;
557 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
559 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
561 D3DXMatrixIdentity(pout);
562 pout->u.m[0][0] = 2.0f * zn / (r - l);
563 pout->u.m[1][1] = -2.0f * zn / (b - t);
564 pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
565 pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
566 pout->u.m[2][2] = zf / (zn - zf);
567 pout->u.m[3][2] = (zn * zf) / (zn -zf);
568 pout->u.m[2][3] = -1.0f;
569 pout->u.m[3][3] = 0.0f;
573 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
575 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
577 D3DXMatrixIdentity(pout);
578 pout->u.m[0][0] = 2.0f * zn / w;
579 pout->u.m[1][1] = 2.0f * zn / h;
580 pout->u.m[2][2] = zf / (zn - zf);
581 pout->u.m[3][2] = (zn * zf) / (zn - zf);
582 pout->u.m[2][3] = -1.0f;
583 pout->u.m[3][3] = 0.0f;
587 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, const D3DXPLANE *pplane)
591 TRACE("pout %p, pplane %p\n", pout, pplane);
593 D3DXPlaneNormalize(&Nplane, pplane);
594 D3DXMatrixIdentity(pout);
595 pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
596 pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
597 pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
598 pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
599 pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
600 pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
601 pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
602 pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
603 pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
604 pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
605 pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
606 pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
610 D3DXMATRIX * WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *out, const D3DXVECTOR3 *v, FLOAT angle)
613 FLOAT sangle, cangle, cdiff;
615 TRACE("out %p, v %p, angle %f\n", out, v, angle);
617 D3DXVec3Normalize(&nv, v);
618 sangle = sinf(angle);
619 cangle = cosf(angle);
620 cdiff = 1.0f - cangle;
622 out->u.m[0][0] = cdiff * nv.x * nv.x + cangle;
623 out->u.m[1][0] = cdiff * nv.x * nv.y - sangle * nv.z;
624 out->u.m[2][0] = cdiff * nv.x * nv.z + sangle * nv.y;
625 out->u.m[3][0] = 0.0f;
626 out->u.m[0][1] = cdiff * nv.y * nv.x + sangle * nv.z;
627 out->u.m[1][1] = cdiff * nv.y * nv.y + cangle;
628 out->u.m[2][1] = cdiff * nv.y * nv.z - sangle * nv.x;
629 out->u.m[3][1] = 0.0f;
630 out->u.m[0][2] = cdiff * nv.z * nv.x - sangle * nv.y;
631 out->u.m[1][2] = cdiff * nv.z * nv.y + sangle * nv.x;
632 out->u.m[2][2] = cdiff * nv.z * nv.z + cangle;
633 out->u.m[3][2] = 0.0f;
634 out->u.m[0][3] = 0.0f;
635 out->u.m[1][3] = 0.0f;
636 out->u.m[2][3] = 0.0f;
637 out->u.m[3][3] = 1.0f;
642 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, const D3DXQUATERNION *pq)
644 TRACE("pout %p, pq %p\n", pout, pq);
646 D3DXMatrixIdentity(pout);
647 pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
648 pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
649 pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
650 pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
651 pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
652 pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
653 pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
654 pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
655 pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
659 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
661 TRACE("pout %p, angle %f\n", pout, angle);
663 D3DXMatrixIdentity(pout);
664 pout->u.m[1][1] = cos(angle);
665 pout->u.m[2][2] = cos(angle);
666 pout->u.m[1][2] = sin(angle);
667 pout->u.m[2][1] = -sin(angle);
671 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
673 TRACE("pout %p, angle %f\n", pout, angle);
675 D3DXMatrixIdentity(pout);
676 pout->u.m[0][0] = cos(angle);
677 pout->u.m[2][2] = cos(angle);
678 pout->u.m[0][2] = -sin(angle);
679 pout->u.m[2][0] = sin(angle);
683 D3DXMATRIX * WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *out, FLOAT yaw, FLOAT pitch, FLOAT roll)
685 FLOAT sroll, croll, spitch, cpitch, syaw, cyaw;
687 TRACE("out %p, yaw %f, pitch %f, roll %f\n", out, yaw, pitch, roll);
691 spitch = sinf(pitch);
692 cpitch = cosf(pitch);
696 out->u.m[0][0] = sroll * spitch * syaw + croll * cyaw;
697 out->u.m[0][1] = sroll * cpitch;
698 out->u.m[0][2] = sroll * spitch * cyaw - croll * syaw;
699 out->u.m[0][3] = 0.0f;
700 out->u.m[1][0] = croll * spitch * syaw - sroll * cyaw;
701 out->u.m[1][1] = croll * cpitch;
702 out->u.m[1][2] = croll * spitch * cyaw + sroll * syaw;
703 out->u.m[1][3] = 0.0f;
704 out->u.m[2][0] = cpitch * syaw;
705 out->u.m[2][1] = -spitch;
706 out->u.m[2][2] = cpitch * cyaw;
707 out->u.m[2][3] = 0.0f;
708 out->u.m[3][0] = 0.0f;
709 out->u.m[3][1] = 0.0f;
710 out->u.m[3][2] = 0.0f;
711 out->u.m[3][3] = 1.0f;
716 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
718 TRACE("pout %p, angle %f\n", pout, angle);
720 D3DXMatrixIdentity(pout);
721 pout->u.m[0][0] = cos(angle);
722 pout->u.m[1][1] = cos(angle);
723 pout->u.m[0][1] = sin(angle);
724 pout->u.m[1][0] = -sin(angle);
728 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
730 TRACE("pout %p, sx %f, sy %f, sz %f\n", pout, sx, sy, sz);
732 D3DXMatrixIdentity(pout);
733 pout->u.m[0][0] = sx;
734 pout->u.m[1][1] = sy;
735 pout->u.m[2][2] = sz;
739 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, const D3DXVECTOR4 *plight, const D3DXPLANE *pplane)
744 TRACE("pout %p, plight %p, pplane %p\n", pout, plight, pplane);
746 D3DXPlaneNormalize(&Nplane, pplane);
747 dot = D3DXPlaneDot(&Nplane, plight);
748 pout->u.m[0][0] = dot - Nplane.a * plight->x;
749 pout->u.m[0][1] = -Nplane.a * plight->y;
750 pout->u.m[0][2] = -Nplane.a * plight->z;
751 pout->u.m[0][3] = -Nplane.a * plight->w;
752 pout->u.m[1][0] = -Nplane.b * plight->x;
753 pout->u.m[1][1] = dot - Nplane.b * plight->y;
754 pout->u.m[1][2] = -Nplane.b * plight->z;
755 pout->u.m[1][3] = -Nplane.b * plight->w;
756 pout->u.m[2][0] = -Nplane.c * plight->x;
757 pout->u.m[2][1] = -Nplane.c * plight->y;
758 pout->u.m[2][2] = dot - Nplane.c * plight->z;
759 pout->u.m[2][3] = -Nplane.c * plight->w;
760 pout->u.m[3][0] = -Nplane.d * plight->x;
761 pout->u.m[3][1] = -Nplane.d * plight->y;
762 pout->u.m[3][2] = -Nplane.d * plight->z;
763 pout->u.m[3][3] = dot - Nplane.d * plight->w;
767 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, const D3DXVECTOR3 *pscalingcenter, const D3DXQUATERNION *pscalingrotation, const D3DXVECTOR3 *pscaling, const D3DXVECTOR3 *protationcenter, const D3DXQUATERNION *protation, const D3DXVECTOR3 *ptranslation)
769 D3DXMATRIX m1, m2, m3, m4, m5, m6, m7;
773 TRACE("pout %p, pscalingcenter %p, pscalingrotation %p, pscaling %p, protationcentr %p, protation %p, ptranslation %p\n",
774 pout, pscalingcenter, pscalingrotation, pscaling, protationcenter, protation, ptranslation);
776 if ( !pscalingcenter )
784 psc.x = pscalingcenter->x;
785 psc.y = pscalingcenter->y;
786 psc.z = pscalingcenter->z;
789 if ( !protationcenter )
797 prc.x = protationcenter->x;
798 prc.y = protationcenter->y;
799 prc.z = protationcenter->z;
810 pt.x = ptranslation->x;
811 pt.y = ptranslation->y;
812 pt.z = ptranslation->z;
815 D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
817 if ( !pscalingrotation )
819 D3DXMatrixIdentity(&m2);
820 D3DXMatrixIdentity(&m4);
824 D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
825 D3DXMatrixInverse(&m2, NULL, &m4);
828 if ( !pscaling ) D3DXMatrixIdentity(&m3);
829 else D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
831 if ( !protation ) D3DXMatrixIdentity(&m6);
832 else D3DXMatrixRotationQuaternion(&m6, protation);
834 D3DXMatrixTranslation(&m5, psc.x - prc.x, psc.y - prc.y, psc.z - prc.z);
835 D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
836 D3DXMatrixMultiply(&m1, &m1, &m2);
837 D3DXMatrixMultiply(&m1, &m1, &m3);
838 D3DXMatrixMultiply(&m1, &m1, &m4);
839 D3DXMatrixMultiply(&m1, &m1, &m5);
840 D3DXMatrixMultiply(&m1, &m1, &m6);
841 D3DXMatrixMultiply(pout, &m1, &m7);
845 D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(D3DXMATRIX *pout, const D3DXVECTOR2 *pscalingcenter, FLOAT scalingrotation, const D3DXVECTOR2 *pscaling, const D3DXVECTOR2 *protationcenter, FLOAT rotation, const D3DXVECTOR2 *ptranslation)
847 D3DXQUATERNION rot, sca_rot;
848 D3DXVECTOR3 rot_center, sca, sca_center, trans;
850 TRACE("pout %p, pscalingcenter %p, scalingrotation %f, pscaling %p, protztioncenter %p, rotation %f, ptranslation %p\n",
851 pout, pscalingcenter, scalingrotation, pscaling, protationcenter, rotation, ptranslation);
853 if ( pscalingcenter )
855 sca_center.x=pscalingcenter->x;
856 sca_center.y=pscalingcenter->y;
879 if ( protationcenter )
881 rot_center.x=protationcenter->x;
882 rot_center.y=protationcenter->y;
894 trans.x=ptranslation->x;
895 trans.y=ptranslation->y;
905 rot.w=cos(rotation/2.0f);
908 rot.z=sin(rotation/2.0f);
910 sca_rot.w=cos(scalingrotation/2.0f);
913 sca_rot.z=sin(scalingrotation/2.0f);
915 D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
920 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
922 TRACE("pout %p, x %f, y %f, z %f\n", pout, x, y, z);
924 D3DXMatrixIdentity(pout);
931 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, const D3DXMATRIX *pm)
933 const D3DXMATRIX m = *pm;
936 TRACE("pout %p, pm %p\n", pout, pm);
939 for (j=0; j<4; j++) pout->u.m[i][j] = m.u.m[j][i];
944 /*_________________D3DXMatrixStack____________________*/
946 static const unsigned int INITIAL_STACK_SIZE = 32;
948 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, ID3DXMatrixStack **ppstack)
950 struct ID3DXMatrixStackImpl *object;
952 TRACE("flags %#x, ppstack %p\n", flags, ppstack);
954 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
958 return E_OUTOFMEMORY;
960 object->ID3DXMatrixStack_iface.lpVtbl = &ID3DXMatrixStack_Vtbl;
963 object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(*object->stack));
966 HeapFree(GetProcessHeap(), 0, object);
968 return E_OUTOFMEMORY;
972 object->stack_size = INITIAL_STACK_SIZE;
973 D3DXMatrixIdentity(&object->stack[0]);
975 TRACE("Created matrix stack %p\n", object);
977 *ppstack = &object->ID3DXMatrixStack_iface;
981 static inline struct ID3DXMatrixStackImpl *impl_from_ID3DXMatrixStack(ID3DXMatrixStack *iface)
983 return CONTAINING_RECORD(iface, struct ID3DXMatrixStackImpl, ID3DXMatrixStack_iface);
986 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **out)
988 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
990 if (IsEqualGUID(riid, &IID_ID3DXMatrixStack)
991 || IsEqualGUID(riid, &IID_IUnknown))
993 ID3DXMatrixStack_AddRef(iface);
998 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
1001 return E_NOINTERFACE;
1004 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
1006 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1007 ULONG ref = InterlockedIncrement(&This->ref);
1008 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
1012 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack *iface)
1014 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1015 ULONG ref = InterlockedDecrement(&This->ref);
1018 HeapFree(GetProcessHeap(), 0, This->stack);
1019 HeapFree(GetProcessHeap(), 0, This);
1021 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
1025 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
1027 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1029 TRACE("iface %p\n", iface);
1031 return &This->stack[This->current];
1034 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
1036 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1038 TRACE("iface %p\n", iface);
1040 D3DXMatrixIdentity(&This->stack[This->current]);
1045 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
1047 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1049 TRACE("iface %p, pm %p\n", iface, pm);
1051 This->stack[This->current] = *pm;
1056 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
1058 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1060 TRACE("iface %p, pm %p\n", iface, pm);
1062 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], pm);
1067 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
1069 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1071 TRACE("iface %p, pm %p\n", iface, pm);
1073 D3DXMatrixMultiply(&This->stack[This->current], pm, &This->stack[This->current]);
1078 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
1080 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1082 TRACE("iface %p\n", iface);
1084 /* Popping the last element on the stack returns D3D_OK, but does nothing. */
1085 if (!This->current) return D3D_OK;
1087 if (This->current <= This->stack_size / 4 && This->stack_size >= INITIAL_STACK_SIZE * 2)
1089 unsigned int new_size;
1090 D3DXMATRIX *new_stack;
1092 new_size = This->stack_size / 2;
1093 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1096 This->stack_size = new_size;
1097 This->stack = new_stack;
1106 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
1108 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1110 TRACE("iface %p\n", iface);
1112 if (This->current == This->stack_size - 1)
1114 unsigned int new_size;
1115 D3DXMATRIX *new_stack;
1117 if (This->stack_size > UINT_MAX / 2) return E_OUTOFMEMORY;
1119 new_size = This->stack_size * 2;
1120 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1121 if (!new_stack) return E_OUTOFMEMORY;
1123 This->stack_size = new_size;
1124 This->stack = new_stack;
1128 This->stack[This->current] = This->stack[This->current - 1];
1133 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, const D3DXVECTOR3 *pv, FLOAT angle)
1136 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1138 TRACE("iface %p, pv %p, angle %f\n", iface, pv, angle);
1140 D3DXMatrixRotationAxis(&temp, pv, angle);
1141 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1146 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, const D3DXVECTOR3 *pv, FLOAT angle)
1149 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1151 TRACE("iface %p, pv %p, angle %f\n", iface, pv, angle);
1153 D3DXMatrixRotationAxis(&temp, pv, angle);
1154 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1159 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1162 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1164 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1166 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1167 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1172 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1175 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1177 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1179 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1180 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1185 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1188 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1190 TRACE("iface %p,x %f, y %f, z %f\n", iface, x, y, z);
1192 D3DXMatrixScaling(&temp, x, y, z);
1193 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1198 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1201 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1203 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1205 D3DXMatrixScaling(&temp, x, y, z);
1206 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1211 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1214 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1216 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1218 D3DXMatrixTranslation(&temp, x, y, z);
1219 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1224 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1227 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1229 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1231 D3DXMatrixTranslation(&temp, x, y, z);
1232 D3DXMatrixMultiply(&This->stack[This->current], &temp,&This->stack[This->current]);
1237 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
1239 ID3DXMatrixStackImpl_QueryInterface,
1240 ID3DXMatrixStackImpl_AddRef,
1241 ID3DXMatrixStackImpl_Release,
1242 ID3DXMatrixStackImpl_Pop,
1243 ID3DXMatrixStackImpl_Push,
1244 ID3DXMatrixStackImpl_LoadIdentity,
1245 ID3DXMatrixStackImpl_LoadMatrix,
1246 ID3DXMatrixStackImpl_MultMatrix,
1247 ID3DXMatrixStackImpl_MultMatrixLocal,
1248 ID3DXMatrixStackImpl_RotateAxis,
1249 ID3DXMatrixStackImpl_RotateAxisLocal,
1250 ID3DXMatrixStackImpl_RotateYawPitchRoll,
1251 ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
1252 ID3DXMatrixStackImpl_Scale,
1253 ID3DXMatrixStackImpl_ScaleLocal,
1254 ID3DXMatrixStackImpl_Translate,
1255 ID3DXMatrixStackImpl_TranslateLocal,
1256 ID3DXMatrixStackImpl_GetTop
1259 /*_________________D3DXPLANE________________*/
1261 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, const D3DXVECTOR3 *pvpoint, const D3DXVECTOR3 *pvnormal)
1263 TRACE("pout %p, pvpoint %p, pvnormal %p\n", pout, pvpoint, pvnormal);
1265 pout->a = pvnormal->x;
1266 pout->b = pvnormal->y;
1267 pout->c = pvnormal->z;
1268 pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
1272 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3)
1274 D3DXVECTOR3 edge1, edge2, normal, Nnormal;
1276 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p\n", pout, pv1, pv2, pv3);
1278 edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
1279 edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
1280 D3DXVec3Subtract(&edge1, pv2, pv1);
1281 D3DXVec3Subtract(&edge2, pv3, pv1);
1282 D3DXVec3Cross(&normal, &edge1, &edge2);
1283 D3DXVec3Normalize(&Nnormal, &normal);
1284 D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
1288 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, const D3DXPLANE *pp, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2)
1290 D3DXVECTOR3 direction, normal;
1293 TRACE("pout %p, pp %p, pv1 %p, pv2 %p\n", pout, pp, pv1, pv2);
1298 direction.x = pv2->x - pv1->x;
1299 direction.y = pv2->y - pv1->y;
1300 direction.z = pv2->z - pv1->z;
1301 dot = D3DXVec3Dot(&normal, &direction);
1302 if ( !dot ) return NULL;
1303 temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
1304 pout->x = pv1->x - temp * direction.x;
1305 pout->y = pv1->y - temp * direction.y;
1306 pout->z = pv1->z - temp * direction.z;
1310 D3DXPLANE * WINAPI D3DXPlaneNormalize(D3DXPLANE *out, const D3DXPLANE *p)
1314 TRACE("out %p, p %p\n", out, p);
1316 norm = sqrtf(p->a * p->a + p->b * p->b + p->c * p->c);
1319 out->a = p->a / norm;
1320 out->b = p->b / norm;
1321 out->c = p->c / norm;
1322 out->d = p->d / norm;
1335 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, const D3DXPLANE *pplane, const D3DXMATRIX *pm)
1337 const D3DXPLANE plane = *pplane;
1339 TRACE("pout %p, pplane %p, pm %p\n", pout, pplane, pm);
1341 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;
1342 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;
1343 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;
1344 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;
1348 D3DXPLANE* WINAPI D3DXPlaneTransformArray(D3DXPLANE* out, UINT outstride, const D3DXPLANE* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1352 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1354 for (i = 0; i < elements; ++i) {
1356 (D3DXPLANE*)((char*)out + outstride * i),
1357 (const D3DXPLANE*)((const char*)in + instride * i),
1363 /*_________________D3DXQUATERNION________________*/
1365 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
1367 D3DXQUATERNION temp1, temp2;
1369 TRACE("pout %p, pq1 %p, pq2 %p, pq3 %p, f %f, g %f\n", pout, pq1, pq2, pq3, f, g);
1371 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
1375 D3DXQUATERNION * WINAPI D3DXQuaternionExp(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1379 TRACE("out %p, q %p\n", out, q);
1381 norm = sqrtf(q->x * q->x + q->y * q->y + q->z * q->z);
1384 out->x = sinf(norm) * q->x / norm;
1385 out->y = sinf(norm) * q->y / norm;
1386 out->z = sinf(norm) * q->z / norm;
1387 out->w = cosf(norm);
1400 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, const D3DXQUATERNION *pq)
1405 TRACE("pout %p, pq %p\n", pout, pq);
1407 norm = D3DXQuaternionLengthSq(pq);
1409 out.x = -pq->x / norm;
1410 out.y = -pq->y / norm;
1411 out.z = -pq->z / norm;
1412 out.w = pq->w / norm;
1418 D3DXQUATERNION * WINAPI D3DXQuaternionLn(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1422 TRACE("out %p, q %p\n", out, q);
1424 if ((q->w >= 1.0f) || (q->w == -1.0f))
1427 t = acosf(q->w) / sqrtf(1.0f - q->w * q->w);
1437 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2)
1441 TRACE("pout %p, pq1 %p, pq2 %p\n", pout, pq1, pq2);
1443 out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
1444 out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
1445 out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
1446 out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
1451 D3DXQUATERNION * WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1455 TRACE("out %p, q %p\n", out, q);
1457 norm = D3DXQuaternionLength(q);
1459 out->x = q->x / norm;
1460 out->y = q->y / norm;
1461 out->z = q->z / norm;
1462 out->w = q->w / norm;
1467 D3DXQUATERNION * WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *out, const D3DXVECTOR3 *v, FLOAT angle)
1471 TRACE("out %p, v %p, angle %f\n", out, v, angle);
1473 D3DXVec3Normalize(&temp, v);
1475 out->x = sinf(angle / 2.0f) * temp.x;
1476 out->y = sinf(angle / 2.0f) * temp.y;
1477 out->z = sinf(angle / 2.0f) * temp.z;
1478 out->w = cosf(angle / 2.0f);
1483 D3DXQUATERNION * WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *out, const D3DXMATRIX *m)
1487 TRACE("out %p, m %p\n", out, m);
1489 trace = m->u.m[0][0] + m->u.m[1][1] + m->u.m[2][2] + 1.0f;
1492 s = 2.0f * sqrtf(trace);
1493 out->x = (m->u.m[1][2] - m->u.m[2][1]) / s;
1494 out->y = (m->u.m[2][0] - m->u.m[0][2]) / s;
1495 out->z = (m->u.m[0][1] - m->u.m[1][0]) / s;
1502 for (i = 1; i < 3; i++)
1504 if (m->u.m[i][i] > m->u.m[maxi][maxi])
1511 s = 2.0f * sqrtf(1.0f + m->u.m[0][0] - m->u.m[1][1] - m->u.m[2][2]);
1513 out->y = (m->u.m[0][1] + m->u.m[1][0]) / s;
1514 out->z = (m->u.m[0][2] + m->u.m[2][0]) / s;
1515 out->w = (m->u.m[1][2] - m->u.m[2][1]) / s;
1519 s = 2.0f * sqrtf(1.0f + m->u.m[1][1] - m->u.m[0][0] - m->u.m[2][2]);
1520 out->x = (m->u.m[0][1] + m->u.m[1][0]) / s;
1522 out->z = (m->u.m[1][2] + m->u.m[2][1]) / s;
1523 out->w = (m->u.m[2][0] - m->u.m[0][2]) / s;
1527 s = 2.0f * sqrtf(1.0f + m->u.m[2][2] - m->u.m[0][0] - m->u.m[1][1]);
1528 out->x = (m->u.m[0][2] + m->u.m[2][0]) / s;
1529 out->y = (m->u.m[1][2] + m->u.m[2][1]) / s;
1531 out->w = (m->u.m[0][1] - m->u.m[1][0]) / s;
1539 D3DXQUATERNION * WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *out, FLOAT yaw, FLOAT pitch, FLOAT roll)
1541 FLOAT syaw, cyaw, spitch, cpitch, sroll, croll;
1543 TRACE("out %p, yaw %f, pitch %f, roll %f\n", out, yaw, pitch, roll);
1545 syaw = sinf(yaw / 2.0f);
1546 cyaw = cosf(yaw / 2.0f);
1547 spitch = sinf(pitch / 2.0f);
1548 cpitch = cosf(pitch / 2.0f);
1549 sroll = sinf(roll / 2.0f);
1550 croll = cosf(roll / 2.0f);
1552 out->x = syaw * cpitch * sroll + cyaw * spitch * croll;
1553 out->y = syaw * cpitch * croll - cyaw * spitch * sroll;
1554 out->z = cyaw * cpitch * sroll - syaw * spitch * croll;
1555 out->w = cyaw * cpitch * croll + syaw * spitch * sroll;
1560 D3DXQUATERNION * WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *out, const D3DXQUATERNION *q1,
1561 const D3DXQUATERNION *q2, FLOAT t)
1565 TRACE("out %p, q1 %p, q2 %p, t %f\n", out, q1, q2, t);
1568 dot = D3DXQuaternionDot(q1, q2);
1575 if (1.0f - dot > 0.001f)
1577 FLOAT theta = acosf(dot);
1579 temp = sinf(theta * temp) / sinf(theta);
1580 t = sinf(theta * t) / sinf(theta);
1583 out->x = temp * q1->x + t * q2->x;
1584 out->y = temp * q1->y + t * q2->y;
1585 out->z = temp * q1->z + t * q2->z;
1586 out->w = temp * q1->w + t * q2->w;
1591 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3, const D3DXQUATERNION *pq4, FLOAT t)
1593 D3DXQUATERNION temp1, temp2;
1595 TRACE("pout %p, pq1 %p, pq2 %p, pq3 %p, pq4 %p, t %f\n", pout, pq1, pq2, pq3, pq4, t);
1597 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1601 static D3DXQUATERNION add_diff(const D3DXQUATERNION *q1, const D3DXQUATERNION *q2, const FLOAT add)
1603 D3DXQUATERNION temp;
1605 temp.x = q1->x + add * q2->x;
1606 temp.y = q1->y + add * q2->y;
1607 temp.z = q1->z + add * q2->z;
1608 temp.w = q1->w + add * q2->w;
1613 void WINAPI D3DXQuaternionSquadSetup(D3DXQUATERNION *paout, D3DXQUATERNION *pbout, D3DXQUATERNION *pcout, const D3DXQUATERNION *pq0, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3)
1615 D3DXQUATERNION q, temp1, temp2, temp3, zero;
1617 TRACE("paout %p, pbout %p, pcout %p, pq0 %p, pq1 %p, pq2 %p, pq3 %p\n", paout, pbout, pcout, pq0, pq1, pq2, pq3);
1624 if ( D3DXQuaternionDot(pq0, pq1) < 0.0f )
1625 temp2 = add_diff(&zero, pq0, -1.0f);
1629 if ( D3DXQuaternionDot(pq1, pq2) < 0.0f )
1630 *pcout = add_diff(&zero, pq2, -1.0f);
1634 if ( D3DXQuaternionDot(pcout, pq3) < 0.0f )
1635 temp3 = add_diff(&zero, pq3, -1.0f);
1639 D3DXQuaternionInverse(&temp1, pq1);
1640 D3DXQuaternionMultiply(&temp2, &temp1, &temp2);
1641 D3DXQuaternionLn(&temp2, &temp2);
1642 D3DXQuaternionMultiply(&q, &temp1, pcout);
1643 D3DXQuaternionLn(&q, &q);
1644 temp1 = add_diff(&temp2, &q, 1.0f);
1649 D3DXQuaternionExp(&temp1, &temp1);
1650 D3DXQuaternionMultiply(paout, pq1, &temp1);
1652 D3DXQuaternionInverse(&temp1, pcout);
1653 D3DXQuaternionMultiply(&temp2, &temp1, pq1);
1654 D3DXQuaternionLn(&temp2, &temp2);
1655 D3DXQuaternionMultiply(&q, &temp1, &temp3);
1656 D3DXQuaternionLn(&q, &q);
1657 temp1 = add_diff(&temp2, &q, 1.0f);
1662 D3DXQuaternionExp(&temp1, &temp1);
1663 D3DXQuaternionMultiply(pbout, pcout, &temp1);
1668 void WINAPI D3DXQuaternionToAxisAngle(const D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1670 TRACE("pq %p, paxis %p, pangle %p\n", pq, paxis, pangle);
1675 *pangle = 2.0f * acos(pq->w);
1678 /*_________________D3DXVec2_____________________*/
1680 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1682 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
1684 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1685 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1689 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv0, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pv3, FLOAT s)
1691 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
1693 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);
1694 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);
1698 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pt1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pt2, FLOAT s)
1700 FLOAT h1, h2, h3, h4;
1702 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
1704 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1705 h2 = s * s * s - 2.0f * s * s + s;
1706 h3 = -2.0f * s * s * s + 3.0f * s * s;
1707 h4 = s * s * s - s * s;
1709 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1710 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1714 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv)
1718 TRACE("pout %p, pv %p\n", pout, pv);
1720 norm = D3DXVec2Length(pv);
1728 pout->x = pv->x / norm;
1729 pout->y = pv->y / norm;
1735 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1737 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1739 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
1740 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
1741 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
1742 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1746 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR2* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1750 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1752 for (i = 0; i < elements; ++i) {
1754 (D3DXVECTOR4*)((char*)out + outstride * i),
1755 (const D3DXVECTOR2*)((const char*)in + instride * i),
1761 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1766 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1769 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1771 pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1772 pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1777 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, const D3DXVECTOR2* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1781 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1783 for (i = 0; i < elements; ++i) {
1784 D3DXVec2TransformCoord(
1785 (D3DXVECTOR2*)((char*)out + outstride * i),
1786 (const D3DXVECTOR2*)((const char*)in + instride * i),
1792 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1794 const D3DXVECTOR2 v = *pv;
1796 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1798 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1799 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1803 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, const D3DXVECTOR2 *in, UINT instride, const D3DXMATRIX *matrix, UINT elements)
1807 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1809 for (i = 0; i < elements; ++i) {
1810 D3DXVec2TransformNormal(
1811 (D3DXVECTOR2*)((char*)out + outstride * i),
1812 (const D3DXVECTOR2*)((const char*)in + instride * i),
1818 /*_________________D3DXVec3_____________________*/
1820 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1822 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
1824 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1825 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1826 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1830 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv0, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3, FLOAT s)
1832 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
1834 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);
1835 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);
1836 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);
1840 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pt1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pt2, FLOAT s)
1842 FLOAT h1, h2, h3, h4;
1844 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
1846 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1847 h2 = s * s * s - 2.0f * s * s + s;
1848 h3 = -2.0f * s * s * s + 3.0f * s * s;
1849 h4 = s * s * s - s * s;
1851 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1852 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1853 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1857 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv)
1861 TRACE("pout %p, pv %p\n", pout, pv);
1863 norm = D3DXVec3Length(pv);
1872 pout->x = pv->x / norm;
1873 pout->y = pv->y / norm;
1874 pout->z = pv->z / norm;
1880 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DVIEWPORT9 *pviewport, const D3DXMATRIX *pprojection, const D3DXMATRIX *pview, const D3DXMATRIX *pworld)
1884 TRACE("pout %p, pv %p, pviewport %p, pprojection %p, pview %p, pworld %p\n", pout, pv, pviewport, pprojection, pview, pworld);
1886 D3DXMatrixIdentity(&m);
1887 if (pworld) D3DXMatrixMultiply(&m, &m, pworld);
1888 if (pview) D3DXMatrixMultiply(&m, &m, pview);
1889 if (pprojection) D3DXMatrixMultiply(&m, &m, pprojection);
1891 D3DXVec3TransformCoord(pout, pv, &m);
1895 pout->x = pviewport->X + ( 1.0f + pout->x ) * pviewport->Width / 2.0f;
1896 pout->y = pviewport->Y + ( 1.0f - pout->y ) * pviewport->Height / 2.0f;
1897 pout->z = pviewport->MinZ + pout->z * ( pviewport->MaxZ - pviewport->MinZ );
1902 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)
1906 TRACE("out %p, outstride %u, in %p, instride %u, viewport %p, projection %p, view %p, world %p, elements %u\n",
1907 out, outstride, in, instride, viewport, projection, view, world, elements);
1909 for (i = 0; i < elements; ++i) {
1911 (D3DXVECTOR3*)((char*)out + outstride * i),
1912 (const D3DXVECTOR3*)((const char*)in + instride * i),
1913 viewport, projection, view, world);
1918 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1920 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1922 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];
1923 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];
1924 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];
1925 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];
1929 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1933 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1935 for (i = 0; i < elements; ++i) {
1937 (D3DXVECTOR4*)((char*)out + outstride * i),
1938 (const D3DXVECTOR3*)((const char*)in + instride * i),
1944 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1949 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1951 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];
1953 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;
1954 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;
1955 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;
1962 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(D3DXVECTOR3* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1966 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1968 for (i = 0; i < elements; ++i) {
1969 D3DXVec3TransformCoord(
1970 (D3DXVECTOR3*)((char*)out + outstride * i),
1971 (const D3DXVECTOR3*)((const char*)in + instride * i),
1977 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1979 const D3DXVECTOR3 v = *pv;
1981 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1983 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1984 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1985 pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1990 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1994 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1996 for (i = 0; i < elements; ++i) {
1997 D3DXVec3TransformNormal(
1998 (D3DXVECTOR3*)((char*)out + outstride * i),
1999 (const D3DXVECTOR3*)((const char*)in + instride * i),
2005 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DVIEWPORT9 *pviewport, const D3DXMATRIX *pprojection, const D3DXMATRIX *pview, const D3DXMATRIX *pworld)
2009 TRACE("pout %p, pv %p, pviewport %p, pprojection %p, pview %p, pworlds %p\n", pout, pv, pviewport, pprojection, pview, pworld);
2011 D3DXMatrixIdentity(&m);
2012 if (pworld) D3DXMatrixMultiply(&m, &m, pworld);
2013 if (pview) D3DXMatrixMultiply(&m, &m, pview);
2014 if (pprojection) D3DXMatrixMultiply(&m, &m, pprojection);
2015 D3DXMatrixInverse(&m, NULL, &m);
2020 pout->x = 2.0f * ( pout->x - pviewport->X ) / pviewport->Width - 1.0f;
2021 pout->y = 1.0f - 2.0f * ( pout->y - pviewport->Y ) / pviewport->Height;
2022 pout->z = ( pout->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
2024 D3DXVec3TransformCoord(pout, pout, &m);
2028 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)
2032 TRACE("out %p, outstride %u, in %p, instride %u, viewport %p, projection %p, view %p, world %p, elements %u\n",
2033 out, outstride, in, instride, viewport, projection, view, world, elements);
2035 for (i = 0; i < elements; ++i) {
2037 (D3DXVECTOR3*)((char*)out + outstride * i),
2038 (const D3DXVECTOR3*)((const char*)in + instride * i),
2039 viewport, projection, view, world);
2044 /*_________________D3DXVec4_____________________*/
2046 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
2048 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
2050 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
2051 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
2052 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
2053 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
2057 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv0, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3, FLOAT s)
2059 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
2061 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);
2062 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);
2063 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);
2064 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);
2068 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3)
2072 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p\n", pout, pv1, pv2, pv3);
2074 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);
2075 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));
2076 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);
2077 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));
2082 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pt1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pt2, FLOAT s)
2084 FLOAT h1, h2, h3, h4;
2086 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
2088 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
2089 h2 = s * s * s - 2.0f * s * s + s;
2090 h3 = -2.0f * s * s * s + 3.0f * s * s;
2091 h4 = s * s * s - s * s;
2093 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
2094 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
2095 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
2096 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
2100 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv)
2104 TRACE("pout %p, pv %p\n", pout, pv);
2106 norm = D3DXVec4Length(pv);
2108 pout->x = pv->x / norm;
2109 pout->y = pv->y / norm;
2110 pout->z = pv->z / norm;
2111 pout->w = pv->w / norm;
2116 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv, const D3DXMATRIX *pm)
2120 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
2122 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;
2123 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;
2124 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;
2125 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;
2130 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR4* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
2134 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
2136 for (i = 0; i < elements; ++i) {
2138 (D3DXVECTOR4*)((char*)out + outstride * i),
2139 (const D3DXVECTOR4*)((const char*)in + instride * i),
2145 unsigned short float_32_to_16(const float in)
2147 int exp = 0, origexp;
2148 float tmp = fabs(in);
2149 int sign = (copysignf(1, in) < 0);
2150 unsigned int mantissa;
2153 /* Deal with special numbers */
2154 if (isinf(in)) return (sign ? 0xffff : 0x7fff);
2155 if (isnan(in)) return (sign ? 0xffff : 0x7fff);
2156 if (in == 0.0f) return (sign ? 0x8000 : 0x0000);
2158 if (tmp < powf(2, 10))
2164 } while (tmp < powf(2, 10));
2166 else if (tmp >= powf(2, 11))
2172 } while (tmp >= powf(2, 11));
2175 exp += 10; /* Normalize the mantissa */
2176 exp += 15; /* Exponent is encoded with excess 15 */
2180 mantissa = (unsigned int) tmp;
2181 if ((tmp - mantissa == 0.5f && mantissa % 2 == 1) || /* round half to even */
2182 (tmp - mantissa > 0.5f))
2184 mantissa++; /* round to nearest, away from zero */
2186 if (mantissa == 2048)
2195 ret = 0x7fff; /* INF */
2199 unsigned int rounding = 0;
2201 /* Denormalized half float */
2203 /* return 0x0000 (=0.0) for numbers too small to represent in half floats */
2205 return (sign ? 0x8000 : 0x0000);
2209 /* the 13 extra bits from single precision are used for rounding */
2210 mantissa = (unsigned int)(tmp * powf(2, 13));
2211 mantissa >>= 1 - exp; /* denormalize */
2213 mantissa -= ~(mantissa >> 13) & 1; /* round half to even */
2214 /* remove 13 least significant bits to get half float precision */
2216 rounding = mantissa & 1;
2219 ret = mantissa + rounding;
2223 ret = (exp << 10) | (mantissa & 0x3ff);
2226 ret |= ((sign ? 1 : 0) << 15); /* Add the sign */
2230 D3DXFLOAT16 *WINAPI D3DXFloat32To16Array(D3DXFLOAT16 *pout, const FLOAT *pin, UINT n)
2234 TRACE("pout %p, pin %p, n %u\n", pout, pin, n);
2236 for (i = 0; i < n; ++i)
2238 pout[i].value = float_32_to_16(pin[i]);
2244 /* Native d3dx9's D3DXFloat16to32Array lacks support for NaN and Inf. Specifically, e = 16 is treated as a
2245 * regular number - e.g., 0x7fff is converted to 131008.0 and 0xffff to -131008.0. */
2246 static inline float float_16_to_32(const unsigned short in)
2248 const unsigned short s = (in & 0x8000);
2249 const unsigned short e = (in & 0x7C00) >> 10;
2250 const unsigned short m = in & 0x3FF;
2251 const float sgn = (s ? -1.0f : 1.0f);
2255 if (m == 0) return sgn * 0.0f; /* +0.0 or -0.0 */
2256 else return sgn * powf(2, -14.0f) * (m / 1024.0f);
2260 return sgn * powf(2, e - 15.0f) * (1.0f + (m / 1024.0f));
2264 FLOAT *WINAPI D3DXFloat16To32Array(FLOAT *pout, const D3DXFLOAT16 *pin, UINT n)
2268 TRACE("pout %p, pin %p, n %u\n", pout, pin, n);
2270 for (i = 0; i < n; ++i)
2272 pout[i] = float_16_to_32(pin[i].value);
2278 /*_________________D3DXSH________________*/
2280 FLOAT* WINAPI D3DXSHAdd(FLOAT *out, UINT order, const FLOAT *a, const FLOAT *b)
2284 TRACE("out %p, order %u, a %p, b %p\n", out, order, a, b);
2286 for (i = 0; i < order * order; i++)
2287 out[i] = a[i] + b[i];
2292 FLOAT WINAPI D3DXSHDot(UINT order, const FLOAT *a, const FLOAT *b)
2297 TRACE("order %u, a %p, b %p\n", order, a, b);
2300 for (i = 1; i < order * order; i++)
2306 static void weightedcapintegrale(FLOAT *out, FLOAT order, FLOAT angle)
2310 coeff[0] = cosf(angle);
2312 out[0] = 2.0f * D3DX_PI * (1.0f - coeff[0]);
2313 out[1] = D3DX_PI * sinf(angle) * sinf(angle);
2317 out[2] = coeff[0] * out[1];
2321 coeff[1] = coeff[0] * coeff[0];
2322 coeff[2] = coeff[1] * coeff[1];
2324 out[3] = D3DX_PI * (-1.25f * coeff[2] + 1.5f * coeff[1] - 0.25f);
2328 out[4] = -0.25f * D3DX_PI * coeff[0] * (7.0f * coeff[2] - 10.0f * coeff[1] + 3.0f);
2332 out[5] = D3DX_PI * (-2.625f * coeff[2] * coeff[1] + 4.375f * coeff[2] - 1.875f * coeff[1] + 0.125f);
2335 HRESULT WINAPI D3DXSHEvalConeLight(UINT order, const D3DXVECTOR3 *dir, FLOAT radius,
2336 FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *rout, FLOAT *gout, FLOAT *bout)
2338 FLOAT cap[6], clamped_angle, norm, scale, temp;
2341 TRACE("order %u, dir %p, radius %f, red %f, green %f, blue %f, rout %p, gout %p, bout %p\n",
2342 order, dir, radius, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2345 return D3DXSHEvalDirectionalLight(order, dir, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2347 clamped_angle = (radius > D3DX_PI / 2.0f) ? (D3DX_PI / 2.0f) : radius;
2348 norm = sinf(clamped_angle) * sinf(clamped_angle);
2350 if (order > D3DXSH_MAXORDER)
2352 WARN("Order clamped at D3DXSH_MAXORDER\n");
2353 order = D3DXSH_MAXORDER;
2356 weightedcapintegrale(cap, order, radius);
2357 D3DXSHEvalDirection(rout, order, dir);
2359 for (i = 0; i < order; i++)
2361 scale = cap[i] / norm;
2363 for (j = 0; j < 2 * i + 1; j++)
2366 temp = rout[index] * scale;
2368 rout[index] = temp * Rintensity;
2370 gout[index] = temp * Gintensity;
2372 bout[index] = temp * Bintensity;
2379 FLOAT* WINAPI D3DXSHEvalDirection(FLOAT *out, UINT order, const D3DXVECTOR3 *dir)
2381 const FLOAT dirxx = dir->x * dir->x;
2382 const FLOAT dirxy = dir->x * dir->y;
2383 const FLOAT dirxz = dir->x * dir->z;
2384 const FLOAT diryy = dir->y * dir->y;
2385 const FLOAT diryz = dir->y * dir->z;
2386 const FLOAT dirzz = dir->z * dir->z;
2387 const FLOAT dirxxxx = dirxx * dirxx;
2388 const FLOAT diryyyy = diryy * diryy;
2389 const FLOAT dirzzzz = dirzz * dirzz;
2390 const FLOAT dirxyxy = dirxy * dirxy;
2392 TRACE("out %p, order %u, dir %p\n", out, order, dir);
2394 if ((order < D3DXSH_MINORDER) || (order > D3DXSH_MAXORDER))
2397 out[0] = 0.5f / sqrtf(D3DX_PI);
2398 out[1] = -0.5f / sqrtf(D3DX_PI / 3.0f) * dir->y;
2399 out[2] = 0.5f / sqrtf(D3DX_PI / 3.0f) * dir->z;
2400 out[3] = -0.5f / sqrtf(D3DX_PI / 3.0f) * dir->x;
2404 out[4] = 0.5f / sqrtf(D3DX_PI / 15.0f) * dirxy;
2405 out[5] = -0.5f / sqrtf(D3DX_PI / 15.0f) * diryz;
2406 out[6] = 0.25f / sqrtf(D3DX_PI / 5.0f) * (3.0f * dirzz - 1.0f);
2407 out[7] = -0.5f / sqrtf(D3DX_PI / 15.0f) * dirxz;
2408 out[8] = 0.25f / sqrtf(D3DX_PI / 15.0f) * (dirxx - diryy);
2412 out[9] = -sqrtf(70.0f / D3DX_PI) / 8.0f * dir->y * (3.0f * dirxx - diryy);
2413 out[10] = sqrtf(105.0f / D3DX_PI) / 2.0f * dirxy * dir->z;
2414 out[11] = -sqrtf(42.0 / D3DX_PI) / 8.0f * dir->y * (-1.0f + 5.0f * dirzz);
2415 out[12] = sqrtf(7.0f / D3DX_PI) / 4.0f * dir->z * (5.0f * dirzz - 3.0f);
2416 out[13] = sqrtf(42.0 / D3DX_PI) / 8.0f * dir->x * (1.0f - 5.0f * dirzz);
2417 out[14] = sqrtf(105.0f / D3DX_PI) / 4.0f * dir->z * (dirxx - diryy);
2418 out[15] = -sqrtf(70.0f / D3DX_PI) / 8.0f * dir->x * (dirxx - 3.0f * diryy);
2422 out[16] = 0.75f * sqrtf(35.0f / D3DX_PI) * dirxy * (dirxx - diryy);
2423 out[17] = 3.0f * dir->z * out[9];
2424 out[18] = 0.75f * sqrtf(5.0f / D3DX_PI) * dirxy * (7.0f * dirzz - 1.0f);
2425 out[19] = 0.375f * sqrtf(10.0f / D3DX_PI) * diryz * (3.0f - 7.0f * dirzz);
2426 out[20] = 3.0f / (16.0f * sqrtf(D3DX_PI)) * (35.0f * dirzzzz - 30.f * dirzz + 3.0f);
2427 out[21] = 0.375f * sqrtf(10.0f / D3DX_PI) * dirxz * (3.0f - 7.0f * dirzz);
2428 out[22] = 0.375f * sqrtf(5.0f / D3DX_PI) * (dirxx - diryy) * (7.0f * dirzz - 1.0f);
2429 out[23] = 3.0 * dir->z * out[15];
2430 out[24] = 3.0f / 16.0f * sqrtf(35.0f / D3DX_PI) * (dirxxxx - 6.0f * dirxyxy + diryyyy);
2434 out[25] = -3.0f/ 32.0f * sqrtf(154.0f / D3DX_PI) * dir->y * (5.0f * dirxxxx - 10.0f * dirxyxy + diryyyy);
2435 out[26] = 0.75f * sqrtf(385.0f / D3DX_PI) * dirxy * dir->z * (dirxx - diryy);
2436 out[27] = sqrtf(770.0f / D3DX_PI) / 32.0f * dir->y * (3.0f * dirxx - diryy) * (1.0f - 9.0f * dirzz);
2437 out[28] = sqrtf(1155.0f / D3DX_PI) / 4.0f * dirxy * dir->z * (3.0f * dirzz - 1.0f);
2438 out[29] = sqrtf(165.0f / D3DX_PI) / 16.0f * dir->y * (14.0f * dirzz - 21.0f * dirzzzz - 1.0f);
2439 out[30] = sqrtf(11.0f / D3DX_PI) / 16.0f * dir->z * (63.0f * dirzzzz - 70.0f * dirzz + 15.0f);
2440 out[31] = sqrtf(165.0f / D3DX_PI) / 16.0f * dir->x * (14.0f * dirzz - 21.0f * dirzzzz - 1.0f);
2441 out[32] = sqrtf(1155.0f / D3DX_PI) / 8.0f * dir->z * (dirxx - diryy) * (3.0f * dirzz - 1.0f);
2442 out[33] = sqrtf(770.0f / D3DX_PI) / 32.0f * dir->x * (dirxx - 3.0f * diryy) * (1.0f - 9.0f * dirzz);
2443 out[34] = 3.0f / 16.0f * sqrtf(385.0f / D3DX_PI) * dir->z * (dirxxxx - 6.0 * dirxyxy + diryyyy);
2444 out[35] = -3.0f/ 32.0f * sqrtf(154.0f / D3DX_PI) * dir->x * (dirxxxx - 10.0f * dirxyxy + 5.0f * diryyyy);
2449 HRESULT WINAPI D3DXSHEvalDirectionalLight(UINT order, const D3DXVECTOR3 *dir, FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *Rout, FLOAT *Gout, FLOAT *Bout)
2454 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);
2463 D3DXSHEvalDirection(Rout, order, dir);
2464 for (j = 0; j < order * order; j++)
2468 Rout[j] = Rintensity * temp;
2470 Gout[j] = Gintensity * temp;
2472 Bout[j] = Bintensity * temp;
2478 HRESULT WINAPI D3DXSHEvalHemisphereLight(UINT order, const D3DXVECTOR3 *dir, D3DXCOLOR top, D3DXCOLOR bottom,
2479 FLOAT *rout, FLOAT *gout, FLOAT *bout)
2481 FLOAT a[2], temp[4];
2484 TRACE("order %u, dir %p, rout %p, gout %p, bout %p\n", order, dir, rout, gout, bout);
2486 D3DXSHEvalDirection(temp, 2, dir);
2488 a[0] = (top.r + bottom.r) * 3.0f * D3DX_PI;
2489 a[1] = (top.r - bottom.r) * D3DX_PI;
2490 for (i = 0; i < order; i++)
2491 for (j = 0; j < 2 * i + 1; j++)
2493 rout[i * i + j] = temp[i * i + j] * a[i];
2495 rout[i * i + j] = 0.0f;
2499 a[0] = (top.g + bottom.g) * 3.0f * D3DX_PI;
2500 a[1] = (top.g - bottom.g) * D3DX_PI;
2501 for (i = 0; i < order; i++)
2502 for (j = 0; j < 2 * i + 1; j++)
2504 gout[i * i + j] = temp[i * i + j] * a[i];
2506 gout[i * i + j] = 0.0f;
2511 a[0] = (top.b + bottom.b) * 3.0f * D3DX_PI;
2512 a[1] = (top.b - bottom.b) * D3DX_PI;
2513 for (i = 0; i < order; i++)
2514 for (j = 0; j < 2 * i + 1; j++)
2516 bout[i * i + j] = temp[i * i + j] * a[i];
2518 bout[i * i + j] = 0.0f;
2524 HRESULT WINAPI D3DXSHEvalSphericalLight(UINT order, const D3DXVECTOR3 *dir, FLOAT radius,
2525 FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *rout, FLOAT *gout, FLOAT *bout)
2528 FLOAT cap[6], clamped_angle, dist, temp;
2531 TRACE("order %u, dir %p, radius %f, red %f, green %f, blue %f, rout %p, gout %p, bout %p\n",
2532 order, dir, radius, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2534 if (order > D3DXSH_MAXORDER)
2536 WARN("Order clamped at D3DXSH_MAXORDER\n");
2537 order = D3DXSH_MAXORDER;
2543 dist = D3DXVec3Length(dir);
2544 clamped_angle = (dist <= radius) ? D3DX_PI / 2.0f : asinf(radius / dist);
2546 weightedcapintegrale(cap, order, clamped_angle);
2547 D3DXVec3Normalize(&normal, dir);
2548 D3DXSHEvalDirection(rout, order, &normal);
2550 for (i = 0; i < order; i++)
2551 for (j = 0; j < 2 * i + 1; j++)
2554 temp = rout[index] * cap[i];
2556 rout[index] = temp * Rintensity;
2558 gout[index] = temp * Gintensity;
2560 bout[index] = temp * Bintensity;
2566 FLOAT * WINAPI D3DXSHMultiply2(FLOAT *out, const FLOAT *a, const FLOAT *b)
2570 TRACE("out %p, a %p, b %p\n", out, a, b);
2572 ta = 0.28209479f * a[0];
2573 tb = 0.28209479f * b[0];
2575 out[0] = 0.28209479f * D3DXSHDot(2, a, b);
2576 out[1] = ta * b[1] + tb * a[1];
2577 out[2] = ta * b[2] + tb * a[2];
2578 out[3] = ta * b[3] + tb * a[3];
2583 FLOAT * WINAPI D3DXSHMultiply3(FLOAT *out, const FLOAT *a, const FLOAT *b)
2587 TRACE("out %p, a %p, b %p\n", out, a, b);
2589 out[0] = 0.28209479f * a[0] * b[0];
2591 ta = 0.28209479f * a[0] - 0.12615662f * a[6] - 0.21850968f * a[8];
2592 tb = 0.28209479f * b[0] - 0.12615662f * b[6] - 0.21850968f * b[8];
2593 out[1] = ta * b[1] + tb * a[1];
2595 out[0] += 0.28209479f * t;
2596 out[6] = -0.12615662f * t;
2597 out[8] = -0.21850968f * t;
2599 ta = 0.21850968f * a[5];
2600 tb = 0.21850968f * b[5];
2601 out[1] += ta * b[2] + tb * a[2];
2602 out[2] = ta * b[1] + tb * a[1];
2603 t = a[1] * b[2] +a[2] * b[1];
2604 out[5] = 0.21850968f * t;
2606 ta = 0.21850968f * a[4];
2607 tb = 0.21850968f * b[4];
2608 out[1] += ta * b[3] + tb * a[3];
2609 out[3] = ta * b[1] + tb * a[1];
2610 t = a[1] * b[3] + a[3] * b[1];
2611 out[4] = 0.21850968f * t;
2613 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2614 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2615 out[2] += ta * b[2] + tb * a[2];
2617 out[0] += 0.28209480f * t;
2618 out[6] += 0.25231326f * t;
2620 ta = 0.21850969f * a[7];
2621 tb = 0.21850969f * b[7];
2622 out[2] += ta * b[3] + tb * a[3];
2623 out[3] += ta * b[2] + tb * a[2];
2624 t = a[2] * b[3] + a[3] * b[2];
2625 out[7] = 0.21850969f * t;
2627 ta = 0.28209479f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2628 tb = 0.28209479f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2629 out[3] += ta * b[3] + tb * a[3];
2631 out[0] += 0.28209479f * t;
2632 out[6] -= 0.12615663f * t;
2633 out[8] += 0.21850969f * t;
2635 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2636 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2637 out[4] += ta * b[4] + tb * a[4];
2639 out[0] += 0.28209479f * t;
2640 out[6] -= 0.18022375f * t;
2642 ta = 0.15607835f * a[7];
2643 tb = 0.15607835f * b[7];
2644 out[4] += ta * b[5] + tb * a[5];
2645 out[5] += ta * b[4] + tb * a[4];
2646 t = a[4] * b[5] + a[5] * b[4];
2647 out[7] += 0.15607834f * t;
2649 ta = 0.28209479f * a[0] + 0.09011186 * a[6] - 0.15607835f * a[8];
2650 tb = 0.28209479f * b[0] + 0.09011186 * b[6] - 0.15607835f * b[8];
2651 out[5] += ta * b[5] + tb * a[5];
2653 out[0] += 0.28209479f * t;
2654 out[6] += 0.09011186f * t;
2655 out[8] -= 0.15607835f * t;
2657 ta = 0.28209480f * a[0];
2658 tb = 0.28209480f * b[0];
2659 out[6] += ta * b[6] + tb * a[6];
2661 out[0] += 0.28209480f * t;
2662 out[6] += 0.18022376f * t;
2664 ta = 0.28209479f * a[0] + 0.09011186 * a[6] + 0.15607835f * a[8];
2665 tb = 0.28209479f * b[0] + 0.09011186 * b[6] + 0.15607835f * b[8];
2666 out[7] += ta * b[7] + tb * a[7];
2668 out[0] += 0.28209479f * t;
2669 out[6] += 0.09011186f * t;
2670 out[8] += 0.15607835f * t;
2672 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2673 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2674 out[8] += ta * b[8] + tb * a[8];
2676 out[0] += 0.28209479f * t;
2677 out[6] -= 0.18022375f * t;
2682 FLOAT * WINAPI D3DXSHMultiply4(FLOAT *out, const FLOAT *a, const FLOAT *b)
2686 TRACE("out %p, a %p, b %p\n", out, a, b);
2688 out[0] = 0.28209479f * a[0] * b[0];
2690 ta = 0.28209479f * a[0] - 0.12615663f * a[6] - 0.21850969f * a[8];
2691 tb = 0.28209479f * b[0] - 0.12615663f * b[6] - 0.21850969f * b[8];
2692 out[1] = ta * b[1] + tb * a[1];
2694 out[0] += 0.28209479f * t;
2695 out[6] = -0.12615663f * t;
2696 out[8] = -0.21850969f * t;
2698 ta = 0.21850969f * a[3] - 0.05839917f * a[13] - 0.22617901f * a[15];
2699 tb = 0.21850969f * b[3] - 0.05839917f * b[13] - 0.22617901f * b[15];
2700 out[1] += ta * b[4] + tb * a[4];
2701 out[4] = ta * b[1] + tb * a[1];
2702 t = a[1] * b[4] + a[4] * b[1];
2703 out[3] = 0.21850969f * t;
2704 out[13] = -0.05839917f * t;
2705 out[15] = -0.22617901f * t;
2707 ta = 0.21850969f * a[2] - 0.14304817f * a[12] - 0.18467439f * a[14];
2708 tb = 0.21850969f * b[2] - 0.14304817f * b[12] - 0.18467439f * b[14];
2709 out[1] += ta * b[5] + tb * a[5];
2710 out[5] = ta * b[1] + tb * a[1];
2711 t = a[1] * b[5] + a[5] * b[1];
2712 out[2] = 0.21850969f * t;
2713 out[12] = -0.14304817f * t;
2714 out[14] = -0.18467439f * t;
2716 ta = 0.20230066f * a[11];
2717 tb = 0.20230066f * b[11];
2718 out[1] += ta * b[6] + tb * a[6];
2719 out[6] += ta * b[1] + tb * a[1];
2720 t = a[1] * b[6] + a[6] * b[1];
2721 out[11] = 0.20230066f * t;
2723 ta = 0.22617901f * a[9] + 0.05839917f * a[11];
2724 tb = 0.22617901f * b[9] + 0.05839917f * b[11];
2725 out[1] += ta * b[8] + tb * a[8];
2726 out[8] += ta * b[1] + tb * a[1];
2727 t = a[1] * b[8] + a[8] * b[1];
2728 out[9] = 0.22617901f * t;
2729 out[11] += 0.05839917f * t;
2731 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2732 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2733 out[2] += ta * b[2] + tb * a[2];
2735 out[0] += 0.28209480f * t;
2736 out[6] += 0.25231326f * t;
2738 ta = 0.24776671f * a[12];
2739 tb = 0.24776671f * b[12];
2740 out[2] += ta * b[6] + tb * a[6];
2741 out[6] += ta * b[2] + tb * a[2];
2742 t = a[2] * b[6] + a[6] * b[2];
2743 out[12] += 0.24776671f * t;
2745 ta = 0.28209480f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2746 tb = 0.28209480f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2747 out[3] += ta * b[3] + tb * a[3];
2749 out[0] += 0.28209480f * t;
2750 out[6] -= 0.12615663f * t;
2751 out[8] += 0.21850969f * t;
2753 ta = 0.20230066f * a[13];
2754 tb = 0.20230066f * b[13];
2755 out[3] += ta * b[6] + tb * a[6];
2756 out[6] += ta * b[3] + tb * a[3];
2757 t = a[3] * b[6] + a[6] * b[3];
2758 out[13] += 0.20230066f * t;
2760 ta = 0.21850969f * a[2] - 0.14304817f * a[12] + 0.18467439f * a[14];
2761 tb = 0.21850969f * b[2] - 0.14304817f * b[12] + 0.18467439f * b[14];
2762 out[3] += ta * b[7] + tb * a[7];
2763 out[7] = ta * b[3] + tb * a[3];
2764 t = a[3] * b[7] + a[7] * b[3];
2765 out[2] += 0.21850969f * t;
2766 out[12] -= 0.14304817f * t;
2767 out[14] += 0.18467439f * t;
2769 ta = -0.05839917f * a[13] + 0.22617901f * a[15];
2770 tb = -0.05839917f * b[13] + 0.22617901f * b[15];
2771 out[3] += ta * b[8] + tb * a[8];
2772 out[8] += ta * b[3] + tb * a[3];
2773 t = a[3] * b[8] + a[8] * b[3];
2774 out[13] -= 0.05839917f * t;
2775 out[15] += 0.22617901f * t;
2777 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2778 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2779 out[4] += ta * b[4] + tb * a[4];
2781 out[0] += 0.28209479f * t;
2782 out[6] -= 0.18022375f * t;
2784 ta = 0.15607835f * a[7];
2785 tb = 0.15607835f * b[7];
2786 out[4] += ta * b[5] + tb * a[5];
2787 out[5] += ta * b[4] + tb * a[4];
2788 t = a[4] * b[5] + a[5] * b[4];
2789 out[7] += 0.15607835f * t;
2791 ta = 0.22617901f * a[3] - 0.09403160f * a[13];
2792 tb = 0.22617901f * b[3] - 0.09403160f * b[13];
2793 out[4] += ta * b[9] + tb * a[9];
2794 out[9] += ta * b[4] + tb * a[4];
2795 t = a[4] * b[9] + a[9] * b[4];
2796 out[3] += 0.22617901f * t;
2797 out[13] -= 0.09403160f * t;
2799 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2800 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2801 out[4] += ta * b[10] + tb * a [10];
2802 out[10] = ta * b[4] + tb * a[4];
2803 t = a[4] * b[10] + a[10] * b[4];
2804 out[2] += 0.18467439f * t;
2805 out[12] -= 0.18806319f * t;
2807 ta = -0.05839917f * a[3] + 0.14567312f * a[13] + 0.09403160f * a[15];
2808 tb = -0.05839917f * b[3] + 0.14567312f * b[13] + 0.09403160f * b[15];
2809 out[4] += ta * b[11] + tb * a[11];
2810 out[11] += ta * b[4] + tb * a[4];
2811 t = a[4] * b[11] + a[11] * b[4];
2812 out[3] -= 0.05839917f * t;
2813 out[13] += 0.14567312f * t;
2814 out[15] += 0.09403160f * t;
2816 ta = 0.28209479f * a[0] + 0.09011186f * a[6] - 0.15607835f * a[8];
2817 tb = 0.28209479f * b[0] + 0.09011186f * b[6] - 0.15607835f * b[8];
2818 out[5] += ta * b[5] + tb * a[5];
2820 out[0] += 0.28209479f * t;
2821 out[6] += 0.09011186f * t;
2822 out[8] -= 0.15607835f * t;
2824 ta = 0.14867701f * a[14];
2825 tb = 0.14867701f * b[14];
2826 out[5] += ta * b[9] + tb * a[9];
2827 out[9] += ta * b[5] + tb * a[5];
2828 t = a[5] * b[9] + a[9] * b[5];
2829 out[14] += 0.14867701f * t;
2831 ta = 0.18467439f * a[3] + 0.11516472f * a[13] - 0.14867701f * a[15];
2832 tb = 0.18467439f * b[3] + 0.11516472f * b[13] - 0.14867701f * b[15];
2833 out[5] += ta * b[10] + tb * a[10];
2834 out[10] += ta * b[5] + tb * a[5];
2835 t = a[5] * b[10] + a[10] * b[5];
2836 out[3] += 0.18467439f * t;
2837 out[13] += 0.11516472f * t;
2838 out[15] -= 0.14867701f * t;
2840 ta = 0.23359668f * a[2] + 0.05947080f * a[12] - 0.11516472f * a[14];
2841 tb = 0.23359668f * b[2] + 0.05947080f * b[12] - 0.11516472f * b[14];
2842 out[5] += ta * b[11] + tb * a[11];
2843 out[11] += ta * b[5] + tb * a[5];
2844 t = a[5] * b[11] + a[11] * b[5];
2845 out[2] += 0.23359668f * t;
2846 out[12] += 0.05947080f * t;
2847 out[14] -= 0.11516472f * t;
2849 ta = 0.28209479f * a[0];
2850 tb = 0.28209479f * b[0];
2851 out[6] += ta * b[6] + tb * a[6];
2853 out[0] += 0.28209479f * t;
2854 out[6] += 0.18022376f * t;
2856 ta = 0.09011186f * a[6] + 0.28209479f * a[0] + 0.15607835f * a[8];
2857 tb = 0.09011186f * b[6] + 0.28209479f * b[0] + 0.15607835f * b[8];
2858 out[7] += ta * b[7] + tb * a[7];
2860 out[6] += 0.09011186f * t;
2861 out[0] += 0.28209479f * t;
2862 out[8] += 0.15607835f * t;
2864 ta = 0.14867701f * a[9] + 0.18467439f * a[1] + 0.11516472f * a[11];
2865 tb = 0.14867701f * b[9] + 0.18467439f * b[1] + 0.11516472f * b[11];
2866 out[7] += ta * b[10] + tb * a[10];
2867 out[10] += ta * b[7] + tb * a[7];
2868 t = a[7] * b[10] + a[10] * b[7];
2869 out[9] += 0.14867701f * t;
2870 out[1] += 0.18467439f * t;
2871 out[11] += 0.11516472f * t;
2873 ta = 0.05947080f * a[12] + 0.23359668f * a[2] + 0.11516472f * a[14];
2874 tb = 0.05947080f * b[12] + 0.23359668f * b[2] + 0.11516472f * b[14];
2875 out[7] += ta * b[13] + tb * a[13];
2876 out[13] += ta * b[7]+ tb * a[7];
2877 t = a[7] * b[13] + a[13] * b[7];
2878 out[12] += 0.05947080f * t;
2879 out[2] += 0.23359668f * t;
2880 out[14] += 0.11516472f * t;
2882 ta = 0.14867701f * a[15];
2883 tb = 0.14867701f * b[15];
2884 out[7] += ta * b[14] + tb * a[14];
2885 out[14] += ta * b[7] + tb * a[7];
2886 t = a[7] * b[14] + a[14] * b[7];
2887 out[15] += 0.14867701f * t;
2889 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2890 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2891 out[8] += ta * b[8] + tb * a[8];
2893 out[0] += 0.28209479f * t;
2894 out[6] -= 0.18022375f * t;
2896 ta = -0.09403160f * a[11];
2897 tb = -0.09403160f * b[11];
2898 out[8] += ta * b[9] + tb * a[9];
2899 out[9] += ta * b[8] + tb * a[8];
2900 t = a[8] * b[9] + a[9] * b[8];
2901 out[11] -= 0.09403160f * t;
2903 ta = -0.09403160f * a[15];
2904 tb = -0.09403160f * b[15];
2905 out[8] += ta * b[13] + tb * a[13];
2906 out[13] += ta * b[8] + tb * a[8];
2907 t = a[8] * b[13] + a[13] * b[8];
2908 out[15] -= 0.09403160f * t;
2910 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2911 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2912 out[8] += ta * b[14] + tb * a[14];
2913 out[14] += ta * b[8] + tb * a[8];
2914 t = a[8] * b[14] + a[14] * b[8];
2915 out[2] += 0.18467439f * t;
2916 out[12] -= 0.18806319f * t;
2918 ta = -0.21026104f * a[6] + 0.28209479f * a[0];
2919 tb = -0.21026104f * b[6] + 0.28209479f * b[0];
2920 out[9] += ta * b[9] + tb * a[9];
2922 out[6] -= 0.21026104f * t;
2923 out[0] += 0.28209479f * t;
2925 ta = 0.28209479f * a[0];
2926 tb = 0.28209479f * b[0];
2927 out[10] += ta * b[10] + tb * a[10];
2929 out[0] += 0.28209479f * t;
2931 ta = 0.28209479f * a[0] + 0.12615663f * a[6] - 0.14567312f * a[8];
2932 tb = 0.28209479f * b[0] + 0.12615663f * b[6] - 0.14567312f * b[8];
2933 out[11] += ta * b[11] + tb * a[11];
2935 out[0] += 0.28209479f * t;
2936 out[6] += 0.12615663f * t;
2937 out[8] -= 0.14567312f * t;
2939 ta = 0.28209479f * a[0] + 0.16820885f * a[6];
2940 tb = 0.28209479f * b[0] + 0.16820885f * b[6];
2941 out[12] += ta * b[12] + tb * a[12];
2943 out[0] += 0.28209479f * t;
2944 out[6] += 0.16820885f * t;
2946 ta =0.28209479f * a[0] + 0.14567312f * a[8] + 0.12615663f * a[6];
2947 tb =0.28209479f * b[0] + 0.14567312f * b[8] + 0.12615663f * b[6];
2948 out[13] += ta * b[13] + tb * a[13];
2950 out[0] += 0.28209479f * t;
2951 out[8] += 0.14567312f * t;
2952 out[6] += 0.12615663f * t;
2954 ta = 0.28209479f * a[0];
2955 tb = 0.28209479f * b[0];
2956 out[14] += ta * b[14] + tb * a[14];
2958 out[0] += 0.28209479f * t;
2960 ta = 0.28209479f * a[0] - 0.21026104f * a[6];
2961 tb = 0.28209479f * b[0] - 0.21026104f * b[6];
2962 out[15] += ta * b[15] + tb * a[15];
2964 out[0] += 0.28209479f * t;
2965 out[6] -= 0.21026104f * t;
2970 static void rotate_X(FLOAT *out, UINT order, FLOAT a, FLOAT *in)
2975 out[2] = -a * in[1];
2980 out[6] = -0.5f * in[6] - 0.8660253882f * in[8];
2981 out[7] = -a * in[4];
2982 out[8] = -0.8660253882f * in[6] + 0.5f * in[8];
2983 out[9] = -a * 0.7905694842f * in[12] + a * 0.6123724580f * in[14];
2986 out[11] = -a * 0.6123724580f * in[12] - a * 0.7905694842f * in[14];
2987 out[12] = a * 0.7905694842f * in[9] + a * 0.6123724580f * in[11];
2988 out[13] = -0.25f * in[13] - 0.9682458639f * in[15];
2989 out[14] = -a * 0.6123724580f * in[9] + a * 0.7905694842f * in[11];
2990 out[15] = -0.9682458639f * in[13] + 0.25f * in[15];
2994 out[16] = -a * 0.9354143739f * in[21] + a * 0.3535533845f * in[23];
2995 out[17] = -0.75f * in[17] + 0.6614378095f * in[19];
2996 out[18] = -a * 0.3535533845f * in[21] - a * 0.9354143739f * in[23];
2997 out[19] = 0.6614378095f * in[17] + 0.75f * in[19];
2998 out[20] = 0.375f * in[20] + 0.5590170026f * in[22] + 0.7395099998f * in[24];
2999 out[21] = a * 0.9354143739f * in[16] + a * 0.3535533845f * in[18];
3000 out[22] = 0.5590170026f * in[20] + 0.5f * in[22] - 0.6614378691f * in[24];
3001 out[23] = -a * 0.3535533845f * in[16] + a * 0.9354143739f * in[18];
3002 out[24] = 0.7395099998f * in[20] - 0.6614378691f * in[22] + 0.125f * in[24];
3006 out[25] = a * 0.7015607357f * in[30] - a * 0.6846531630f * in[32] + a * 0.1976423711f * in[34];
3007 out[26] = -0.5f * in[26] + 0.8660253882f * in[28];
3008 out[27] = a * 0.5229125023f * in[30] + a * 0.3061861992f * in[32] - a * 0.7954951525 * in[34];
3009 out[28] = 0.8660253882f * in[26] + 0.5f * in[28];
3010 out[29] = a * 0.4841229022f * in[30] + a * 0.6614378691f * in[32] + a * 0.5728219748f * in[34];
3011 out[30] = -a * 0.7015607357f * in[25] - a * 0.5229125023f * in[27] - a * 0.4841229022f * in[29];
3012 out[31] = 0.125f * in[31] + 0.4050463140f * in[33] + 0.9057110548f * in[35];
3013 out[32] = a * 0.6846531630f * in[25] - a * 0.3061861992f * in[27] - a * 0.6614378691f * in[29];
3014 out[33] = 0.4050463140f * in[31] + 0.8125f * in[33] - 0.4192627370f * in[35];
3015 out[34] = -a * 0.1976423711f * in[25] + a * 0.7954951525f * in[27] - a * 0.5728219748f * in[29];
3016 out[35] = 0.9057110548f * in[31] - 0.4192627370f * in[33] + 0.0624999329f * in[35];
3019 FLOAT* WINAPI D3DXSHRotate(FLOAT *out, UINT order, const D3DXMATRIX *matrix, const FLOAT *in)
3021 FLOAT alpha, beta, gamma, sinb, temp[36], temp1[36];
3023 TRACE("out %p, order %u, matrix %p, in %p\n", out, order, matrix, in);
3027 if ((order > D3DXSH_MAXORDER) || (order < D3DXSH_MINORDER))
3032 out[1] = matrix->u.m[1][1] * in[1] - matrix->u.m[2][1] * in[2] + matrix->u.m[0][1] * in[3];
3033 out[2] = -matrix->u.m[1][2] * in[1] + matrix->u.m[2][2] * in[2] - matrix->u.m[0][2] * in[3];
3034 out[3] = matrix->u.m[1][0] * in[1] - matrix->u.m[2][0] * in[2] + matrix->u.m[0][0] * in[3];
3039 matrix->u.m[1][0] * matrix->u.m[0][0], matrix->u.m[1][1] * matrix->u.m[0][1],
3040 matrix->u.m[1][1] * matrix->u.m[2][1], matrix->u.m[1][0] * matrix->u.m[2][0],
3041 matrix->u.m[2][0] * matrix->u.m[2][0], matrix->u.m[2][1] * matrix->u.m[2][1],
3042 matrix->u.m[0][0] * matrix->u.m[2][0], matrix->u.m[0][1] * matrix->u.m[2][1],
3043 matrix->u.m[0][1] * matrix->u.m[0][1], matrix->u.m[1][0] * matrix->u.m[1][0],
3044 matrix->u.m[1][1] * matrix->u.m[1][1], matrix->u.m[0][0] * matrix->u.m[0][0], };
3046 out[4] = (matrix->u.m[1][1] * matrix->u.m[0][0] + matrix->u.m[0][1] * matrix->u.m[1][0]) * in[4];
3047 out[4] -= (matrix->u.m[1][0] * matrix->u.m[2][1] + matrix->u.m[1][1] * matrix->u.m[2][0]) * in[5];
3048 out[4] += 1.7320508076f * matrix->u.m[2][0] * matrix->u.m[2][1] * in[6];
3049 out[4] -= (matrix->u.m[0][1] * matrix->u.m[2][0] + matrix->u.m[0][0] * matrix->u.m[2][1]) * in[7];
3050 out[4] += (matrix->u.m[0][0] * matrix->u.m[0][1] - matrix->u.m[1][0] * matrix->u.m[1][1]) * in[8];
3052 out[5] = (matrix->u.m[1][1] * matrix->u.m[2][2] + matrix->u.m[1][2] * matrix->u.m[2][1]) * in[5];
3053 out[5] -= (matrix->u.m[1][1] * matrix->u.m[0][2] + matrix->u.m[1][2] * matrix->u.m[0][1]) * in[4];
3054 out[5] -= 1.7320508076f * matrix->u.m[2][2] * matrix->u.m[2][1] * in[6];
3055 out[5] += (matrix->u.m[0][2] * matrix->u.m[2][1] + matrix->u.m[0][1] * matrix->u.m[2][2]) * in[7];
3056 out[5] -= (matrix->u.m[0][1] * matrix->u.m[0][2] - matrix->u.m[1][1] * matrix->u.m[1][2]) * in[8];
3058 out[6] = (matrix->u.m[2][2] * matrix->u.m[2][2] - 0.5f * (coeff[4] + coeff[5])) * in[6];
3059 out[6] -= (0.5773502692f * (coeff[0] + coeff[1]) - 1.1547005384f * matrix->u.m[1][2] * matrix->u.m[0][2]) * in[4];
3060 out[6] += (0.5773502692f * (coeff[2] + coeff[3]) - 1.1547005384f * matrix->u.m[1][2] * matrix->u.m[2][2]) * in[5];
3061 out[6] += (0.5773502692f * (coeff[6] + coeff[7]) - 1.1547005384f * matrix->u.m[0][2] * matrix->u.m[2][2]) * in[7];
3062 out[6] += (0.2886751347f * (coeff[9] - coeff[8] + coeff[10] - coeff[11]) - 0.5773502692f *
3063 (matrix->u.m[1][2] * matrix->u.m[1][2] - matrix->u.m[0][2] * matrix->u.m[0][2])) * in[8];
3065 out[7] = (matrix->u.m[0][0] * matrix->u.m[2][2] + matrix->u.m[0][2] * matrix->u.m[2][0]) * in[7];
3066 out[7] -= (matrix->u.m[1][0] * matrix->u.m[0][2] + matrix->u.m[1][2] * matrix->u.m[0][0]) * in[4];
3067 out[7] += (matrix->u.m[1][0] * matrix->u.m[2][2] + matrix->u.m[1][2] * matrix->u.m[2][0]) * in[5];
3068 out[7] -= 1.7320508076f * matrix->u.m[2][2] * matrix->u.m[2][0] * in[6];
3069 out[7] -= (matrix->u.m[0][0] * matrix->u.m[0][2] - matrix->u.m[1][0] * matrix->u.m[1][2]) * in[8];
3071 out[8] = 0.5f * (coeff[11] - coeff[8] - coeff[9] + coeff[10]) * in[8];
3072 out[8] += (coeff[0] - coeff[1]) * in[4];
3073 out[8] += (coeff[2] - coeff[3]) * in[5];
3074 out[8] += 0.86602540f * (coeff[4] - coeff[5]) * in[6];
3075 out[8] += (coeff[7] - coeff[6]) * in[7];
3081 if (fabsf(matrix->u.m[2][2]) != 1.0f)
3083 sinb = sqrtf(1.0f - matrix->u.m[2][2] * matrix->u.m[2][2]);
3084 alpha = atan2f(matrix->u.m[2][1] / sinb, matrix->u.m[2][0] / sinb);
3085 beta = atan2f(sinb, matrix->u.m[2][2]);
3086 gamma = atan2f(matrix->u.m[1][2] / sinb, -matrix->u.m[0][2] / sinb);
3090 alpha = atan2f(matrix->u.m[0][1], matrix->u.m[0][0]);
3095 D3DXSHRotateZ(temp, order, gamma, in);
3096 rotate_X(temp1, order, 1.0f, temp);
3097 D3DXSHRotateZ(temp, order, beta, temp1);
3098 rotate_X(temp1, order, -1.0f, temp);
3099 D3DXSHRotateZ(out, order, alpha, temp1);
3104 FLOAT * WINAPI D3DXSHRotateZ(FLOAT *out, UINT order, FLOAT angle, const FLOAT *in)
3109 TRACE("out %p, order %u, angle %f, in %p\n", out, order, angle, in);
3111 order = min(max(order, D3DXSH_MINORDER), D3DXSH_MAXORDER);
3115 for (i = 1; i < order; i++)
3119 c[i - 1] = cosf(i * angle);
3120 s[i - 1] = sinf(i * angle);
3123 out[sum - i] = c[i - 1] * in[sum - i];
3124 out[sum - i] += s[i - 1] * in[sum + i];
3125 for (j = i - 1; j > 0; j--)
3127 out[sum - j] = 0.0f;
3128 out[sum - j] = c[j - 1] * in[sum - j];
3129 out[sum - j] += s[j - 1] * in[sum + j];
3137 for (j = 1; j < i; j++)
3139 out[sum + j] = 0.0f;
3140 out[sum + j] = -s[j - 1] * in[sum - j];
3141 out[sum + j] += c[j - 1] * in[sum + j];
3143 out[sum + i] = -s[i - 1] * in[sum - i];
3144 out[sum + i] += c[i - 1] * in[sum + i];
3150 FLOAT* WINAPI D3DXSHScale(FLOAT *out, UINT order, const FLOAT *a, const FLOAT scale)
3154 TRACE("out %p, order %u, a %p, scale %f\n", out, order, a, scale);
3156 for (i = 0; i < order * order; i++)
3157 out[i] = a[i] * scale;