d3dx9: D3DXQuaternionToAxisAngle should not crash on NULLs in output parameters.
[wine.git] / dlls / d3dx9_36 / math.c
blob123bd2f859efc3a9099b03cd86ad6cff36ab3e7b
1 /*
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
27 #include "config.h"
28 #include "wine/port.h"
30 #include "windef.h"
31 #include "wingdi.h"
32 #include "d3dx9_36_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
38 struct ID3DXMatrixStackImpl
40 ID3DXMatrixStack ID3DXMatrixStack_iface;
41 LONG ref;
43 unsigned int current;
44 unsigned int stack_size;
45 D3DXMATRIX *stack;
48 static const unsigned int INITIAL_STACK_SIZE = 32;
50 /*_________________D3DXColor____________________*/
52 D3DXCOLOR* WINAPI D3DXColorAdjustContrast(D3DXCOLOR *pout, const D3DXCOLOR *pc, FLOAT s)
54 TRACE("pout %p, pc %p, s %f\n", pout, pc, s);
56 pout->r = 0.5f + s * (pc->r - 0.5f);
57 pout->g = 0.5f + s * (pc->g - 0.5f);
58 pout->b = 0.5f + s * (pc->b - 0.5f);
59 pout->a = pc->a;
60 return pout;
63 D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, const D3DXCOLOR *pc, FLOAT s)
65 FLOAT grey;
67 TRACE("pout %p, pc %p, s %f\n", pout, pc, s);
69 grey = pc->r * 0.2125f + pc->g * 0.7154f + pc->b * 0.0721f;
70 pout->r = grey + s * (pc->r - grey);
71 pout->g = grey + s * (pc->g - grey);
72 pout->b = grey + s * (pc->b - grey);
73 pout->a = pc->a;
74 return pout;
77 /*_________________Misc__________________________*/
79 FLOAT WINAPI D3DXFresnelTerm(FLOAT costheta, FLOAT refractionindex)
81 FLOAT a, d, g, result;
83 TRACE("costheta %f, refractionindex %f\n", costheta, refractionindex);
85 g = sqrtf(refractionindex * refractionindex + costheta * costheta - 1.0f);
86 a = g + costheta;
87 d = g - costheta;
88 result = (costheta * a - 1.0f) * (costheta * a - 1.0f) / ((costheta * d + 1.0f) * (costheta * d + 1.0f)) + 1.0f;
89 result *= 0.5f * d * d / (a * a);
91 return result;
94 /*_________________D3DXMatrix____________________*/
96 D3DXMATRIX * WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *out, FLOAT scaling, const D3DXVECTOR3 *rotationcenter,
97 const D3DXQUATERNION *rotation, const D3DXVECTOR3 *translation)
99 TRACE("out %p, scaling %f, rotationcenter %p, rotation %p, translation %p\n",
100 out, scaling, rotationcenter, rotation, translation);
102 D3DXMatrixIdentity(out);
104 if (rotation)
106 FLOAT temp00, temp01, temp02, temp10, temp11, temp12, temp20, temp21, temp22;
108 temp00 = 1.0f - 2.0f * (rotation->y * rotation->y + rotation->z * rotation->z);
109 temp01 = 2.0f * (rotation->x * rotation->y + rotation->z * rotation->w);
110 temp02 = 2.0f * (rotation->x * rotation->z - rotation->y * rotation->w);
111 temp10 = 2.0f * (rotation->x * rotation->y - rotation->z * rotation->w);
112 temp11 = 1.0f - 2.0f * (rotation->x * rotation->x + rotation->z * rotation->z);
113 temp12 = 2.0f * (rotation->y * rotation->z + rotation->x * rotation->w);
114 temp20 = 2.0f * (rotation->x * rotation->z + rotation->y * rotation->w);
115 temp21 = 2.0f * (rotation->y * rotation->z - rotation->x * rotation->w);
116 temp22 = 1.0f - 2.0f * (rotation->x * rotation->x + rotation->y * rotation->y);
118 out->u.m[0][0] = scaling * temp00;
119 out->u.m[0][1] = scaling * temp01;
120 out->u.m[0][2] = scaling * temp02;
121 out->u.m[1][0] = scaling * temp10;
122 out->u.m[1][1] = scaling * temp11;
123 out->u.m[1][2] = scaling * temp12;
124 out->u.m[2][0] = scaling * temp20;
125 out->u.m[2][1] = scaling * temp21;
126 out->u.m[2][2] = scaling * temp22;
128 if (rotationcenter)
130 out->u.m[3][0] = rotationcenter->x * (1.0f - temp00) - rotationcenter->y * temp10
131 - rotationcenter->z * temp20;
132 out->u.m[3][1] = rotationcenter->y * (1.0f - temp11) - rotationcenter->x * temp01
133 - rotationcenter->z * temp21;
134 out->u.m[3][2] = rotationcenter->z * (1.0f - temp22) - rotationcenter->x * temp02
135 - rotationcenter->y * temp12;
138 else
140 out->u.m[0][0] = scaling;
141 out->u.m[1][1] = scaling;
142 out->u.m[2][2] = scaling;
145 if (translation)
147 out->u.m[3][0] += translation->x;
148 out->u.m[3][1] += translation->y;
149 out->u.m[3][2] += translation->z;
152 return out;
155 D3DXMATRIX * WINAPI D3DXMatrixAffineTransformation2D(D3DXMATRIX *out, FLOAT scaling,
156 const D3DXVECTOR2 *rotationcenter, FLOAT rotation, const D3DXVECTOR2 *translation)
158 FLOAT tmp1, tmp2, s;
160 TRACE("out %p, scaling %f, rotationcenter %p, rotation %f, translation %p\n",
161 out, scaling, rotationcenter, rotation, translation);
163 s = sinf(rotation / 2.0f);
164 tmp1 = 1.0f - 2.0f * s * s;
165 tmp2 = 2.0f * s * cosf(rotation / 2.0f);
167 D3DXMatrixIdentity(out);
168 out->u.m[0][0] = scaling * tmp1;
169 out->u.m[0][1] = scaling * tmp2;
170 out->u.m[1][0] = -scaling * tmp2;
171 out->u.m[1][1] = scaling * tmp1;
173 if (rotationcenter)
175 FLOAT x, y;
177 x = rotationcenter->x;
178 y = rotationcenter->y;
180 out->u.m[3][0] = y * tmp2 - x * tmp1 + x;
181 out->u.m[3][1] = -x * tmp2 - y * tmp1 + y;
184 if (translation)
186 out->u.m[3][0] += translation->x;
187 out->u.m[3][1] += translation->y;
190 return out;
193 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, const D3DXMATRIX *pm)
195 D3DXMATRIX normalized;
196 D3DXVECTOR3 vec;
198 TRACE("poutscale %p, poutrotation %p, pouttranslation %p, pm %p\n", poutscale, poutrotation, pouttranslation, pm);
200 /*Compute the scaling part.*/
201 vec.x=pm->u.m[0][0];
202 vec.y=pm->u.m[0][1];
203 vec.z=pm->u.m[0][2];
204 poutscale->x=D3DXVec3Length(&vec);
206 vec.x=pm->u.m[1][0];
207 vec.y=pm->u.m[1][1];
208 vec.z=pm->u.m[1][2];
209 poutscale->y=D3DXVec3Length(&vec);
211 vec.x=pm->u.m[2][0];
212 vec.y=pm->u.m[2][1];
213 vec.z=pm->u.m[2][2];
214 poutscale->z=D3DXVec3Length(&vec);
216 /*Compute the translation part.*/
217 pouttranslation->x=pm->u.m[3][0];
218 pouttranslation->y=pm->u.m[3][1];
219 pouttranslation->z=pm->u.m[3][2];
221 /*Let's calculate the rotation now*/
222 if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) ) return D3DERR_INVALIDCALL;
224 normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
225 normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
226 normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
227 normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
228 normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
229 normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
230 normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
231 normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
232 normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
234 D3DXQuaternionRotationMatrix(poutrotation,&normalized);
235 return S_OK;
238 FLOAT WINAPI D3DXMatrixDeterminant(const D3DXMATRIX *pm)
240 FLOAT t[3], v[4];
242 TRACE("pm %p\n", pm);
244 t[0] = pm->u.m[2][2] * pm->u.m[3][3] - pm->u.m[2][3] * pm->u.m[3][2];
245 t[1] = pm->u.m[1][2] * pm->u.m[3][3] - pm->u.m[1][3] * pm->u.m[3][2];
246 t[2] = pm->u.m[1][2] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][2];
247 v[0] = pm->u.m[1][1] * t[0] - pm->u.m[2][1] * t[1] + pm->u.m[3][1] * t[2];
248 v[1] = -pm->u.m[1][0] * t[0] + pm->u.m[2][0] * t[1] - pm->u.m[3][0] * t[2];
250 t[0] = pm->u.m[1][0] * pm->u.m[2][1] - pm->u.m[2][0] * pm->u.m[1][1];
251 t[1] = pm->u.m[1][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[1][1];
252 t[2] = pm->u.m[2][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[2][1];
253 v[2] = pm->u.m[3][3] * t[0] - pm->u.m[2][3] * t[1] + pm->u.m[1][3] * t[2];
254 v[3] = -pm->u.m[3][2] * t[0] + pm->u.m[2][2] * t[1] - pm->u.m[1][2] * t[2];
256 return pm->u.m[0][0] * v[0] + pm->u.m[0][1] * v[1] +
257 pm->u.m[0][2] * v[2] + pm->u.m[0][3] * v[3];
260 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, const D3DXMATRIX *pm)
262 FLOAT det, t[3], v[16];
263 UINT i, j;
265 TRACE("pout %p, pdeterminant %p, pm %p\n", pout, pdeterminant, pm);
267 t[0] = pm->u.m[2][2] * pm->u.m[3][3] - pm->u.m[2][3] * pm->u.m[3][2];
268 t[1] = pm->u.m[1][2] * pm->u.m[3][3] - pm->u.m[1][3] * pm->u.m[3][2];
269 t[2] = pm->u.m[1][2] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][2];
270 v[0] = pm->u.m[1][1] * t[0] - pm->u.m[2][1] * t[1] + pm->u.m[3][1] * t[2];
271 v[4] = -pm->u.m[1][0] * t[0] + pm->u.m[2][0] * t[1] - pm->u.m[3][0] * t[2];
273 t[0] = pm->u.m[1][0] * pm->u.m[2][1] - pm->u.m[2][0] * pm->u.m[1][1];
274 t[1] = pm->u.m[1][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[1][1];
275 t[2] = pm->u.m[2][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[2][1];
276 v[8] = pm->u.m[3][3] * t[0] - pm->u.m[2][3] * t[1] + pm->u.m[1][3] * t[2];
277 v[12] = -pm->u.m[3][2] * t[0] + pm->u.m[2][2] * t[1] - pm->u.m[1][2] * t[2];
279 det = pm->u.m[0][0] * v[0] + pm->u.m[0][1] * v[4] +
280 pm->u.m[0][2] * v[8] + pm->u.m[0][3] * v[12];
281 if (det == 0.0f)
282 return NULL;
283 if (pdeterminant)
284 *pdeterminant = det;
286 t[0] = pm->u.m[2][2] * pm->u.m[3][3] - pm->u.m[2][3] * pm->u.m[3][2];
287 t[1] = pm->u.m[0][2] * pm->u.m[3][3] - pm->u.m[0][3] * pm->u.m[3][2];
288 t[2] = pm->u.m[0][2] * pm->u.m[2][3] - pm->u.m[0][3] * pm->u.m[2][2];
289 v[1] = -pm->u.m[0][1] * t[0] + pm->u.m[2][1] * t[1] - pm->u.m[3][1] * t[2];
290 v[5] = pm->u.m[0][0] * t[0] - pm->u.m[2][0] * t[1] + pm->u.m[3][0] * t[2];
292 t[0] = pm->u.m[0][0] * pm->u.m[2][1] - pm->u.m[2][0] * pm->u.m[0][1];
293 t[1] = pm->u.m[3][0] * pm->u.m[0][1] - pm->u.m[0][0] * pm->u.m[3][1];
294 t[2] = pm->u.m[2][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[2][1];
295 v[9] = -pm->u.m[3][3] * t[0] - pm->u.m[2][3] * t[1]- pm->u.m[0][3] * t[2];
296 v[13] = pm->u.m[3][2] * t[0] + pm->u.m[2][2] * t[1] + pm->u.m[0][2] * t[2];
298 t[0] = pm->u.m[1][2] * pm->u.m[3][3] - pm->u.m[1][3] * pm->u.m[3][2];
299 t[1] = pm->u.m[0][2] * pm->u.m[3][3] - pm->u.m[0][3] * pm->u.m[3][2];
300 t[2] = pm->u.m[0][2] * pm->u.m[1][3] - pm->u.m[0][3] * pm->u.m[1][2];
301 v[2] = pm->u.m[0][1] * t[0] - pm->u.m[1][1] * t[1] + pm->u.m[3][1] * t[2];
302 v[6] = -pm->u.m[0][0] * t[0] + pm->u.m[1][0] * t[1] - pm->u.m[3][0] * t[2];
304 t[0] = pm->u.m[0][0] * pm->u.m[1][1] - pm->u.m[1][0] * pm->u.m[0][1];
305 t[1] = pm->u.m[3][0] * pm->u.m[0][1] - pm->u.m[0][0] * pm->u.m[3][1];
306 t[2] = pm->u.m[1][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[1][1];
307 v[10] = pm->u.m[3][3] * t[0] + pm->u.m[1][3] * t[1] + pm->u.m[0][3] * t[2];
308 v[14] = -pm->u.m[3][2] * t[0] - pm->u.m[1][2] * t[1] - pm->u.m[0][2] * t[2];
310 t[0] = pm->u.m[1][2] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][2];
311 t[1] = pm->u.m[0][2] * pm->u.m[2][3] - pm->u.m[0][3] * pm->u.m[2][2];
312 t[2] = pm->u.m[0][2] * pm->u.m[1][3] - pm->u.m[0][3] * pm->u.m[1][2];
313 v[3] = -pm->u.m[0][1] * t[0] + pm->u.m[1][1] * t[1] - pm->u.m[2][1] * t[2];
314 v[7] = pm->u.m[0][0] * t[0] - pm->u.m[1][0] * t[1] + pm->u.m[2][0] * t[2];
316 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]) +
317 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]) -
318 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]);
320 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]) -
321 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]) +
322 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]);
324 det = 1.0f / det;
326 for (i = 0; i < 4; i++)
327 for (j = 0; j < 4; j++)
328 pout->u.m[i][j] = v[4 * i + j] * det;
330 return pout;
333 D3DXMATRIX * WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *out, const D3DXVECTOR3 *eye, const D3DXVECTOR3 *at,
334 const D3DXVECTOR3 *up)
336 D3DXVECTOR3 right, upn, vec;
338 TRACE("out %p, eye %p, at %p, up %p\n", out, eye, at, up);
340 D3DXVec3Subtract(&vec, at, eye);
341 D3DXVec3Normalize(&vec, &vec);
342 D3DXVec3Cross(&right, up, &vec);
343 D3DXVec3Cross(&upn, &vec, &right);
344 D3DXVec3Normalize(&right, &right);
345 D3DXVec3Normalize(&upn, &upn);
346 out->u.m[0][0] = right.x;
347 out->u.m[1][0] = right.y;
348 out->u.m[2][0] = right.z;
349 out->u.m[3][0] = -D3DXVec3Dot(&right, eye);
350 out->u.m[0][1] = upn.x;
351 out->u.m[1][1] = upn.y;
352 out->u.m[2][1] = upn.z;
353 out->u.m[3][1] = -D3DXVec3Dot(&upn, eye);
354 out->u.m[0][2] = vec.x;
355 out->u.m[1][2] = vec.y;
356 out->u.m[2][2] = vec.z;
357 out->u.m[3][2] = -D3DXVec3Dot(&vec, eye);
358 out->u.m[0][3] = 0.0f;
359 out->u.m[1][3] = 0.0f;
360 out->u.m[2][3] = 0.0f;
361 out->u.m[3][3] = 1.0f;
363 return out;
366 D3DXMATRIX * WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *out, const D3DXVECTOR3 *eye, const D3DXVECTOR3 *at,
367 const D3DXVECTOR3 *up)
369 D3DXVECTOR3 right, upn, vec;
371 TRACE("out %p, eye %p, at %p, up %p\n", out, eye, at, up);
373 D3DXVec3Subtract(&vec, at, eye);
374 D3DXVec3Normalize(&vec, &vec);
375 D3DXVec3Cross(&right, up, &vec);
376 D3DXVec3Cross(&upn, &vec, &right);
377 D3DXVec3Normalize(&right, &right);
378 D3DXVec3Normalize(&upn, &upn);
379 out->u.m[0][0] = -right.x;
380 out->u.m[1][0] = -right.y;
381 out->u.m[2][0] = -right.z;
382 out->u.m[3][0] = D3DXVec3Dot(&right, eye);
383 out->u.m[0][1] = upn.x;
384 out->u.m[1][1] = upn.y;
385 out->u.m[2][1] = upn.z;
386 out->u.m[3][1] = -D3DXVec3Dot(&upn, eye);
387 out->u.m[0][2] = -vec.x;
388 out->u.m[1][2] = -vec.y;
389 out->u.m[2][2] = -vec.z;
390 out->u.m[3][2] = D3DXVec3Dot(&vec, eye);
391 out->u.m[0][3] = 0.0f;
392 out->u.m[1][3] = 0.0f;
393 out->u.m[2][3] = 0.0f;
394 out->u.m[3][3] = 1.0f;
396 return out;
399 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, const D3DXMATRIX *pm1, const D3DXMATRIX *pm2)
401 D3DXMATRIX out;
402 int i,j;
404 TRACE("pout %p, pm1 %p, pm2 %p\n", pout, pm1, pm2);
406 for (i=0; i<4; i++)
408 for (j=0; j<4; j++)
410 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];
414 *pout = out;
415 return pout;
418 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, const D3DXMATRIX *pm1, const D3DXMATRIX *pm2)
420 D3DXMATRIX temp;
421 int i, j;
423 TRACE("pout %p, pm1 %p, pm2 %p\n", pout, pm1, pm2);
425 for (i = 0; i < 4; i++)
426 for (j = 0; j < 4; j++)
427 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];
429 *pout = temp;
430 return pout;
433 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
435 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
437 D3DXMatrixIdentity(pout);
438 pout->u.m[0][0] = 2.0f / w;
439 pout->u.m[1][1] = 2.0f / h;
440 pout->u.m[2][2] = 1.0f / (zf - zn);
441 pout->u.m[3][2] = zn / (zn - zf);
442 return pout;
445 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
447 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
449 D3DXMatrixIdentity(pout);
450 pout->u.m[0][0] = 2.0f / (r - l);
451 pout->u.m[1][1] = 2.0f / (t - b);
452 pout->u.m[2][2] = 1.0f / (zf -zn);
453 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
454 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
455 pout->u.m[3][2] = zn / (zn -zf);
456 return pout;
459 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(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 / (zn -zf);
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);
470 return pout;
473 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
475 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
477 D3DXMatrixIdentity(pout);
478 pout->u.m[0][0] = 2.0f / w;
479 pout->u.m[1][1] = 2.0f / h;
480 pout->u.m[2][2] = 1.0f / (zn - zf);
481 pout->u.m[3][2] = zn / (zn - zf);
482 return pout;
485 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
487 TRACE("pout %p, fovy %f, aspect %f, zn %f, zf %f\n", pout, fovy, aspect, zn, zf);
489 D3DXMatrixIdentity(pout);
490 pout->u.m[0][0] = 1.0f / (aspect * tanf(fovy/2.0f));
491 pout->u.m[1][1] = 1.0f / tanf(fovy/2.0f);
492 pout->u.m[2][2] = zf / (zf - zn);
493 pout->u.m[2][3] = 1.0f;
494 pout->u.m[3][2] = (zf * zn) / (zn - zf);
495 pout->u.m[3][3] = 0.0f;
496 return pout;
499 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(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 * tanf(fovy/2.0f));
505 pout->u.m[1][1] = 1.0f / tanf(fovy/2.0f);
506 pout->u.m[2][2] = zf / (zn - zf);
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;
510 return pout;
513 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
515 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
517 D3DXMatrixIdentity(pout);
518 pout->u.m[0][0] = 2.0f * zn / w;
519 pout->u.m[1][1] = 2.0f * zn / h;
520 pout->u.m[2][2] = zf / (zf - zn);
521 pout->u.m[3][2] = (zn * zf) / (zn - zf);
522 pout->u.m[2][3] = 1.0f;
523 pout->u.m[3][3] = 0.0f;
524 return pout;
527 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
529 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
531 D3DXMatrixIdentity(pout);
532 pout->u.m[0][0] = 2.0f * zn / (r - l);
533 pout->u.m[1][1] = -2.0f * zn / (b - t);
534 pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
535 pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
536 pout->u.m[2][2] = - zf / (zn - zf);
537 pout->u.m[3][2] = (zn * zf) / (zn -zf);
538 pout->u.m[2][3] = 1.0f;
539 pout->u.m[3][3] = 0.0f;
540 return pout;
543 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
545 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
547 D3DXMatrixIdentity(pout);
548 pout->u.m[0][0] = 2.0f * zn / (r - l);
549 pout->u.m[1][1] = -2.0f * zn / (b - t);
550 pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
551 pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
552 pout->u.m[2][2] = zf / (zn - zf);
553 pout->u.m[3][2] = (zn * zf) / (zn -zf);
554 pout->u.m[2][3] = -1.0f;
555 pout->u.m[3][3] = 0.0f;
556 return pout;
559 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
561 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
563 D3DXMatrixIdentity(pout);
564 pout->u.m[0][0] = 2.0f * zn / w;
565 pout->u.m[1][1] = 2.0f * zn / h;
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;
570 return pout;
573 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, const D3DXPLANE *pplane)
575 D3DXPLANE Nplane;
577 TRACE("pout %p, pplane %p\n", pout, pplane);
579 D3DXPlaneNormalize(&Nplane, pplane);
580 D3DXMatrixIdentity(pout);
581 pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
582 pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
583 pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
584 pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
585 pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
586 pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
587 pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
588 pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
589 pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
590 pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
591 pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
592 pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
593 return pout;
596 D3DXMATRIX * WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *out, const D3DXVECTOR3 *v, FLOAT angle)
598 D3DXVECTOR3 nv;
599 FLOAT sangle, cangle, cdiff;
601 TRACE("out %p, v %p, angle %f\n", out, v, angle);
603 D3DXVec3Normalize(&nv, v);
604 sangle = sinf(angle);
605 cangle = cosf(angle);
606 cdiff = 1.0f - cangle;
608 out->u.m[0][0] = cdiff * nv.x * nv.x + cangle;
609 out->u.m[1][0] = cdiff * nv.x * nv.y - sangle * nv.z;
610 out->u.m[2][0] = cdiff * nv.x * nv.z + sangle * nv.y;
611 out->u.m[3][0] = 0.0f;
612 out->u.m[0][1] = cdiff * nv.y * nv.x + sangle * nv.z;
613 out->u.m[1][1] = cdiff * nv.y * nv.y + cangle;
614 out->u.m[2][1] = cdiff * nv.y * nv.z - sangle * nv.x;
615 out->u.m[3][1] = 0.0f;
616 out->u.m[0][2] = cdiff * nv.z * nv.x - sangle * nv.y;
617 out->u.m[1][2] = cdiff * nv.z * nv.y + sangle * nv.x;
618 out->u.m[2][2] = cdiff * nv.z * nv.z + cangle;
619 out->u.m[3][2] = 0.0f;
620 out->u.m[0][3] = 0.0f;
621 out->u.m[1][3] = 0.0f;
622 out->u.m[2][3] = 0.0f;
623 out->u.m[3][3] = 1.0f;
625 return out;
628 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, const D3DXQUATERNION *pq)
630 TRACE("pout %p, pq %p\n", pout, pq);
632 D3DXMatrixIdentity(pout);
633 pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
634 pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
635 pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
636 pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
637 pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
638 pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
639 pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
640 pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
641 pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
642 return pout;
645 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
647 TRACE("pout %p, angle %f\n", pout, angle);
649 D3DXMatrixIdentity(pout);
650 pout->u.m[1][1] = cosf(angle);
651 pout->u.m[2][2] = cosf(angle);
652 pout->u.m[1][2] = sinf(angle);
653 pout->u.m[2][1] = -sinf(angle);
654 return pout;
657 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
659 TRACE("pout %p, angle %f\n", pout, angle);
661 D3DXMatrixIdentity(pout);
662 pout->u.m[0][0] = cosf(angle);
663 pout->u.m[2][2] = cosf(angle);
664 pout->u.m[0][2] = -sinf(angle);
665 pout->u.m[2][0] = sinf(angle);
666 return pout;
669 D3DXMATRIX * WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *out, FLOAT yaw, FLOAT pitch, FLOAT roll)
671 FLOAT sroll, croll, spitch, cpitch, syaw, cyaw;
673 TRACE("out %p, yaw %f, pitch %f, roll %f\n", out, yaw, pitch, roll);
675 sroll = sinf(roll);
676 croll = cosf(roll);
677 spitch = sinf(pitch);
678 cpitch = cosf(pitch);
679 syaw = sinf(yaw);
680 cyaw = cosf(yaw);
682 out->u.m[0][0] = sroll * spitch * syaw + croll * cyaw;
683 out->u.m[0][1] = sroll * cpitch;
684 out->u.m[0][2] = sroll * spitch * cyaw - croll * syaw;
685 out->u.m[0][3] = 0.0f;
686 out->u.m[1][0] = croll * spitch * syaw - sroll * cyaw;
687 out->u.m[1][1] = croll * cpitch;
688 out->u.m[1][2] = croll * spitch * cyaw + sroll * syaw;
689 out->u.m[1][3] = 0.0f;
690 out->u.m[2][0] = cpitch * syaw;
691 out->u.m[2][1] = -spitch;
692 out->u.m[2][2] = cpitch * cyaw;
693 out->u.m[2][3] = 0.0f;
694 out->u.m[3][0] = 0.0f;
695 out->u.m[3][1] = 0.0f;
696 out->u.m[3][2] = 0.0f;
697 out->u.m[3][3] = 1.0f;
699 return out;
702 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
704 TRACE("pout %p, angle %f\n", pout, angle);
706 D3DXMatrixIdentity(pout);
707 pout->u.m[0][0] = cosf(angle);
708 pout->u.m[1][1] = cosf(angle);
709 pout->u.m[0][1] = sinf(angle);
710 pout->u.m[1][0] = -sinf(angle);
711 return pout;
714 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
716 TRACE("pout %p, sx %f, sy %f, sz %f\n", pout, sx, sy, sz);
718 D3DXMatrixIdentity(pout);
719 pout->u.m[0][0] = sx;
720 pout->u.m[1][1] = sy;
721 pout->u.m[2][2] = sz;
722 return pout;
725 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, const D3DXVECTOR4 *plight, const D3DXPLANE *pplane)
727 D3DXPLANE Nplane;
728 FLOAT dot;
730 TRACE("pout %p, plight %p, pplane %p\n", pout, plight, pplane);
732 D3DXPlaneNormalize(&Nplane, pplane);
733 dot = D3DXPlaneDot(&Nplane, plight);
734 pout->u.m[0][0] = dot - Nplane.a * plight->x;
735 pout->u.m[0][1] = -Nplane.a * plight->y;
736 pout->u.m[0][2] = -Nplane.a * plight->z;
737 pout->u.m[0][3] = -Nplane.a * plight->w;
738 pout->u.m[1][0] = -Nplane.b * plight->x;
739 pout->u.m[1][1] = dot - Nplane.b * plight->y;
740 pout->u.m[1][2] = -Nplane.b * plight->z;
741 pout->u.m[1][3] = -Nplane.b * plight->w;
742 pout->u.m[2][0] = -Nplane.c * plight->x;
743 pout->u.m[2][1] = -Nplane.c * plight->y;
744 pout->u.m[2][2] = dot - Nplane.c * plight->z;
745 pout->u.m[2][3] = -Nplane.c * plight->w;
746 pout->u.m[3][0] = -Nplane.d * plight->x;
747 pout->u.m[3][1] = -Nplane.d * plight->y;
748 pout->u.m[3][2] = -Nplane.d * plight->z;
749 pout->u.m[3][3] = dot - Nplane.d * plight->w;
750 return pout;
753 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, const D3DXVECTOR3 *pscalingcenter, const D3DXQUATERNION *pscalingrotation, const D3DXVECTOR3 *pscaling, const D3DXVECTOR3 *protationcenter, const D3DXQUATERNION *protation, const D3DXVECTOR3 *ptranslation)
755 D3DXMATRIX m1, m2, m3, m4, m5, m6, m7;
756 D3DXQUATERNION prc;
757 D3DXVECTOR3 psc, pt;
759 TRACE("pout %p, pscalingcenter %p, pscalingrotation %p, pscaling %p, protationcentr %p, protation %p, ptranslation %p\n",
760 pout, pscalingcenter, pscalingrotation, pscaling, protationcenter, protation, ptranslation);
762 if ( !pscalingcenter )
764 psc.x = 0.0f;
765 psc.y = 0.0f;
766 psc.z = 0.0f;
768 else
770 psc.x = pscalingcenter->x;
771 psc.y = pscalingcenter->y;
772 psc.z = pscalingcenter->z;
775 if ( !protationcenter )
777 prc.x = 0.0f;
778 prc.y = 0.0f;
779 prc.z = 0.0f;
781 else
783 prc.x = protationcenter->x;
784 prc.y = protationcenter->y;
785 prc.z = protationcenter->z;
788 if ( !ptranslation )
790 pt.x = 0.0f;
791 pt.y = 0.0f;
792 pt.z = 0.0f;
794 else
796 pt.x = ptranslation->x;
797 pt.y = ptranslation->y;
798 pt.z = ptranslation->z;
801 D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
803 if ( !pscalingrotation )
805 D3DXMatrixIdentity(&m2);
806 D3DXMatrixIdentity(&m4);
808 else
810 D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
811 D3DXMatrixInverse(&m2, NULL, &m4);
814 if ( !pscaling ) D3DXMatrixIdentity(&m3);
815 else D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
817 if ( !protation ) D3DXMatrixIdentity(&m6);
818 else D3DXMatrixRotationQuaternion(&m6, protation);
820 D3DXMatrixTranslation(&m5, psc.x - prc.x, psc.y - prc.y, psc.z - prc.z);
821 D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
822 D3DXMatrixMultiply(&m1, &m1, &m2);
823 D3DXMatrixMultiply(&m1, &m1, &m3);
824 D3DXMatrixMultiply(&m1, &m1, &m4);
825 D3DXMatrixMultiply(&m1, &m1, &m5);
826 D3DXMatrixMultiply(&m1, &m1, &m6);
827 D3DXMatrixMultiply(pout, &m1, &m7);
828 return pout;
831 D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(D3DXMATRIX *pout, const D3DXVECTOR2 *pscalingcenter, FLOAT scalingrotation, const D3DXVECTOR2 *pscaling, const D3DXVECTOR2 *protationcenter, FLOAT rotation, const D3DXVECTOR2 *ptranslation)
833 D3DXQUATERNION rot, sca_rot;
834 D3DXVECTOR3 rot_center, sca, sca_center, trans;
836 TRACE("pout %p, pscalingcenter %p, scalingrotation %f, pscaling %p, protztioncenter %p, rotation %f, ptranslation %p\n",
837 pout, pscalingcenter, scalingrotation, pscaling, protationcenter, rotation, ptranslation);
839 if ( pscalingcenter )
841 sca_center.x=pscalingcenter->x;
842 sca_center.y=pscalingcenter->y;
843 sca_center.z=0.0f;
845 else
847 sca_center.x=0.0f;
848 sca_center.y=0.0f;
849 sca_center.z=0.0f;
852 if ( pscaling )
854 sca.x=pscaling->x;
855 sca.y=pscaling->y;
856 sca.z=1.0f;
858 else
860 sca.x=1.0f;
861 sca.y=1.0f;
862 sca.z=1.0f;
865 if ( protationcenter )
867 rot_center.x=protationcenter->x;
868 rot_center.y=protationcenter->y;
869 rot_center.z=0.0f;
871 else
873 rot_center.x=0.0f;
874 rot_center.y=0.0f;
875 rot_center.z=0.0f;
878 if ( ptranslation )
880 trans.x=ptranslation->x;
881 trans.y=ptranslation->y;
882 trans.z=0.0f;
884 else
886 trans.x=0.0f;
887 trans.y=0.0f;
888 trans.z=0.0f;
891 rot.w=cosf(rotation/2.0f);
892 rot.x=0.0f;
893 rot.y=0.0f;
894 rot.z=sinf(rotation/2.0f);
896 sca_rot.w=cosf(scalingrotation/2.0f);
897 sca_rot.x=0.0f;
898 sca_rot.y=0.0f;
899 sca_rot.z=sinf(scalingrotation/2.0f);
901 D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
903 return pout;
906 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
908 TRACE("pout %p, x %f, y %f, z %f\n", pout, x, y, z);
910 D3DXMatrixIdentity(pout);
911 pout->u.m[3][0] = x;
912 pout->u.m[3][1] = y;
913 pout->u.m[3][2] = z;
914 return pout;
917 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, const D3DXMATRIX *pm)
919 const D3DXMATRIX m = *pm;
920 int i,j;
922 TRACE("pout %p, pm %p\n", pout, pm);
924 for (i=0; i<4; i++)
925 for (j=0; j<4; j++) pout->u.m[i][j] = m.u.m[j][i];
927 return pout;
930 /*_________________D3DXMatrixStack____________________*/
933 static inline struct ID3DXMatrixStackImpl *impl_from_ID3DXMatrixStack(ID3DXMatrixStack *iface)
935 return CONTAINING_RECORD(iface, struct ID3DXMatrixStackImpl, ID3DXMatrixStack_iface);
938 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **out)
940 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
942 if (IsEqualGUID(riid, &IID_ID3DXMatrixStack)
943 || IsEqualGUID(riid, &IID_IUnknown))
945 ID3DXMatrixStack_AddRef(iface);
946 *out = iface;
947 return S_OK;
950 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
952 *out = NULL;
953 return E_NOINTERFACE;
956 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
958 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
959 ULONG ref = InterlockedIncrement(&This->ref);
960 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
961 return ref;
964 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack *iface)
966 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
967 ULONG ref = InterlockedDecrement(&This->ref);
968 if (!ref)
970 HeapFree(GetProcessHeap(), 0, This->stack);
971 HeapFree(GetProcessHeap(), 0, This);
973 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
974 return ref;
977 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
979 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
981 TRACE("iface %p\n", iface);
983 return &This->stack[This->current];
986 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
988 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
990 TRACE("iface %p\n", iface);
992 D3DXMatrixIdentity(&This->stack[This->current]);
994 return D3D_OK;
997 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
999 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1001 TRACE("iface %p, pm %p\n", iface, pm);
1003 This->stack[This->current] = *pm;
1005 return D3D_OK;
1008 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
1010 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1012 TRACE("iface %p, pm %p\n", iface, pm);
1014 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], pm);
1016 return D3D_OK;
1019 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
1021 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1023 TRACE("iface %p, pm %p\n", iface, pm);
1025 D3DXMatrixMultiply(&This->stack[This->current], pm, &This->stack[This->current]);
1027 return D3D_OK;
1030 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
1032 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1034 TRACE("iface %p\n", iface);
1036 /* Popping the last element on the stack returns D3D_OK, but does nothing. */
1037 if (!This->current) return D3D_OK;
1039 if (This->current <= This->stack_size / 4 && This->stack_size >= INITIAL_STACK_SIZE * 2)
1041 unsigned int new_size;
1042 D3DXMATRIX *new_stack;
1044 new_size = This->stack_size / 2;
1045 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1046 if (new_stack)
1048 This->stack_size = new_size;
1049 This->stack = new_stack;
1053 --This->current;
1055 return D3D_OK;
1058 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
1060 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1062 TRACE("iface %p\n", iface);
1064 if (This->current == This->stack_size - 1)
1066 unsigned int new_size;
1067 D3DXMATRIX *new_stack;
1069 if (This->stack_size > UINT_MAX / 2) return E_OUTOFMEMORY;
1071 new_size = This->stack_size * 2;
1072 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1073 if (!new_stack) return E_OUTOFMEMORY;
1075 This->stack_size = new_size;
1076 This->stack = new_stack;
1079 ++This->current;
1080 This->stack[This->current] = This->stack[This->current - 1];
1082 return D3D_OK;
1085 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, const D3DXVECTOR3 *pv, FLOAT angle)
1087 D3DXMATRIX temp;
1088 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1090 TRACE("iface %p, pv %p, angle %f\n", iface, pv, angle);
1092 D3DXMatrixRotationAxis(&temp, pv, angle);
1093 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1095 return D3D_OK;
1098 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, const D3DXVECTOR3 *pv, FLOAT angle)
1100 D3DXMATRIX temp;
1101 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1103 TRACE("iface %p, pv %p, angle %f\n", iface, pv, angle);
1105 D3DXMatrixRotationAxis(&temp, pv, angle);
1106 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1108 return D3D_OK;
1111 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1113 D3DXMATRIX temp;
1114 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1116 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1118 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1119 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1121 return D3D_OK;
1124 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1126 D3DXMATRIX temp;
1127 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1129 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1131 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1132 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1134 return D3D_OK;
1137 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1139 D3DXMATRIX temp;
1140 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1142 TRACE("iface %p,x %f, y %f, z %f\n", iface, x, y, z);
1144 D3DXMatrixScaling(&temp, x, y, z);
1145 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1147 return D3D_OK;
1150 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1152 D3DXMATRIX temp;
1153 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1155 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1157 D3DXMatrixScaling(&temp, x, y, z);
1158 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1160 return D3D_OK;
1163 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1165 D3DXMATRIX temp;
1166 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1168 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1170 D3DXMatrixTranslation(&temp, x, y, z);
1171 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1173 return D3D_OK;
1176 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1178 D3DXMATRIX temp;
1179 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1181 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1183 D3DXMatrixTranslation(&temp, x, y, z);
1184 D3DXMatrixMultiply(&This->stack[This->current], &temp,&This->stack[This->current]);
1186 return D3D_OK;
1189 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
1191 ID3DXMatrixStackImpl_QueryInterface,
1192 ID3DXMatrixStackImpl_AddRef,
1193 ID3DXMatrixStackImpl_Release,
1194 ID3DXMatrixStackImpl_Pop,
1195 ID3DXMatrixStackImpl_Push,
1196 ID3DXMatrixStackImpl_LoadIdentity,
1197 ID3DXMatrixStackImpl_LoadMatrix,
1198 ID3DXMatrixStackImpl_MultMatrix,
1199 ID3DXMatrixStackImpl_MultMatrixLocal,
1200 ID3DXMatrixStackImpl_RotateAxis,
1201 ID3DXMatrixStackImpl_RotateAxisLocal,
1202 ID3DXMatrixStackImpl_RotateYawPitchRoll,
1203 ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
1204 ID3DXMatrixStackImpl_Scale,
1205 ID3DXMatrixStackImpl_ScaleLocal,
1206 ID3DXMatrixStackImpl_Translate,
1207 ID3DXMatrixStackImpl_TranslateLocal,
1208 ID3DXMatrixStackImpl_GetTop
1211 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, ID3DXMatrixStack **stack)
1213 struct ID3DXMatrixStackImpl *object;
1215 TRACE("flags %#x, stack %p.\n", flags, stack);
1217 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
1219 *stack = NULL;
1220 return E_OUTOFMEMORY;
1222 object->ID3DXMatrixStack_iface.lpVtbl = &ID3DXMatrixStack_Vtbl;
1223 object->ref = 1;
1225 if (!(object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(*object->stack))))
1227 HeapFree(GetProcessHeap(), 0, object);
1228 *stack = NULL;
1229 return E_OUTOFMEMORY;
1232 object->current = 0;
1233 object->stack_size = INITIAL_STACK_SIZE;
1234 D3DXMatrixIdentity(&object->stack[0]);
1236 TRACE("Created matrix stack %p.\n", object);
1238 *stack = &object->ID3DXMatrixStack_iface;
1239 return D3D_OK;
1242 /*_________________D3DXPLANE________________*/
1244 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, const D3DXVECTOR3 *pvpoint, const D3DXVECTOR3 *pvnormal)
1246 TRACE("pout %p, pvpoint %p, pvnormal %p\n", pout, pvpoint, pvnormal);
1248 pout->a = pvnormal->x;
1249 pout->b = pvnormal->y;
1250 pout->c = pvnormal->z;
1251 pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
1252 return pout;
1255 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3)
1257 D3DXVECTOR3 edge1, edge2, normal, Nnormal;
1259 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p\n", pout, pv1, pv2, pv3);
1261 edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
1262 edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
1263 D3DXVec3Subtract(&edge1, pv2, pv1);
1264 D3DXVec3Subtract(&edge2, pv3, pv1);
1265 D3DXVec3Cross(&normal, &edge1, &edge2);
1266 D3DXVec3Normalize(&Nnormal, &normal);
1267 D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
1268 return pout;
1271 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, const D3DXPLANE *pp, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2)
1273 D3DXVECTOR3 direction, normal;
1274 FLOAT dot, temp;
1276 TRACE("pout %p, pp %p, pv1 %p, pv2 %p\n", pout, pp, pv1, pv2);
1278 normal.x = pp->a;
1279 normal.y = pp->b;
1280 normal.z = pp->c;
1281 direction.x = pv2->x - pv1->x;
1282 direction.y = pv2->y - pv1->y;
1283 direction.z = pv2->z - pv1->z;
1284 dot = D3DXVec3Dot(&normal, &direction);
1285 if ( !dot ) return NULL;
1286 temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
1287 pout->x = pv1->x - temp * direction.x;
1288 pout->y = pv1->y - temp * direction.y;
1289 pout->z = pv1->z - temp * direction.z;
1290 return pout;
1293 D3DXPLANE * WINAPI D3DXPlaneNormalize(D3DXPLANE *out, const D3DXPLANE *p)
1295 FLOAT norm;
1297 TRACE("out %p, p %p\n", out, p);
1299 norm = sqrtf(p->a * p->a + p->b * p->b + p->c * p->c);
1300 if (norm)
1302 out->a = p->a / norm;
1303 out->b = p->b / norm;
1304 out->c = p->c / norm;
1305 out->d = p->d / norm;
1307 else
1309 out->a = 0.0f;
1310 out->b = 0.0f;
1311 out->c = 0.0f;
1312 out->d = 0.0f;
1315 return out;
1318 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, const D3DXPLANE *pplane, const D3DXMATRIX *pm)
1320 const D3DXPLANE plane = *pplane;
1322 TRACE("pout %p, pplane %p, pm %p\n", pout, pplane, pm);
1324 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;
1325 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;
1326 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;
1327 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;
1328 return pout;
1331 D3DXPLANE* WINAPI D3DXPlaneTransformArray(D3DXPLANE* out, UINT outstride, const D3DXPLANE* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1333 UINT i;
1335 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1337 for (i = 0; i < elements; ++i) {
1338 D3DXPlaneTransform(
1339 (D3DXPLANE*)((char*)out + outstride * i),
1340 (const D3DXPLANE*)((const char*)in + instride * i),
1341 matrix);
1343 return out;
1346 /*_________________D3DXQUATERNION________________*/
1348 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
1350 D3DXQUATERNION temp1, temp2;
1352 TRACE("pout %p, pq1 %p, pq2 %p, pq3 %p, f %f, g %f\n", pout, pq1, pq2, pq3, f, g);
1354 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
1355 return pout;
1358 D3DXQUATERNION * WINAPI D3DXQuaternionExp(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1360 FLOAT norm;
1362 TRACE("out %p, q %p\n", out, q);
1364 norm = sqrtf(q->x * q->x + q->y * q->y + q->z * q->z);
1365 if (norm)
1367 out->x = sinf(norm) * q->x / norm;
1368 out->y = sinf(norm) * q->y / norm;
1369 out->z = sinf(norm) * q->z / norm;
1370 out->w = cosf(norm);
1372 else
1374 out->x = 0.0f;
1375 out->y = 0.0f;
1376 out->z = 0.0f;
1377 out->w = 1.0f;
1380 return out;
1383 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, const D3DXQUATERNION *pq)
1385 D3DXQUATERNION out;
1386 FLOAT norm;
1388 TRACE("pout %p, pq %p\n", pout, pq);
1390 norm = D3DXQuaternionLengthSq(pq);
1392 out.x = -pq->x / norm;
1393 out.y = -pq->y / norm;
1394 out.z = -pq->z / norm;
1395 out.w = pq->w / norm;
1397 *pout =out;
1398 return pout;
1401 D3DXQUATERNION * WINAPI D3DXQuaternionLn(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1403 FLOAT t;
1405 TRACE("out %p, q %p\n", out, q);
1407 if ((q->w >= 1.0f) || (q->w == -1.0f))
1408 t = 1.0f;
1409 else
1410 t = acosf(q->w) / sqrtf(1.0f - q->w * q->w);
1412 out->x = t * q->x;
1413 out->y = t * q->y;
1414 out->z = t * q->z;
1415 out->w = 0.0f;
1417 return out;
1420 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2)
1422 D3DXQUATERNION out;
1424 TRACE("pout %p, pq1 %p, pq2 %p\n", pout, pq1, pq2);
1426 out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
1427 out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
1428 out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
1429 out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
1430 *pout = out;
1431 return pout;
1434 D3DXQUATERNION * WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1436 FLOAT norm;
1438 TRACE("out %p, q %p\n", out, q);
1440 norm = D3DXQuaternionLength(q);
1442 out->x = q->x / norm;
1443 out->y = q->y / norm;
1444 out->z = q->z / norm;
1445 out->w = q->w / norm;
1447 return out;
1450 D3DXQUATERNION * WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *out, const D3DXVECTOR3 *v, FLOAT angle)
1452 D3DXVECTOR3 temp;
1454 TRACE("out %p, v %p, angle %f\n", out, v, angle);
1456 D3DXVec3Normalize(&temp, v);
1458 out->x = sinf(angle / 2.0f) * temp.x;
1459 out->y = sinf(angle / 2.0f) * temp.y;
1460 out->z = sinf(angle / 2.0f) * temp.z;
1461 out->w = cosf(angle / 2.0f);
1463 return out;
1466 D3DXQUATERNION * WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *out, const D3DXMATRIX *m)
1468 FLOAT s, trace;
1470 TRACE("out %p, m %p\n", out, m);
1472 trace = m->u.m[0][0] + m->u.m[1][1] + m->u.m[2][2] + 1.0f;
1473 if (trace > 1.0f)
1475 s = 2.0f * sqrtf(trace);
1476 out->x = (m->u.m[1][2] - m->u.m[2][1]) / s;
1477 out->y = (m->u.m[2][0] - m->u.m[0][2]) / s;
1478 out->z = (m->u.m[0][1] - m->u.m[1][0]) / s;
1479 out->w = 0.25f * s;
1481 else
1483 int i, maxi = 0;
1485 for (i = 1; i < 3; i++)
1487 if (m->u.m[i][i] > m->u.m[maxi][maxi])
1488 maxi = i;
1491 switch (maxi)
1493 case 0:
1494 s = 2.0f * sqrtf(1.0f + m->u.m[0][0] - m->u.m[1][1] - m->u.m[2][2]);
1495 out->x = 0.25f * s;
1496 out->y = (m->u.m[0][1] + m->u.m[1][0]) / s;
1497 out->z = (m->u.m[0][2] + m->u.m[2][0]) / s;
1498 out->w = (m->u.m[1][2] - m->u.m[2][1]) / s;
1499 break;
1501 case 1:
1502 s = 2.0f * sqrtf(1.0f + m->u.m[1][1] - m->u.m[0][0] - m->u.m[2][2]);
1503 out->x = (m->u.m[0][1] + m->u.m[1][0]) / s;
1504 out->y = 0.25f * s;
1505 out->z = (m->u.m[1][2] + m->u.m[2][1]) / s;
1506 out->w = (m->u.m[2][0] - m->u.m[0][2]) / s;
1507 break;
1509 case 2:
1510 s = 2.0f * sqrtf(1.0f + m->u.m[2][2] - m->u.m[0][0] - m->u.m[1][1]);
1511 out->x = (m->u.m[0][2] + m->u.m[2][0]) / s;
1512 out->y = (m->u.m[1][2] + m->u.m[2][1]) / s;
1513 out->z = 0.25f * s;
1514 out->w = (m->u.m[0][1] - m->u.m[1][0]) / s;
1515 break;
1519 return out;
1522 D3DXQUATERNION * WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *out, FLOAT yaw, FLOAT pitch, FLOAT roll)
1524 FLOAT syaw, cyaw, spitch, cpitch, sroll, croll;
1526 TRACE("out %p, yaw %f, pitch %f, roll %f\n", out, yaw, pitch, roll);
1528 syaw = sinf(yaw / 2.0f);
1529 cyaw = cosf(yaw / 2.0f);
1530 spitch = sinf(pitch / 2.0f);
1531 cpitch = cosf(pitch / 2.0f);
1532 sroll = sinf(roll / 2.0f);
1533 croll = cosf(roll / 2.0f);
1535 out->x = syaw * cpitch * sroll + cyaw * spitch * croll;
1536 out->y = syaw * cpitch * croll - cyaw * spitch * sroll;
1537 out->z = cyaw * cpitch * sroll - syaw * spitch * croll;
1538 out->w = cyaw * cpitch * croll + syaw * spitch * sroll;
1540 return out;
1543 D3DXQUATERNION * WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *out, const D3DXQUATERNION *q1,
1544 const D3DXQUATERNION *q2, FLOAT t)
1546 FLOAT dot, temp;
1548 TRACE("out %p, q1 %p, q2 %p, t %f\n", out, q1, q2, t);
1550 temp = 1.0f - t;
1551 dot = D3DXQuaternionDot(q1, q2);
1552 if (dot < 0.0f)
1554 t = -t;
1555 dot = -dot;
1558 if (1.0f - dot > 0.001f)
1560 FLOAT theta = acosf(dot);
1562 temp = sinf(theta * temp) / sinf(theta);
1563 t = sinf(theta * t) / sinf(theta);
1566 out->x = temp * q1->x + t * q2->x;
1567 out->y = temp * q1->y + t * q2->y;
1568 out->z = temp * q1->z + t * q2->z;
1569 out->w = temp * q1->w + t * q2->w;
1571 return out;
1574 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3, const D3DXQUATERNION *pq4, FLOAT t)
1576 D3DXQUATERNION temp1, temp2;
1578 TRACE("pout %p, pq1 %p, pq2 %p, pq3 %p, pq4 %p, t %f\n", pout, pq1, pq2, pq3, pq4, t);
1580 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1581 return pout;
1584 static D3DXQUATERNION add_diff(const D3DXQUATERNION *q1, const D3DXQUATERNION *q2, const FLOAT add)
1586 D3DXQUATERNION temp;
1588 temp.x = q1->x + add * q2->x;
1589 temp.y = q1->y + add * q2->y;
1590 temp.z = q1->z + add * q2->z;
1591 temp.w = q1->w + add * q2->w;
1593 return temp;
1596 void WINAPI D3DXQuaternionSquadSetup(D3DXQUATERNION *paout, D3DXQUATERNION *pbout, D3DXQUATERNION *pcout, const D3DXQUATERNION *pq0, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3)
1598 D3DXQUATERNION q, temp1, temp2, temp3, zero;
1600 TRACE("paout %p, pbout %p, pcout %p, pq0 %p, pq1 %p, pq2 %p, pq3 %p\n", paout, pbout, pcout, pq0, pq1, pq2, pq3);
1602 zero.x = 0.0f;
1603 zero.y = 0.0f;
1604 zero.z = 0.0f;
1605 zero.w = 0.0f;
1607 if ( D3DXQuaternionDot(pq0, pq1) < 0.0f )
1608 temp2 = add_diff(&zero, pq0, -1.0f);
1609 else
1610 temp2 = *pq0;
1612 if ( D3DXQuaternionDot(pq1, pq2) < 0.0f )
1613 *pcout = add_diff(&zero, pq2, -1.0f);
1614 else
1615 *pcout = *pq2;
1617 if ( D3DXQuaternionDot(pcout, pq3) < 0.0f )
1618 temp3 = add_diff(&zero, pq3, -1.0f);
1619 else
1620 temp3 = *pq3;
1622 D3DXQuaternionInverse(&temp1, pq1);
1623 D3DXQuaternionMultiply(&temp2, &temp1, &temp2);
1624 D3DXQuaternionLn(&temp2, &temp2);
1625 D3DXQuaternionMultiply(&q, &temp1, pcout);
1626 D3DXQuaternionLn(&q, &q);
1627 temp1 = add_diff(&temp2, &q, 1.0f);
1628 temp1.x *= -0.25f;
1629 temp1.y *= -0.25f;
1630 temp1.z *= -0.25f;
1631 temp1.w *= -0.25f;
1632 D3DXQuaternionExp(&temp1, &temp1);
1633 D3DXQuaternionMultiply(paout, pq1, &temp1);
1635 D3DXQuaternionInverse(&temp1, pcout);
1636 D3DXQuaternionMultiply(&temp2, &temp1, pq1);
1637 D3DXQuaternionLn(&temp2, &temp2);
1638 D3DXQuaternionMultiply(&q, &temp1, &temp3);
1639 D3DXQuaternionLn(&q, &q);
1640 temp1 = add_diff(&temp2, &q, 1.0f);
1641 temp1.x *= -0.25f;
1642 temp1.y *= -0.25f;
1643 temp1.z *= -0.25f;
1644 temp1.w *= -0.25f;
1645 D3DXQuaternionExp(&temp1, &temp1);
1646 D3DXQuaternionMultiply(pbout, pcout, &temp1);
1648 return;
1651 void WINAPI D3DXQuaternionToAxisAngle(const D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1653 TRACE("pq %p, paxis %p, pangle %p\n", pq, paxis, pangle);
1655 if (paxis)
1657 paxis->x = pq->x;
1658 paxis->y = pq->y;
1659 paxis->z = pq->z;
1661 if (pangle)
1662 *pangle = 2.0f * acosf(pq->w);
1665 /*_________________D3DXVec2_____________________*/
1667 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1669 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
1671 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1672 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1673 return pout;
1676 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv0, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pv3, FLOAT s)
1678 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
1680 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);
1681 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);
1682 return pout;
1685 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pt1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pt2, FLOAT s)
1687 FLOAT h1, h2, h3, h4;
1689 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
1691 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1692 h2 = s * s * s - 2.0f * s * s + s;
1693 h3 = -2.0f * s * s * s + 3.0f * s * s;
1694 h4 = s * s * s - s * s;
1696 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1697 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1698 return pout;
1701 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv)
1703 FLOAT norm;
1705 TRACE("pout %p, pv %p\n", pout, pv);
1707 norm = D3DXVec2Length(pv);
1708 if ( !norm )
1710 pout->x = 0.0f;
1711 pout->y = 0.0f;
1713 else
1715 pout->x = pv->x / norm;
1716 pout->y = pv->y / norm;
1719 return pout;
1722 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1724 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1726 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
1727 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
1728 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
1729 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1730 return pout;
1733 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR2* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1735 UINT i;
1737 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1739 for (i = 0; i < elements; ++i) {
1740 D3DXVec2Transform(
1741 (D3DXVECTOR4*)((char*)out + outstride * i),
1742 (const D3DXVECTOR2*)((const char*)in + instride * i),
1743 matrix);
1745 return out;
1748 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1750 D3DXVECTOR2 v;
1751 FLOAT norm;
1753 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1755 v = *pv;
1756 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1758 pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1759 pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1761 return pout;
1764 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, const D3DXVECTOR2* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1766 UINT i;
1768 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1770 for (i = 0; i < elements; ++i) {
1771 D3DXVec2TransformCoord(
1772 (D3DXVECTOR2*)((char*)out + outstride * i),
1773 (const D3DXVECTOR2*)((const char*)in + instride * i),
1774 matrix);
1776 return out;
1779 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1781 const D3DXVECTOR2 v = *pv;
1783 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1785 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1786 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1787 return pout;
1790 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, const D3DXVECTOR2 *in, UINT instride, const D3DXMATRIX *matrix, UINT elements)
1792 UINT i;
1794 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1796 for (i = 0; i < elements; ++i) {
1797 D3DXVec2TransformNormal(
1798 (D3DXVECTOR2*)((char*)out + outstride * i),
1799 (const D3DXVECTOR2*)((const char*)in + instride * i),
1800 matrix);
1802 return out;
1805 /*_________________D3DXVec3_____________________*/
1807 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1809 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
1811 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1812 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1813 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1814 return pout;
1817 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv0, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3, FLOAT s)
1819 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
1821 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);
1822 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);
1823 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);
1824 return pout;
1827 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pt1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pt2, FLOAT s)
1829 FLOAT h1, h2, h3, h4;
1831 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
1833 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1834 h2 = s * s * s - 2.0f * s * s + s;
1835 h3 = -2.0f * s * s * s + 3.0f * s * s;
1836 h4 = s * s * s - s * s;
1838 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1839 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1840 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1841 return pout;
1844 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv)
1846 FLOAT norm;
1848 TRACE("pout %p, pv %p\n", pout, pv);
1850 norm = D3DXVec3Length(pv);
1851 if ( !norm )
1853 pout->x = 0.0f;
1854 pout->y = 0.0f;
1855 pout->z = 0.0f;
1857 else
1859 pout->x = pv->x / norm;
1860 pout->y = pv->y / norm;
1861 pout->z = pv->z / norm;
1864 return pout;
1867 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DVIEWPORT9 *pviewport, const D3DXMATRIX *pprojection, const D3DXMATRIX *pview, const D3DXMATRIX *pworld)
1869 D3DXMATRIX m;
1871 TRACE("pout %p, pv %p, pviewport %p, pprojection %p, pview %p, pworld %p\n", pout, pv, pviewport, pprojection, pview, pworld);
1873 D3DXMatrixIdentity(&m);
1874 if (pworld) D3DXMatrixMultiply(&m, &m, pworld);
1875 if (pview) D3DXMatrixMultiply(&m, &m, pview);
1876 if (pprojection) D3DXMatrixMultiply(&m, &m, pprojection);
1878 D3DXVec3TransformCoord(pout, pv, &m);
1880 if (pviewport)
1882 pout->x = pviewport->X + ( 1.0f + pout->x ) * pviewport->Width / 2.0f;
1883 pout->y = pviewport->Y + ( 1.0f - pout->y ) * pviewport->Height / 2.0f;
1884 pout->z = pviewport->MinZ + pout->z * ( pviewport->MaxZ - pviewport->MinZ );
1886 return pout;
1889 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)
1891 UINT i;
1893 TRACE("out %p, outstride %u, in %p, instride %u, viewport %p, projection %p, view %p, world %p, elements %u\n",
1894 out, outstride, in, instride, viewport, projection, view, world, elements);
1896 for (i = 0; i < elements; ++i) {
1897 D3DXVec3Project(
1898 (D3DXVECTOR3*)((char*)out + outstride * i),
1899 (const D3DXVECTOR3*)((const char*)in + instride * i),
1900 viewport, projection, view, world);
1902 return out;
1905 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1907 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1909 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];
1910 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];
1911 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];
1912 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];
1913 return pout;
1916 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1918 UINT i;
1920 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1922 for (i = 0; i < elements; ++i) {
1923 D3DXVec3Transform(
1924 (D3DXVECTOR4*)((char*)out + outstride * i),
1925 (const D3DXVECTOR3*)((const char*)in + instride * i),
1926 matrix);
1928 return out;
1931 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1933 D3DXVECTOR3 out;
1934 FLOAT norm;
1936 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1938 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];
1940 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;
1941 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;
1942 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;
1944 *pout = out;
1946 return pout;
1949 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(D3DXVECTOR3* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1951 UINT i;
1953 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1955 for (i = 0; i < elements; ++i) {
1956 D3DXVec3TransformCoord(
1957 (D3DXVECTOR3*)((char*)out + outstride * i),
1958 (const D3DXVECTOR3*)((const char*)in + instride * i),
1959 matrix);
1961 return out;
1964 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1966 const D3DXVECTOR3 v = *pv;
1968 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1970 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1971 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1972 pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1973 return pout;
1977 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1979 UINT i;
1981 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1983 for (i = 0; i < elements; ++i) {
1984 D3DXVec3TransformNormal(
1985 (D3DXVECTOR3*)((char*)out + outstride * i),
1986 (const D3DXVECTOR3*)((const char*)in + instride * i),
1987 matrix);
1989 return out;
1992 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DVIEWPORT9 *pviewport, const D3DXMATRIX *pprojection, const D3DXMATRIX *pview, const D3DXMATRIX *pworld)
1994 D3DXMATRIX m;
1996 TRACE("pout %p, pv %p, pviewport %p, pprojection %p, pview %p, pworlds %p\n", pout, pv, pviewport, pprojection, pview, pworld);
1998 D3DXMatrixIdentity(&m);
1999 if (pworld) D3DXMatrixMultiply(&m, &m, pworld);
2000 if (pview) D3DXMatrixMultiply(&m, &m, pview);
2001 if (pprojection) D3DXMatrixMultiply(&m, &m, pprojection);
2002 D3DXMatrixInverse(&m, NULL, &m);
2004 *pout = *pv;
2005 if (pviewport)
2007 pout->x = 2.0f * ( pout->x - pviewport->X ) / pviewport->Width - 1.0f;
2008 pout->y = 1.0f - 2.0f * ( pout->y - pviewport->Y ) / pviewport->Height;
2009 pout->z = ( pout->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
2011 D3DXVec3TransformCoord(pout, pout, &m);
2012 return pout;
2015 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)
2017 UINT i;
2019 TRACE("out %p, outstride %u, in %p, instride %u, viewport %p, projection %p, view %p, world %p, elements %u\n",
2020 out, outstride, in, instride, viewport, projection, view, world, elements);
2022 for (i = 0; i < elements; ++i) {
2023 D3DXVec3Unproject(
2024 (D3DXVECTOR3*)((char*)out + outstride * i),
2025 (const D3DXVECTOR3*)((const char*)in + instride * i),
2026 viewport, projection, view, world);
2028 return out;
2031 /*_________________D3DXVec4_____________________*/
2033 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
2035 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
2037 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
2038 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
2039 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
2040 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
2041 return pout;
2044 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv0, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3, FLOAT s)
2046 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
2048 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);
2049 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);
2050 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);
2051 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);
2052 return pout;
2055 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3)
2057 D3DXVECTOR4 out;
2059 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p\n", pout, pv1, pv2, pv3);
2061 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);
2062 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));
2063 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);
2064 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));
2065 *pout = out;
2066 return pout;
2069 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pt1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pt2, FLOAT s)
2071 FLOAT h1, h2, h3, h4;
2073 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
2075 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
2076 h2 = s * s * s - 2.0f * s * s + s;
2077 h3 = -2.0f * s * s * s + 3.0f * s * s;
2078 h4 = s * s * s - s * s;
2080 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
2081 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
2082 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
2083 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
2084 return pout;
2087 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv)
2089 FLOAT norm;
2091 TRACE("pout %p, pv %p\n", pout, pv);
2093 norm = D3DXVec4Length(pv);
2095 pout->x = pv->x / norm;
2096 pout->y = pv->y / norm;
2097 pout->z = pv->z / norm;
2098 pout->w = pv->w / norm;
2100 return pout;
2103 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv, const D3DXMATRIX *pm)
2105 D3DXVECTOR4 out;
2107 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
2109 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;
2110 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;
2111 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;
2112 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;
2113 *pout = out;
2114 return pout;
2117 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR4* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
2119 UINT i;
2121 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
2123 for (i = 0; i < elements; ++i) {
2124 D3DXVec4Transform(
2125 (D3DXVECTOR4*)((char*)out + outstride * i),
2126 (const D3DXVECTOR4*)((const char*)in + instride * i),
2127 matrix);
2129 return out;
2132 unsigned short float_32_to_16(const float in)
2134 int exp = 0, origexp;
2135 float tmp = fabsf(in);
2136 int sign = (copysignf(1, in) < 0);
2137 unsigned int mantissa;
2138 unsigned short ret;
2140 /* Deal with special numbers */
2141 if (isinf(in)) return (sign ? 0xffff : 0x7fff);
2142 if (isnan(in)) return (sign ? 0xffff : 0x7fff);
2143 if (in == 0.0f) return (sign ? 0x8000 : 0x0000);
2145 if (tmp < (float)(1u << 10))
2149 tmp *= 2.0f;
2150 exp--;
2151 } while (tmp < (float)(1u << 10));
2153 else if (tmp >= (float)(1u << 11))
2157 tmp /= 2.0f;
2158 exp++;
2159 } while (tmp >= (float)(1u << 11));
2162 exp += 10; /* Normalize the mantissa */
2163 exp += 15; /* Exponent is encoded with excess 15 */
2165 origexp = exp;
2167 mantissa = (unsigned int) tmp;
2168 if ((tmp - mantissa == 0.5f && mantissa % 2 == 1) || /* round half to even */
2169 (tmp - mantissa > 0.5f))
2171 mantissa++; /* round to nearest, away from zero */
2173 if (mantissa == 2048)
2175 mantissa = 1024;
2176 exp++;
2179 if (exp > 31)
2181 /* too big */
2182 ret = 0x7fff; /* INF */
2184 else if (exp <= 0)
2186 unsigned int rounding = 0;
2188 /* Denormalized half float */
2190 /* return 0x0000 (=0.0) for numbers too small to represent in half floats */
2191 if (exp < -11)
2192 return (sign ? 0x8000 : 0x0000);
2194 exp = origexp;
2196 /* the 13 extra bits from single precision are used for rounding */
2197 mantissa = (unsigned int)(tmp * (1u << 13));
2198 mantissa >>= 1 - exp; /* denormalize */
2200 mantissa -= ~(mantissa >> 13) & 1; /* round half to even */
2201 /* remove 13 least significant bits to get half float precision */
2202 mantissa >>= 12;
2203 rounding = mantissa & 1;
2204 mantissa >>= 1;
2206 ret = mantissa + rounding;
2208 else
2210 ret = (exp << 10) | (mantissa & 0x3ff);
2213 ret |= ((sign ? 1 : 0) << 15); /* Add the sign */
2214 return ret;
2217 D3DXFLOAT16 *WINAPI D3DXFloat32To16Array(D3DXFLOAT16 *pout, const FLOAT *pin, UINT n)
2219 unsigned int i;
2221 TRACE("pout %p, pin %p, n %u\n", pout, pin, n);
2223 for (i = 0; i < n; ++i)
2225 pout[i].value = float_32_to_16(pin[i]);
2228 return pout;
2231 /* Native d3dx9's D3DXFloat16to32Array lacks support for NaN and Inf. Specifically, e = 16 is treated as a
2232 * regular number - e.g., 0x7fff is converted to 131008.0 and 0xffff to -131008.0. */
2233 float float_16_to_32(const unsigned short in)
2235 const unsigned short s = (in & 0x8000);
2236 const unsigned short e = (in & 0x7C00) >> 10;
2237 const unsigned short m = in & 0x3FF;
2238 const float sgn = (s ? -1.0f : 1.0f);
2240 if (e == 0)
2242 if (m == 0) return sgn * 0.0f; /* +0.0 or -0.0 */
2243 else return sgn * powf(2, -14.0f) * (m / 1024.0f);
2245 else
2247 return sgn * powf(2, e - 15.0f) * (1.0f + (m / 1024.0f));
2251 FLOAT *WINAPI D3DXFloat16To32Array(FLOAT *pout, const D3DXFLOAT16 *pin, UINT n)
2253 unsigned int i;
2255 TRACE("pout %p, pin %p, n %u\n", pout, pin, n);
2257 for (i = 0; i < n; ++i)
2259 pout[i] = float_16_to_32(pin[i].value);
2262 return pout;
2265 /*_________________D3DXSH________________*/
2267 FLOAT* WINAPI D3DXSHAdd(FLOAT *out, UINT order, const FLOAT *a, const FLOAT *b)
2269 UINT i;
2271 TRACE("out %p, order %u, a %p, b %p\n", out, order, a, b);
2273 for (i = 0; i < order * order; i++)
2274 out[i] = a[i] + b[i];
2276 return out;
2279 FLOAT WINAPI D3DXSHDot(UINT order, const FLOAT *a, const FLOAT *b)
2281 FLOAT s;
2282 UINT i;
2284 TRACE("order %u, a %p, b %p\n", order, a, b);
2286 s = a[0] * b[0];
2287 for (i = 1; i < order * order; i++)
2288 s += a[i] * b[i];
2290 return s;
2293 static void weightedcapintegrale(FLOAT *out, UINT order, FLOAT angle)
2295 FLOAT coeff[3];
2297 coeff[0] = cosf(angle);
2299 out[0] = 2.0f * D3DX_PI * (1.0f - coeff[0]);
2300 out[1] = D3DX_PI * sinf(angle) * sinf(angle);
2301 if (order <= 2)
2302 return;
2304 out[2] = coeff[0] * out[1];
2305 if (order == 3)
2306 return;
2308 coeff[1] = coeff[0] * coeff[0];
2309 coeff[2] = coeff[1] * coeff[1];
2311 out[3] = D3DX_PI * (-1.25f * coeff[2] + 1.5f * coeff[1] - 0.25f);
2312 if (order == 4)
2313 return;
2315 out[4] = -0.25f * D3DX_PI * coeff[0] * (7.0f * coeff[2] - 10.0f * coeff[1] + 3.0f);
2316 if (order == 5)
2317 return;
2319 out[5] = D3DX_PI * (-2.625f * coeff[2] * coeff[1] + 4.375f * coeff[2] - 1.875f * coeff[1] + 0.125f);
2322 HRESULT WINAPI D3DXSHEvalConeLight(UINT order, const D3DXVECTOR3 *dir, FLOAT radius,
2323 FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *rout, FLOAT *gout, FLOAT *bout)
2325 FLOAT cap[6], clamped_angle, norm, scale, temp;
2326 UINT i, index, j;
2328 TRACE("order %u, dir %p, radius %f, red %f, green %f, blue %f, rout %p, gout %p, bout %p\n",
2329 order, dir, radius, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2331 if (radius <= 0.0f)
2332 return D3DXSHEvalDirectionalLight(order, dir, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2334 clamped_angle = (radius > D3DX_PI / 2.0f) ? (D3DX_PI / 2.0f) : radius;
2335 norm = sinf(clamped_angle) * sinf(clamped_angle);
2337 if (order > D3DXSH_MAXORDER)
2339 WARN("Order clamped at D3DXSH_MAXORDER\n");
2340 order = D3DXSH_MAXORDER;
2343 weightedcapintegrale(cap, order, radius);
2344 D3DXSHEvalDirection(rout, order, dir);
2346 for (i = 0; i < order; i++)
2348 scale = cap[i] / norm;
2350 for (j = 0; j < 2 * i + 1; j++)
2352 index = i * i + j;
2353 temp = rout[index] * scale;
2355 rout[index] = temp * Rintensity;
2356 if (gout)
2357 gout[index] = temp * Gintensity;
2358 if (bout)
2359 bout[index] = temp * Bintensity;
2363 return D3D_OK;
2366 FLOAT* WINAPI D3DXSHEvalDirection(FLOAT *out, UINT order, const D3DXVECTOR3 *dir)
2368 const FLOAT dirxx = dir->x * dir->x;
2369 const FLOAT dirxy = dir->x * dir->y;
2370 const FLOAT dirxz = dir->x * dir->z;
2371 const FLOAT diryy = dir->y * dir->y;
2372 const FLOAT diryz = dir->y * dir->z;
2373 const FLOAT dirzz = dir->z * dir->z;
2374 const FLOAT dirxxxx = dirxx * dirxx;
2375 const FLOAT diryyyy = diryy * diryy;
2376 const FLOAT dirzzzz = dirzz * dirzz;
2377 const FLOAT dirxyxy = dirxy * dirxy;
2379 TRACE("out %p, order %u, dir %p\n", out, order, dir);
2381 if ((order < D3DXSH_MINORDER) || (order > D3DXSH_MAXORDER))
2382 return out;
2384 out[0] = 0.5f / sqrtf(D3DX_PI);
2385 out[1] = -0.5f / sqrtf(D3DX_PI / 3.0f) * dir->y;
2386 out[2] = 0.5f / sqrtf(D3DX_PI / 3.0f) * dir->z;
2387 out[3] = -0.5f / sqrtf(D3DX_PI / 3.0f) * dir->x;
2388 if (order == 2)
2389 return out;
2391 out[4] = 0.5f / sqrtf(D3DX_PI / 15.0f) * dirxy;
2392 out[5] = -0.5f / sqrtf(D3DX_PI / 15.0f) * diryz;
2393 out[6] = 0.25f / sqrtf(D3DX_PI / 5.0f) * (3.0f * dirzz - 1.0f);
2394 out[7] = -0.5f / sqrtf(D3DX_PI / 15.0f) * dirxz;
2395 out[8] = 0.25f / sqrtf(D3DX_PI / 15.0f) * (dirxx - diryy);
2396 if (order == 3)
2397 return out;
2399 out[9] = -sqrtf(70.0f / D3DX_PI) / 8.0f * dir->y * (3.0f * dirxx - diryy);
2400 out[10] = sqrtf(105.0f / D3DX_PI) / 2.0f * dirxy * dir->z;
2401 out[11] = -sqrtf(42.0f / D3DX_PI) / 8.0f * dir->y * (-1.0f + 5.0f * dirzz);
2402 out[12] = sqrtf(7.0f / D3DX_PI) / 4.0f * dir->z * (5.0f * dirzz - 3.0f);
2403 out[13] = sqrtf(42.0f / D3DX_PI) / 8.0f * dir->x * (1.0f - 5.0f * dirzz);
2404 out[14] = sqrtf(105.0f / D3DX_PI) / 4.0f * dir->z * (dirxx - diryy);
2405 out[15] = -sqrtf(70.0f / D3DX_PI) / 8.0f * dir->x * (dirxx - 3.0f * diryy);
2406 if (order == 4)
2407 return out;
2409 out[16] = 0.75f * sqrtf(35.0f / D3DX_PI) * dirxy * (dirxx - diryy);
2410 out[17] = 3.0f * dir->z * out[9];
2411 out[18] = 0.75f * sqrtf(5.0f / D3DX_PI) * dirxy * (7.0f * dirzz - 1.0f);
2412 out[19] = 0.375f * sqrtf(10.0f / D3DX_PI) * diryz * (3.0f - 7.0f * dirzz);
2413 out[20] = 3.0f / (16.0f * sqrtf(D3DX_PI)) * (35.0f * dirzzzz - 30.f * dirzz + 3.0f);
2414 out[21] = 0.375f * sqrtf(10.0f / D3DX_PI) * dirxz * (3.0f - 7.0f * dirzz);
2415 out[22] = 0.375f * sqrtf(5.0f / D3DX_PI) * (dirxx - diryy) * (7.0f * dirzz - 1.0f);
2416 out[23] = 3.0f * dir->z * out[15];
2417 out[24] = 3.0f / 16.0f * sqrtf(35.0f / D3DX_PI) * (dirxxxx - 6.0f * dirxyxy + diryyyy);
2418 if (order == 5)
2419 return out;
2421 out[25] = -3.0f/ 32.0f * sqrtf(154.0f / D3DX_PI) * dir->y * (5.0f * dirxxxx - 10.0f * dirxyxy + diryyyy);
2422 out[26] = 0.75f * sqrtf(385.0f / D3DX_PI) * dirxy * dir->z * (dirxx - diryy);
2423 out[27] = sqrtf(770.0f / D3DX_PI) / 32.0f * dir->y * (3.0f * dirxx - diryy) * (1.0f - 9.0f * dirzz);
2424 out[28] = sqrtf(1155.0f / D3DX_PI) / 4.0f * dirxy * dir->z * (3.0f * dirzz - 1.0f);
2425 out[29] = sqrtf(165.0f / D3DX_PI) / 16.0f * dir->y * (14.0f * dirzz - 21.0f * dirzzzz - 1.0f);
2426 out[30] = sqrtf(11.0f / D3DX_PI) / 16.0f * dir->z * (63.0f * dirzzzz - 70.0f * dirzz + 15.0f);
2427 out[31] = sqrtf(165.0f / D3DX_PI) / 16.0f * dir->x * (14.0f * dirzz - 21.0f * dirzzzz - 1.0f);
2428 out[32] = sqrtf(1155.0f / D3DX_PI) / 8.0f * dir->z * (dirxx - diryy) * (3.0f * dirzz - 1.0f);
2429 out[33] = sqrtf(770.0f / D3DX_PI) / 32.0f * dir->x * (dirxx - 3.0f * diryy) * (1.0f - 9.0f * dirzz);
2430 out[34] = 3.0f / 16.0f * sqrtf(385.0f / D3DX_PI) * dir->z * (dirxxxx - 6.0f * dirxyxy + diryyyy);
2431 out[35] = -3.0f/ 32.0f * sqrtf(154.0f / D3DX_PI) * dir->x * (dirxxxx - 10.0f * dirxyxy + 5.0f * diryyyy);
2433 return out;
2436 HRESULT WINAPI D3DXSHEvalDirectionalLight(UINT order, const D3DXVECTOR3 *dir, FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *Rout, FLOAT *Gout, FLOAT *Bout)
2438 FLOAT s, temp;
2439 UINT j;
2441 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);
2443 s = 0.75f;
2444 if ( order > 2 )
2445 s += 5.0f / 16.0f;
2446 if ( order > 4 )
2447 s -= 3.0f / 32.0f;
2448 s /= D3DX_PI;
2450 D3DXSHEvalDirection(Rout, order, dir);
2451 for (j = 0; j < order * order; j++)
2453 temp = Rout[j] / s;
2455 Rout[j] = Rintensity * temp;
2456 if ( Gout )
2457 Gout[j] = Gintensity * temp;
2458 if ( Bout )
2459 Bout[j] = Bintensity * temp;
2462 return D3D_OK;
2465 HRESULT WINAPI D3DXSHEvalHemisphereLight(UINT order, const D3DXVECTOR3 *dir, D3DXCOLOR top, D3DXCOLOR bottom,
2466 FLOAT *rout, FLOAT *gout, FLOAT *bout)
2468 FLOAT a[2], temp[4];
2469 UINT i, j;
2471 TRACE("order %u, dir %p, rout %p, gout %p, bout %p\n", order, dir, rout, gout, bout);
2473 D3DXSHEvalDirection(temp, 2, dir);
2475 a[0] = (top.r + bottom.r) * 3.0f * D3DX_PI;
2476 a[1] = (top.r - bottom.r) * D3DX_PI;
2477 for (i = 0; i < order; i++)
2478 for (j = 0; j < 2 * i + 1; j++)
2479 if (i < 2)
2480 rout[i * i + j] = temp[i * i + j] * a[i];
2481 else
2482 rout[i * i + j] = 0.0f;
2484 if (gout)
2486 a[0] = (top.g + bottom.g) * 3.0f * D3DX_PI;
2487 a[1] = (top.g - bottom.g) * D3DX_PI;
2488 for (i = 0; i < order; i++)
2489 for (j = 0; j < 2 * i + 1; j++)
2490 if (i < 2)
2491 gout[i * i + j] = temp[i * i + j] * a[i];
2492 else
2493 gout[i * i + j] = 0.0f;
2496 if (bout)
2498 a[0] = (top.b + bottom.b) * 3.0f * D3DX_PI;
2499 a[1] = (top.b - bottom.b) * D3DX_PI;
2500 for (i = 0; i < order; i++)
2501 for (j = 0; j < 2 * i + 1; j++)
2502 if (i < 2)
2503 bout[i * i + j] = temp[i * i + j] * a[i];
2504 else
2505 bout[i * i + j] = 0.0f;
2508 return D3D_OK;
2511 HRESULT WINAPI D3DXSHEvalSphericalLight(UINT order, const D3DXVECTOR3 *dir, FLOAT radius,
2512 FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *rout, FLOAT *gout, FLOAT *bout)
2514 D3DXVECTOR3 normal;
2515 FLOAT cap[6], clamped_angle, dist, temp;
2516 UINT i, index, j;
2518 TRACE("order %u, dir %p, radius %f, red %f, green %f, blue %f, rout %p, gout %p, bout %p\n",
2519 order, dir, radius, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2521 if (order > D3DXSH_MAXORDER)
2523 WARN("Order clamped at D3DXSH_MAXORDER\n");
2524 order = D3DXSH_MAXORDER;
2527 if (radius < 0.0f)
2528 radius = -radius;
2530 dist = D3DXVec3Length(dir);
2531 clamped_angle = (dist <= radius) ? D3DX_PI / 2.0f : asinf(radius / dist);
2533 weightedcapintegrale(cap, order, clamped_angle);
2534 D3DXVec3Normalize(&normal, dir);
2535 D3DXSHEvalDirection(rout, order, &normal);
2537 for (i = 0; i < order; i++)
2538 for (j = 0; j < 2 * i + 1; j++)
2540 index = i * i + j;
2541 temp = rout[index] * cap[i];
2543 rout[index] = temp * Rintensity;
2544 if (gout)
2545 gout[index] = temp * Gintensity;
2546 if (bout)
2547 bout[index] = temp * Bintensity;
2550 return D3D_OK;
2553 FLOAT * WINAPI D3DXSHMultiply2(FLOAT *out, const FLOAT *a, const FLOAT *b)
2555 FLOAT ta, tb;
2557 TRACE("out %p, a %p, b %p\n", out, a, b);
2559 ta = 0.28209479f * a[0];
2560 tb = 0.28209479f * b[0];
2562 out[0] = 0.28209479f * D3DXSHDot(2, a, b);
2563 out[1] = ta * b[1] + tb * a[1];
2564 out[2] = ta * b[2] + tb * a[2];
2565 out[3] = ta * b[3] + tb * a[3];
2567 return out;
2570 FLOAT * WINAPI D3DXSHMultiply3(FLOAT *out, const FLOAT *a, const FLOAT *b)
2572 FLOAT t, ta, tb;
2574 TRACE("out %p, a %p, b %p\n", out, a, b);
2576 out[0] = 0.28209479f * a[0] * b[0];
2578 ta = 0.28209479f * a[0] - 0.12615662f * a[6] - 0.21850968f * a[8];
2579 tb = 0.28209479f * b[0] - 0.12615662f * b[6] - 0.21850968f * b[8];
2580 out[1] = ta * b[1] + tb * a[1];
2581 t = a[1] * b[1];
2582 out[0] += 0.28209479f * t;
2583 out[6] = -0.12615662f * t;
2584 out[8] = -0.21850968f * t;
2586 ta = 0.21850968f * a[5];
2587 tb = 0.21850968f * b[5];
2588 out[1] += ta * b[2] + tb * a[2];
2589 out[2] = ta * b[1] + tb * a[1];
2590 t = a[1] * b[2] +a[2] * b[1];
2591 out[5] = 0.21850968f * t;
2593 ta = 0.21850968f * a[4];
2594 tb = 0.21850968f * b[4];
2595 out[1] += ta * b[3] + tb * a[3];
2596 out[3] = ta * b[1] + tb * a[1];
2597 t = a[1] * b[3] + a[3] * b[1];
2598 out[4] = 0.21850968f * t;
2600 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2601 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2602 out[2] += ta * b[2] + tb * a[2];
2603 t = a[2] * b[2];
2604 out[0] += 0.28209480f * t;
2605 out[6] += 0.25231326f * t;
2607 ta = 0.21850969f * a[7];
2608 tb = 0.21850969f * b[7];
2609 out[2] += ta * b[3] + tb * a[3];
2610 out[3] += ta * b[2] + tb * a[2];
2611 t = a[2] * b[3] + a[3] * b[2];
2612 out[7] = 0.21850969f * t;
2614 ta = 0.28209479f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2615 tb = 0.28209479f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2616 out[3] += ta * b[3] + tb * a[3];
2617 t = a[3] * b[3];
2618 out[0] += 0.28209479f * t;
2619 out[6] -= 0.12615663f * t;
2620 out[8] += 0.21850969f * t;
2622 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2623 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2624 out[4] += ta * b[4] + tb * a[4];
2625 t = a[4] * b[4];
2626 out[0] += 0.28209479f * t;
2627 out[6] -= 0.18022375f * t;
2629 ta = 0.15607835f * a[7];
2630 tb = 0.15607835f * b[7];
2631 out[4] += ta * b[5] + tb * a[5];
2632 out[5] += ta * b[4] + tb * a[4];
2633 t = a[4] * b[5] + a[5] * b[4];
2634 out[7] += 0.15607834f * t;
2636 ta = 0.28209479f * a[0] + 0.09011186f * a[6] - 0.15607835f * a[8];
2637 tb = 0.28209479f * b[0] + 0.09011186f * b[6] - 0.15607835f * b[8];
2638 out[5] += ta * b[5] + tb * a[5];
2639 t = a[5] * b[5];
2640 out[0] += 0.28209479f * t;
2641 out[6] += 0.09011186f * t;
2642 out[8] -= 0.15607835f * t;
2644 ta = 0.28209480f * a[0];
2645 tb = 0.28209480f * b[0];
2646 out[6] += ta * b[6] + tb * a[6];
2647 t = a[6] * b[6];
2648 out[0] += 0.28209480f * t;
2649 out[6] += 0.18022376f * t;
2651 ta = 0.28209479f * a[0] + 0.09011186f * a[6] + 0.15607835f * a[8];
2652 tb = 0.28209479f * b[0] + 0.09011186f * b[6] + 0.15607835f * b[8];
2653 out[7] += ta * b[7] + tb * a[7];
2654 t = a[7] * b[7];
2655 out[0] += 0.28209479f * t;
2656 out[6] += 0.09011186f * t;
2657 out[8] += 0.15607835f * t;
2659 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2660 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2661 out[8] += ta * b[8] + tb * a[8];
2662 t = a[8] * b[8];
2663 out[0] += 0.28209479f * t;
2664 out[6] -= 0.18022375f * t;
2666 return out;
2669 FLOAT * WINAPI D3DXSHMultiply4(FLOAT *out, const FLOAT *a, const FLOAT *b)
2671 FLOAT ta, tb, t;
2673 TRACE("out %p, a %p, b %p\n", out, a, b);
2675 out[0] = 0.28209479f * a[0] * b[0];
2677 ta = 0.28209479f * a[0] - 0.12615663f * a[6] - 0.21850969f * a[8];
2678 tb = 0.28209479f * b[0] - 0.12615663f * b[6] - 0.21850969f * b[8];
2679 out[1] = ta * b[1] + tb * a[1];
2680 t = a[1] * b[1];
2681 out[0] += 0.28209479f * t;
2682 out[6] = -0.12615663f * t;
2683 out[8] = -0.21850969f * t;
2685 ta = 0.21850969f * a[3] - 0.05839917f * a[13] - 0.22617901f * a[15];
2686 tb = 0.21850969f * b[3] - 0.05839917f * b[13] - 0.22617901f * b[15];
2687 out[1] += ta * b[4] + tb * a[4];
2688 out[4] = ta * b[1] + tb * a[1];
2689 t = a[1] * b[4] + a[4] * b[1];
2690 out[3] = 0.21850969f * t;
2691 out[13] = -0.05839917f * t;
2692 out[15] = -0.22617901f * t;
2694 ta = 0.21850969f * a[2] - 0.14304817f * a[12] - 0.18467439f * a[14];
2695 tb = 0.21850969f * b[2] - 0.14304817f * b[12] - 0.18467439f * b[14];
2696 out[1] += ta * b[5] + tb * a[5];
2697 out[5] = ta * b[1] + tb * a[1];
2698 t = a[1] * b[5] + a[5] * b[1];
2699 out[2] = 0.21850969f * t;
2700 out[12] = -0.14304817f * t;
2701 out[14] = -0.18467439f * t;
2703 ta = 0.20230066f * a[11];
2704 tb = 0.20230066f * b[11];
2705 out[1] += ta * b[6] + tb * a[6];
2706 out[6] += ta * b[1] + tb * a[1];
2707 t = a[1] * b[6] + a[6] * b[1];
2708 out[11] = 0.20230066f * t;
2710 ta = 0.22617901f * a[9] + 0.05839917f * a[11];
2711 tb = 0.22617901f * b[9] + 0.05839917f * b[11];
2712 out[1] += ta * b[8] + tb * a[8];
2713 out[8] += ta * b[1] + tb * a[1];
2714 t = a[1] * b[8] + a[8] * b[1];
2715 out[9] = 0.22617901f * t;
2716 out[11] += 0.05839917f * t;
2718 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2719 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2720 out[2] += ta * b[2] + tb * a[2];
2721 t = a[2] * b[2];
2722 out[0] += 0.28209480f * t;
2723 out[6] += 0.25231326f * t;
2725 ta = 0.24776671f * a[12];
2726 tb = 0.24776671f * b[12];
2727 out[2] += ta * b[6] + tb * a[6];
2728 out[6] += ta * b[2] + tb * a[2];
2729 t = a[2] * b[6] + a[6] * b[2];
2730 out[12] += 0.24776671f * t;
2732 ta = 0.28209480f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2733 tb = 0.28209480f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2734 out[3] += ta * b[3] + tb * a[3];
2735 t = a[3] * b[3];
2736 out[0] += 0.28209480f * t;
2737 out[6] -= 0.12615663f * t;
2738 out[8] += 0.21850969f * t;
2740 ta = 0.20230066f * a[13];
2741 tb = 0.20230066f * b[13];
2742 out[3] += ta * b[6] + tb * a[6];
2743 out[6] += ta * b[3] + tb * a[3];
2744 t = a[3] * b[6] + a[6] * b[3];
2745 out[13] += 0.20230066f * t;
2747 ta = 0.21850969f * a[2] - 0.14304817f * a[12] + 0.18467439f * a[14];
2748 tb = 0.21850969f * b[2] - 0.14304817f * b[12] + 0.18467439f * b[14];
2749 out[3] += ta * b[7] + tb * a[7];
2750 out[7] = ta * b[3] + tb * a[3];
2751 t = a[3] * b[7] + a[7] * b[3];
2752 out[2] += 0.21850969f * t;
2753 out[12] -= 0.14304817f * t;
2754 out[14] += 0.18467439f * t;
2756 ta = -0.05839917f * a[13] + 0.22617901f * a[15];
2757 tb = -0.05839917f * b[13] + 0.22617901f * b[15];
2758 out[3] += ta * b[8] + tb * a[8];
2759 out[8] += ta * b[3] + tb * a[3];
2760 t = a[3] * b[8] + a[8] * b[3];
2761 out[13] -= 0.05839917f * t;
2762 out[15] += 0.22617901f * t;
2764 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2765 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2766 out[4] += ta * b[4] + tb * a[4];
2767 t = a[4] * b[4];
2768 out[0] += 0.28209479f * t;
2769 out[6] -= 0.18022375f * t;
2771 ta = 0.15607835f * a[7];
2772 tb = 0.15607835f * b[7];
2773 out[4] += ta * b[5] + tb * a[5];
2774 out[5] += ta * b[4] + tb * a[4];
2775 t = a[4] * b[5] + a[5] * b[4];
2776 out[7] += 0.15607835f * t;
2778 ta = 0.22617901f * a[3] - 0.09403160f * a[13];
2779 tb = 0.22617901f * b[3] - 0.09403160f * b[13];
2780 out[4] += ta * b[9] + tb * a[9];
2781 out[9] += ta * b[4] + tb * a[4];
2782 t = a[4] * b[9] + a[9] * b[4];
2783 out[3] += 0.22617901f * t;
2784 out[13] -= 0.09403160f * t;
2786 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2787 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2788 out[4] += ta * b[10] + tb * a [10];
2789 out[10] = ta * b[4] + tb * a[4];
2790 t = a[4] * b[10] + a[10] * b[4];
2791 out[2] += 0.18467439f * t;
2792 out[12] -= 0.18806319f * t;
2794 ta = -0.05839917f * a[3] + 0.14567312f * a[13] + 0.09403160f * a[15];
2795 tb = -0.05839917f * b[3] + 0.14567312f * b[13] + 0.09403160f * b[15];
2796 out[4] += ta * b[11] + tb * a[11];
2797 out[11] += ta * b[4] + tb * a[4];
2798 t = a[4] * b[11] + a[11] * b[4];
2799 out[3] -= 0.05839917f * t;
2800 out[13] += 0.14567312f * t;
2801 out[15] += 0.09403160f * t;
2803 ta = 0.28209479f * a[0] + 0.09011186f * a[6] - 0.15607835f * a[8];
2804 tb = 0.28209479f * b[0] + 0.09011186f * b[6] - 0.15607835f * b[8];
2805 out[5] += ta * b[5] + tb * a[5];
2806 t = a[5] * b[5];
2807 out[0] += 0.28209479f * t;
2808 out[6] += 0.09011186f * t;
2809 out[8] -= 0.15607835f * t;
2811 ta = 0.14867701f * a[14];
2812 tb = 0.14867701f * b[14];
2813 out[5] += ta * b[9] + tb * a[9];
2814 out[9] += ta * b[5] + tb * a[5];
2815 t = a[5] * b[9] + a[9] * b[5];
2816 out[14] += 0.14867701f * t;
2818 ta = 0.18467439f * a[3] + 0.11516472f * a[13] - 0.14867701f * a[15];
2819 tb = 0.18467439f * b[3] + 0.11516472f * b[13] - 0.14867701f * b[15];
2820 out[5] += ta * b[10] + tb * a[10];
2821 out[10] += ta * b[5] + tb * a[5];
2822 t = a[5] * b[10] + a[10] * b[5];
2823 out[3] += 0.18467439f * t;
2824 out[13] += 0.11516472f * t;
2825 out[15] -= 0.14867701f * t;
2827 ta = 0.23359668f * a[2] + 0.05947080f * a[12] - 0.11516472f * a[14];
2828 tb = 0.23359668f * b[2] + 0.05947080f * b[12] - 0.11516472f * b[14];
2829 out[5] += ta * b[11] + tb * a[11];
2830 out[11] += ta * b[5] + tb * a[5];
2831 t = a[5] * b[11] + a[11] * b[5];
2832 out[2] += 0.23359668f * t;
2833 out[12] += 0.05947080f * t;
2834 out[14] -= 0.11516472f * t;
2836 ta = 0.28209479f * a[0];
2837 tb = 0.28209479f * b[0];
2838 out[6] += ta * b[6] + tb * a[6];
2839 t = a[6] * b[6];
2840 out[0] += 0.28209479f * t;
2841 out[6] += 0.18022376f * t;
2843 ta = 0.09011186f * a[6] + 0.28209479f * a[0] + 0.15607835f * a[8];
2844 tb = 0.09011186f * b[6] + 0.28209479f * b[0] + 0.15607835f * b[8];
2845 out[7] += ta * b[7] + tb * a[7];
2846 t = a[7] * b[7];
2847 out[6] += 0.09011186f * t;
2848 out[0] += 0.28209479f * t;
2849 out[8] += 0.15607835f * t;
2851 ta = 0.14867701f * a[9] + 0.18467439f * a[1] + 0.11516472f * a[11];
2852 tb = 0.14867701f * b[9] + 0.18467439f * b[1] + 0.11516472f * b[11];
2853 out[7] += ta * b[10] + tb * a[10];
2854 out[10] += ta * b[7] + tb * a[7];
2855 t = a[7] * b[10] + a[10] * b[7];
2856 out[9] += 0.14867701f * t;
2857 out[1] += 0.18467439f * t;
2858 out[11] += 0.11516472f * t;
2860 ta = 0.05947080f * a[12] + 0.23359668f * a[2] + 0.11516472f * a[14];
2861 tb = 0.05947080f * b[12] + 0.23359668f * b[2] + 0.11516472f * b[14];
2862 out[7] += ta * b[13] + tb * a[13];
2863 out[13] += ta * b[7]+ tb * a[7];
2864 t = a[7] * b[13] + a[13] * b[7];
2865 out[12] += 0.05947080f * t;
2866 out[2] += 0.23359668f * t;
2867 out[14] += 0.11516472f * t;
2869 ta = 0.14867701f * a[15];
2870 tb = 0.14867701f * b[15];
2871 out[7] += ta * b[14] + tb * a[14];
2872 out[14] += ta * b[7] + tb * a[7];
2873 t = a[7] * b[14] + a[14] * b[7];
2874 out[15] += 0.14867701f * t;
2876 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2877 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2878 out[8] += ta * b[8] + tb * a[8];
2879 t = a[8] * b[8];
2880 out[0] += 0.28209479f * t;
2881 out[6] -= 0.18022375f * t;
2883 ta = -0.09403160f * a[11];
2884 tb = -0.09403160f * b[11];
2885 out[8] += ta * b[9] + tb * a[9];
2886 out[9] += ta * b[8] + tb * a[8];
2887 t = a[8] * b[9] + a[9] * b[8];
2888 out[11] -= 0.09403160f * t;
2890 ta = -0.09403160f * a[15];
2891 tb = -0.09403160f * b[15];
2892 out[8] += ta * b[13] + tb * a[13];
2893 out[13] += ta * b[8] + tb * a[8];
2894 t = a[8] * b[13] + a[13] * b[8];
2895 out[15] -= 0.09403160f * t;
2897 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2898 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2899 out[8] += ta * b[14] + tb * a[14];
2900 out[14] += ta * b[8] + tb * a[8];
2901 t = a[8] * b[14] + a[14] * b[8];
2902 out[2] += 0.18467439f * t;
2903 out[12] -= 0.18806319f * t;
2905 ta = -0.21026104f * a[6] + 0.28209479f * a[0];
2906 tb = -0.21026104f * b[6] + 0.28209479f * b[0];
2907 out[9] += ta * b[9] + tb * a[9];
2908 t = a[9] * b[9];
2909 out[6] -= 0.21026104f * t;
2910 out[0] += 0.28209479f * t;
2912 ta = 0.28209479f * a[0];
2913 tb = 0.28209479f * b[0];
2914 out[10] += ta * b[10] + tb * a[10];
2915 t = a[10] * b[10];
2916 out[0] += 0.28209479f * t;
2918 ta = 0.28209479f * a[0] + 0.12615663f * a[6] - 0.14567312f * a[8];
2919 tb = 0.28209479f * b[0] + 0.12615663f * b[6] - 0.14567312f * b[8];
2920 out[11] += ta * b[11] + tb * a[11];
2921 t = a[11] * b[11];
2922 out[0] += 0.28209479f * t;
2923 out[6] += 0.12615663f * t;
2924 out[8] -= 0.14567312f * t;
2926 ta = 0.28209479f * a[0] + 0.16820885f * a[6];
2927 tb = 0.28209479f * b[0] + 0.16820885f * b[6];
2928 out[12] += ta * b[12] + tb * a[12];
2929 t = a[12] * b[12];
2930 out[0] += 0.28209479f * t;
2931 out[6] += 0.16820885f * t;
2933 ta =0.28209479f * a[0] + 0.14567312f * a[8] + 0.12615663f * a[6];
2934 tb =0.28209479f * b[0] + 0.14567312f * b[8] + 0.12615663f * b[6];
2935 out[13] += ta * b[13] + tb * a[13];
2936 t = a[13] * b[13];
2937 out[0] += 0.28209479f * t;
2938 out[8] += 0.14567312f * t;
2939 out[6] += 0.12615663f * t;
2941 ta = 0.28209479f * a[0];
2942 tb = 0.28209479f * b[0];
2943 out[14] += ta * b[14] + tb * a[14];
2944 t = a[14] * b[14];
2945 out[0] += 0.28209479f * t;
2947 ta = 0.28209479f * a[0] - 0.21026104f * a[6];
2948 tb = 0.28209479f * b[0] - 0.21026104f * b[6];
2949 out[15] += ta * b[15] + tb * a[15];
2950 t = a[15] * b[15];
2951 out[0] += 0.28209479f * t;
2952 out[6] -= 0.21026104f * t;
2954 return out;
2957 static void rotate_X(FLOAT *out, UINT order, FLOAT a, FLOAT *in)
2959 out[0] = in[0];
2961 out[1] = a * in[2];
2962 out[2] = -a * in[1];
2963 out[3] = in[3];
2965 out[4] = a * in[7];
2966 out[5] = -in[5];
2967 out[6] = -0.5f * in[6] - 0.8660253882f * in[8];
2968 out[7] = -a * in[4];
2969 out[8] = -0.8660253882f * in[6] + 0.5f * in[8];
2970 out[9] = -a * 0.7905694842f * in[12] + a * 0.6123724580f * in[14];
2972 out[10] = -in[10];
2973 out[11] = -a * 0.6123724580f * in[12] - a * 0.7905694842f * in[14];
2974 out[12] = a * 0.7905694842f * in[9] + a * 0.6123724580f * in[11];
2975 out[13] = -0.25f * in[13] - 0.9682458639f * in[15];
2976 out[14] = -a * 0.6123724580f * in[9] + a * 0.7905694842f * in[11];
2977 out[15] = -0.9682458639f * in[13] + 0.25f * in[15];
2978 if (order == 4)
2979 return;
2981 out[16] = -a * 0.9354143739f * in[21] + a * 0.3535533845f * in[23];
2982 out[17] = -0.75f * in[17] + 0.6614378095f * in[19];
2983 out[18] = -a * 0.3535533845f * in[21] - a * 0.9354143739f * in[23];
2984 out[19] = 0.6614378095f * in[17] + 0.75f * in[19];
2985 out[20] = 0.375f * in[20] + 0.5590170026f * in[22] + 0.7395099998f * in[24];
2986 out[21] = a * 0.9354143739f * in[16] + a * 0.3535533845f * in[18];
2987 out[22] = 0.5590170026f * in[20] + 0.5f * in[22] - 0.6614378691f * in[24];
2988 out[23] = -a * 0.3535533845f * in[16] + a * 0.9354143739f * in[18];
2989 out[24] = 0.7395099998f * in[20] - 0.6614378691f * in[22] + 0.125f * in[24];
2990 if (order == 5)
2991 return;
2993 out[25] = a * 0.7015607357f * in[30] - a * 0.6846531630f * in[32] + a * 0.1976423711f * in[34];
2994 out[26] = -0.5f * in[26] + 0.8660253882f * in[28];
2995 out[27] = a * 0.5229125023f * in[30] + a * 0.3061861992f * in[32] - a * 0.7954951525f * in[34];
2996 out[28] = 0.8660253882f * in[26] + 0.5f * in[28];
2997 out[29] = a * 0.4841229022f * in[30] + a * 0.6614378691f * in[32] + a * 0.5728219748f * in[34];
2998 out[30] = -a * 0.7015607357f * in[25] - a * 0.5229125023f * in[27] - a * 0.4841229022f * in[29];
2999 out[31] = 0.125f * in[31] + 0.4050463140f * in[33] + 0.9057110548f * in[35];
3000 out[32] = a * 0.6846531630f * in[25] - a * 0.3061861992f * in[27] - a * 0.6614378691f * in[29];
3001 out[33] = 0.4050463140f * in[31] + 0.8125f * in[33] - 0.4192627370f * in[35];
3002 out[34] = -a * 0.1976423711f * in[25] + a * 0.7954951525f * in[27] - a * 0.5728219748f * in[29];
3003 out[35] = 0.9057110548f * in[31] - 0.4192627370f * in[33] + 0.0624999329f * in[35];
3006 FLOAT* WINAPI D3DXSHRotate(FLOAT *out, UINT order, const D3DXMATRIX *matrix, const FLOAT *in)
3008 FLOAT alpha, beta, gamma, sinb, temp[36], temp1[36];
3010 TRACE("out %p, order %u, matrix %p, in %p\n", out, order, matrix, in);
3012 out[0] = in[0];
3014 if ((order > D3DXSH_MAXORDER) || (order < D3DXSH_MINORDER))
3015 return out;
3017 if (order <= 3)
3019 out[1] = matrix->u.m[1][1] * in[1] - matrix->u.m[2][1] * in[2] + matrix->u.m[0][1] * in[3];
3020 out[2] = -matrix->u.m[1][2] * in[1] + matrix->u.m[2][2] * in[2] - matrix->u.m[0][2] * in[3];
3021 out[3] = matrix->u.m[1][0] * in[1] - matrix->u.m[2][0] * in[2] + matrix->u.m[0][0] * in[3];
3023 if (order == 3)
3025 FLOAT coeff[]={
3026 matrix->u.m[1][0] * matrix->u.m[0][0], matrix->u.m[1][1] * matrix->u.m[0][1],
3027 matrix->u.m[1][1] * matrix->u.m[2][1], matrix->u.m[1][0] * matrix->u.m[2][0],
3028 matrix->u.m[2][0] * matrix->u.m[2][0], matrix->u.m[2][1] * matrix->u.m[2][1],
3029 matrix->u.m[0][0] * matrix->u.m[2][0], matrix->u.m[0][1] * matrix->u.m[2][1],
3030 matrix->u.m[0][1] * matrix->u.m[0][1], matrix->u.m[1][0] * matrix->u.m[1][0],
3031 matrix->u.m[1][1] * matrix->u.m[1][1], matrix->u.m[0][0] * matrix->u.m[0][0], };
3033 out[4] = (matrix->u.m[1][1] * matrix->u.m[0][0] + matrix->u.m[0][1] * matrix->u.m[1][0]) * in[4];
3034 out[4] -= (matrix->u.m[1][0] * matrix->u.m[2][1] + matrix->u.m[1][1] * matrix->u.m[2][0]) * in[5];
3035 out[4] += 1.7320508076f * matrix->u.m[2][0] * matrix->u.m[2][1] * in[6];
3036 out[4] -= (matrix->u.m[0][1] * matrix->u.m[2][0] + matrix->u.m[0][0] * matrix->u.m[2][1]) * in[7];
3037 out[4] += (matrix->u.m[0][0] * matrix->u.m[0][1] - matrix->u.m[1][0] * matrix->u.m[1][1]) * in[8];
3039 out[5] = (matrix->u.m[1][1] * matrix->u.m[2][2] + matrix->u.m[1][2] * matrix->u.m[2][1]) * in[5];
3040 out[5] -= (matrix->u.m[1][1] * matrix->u.m[0][2] + matrix->u.m[1][2] * matrix->u.m[0][1]) * in[4];
3041 out[5] -= 1.7320508076f * matrix->u.m[2][2] * matrix->u.m[2][1] * in[6];
3042 out[5] += (matrix->u.m[0][2] * matrix->u.m[2][1] + matrix->u.m[0][1] * matrix->u.m[2][2]) * in[7];
3043 out[5] -= (matrix->u.m[0][1] * matrix->u.m[0][2] - matrix->u.m[1][1] * matrix->u.m[1][2]) * in[8];
3045 out[6] = (matrix->u.m[2][2] * matrix->u.m[2][2] - 0.5f * (coeff[4] + coeff[5])) * in[6];
3046 out[6] -= (0.5773502692f * (coeff[0] + coeff[1]) - 1.1547005384f * matrix->u.m[1][2] * matrix->u.m[0][2]) * in[4];
3047 out[6] += (0.5773502692f * (coeff[2] + coeff[3]) - 1.1547005384f * matrix->u.m[1][2] * matrix->u.m[2][2]) * in[5];
3048 out[6] += (0.5773502692f * (coeff[6] + coeff[7]) - 1.1547005384f * matrix->u.m[0][2] * matrix->u.m[2][2]) * in[7];
3049 out[6] += (0.2886751347f * (coeff[9] - coeff[8] + coeff[10] - coeff[11]) - 0.5773502692f *
3050 (matrix->u.m[1][2] * matrix->u.m[1][2] - matrix->u.m[0][2] * matrix->u.m[0][2])) * in[8];
3052 out[7] = (matrix->u.m[0][0] * matrix->u.m[2][2] + matrix->u.m[0][2] * matrix->u.m[2][0]) * in[7];
3053 out[7] -= (matrix->u.m[1][0] * matrix->u.m[0][2] + matrix->u.m[1][2] * matrix->u.m[0][0]) * in[4];
3054 out[7] += (matrix->u.m[1][0] * matrix->u.m[2][2] + matrix->u.m[1][2] * matrix->u.m[2][0]) * in[5];
3055 out[7] -= 1.7320508076f * matrix->u.m[2][2] * matrix->u.m[2][0] * in[6];
3056 out[7] -= (matrix->u.m[0][0] * matrix->u.m[0][2] - matrix->u.m[1][0] * matrix->u.m[1][2]) * in[8];
3058 out[8] = 0.5f * (coeff[11] - coeff[8] - coeff[9] + coeff[10]) * in[8];
3059 out[8] += (coeff[0] - coeff[1]) * in[4];
3060 out[8] += (coeff[2] - coeff[3]) * in[5];
3061 out[8] += 0.86602540f * (coeff[4] - coeff[5]) * in[6];
3062 out[8] += (coeff[7] - coeff[6]) * in[7];
3065 return out;
3068 if (fabsf(matrix->u.m[2][2]) != 1.0f)
3070 sinb = sqrtf(1.0f - matrix->u.m[2][2] * matrix->u.m[2][2]);
3071 alpha = atan2f(matrix->u.m[2][1] / sinb, matrix->u.m[2][0] / sinb);
3072 beta = atan2f(sinb, matrix->u.m[2][2]);
3073 gamma = atan2f(matrix->u.m[1][2] / sinb, -matrix->u.m[0][2] / sinb);
3075 else
3077 alpha = atan2f(matrix->u.m[0][1], matrix->u.m[0][0]);
3078 beta = 0.0f;
3079 gamma = 0.0f;
3082 D3DXSHRotateZ(temp, order, gamma, in);
3083 rotate_X(temp1, order, 1.0f, temp);
3084 D3DXSHRotateZ(temp, order, beta, temp1);
3085 rotate_X(temp1, order, -1.0f, temp);
3086 D3DXSHRotateZ(out, order, alpha, temp1);
3088 return out;
3091 FLOAT * WINAPI D3DXSHRotateZ(FLOAT *out, UINT order, FLOAT angle, const FLOAT *in)
3093 UINT i, sum = 0;
3094 FLOAT c[5], s[5];
3096 TRACE("out %p, order %u, angle %f, in %p\n", out, order, angle, in);
3098 order = min(max(order, D3DXSH_MINORDER), D3DXSH_MAXORDER);
3100 out[0] = in[0];
3102 for (i = 1; i < order; i++)
3104 UINT j;
3106 c[i - 1] = cosf(i * angle);
3107 s[i - 1] = sinf(i * angle);
3108 sum += i * 2;
3110 out[sum - i] = c[i - 1] * in[sum - i];
3111 out[sum - i] += s[i - 1] * in[sum + i];
3112 for (j = i - 1; j > 0; j--)
3114 out[sum - j] = 0.0f;
3115 out[sum - j] = c[j - 1] * in[sum - j];
3116 out[sum - j] += s[j - 1] * in[sum + j];
3119 if (in == out)
3120 out[sum] = 0.0f;
3121 else
3122 out[sum] = in[sum];
3124 for (j = 1; j < i; j++)
3126 out[sum + j] = 0.0f;
3127 out[sum + j] = -s[j - 1] * in[sum - j];
3128 out[sum + j] += c[j - 1] * in[sum + j];
3130 out[sum + i] = -s[i - 1] * in[sum - i];
3131 out[sum + i] += c[i - 1] * in[sum + i];
3134 return out;
3137 FLOAT* WINAPI D3DXSHScale(FLOAT *out, UINT order, const FLOAT *a, const FLOAT scale)
3139 UINT i;
3141 TRACE("out %p, order %u, a %p, scale %f\n", out, order, a, scale);
3143 for (i = 0; i < order * order; i++)
3144 out[i] = a[i] * scale;
3146 return out;