include: Add event trace flags and guid to evntrace.h.
[wine.git] / dlls / d3dx9_36 / math.c
blob09c055e7275a8187faf87a4893b454e0c587d2ed
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 paxis->x = pq->x;
1656 paxis->y = pq->y;
1657 paxis->z = pq->z;
1658 *pangle = 2.0f * acosf(pq->w);
1661 /*_________________D3DXVec2_____________________*/
1663 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1665 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
1667 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1668 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1669 return pout;
1672 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv0, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pv3, FLOAT s)
1674 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
1676 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);
1677 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);
1678 return pout;
1681 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pt1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pt2, FLOAT s)
1683 FLOAT h1, h2, h3, h4;
1685 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
1687 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1688 h2 = s * s * s - 2.0f * s * s + s;
1689 h3 = -2.0f * s * s * s + 3.0f * s * s;
1690 h4 = s * s * s - s * s;
1692 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1693 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1694 return pout;
1697 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv)
1699 FLOAT norm;
1701 TRACE("pout %p, pv %p\n", pout, pv);
1703 norm = D3DXVec2Length(pv);
1704 if ( !norm )
1706 pout->x = 0.0f;
1707 pout->y = 0.0f;
1709 else
1711 pout->x = pv->x / norm;
1712 pout->y = pv->y / norm;
1715 return pout;
1718 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1720 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1722 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
1723 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
1724 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
1725 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1726 return pout;
1729 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR2* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1731 UINT i;
1733 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1735 for (i = 0; i < elements; ++i) {
1736 D3DXVec2Transform(
1737 (D3DXVECTOR4*)((char*)out + outstride * i),
1738 (const D3DXVECTOR2*)((const char*)in + instride * i),
1739 matrix);
1741 return out;
1744 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1746 D3DXVECTOR2 v;
1747 FLOAT norm;
1749 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1751 v = *pv;
1752 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1754 pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1755 pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1757 return pout;
1760 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, const D3DXVECTOR2* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1762 UINT i;
1764 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1766 for (i = 0; i < elements; ++i) {
1767 D3DXVec2TransformCoord(
1768 (D3DXVECTOR2*)((char*)out + outstride * i),
1769 (const D3DXVECTOR2*)((const char*)in + instride * i),
1770 matrix);
1772 return out;
1775 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1777 const D3DXVECTOR2 v = *pv;
1779 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1781 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1782 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1783 return pout;
1786 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, const D3DXVECTOR2 *in, UINT instride, const D3DXMATRIX *matrix, UINT elements)
1788 UINT i;
1790 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1792 for (i = 0; i < elements; ++i) {
1793 D3DXVec2TransformNormal(
1794 (D3DXVECTOR2*)((char*)out + outstride * i),
1795 (const D3DXVECTOR2*)((const char*)in + instride * i),
1796 matrix);
1798 return out;
1801 /*_________________D3DXVec3_____________________*/
1803 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1805 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
1807 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1808 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1809 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1810 return pout;
1813 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv0, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3, FLOAT s)
1815 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
1817 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);
1818 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);
1819 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);
1820 return pout;
1823 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pt1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pt2, FLOAT s)
1825 FLOAT h1, h2, h3, h4;
1827 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
1829 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1830 h2 = s * s * s - 2.0f * s * s + s;
1831 h3 = -2.0f * s * s * s + 3.0f * s * s;
1832 h4 = s * s * s - s * s;
1834 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1835 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1836 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1837 return pout;
1840 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv)
1842 FLOAT norm;
1844 TRACE("pout %p, pv %p\n", pout, pv);
1846 norm = D3DXVec3Length(pv);
1847 if ( !norm )
1849 pout->x = 0.0f;
1850 pout->y = 0.0f;
1851 pout->z = 0.0f;
1853 else
1855 pout->x = pv->x / norm;
1856 pout->y = pv->y / norm;
1857 pout->z = pv->z / norm;
1860 return pout;
1863 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DVIEWPORT9 *pviewport, const D3DXMATRIX *pprojection, const D3DXMATRIX *pview, const D3DXMATRIX *pworld)
1865 D3DXMATRIX m;
1867 TRACE("pout %p, pv %p, pviewport %p, pprojection %p, pview %p, pworld %p\n", pout, pv, pviewport, pprojection, pview, pworld);
1869 D3DXMatrixIdentity(&m);
1870 if (pworld) D3DXMatrixMultiply(&m, &m, pworld);
1871 if (pview) D3DXMatrixMultiply(&m, &m, pview);
1872 if (pprojection) D3DXMatrixMultiply(&m, &m, pprojection);
1874 D3DXVec3TransformCoord(pout, pv, &m);
1876 if (pviewport)
1878 pout->x = pviewport->X + ( 1.0f + pout->x ) * pviewport->Width / 2.0f;
1879 pout->y = pviewport->Y + ( 1.0f - pout->y ) * pviewport->Height / 2.0f;
1880 pout->z = pviewport->MinZ + pout->z * ( pviewport->MaxZ - pviewport->MinZ );
1882 return pout;
1885 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)
1887 UINT i;
1889 TRACE("out %p, outstride %u, in %p, instride %u, viewport %p, projection %p, view %p, world %p, elements %u\n",
1890 out, outstride, in, instride, viewport, projection, view, world, elements);
1892 for (i = 0; i < elements; ++i) {
1893 D3DXVec3Project(
1894 (D3DXVECTOR3*)((char*)out + outstride * i),
1895 (const D3DXVECTOR3*)((const char*)in + instride * i),
1896 viewport, projection, view, world);
1898 return out;
1901 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1903 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1905 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];
1906 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];
1907 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];
1908 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];
1909 return pout;
1912 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1914 UINT i;
1916 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1918 for (i = 0; i < elements; ++i) {
1919 D3DXVec3Transform(
1920 (D3DXVECTOR4*)((char*)out + outstride * i),
1921 (const D3DXVECTOR3*)((const char*)in + instride * i),
1922 matrix);
1924 return out;
1927 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1929 D3DXVECTOR3 out;
1930 FLOAT norm;
1932 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1934 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];
1936 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;
1937 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;
1938 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;
1940 *pout = out;
1942 return pout;
1945 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(D3DXVECTOR3* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1947 UINT i;
1949 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1951 for (i = 0; i < elements; ++i) {
1952 D3DXVec3TransformCoord(
1953 (D3DXVECTOR3*)((char*)out + outstride * i),
1954 (const D3DXVECTOR3*)((const char*)in + instride * i),
1955 matrix);
1957 return out;
1960 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1962 const D3DXVECTOR3 v = *pv;
1964 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1966 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1967 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1968 pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1969 return pout;
1973 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1975 UINT i;
1977 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1979 for (i = 0; i < elements; ++i) {
1980 D3DXVec3TransformNormal(
1981 (D3DXVECTOR3*)((char*)out + outstride * i),
1982 (const D3DXVECTOR3*)((const char*)in + instride * i),
1983 matrix);
1985 return out;
1988 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DVIEWPORT9 *pviewport, const D3DXMATRIX *pprojection, const D3DXMATRIX *pview, const D3DXMATRIX *pworld)
1990 D3DXMATRIX m;
1992 TRACE("pout %p, pv %p, pviewport %p, pprojection %p, pview %p, pworlds %p\n", pout, pv, pviewport, pprojection, pview, pworld);
1994 D3DXMatrixIdentity(&m);
1995 if (pworld) D3DXMatrixMultiply(&m, &m, pworld);
1996 if (pview) D3DXMatrixMultiply(&m, &m, pview);
1997 if (pprojection) D3DXMatrixMultiply(&m, &m, pprojection);
1998 D3DXMatrixInverse(&m, NULL, &m);
2000 *pout = *pv;
2001 if (pviewport)
2003 pout->x = 2.0f * ( pout->x - pviewport->X ) / pviewport->Width - 1.0f;
2004 pout->y = 1.0f - 2.0f * ( pout->y - pviewport->Y ) / pviewport->Height;
2005 pout->z = ( pout->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
2007 D3DXVec3TransformCoord(pout, pout, &m);
2008 return pout;
2011 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)
2013 UINT i;
2015 TRACE("out %p, outstride %u, in %p, instride %u, viewport %p, projection %p, view %p, world %p, elements %u\n",
2016 out, outstride, in, instride, viewport, projection, view, world, elements);
2018 for (i = 0; i < elements; ++i) {
2019 D3DXVec3Unproject(
2020 (D3DXVECTOR3*)((char*)out + outstride * i),
2021 (const D3DXVECTOR3*)((const char*)in + instride * i),
2022 viewport, projection, view, world);
2024 return out;
2027 /*_________________D3DXVec4_____________________*/
2029 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
2031 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
2033 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
2034 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
2035 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
2036 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
2037 return pout;
2040 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv0, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3, FLOAT s)
2042 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
2044 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);
2045 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);
2046 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);
2047 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);
2048 return pout;
2051 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3)
2053 D3DXVECTOR4 out;
2055 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p\n", pout, pv1, pv2, pv3);
2057 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);
2058 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));
2059 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);
2060 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));
2061 *pout = out;
2062 return pout;
2065 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pt1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pt2, FLOAT s)
2067 FLOAT h1, h2, h3, h4;
2069 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
2071 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
2072 h2 = s * s * s - 2.0f * s * s + s;
2073 h3 = -2.0f * s * s * s + 3.0f * s * s;
2074 h4 = s * s * s - s * s;
2076 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
2077 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
2078 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
2079 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
2080 return pout;
2083 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv)
2085 FLOAT norm;
2087 TRACE("pout %p, pv %p\n", pout, pv);
2089 norm = D3DXVec4Length(pv);
2091 pout->x = pv->x / norm;
2092 pout->y = pv->y / norm;
2093 pout->z = pv->z / norm;
2094 pout->w = pv->w / norm;
2096 return pout;
2099 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv, const D3DXMATRIX *pm)
2101 D3DXVECTOR4 out;
2103 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
2105 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;
2106 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;
2107 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;
2108 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;
2109 *pout = out;
2110 return pout;
2113 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR4* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
2115 UINT i;
2117 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
2119 for (i = 0; i < elements; ++i) {
2120 D3DXVec4Transform(
2121 (D3DXVECTOR4*)((char*)out + outstride * i),
2122 (const D3DXVECTOR4*)((const char*)in + instride * i),
2123 matrix);
2125 return out;
2128 unsigned short float_32_to_16(const float in)
2130 int exp = 0, origexp;
2131 float tmp = fabsf(in);
2132 int sign = (copysignf(1, in) < 0);
2133 unsigned int mantissa;
2134 unsigned short ret;
2136 /* Deal with special numbers */
2137 if (isinf(in)) return (sign ? 0xffff : 0x7fff);
2138 if (isnan(in)) return (sign ? 0xffff : 0x7fff);
2139 if (in == 0.0f) return (sign ? 0x8000 : 0x0000);
2141 if (tmp < powf(2, 10))
2145 tmp *= 2.0f;
2146 exp--;
2147 } while (tmp < powf(2, 10));
2149 else if (tmp >= powf(2, 11))
2153 tmp /= 2.0f;
2154 exp++;
2155 } while (tmp >= powf(2, 11));
2158 exp += 10; /* Normalize the mantissa */
2159 exp += 15; /* Exponent is encoded with excess 15 */
2161 origexp = exp;
2163 mantissa = (unsigned int) tmp;
2164 if ((tmp - mantissa == 0.5f && mantissa % 2 == 1) || /* round half to even */
2165 (tmp - mantissa > 0.5f))
2167 mantissa++; /* round to nearest, away from zero */
2169 if (mantissa == 2048)
2171 mantissa = 1024;
2172 exp++;
2175 if (exp > 31)
2177 /* too big */
2178 ret = 0x7fff; /* INF */
2180 else if (exp <= 0)
2182 unsigned int rounding = 0;
2184 /* Denormalized half float */
2186 /* return 0x0000 (=0.0) for numbers too small to represent in half floats */
2187 if (exp < -11)
2188 return (sign ? 0x8000 : 0x0000);
2190 exp = origexp;
2192 /* the 13 extra bits from single precision are used for rounding */
2193 mantissa = (unsigned int)(tmp * powf(2, 13));
2194 mantissa >>= 1 - exp; /* denormalize */
2196 mantissa -= ~(mantissa >> 13) & 1; /* round half to even */
2197 /* remove 13 least significant bits to get half float precision */
2198 mantissa >>= 12;
2199 rounding = mantissa & 1;
2200 mantissa >>= 1;
2202 ret = mantissa + rounding;
2204 else
2206 ret = (exp << 10) | (mantissa & 0x3ff);
2209 ret |= ((sign ? 1 : 0) << 15); /* Add the sign */
2210 return ret;
2213 D3DXFLOAT16 *WINAPI D3DXFloat32To16Array(D3DXFLOAT16 *pout, const FLOAT *pin, UINT n)
2215 unsigned int i;
2217 TRACE("pout %p, pin %p, n %u\n", pout, pin, n);
2219 for (i = 0; i < n; ++i)
2221 pout[i].value = float_32_to_16(pin[i]);
2224 return pout;
2227 /* Native d3dx9's D3DXFloat16to32Array lacks support for NaN and Inf. Specifically, e = 16 is treated as a
2228 * regular number - e.g., 0x7fff is converted to 131008.0 and 0xffff to -131008.0. */
2229 float float_16_to_32(const unsigned short in)
2231 const unsigned short s = (in & 0x8000);
2232 const unsigned short e = (in & 0x7C00) >> 10;
2233 const unsigned short m = in & 0x3FF;
2234 const float sgn = (s ? -1.0f : 1.0f);
2236 if (e == 0)
2238 if (m == 0) return sgn * 0.0f; /* +0.0 or -0.0 */
2239 else return sgn * powf(2, -14.0f) * (m / 1024.0f);
2241 else
2243 return sgn * powf(2, e - 15.0f) * (1.0f + (m / 1024.0f));
2247 FLOAT *WINAPI D3DXFloat16To32Array(FLOAT *pout, const D3DXFLOAT16 *pin, UINT n)
2249 unsigned int i;
2251 TRACE("pout %p, pin %p, n %u\n", pout, pin, n);
2253 for (i = 0; i < n; ++i)
2255 pout[i] = float_16_to_32(pin[i].value);
2258 return pout;
2261 /*_________________D3DXSH________________*/
2263 FLOAT* WINAPI D3DXSHAdd(FLOAT *out, UINT order, const FLOAT *a, const FLOAT *b)
2265 UINT i;
2267 TRACE("out %p, order %u, a %p, b %p\n", out, order, a, b);
2269 for (i = 0; i < order * order; i++)
2270 out[i] = a[i] + b[i];
2272 return out;
2275 FLOAT WINAPI D3DXSHDot(UINT order, const FLOAT *a, const FLOAT *b)
2277 FLOAT s;
2278 UINT i;
2280 TRACE("order %u, a %p, b %p\n", order, a, b);
2282 s = a[0] * b[0];
2283 for (i = 1; i < order * order; i++)
2284 s += a[i] * b[i];
2286 return s;
2289 static void weightedcapintegrale(FLOAT *out, UINT order, FLOAT angle)
2291 FLOAT coeff[3];
2293 coeff[0] = cosf(angle);
2295 out[0] = 2.0f * D3DX_PI * (1.0f - coeff[0]);
2296 out[1] = D3DX_PI * sinf(angle) * sinf(angle);
2297 if (order <= 2)
2298 return;
2300 out[2] = coeff[0] * out[1];
2301 if (order == 3)
2302 return;
2304 coeff[1] = coeff[0] * coeff[0];
2305 coeff[2] = coeff[1] * coeff[1];
2307 out[3] = D3DX_PI * (-1.25f * coeff[2] + 1.5f * coeff[1] - 0.25f);
2308 if (order == 4)
2309 return;
2311 out[4] = -0.25f * D3DX_PI * coeff[0] * (7.0f * coeff[2] - 10.0f * coeff[1] + 3.0f);
2312 if (order == 5)
2313 return;
2315 out[5] = D3DX_PI * (-2.625f * coeff[2] * coeff[1] + 4.375f * coeff[2] - 1.875f * coeff[1] + 0.125f);
2318 HRESULT WINAPI D3DXSHEvalConeLight(UINT order, const D3DXVECTOR3 *dir, FLOAT radius,
2319 FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *rout, FLOAT *gout, FLOAT *bout)
2321 FLOAT cap[6], clamped_angle, norm, scale, temp;
2322 UINT i, index, j;
2324 TRACE("order %u, dir %p, radius %f, red %f, green %f, blue %f, rout %p, gout %p, bout %p\n",
2325 order, dir, radius, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2327 if (radius <= 0.0f)
2328 return D3DXSHEvalDirectionalLight(order, dir, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2330 clamped_angle = (radius > D3DX_PI / 2.0f) ? (D3DX_PI / 2.0f) : radius;
2331 norm = sinf(clamped_angle) * sinf(clamped_angle);
2333 if (order > D3DXSH_MAXORDER)
2335 WARN("Order clamped at D3DXSH_MAXORDER\n");
2336 order = D3DXSH_MAXORDER;
2339 weightedcapintegrale(cap, order, radius);
2340 D3DXSHEvalDirection(rout, order, dir);
2342 for (i = 0; i < order; i++)
2344 scale = cap[i] / norm;
2346 for (j = 0; j < 2 * i + 1; j++)
2348 index = i * i + j;
2349 temp = rout[index] * scale;
2351 rout[index] = temp * Rintensity;
2352 if (gout)
2353 gout[index] = temp * Gintensity;
2354 if (bout)
2355 bout[index] = temp * Bintensity;
2359 return D3D_OK;
2362 FLOAT* WINAPI D3DXSHEvalDirection(FLOAT *out, UINT order, const D3DXVECTOR3 *dir)
2364 const FLOAT dirxx = dir->x * dir->x;
2365 const FLOAT dirxy = dir->x * dir->y;
2366 const FLOAT dirxz = dir->x * dir->z;
2367 const FLOAT diryy = dir->y * dir->y;
2368 const FLOAT diryz = dir->y * dir->z;
2369 const FLOAT dirzz = dir->z * dir->z;
2370 const FLOAT dirxxxx = dirxx * dirxx;
2371 const FLOAT diryyyy = diryy * diryy;
2372 const FLOAT dirzzzz = dirzz * dirzz;
2373 const FLOAT dirxyxy = dirxy * dirxy;
2375 TRACE("out %p, order %u, dir %p\n", out, order, dir);
2377 if ((order < D3DXSH_MINORDER) || (order > D3DXSH_MAXORDER))
2378 return out;
2380 out[0] = 0.5f / sqrtf(D3DX_PI);
2381 out[1] = -0.5f / sqrtf(D3DX_PI / 3.0f) * dir->y;
2382 out[2] = 0.5f / sqrtf(D3DX_PI / 3.0f) * dir->z;
2383 out[3] = -0.5f / sqrtf(D3DX_PI / 3.0f) * dir->x;
2384 if (order == 2)
2385 return out;
2387 out[4] = 0.5f / sqrtf(D3DX_PI / 15.0f) * dirxy;
2388 out[5] = -0.5f / sqrtf(D3DX_PI / 15.0f) * diryz;
2389 out[6] = 0.25f / sqrtf(D3DX_PI / 5.0f) * (3.0f * dirzz - 1.0f);
2390 out[7] = -0.5f / sqrtf(D3DX_PI / 15.0f) * dirxz;
2391 out[8] = 0.25f / sqrtf(D3DX_PI / 15.0f) * (dirxx - diryy);
2392 if (order == 3)
2393 return out;
2395 out[9] = -sqrtf(70.0f / D3DX_PI) / 8.0f * dir->y * (3.0f * dirxx - diryy);
2396 out[10] = sqrtf(105.0f / D3DX_PI) / 2.0f * dirxy * dir->z;
2397 out[11] = -sqrtf(42.0f / D3DX_PI) / 8.0f * dir->y * (-1.0f + 5.0f * dirzz);
2398 out[12] = sqrtf(7.0f / D3DX_PI) / 4.0f * dir->z * (5.0f * dirzz - 3.0f);
2399 out[13] = sqrtf(42.0f / D3DX_PI) / 8.0f * dir->x * (1.0f - 5.0f * dirzz);
2400 out[14] = sqrtf(105.0f / D3DX_PI) / 4.0f * dir->z * (dirxx - diryy);
2401 out[15] = -sqrtf(70.0f / D3DX_PI) / 8.0f * dir->x * (dirxx - 3.0f * diryy);
2402 if (order == 4)
2403 return out;
2405 out[16] = 0.75f * sqrtf(35.0f / D3DX_PI) * dirxy * (dirxx - diryy);
2406 out[17] = 3.0f * dir->z * out[9];
2407 out[18] = 0.75f * sqrtf(5.0f / D3DX_PI) * dirxy * (7.0f * dirzz - 1.0f);
2408 out[19] = 0.375f * sqrtf(10.0f / D3DX_PI) * diryz * (3.0f - 7.0f * dirzz);
2409 out[20] = 3.0f / (16.0f * sqrtf(D3DX_PI)) * (35.0f * dirzzzz - 30.f * dirzz + 3.0f);
2410 out[21] = 0.375f * sqrtf(10.0f / D3DX_PI) * dirxz * (3.0f - 7.0f * dirzz);
2411 out[22] = 0.375f * sqrtf(5.0f / D3DX_PI) * (dirxx - diryy) * (7.0f * dirzz - 1.0f);
2412 out[23] = 3.0f * dir->z * out[15];
2413 out[24] = 3.0f / 16.0f * sqrtf(35.0f / D3DX_PI) * (dirxxxx - 6.0f * dirxyxy + diryyyy);
2414 if (order == 5)
2415 return out;
2417 out[25] = -3.0f/ 32.0f * sqrtf(154.0f / D3DX_PI) * dir->y * (5.0f * dirxxxx - 10.0f * dirxyxy + diryyyy);
2418 out[26] = 0.75f * sqrtf(385.0f / D3DX_PI) * dirxy * dir->z * (dirxx - diryy);
2419 out[27] = sqrtf(770.0f / D3DX_PI) / 32.0f * dir->y * (3.0f * dirxx - diryy) * (1.0f - 9.0f * dirzz);
2420 out[28] = sqrtf(1155.0f / D3DX_PI) / 4.0f * dirxy * dir->z * (3.0f * dirzz - 1.0f);
2421 out[29] = sqrtf(165.0f / D3DX_PI) / 16.0f * dir->y * (14.0f * dirzz - 21.0f * dirzzzz - 1.0f);
2422 out[30] = sqrtf(11.0f / D3DX_PI) / 16.0f * dir->z * (63.0f * dirzzzz - 70.0f * dirzz + 15.0f);
2423 out[31] = sqrtf(165.0f / D3DX_PI) / 16.0f * dir->x * (14.0f * dirzz - 21.0f * dirzzzz - 1.0f);
2424 out[32] = sqrtf(1155.0f / D3DX_PI) / 8.0f * dir->z * (dirxx - diryy) * (3.0f * dirzz - 1.0f);
2425 out[33] = sqrtf(770.0f / D3DX_PI) / 32.0f * dir->x * (dirxx - 3.0f * diryy) * (1.0f - 9.0f * dirzz);
2426 out[34] = 3.0f / 16.0f * sqrtf(385.0f / D3DX_PI) * dir->z * (dirxxxx - 6.0f * dirxyxy + diryyyy);
2427 out[35] = -3.0f/ 32.0f * sqrtf(154.0f / D3DX_PI) * dir->x * (dirxxxx - 10.0f * dirxyxy + 5.0f * diryyyy);
2429 return out;
2432 HRESULT WINAPI D3DXSHEvalDirectionalLight(UINT order, const D3DXVECTOR3 *dir, FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *Rout, FLOAT *Gout, FLOAT *Bout)
2434 FLOAT s, temp;
2435 UINT j;
2437 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);
2439 s = 0.75f;
2440 if ( order > 2 )
2441 s += 5.0f / 16.0f;
2442 if ( order > 4 )
2443 s -= 3.0f / 32.0f;
2444 s /= D3DX_PI;
2446 D3DXSHEvalDirection(Rout, order, dir);
2447 for (j = 0; j < order * order; j++)
2449 temp = Rout[j] / s;
2451 Rout[j] = Rintensity * temp;
2452 if ( Gout )
2453 Gout[j] = Gintensity * temp;
2454 if ( Bout )
2455 Bout[j] = Bintensity * temp;
2458 return D3D_OK;
2461 HRESULT WINAPI D3DXSHEvalHemisphereLight(UINT order, const D3DXVECTOR3 *dir, D3DXCOLOR top, D3DXCOLOR bottom,
2462 FLOAT *rout, FLOAT *gout, FLOAT *bout)
2464 FLOAT a[2], temp[4];
2465 UINT i, j;
2467 TRACE("order %u, dir %p, rout %p, gout %p, bout %p\n", order, dir, rout, gout, bout);
2469 D3DXSHEvalDirection(temp, 2, dir);
2471 a[0] = (top.r + bottom.r) * 3.0f * D3DX_PI;
2472 a[1] = (top.r - bottom.r) * D3DX_PI;
2473 for (i = 0; i < order; i++)
2474 for (j = 0; j < 2 * i + 1; j++)
2475 if (i < 2)
2476 rout[i * i + j] = temp[i * i + j] * a[i];
2477 else
2478 rout[i * i + j] = 0.0f;
2480 if (gout)
2482 a[0] = (top.g + bottom.g) * 3.0f * D3DX_PI;
2483 a[1] = (top.g - bottom.g) * D3DX_PI;
2484 for (i = 0; i < order; i++)
2485 for (j = 0; j < 2 * i + 1; j++)
2486 if (i < 2)
2487 gout[i * i + j] = temp[i * i + j] * a[i];
2488 else
2489 gout[i * i + j] = 0.0f;
2492 if (bout)
2494 a[0] = (top.b + bottom.b) * 3.0f * D3DX_PI;
2495 a[1] = (top.b - bottom.b) * D3DX_PI;
2496 for (i = 0; i < order; i++)
2497 for (j = 0; j < 2 * i + 1; j++)
2498 if (i < 2)
2499 bout[i * i + j] = temp[i * i + j] * a[i];
2500 else
2501 bout[i * i + j] = 0.0f;
2504 return D3D_OK;
2507 HRESULT WINAPI D3DXSHEvalSphericalLight(UINT order, const D3DXVECTOR3 *dir, FLOAT radius,
2508 FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *rout, FLOAT *gout, FLOAT *bout)
2510 D3DXVECTOR3 normal;
2511 FLOAT cap[6], clamped_angle, dist, temp;
2512 UINT i, index, j;
2514 TRACE("order %u, dir %p, radius %f, red %f, green %f, blue %f, rout %p, gout %p, bout %p\n",
2515 order, dir, radius, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2517 if (order > D3DXSH_MAXORDER)
2519 WARN("Order clamped at D3DXSH_MAXORDER\n");
2520 order = D3DXSH_MAXORDER;
2523 if (radius < 0.0f)
2524 radius = -radius;
2526 dist = D3DXVec3Length(dir);
2527 clamped_angle = (dist <= radius) ? D3DX_PI / 2.0f : asinf(radius / dist);
2529 weightedcapintegrale(cap, order, clamped_angle);
2530 D3DXVec3Normalize(&normal, dir);
2531 D3DXSHEvalDirection(rout, order, &normal);
2533 for (i = 0; i < order; i++)
2534 for (j = 0; j < 2 * i + 1; j++)
2536 index = i * i + j;
2537 temp = rout[index] * cap[i];
2539 rout[index] = temp * Rintensity;
2540 if (gout)
2541 gout[index] = temp * Gintensity;
2542 if (bout)
2543 bout[index] = temp * Bintensity;
2546 return D3D_OK;
2549 FLOAT * WINAPI D3DXSHMultiply2(FLOAT *out, const FLOAT *a, const FLOAT *b)
2551 FLOAT ta, tb;
2553 TRACE("out %p, a %p, b %p\n", out, a, b);
2555 ta = 0.28209479f * a[0];
2556 tb = 0.28209479f * b[0];
2558 out[0] = 0.28209479f * D3DXSHDot(2, a, b);
2559 out[1] = ta * b[1] + tb * a[1];
2560 out[2] = ta * b[2] + tb * a[2];
2561 out[3] = ta * b[3] + tb * a[3];
2563 return out;
2566 FLOAT * WINAPI D3DXSHMultiply3(FLOAT *out, const FLOAT *a, const FLOAT *b)
2568 FLOAT t, ta, tb;
2570 TRACE("out %p, a %p, b %p\n", out, a, b);
2572 out[0] = 0.28209479f * a[0] * b[0];
2574 ta = 0.28209479f * a[0] - 0.12615662f * a[6] - 0.21850968f * a[8];
2575 tb = 0.28209479f * b[0] - 0.12615662f * b[6] - 0.21850968f * b[8];
2576 out[1] = ta * b[1] + tb * a[1];
2577 t = a[1] * b[1];
2578 out[0] += 0.28209479f * t;
2579 out[6] = -0.12615662f * t;
2580 out[8] = -0.21850968f * t;
2582 ta = 0.21850968f * a[5];
2583 tb = 0.21850968f * b[5];
2584 out[1] += ta * b[2] + tb * a[2];
2585 out[2] = ta * b[1] + tb * a[1];
2586 t = a[1] * b[2] +a[2] * b[1];
2587 out[5] = 0.21850968f * t;
2589 ta = 0.21850968f * a[4];
2590 tb = 0.21850968f * b[4];
2591 out[1] += ta * b[3] + tb * a[3];
2592 out[3] = ta * b[1] + tb * a[1];
2593 t = a[1] * b[3] + a[3] * b[1];
2594 out[4] = 0.21850968f * t;
2596 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2597 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2598 out[2] += ta * b[2] + tb * a[2];
2599 t = a[2] * b[2];
2600 out[0] += 0.28209480f * t;
2601 out[6] += 0.25231326f * t;
2603 ta = 0.21850969f * a[7];
2604 tb = 0.21850969f * b[7];
2605 out[2] += ta * b[3] + tb * a[3];
2606 out[3] += ta * b[2] + tb * a[2];
2607 t = a[2] * b[3] + a[3] * b[2];
2608 out[7] = 0.21850969f * t;
2610 ta = 0.28209479f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2611 tb = 0.28209479f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2612 out[3] += ta * b[3] + tb * a[3];
2613 t = a[3] * b[3];
2614 out[0] += 0.28209479f * t;
2615 out[6] -= 0.12615663f * t;
2616 out[8] += 0.21850969f * t;
2618 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2619 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2620 out[4] += ta * b[4] + tb * a[4];
2621 t = a[4] * b[4];
2622 out[0] += 0.28209479f * t;
2623 out[6] -= 0.18022375f * t;
2625 ta = 0.15607835f * a[7];
2626 tb = 0.15607835f * b[7];
2627 out[4] += ta * b[5] + tb * a[5];
2628 out[5] += ta * b[4] + tb * a[4];
2629 t = a[4] * b[5] + a[5] * b[4];
2630 out[7] += 0.15607834f * t;
2632 ta = 0.28209479f * a[0] + 0.09011186f * a[6] - 0.15607835f * a[8];
2633 tb = 0.28209479f * b[0] + 0.09011186f * b[6] - 0.15607835f * b[8];
2634 out[5] += ta * b[5] + tb * a[5];
2635 t = a[5] * b[5];
2636 out[0] += 0.28209479f * t;
2637 out[6] += 0.09011186f * t;
2638 out[8] -= 0.15607835f * t;
2640 ta = 0.28209480f * a[0];
2641 tb = 0.28209480f * b[0];
2642 out[6] += ta * b[6] + tb * a[6];
2643 t = a[6] * b[6];
2644 out[0] += 0.28209480f * t;
2645 out[6] += 0.18022376f * t;
2647 ta = 0.28209479f * a[0] + 0.09011186f * a[6] + 0.15607835f * a[8];
2648 tb = 0.28209479f * b[0] + 0.09011186f * b[6] + 0.15607835f * b[8];
2649 out[7] += ta * b[7] + tb * a[7];
2650 t = a[7] * b[7];
2651 out[0] += 0.28209479f * t;
2652 out[6] += 0.09011186f * t;
2653 out[8] += 0.15607835f * t;
2655 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2656 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2657 out[8] += ta * b[8] + tb * a[8];
2658 t = a[8] * b[8];
2659 out[0] += 0.28209479f * t;
2660 out[6] -= 0.18022375f * t;
2662 return out;
2665 FLOAT * WINAPI D3DXSHMultiply4(FLOAT *out, const FLOAT *a, const FLOAT *b)
2667 FLOAT ta, tb, t;
2669 TRACE("out %p, a %p, b %p\n", out, a, b);
2671 out[0] = 0.28209479f * a[0] * b[0];
2673 ta = 0.28209479f * a[0] - 0.12615663f * a[6] - 0.21850969f * a[8];
2674 tb = 0.28209479f * b[0] - 0.12615663f * b[6] - 0.21850969f * b[8];
2675 out[1] = ta * b[1] + tb * a[1];
2676 t = a[1] * b[1];
2677 out[0] += 0.28209479f * t;
2678 out[6] = -0.12615663f * t;
2679 out[8] = -0.21850969f * t;
2681 ta = 0.21850969f * a[3] - 0.05839917f * a[13] - 0.22617901f * a[15];
2682 tb = 0.21850969f * b[3] - 0.05839917f * b[13] - 0.22617901f * b[15];
2683 out[1] += ta * b[4] + tb * a[4];
2684 out[4] = ta * b[1] + tb * a[1];
2685 t = a[1] * b[4] + a[4] * b[1];
2686 out[3] = 0.21850969f * t;
2687 out[13] = -0.05839917f * t;
2688 out[15] = -0.22617901f * t;
2690 ta = 0.21850969f * a[2] - 0.14304817f * a[12] - 0.18467439f * a[14];
2691 tb = 0.21850969f * b[2] - 0.14304817f * b[12] - 0.18467439f * b[14];
2692 out[1] += ta * b[5] + tb * a[5];
2693 out[5] = ta * b[1] + tb * a[1];
2694 t = a[1] * b[5] + a[5] * b[1];
2695 out[2] = 0.21850969f * t;
2696 out[12] = -0.14304817f * t;
2697 out[14] = -0.18467439f * t;
2699 ta = 0.20230066f * a[11];
2700 tb = 0.20230066f * b[11];
2701 out[1] += ta * b[6] + tb * a[6];
2702 out[6] += ta * b[1] + tb * a[1];
2703 t = a[1] * b[6] + a[6] * b[1];
2704 out[11] = 0.20230066f * t;
2706 ta = 0.22617901f * a[9] + 0.05839917f * a[11];
2707 tb = 0.22617901f * b[9] + 0.05839917f * b[11];
2708 out[1] += ta * b[8] + tb * a[8];
2709 out[8] += ta * b[1] + tb * a[1];
2710 t = a[1] * b[8] + a[8] * b[1];
2711 out[9] = 0.22617901f * t;
2712 out[11] += 0.05839917f * t;
2714 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2715 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2716 out[2] += ta * b[2] + tb * a[2];
2717 t = a[2] * b[2];
2718 out[0] += 0.28209480f * t;
2719 out[6] += 0.25231326f * t;
2721 ta = 0.24776671f * a[12];
2722 tb = 0.24776671f * b[12];
2723 out[2] += ta * b[6] + tb * a[6];
2724 out[6] += ta * b[2] + tb * a[2];
2725 t = a[2] * b[6] + a[6] * b[2];
2726 out[12] += 0.24776671f * t;
2728 ta = 0.28209480f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2729 tb = 0.28209480f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2730 out[3] += ta * b[3] + tb * a[3];
2731 t = a[3] * b[3];
2732 out[0] += 0.28209480f * t;
2733 out[6] -= 0.12615663f * t;
2734 out[8] += 0.21850969f * t;
2736 ta = 0.20230066f * a[13];
2737 tb = 0.20230066f * b[13];
2738 out[3] += ta * b[6] + tb * a[6];
2739 out[6] += ta * b[3] + tb * a[3];
2740 t = a[3] * b[6] + a[6] * b[3];
2741 out[13] += 0.20230066f * t;
2743 ta = 0.21850969f * a[2] - 0.14304817f * a[12] + 0.18467439f * a[14];
2744 tb = 0.21850969f * b[2] - 0.14304817f * b[12] + 0.18467439f * b[14];
2745 out[3] += ta * b[7] + tb * a[7];
2746 out[7] = ta * b[3] + tb * a[3];
2747 t = a[3] * b[7] + a[7] * b[3];
2748 out[2] += 0.21850969f * t;
2749 out[12] -= 0.14304817f * t;
2750 out[14] += 0.18467439f * t;
2752 ta = -0.05839917f * a[13] + 0.22617901f * a[15];
2753 tb = -0.05839917f * b[13] + 0.22617901f * b[15];
2754 out[3] += ta * b[8] + tb * a[8];
2755 out[8] += ta * b[3] + tb * a[3];
2756 t = a[3] * b[8] + a[8] * b[3];
2757 out[13] -= 0.05839917f * t;
2758 out[15] += 0.22617901f * t;
2760 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2761 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2762 out[4] += ta * b[4] + tb * a[4];
2763 t = a[4] * b[4];
2764 out[0] += 0.28209479f * t;
2765 out[6] -= 0.18022375f * t;
2767 ta = 0.15607835f * a[7];
2768 tb = 0.15607835f * b[7];
2769 out[4] += ta * b[5] + tb * a[5];
2770 out[5] += ta * b[4] + tb * a[4];
2771 t = a[4] * b[5] + a[5] * b[4];
2772 out[7] += 0.15607835f * t;
2774 ta = 0.22617901f * a[3] - 0.09403160f * a[13];
2775 tb = 0.22617901f * b[3] - 0.09403160f * b[13];
2776 out[4] += ta * b[9] + tb * a[9];
2777 out[9] += ta * b[4] + tb * a[4];
2778 t = a[4] * b[9] + a[9] * b[4];
2779 out[3] += 0.22617901f * t;
2780 out[13] -= 0.09403160f * t;
2782 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2783 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2784 out[4] += ta * b[10] + tb * a [10];
2785 out[10] = ta * b[4] + tb * a[4];
2786 t = a[4] * b[10] + a[10] * b[4];
2787 out[2] += 0.18467439f * t;
2788 out[12] -= 0.18806319f * t;
2790 ta = -0.05839917f * a[3] + 0.14567312f * a[13] + 0.09403160f * a[15];
2791 tb = -0.05839917f * b[3] + 0.14567312f * b[13] + 0.09403160f * b[15];
2792 out[4] += ta * b[11] + tb * a[11];
2793 out[11] += ta * b[4] + tb * a[4];
2794 t = a[4] * b[11] + a[11] * b[4];
2795 out[3] -= 0.05839917f * t;
2796 out[13] += 0.14567312f * t;
2797 out[15] += 0.09403160f * t;
2799 ta = 0.28209479f * a[0] + 0.09011186f * a[6] - 0.15607835f * a[8];
2800 tb = 0.28209479f * b[0] + 0.09011186f * b[6] - 0.15607835f * b[8];
2801 out[5] += ta * b[5] + tb * a[5];
2802 t = a[5] * b[5];
2803 out[0] += 0.28209479f * t;
2804 out[6] += 0.09011186f * t;
2805 out[8] -= 0.15607835f * t;
2807 ta = 0.14867701f * a[14];
2808 tb = 0.14867701f * b[14];
2809 out[5] += ta * b[9] + tb * a[9];
2810 out[9] += ta * b[5] + tb * a[5];
2811 t = a[5] * b[9] + a[9] * b[5];
2812 out[14] += 0.14867701f * t;
2814 ta = 0.18467439f * a[3] + 0.11516472f * a[13] - 0.14867701f * a[15];
2815 tb = 0.18467439f * b[3] + 0.11516472f * b[13] - 0.14867701f * b[15];
2816 out[5] += ta * b[10] + tb * a[10];
2817 out[10] += ta * b[5] + tb * a[5];
2818 t = a[5] * b[10] + a[10] * b[5];
2819 out[3] += 0.18467439f * t;
2820 out[13] += 0.11516472f * t;
2821 out[15] -= 0.14867701f * t;
2823 ta = 0.23359668f * a[2] + 0.05947080f * a[12] - 0.11516472f * a[14];
2824 tb = 0.23359668f * b[2] + 0.05947080f * b[12] - 0.11516472f * b[14];
2825 out[5] += ta * b[11] + tb * a[11];
2826 out[11] += ta * b[5] + tb * a[5];
2827 t = a[5] * b[11] + a[11] * b[5];
2828 out[2] += 0.23359668f * t;
2829 out[12] += 0.05947080f * t;
2830 out[14] -= 0.11516472f * t;
2832 ta = 0.28209479f * a[0];
2833 tb = 0.28209479f * b[0];
2834 out[6] += ta * b[6] + tb * a[6];
2835 t = a[6] * b[6];
2836 out[0] += 0.28209479f * t;
2837 out[6] += 0.18022376f * t;
2839 ta = 0.09011186f * a[6] + 0.28209479f * a[0] + 0.15607835f * a[8];
2840 tb = 0.09011186f * b[6] + 0.28209479f * b[0] + 0.15607835f * b[8];
2841 out[7] += ta * b[7] + tb * a[7];
2842 t = a[7] * b[7];
2843 out[6] += 0.09011186f * t;
2844 out[0] += 0.28209479f * t;
2845 out[8] += 0.15607835f * t;
2847 ta = 0.14867701f * a[9] + 0.18467439f * a[1] + 0.11516472f * a[11];
2848 tb = 0.14867701f * b[9] + 0.18467439f * b[1] + 0.11516472f * b[11];
2849 out[7] += ta * b[10] + tb * a[10];
2850 out[10] += ta * b[7] + tb * a[7];
2851 t = a[7] * b[10] + a[10] * b[7];
2852 out[9] += 0.14867701f * t;
2853 out[1] += 0.18467439f * t;
2854 out[11] += 0.11516472f * t;
2856 ta = 0.05947080f * a[12] + 0.23359668f * a[2] + 0.11516472f * a[14];
2857 tb = 0.05947080f * b[12] + 0.23359668f * b[2] + 0.11516472f * b[14];
2858 out[7] += ta * b[13] + tb * a[13];
2859 out[13] += ta * b[7]+ tb * a[7];
2860 t = a[7] * b[13] + a[13] * b[7];
2861 out[12] += 0.05947080f * t;
2862 out[2] += 0.23359668f * t;
2863 out[14] += 0.11516472f * t;
2865 ta = 0.14867701f * a[15];
2866 tb = 0.14867701f * b[15];
2867 out[7] += ta * b[14] + tb * a[14];
2868 out[14] += ta * b[7] + tb * a[7];
2869 t = a[7] * b[14] + a[14] * b[7];
2870 out[15] += 0.14867701f * t;
2872 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2873 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2874 out[8] += ta * b[8] + tb * a[8];
2875 t = a[8] * b[8];
2876 out[0] += 0.28209479f * t;
2877 out[6] -= 0.18022375f * t;
2879 ta = -0.09403160f * a[11];
2880 tb = -0.09403160f * b[11];
2881 out[8] += ta * b[9] + tb * a[9];
2882 out[9] += ta * b[8] + tb * a[8];
2883 t = a[8] * b[9] + a[9] * b[8];
2884 out[11] -= 0.09403160f * t;
2886 ta = -0.09403160f * a[15];
2887 tb = -0.09403160f * b[15];
2888 out[8] += ta * b[13] + tb * a[13];
2889 out[13] += ta * b[8] + tb * a[8];
2890 t = a[8] * b[13] + a[13] * b[8];
2891 out[15] -= 0.09403160f * t;
2893 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2894 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2895 out[8] += ta * b[14] + tb * a[14];
2896 out[14] += ta * b[8] + tb * a[8];
2897 t = a[8] * b[14] + a[14] * b[8];
2898 out[2] += 0.18467439f * t;
2899 out[12] -= 0.18806319f * t;
2901 ta = -0.21026104f * a[6] + 0.28209479f * a[0];
2902 tb = -0.21026104f * b[6] + 0.28209479f * b[0];
2903 out[9] += ta * b[9] + tb * a[9];
2904 t = a[9] * b[9];
2905 out[6] -= 0.21026104f * t;
2906 out[0] += 0.28209479f * t;
2908 ta = 0.28209479f * a[0];
2909 tb = 0.28209479f * b[0];
2910 out[10] += ta * b[10] + tb * a[10];
2911 t = a[10] * b[10];
2912 out[0] += 0.28209479f * t;
2914 ta = 0.28209479f * a[0] + 0.12615663f * a[6] - 0.14567312f * a[8];
2915 tb = 0.28209479f * b[0] + 0.12615663f * b[6] - 0.14567312f * b[8];
2916 out[11] += ta * b[11] + tb * a[11];
2917 t = a[11] * b[11];
2918 out[0] += 0.28209479f * t;
2919 out[6] += 0.12615663f * t;
2920 out[8] -= 0.14567312f * t;
2922 ta = 0.28209479f * a[0] + 0.16820885f * a[6];
2923 tb = 0.28209479f * b[0] + 0.16820885f * b[6];
2924 out[12] += ta * b[12] + tb * a[12];
2925 t = a[12] * b[12];
2926 out[0] += 0.28209479f * t;
2927 out[6] += 0.16820885f * t;
2929 ta =0.28209479f * a[0] + 0.14567312f * a[8] + 0.12615663f * a[6];
2930 tb =0.28209479f * b[0] + 0.14567312f * b[8] + 0.12615663f * b[6];
2931 out[13] += ta * b[13] + tb * a[13];
2932 t = a[13] * b[13];
2933 out[0] += 0.28209479f * t;
2934 out[8] += 0.14567312f * t;
2935 out[6] += 0.12615663f * t;
2937 ta = 0.28209479f * a[0];
2938 tb = 0.28209479f * b[0];
2939 out[14] += ta * b[14] + tb * a[14];
2940 t = a[14] * b[14];
2941 out[0] += 0.28209479f * t;
2943 ta = 0.28209479f * a[0] - 0.21026104f * a[6];
2944 tb = 0.28209479f * b[0] - 0.21026104f * b[6];
2945 out[15] += ta * b[15] + tb * a[15];
2946 t = a[15] * b[15];
2947 out[0] += 0.28209479f * t;
2948 out[6] -= 0.21026104f * t;
2950 return out;
2953 static void rotate_X(FLOAT *out, UINT order, FLOAT a, FLOAT *in)
2955 out[0] = in[0];
2957 out[1] = a * in[2];
2958 out[2] = -a * in[1];
2959 out[3] = in[3];
2961 out[4] = a * in[7];
2962 out[5] = -in[5];
2963 out[6] = -0.5f * in[6] - 0.8660253882f * in[8];
2964 out[7] = -a * in[4];
2965 out[8] = -0.8660253882f * in[6] + 0.5f * in[8];
2966 out[9] = -a * 0.7905694842f * in[12] + a * 0.6123724580f * in[14];
2968 out[10] = -in[10];
2969 out[11] = -a * 0.6123724580f * in[12] - a * 0.7905694842f * in[14];
2970 out[12] = a * 0.7905694842f * in[9] + a * 0.6123724580f * in[11];
2971 out[13] = -0.25f * in[13] - 0.9682458639f * in[15];
2972 out[14] = -a * 0.6123724580f * in[9] + a * 0.7905694842f * in[11];
2973 out[15] = -0.9682458639f * in[13] + 0.25f * in[15];
2974 if (order == 4)
2975 return;
2977 out[16] = -a * 0.9354143739f * in[21] + a * 0.3535533845f * in[23];
2978 out[17] = -0.75f * in[17] + 0.6614378095f * in[19];
2979 out[18] = -a * 0.3535533845f * in[21] - a * 0.9354143739f * in[23];
2980 out[19] = 0.6614378095f * in[17] + 0.75f * in[19];
2981 out[20] = 0.375f * in[20] + 0.5590170026f * in[22] + 0.7395099998f * in[24];
2982 out[21] = a * 0.9354143739f * in[16] + a * 0.3535533845f * in[18];
2983 out[22] = 0.5590170026f * in[20] + 0.5f * in[22] - 0.6614378691f * in[24];
2984 out[23] = -a * 0.3535533845f * in[16] + a * 0.9354143739f * in[18];
2985 out[24] = 0.7395099998f * in[20] - 0.6614378691f * in[22] + 0.125f * in[24];
2986 if (order == 5)
2987 return;
2989 out[25] = a * 0.7015607357f * in[30] - a * 0.6846531630f * in[32] + a * 0.1976423711f * in[34];
2990 out[26] = -0.5f * in[26] + 0.8660253882f * in[28];
2991 out[27] = a * 0.5229125023f * in[30] + a * 0.3061861992f * in[32] - a * 0.7954951525f * in[34];
2992 out[28] = 0.8660253882f * in[26] + 0.5f * in[28];
2993 out[29] = a * 0.4841229022f * in[30] + a * 0.6614378691f * in[32] + a * 0.5728219748f * in[34];
2994 out[30] = -a * 0.7015607357f * in[25] - a * 0.5229125023f * in[27] - a * 0.4841229022f * in[29];
2995 out[31] = 0.125f * in[31] + 0.4050463140f * in[33] + 0.9057110548f * in[35];
2996 out[32] = a * 0.6846531630f * in[25] - a * 0.3061861992f * in[27] - a * 0.6614378691f * in[29];
2997 out[33] = 0.4050463140f * in[31] + 0.8125f * in[33] - 0.4192627370f * in[35];
2998 out[34] = -a * 0.1976423711f * in[25] + a * 0.7954951525f * in[27] - a * 0.5728219748f * in[29];
2999 out[35] = 0.9057110548f * in[31] - 0.4192627370f * in[33] + 0.0624999329f * in[35];
3002 FLOAT* WINAPI D3DXSHRotate(FLOAT *out, UINT order, const D3DXMATRIX *matrix, const FLOAT *in)
3004 FLOAT alpha, beta, gamma, sinb, temp[36], temp1[36];
3006 TRACE("out %p, order %u, matrix %p, in %p\n", out, order, matrix, in);
3008 out[0] = in[0];
3010 if ((order > D3DXSH_MAXORDER) || (order < D3DXSH_MINORDER))
3011 return out;
3013 if (order <= 3)
3015 out[1] = matrix->u.m[1][1] * in[1] - matrix->u.m[2][1] * in[2] + matrix->u.m[0][1] * in[3];
3016 out[2] = -matrix->u.m[1][2] * in[1] + matrix->u.m[2][2] * in[2] - matrix->u.m[0][2] * in[3];
3017 out[3] = matrix->u.m[1][0] * in[1] - matrix->u.m[2][0] * in[2] + matrix->u.m[0][0] * in[3];
3019 if (order == 3)
3021 FLOAT coeff[]={
3022 matrix->u.m[1][0] * matrix->u.m[0][0], matrix->u.m[1][1] * matrix->u.m[0][1],
3023 matrix->u.m[1][1] * matrix->u.m[2][1], matrix->u.m[1][0] * matrix->u.m[2][0],
3024 matrix->u.m[2][0] * matrix->u.m[2][0], matrix->u.m[2][1] * matrix->u.m[2][1],
3025 matrix->u.m[0][0] * matrix->u.m[2][0], matrix->u.m[0][1] * matrix->u.m[2][1],
3026 matrix->u.m[0][1] * matrix->u.m[0][1], matrix->u.m[1][0] * matrix->u.m[1][0],
3027 matrix->u.m[1][1] * matrix->u.m[1][1], matrix->u.m[0][0] * matrix->u.m[0][0], };
3029 out[4] = (matrix->u.m[1][1] * matrix->u.m[0][0] + matrix->u.m[0][1] * matrix->u.m[1][0]) * in[4];
3030 out[4] -= (matrix->u.m[1][0] * matrix->u.m[2][1] + matrix->u.m[1][1] * matrix->u.m[2][0]) * in[5];
3031 out[4] += 1.7320508076f * matrix->u.m[2][0] * matrix->u.m[2][1] * in[6];
3032 out[4] -= (matrix->u.m[0][1] * matrix->u.m[2][0] + matrix->u.m[0][0] * matrix->u.m[2][1]) * in[7];
3033 out[4] += (matrix->u.m[0][0] * matrix->u.m[0][1] - matrix->u.m[1][0] * matrix->u.m[1][1]) * in[8];
3035 out[5] = (matrix->u.m[1][1] * matrix->u.m[2][2] + matrix->u.m[1][2] * matrix->u.m[2][1]) * in[5];
3036 out[5] -= (matrix->u.m[1][1] * matrix->u.m[0][2] + matrix->u.m[1][2] * matrix->u.m[0][1]) * in[4];
3037 out[5] -= 1.7320508076f * matrix->u.m[2][2] * matrix->u.m[2][1] * in[6];
3038 out[5] += (matrix->u.m[0][2] * matrix->u.m[2][1] + matrix->u.m[0][1] * matrix->u.m[2][2]) * in[7];
3039 out[5] -= (matrix->u.m[0][1] * matrix->u.m[0][2] - matrix->u.m[1][1] * matrix->u.m[1][2]) * in[8];
3041 out[6] = (matrix->u.m[2][2] * matrix->u.m[2][2] - 0.5f * (coeff[4] + coeff[5])) * in[6];
3042 out[6] -= (0.5773502692f * (coeff[0] + coeff[1]) - 1.1547005384f * matrix->u.m[1][2] * matrix->u.m[0][2]) * in[4];
3043 out[6] += (0.5773502692f * (coeff[2] + coeff[3]) - 1.1547005384f * matrix->u.m[1][2] * matrix->u.m[2][2]) * in[5];
3044 out[6] += (0.5773502692f * (coeff[6] + coeff[7]) - 1.1547005384f * matrix->u.m[0][2] * matrix->u.m[2][2]) * in[7];
3045 out[6] += (0.2886751347f * (coeff[9] - coeff[8] + coeff[10] - coeff[11]) - 0.5773502692f *
3046 (matrix->u.m[1][2] * matrix->u.m[1][2] - matrix->u.m[0][2] * matrix->u.m[0][2])) * in[8];
3048 out[7] = (matrix->u.m[0][0] * matrix->u.m[2][2] + matrix->u.m[0][2] * matrix->u.m[2][0]) * in[7];
3049 out[7] -= (matrix->u.m[1][0] * matrix->u.m[0][2] + matrix->u.m[1][2] * matrix->u.m[0][0]) * in[4];
3050 out[7] += (matrix->u.m[1][0] * matrix->u.m[2][2] + matrix->u.m[1][2] * matrix->u.m[2][0]) * in[5];
3051 out[7] -= 1.7320508076f * matrix->u.m[2][2] * matrix->u.m[2][0] * in[6];
3052 out[7] -= (matrix->u.m[0][0] * matrix->u.m[0][2] - matrix->u.m[1][0] * matrix->u.m[1][2]) * in[8];
3054 out[8] = 0.5f * (coeff[11] - coeff[8] - coeff[9] + coeff[10]) * in[8];
3055 out[8] += (coeff[0] - coeff[1]) * in[4];
3056 out[8] += (coeff[2] - coeff[3]) * in[5];
3057 out[8] += 0.86602540f * (coeff[4] - coeff[5]) * in[6];
3058 out[8] += (coeff[7] - coeff[6]) * in[7];
3061 return out;
3064 if (fabsf(matrix->u.m[2][2]) != 1.0f)
3066 sinb = sqrtf(1.0f - matrix->u.m[2][2] * matrix->u.m[2][2]);
3067 alpha = atan2f(matrix->u.m[2][1] / sinb, matrix->u.m[2][0] / sinb);
3068 beta = atan2f(sinb, matrix->u.m[2][2]);
3069 gamma = atan2f(matrix->u.m[1][2] / sinb, -matrix->u.m[0][2] / sinb);
3071 else
3073 alpha = atan2f(matrix->u.m[0][1], matrix->u.m[0][0]);
3074 beta = 0.0f;
3075 gamma = 0.0f;
3078 D3DXSHRotateZ(temp, order, gamma, in);
3079 rotate_X(temp1, order, 1.0f, temp);
3080 D3DXSHRotateZ(temp, order, beta, temp1);
3081 rotate_X(temp1, order, -1.0f, temp);
3082 D3DXSHRotateZ(out, order, alpha, temp1);
3084 return out;
3087 FLOAT * WINAPI D3DXSHRotateZ(FLOAT *out, UINT order, FLOAT angle, const FLOAT *in)
3089 UINT i, sum = 0;
3090 FLOAT c[5], s[5];
3092 TRACE("out %p, order %u, angle %f, in %p\n", out, order, angle, in);
3094 order = min(max(order, D3DXSH_MINORDER), D3DXSH_MAXORDER);
3096 out[0] = in[0];
3098 for (i = 1; i < order; i++)
3100 UINT j;
3102 c[i - 1] = cosf(i * angle);
3103 s[i - 1] = sinf(i * angle);
3104 sum += i * 2;
3106 out[sum - i] = c[i - 1] * in[sum - i];
3107 out[sum - i] += s[i - 1] * in[sum + i];
3108 for (j = i - 1; j > 0; j--)
3110 out[sum - j] = 0.0f;
3111 out[sum - j] = c[j - 1] * in[sum - j];
3112 out[sum - j] += s[j - 1] * in[sum + j];
3115 if (in == out)
3116 out[sum] = 0.0f;
3117 else
3118 out[sum] = in[sum];
3120 for (j = 1; j < i; j++)
3122 out[sum + j] = 0.0f;
3123 out[sum + j] = -s[j - 1] * in[sum - j];
3124 out[sum + j] += c[j - 1] * in[sum + j];
3126 out[sum + i] = -s[i - 1] * in[sum - i];
3127 out[sum + i] += c[i - 1] * in[sum + i];
3130 return out;
3133 FLOAT* WINAPI D3DXSHScale(FLOAT *out, UINT order, const FLOAT *a, const FLOAT scale)
3135 UINT i;
3137 TRACE("out %p, order %u, a %p, scale %f\n", out, order, a, scale);
3139 for (i = 0; i < order * order; i++)
3140 out[i] = a[i] * scale;
3142 return out;