d3dx9: D3DXVec3Transform should support input and output parameter overlap.
[wine.git] / dlls / d3dx9_36 / math.c
blob394ff059f31d830507c328602bf32dfe1a737ad6
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 #include "config.h"
26 #include "wine/port.h"
28 #include "d3dx9_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
32 struct ID3DXMatrixStackImpl
34 ID3DXMatrixStack ID3DXMatrixStack_iface;
35 LONG ref;
37 unsigned int current;
38 unsigned int stack_size;
39 D3DXMATRIX *stack;
42 static const unsigned int INITIAL_STACK_SIZE = 32;
44 /*_________________D3DXColor____________________*/
46 D3DXCOLOR* WINAPI D3DXColorAdjustContrast(D3DXCOLOR *pout, const D3DXCOLOR *pc, FLOAT s)
48 TRACE("pout %p, pc %p, s %f\n", pout, pc, s);
50 pout->r = 0.5f + s * (pc->r - 0.5f);
51 pout->g = 0.5f + s * (pc->g - 0.5f);
52 pout->b = 0.5f + s * (pc->b - 0.5f);
53 pout->a = pc->a;
54 return pout;
57 D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, const D3DXCOLOR *pc, FLOAT s)
59 FLOAT grey;
61 TRACE("pout %p, pc %p, s %f\n", pout, pc, s);
63 grey = pc->r * 0.2125f + pc->g * 0.7154f + pc->b * 0.0721f;
64 pout->r = grey + s * (pc->r - grey);
65 pout->g = grey + s * (pc->g - grey);
66 pout->b = grey + s * (pc->b - grey);
67 pout->a = pc->a;
68 return pout;
71 /*_________________Misc__________________________*/
73 FLOAT WINAPI D3DXFresnelTerm(FLOAT costheta, FLOAT refractionindex)
75 FLOAT a, d, g, result;
77 TRACE("costheta %f, refractionindex %f\n", costheta, refractionindex);
79 g = sqrtf(refractionindex * refractionindex + costheta * costheta - 1.0f);
80 a = g + costheta;
81 d = g - costheta;
82 result = (costheta * a - 1.0f) * (costheta * a - 1.0f) / ((costheta * d + 1.0f) * (costheta * d + 1.0f)) + 1.0f;
83 result *= 0.5f * d * d / (a * a);
85 return result;
88 /*_________________D3DXMatrix____________________*/
90 D3DXMATRIX * WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *out, FLOAT scaling, const D3DXVECTOR3 *rotationcenter,
91 const D3DXQUATERNION *rotation, const D3DXVECTOR3 *translation)
93 TRACE("out %p, scaling %f, rotationcenter %p, rotation %p, translation %p\n",
94 out, scaling, rotationcenter, rotation, translation);
96 D3DXMatrixIdentity(out);
98 if (rotation)
100 FLOAT temp00, temp01, temp02, temp10, temp11, temp12, temp20, temp21, temp22;
102 temp00 = 1.0f - 2.0f * (rotation->y * rotation->y + rotation->z * rotation->z);
103 temp01 = 2.0f * (rotation->x * rotation->y + rotation->z * rotation->w);
104 temp02 = 2.0f * (rotation->x * rotation->z - rotation->y * rotation->w);
105 temp10 = 2.0f * (rotation->x * rotation->y - rotation->z * rotation->w);
106 temp11 = 1.0f - 2.0f * (rotation->x * rotation->x + rotation->z * rotation->z);
107 temp12 = 2.0f * (rotation->y * rotation->z + rotation->x * rotation->w);
108 temp20 = 2.0f * (rotation->x * rotation->z + rotation->y * rotation->w);
109 temp21 = 2.0f * (rotation->y * rotation->z - rotation->x * rotation->w);
110 temp22 = 1.0f - 2.0f * (rotation->x * rotation->x + rotation->y * rotation->y);
112 out->u.m[0][0] = scaling * temp00;
113 out->u.m[0][1] = scaling * temp01;
114 out->u.m[0][2] = scaling * temp02;
115 out->u.m[1][0] = scaling * temp10;
116 out->u.m[1][1] = scaling * temp11;
117 out->u.m[1][2] = scaling * temp12;
118 out->u.m[2][0] = scaling * temp20;
119 out->u.m[2][1] = scaling * temp21;
120 out->u.m[2][2] = scaling * temp22;
122 if (rotationcenter)
124 out->u.m[3][0] = rotationcenter->x * (1.0f - temp00) - rotationcenter->y * temp10
125 - rotationcenter->z * temp20;
126 out->u.m[3][1] = rotationcenter->y * (1.0f - temp11) - rotationcenter->x * temp01
127 - rotationcenter->z * temp21;
128 out->u.m[3][2] = rotationcenter->z * (1.0f - temp22) - rotationcenter->x * temp02
129 - rotationcenter->y * temp12;
132 else
134 out->u.m[0][0] = scaling;
135 out->u.m[1][1] = scaling;
136 out->u.m[2][2] = scaling;
139 if (translation)
141 out->u.m[3][0] += translation->x;
142 out->u.m[3][1] += translation->y;
143 out->u.m[3][2] += translation->z;
146 return out;
149 D3DXMATRIX * WINAPI D3DXMatrixAffineTransformation2D(D3DXMATRIX *out, FLOAT scaling,
150 const D3DXVECTOR2 *rotationcenter, FLOAT rotation, const D3DXVECTOR2 *translation)
152 FLOAT tmp1, tmp2, s;
154 TRACE("out %p, scaling %f, rotationcenter %p, rotation %f, translation %p\n",
155 out, scaling, rotationcenter, rotation, translation);
157 s = sinf(rotation / 2.0f);
158 tmp1 = 1.0f - 2.0f * s * s;
159 tmp2 = 2.0f * s * cosf(rotation / 2.0f);
161 D3DXMatrixIdentity(out);
162 out->u.m[0][0] = scaling * tmp1;
163 out->u.m[0][1] = scaling * tmp2;
164 out->u.m[1][0] = -scaling * tmp2;
165 out->u.m[1][1] = scaling * tmp1;
167 if (rotationcenter)
169 FLOAT x, y;
171 x = rotationcenter->x;
172 y = rotationcenter->y;
174 out->u.m[3][0] = y * tmp2 - x * tmp1 + x;
175 out->u.m[3][1] = -x * tmp2 - y * tmp1 + y;
178 if (translation)
180 out->u.m[3][0] += translation->x;
181 out->u.m[3][1] += translation->y;
184 return out;
187 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, const D3DXMATRIX *pm)
189 D3DXMATRIX normalized;
190 D3DXVECTOR3 vec;
192 TRACE("poutscale %p, poutrotation %p, pouttranslation %p, pm %p\n", poutscale, poutrotation, pouttranslation, pm);
194 /*Compute the scaling part.*/
195 vec.x=pm->u.m[0][0];
196 vec.y=pm->u.m[0][1];
197 vec.z=pm->u.m[0][2];
198 poutscale->x=D3DXVec3Length(&vec);
200 vec.x=pm->u.m[1][0];
201 vec.y=pm->u.m[1][1];
202 vec.z=pm->u.m[1][2];
203 poutscale->y=D3DXVec3Length(&vec);
205 vec.x=pm->u.m[2][0];
206 vec.y=pm->u.m[2][1];
207 vec.z=pm->u.m[2][2];
208 poutscale->z=D3DXVec3Length(&vec);
210 /*Compute the translation part.*/
211 pouttranslation->x=pm->u.m[3][0];
212 pouttranslation->y=pm->u.m[3][1];
213 pouttranslation->z=pm->u.m[3][2];
215 /*Let's calculate the rotation now*/
216 if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) ) return D3DERR_INVALIDCALL;
218 normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
219 normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
220 normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
221 normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
222 normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
223 normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
224 normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
225 normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
226 normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
228 D3DXQuaternionRotationMatrix(poutrotation,&normalized);
229 return S_OK;
232 FLOAT WINAPI D3DXMatrixDeterminant(const D3DXMATRIX *pm)
234 FLOAT t[3], v[4];
236 TRACE("pm %p\n", pm);
238 t[0] = pm->u.m[2][2] * pm->u.m[3][3] - pm->u.m[2][3] * pm->u.m[3][2];
239 t[1] = pm->u.m[1][2] * pm->u.m[3][3] - pm->u.m[1][3] * pm->u.m[3][2];
240 t[2] = pm->u.m[1][2] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][2];
241 v[0] = pm->u.m[1][1] * t[0] - pm->u.m[2][1] * t[1] + pm->u.m[3][1] * t[2];
242 v[1] = -pm->u.m[1][0] * t[0] + pm->u.m[2][0] * t[1] - pm->u.m[3][0] * t[2];
244 t[0] = pm->u.m[1][0] * pm->u.m[2][1] - pm->u.m[2][0] * pm->u.m[1][1];
245 t[1] = pm->u.m[1][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[1][1];
246 t[2] = pm->u.m[2][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[2][1];
247 v[2] = pm->u.m[3][3] * t[0] - pm->u.m[2][3] * t[1] + pm->u.m[1][3] * t[2];
248 v[3] = -pm->u.m[3][2] * t[0] + pm->u.m[2][2] * t[1] - pm->u.m[1][2] * t[2];
250 return pm->u.m[0][0] * v[0] + pm->u.m[0][1] * v[1] +
251 pm->u.m[0][2] * v[2] + pm->u.m[0][3] * v[3];
254 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, const D3DXMATRIX *pm)
256 FLOAT det, t[3], v[16];
257 UINT i, j;
259 TRACE("pout %p, pdeterminant %p, pm %p\n", pout, pdeterminant, pm);
261 t[0] = pm->u.m[2][2] * pm->u.m[3][3] - pm->u.m[2][3] * pm->u.m[3][2];
262 t[1] = pm->u.m[1][2] * pm->u.m[3][3] - pm->u.m[1][3] * pm->u.m[3][2];
263 t[2] = pm->u.m[1][2] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][2];
264 v[0] = pm->u.m[1][1] * t[0] - pm->u.m[2][1] * t[1] + pm->u.m[3][1] * t[2];
265 v[4] = -pm->u.m[1][0] * t[0] + pm->u.m[2][0] * t[1] - pm->u.m[3][0] * t[2];
267 t[0] = pm->u.m[1][0] * pm->u.m[2][1] - pm->u.m[2][0] * pm->u.m[1][1];
268 t[1] = pm->u.m[1][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[1][1];
269 t[2] = pm->u.m[2][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[2][1];
270 v[8] = pm->u.m[3][3] * t[0] - pm->u.m[2][3] * t[1] + pm->u.m[1][3] * t[2];
271 v[12] = -pm->u.m[3][2] * t[0] + pm->u.m[2][2] * t[1] - pm->u.m[1][2] * t[2];
273 det = pm->u.m[0][0] * v[0] + pm->u.m[0][1] * v[4] +
274 pm->u.m[0][2] * v[8] + pm->u.m[0][3] * v[12];
275 if (det == 0.0f)
276 return NULL;
277 if (pdeterminant)
278 *pdeterminant = det;
280 t[0] = pm->u.m[2][2] * pm->u.m[3][3] - pm->u.m[2][3] * pm->u.m[3][2];
281 t[1] = pm->u.m[0][2] * pm->u.m[3][3] - pm->u.m[0][3] * pm->u.m[3][2];
282 t[2] = pm->u.m[0][2] * pm->u.m[2][3] - pm->u.m[0][3] * pm->u.m[2][2];
283 v[1] = -pm->u.m[0][1] * t[0] + pm->u.m[2][1] * t[1] - pm->u.m[3][1] * t[2];
284 v[5] = pm->u.m[0][0] * t[0] - pm->u.m[2][0] * t[1] + pm->u.m[3][0] * t[2];
286 t[0] = pm->u.m[0][0] * pm->u.m[2][1] - pm->u.m[2][0] * pm->u.m[0][1];
287 t[1] = pm->u.m[3][0] * pm->u.m[0][1] - pm->u.m[0][0] * pm->u.m[3][1];
288 t[2] = pm->u.m[2][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[2][1];
289 v[9] = -pm->u.m[3][3] * t[0] - pm->u.m[2][3] * t[1]- pm->u.m[0][3] * t[2];
290 v[13] = pm->u.m[3][2] * t[0] + pm->u.m[2][2] * t[1] + pm->u.m[0][2] * t[2];
292 t[0] = pm->u.m[1][2] * pm->u.m[3][3] - pm->u.m[1][3] * pm->u.m[3][2];
293 t[1] = pm->u.m[0][2] * pm->u.m[3][3] - pm->u.m[0][3] * pm->u.m[3][2];
294 t[2] = pm->u.m[0][2] * pm->u.m[1][3] - pm->u.m[0][3] * pm->u.m[1][2];
295 v[2] = pm->u.m[0][1] * t[0] - pm->u.m[1][1] * t[1] + pm->u.m[3][1] * t[2];
296 v[6] = -pm->u.m[0][0] * t[0] + pm->u.m[1][0] * t[1] - pm->u.m[3][0] * t[2];
298 t[0] = pm->u.m[0][0] * pm->u.m[1][1] - pm->u.m[1][0] * pm->u.m[0][1];
299 t[1] = pm->u.m[3][0] * pm->u.m[0][1] - pm->u.m[0][0] * pm->u.m[3][1];
300 t[2] = pm->u.m[1][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[1][1];
301 v[10] = pm->u.m[3][3] * t[0] + pm->u.m[1][3] * t[1] + pm->u.m[0][3] * t[2];
302 v[14] = -pm->u.m[3][2] * t[0] - pm->u.m[1][2] * t[1] - pm->u.m[0][2] * t[2];
304 t[0] = pm->u.m[1][2] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][2];
305 t[1] = pm->u.m[0][2] * pm->u.m[2][3] - pm->u.m[0][3] * pm->u.m[2][2];
306 t[2] = pm->u.m[0][2] * pm->u.m[1][3] - pm->u.m[0][3] * pm->u.m[1][2];
307 v[3] = -pm->u.m[0][1] * t[0] + pm->u.m[1][1] * t[1] - pm->u.m[2][1] * t[2];
308 v[7] = pm->u.m[0][0] * t[0] - pm->u.m[1][0] * t[1] + pm->u.m[2][0] * t[2];
310 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]) +
311 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]) -
312 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]);
314 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]) -
315 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]) +
316 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]);
318 det = 1.0f / det;
320 for (i = 0; i < 4; i++)
321 for (j = 0; j < 4; j++)
322 pout->u.m[i][j] = v[4 * i + j] * det;
324 return pout;
327 D3DXMATRIX * WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *out, const D3DXVECTOR3 *eye, const D3DXVECTOR3 *at,
328 const D3DXVECTOR3 *up)
330 D3DXVECTOR3 right, upn, vec;
332 TRACE("out %p, eye %p, at %p, up %p\n", out, eye, at, up);
334 D3DXVec3Subtract(&vec, at, eye);
335 D3DXVec3Normalize(&vec, &vec);
336 D3DXVec3Cross(&right, up, &vec);
337 D3DXVec3Cross(&upn, &vec, &right);
338 D3DXVec3Normalize(&right, &right);
339 D3DXVec3Normalize(&upn, &upn);
340 out->u.m[0][0] = right.x;
341 out->u.m[1][0] = right.y;
342 out->u.m[2][0] = right.z;
343 out->u.m[3][0] = -D3DXVec3Dot(&right, eye);
344 out->u.m[0][1] = upn.x;
345 out->u.m[1][1] = upn.y;
346 out->u.m[2][1] = upn.z;
347 out->u.m[3][1] = -D3DXVec3Dot(&upn, eye);
348 out->u.m[0][2] = vec.x;
349 out->u.m[1][2] = vec.y;
350 out->u.m[2][2] = vec.z;
351 out->u.m[3][2] = -D3DXVec3Dot(&vec, eye);
352 out->u.m[0][3] = 0.0f;
353 out->u.m[1][3] = 0.0f;
354 out->u.m[2][3] = 0.0f;
355 out->u.m[3][3] = 1.0f;
357 return out;
360 D3DXMATRIX * WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *out, const D3DXVECTOR3 *eye, const D3DXVECTOR3 *at,
361 const D3DXVECTOR3 *up)
363 D3DXVECTOR3 right, upn, vec;
365 TRACE("out %p, eye %p, at %p, up %p\n", out, eye, at, up);
367 D3DXVec3Subtract(&vec, at, eye);
368 D3DXVec3Normalize(&vec, &vec);
369 D3DXVec3Cross(&right, up, &vec);
370 D3DXVec3Cross(&upn, &vec, &right);
371 D3DXVec3Normalize(&right, &right);
372 D3DXVec3Normalize(&upn, &upn);
373 out->u.m[0][0] = -right.x;
374 out->u.m[1][0] = -right.y;
375 out->u.m[2][0] = -right.z;
376 out->u.m[3][0] = D3DXVec3Dot(&right, eye);
377 out->u.m[0][1] = upn.x;
378 out->u.m[1][1] = upn.y;
379 out->u.m[2][1] = upn.z;
380 out->u.m[3][1] = -D3DXVec3Dot(&upn, eye);
381 out->u.m[0][2] = -vec.x;
382 out->u.m[1][2] = -vec.y;
383 out->u.m[2][2] = -vec.z;
384 out->u.m[3][2] = D3DXVec3Dot(&vec, eye);
385 out->u.m[0][3] = 0.0f;
386 out->u.m[1][3] = 0.0f;
387 out->u.m[2][3] = 0.0f;
388 out->u.m[3][3] = 1.0f;
390 return out;
393 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, const D3DXMATRIX *pm1, const D3DXMATRIX *pm2)
395 D3DXMATRIX out;
396 int i,j;
398 TRACE("pout %p, pm1 %p, pm2 %p\n", pout, pm1, pm2);
400 for (i=0; i<4; i++)
402 for (j=0; j<4; j++)
404 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];
408 *pout = out;
409 return pout;
412 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, const D3DXMATRIX *pm1, const D3DXMATRIX *pm2)
414 D3DXMATRIX temp;
415 int i, j;
417 TRACE("pout %p, pm1 %p, pm2 %p\n", pout, pm1, pm2);
419 for (i = 0; i < 4; i++)
420 for (j = 0; j < 4; j++)
421 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];
423 *pout = temp;
424 return pout;
427 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
429 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
431 D3DXMatrixIdentity(pout);
432 pout->u.m[0][0] = 2.0f / w;
433 pout->u.m[1][1] = 2.0f / h;
434 pout->u.m[2][2] = 1.0f / (zf - zn);
435 pout->u.m[3][2] = zn / (zn - zf);
436 return pout;
439 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
441 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
443 D3DXMatrixIdentity(pout);
444 pout->u.m[0][0] = 2.0f / (r - l);
445 pout->u.m[1][1] = 2.0f / (t - b);
446 pout->u.m[2][2] = 1.0f / (zf -zn);
447 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
448 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
449 pout->u.m[3][2] = zn / (zn -zf);
450 return pout;
453 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
455 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
457 D3DXMatrixIdentity(pout);
458 pout->u.m[0][0] = 2.0f / (r - l);
459 pout->u.m[1][1] = 2.0f / (t - b);
460 pout->u.m[2][2] = 1.0f / (zn -zf);
461 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
462 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
463 pout->u.m[3][2] = zn / (zn -zf);
464 return pout;
467 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
469 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
471 D3DXMatrixIdentity(pout);
472 pout->u.m[0][0] = 2.0f / w;
473 pout->u.m[1][1] = 2.0f / h;
474 pout->u.m[2][2] = 1.0f / (zn - zf);
475 pout->u.m[3][2] = zn / (zn - zf);
476 return pout;
479 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
481 TRACE("pout %p, fovy %f, aspect %f, zn %f, zf %f\n", pout, fovy, aspect, zn, zf);
483 D3DXMatrixIdentity(pout);
484 pout->u.m[0][0] = 1.0f / (aspect * tanf(fovy/2.0f));
485 pout->u.m[1][1] = 1.0f / tanf(fovy/2.0f);
486 pout->u.m[2][2] = zf / (zf - zn);
487 pout->u.m[2][3] = 1.0f;
488 pout->u.m[3][2] = (zf * zn) / (zn - zf);
489 pout->u.m[3][3] = 0.0f;
490 return pout;
493 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
495 TRACE("pout %p, fovy %f, aspect %f, zn %f, zf %f\n", pout, fovy, aspect, zn, zf);
497 D3DXMatrixIdentity(pout);
498 pout->u.m[0][0] = 1.0f / (aspect * tanf(fovy/2.0f));
499 pout->u.m[1][1] = 1.0f / tanf(fovy/2.0f);
500 pout->u.m[2][2] = zf / (zn - zf);
501 pout->u.m[2][3] = -1.0f;
502 pout->u.m[3][2] = (zf * zn) / (zn - zf);
503 pout->u.m[3][3] = 0.0f;
504 return pout;
507 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
509 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
511 D3DXMatrixIdentity(pout);
512 pout->u.m[0][0] = 2.0f * zn / w;
513 pout->u.m[1][1] = 2.0f * zn / h;
514 pout->u.m[2][2] = zf / (zf - zn);
515 pout->u.m[3][2] = (zn * zf) / (zn - zf);
516 pout->u.m[2][3] = 1.0f;
517 pout->u.m[3][3] = 0.0f;
518 return pout;
521 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
523 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
525 D3DXMatrixIdentity(pout);
526 pout->u.m[0][0] = 2.0f * zn / (r - l);
527 pout->u.m[1][1] = -2.0f * zn / (b - t);
528 pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
529 pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
530 pout->u.m[2][2] = - zf / (zn - zf);
531 pout->u.m[3][2] = (zn * zf) / (zn -zf);
532 pout->u.m[2][3] = 1.0f;
533 pout->u.m[3][3] = 0.0f;
534 return pout;
537 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
539 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
541 D3DXMatrixIdentity(pout);
542 pout->u.m[0][0] = 2.0f * zn / (r - l);
543 pout->u.m[1][1] = -2.0f * zn / (b - t);
544 pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
545 pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
546 pout->u.m[2][2] = zf / (zn - zf);
547 pout->u.m[3][2] = (zn * zf) / (zn -zf);
548 pout->u.m[2][3] = -1.0f;
549 pout->u.m[3][3] = 0.0f;
550 return pout;
553 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
555 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
557 D3DXMatrixIdentity(pout);
558 pout->u.m[0][0] = 2.0f * zn / w;
559 pout->u.m[1][1] = 2.0f * zn / h;
560 pout->u.m[2][2] = zf / (zn - zf);
561 pout->u.m[3][2] = (zn * zf) / (zn - zf);
562 pout->u.m[2][3] = -1.0f;
563 pout->u.m[3][3] = 0.0f;
564 return pout;
567 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, const D3DXPLANE *pplane)
569 D3DXPLANE Nplane;
571 TRACE("pout %p, pplane %p\n", pout, pplane);
573 D3DXPlaneNormalize(&Nplane, pplane);
574 D3DXMatrixIdentity(pout);
575 pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
576 pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
577 pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
578 pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
579 pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
580 pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
581 pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
582 pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
583 pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
584 pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
585 pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
586 pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
587 return pout;
590 D3DXMATRIX * WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *out, const D3DXVECTOR3 *v, FLOAT angle)
592 D3DXVECTOR3 nv;
593 FLOAT sangle, cangle, cdiff;
595 TRACE("out %p, v %p, angle %f\n", out, v, angle);
597 D3DXVec3Normalize(&nv, v);
598 sangle = sinf(angle);
599 cangle = cosf(angle);
600 cdiff = 1.0f - cangle;
602 out->u.m[0][0] = cdiff * nv.x * nv.x + cangle;
603 out->u.m[1][0] = cdiff * nv.x * nv.y - sangle * nv.z;
604 out->u.m[2][0] = cdiff * nv.x * nv.z + sangle * nv.y;
605 out->u.m[3][0] = 0.0f;
606 out->u.m[0][1] = cdiff * nv.y * nv.x + sangle * nv.z;
607 out->u.m[1][1] = cdiff * nv.y * nv.y + cangle;
608 out->u.m[2][1] = cdiff * nv.y * nv.z - sangle * nv.x;
609 out->u.m[3][1] = 0.0f;
610 out->u.m[0][2] = cdiff * nv.z * nv.x - sangle * nv.y;
611 out->u.m[1][2] = cdiff * nv.z * nv.y + sangle * nv.x;
612 out->u.m[2][2] = cdiff * nv.z * nv.z + cangle;
613 out->u.m[3][2] = 0.0f;
614 out->u.m[0][3] = 0.0f;
615 out->u.m[1][3] = 0.0f;
616 out->u.m[2][3] = 0.0f;
617 out->u.m[3][3] = 1.0f;
619 return out;
622 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, const D3DXQUATERNION *pq)
624 TRACE("pout %p, pq %p\n", pout, pq);
626 D3DXMatrixIdentity(pout);
627 pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
628 pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
629 pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
630 pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
631 pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
632 pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
633 pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
634 pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
635 pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
636 return pout;
639 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
641 TRACE("pout %p, angle %f\n", pout, angle);
643 D3DXMatrixIdentity(pout);
644 pout->u.m[1][1] = cosf(angle);
645 pout->u.m[2][2] = cosf(angle);
646 pout->u.m[1][2] = sinf(angle);
647 pout->u.m[2][1] = -sinf(angle);
648 return pout;
651 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
653 TRACE("pout %p, angle %f\n", pout, angle);
655 D3DXMatrixIdentity(pout);
656 pout->u.m[0][0] = cosf(angle);
657 pout->u.m[2][2] = cosf(angle);
658 pout->u.m[0][2] = -sinf(angle);
659 pout->u.m[2][0] = sinf(angle);
660 return pout;
663 D3DXMATRIX * WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *out, FLOAT yaw, FLOAT pitch, FLOAT roll)
665 FLOAT sroll, croll, spitch, cpitch, syaw, cyaw;
667 TRACE("out %p, yaw %f, pitch %f, roll %f\n", out, yaw, pitch, roll);
669 sroll = sinf(roll);
670 croll = cosf(roll);
671 spitch = sinf(pitch);
672 cpitch = cosf(pitch);
673 syaw = sinf(yaw);
674 cyaw = cosf(yaw);
676 out->u.m[0][0] = sroll * spitch * syaw + croll * cyaw;
677 out->u.m[0][1] = sroll * cpitch;
678 out->u.m[0][2] = sroll * spitch * cyaw - croll * syaw;
679 out->u.m[0][3] = 0.0f;
680 out->u.m[1][0] = croll * spitch * syaw - sroll * cyaw;
681 out->u.m[1][1] = croll * cpitch;
682 out->u.m[1][2] = croll * spitch * cyaw + sroll * syaw;
683 out->u.m[1][3] = 0.0f;
684 out->u.m[2][0] = cpitch * syaw;
685 out->u.m[2][1] = -spitch;
686 out->u.m[2][2] = cpitch * cyaw;
687 out->u.m[2][3] = 0.0f;
688 out->u.m[3][0] = 0.0f;
689 out->u.m[3][1] = 0.0f;
690 out->u.m[3][2] = 0.0f;
691 out->u.m[3][3] = 1.0f;
693 return out;
696 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
698 TRACE("pout %p, angle %f\n", pout, angle);
700 D3DXMatrixIdentity(pout);
701 pout->u.m[0][0] = cosf(angle);
702 pout->u.m[1][1] = cosf(angle);
703 pout->u.m[0][1] = sinf(angle);
704 pout->u.m[1][0] = -sinf(angle);
705 return pout;
708 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
710 TRACE("pout %p, sx %f, sy %f, sz %f\n", pout, sx, sy, sz);
712 D3DXMatrixIdentity(pout);
713 pout->u.m[0][0] = sx;
714 pout->u.m[1][1] = sy;
715 pout->u.m[2][2] = sz;
716 return pout;
719 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, const D3DXVECTOR4 *plight, const D3DXPLANE *pplane)
721 D3DXPLANE Nplane;
722 FLOAT dot;
724 TRACE("pout %p, plight %p, pplane %p\n", pout, plight, pplane);
726 D3DXPlaneNormalize(&Nplane, pplane);
727 dot = D3DXPlaneDot(&Nplane, plight);
728 pout->u.m[0][0] = dot - Nplane.a * plight->x;
729 pout->u.m[0][1] = -Nplane.a * plight->y;
730 pout->u.m[0][2] = -Nplane.a * plight->z;
731 pout->u.m[0][3] = -Nplane.a * plight->w;
732 pout->u.m[1][0] = -Nplane.b * plight->x;
733 pout->u.m[1][1] = dot - Nplane.b * plight->y;
734 pout->u.m[1][2] = -Nplane.b * plight->z;
735 pout->u.m[1][3] = -Nplane.b * plight->w;
736 pout->u.m[2][0] = -Nplane.c * plight->x;
737 pout->u.m[2][1] = -Nplane.c * plight->y;
738 pout->u.m[2][2] = dot - Nplane.c * plight->z;
739 pout->u.m[2][3] = -Nplane.c * plight->w;
740 pout->u.m[3][0] = -Nplane.d * plight->x;
741 pout->u.m[3][1] = -Nplane.d * plight->y;
742 pout->u.m[3][2] = -Nplane.d * plight->z;
743 pout->u.m[3][3] = dot - Nplane.d * plight->w;
744 return pout;
747 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, const D3DXVECTOR3 *pscalingcenter, const D3DXQUATERNION *pscalingrotation, const D3DXVECTOR3 *pscaling, const D3DXVECTOR3 *protationcenter, const D3DXQUATERNION *protation, const D3DXVECTOR3 *ptranslation)
749 D3DXMATRIX m1, m2, m3, m4, m5, m6, m7;
750 D3DXQUATERNION prc;
751 D3DXVECTOR3 psc, pt;
753 TRACE("pout %p, pscalingcenter %p, pscalingrotation %p, pscaling %p, protationcentr %p, protation %p, ptranslation %p\n",
754 pout, pscalingcenter, pscalingrotation, pscaling, protationcenter, protation, ptranslation);
756 if ( !pscalingcenter )
758 psc.x = 0.0f;
759 psc.y = 0.0f;
760 psc.z = 0.0f;
762 else
764 psc.x = pscalingcenter->x;
765 psc.y = pscalingcenter->y;
766 psc.z = pscalingcenter->z;
769 if ( !protationcenter )
771 prc.x = 0.0f;
772 prc.y = 0.0f;
773 prc.z = 0.0f;
775 else
777 prc.x = protationcenter->x;
778 prc.y = protationcenter->y;
779 prc.z = protationcenter->z;
782 if ( !ptranslation )
784 pt.x = 0.0f;
785 pt.y = 0.0f;
786 pt.z = 0.0f;
788 else
790 pt.x = ptranslation->x;
791 pt.y = ptranslation->y;
792 pt.z = ptranslation->z;
795 D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
797 if ( !pscalingrotation )
799 D3DXMatrixIdentity(&m2);
800 D3DXMatrixIdentity(&m4);
802 else
804 D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
805 D3DXMatrixInverse(&m2, NULL, &m4);
808 if ( !pscaling ) D3DXMatrixIdentity(&m3);
809 else D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
811 if ( !protation ) D3DXMatrixIdentity(&m6);
812 else D3DXMatrixRotationQuaternion(&m6, protation);
814 D3DXMatrixTranslation(&m5, psc.x - prc.x, psc.y - prc.y, psc.z - prc.z);
815 D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
816 D3DXMatrixMultiply(&m1, &m1, &m2);
817 D3DXMatrixMultiply(&m1, &m1, &m3);
818 D3DXMatrixMultiply(&m1, &m1, &m4);
819 D3DXMatrixMultiply(&m1, &m1, &m5);
820 D3DXMatrixMultiply(&m1, &m1, &m6);
821 D3DXMatrixMultiply(pout, &m1, &m7);
822 return pout;
825 D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(D3DXMATRIX *pout, const D3DXVECTOR2 *pscalingcenter, FLOAT scalingrotation, const D3DXVECTOR2 *pscaling, const D3DXVECTOR2 *protationcenter, FLOAT rotation, const D3DXVECTOR2 *ptranslation)
827 D3DXQUATERNION rot, sca_rot;
828 D3DXVECTOR3 rot_center, sca, sca_center, trans;
830 TRACE("pout %p, pscalingcenter %p, scalingrotation %f, pscaling %p, protztioncenter %p, rotation %f, ptranslation %p\n",
831 pout, pscalingcenter, scalingrotation, pscaling, protationcenter, rotation, ptranslation);
833 if ( pscalingcenter )
835 sca_center.x=pscalingcenter->x;
836 sca_center.y=pscalingcenter->y;
837 sca_center.z=0.0f;
839 else
841 sca_center.x=0.0f;
842 sca_center.y=0.0f;
843 sca_center.z=0.0f;
846 if ( pscaling )
848 sca.x=pscaling->x;
849 sca.y=pscaling->y;
850 sca.z=1.0f;
852 else
854 sca.x=1.0f;
855 sca.y=1.0f;
856 sca.z=1.0f;
859 if ( protationcenter )
861 rot_center.x=protationcenter->x;
862 rot_center.y=protationcenter->y;
863 rot_center.z=0.0f;
865 else
867 rot_center.x=0.0f;
868 rot_center.y=0.0f;
869 rot_center.z=0.0f;
872 if ( ptranslation )
874 trans.x=ptranslation->x;
875 trans.y=ptranslation->y;
876 trans.z=0.0f;
878 else
880 trans.x=0.0f;
881 trans.y=0.0f;
882 trans.z=0.0f;
885 rot.w=cosf(rotation/2.0f);
886 rot.x=0.0f;
887 rot.y=0.0f;
888 rot.z=sinf(rotation/2.0f);
890 sca_rot.w=cosf(scalingrotation/2.0f);
891 sca_rot.x=0.0f;
892 sca_rot.y=0.0f;
893 sca_rot.z=sinf(scalingrotation/2.0f);
895 D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
897 return pout;
900 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
902 TRACE("pout %p, x %f, y %f, z %f\n", pout, x, y, z);
904 D3DXMatrixIdentity(pout);
905 pout->u.m[3][0] = x;
906 pout->u.m[3][1] = y;
907 pout->u.m[3][2] = z;
908 return pout;
911 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, const D3DXMATRIX *pm)
913 const D3DXMATRIX m = *pm;
914 int i,j;
916 TRACE("pout %p, pm %p\n", pout, pm);
918 for (i=0; i<4; i++)
919 for (j=0; j<4; j++) pout->u.m[i][j] = m.u.m[j][i];
921 return pout;
924 /*_________________D3DXMatrixStack____________________*/
927 static inline struct ID3DXMatrixStackImpl *impl_from_ID3DXMatrixStack(ID3DXMatrixStack *iface)
929 return CONTAINING_RECORD(iface, struct ID3DXMatrixStackImpl, ID3DXMatrixStack_iface);
932 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **out)
934 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
936 if (IsEqualGUID(riid, &IID_ID3DXMatrixStack)
937 || IsEqualGUID(riid, &IID_IUnknown))
939 ID3DXMatrixStack_AddRef(iface);
940 *out = iface;
941 return S_OK;
944 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
946 *out = NULL;
947 return E_NOINTERFACE;
950 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
952 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
953 ULONG ref = InterlockedIncrement(&This->ref);
954 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
955 return ref;
958 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack *iface)
960 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
961 ULONG ref = InterlockedDecrement(&This->ref);
962 if (!ref)
964 HeapFree(GetProcessHeap(), 0, This->stack);
965 HeapFree(GetProcessHeap(), 0, This);
967 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
968 return ref;
971 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
973 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
975 TRACE("iface %p\n", iface);
977 return &This->stack[This->current];
980 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
982 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
984 TRACE("iface %p\n", iface);
986 D3DXMatrixIdentity(&This->stack[This->current]);
988 return D3D_OK;
991 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
993 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
995 TRACE("iface %p, pm %p\n", iface, pm);
997 This->stack[This->current] = *pm;
999 return D3D_OK;
1002 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
1004 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1006 TRACE("iface %p, pm %p\n", iface, pm);
1008 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], pm);
1010 return D3D_OK;
1013 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
1015 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1017 TRACE("iface %p, pm %p\n", iface, pm);
1019 D3DXMatrixMultiply(&This->stack[This->current], pm, &This->stack[This->current]);
1021 return D3D_OK;
1024 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
1026 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1028 TRACE("iface %p\n", iface);
1030 /* Popping the last element on the stack returns D3D_OK, but does nothing. */
1031 if (!This->current) return D3D_OK;
1033 if (This->current <= This->stack_size / 4 && This->stack_size >= INITIAL_STACK_SIZE * 2)
1035 unsigned int new_size;
1036 D3DXMATRIX *new_stack;
1038 new_size = This->stack_size / 2;
1039 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1040 if (new_stack)
1042 This->stack_size = new_size;
1043 This->stack = new_stack;
1047 --This->current;
1049 return D3D_OK;
1052 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
1054 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1056 TRACE("iface %p\n", iface);
1058 if (This->current == This->stack_size - 1)
1060 unsigned int new_size;
1061 D3DXMATRIX *new_stack;
1063 if (This->stack_size > UINT_MAX / 2) return E_OUTOFMEMORY;
1065 new_size = This->stack_size * 2;
1066 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1067 if (!new_stack) return E_OUTOFMEMORY;
1069 This->stack_size = new_size;
1070 This->stack = new_stack;
1073 ++This->current;
1074 This->stack[This->current] = This->stack[This->current - 1];
1076 return D3D_OK;
1079 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, const D3DXVECTOR3 *pv, FLOAT angle)
1081 D3DXMATRIX temp;
1082 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1084 TRACE("iface %p, pv %p, angle %f\n", iface, pv, angle);
1086 D3DXMatrixRotationAxis(&temp, pv, angle);
1087 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1089 return D3D_OK;
1092 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, const D3DXVECTOR3 *pv, FLOAT angle)
1094 D3DXMATRIX temp;
1095 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1097 TRACE("iface %p, pv %p, angle %f\n", iface, pv, angle);
1099 D3DXMatrixRotationAxis(&temp, pv, angle);
1100 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1102 return D3D_OK;
1105 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1107 D3DXMATRIX temp;
1108 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1110 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1112 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1113 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1115 return D3D_OK;
1118 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1120 D3DXMATRIX temp;
1121 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1123 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1125 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1126 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1128 return D3D_OK;
1131 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1133 D3DXMATRIX temp;
1134 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1136 TRACE("iface %p,x %f, y %f, z %f\n", iface, x, y, z);
1138 D3DXMatrixScaling(&temp, x, y, z);
1139 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1141 return D3D_OK;
1144 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1146 D3DXMATRIX temp;
1147 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1149 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1151 D3DXMatrixScaling(&temp, x, y, z);
1152 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1154 return D3D_OK;
1157 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1159 D3DXMATRIX temp;
1160 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1162 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1164 D3DXMatrixTranslation(&temp, x, y, z);
1165 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1167 return D3D_OK;
1170 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1172 D3DXMATRIX temp;
1173 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1175 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1177 D3DXMatrixTranslation(&temp, x, y, z);
1178 D3DXMatrixMultiply(&This->stack[This->current], &temp,&This->stack[This->current]);
1180 return D3D_OK;
1183 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
1185 ID3DXMatrixStackImpl_QueryInterface,
1186 ID3DXMatrixStackImpl_AddRef,
1187 ID3DXMatrixStackImpl_Release,
1188 ID3DXMatrixStackImpl_Pop,
1189 ID3DXMatrixStackImpl_Push,
1190 ID3DXMatrixStackImpl_LoadIdentity,
1191 ID3DXMatrixStackImpl_LoadMatrix,
1192 ID3DXMatrixStackImpl_MultMatrix,
1193 ID3DXMatrixStackImpl_MultMatrixLocal,
1194 ID3DXMatrixStackImpl_RotateAxis,
1195 ID3DXMatrixStackImpl_RotateAxisLocal,
1196 ID3DXMatrixStackImpl_RotateYawPitchRoll,
1197 ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
1198 ID3DXMatrixStackImpl_Scale,
1199 ID3DXMatrixStackImpl_ScaleLocal,
1200 ID3DXMatrixStackImpl_Translate,
1201 ID3DXMatrixStackImpl_TranslateLocal,
1202 ID3DXMatrixStackImpl_GetTop
1205 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, ID3DXMatrixStack **stack)
1207 struct ID3DXMatrixStackImpl *object;
1209 TRACE("flags %#x, stack %p.\n", flags, stack);
1211 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
1213 *stack = NULL;
1214 return E_OUTOFMEMORY;
1216 object->ID3DXMatrixStack_iface.lpVtbl = &ID3DXMatrixStack_Vtbl;
1217 object->ref = 1;
1219 if (!(object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(*object->stack))))
1221 HeapFree(GetProcessHeap(), 0, object);
1222 *stack = NULL;
1223 return E_OUTOFMEMORY;
1226 object->current = 0;
1227 object->stack_size = INITIAL_STACK_SIZE;
1228 D3DXMatrixIdentity(&object->stack[0]);
1230 TRACE("Created matrix stack %p.\n", object);
1232 *stack = &object->ID3DXMatrixStack_iface;
1233 return D3D_OK;
1236 /*_________________D3DXPLANE________________*/
1238 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, const D3DXVECTOR3 *pvpoint, const D3DXVECTOR3 *pvnormal)
1240 TRACE("pout %p, pvpoint %p, pvnormal %p\n", pout, pvpoint, pvnormal);
1242 pout->a = pvnormal->x;
1243 pout->b = pvnormal->y;
1244 pout->c = pvnormal->z;
1245 pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
1246 return pout;
1249 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3)
1251 D3DXVECTOR3 edge1, edge2, normal, Nnormal;
1253 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p\n", pout, pv1, pv2, pv3);
1255 edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
1256 edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
1257 D3DXVec3Subtract(&edge1, pv2, pv1);
1258 D3DXVec3Subtract(&edge2, pv3, pv1);
1259 D3DXVec3Cross(&normal, &edge1, &edge2);
1260 D3DXVec3Normalize(&Nnormal, &normal);
1261 D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
1262 return pout;
1265 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, const D3DXPLANE *pp, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2)
1267 D3DXVECTOR3 direction, normal;
1268 FLOAT dot, temp;
1270 TRACE("pout %p, pp %p, pv1 %p, pv2 %p\n", pout, pp, pv1, pv2);
1272 normal.x = pp->a;
1273 normal.y = pp->b;
1274 normal.z = pp->c;
1275 direction.x = pv2->x - pv1->x;
1276 direction.y = pv2->y - pv1->y;
1277 direction.z = pv2->z - pv1->z;
1278 dot = D3DXVec3Dot(&normal, &direction);
1279 if ( !dot ) return NULL;
1280 temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
1281 pout->x = pv1->x - temp * direction.x;
1282 pout->y = pv1->y - temp * direction.y;
1283 pout->z = pv1->z - temp * direction.z;
1284 return pout;
1287 D3DXPLANE * WINAPI D3DXPlaneNormalize(D3DXPLANE *out, const D3DXPLANE *p)
1289 FLOAT norm;
1291 TRACE("out %p, p %p\n", out, p);
1293 norm = sqrtf(p->a * p->a + p->b * p->b + p->c * p->c);
1294 if (norm)
1296 out->a = p->a / norm;
1297 out->b = p->b / norm;
1298 out->c = p->c / norm;
1299 out->d = p->d / norm;
1301 else
1303 out->a = 0.0f;
1304 out->b = 0.0f;
1305 out->c = 0.0f;
1306 out->d = 0.0f;
1309 return out;
1312 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, const D3DXPLANE *pplane, const D3DXMATRIX *pm)
1314 const D3DXPLANE plane = *pplane;
1316 TRACE("pout %p, pplane %p, pm %p\n", pout, pplane, pm);
1318 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;
1319 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;
1320 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;
1321 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;
1322 return pout;
1325 D3DXPLANE* WINAPI D3DXPlaneTransformArray(D3DXPLANE* out, UINT outstride, const D3DXPLANE* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1327 UINT i;
1329 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1331 for (i = 0; i < elements; ++i) {
1332 D3DXPlaneTransform(
1333 (D3DXPLANE*)((char*)out + outstride * i),
1334 (const D3DXPLANE*)((const char*)in + instride * i),
1335 matrix);
1337 return out;
1340 /*_________________D3DXQUATERNION________________*/
1342 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
1344 D3DXQUATERNION temp1, temp2;
1346 TRACE("pout %p, pq1 %p, pq2 %p, pq3 %p, f %f, g %f\n", pout, pq1, pq2, pq3, f, g);
1348 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
1349 return pout;
1352 D3DXQUATERNION * WINAPI D3DXQuaternionExp(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1354 FLOAT norm;
1356 TRACE("out %p, q %p\n", out, q);
1358 norm = sqrtf(q->x * q->x + q->y * q->y + q->z * q->z);
1359 if (norm)
1361 out->x = sinf(norm) * q->x / norm;
1362 out->y = sinf(norm) * q->y / norm;
1363 out->z = sinf(norm) * q->z / norm;
1364 out->w = cosf(norm);
1366 else
1368 out->x = 0.0f;
1369 out->y = 0.0f;
1370 out->z = 0.0f;
1371 out->w = 1.0f;
1374 return out;
1377 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, const D3DXQUATERNION *pq)
1379 D3DXQUATERNION out;
1380 FLOAT norm;
1382 TRACE("pout %p, pq %p\n", pout, pq);
1384 norm = D3DXQuaternionLengthSq(pq);
1386 out.x = -pq->x / norm;
1387 out.y = -pq->y / norm;
1388 out.z = -pq->z / norm;
1389 out.w = pq->w / norm;
1391 *pout =out;
1392 return pout;
1395 D3DXQUATERNION * WINAPI D3DXQuaternionLn(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1397 FLOAT t;
1399 TRACE("out %p, q %p\n", out, q);
1401 if ((q->w >= 1.0f) || (q->w == -1.0f))
1402 t = 1.0f;
1403 else
1404 t = acosf(q->w) / sqrtf(1.0f - q->w * q->w);
1406 out->x = t * q->x;
1407 out->y = t * q->y;
1408 out->z = t * q->z;
1409 out->w = 0.0f;
1411 return out;
1414 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2)
1416 D3DXQUATERNION out;
1418 TRACE("pout %p, pq1 %p, pq2 %p\n", pout, pq1, pq2);
1420 out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
1421 out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
1422 out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
1423 out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
1424 *pout = out;
1425 return pout;
1428 D3DXQUATERNION * WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1430 FLOAT norm;
1432 TRACE("out %p, q %p\n", out, q);
1434 norm = D3DXQuaternionLength(q);
1436 out->x = q->x / norm;
1437 out->y = q->y / norm;
1438 out->z = q->z / norm;
1439 out->w = q->w / norm;
1441 return out;
1444 D3DXQUATERNION * WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *out, const D3DXVECTOR3 *v, FLOAT angle)
1446 D3DXVECTOR3 temp;
1448 TRACE("out %p, v %p, angle %f\n", out, v, angle);
1450 D3DXVec3Normalize(&temp, v);
1452 out->x = sinf(angle / 2.0f) * temp.x;
1453 out->y = sinf(angle / 2.0f) * temp.y;
1454 out->z = sinf(angle / 2.0f) * temp.z;
1455 out->w = cosf(angle / 2.0f);
1457 return out;
1460 D3DXQUATERNION * WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *out, const D3DXMATRIX *m)
1462 FLOAT s, trace;
1464 TRACE("out %p, m %p\n", out, m);
1466 trace = m->u.m[0][0] + m->u.m[1][1] + m->u.m[2][2] + 1.0f;
1467 if (trace > 1.0f)
1469 s = 2.0f * sqrtf(trace);
1470 out->x = (m->u.m[1][2] - m->u.m[2][1]) / s;
1471 out->y = (m->u.m[2][0] - m->u.m[0][2]) / s;
1472 out->z = (m->u.m[0][1] - m->u.m[1][0]) / s;
1473 out->w = 0.25f * s;
1475 else
1477 int i, maxi = 0;
1479 for (i = 1; i < 3; i++)
1481 if (m->u.m[i][i] > m->u.m[maxi][maxi])
1482 maxi = i;
1485 switch (maxi)
1487 case 0:
1488 s = 2.0f * sqrtf(1.0f + m->u.m[0][0] - m->u.m[1][1] - m->u.m[2][2]);
1489 out->x = 0.25f * s;
1490 out->y = (m->u.m[0][1] + m->u.m[1][0]) / s;
1491 out->z = (m->u.m[0][2] + m->u.m[2][0]) / s;
1492 out->w = (m->u.m[1][2] - m->u.m[2][1]) / s;
1493 break;
1495 case 1:
1496 s = 2.0f * sqrtf(1.0f + m->u.m[1][1] - m->u.m[0][0] - m->u.m[2][2]);
1497 out->x = (m->u.m[0][1] + m->u.m[1][0]) / s;
1498 out->y = 0.25f * s;
1499 out->z = (m->u.m[1][2] + m->u.m[2][1]) / s;
1500 out->w = (m->u.m[2][0] - m->u.m[0][2]) / s;
1501 break;
1503 case 2:
1504 s = 2.0f * sqrtf(1.0f + m->u.m[2][2] - m->u.m[0][0] - m->u.m[1][1]);
1505 out->x = (m->u.m[0][2] + m->u.m[2][0]) / s;
1506 out->y = (m->u.m[1][2] + m->u.m[2][1]) / s;
1507 out->z = 0.25f * s;
1508 out->w = (m->u.m[0][1] - m->u.m[1][0]) / s;
1509 break;
1513 return out;
1516 D3DXQUATERNION * WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *out, FLOAT yaw, FLOAT pitch, FLOAT roll)
1518 FLOAT syaw, cyaw, spitch, cpitch, sroll, croll;
1520 TRACE("out %p, yaw %f, pitch %f, roll %f\n", out, yaw, pitch, roll);
1522 syaw = sinf(yaw / 2.0f);
1523 cyaw = cosf(yaw / 2.0f);
1524 spitch = sinf(pitch / 2.0f);
1525 cpitch = cosf(pitch / 2.0f);
1526 sroll = sinf(roll / 2.0f);
1527 croll = cosf(roll / 2.0f);
1529 out->x = syaw * cpitch * sroll + cyaw * spitch * croll;
1530 out->y = syaw * cpitch * croll - cyaw * spitch * sroll;
1531 out->z = cyaw * cpitch * sroll - syaw * spitch * croll;
1532 out->w = cyaw * cpitch * croll + syaw * spitch * sroll;
1534 return out;
1537 D3DXQUATERNION * WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *out, const D3DXQUATERNION *q1,
1538 const D3DXQUATERNION *q2, FLOAT t)
1540 FLOAT dot, temp;
1542 TRACE("out %p, q1 %p, q2 %p, t %f\n", out, q1, q2, t);
1544 temp = 1.0f - t;
1545 dot = D3DXQuaternionDot(q1, q2);
1546 if (dot < 0.0f)
1548 t = -t;
1549 dot = -dot;
1552 if (1.0f - dot > 0.001f)
1554 FLOAT theta = acosf(dot);
1556 temp = sinf(theta * temp) / sinf(theta);
1557 t = sinf(theta * t) / sinf(theta);
1560 out->x = temp * q1->x + t * q2->x;
1561 out->y = temp * q1->y + t * q2->y;
1562 out->z = temp * q1->z + t * q2->z;
1563 out->w = temp * q1->w + t * q2->w;
1565 return out;
1568 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3, const D3DXQUATERNION *pq4, FLOAT t)
1570 D3DXQUATERNION temp1, temp2;
1572 TRACE("pout %p, pq1 %p, pq2 %p, pq3 %p, pq4 %p, t %f\n", pout, pq1, pq2, pq3, pq4, t);
1574 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1575 return pout;
1578 static D3DXQUATERNION add_diff(const D3DXQUATERNION *q1, const D3DXQUATERNION *q2, const FLOAT add)
1580 D3DXQUATERNION temp;
1582 temp.x = q1->x + add * q2->x;
1583 temp.y = q1->y + add * q2->y;
1584 temp.z = q1->z + add * q2->z;
1585 temp.w = q1->w + add * q2->w;
1587 return temp;
1590 void WINAPI D3DXQuaternionSquadSetup(D3DXQUATERNION *paout, D3DXQUATERNION *pbout, D3DXQUATERNION *pcout, const D3DXQUATERNION *pq0, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3)
1592 D3DXQUATERNION q, temp1, temp2, temp3, zero;
1594 TRACE("paout %p, pbout %p, pcout %p, pq0 %p, pq1 %p, pq2 %p, pq3 %p\n", paout, pbout, pcout, pq0, pq1, pq2, pq3);
1596 zero.x = 0.0f;
1597 zero.y = 0.0f;
1598 zero.z = 0.0f;
1599 zero.w = 0.0f;
1601 if ( D3DXQuaternionDot(pq0, pq1) < 0.0f )
1602 temp2 = add_diff(&zero, pq0, -1.0f);
1603 else
1604 temp2 = *pq0;
1606 if ( D3DXQuaternionDot(pq1, pq2) < 0.0f )
1607 *pcout = add_diff(&zero, pq2, -1.0f);
1608 else
1609 *pcout = *pq2;
1611 if ( D3DXQuaternionDot(pcout, pq3) < 0.0f )
1612 temp3 = add_diff(&zero, pq3, -1.0f);
1613 else
1614 temp3 = *pq3;
1616 D3DXQuaternionInverse(&temp1, pq1);
1617 D3DXQuaternionMultiply(&temp2, &temp1, &temp2);
1618 D3DXQuaternionLn(&temp2, &temp2);
1619 D3DXQuaternionMultiply(&q, &temp1, pcout);
1620 D3DXQuaternionLn(&q, &q);
1621 temp1 = add_diff(&temp2, &q, 1.0f);
1622 temp1.x *= -0.25f;
1623 temp1.y *= -0.25f;
1624 temp1.z *= -0.25f;
1625 temp1.w *= -0.25f;
1626 D3DXQuaternionExp(&temp1, &temp1);
1627 D3DXQuaternionMultiply(paout, pq1, &temp1);
1629 D3DXQuaternionInverse(&temp1, pcout);
1630 D3DXQuaternionMultiply(&temp2, &temp1, pq1);
1631 D3DXQuaternionLn(&temp2, &temp2);
1632 D3DXQuaternionMultiply(&q, &temp1, &temp3);
1633 D3DXQuaternionLn(&q, &q);
1634 temp1 = add_diff(&temp2, &q, 1.0f);
1635 temp1.x *= -0.25f;
1636 temp1.y *= -0.25f;
1637 temp1.z *= -0.25f;
1638 temp1.w *= -0.25f;
1639 D3DXQuaternionExp(&temp1, &temp1);
1640 D3DXQuaternionMultiply(pbout, pcout, &temp1);
1642 return;
1645 void WINAPI D3DXQuaternionToAxisAngle(const D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1647 TRACE("pq %p, paxis %p, pangle %p\n", pq, paxis, pangle);
1649 if (paxis)
1651 paxis->x = pq->x;
1652 paxis->y = pq->y;
1653 paxis->z = pq->z;
1655 if (pangle)
1656 *pangle = 2.0f * acosf(pq->w);
1659 /*_________________D3DXVec2_____________________*/
1661 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1663 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
1665 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1666 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1667 return pout;
1670 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv0, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pv3, FLOAT s)
1672 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
1674 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);
1675 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);
1676 return pout;
1679 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pt1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pt2, FLOAT s)
1681 FLOAT h1, h2, h3, h4;
1683 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
1685 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1686 h2 = s * s * s - 2.0f * s * s + s;
1687 h3 = -2.0f * s * s * s + 3.0f * s * s;
1688 h4 = s * s * s - s * s;
1690 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1691 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1692 return pout;
1695 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv)
1697 FLOAT norm;
1699 TRACE("pout %p, pv %p\n", pout, pv);
1701 norm = D3DXVec2Length(pv);
1702 if ( !norm )
1704 pout->x = 0.0f;
1705 pout->y = 0.0f;
1707 else
1709 pout->x = pv->x / norm;
1710 pout->y = pv->y / norm;
1713 return pout;
1716 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1718 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1720 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
1721 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
1722 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
1723 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1724 return pout;
1727 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR2* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1729 UINT i;
1731 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1733 for (i = 0; i < elements; ++i) {
1734 D3DXVec2Transform(
1735 (D3DXVECTOR4*)((char*)out + outstride * i),
1736 (const D3DXVECTOR2*)((const char*)in + instride * i),
1737 matrix);
1739 return out;
1742 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1744 D3DXVECTOR2 v;
1745 FLOAT norm;
1747 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1749 v = *pv;
1750 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1752 pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1753 pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1755 return pout;
1758 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, const D3DXVECTOR2* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1760 UINT i;
1762 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1764 for (i = 0; i < elements; ++i) {
1765 D3DXVec2TransformCoord(
1766 (D3DXVECTOR2*)((char*)out + outstride * i),
1767 (const D3DXVECTOR2*)((const char*)in + instride * i),
1768 matrix);
1770 return out;
1773 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1775 const D3DXVECTOR2 v = *pv;
1777 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1779 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1780 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1781 return pout;
1784 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, const D3DXVECTOR2 *in, UINT instride, const D3DXMATRIX *matrix, UINT elements)
1786 UINT i;
1788 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1790 for (i = 0; i < elements; ++i) {
1791 D3DXVec2TransformNormal(
1792 (D3DXVECTOR2*)((char*)out + outstride * i),
1793 (const D3DXVECTOR2*)((const char*)in + instride * i),
1794 matrix);
1796 return out;
1799 /*_________________D3DXVec3_____________________*/
1801 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1803 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
1805 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1806 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1807 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1808 return pout;
1811 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv0, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3, FLOAT s)
1813 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
1815 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);
1816 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);
1817 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);
1818 return pout;
1821 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pt1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pt2, FLOAT s)
1823 FLOAT h1, h2, h3, h4;
1825 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
1827 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1828 h2 = s * s * s - 2.0f * s * s + s;
1829 h3 = -2.0f * s * s * s + 3.0f * s * s;
1830 h4 = s * s * s - s * s;
1832 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1833 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1834 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1835 return pout;
1838 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv)
1840 FLOAT norm;
1842 TRACE("pout %p, pv %p\n", pout, pv);
1844 norm = D3DXVec3Length(pv);
1845 if ( !norm )
1847 pout->x = 0.0f;
1848 pout->y = 0.0f;
1849 pout->z = 0.0f;
1851 else
1853 pout->x = pv->x / norm;
1854 pout->y = pv->y / norm;
1855 pout->z = pv->z / norm;
1858 return pout;
1861 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DVIEWPORT9 *pviewport, const D3DXMATRIX *pprojection, const D3DXMATRIX *pview, const D3DXMATRIX *pworld)
1863 D3DXMATRIX m;
1865 TRACE("pout %p, pv %p, pviewport %p, pprojection %p, pview %p, pworld %p\n", pout, pv, pviewport, pprojection, pview, pworld);
1867 D3DXMatrixIdentity(&m);
1868 if (pworld) D3DXMatrixMultiply(&m, &m, pworld);
1869 if (pview) D3DXMatrixMultiply(&m, &m, pview);
1870 if (pprojection) D3DXMatrixMultiply(&m, &m, pprojection);
1872 D3DXVec3TransformCoord(pout, pv, &m);
1874 if (pviewport)
1876 pout->x = pviewport->X + ( 1.0f + pout->x ) * pviewport->Width / 2.0f;
1877 pout->y = pviewport->Y + ( 1.0f - pout->y ) * pviewport->Height / 2.0f;
1878 pout->z = pviewport->MinZ + pout->z * ( pviewport->MaxZ - pviewport->MinZ );
1880 return pout;
1883 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)
1885 UINT i;
1887 TRACE("out %p, outstride %u, in %p, instride %u, viewport %p, projection %p, view %p, world %p, elements %u\n",
1888 out, outstride, in, instride, viewport, projection, view, world, elements);
1890 for (i = 0; i < elements; ++i) {
1891 D3DXVec3Project(
1892 (D3DXVECTOR3*)((char*)out + outstride * i),
1893 (const D3DXVECTOR3*)((const char*)in + instride * i),
1894 viewport, projection, view, world);
1896 return out;
1899 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1901 D3DXVECTOR4 out;
1903 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1905 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];
1906 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];
1907 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];
1908 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];
1909 *pout = out;
1910 return pout;
1913 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1915 UINT i;
1917 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1919 for (i = 0; i < elements; ++i) {
1920 D3DXVec3Transform(
1921 (D3DXVECTOR4*)((char*)out + outstride * i),
1922 (const D3DXVECTOR3*)((const char*)in + instride * i),
1923 matrix);
1925 return out;
1928 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1930 D3DXVECTOR3 out;
1931 FLOAT norm;
1933 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1935 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];
1937 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;
1938 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;
1939 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;
1941 *pout = out;
1943 return pout;
1946 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(D3DXVECTOR3* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1948 UINT i;
1950 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1952 for (i = 0; i < elements; ++i) {
1953 D3DXVec3TransformCoord(
1954 (D3DXVECTOR3*)((char*)out + outstride * i),
1955 (const D3DXVECTOR3*)((const char*)in + instride * i),
1956 matrix);
1958 return out;
1961 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1963 const D3DXVECTOR3 v = *pv;
1965 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1967 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1968 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1969 pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1970 return pout;
1974 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1976 UINT i;
1978 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1980 for (i = 0; i < elements; ++i) {
1981 D3DXVec3TransformNormal(
1982 (D3DXVECTOR3*)((char*)out + outstride * i),
1983 (const D3DXVECTOR3*)((const char*)in + instride * i),
1984 matrix);
1986 return out;
1989 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DVIEWPORT9 *pviewport, const D3DXMATRIX *pprojection, const D3DXMATRIX *pview, const D3DXMATRIX *pworld)
1991 D3DXMATRIX m;
1993 TRACE("pout %p, pv %p, pviewport %p, pprojection %p, pview %p, pworlds %p\n", pout, pv, pviewport, pprojection, pview, pworld);
1995 D3DXMatrixIdentity(&m);
1996 if (pworld) D3DXMatrixMultiply(&m, &m, pworld);
1997 if (pview) D3DXMatrixMultiply(&m, &m, pview);
1998 if (pprojection) D3DXMatrixMultiply(&m, &m, pprojection);
1999 D3DXMatrixInverse(&m, NULL, &m);
2001 *pout = *pv;
2002 if (pviewport)
2004 pout->x = 2.0f * ( pout->x - pviewport->X ) / pviewport->Width - 1.0f;
2005 pout->y = 1.0f - 2.0f * ( pout->y - pviewport->Y ) / pviewport->Height;
2006 pout->z = ( pout->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
2008 D3DXVec3TransformCoord(pout, pout, &m);
2009 return pout;
2012 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)
2014 UINT i;
2016 TRACE("out %p, outstride %u, in %p, instride %u, viewport %p, projection %p, view %p, world %p, elements %u\n",
2017 out, outstride, in, instride, viewport, projection, view, world, elements);
2019 for (i = 0; i < elements; ++i) {
2020 D3DXVec3Unproject(
2021 (D3DXVECTOR3*)((char*)out + outstride * i),
2022 (const D3DXVECTOR3*)((const char*)in + instride * i),
2023 viewport, projection, view, world);
2025 return out;
2028 /*_________________D3DXVec4_____________________*/
2030 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
2032 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
2034 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
2035 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
2036 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
2037 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
2038 return pout;
2041 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv0, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3, FLOAT s)
2043 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
2045 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);
2046 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);
2047 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);
2048 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);
2049 return pout;
2052 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3)
2054 D3DXVECTOR4 out;
2056 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p\n", pout, pv1, pv2, pv3);
2058 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);
2059 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));
2060 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);
2061 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));
2062 *pout = out;
2063 return pout;
2066 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pt1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pt2, FLOAT s)
2068 FLOAT h1, h2, h3, h4;
2070 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
2072 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
2073 h2 = s * s * s - 2.0f * s * s + s;
2074 h3 = -2.0f * s * s * s + 3.0f * s * s;
2075 h4 = s * s * s - s * s;
2077 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
2078 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
2079 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
2080 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
2081 return pout;
2084 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv)
2086 FLOAT norm;
2088 TRACE("pout %p, pv %p\n", pout, pv);
2090 norm = D3DXVec4Length(pv);
2092 pout->x = pv->x / norm;
2093 pout->y = pv->y / norm;
2094 pout->z = pv->z / norm;
2095 pout->w = pv->w / norm;
2097 return pout;
2100 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv, const D3DXMATRIX *pm)
2102 D3DXVECTOR4 out;
2104 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
2106 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;
2107 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;
2108 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;
2109 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;
2110 *pout = out;
2111 return pout;
2114 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR4* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
2116 UINT i;
2118 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
2120 for (i = 0; i < elements; ++i) {
2121 D3DXVec4Transform(
2122 (D3DXVECTOR4*)((char*)out + outstride * i),
2123 (const D3DXVECTOR4*)((const char*)in + instride * i),
2124 matrix);
2126 return out;
2129 unsigned short float_32_to_16(const float in)
2131 int exp = 0, origexp;
2132 float tmp = fabsf(in);
2133 int sign = (copysignf(1, in) < 0);
2134 unsigned int mantissa;
2135 unsigned short ret;
2137 /* Deal with special numbers */
2138 if (isinf(in)) return (sign ? 0xffff : 0x7fff);
2139 if (isnan(in)) return (sign ? 0xffff : 0x7fff);
2140 if (in == 0.0f) return (sign ? 0x8000 : 0x0000);
2142 if (tmp < (float)(1u << 10))
2146 tmp *= 2.0f;
2147 exp--;
2148 } while (tmp < (float)(1u << 10));
2150 else if (tmp >= (float)(1u << 11))
2154 tmp /= 2.0f;
2155 exp++;
2156 } while (tmp >= (float)(1u << 11));
2159 exp += 10; /* Normalize the mantissa */
2160 exp += 15; /* Exponent is encoded with excess 15 */
2162 origexp = exp;
2164 mantissa = (unsigned int) tmp;
2165 if ((tmp - mantissa == 0.5f && mantissa % 2 == 1) || /* round half to even */
2166 (tmp - mantissa > 0.5f))
2168 mantissa++; /* round to nearest, away from zero */
2170 if (mantissa == 2048)
2172 mantissa = 1024;
2173 exp++;
2176 if (exp > 31)
2178 /* too big */
2179 ret = 0x7fff; /* INF */
2181 else if (exp <= 0)
2183 unsigned int rounding = 0;
2185 /* Denormalized half float */
2187 /* return 0x0000 (=0.0) for numbers too small to represent in half floats */
2188 if (exp < -11)
2189 return (sign ? 0x8000 : 0x0000);
2191 exp = origexp;
2193 /* the 13 extra bits from single precision are used for rounding */
2194 mantissa = (unsigned int)(tmp * (1u << 13));
2195 mantissa >>= 1 - exp; /* denormalize */
2197 mantissa -= ~(mantissa >> 13) & 1; /* round half to even */
2198 /* remove 13 least significant bits to get half float precision */
2199 mantissa >>= 12;
2200 rounding = mantissa & 1;
2201 mantissa >>= 1;
2203 ret = mantissa + rounding;
2205 else
2207 ret = (exp << 10) | (mantissa & 0x3ff);
2210 ret |= ((sign ? 1 : 0) << 15); /* Add the sign */
2211 return ret;
2214 D3DXFLOAT16 *WINAPI D3DXFloat32To16Array(D3DXFLOAT16 *pout, const FLOAT *pin, UINT n)
2216 unsigned int i;
2218 TRACE("pout %p, pin %p, n %u\n", pout, pin, n);
2220 for (i = 0; i < n; ++i)
2222 pout[i].value = float_32_to_16(pin[i]);
2225 return pout;
2228 /* Native d3dx9's D3DXFloat16to32Array lacks support for NaN and Inf. Specifically, e = 16 is treated as a
2229 * regular number - e.g., 0x7fff is converted to 131008.0 and 0xffff to -131008.0. */
2230 float float_16_to_32(const unsigned short in)
2232 const unsigned short s = (in & 0x8000);
2233 const unsigned short e = (in & 0x7C00) >> 10;
2234 const unsigned short m = in & 0x3FF;
2235 const float sgn = (s ? -1.0f : 1.0f);
2237 if (e == 0)
2239 if (m == 0) return sgn * 0.0f; /* +0.0 or -0.0 */
2240 else return sgn * powf(2, -14.0f) * (m / 1024.0f);
2242 else
2244 return sgn * powf(2, e - 15.0f) * (1.0f + (m / 1024.0f));
2248 FLOAT *WINAPI D3DXFloat16To32Array(FLOAT *pout, const D3DXFLOAT16 *pin, UINT n)
2250 unsigned int i;
2252 TRACE("pout %p, pin %p, n %u\n", pout, pin, n);
2254 for (i = 0; i < n; ++i)
2256 pout[i] = float_16_to_32(pin[i].value);
2259 return pout;
2262 /*_________________D3DXSH________________*/
2264 FLOAT* WINAPI D3DXSHAdd(FLOAT *out, UINT order, const FLOAT *a, const FLOAT *b)
2266 UINT i;
2268 TRACE("out %p, order %u, a %p, b %p\n", out, order, a, b);
2270 for (i = 0; i < order * order; i++)
2271 out[i] = a[i] + b[i];
2273 return out;
2276 FLOAT WINAPI D3DXSHDot(UINT order, const FLOAT *a, const FLOAT *b)
2278 FLOAT s;
2279 UINT i;
2281 TRACE("order %u, a %p, b %p\n", order, a, b);
2283 s = a[0] * b[0];
2284 for (i = 1; i < order * order; i++)
2285 s += a[i] * b[i];
2287 return s;
2290 static void weightedcapintegrale(FLOAT *out, UINT order, FLOAT angle)
2292 FLOAT coeff[3];
2294 coeff[0] = cosf(angle);
2296 out[0] = 2.0f * D3DX_PI * (1.0f - coeff[0]);
2297 out[1] = D3DX_PI * sinf(angle) * sinf(angle);
2298 if (order <= 2)
2299 return;
2301 out[2] = coeff[0] * out[1];
2302 if (order == 3)
2303 return;
2305 coeff[1] = coeff[0] * coeff[0];
2306 coeff[2] = coeff[1] * coeff[1];
2308 out[3] = D3DX_PI * (-1.25f * coeff[2] + 1.5f * coeff[1] - 0.25f);
2309 if (order == 4)
2310 return;
2312 out[4] = -0.25f * D3DX_PI * coeff[0] * (7.0f * coeff[2] - 10.0f * coeff[1] + 3.0f);
2313 if (order == 5)
2314 return;
2316 out[5] = D3DX_PI * (-2.625f * coeff[2] * coeff[1] + 4.375f * coeff[2] - 1.875f * coeff[1] + 0.125f);
2319 HRESULT WINAPI D3DXSHEvalConeLight(UINT order, const D3DXVECTOR3 *dir, FLOAT radius,
2320 FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *rout, FLOAT *gout, FLOAT *bout)
2322 FLOAT cap[6], clamped_angle, norm, scale, temp;
2323 UINT i, index, j;
2325 TRACE("order %u, dir %p, radius %f, red %f, green %f, blue %f, rout %p, gout %p, bout %p\n",
2326 order, dir, radius, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2328 if (radius <= 0.0f)
2329 return D3DXSHEvalDirectionalLight(order, dir, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2331 clamped_angle = (radius > D3DX_PI / 2.0f) ? (D3DX_PI / 2.0f) : radius;
2332 norm = sinf(clamped_angle) * sinf(clamped_angle);
2334 if (order > D3DXSH_MAXORDER)
2336 WARN("Order clamped at D3DXSH_MAXORDER\n");
2337 order = D3DXSH_MAXORDER;
2340 weightedcapintegrale(cap, order, radius);
2341 D3DXSHEvalDirection(rout, order, dir);
2343 for (i = 0; i < order; i++)
2345 scale = cap[i] / norm;
2347 for (j = 0; j < 2 * i + 1; j++)
2349 index = i * i + j;
2350 temp = rout[index] * scale;
2352 rout[index] = temp * Rintensity;
2353 if (gout)
2354 gout[index] = temp * Gintensity;
2355 if (bout)
2356 bout[index] = temp * Bintensity;
2360 return D3D_OK;
2363 FLOAT* WINAPI D3DXSHEvalDirection(FLOAT *out, UINT order, const D3DXVECTOR3 *dir)
2365 const FLOAT dirxx = dir->x * dir->x;
2366 const FLOAT dirxy = dir->x * dir->y;
2367 const FLOAT dirxz = dir->x * dir->z;
2368 const FLOAT diryy = dir->y * dir->y;
2369 const FLOAT diryz = dir->y * dir->z;
2370 const FLOAT dirzz = dir->z * dir->z;
2371 const FLOAT dirxxxx = dirxx * dirxx;
2372 const FLOAT diryyyy = diryy * diryy;
2373 const FLOAT dirzzzz = dirzz * dirzz;
2374 const FLOAT dirxyxy = dirxy * dirxy;
2376 TRACE("out %p, order %u, dir %p\n", out, order, dir);
2378 if ((order < D3DXSH_MINORDER) || (order > D3DXSH_MAXORDER))
2379 return out;
2381 out[0] = 0.5f / sqrtf(D3DX_PI);
2382 out[1] = -0.5f / sqrtf(D3DX_PI / 3.0f) * dir->y;
2383 out[2] = 0.5f / sqrtf(D3DX_PI / 3.0f) * dir->z;
2384 out[3] = -0.5f / sqrtf(D3DX_PI / 3.0f) * dir->x;
2385 if (order == 2)
2386 return out;
2388 out[4] = 0.5f / sqrtf(D3DX_PI / 15.0f) * dirxy;
2389 out[5] = -0.5f / sqrtf(D3DX_PI / 15.0f) * diryz;
2390 out[6] = 0.25f / sqrtf(D3DX_PI / 5.0f) * (3.0f * dirzz - 1.0f);
2391 out[7] = -0.5f / sqrtf(D3DX_PI / 15.0f) * dirxz;
2392 out[8] = 0.25f / sqrtf(D3DX_PI / 15.0f) * (dirxx - diryy);
2393 if (order == 3)
2394 return out;
2396 out[9] = -sqrtf(70.0f / D3DX_PI) / 8.0f * dir->y * (3.0f * dirxx - diryy);
2397 out[10] = sqrtf(105.0f / D3DX_PI) / 2.0f * dirxy * dir->z;
2398 out[11] = -sqrtf(42.0f / D3DX_PI) / 8.0f * dir->y * (-1.0f + 5.0f * dirzz);
2399 out[12] = sqrtf(7.0f / D3DX_PI) / 4.0f * dir->z * (5.0f * dirzz - 3.0f);
2400 out[13] = sqrtf(42.0f / D3DX_PI) / 8.0f * dir->x * (1.0f - 5.0f * dirzz);
2401 out[14] = sqrtf(105.0f / D3DX_PI) / 4.0f * dir->z * (dirxx - diryy);
2402 out[15] = -sqrtf(70.0f / D3DX_PI) / 8.0f * dir->x * (dirxx - 3.0f * diryy);
2403 if (order == 4)
2404 return out;
2406 out[16] = 0.75f * sqrtf(35.0f / D3DX_PI) * dirxy * (dirxx - diryy);
2407 out[17] = 3.0f * dir->z * out[9];
2408 out[18] = 0.75f * sqrtf(5.0f / D3DX_PI) * dirxy * (7.0f * dirzz - 1.0f);
2409 out[19] = 0.375f * sqrtf(10.0f / D3DX_PI) * diryz * (3.0f - 7.0f * dirzz);
2410 out[20] = 3.0f / (16.0f * sqrtf(D3DX_PI)) * (35.0f * dirzzzz - 30.f * dirzz + 3.0f);
2411 out[21] = 0.375f * sqrtf(10.0f / D3DX_PI) * dirxz * (3.0f - 7.0f * dirzz);
2412 out[22] = 0.375f * sqrtf(5.0f / D3DX_PI) * (dirxx - diryy) * (7.0f * dirzz - 1.0f);
2413 out[23] = 3.0f * dir->z * out[15];
2414 out[24] = 3.0f / 16.0f * sqrtf(35.0f / D3DX_PI) * (dirxxxx - 6.0f * dirxyxy + diryyyy);
2415 if (order == 5)
2416 return out;
2418 out[25] = -3.0f/ 32.0f * sqrtf(154.0f / D3DX_PI) * dir->y * (5.0f * dirxxxx - 10.0f * dirxyxy + diryyyy);
2419 out[26] = 0.75f * sqrtf(385.0f / D3DX_PI) * dirxy * dir->z * (dirxx - diryy);
2420 out[27] = sqrtf(770.0f / D3DX_PI) / 32.0f * dir->y * (3.0f * dirxx - diryy) * (1.0f - 9.0f * dirzz);
2421 out[28] = sqrtf(1155.0f / D3DX_PI) / 4.0f * dirxy * dir->z * (3.0f * dirzz - 1.0f);
2422 out[29] = sqrtf(165.0f / D3DX_PI) / 16.0f * dir->y * (14.0f * dirzz - 21.0f * dirzzzz - 1.0f);
2423 out[30] = sqrtf(11.0f / D3DX_PI) / 16.0f * dir->z * (63.0f * dirzzzz - 70.0f * dirzz + 15.0f);
2424 out[31] = sqrtf(165.0f / D3DX_PI) / 16.0f * dir->x * (14.0f * dirzz - 21.0f * dirzzzz - 1.0f);
2425 out[32] = sqrtf(1155.0f / D3DX_PI) / 8.0f * dir->z * (dirxx - diryy) * (3.0f * dirzz - 1.0f);
2426 out[33] = sqrtf(770.0f / D3DX_PI) / 32.0f * dir->x * (dirxx - 3.0f * diryy) * (1.0f - 9.0f * dirzz);
2427 out[34] = 3.0f / 16.0f * sqrtf(385.0f / D3DX_PI) * dir->z * (dirxxxx - 6.0f * dirxyxy + diryyyy);
2428 out[35] = -3.0f/ 32.0f * sqrtf(154.0f / D3DX_PI) * dir->x * (dirxxxx - 10.0f * dirxyxy + 5.0f * diryyyy);
2430 return out;
2433 HRESULT WINAPI D3DXSHEvalDirectionalLight(UINT order, const D3DXVECTOR3 *dir, FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *Rout, FLOAT *Gout, FLOAT *Bout)
2435 FLOAT s, temp;
2436 UINT j;
2438 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);
2440 s = 0.75f;
2441 if ( order > 2 )
2442 s += 5.0f / 16.0f;
2443 if ( order > 4 )
2444 s -= 3.0f / 32.0f;
2445 s /= D3DX_PI;
2447 D3DXSHEvalDirection(Rout, order, dir);
2448 for (j = 0; j < order * order; j++)
2450 temp = Rout[j] / s;
2452 Rout[j] = Rintensity * temp;
2453 if ( Gout )
2454 Gout[j] = Gintensity * temp;
2455 if ( Bout )
2456 Bout[j] = Bintensity * temp;
2459 return D3D_OK;
2462 HRESULT WINAPI D3DXSHEvalHemisphereLight(UINT order, const D3DXVECTOR3 *dir, D3DXCOLOR top, D3DXCOLOR bottom,
2463 FLOAT *rout, FLOAT *gout, FLOAT *bout)
2465 FLOAT a[2], temp[4];
2466 UINT i, j;
2468 TRACE("order %u, dir %p, rout %p, gout %p, bout %p\n", order, dir, rout, gout, bout);
2470 D3DXSHEvalDirection(temp, 2, dir);
2472 a[0] = (top.r + bottom.r) * 3.0f * D3DX_PI;
2473 a[1] = (top.r - bottom.r) * D3DX_PI;
2474 for (i = 0; i < order; i++)
2475 for (j = 0; j < 2 * i + 1; j++)
2476 if (i < 2)
2477 rout[i * i + j] = temp[i * i + j] * a[i];
2478 else
2479 rout[i * i + j] = 0.0f;
2481 if (gout)
2483 a[0] = (top.g + bottom.g) * 3.0f * D3DX_PI;
2484 a[1] = (top.g - bottom.g) * D3DX_PI;
2485 for (i = 0; i < order; i++)
2486 for (j = 0; j < 2 * i + 1; j++)
2487 if (i < 2)
2488 gout[i * i + j] = temp[i * i + j] * a[i];
2489 else
2490 gout[i * i + j] = 0.0f;
2493 if (bout)
2495 a[0] = (top.b + bottom.b) * 3.0f * D3DX_PI;
2496 a[1] = (top.b - bottom.b) * D3DX_PI;
2497 for (i = 0; i < order; i++)
2498 for (j = 0; j < 2 * i + 1; j++)
2499 if (i < 2)
2500 bout[i * i + j] = temp[i * i + j] * a[i];
2501 else
2502 bout[i * i + j] = 0.0f;
2505 return D3D_OK;
2508 HRESULT WINAPI D3DXSHEvalSphericalLight(UINT order, const D3DXVECTOR3 *dir, FLOAT radius,
2509 FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *rout, FLOAT *gout, FLOAT *bout)
2511 D3DXVECTOR3 normal;
2512 FLOAT cap[6], clamped_angle, dist, temp;
2513 UINT i, index, j;
2515 TRACE("order %u, dir %p, radius %f, red %f, green %f, blue %f, rout %p, gout %p, bout %p\n",
2516 order, dir, radius, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2518 if (order > D3DXSH_MAXORDER)
2520 WARN("Order clamped at D3DXSH_MAXORDER\n");
2521 order = D3DXSH_MAXORDER;
2524 if (radius < 0.0f)
2525 radius = -radius;
2527 dist = D3DXVec3Length(dir);
2528 clamped_angle = (dist <= radius) ? D3DX_PI / 2.0f : asinf(radius / dist);
2530 weightedcapintegrale(cap, order, clamped_angle);
2531 D3DXVec3Normalize(&normal, dir);
2532 D3DXSHEvalDirection(rout, order, &normal);
2534 for (i = 0; i < order; i++)
2535 for (j = 0; j < 2 * i + 1; j++)
2537 index = i * i + j;
2538 temp = rout[index] * cap[i];
2540 rout[index] = temp * Rintensity;
2541 if (gout)
2542 gout[index] = temp * Gintensity;
2543 if (bout)
2544 bout[index] = temp * Bintensity;
2547 return D3D_OK;
2550 FLOAT * WINAPI D3DXSHMultiply2(FLOAT *out, const FLOAT *a, const FLOAT *b)
2552 FLOAT ta, tb;
2554 TRACE("out %p, a %p, b %p\n", out, a, b);
2556 ta = 0.28209479f * a[0];
2557 tb = 0.28209479f * b[0];
2559 out[0] = 0.28209479f * D3DXSHDot(2, a, b);
2560 out[1] = ta * b[1] + tb * a[1];
2561 out[2] = ta * b[2] + tb * a[2];
2562 out[3] = ta * b[3] + tb * a[3];
2564 return out;
2567 FLOAT * WINAPI D3DXSHMultiply3(FLOAT *out, const FLOAT *a, const FLOAT *b)
2569 FLOAT t, ta, tb;
2571 TRACE("out %p, a %p, b %p\n", out, a, b);
2573 out[0] = 0.28209479f * a[0] * b[0];
2575 ta = 0.28209479f * a[0] - 0.12615662f * a[6] - 0.21850968f * a[8];
2576 tb = 0.28209479f * b[0] - 0.12615662f * b[6] - 0.21850968f * b[8];
2577 out[1] = ta * b[1] + tb * a[1];
2578 t = a[1] * b[1];
2579 out[0] += 0.28209479f * t;
2580 out[6] = -0.12615662f * t;
2581 out[8] = -0.21850968f * t;
2583 ta = 0.21850968f * a[5];
2584 tb = 0.21850968f * b[5];
2585 out[1] += ta * b[2] + tb * a[2];
2586 out[2] = ta * b[1] + tb * a[1];
2587 t = a[1] * b[2] +a[2] * b[1];
2588 out[5] = 0.21850968f * t;
2590 ta = 0.21850968f * a[4];
2591 tb = 0.21850968f * b[4];
2592 out[1] += ta * b[3] + tb * a[3];
2593 out[3] = ta * b[1] + tb * a[1];
2594 t = a[1] * b[3] + a[3] * b[1];
2595 out[4] = 0.21850968f * t;
2597 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2598 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2599 out[2] += ta * b[2] + tb * a[2];
2600 t = a[2] * b[2];
2601 out[0] += 0.28209480f * t;
2602 out[6] += 0.25231326f * t;
2604 ta = 0.21850969f * a[7];
2605 tb = 0.21850969f * b[7];
2606 out[2] += ta * b[3] + tb * a[3];
2607 out[3] += ta * b[2] + tb * a[2];
2608 t = a[2] * b[3] + a[3] * b[2];
2609 out[7] = 0.21850969f * t;
2611 ta = 0.28209479f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2612 tb = 0.28209479f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2613 out[3] += ta * b[3] + tb * a[3];
2614 t = a[3] * b[3];
2615 out[0] += 0.28209479f * t;
2616 out[6] -= 0.12615663f * t;
2617 out[8] += 0.21850969f * t;
2619 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2620 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2621 out[4] += ta * b[4] + tb * a[4];
2622 t = a[4] * b[4];
2623 out[0] += 0.28209479f * t;
2624 out[6] -= 0.18022375f * t;
2626 ta = 0.15607835f * a[7];
2627 tb = 0.15607835f * b[7];
2628 out[4] += ta * b[5] + tb * a[5];
2629 out[5] += ta * b[4] + tb * a[4];
2630 t = a[4] * b[5] + a[5] * b[4];
2631 out[7] += 0.15607834f * t;
2633 ta = 0.28209479f * a[0] + 0.09011186f * a[6] - 0.15607835f * a[8];
2634 tb = 0.28209479f * b[0] + 0.09011186f * b[6] - 0.15607835f * b[8];
2635 out[5] += ta * b[5] + tb * a[5];
2636 t = a[5] * b[5];
2637 out[0] += 0.28209479f * t;
2638 out[6] += 0.09011186f * t;
2639 out[8] -= 0.15607835f * t;
2641 ta = 0.28209480f * a[0];
2642 tb = 0.28209480f * b[0];
2643 out[6] += ta * b[6] + tb * a[6];
2644 t = a[6] * b[6];
2645 out[0] += 0.28209480f * t;
2646 out[6] += 0.18022376f * t;
2648 ta = 0.28209479f * a[0] + 0.09011186f * a[6] + 0.15607835f * a[8];
2649 tb = 0.28209479f * b[0] + 0.09011186f * b[6] + 0.15607835f * b[8];
2650 out[7] += ta * b[7] + tb * a[7];
2651 t = a[7] * b[7];
2652 out[0] += 0.28209479f * t;
2653 out[6] += 0.09011186f * t;
2654 out[8] += 0.15607835f * t;
2656 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2657 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2658 out[8] += ta * b[8] + tb * a[8];
2659 t = a[8] * b[8];
2660 out[0] += 0.28209479f * t;
2661 out[6] -= 0.18022375f * t;
2663 return out;
2666 FLOAT * WINAPI D3DXSHMultiply4(FLOAT *out, const FLOAT *a, const FLOAT *b)
2668 FLOAT ta, tb, t;
2670 TRACE("out %p, a %p, b %p\n", out, a, b);
2672 out[0] = 0.28209479f * a[0] * b[0];
2674 ta = 0.28209479f * a[0] - 0.12615663f * a[6] - 0.21850969f * a[8];
2675 tb = 0.28209479f * b[0] - 0.12615663f * b[6] - 0.21850969f * b[8];
2676 out[1] = ta * b[1] + tb * a[1];
2677 t = a[1] * b[1];
2678 out[0] += 0.28209479f * t;
2679 out[6] = -0.12615663f * t;
2680 out[8] = -0.21850969f * t;
2682 ta = 0.21850969f * a[3] - 0.05839917f * a[13] - 0.22617901f * a[15];
2683 tb = 0.21850969f * b[3] - 0.05839917f * b[13] - 0.22617901f * b[15];
2684 out[1] += ta * b[4] + tb * a[4];
2685 out[4] = ta * b[1] + tb * a[1];
2686 t = a[1] * b[4] + a[4] * b[1];
2687 out[3] = 0.21850969f * t;
2688 out[13] = -0.05839917f * t;
2689 out[15] = -0.22617901f * t;
2691 ta = 0.21850969f * a[2] - 0.14304817f * a[12] - 0.18467439f * a[14];
2692 tb = 0.21850969f * b[2] - 0.14304817f * b[12] - 0.18467439f * b[14];
2693 out[1] += ta * b[5] + tb * a[5];
2694 out[5] = ta * b[1] + tb * a[1];
2695 t = a[1] * b[5] + a[5] * b[1];
2696 out[2] = 0.21850969f * t;
2697 out[12] = -0.14304817f * t;
2698 out[14] = -0.18467439f * t;
2700 ta = 0.20230066f * a[11];
2701 tb = 0.20230066f * b[11];
2702 out[1] += ta * b[6] + tb * a[6];
2703 out[6] += ta * b[1] + tb * a[1];
2704 t = a[1] * b[6] + a[6] * b[1];
2705 out[11] = 0.20230066f * t;
2707 ta = 0.22617901f * a[9] + 0.05839917f * a[11];
2708 tb = 0.22617901f * b[9] + 0.05839917f * b[11];
2709 out[1] += ta * b[8] + tb * a[8];
2710 out[8] += ta * b[1] + tb * a[1];
2711 t = a[1] * b[8] + a[8] * b[1];
2712 out[9] = 0.22617901f * t;
2713 out[11] += 0.05839917f * t;
2715 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2716 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2717 out[2] += ta * b[2] + tb * a[2];
2718 t = a[2] * b[2];
2719 out[0] += 0.28209480f * t;
2720 out[6] += 0.25231326f * t;
2722 ta = 0.24776671f * a[12];
2723 tb = 0.24776671f * b[12];
2724 out[2] += ta * b[6] + tb * a[6];
2725 out[6] += ta * b[2] + tb * a[2];
2726 t = a[2] * b[6] + a[6] * b[2];
2727 out[12] += 0.24776671f * t;
2729 ta = 0.28209480f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2730 tb = 0.28209480f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2731 out[3] += ta * b[3] + tb * a[3];
2732 t = a[3] * b[3];
2733 out[0] += 0.28209480f * t;
2734 out[6] -= 0.12615663f * t;
2735 out[8] += 0.21850969f * t;
2737 ta = 0.20230066f * a[13];
2738 tb = 0.20230066f * b[13];
2739 out[3] += ta * b[6] + tb * a[6];
2740 out[6] += ta * b[3] + tb * a[3];
2741 t = a[3] * b[6] + a[6] * b[3];
2742 out[13] += 0.20230066f * t;
2744 ta = 0.21850969f * a[2] - 0.14304817f * a[12] + 0.18467439f * a[14];
2745 tb = 0.21850969f * b[2] - 0.14304817f * b[12] + 0.18467439f * b[14];
2746 out[3] += ta * b[7] + tb * a[7];
2747 out[7] = ta * b[3] + tb * a[3];
2748 t = a[3] * b[7] + a[7] * b[3];
2749 out[2] += 0.21850969f * t;
2750 out[12] -= 0.14304817f * t;
2751 out[14] += 0.18467439f * t;
2753 ta = -0.05839917f * a[13] + 0.22617901f * a[15];
2754 tb = -0.05839917f * b[13] + 0.22617901f * b[15];
2755 out[3] += ta * b[8] + tb * a[8];
2756 out[8] += ta * b[3] + tb * a[3];
2757 t = a[3] * b[8] + a[8] * b[3];
2758 out[13] -= 0.05839917f * t;
2759 out[15] += 0.22617901f * t;
2761 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2762 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2763 out[4] += ta * b[4] + tb * a[4];
2764 t = a[4] * b[4];
2765 out[0] += 0.28209479f * t;
2766 out[6] -= 0.18022375f * t;
2768 ta = 0.15607835f * a[7];
2769 tb = 0.15607835f * b[7];
2770 out[4] += ta * b[5] + tb * a[5];
2771 out[5] += ta * b[4] + tb * a[4];
2772 t = a[4] * b[5] + a[5] * b[4];
2773 out[7] += 0.15607835f * t;
2775 ta = 0.22617901f * a[3] - 0.09403160f * a[13];
2776 tb = 0.22617901f * b[3] - 0.09403160f * b[13];
2777 out[4] += ta * b[9] + tb * a[9];
2778 out[9] += ta * b[4] + tb * a[4];
2779 t = a[4] * b[9] + a[9] * b[4];
2780 out[3] += 0.22617901f * t;
2781 out[13] -= 0.09403160f * t;
2783 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2784 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2785 out[4] += ta * b[10] + tb * a [10];
2786 out[10] = ta * b[4] + tb * a[4];
2787 t = a[4] * b[10] + a[10] * b[4];
2788 out[2] += 0.18467439f * t;
2789 out[12] -= 0.18806319f * t;
2791 ta = -0.05839917f * a[3] + 0.14567312f * a[13] + 0.09403160f * a[15];
2792 tb = -0.05839917f * b[3] + 0.14567312f * b[13] + 0.09403160f * b[15];
2793 out[4] += ta * b[11] + tb * a[11];
2794 out[11] += ta * b[4] + tb * a[4];
2795 t = a[4] * b[11] + a[11] * b[4];
2796 out[3] -= 0.05839917f * t;
2797 out[13] += 0.14567312f * t;
2798 out[15] += 0.09403160f * t;
2800 ta = 0.28209479f * a[0] + 0.09011186f * a[6] - 0.15607835f * a[8];
2801 tb = 0.28209479f * b[0] + 0.09011186f * b[6] - 0.15607835f * b[8];
2802 out[5] += ta * b[5] + tb * a[5];
2803 t = a[5] * b[5];
2804 out[0] += 0.28209479f * t;
2805 out[6] += 0.09011186f * t;
2806 out[8] -= 0.15607835f * t;
2808 ta = 0.14867701f * a[14];
2809 tb = 0.14867701f * b[14];
2810 out[5] += ta * b[9] + tb * a[9];
2811 out[9] += ta * b[5] + tb * a[5];
2812 t = a[5] * b[9] + a[9] * b[5];
2813 out[14] += 0.14867701f * t;
2815 ta = 0.18467439f * a[3] + 0.11516472f * a[13] - 0.14867701f * a[15];
2816 tb = 0.18467439f * b[3] + 0.11516472f * b[13] - 0.14867701f * b[15];
2817 out[5] += ta * b[10] + tb * a[10];
2818 out[10] += ta * b[5] + tb * a[5];
2819 t = a[5] * b[10] + a[10] * b[5];
2820 out[3] += 0.18467439f * t;
2821 out[13] += 0.11516472f * t;
2822 out[15] -= 0.14867701f * t;
2824 ta = 0.23359668f * a[2] + 0.05947080f * a[12] - 0.11516472f * a[14];
2825 tb = 0.23359668f * b[2] + 0.05947080f * b[12] - 0.11516472f * b[14];
2826 out[5] += ta * b[11] + tb * a[11];
2827 out[11] += ta * b[5] + tb * a[5];
2828 t = a[5] * b[11] + a[11] * b[5];
2829 out[2] += 0.23359668f * t;
2830 out[12] += 0.05947080f * t;
2831 out[14] -= 0.11516472f * t;
2833 ta = 0.28209479f * a[0];
2834 tb = 0.28209479f * b[0];
2835 out[6] += ta * b[6] + tb * a[6];
2836 t = a[6] * b[6];
2837 out[0] += 0.28209479f * t;
2838 out[6] += 0.18022376f * t;
2840 ta = 0.09011186f * a[6] + 0.28209479f * a[0] + 0.15607835f * a[8];
2841 tb = 0.09011186f * b[6] + 0.28209479f * b[0] + 0.15607835f * b[8];
2842 out[7] += ta * b[7] + tb * a[7];
2843 t = a[7] * b[7];
2844 out[6] += 0.09011186f * t;
2845 out[0] += 0.28209479f * t;
2846 out[8] += 0.15607835f * t;
2848 ta = 0.14867701f * a[9] + 0.18467439f * a[1] + 0.11516472f * a[11];
2849 tb = 0.14867701f * b[9] + 0.18467439f * b[1] + 0.11516472f * b[11];
2850 out[7] += ta * b[10] + tb * a[10];
2851 out[10] += ta * b[7] + tb * a[7];
2852 t = a[7] * b[10] + a[10] * b[7];
2853 out[9] += 0.14867701f * t;
2854 out[1] += 0.18467439f * t;
2855 out[11] += 0.11516472f * t;
2857 ta = 0.05947080f * a[12] + 0.23359668f * a[2] + 0.11516472f * a[14];
2858 tb = 0.05947080f * b[12] + 0.23359668f * b[2] + 0.11516472f * b[14];
2859 out[7] += ta * b[13] + tb * a[13];
2860 out[13] += ta * b[7]+ tb * a[7];
2861 t = a[7] * b[13] + a[13] * b[7];
2862 out[12] += 0.05947080f * t;
2863 out[2] += 0.23359668f * t;
2864 out[14] += 0.11516472f * t;
2866 ta = 0.14867701f * a[15];
2867 tb = 0.14867701f * b[15];
2868 out[7] += ta * b[14] + tb * a[14];
2869 out[14] += ta * b[7] + tb * a[7];
2870 t = a[7] * b[14] + a[14] * b[7];
2871 out[15] += 0.14867701f * t;
2873 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2874 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2875 out[8] += ta * b[8] + tb * a[8];
2876 t = a[8] * b[8];
2877 out[0] += 0.28209479f * t;
2878 out[6] -= 0.18022375f * t;
2880 ta = -0.09403160f * a[11];
2881 tb = -0.09403160f * b[11];
2882 out[8] += ta * b[9] + tb * a[9];
2883 out[9] += ta * b[8] + tb * a[8];
2884 t = a[8] * b[9] + a[9] * b[8];
2885 out[11] -= 0.09403160f * t;
2887 ta = -0.09403160f * a[15];
2888 tb = -0.09403160f * b[15];
2889 out[8] += ta * b[13] + tb * a[13];
2890 out[13] += ta * b[8] + tb * a[8];
2891 t = a[8] * b[13] + a[13] * b[8];
2892 out[15] -= 0.09403160f * t;
2894 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2895 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2896 out[8] += ta * b[14] + tb * a[14];
2897 out[14] += ta * b[8] + tb * a[8];
2898 t = a[8] * b[14] + a[14] * b[8];
2899 out[2] += 0.18467439f * t;
2900 out[12] -= 0.18806319f * t;
2902 ta = -0.21026104f * a[6] + 0.28209479f * a[0];
2903 tb = -0.21026104f * b[6] + 0.28209479f * b[0];
2904 out[9] += ta * b[9] + tb * a[9];
2905 t = a[9] * b[9];
2906 out[6] -= 0.21026104f * t;
2907 out[0] += 0.28209479f * t;
2909 ta = 0.28209479f * a[0];
2910 tb = 0.28209479f * b[0];
2911 out[10] += ta * b[10] + tb * a[10];
2912 t = a[10] * b[10];
2913 out[0] += 0.28209479f * t;
2915 ta = 0.28209479f * a[0] + 0.12615663f * a[6] - 0.14567312f * a[8];
2916 tb = 0.28209479f * b[0] + 0.12615663f * b[6] - 0.14567312f * b[8];
2917 out[11] += ta * b[11] + tb * a[11];
2918 t = a[11] * b[11];
2919 out[0] += 0.28209479f * t;
2920 out[6] += 0.12615663f * t;
2921 out[8] -= 0.14567312f * t;
2923 ta = 0.28209479f * a[0] + 0.16820885f * a[6];
2924 tb = 0.28209479f * b[0] + 0.16820885f * b[6];
2925 out[12] += ta * b[12] + tb * a[12];
2926 t = a[12] * b[12];
2927 out[0] += 0.28209479f * t;
2928 out[6] += 0.16820885f * t;
2930 ta =0.28209479f * a[0] + 0.14567312f * a[8] + 0.12615663f * a[6];
2931 tb =0.28209479f * b[0] + 0.14567312f * b[8] + 0.12615663f * b[6];
2932 out[13] += ta * b[13] + tb * a[13];
2933 t = a[13] * b[13];
2934 out[0] += 0.28209479f * t;
2935 out[8] += 0.14567312f * t;
2936 out[6] += 0.12615663f * t;
2938 ta = 0.28209479f * a[0];
2939 tb = 0.28209479f * b[0];
2940 out[14] += ta * b[14] + tb * a[14];
2941 t = a[14] * b[14];
2942 out[0] += 0.28209479f * t;
2944 ta = 0.28209479f * a[0] - 0.21026104f * a[6];
2945 tb = 0.28209479f * b[0] - 0.21026104f * b[6];
2946 out[15] += ta * b[15] + tb * a[15];
2947 t = a[15] * b[15];
2948 out[0] += 0.28209479f * t;
2949 out[6] -= 0.21026104f * t;
2951 return out;
2954 static void rotate_X(FLOAT *out, UINT order, FLOAT a, FLOAT *in)
2956 out[0] = in[0];
2958 out[1] = a * in[2];
2959 out[2] = -a * in[1];
2960 out[3] = in[3];
2962 out[4] = a * in[7];
2963 out[5] = -in[5];
2964 out[6] = -0.5f * in[6] - 0.8660253882f * in[8];
2965 out[7] = -a * in[4];
2966 out[8] = -0.8660253882f * in[6] + 0.5f * in[8];
2967 out[9] = -a * 0.7905694842f * in[12] + a * 0.6123724580f * in[14];
2969 out[10] = -in[10];
2970 out[11] = -a * 0.6123724580f * in[12] - a * 0.7905694842f * in[14];
2971 out[12] = a * 0.7905694842f * in[9] + a * 0.6123724580f * in[11];
2972 out[13] = -0.25f * in[13] - 0.9682458639f * in[15];
2973 out[14] = -a * 0.6123724580f * in[9] + a * 0.7905694842f * in[11];
2974 out[15] = -0.9682458639f * in[13] + 0.25f * in[15];
2975 if (order == 4)
2976 return;
2978 out[16] = -a * 0.9354143739f * in[21] + a * 0.3535533845f * in[23];
2979 out[17] = -0.75f * in[17] + 0.6614378095f * in[19];
2980 out[18] = -a * 0.3535533845f * in[21] - a * 0.9354143739f * in[23];
2981 out[19] = 0.6614378095f * in[17] + 0.75f * in[19];
2982 out[20] = 0.375f * in[20] + 0.5590170026f * in[22] + 0.7395099998f * in[24];
2983 out[21] = a * 0.9354143739f * in[16] + a * 0.3535533845f * in[18];
2984 out[22] = 0.5590170026f * in[20] + 0.5f * in[22] - 0.6614378691f * in[24];
2985 out[23] = -a * 0.3535533845f * in[16] + a * 0.9354143739f * in[18];
2986 out[24] = 0.7395099998f * in[20] - 0.6614378691f * in[22] + 0.125f * in[24];
2987 if (order == 5)
2988 return;
2990 out[25] = a * 0.7015607357f * in[30] - a * 0.6846531630f * in[32] + a * 0.1976423711f * in[34];
2991 out[26] = -0.5f * in[26] + 0.8660253882f * in[28];
2992 out[27] = a * 0.5229125023f * in[30] + a * 0.3061861992f * in[32] - a * 0.7954951525f * in[34];
2993 out[28] = 0.8660253882f * in[26] + 0.5f * in[28];
2994 out[29] = a * 0.4841229022f * in[30] + a * 0.6614378691f * in[32] + a * 0.5728219748f * in[34];
2995 out[30] = -a * 0.7015607357f * in[25] - a * 0.5229125023f * in[27] - a * 0.4841229022f * in[29];
2996 out[31] = 0.125f * in[31] + 0.4050463140f * in[33] + 0.9057110548f * in[35];
2997 out[32] = a * 0.6846531630f * in[25] - a * 0.3061861992f * in[27] - a * 0.6614378691f * in[29];
2998 out[33] = 0.4050463140f * in[31] + 0.8125f * in[33] - 0.4192627370f * in[35];
2999 out[34] = -a * 0.1976423711f * in[25] + a * 0.7954951525f * in[27] - a * 0.5728219748f * in[29];
3000 out[35] = 0.9057110548f * in[31] - 0.4192627370f * in[33] + 0.0624999329f * in[35];
3003 FLOAT* WINAPI D3DXSHRotate(FLOAT *out, UINT order, const D3DXMATRIX *matrix, const FLOAT *in)
3005 FLOAT alpha, beta, gamma, sinb, temp[36], temp1[36];
3007 TRACE("out %p, order %u, matrix %p, in %p\n", out, order, matrix, in);
3009 out[0] = in[0];
3011 if ((order > D3DXSH_MAXORDER) || (order < D3DXSH_MINORDER))
3012 return out;
3014 if (order <= 3)
3016 out[1] = matrix->u.m[1][1] * in[1] - matrix->u.m[2][1] * in[2] + matrix->u.m[0][1] * in[3];
3017 out[2] = -matrix->u.m[1][2] * in[1] + matrix->u.m[2][2] * in[2] - matrix->u.m[0][2] * in[3];
3018 out[3] = matrix->u.m[1][0] * in[1] - matrix->u.m[2][0] * in[2] + matrix->u.m[0][0] * in[3];
3020 if (order == 3)
3022 FLOAT coeff[]={
3023 matrix->u.m[1][0] * matrix->u.m[0][0], matrix->u.m[1][1] * matrix->u.m[0][1],
3024 matrix->u.m[1][1] * matrix->u.m[2][1], matrix->u.m[1][0] * matrix->u.m[2][0],
3025 matrix->u.m[2][0] * matrix->u.m[2][0], matrix->u.m[2][1] * matrix->u.m[2][1],
3026 matrix->u.m[0][0] * matrix->u.m[2][0], matrix->u.m[0][1] * matrix->u.m[2][1],
3027 matrix->u.m[0][1] * matrix->u.m[0][1], matrix->u.m[1][0] * matrix->u.m[1][0],
3028 matrix->u.m[1][1] * matrix->u.m[1][1], matrix->u.m[0][0] * matrix->u.m[0][0], };
3030 out[4] = (matrix->u.m[1][1] * matrix->u.m[0][0] + matrix->u.m[0][1] * matrix->u.m[1][0]) * in[4];
3031 out[4] -= (matrix->u.m[1][0] * matrix->u.m[2][1] + matrix->u.m[1][1] * matrix->u.m[2][0]) * in[5];
3032 out[4] += 1.7320508076f * matrix->u.m[2][0] * matrix->u.m[2][1] * in[6];
3033 out[4] -= (matrix->u.m[0][1] * matrix->u.m[2][0] + matrix->u.m[0][0] * matrix->u.m[2][1]) * in[7];
3034 out[4] += (matrix->u.m[0][0] * matrix->u.m[0][1] - matrix->u.m[1][0] * matrix->u.m[1][1]) * in[8];
3036 out[5] = (matrix->u.m[1][1] * matrix->u.m[2][2] + matrix->u.m[1][2] * matrix->u.m[2][1]) * in[5];
3037 out[5] -= (matrix->u.m[1][1] * matrix->u.m[0][2] + matrix->u.m[1][2] * matrix->u.m[0][1]) * in[4];
3038 out[5] -= 1.7320508076f * matrix->u.m[2][2] * matrix->u.m[2][1] * in[6];
3039 out[5] += (matrix->u.m[0][2] * matrix->u.m[2][1] + matrix->u.m[0][1] * matrix->u.m[2][2]) * in[7];
3040 out[5] -= (matrix->u.m[0][1] * matrix->u.m[0][2] - matrix->u.m[1][1] * matrix->u.m[1][2]) * in[8];
3042 out[6] = (matrix->u.m[2][2] * matrix->u.m[2][2] - 0.5f * (coeff[4] + coeff[5])) * in[6];
3043 out[6] -= (0.5773502692f * (coeff[0] + coeff[1]) - 1.1547005384f * matrix->u.m[1][2] * matrix->u.m[0][2]) * in[4];
3044 out[6] += (0.5773502692f * (coeff[2] + coeff[3]) - 1.1547005384f * matrix->u.m[1][2] * matrix->u.m[2][2]) * in[5];
3045 out[6] += (0.5773502692f * (coeff[6] + coeff[7]) - 1.1547005384f * matrix->u.m[0][2] * matrix->u.m[2][2]) * in[7];
3046 out[6] += (0.2886751347f * (coeff[9] - coeff[8] + coeff[10] - coeff[11]) - 0.5773502692f *
3047 (matrix->u.m[1][2] * matrix->u.m[1][2] - matrix->u.m[0][2] * matrix->u.m[0][2])) * in[8];
3049 out[7] = (matrix->u.m[0][0] * matrix->u.m[2][2] + matrix->u.m[0][2] * matrix->u.m[2][0]) * in[7];
3050 out[7] -= (matrix->u.m[1][0] * matrix->u.m[0][2] + matrix->u.m[1][2] * matrix->u.m[0][0]) * in[4];
3051 out[7] += (matrix->u.m[1][0] * matrix->u.m[2][2] + matrix->u.m[1][2] * matrix->u.m[2][0]) * in[5];
3052 out[7] -= 1.7320508076f * matrix->u.m[2][2] * matrix->u.m[2][0] * in[6];
3053 out[7] -= (matrix->u.m[0][0] * matrix->u.m[0][2] - matrix->u.m[1][0] * matrix->u.m[1][2]) * in[8];
3055 out[8] = 0.5f * (coeff[11] - coeff[8] - coeff[9] + coeff[10]) * in[8];
3056 out[8] += (coeff[0] - coeff[1]) * in[4];
3057 out[8] += (coeff[2] - coeff[3]) * in[5];
3058 out[8] += 0.86602540f * (coeff[4] - coeff[5]) * in[6];
3059 out[8] += (coeff[7] - coeff[6]) * in[7];
3062 return out;
3065 if (fabsf(matrix->u.m[2][2]) != 1.0f)
3067 sinb = sqrtf(1.0f - matrix->u.m[2][2] * matrix->u.m[2][2]);
3068 alpha = atan2f(matrix->u.m[2][1] / sinb, matrix->u.m[2][0] / sinb);
3069 beta = atan2f(sinb, matrix->u.m[2][2]);
3070 gamma = atan2f(matrix->u.m[1][2] / sinb, -matrix->u.m[0][2] / sinb);
3072 else
3074 alpha = atan2f(matrix->u.m[0][1], matrix->u.m[0][0]);
3075 beta = 0.0f;
3076 gamma = 0.0f;
3079 D3DXSHRotateZ(temp, order, gamma, in);
3080 rotate_X(temp1, order, 1.0f, temp);
3081 D3DXSHRotateZ(temp, order, beta, temp1);
3082 rotate_X(temp1, order, -1.0f, temp);
3083 D3DXSHRotateZ(out, order, alpha, temp1);
3085 return out;
3088 FLOAT * WINAPI D3DXSHRotateZ(FLOAT *out, UINT order, FLOAT angle, const FLOAT *in)
3090 UINT i, sum = 0;
3091 FLOAT c[5], s[5];
3093 TRACE("out %p, order %u, angle %f, in %p\n", out, order, angle, in);
3095 order = min(max(order, D3DXSH_MINORDER), D3DXSH_MAXORDER);
3097 out[0] = in[0];
3099 for (i = 1; i < order; i++)
3101 UINT j;
3103 c[i - 1] = cosf(i * angle);
3104 s[i - 1] = sinf(i * angle);
3105 sum += i * 2;
3107 out[sum - i] = c[i - 1] * in[sum - i];
3108 out[sum - i] += s[i - 1] * in[sum + i];
3109 for (j = i - 1; j > 0; j--)
3111 out[sum - j] = 0.0f;
3112 out[sum - j] = c[j - 1] * in[sum - j];
3113 out[sum - j] += s[j - 1] * in[sum + j];
3116 if (in == out)
3117 out[sum] = 0.0f;
3118 else
3119 out[sum] = in[sum];
3121 for (j = 1; j < i; j++)
3123 out[sum + j] = 0.0f;
3124 out[sum + j] = -s[j - 1] * in[sum - j];
3125 out[sum + j] += c[j - 1] * in[sum + j];
3127 out[sum + i] = -s[i - 1] * in[sum - i];
3128 out[sum + i] += c[i - 1] * in[sum + i];
3131 return out;
3134 FLOAT* WINAPI D3DXSHScale(FLOAT *out, UINT order, const FLOAT *a, const FLOAT scale)
3136 UINT i;
3138 TRACE("out %p, order %u, a %p, scale %f\n", out, order, a, scale);
3140 for (i = 0; i < order * order; i++)
3141 out[i] = a[i] * scale;
3143 return out;