d3dx9: Use float functions in D3DXSHRotateZ().
[wine] / dlls / d3dx9_36 / math.c
1 /*
2  * Mathematical operations specific to D3DX9.
3  *
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
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #define NONAMELESSUNION
26
27 #include "config.h"
28 #include "wine/port.h"
29
30 #include "windef.h"
31 #include "wingdi.h"
32 #include "d3dx9_36_private.h"
33
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
37
38 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl;
39
40 struct ID3DXMatrixStackImpl
41 {
42   ID3DXMatrixStack ID3DXMatrixStack_iface;
43   LONG ref;
44
45   unsigned int current;
46   unsigned int stack_size;
47   D3DXMATRIX *stack;
48 };
49
50
51 /*_________________D3DXColor____________________*/
52
53 D3DXCOLOR* WINAPI D3DXColorAdjustContrast(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
54 {
55     TRACE("(%p, %p, %f)\n", pout, pc, s);
56
57     pout->r = 0.5f + s * (pc->r - 0.5f);
58     pout->g = 0.5f + s * (pc->g - 0.5f);
59     pout->b = 0.5f + s * (pc->b - 0.5f);
60     pout->a = pc->a;
61     return pout;
62 }
63
64 D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
65 {
66     FLOAT grey;
67
68     TRACE("(%p, %p, %f)\n", pout, pc, s);
69
70     grey = pc->r * 0.2125f + pc->g * 0.7154f + pc->b * 0.0721f;
71     pout->r = grey + s * (pc->r - grey);
72     pout->g = grey + s * (pc->g - grey);
73     pout->b = grey + s * (pc->b - grey);
74     pout->a = pc->a;
75     return pout;
76 }
77
78 /*_________________Misc__________________________*/
79
80 FLOAT WINAPI D3DXFresnelTerm(FLOAT costheta, FLOAT refractionindex)
81 {
82     FLOAT a, d, g, result;
83
84     TRACE("(%f, %f)\n", costheta, refractionindex);
85
86     g = sqrt(refractionindex * refractionindex + costheta * costheta - 1.0f);
87     a = g + costheta;
88     d = g - costheta;
89     result = ( costheta * a - 1.0f ) * ( costheta * a - 1.0f ) / ( ( costheta * d + 1.0f ) * ( costheta * d + 1.0f ) ) + 1.0f;
90     result = result * 0.5f * d * d / ( a * a );
91     return result;
92 }
93
94 /*_________________D3DXMatrix____________________*/
95
96 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR3 *rotationcenter, CONST D3DXQUATERNION *rotation, CONST D3DXVECTOR3 *translation)
97 {
98     D3DXMATRIX m1, m2, m3, m4, m5;
99
100     TRACE("(%p, %f, %p, %p, %p)\n", pout, scaling, rotationcenter, rotation, translation);
101
102     D3DXMatrixScaling(&m1, scaling, scaling, scaling);
103
104     if ( !rotationcenter )
105     {
106         D3DXMatrixIdentity(&m2);
107         D3DXMatrixIdentity(&m4);
108     }
109     else
110     {
111         D3DXMatrixTranslation(&m2, -rotationcenter->x, -rotationcenter->y, -rotationcenter->z);
112         D3DXMatrixTranslation(&m4, rotationcenter->x, rotationcenter->y, rotationcenter->z);
113     }
114
115     if ( !rotation ) D3DXMatrixIdentity(&m3);
116     else D3DXMatrixRotationQuaternion(&m3, rotation);
117
118     if ( !translation ) D3DXMatrixIdentity(&m5);
119     else D3DXMatrixTranslation(&m5, translation->x, translation->y, translation->z);
120
121     D3DXMatrixMultiply(&m1, &m1, &m2);
122     D3DXMatrixMultiply(&m1, &m1, &m3);
123     D3DXMatrixMultiply(&m1, &m1, &m4);
124     D3DXMatrixMultiply(pout, &m1, &m5);
125     return pout;
126 }
127
128 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation2D(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR2 *protationcenter, FLOAT rotation, CONST D3DXVECTOR2 *ptranslation)
129 {
130     D3DXMATRIX m1, m2, m3, m4, m5;
131     D3DXQUATERNION rot;
132     D3DXVECTOR3 rot_center, trans;
133
134     TRACE("(%p, %f, %p, %f, %p)\n", pout, scaling, protationcenter, rotation, ptranslation);
135
136     rot.w=cos(rotation/2.0f);
137     rot.x=0.0f;
138     rot.y=0.0f;
139     rot.z=sin(rotation/2.0f);
140
141     if ( protationcenter )
142     {
143         rot_center.x=protationcenter->x;
144         rot_center.y=protationcenter->y;
145         rot_center.z=0.0f;
146     }
147     else
148     {
149         rot_center.x=0.0f;
150         rot_center.y=0.0f;
151         rot_center.z=0.0f;
152     }
153
154     if ( ptranslation )
155     {
156         trans.x=ptranslation->x;
157         trans.y=ptranslation->y;
158         trans.z=0.0f;
159     }
160     else
161     {
162         trans.x=0.0f;
163         trans.y=0.0f;
164         trans.z=0.0f;
165     }
166
167     D3DXMatrixScaling(&m1, scaling, scaling, 1.0f);
168     D3DXMatrixTranslation(&m2, -rot_center.x, -rot_center.y, -rot_center.z);
169     D3DXMatrixTranslation(&m4, rot_center.x, rot_center.y, rot_center.z);
170     D3DXMatrixRotationQuaternion(&m3, &rot);
171     D3DXMatrixTranslation(&m5, trans.x, trans.y, trans.z);
172
173     D3DXMatrixMultiply(&m1, &m1, &m2);
174     D3DXMatrixMultiply(&m1, &m1, &m3);
175     D3DXMatrixMultiply(&m1, &m1, &m4);
176     D3DXMatrixMultiply(pout, &m1, &m5);
177
178     return pout;
179 }
180
181 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, CONST D3DXMATRIX *pm)
182 {
183     D3DXMATRIX normalized;
184     D3DXVECTOR3 vec;
185
186     TRACE("(%p, %p, %p, %p)\n", poutscale, poutrotation, pouttranslation, pm);
187
188     /*Compute the scaling part.*/
189     vec.x=pm->u.m[0][0];
190     vec.y=pm->u.m[0][1];
191     vec.z=pm->u.m[0][2];
192     poutscale->x=D3DXVec3Length(&vec);
193
194     vec.x=pm->u.m[1][0];
195     vec.y=pm->u.m[1][1];
196     vec.z=pm->u.m[1][2];
197     poutscale->y=D3DXVec3Length(&vec);
198
199     vec.x=pm->u.m[2][0];
200     vec.y=pm->u.m[2][1];
201     vec.z=pm->u.m[2][2];
202     poutscale->z=D3DXVec3Length(&vec);
203
204     /*Compute the translation part.*/
205     pouttranslation->x=pm->u.m[3][0];
206     pouttranslation->y=pm->u.m[3][1];
207     pouttranslation->z=pm->u.m[3][2];
208
209     /*Let's calculate the rotation now*/
210     if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) ) return D3DERR_INVALIDCALL;
211
212     normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
213     normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
214     normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
215     normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
216     normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
217     normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
218     normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
219     normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
220     normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
221
222     D3DXQuaternionRotationMatrix(poutrotation,&normalized);
223     return S_OK;
224 }
225
226 FLOAT WINAPI D3DXMatrixDeterminant(CONST D3DXMATRIX *pm)
227 {
228     D3DXVECTOR4 minor, v1, v2, v3;
229     FLOAT det;
230
231     TRACE("(%p)\n", pm);
232
233     v1.x = pm->u.m[0][0]; v1.y = pm->u.m[1][0]; v1.z = pm->u.m[2][0]; v1.w = pm->u.m[3][0];
234     v2.x = pm->u.m[0][1]; v2.y = pm->u.m[1][1]; v2.z = pm->u.m[2][1]; v2.w = pm->u.m[3][1];
235     v3.x = pm->u.m[0][2]; v3.y = pm->u.m[1][2]; v3.z = pm->u.m[2][2]; v3.w = pm->u.m[3][2];
236     D3DXVec4Cross(&minor, &v1, &v2, &v3);
237     det =  - (pm->u.m[0][3] * minor.x + pm->u.m[1][3] * minor.y + pm->u.m[2][3] * minor.z + pm->u.m[3][3] * minor.w);
238     return det;
239 }
240
241 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, CONST D3DXMATRIX *pm)
242 {
243     int a, i, j;
244     D3DXMATRIX out;
245     D3DXVECTOR4 v, vec[3];
246     FLOAT det;
247
248     TRACE("(%p, %p, %p)\n", pout, pdeterminant, pm);
249
250     det = D3DXMatrixDeterminant(pm);
251     if ( !det ) return NULL;
252     if ( pdeterminant ) *pdeterminant = det;
253     for (i=0; i<4; i++)
254     {
255         for (j=0; j<4; j++)
256         {
257             if (j != i )
258             {
259                 a = j;
260                 if ( j > i ) a = a-1;
261                 vec[a].x = pm->u.m[j][0];
262                 vec[a].y = pm->u.m[j][1];
263                 vec[a].z = pm->u.m[j][2];
264                 vec[a].w = pm->u.m[j][3];
265             }
266         }
267     D3DXVec4Cross(&v, &vec[0], &vec[1], &vec[2]);
268     out.u.m[0][i] = pow(-1.0f, i) * v.x / det;
269     out.u.m[1][i] = pow(-1.0f, i) * v.y / det;
270     out.u.m[2][i] = pow(-1.0f, i) * v.z / det;
271     out.u.m[3][i] = pow(-1.0f, i) * v.w / det;
272    }
273
274    *pout = out;
275    return pout;
276 }
277
278 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
279 {
280     D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
281
282     TRACE("(%p, %p, %p, %p)\n", pout, peye, pat, pup);
283
284     D3DXVec3Subtract(&vec2, pat, peye);
285     D3DXVec3Normalize(&vec, &vec2);
286     D3DXVec3Cross(&right, pup, &vec);
287     D3DXVec3Cross(&up, &vec, &right);
288     D3DXVec3Normalize(&rightn, &right);
289     D3DXVec3Normalize(&upn, &up);
290     pout->u.m[0][0] = rightn.x;
291     pout->u.m[1][0] = rightn.y;
292     pout->u.m[2][0] = rightn.z;
293     pout->u.m[3][0] = -D3DXVec3Dot(&rightn,peye);
294     pout->u.m[0][1] = upn.x;
295     pout->u.m[1][1] = upn.y;
296     pout->u.m[2][1] = upn.z;
297     pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
298     pout->u.m[0][2] = vec.x;
299     pout->u.m[1][2] = vec.y;
300     pout->u.m[2][2] = vec.z;
301     pout->u.m[3][2] = -D3DXVec3Dot(&vec, peye);
302     pout->u.m[0][3] = 0.0f;
303     pout->u.m[1][3] = 0.0f;
304     pout->u.m[2][3] = 0.0f;
305     pout->u.m[3][3] = 1.0f;
306     return pout;
307 }
308
309 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
310 {
311     D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
312
313     TRACE("(%p, %p, %p, %p)\n", pout, peye, pat, pup);
314
315     D3DXVec3Subtract(&vec2, pat, peye);
316     D3DXVec3Normalize(&vec, &vec2);
317     D3DXVec3Cross(&right, pup, &vec);
318     D3DXVec3Cross(&up, &vec, &right);
319     D3DXVec3Normalize(&rightn, &right);
320     D3DXVec3Normalize(&upn, &up);
321     pout->u.m[0][0] = -rightn.x;
322     pout->u.m[1][0] = -rightn.y;
323     pout->u.m[2][0] = -rightn.z;
324     pout->u.m[3][0] = D3DXVec3Dot(&rightn,peye);
325     pout->u.m[0][1] = upn.x;
326     pout->u.m[1][1] = upn.y;
327     pout->u.m[2][1] = upn.z;
328     pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
329     pout->u.m[0][2] = -vec.x;
330     pout->u.m[1][2] = -vec.y;
331     pout->u.m[2][2] = -vec.z;
332     pout->u.m[3][2] = D3DXVec3Dot(&vec, peye);
333     pout->u.m[0][3] = 0.0f;
334     pout->u.m[1][3] = 0.0f;
335     pout->u.m[2][3] = 0.0f;
336     pout->u.m[3][3] = 1.0f;
337     return pout;
338 }
339
340 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
341 {
342     D3DXMATRIX out;
343     int i,j;
344
345     TRACE("(%p, %p, %p)\n", pout, pm1, pm2);
346
347     for (i=0; i<4; i++)
348     {
349         for (j=0; j<4; j++)
350         {
351             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];
352         }
353     }
354
355     *pout = out;
356     return pout;
357 }
358
359 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
360 {
361     TRACE("%p, %p, %p)\n", pout, pm1, pm2);
362
363     D3DXMatrixMultiply(pout, pm1, pm2);
364     D3DXMatrixTranspose(pout, pout);
365     return pout;
366 }
367
368 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
369 {
370     TRACE("(%p, %f, %f, %f, %f)\n", pout, w, h, zn, zf);
371
372     D3DXMatrixIdentity(pout);
373     pout->u.m[0][0] = 2.0f / w;
374     pout->u.m[1][1] = 2.0f / h;
375     pout->u.m[2][2] = 1.0f / (zf - zn);
376     pout->u.m[3][2] = zn / (zn - zf);
377     return pout;
378 }
379
380 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
381 {
382     TRACE("(%p, %f, %f, %f, %f, %f, %f)\n", pout, l, r, b, t, zn, zf);
383
384     D3DXMatrixIdentity(pout);
385     pout->u.m[0][0] = 2.0f / (r - l);
386     pout->u.m[1][1] = 2.0f / (t - b);
387     pout->u.m[2][2] = 1.0f / (zf -zn);
388     pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
389     pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
390     pout->u.m[3][2] = zn / (zn -zf);
391     return pout;
392 }
393
394 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
395 {
396     TRACE("(%p, %f, %f, %f, %f, %f, %f)\n", pout, l, r, b, t, zn, zf);
397
398     D3DXMatrixIdentity(pout);
399     pout->u.m[0][0] = 2.0f / (r - l);
400     pout->u.m[1][1] = 2.0f / (t - b);
401     pout->u.m[2][2] = 1.0f / (zn -zf);
402     pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
403     pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
404     pout->u.m[3][2] = zn / (zn -zf);
405     return pout;
406 }
407
408 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
409 {
410     TRACE("(%p, %f, %f, %f, %f)\n", pout, w, h, zn, zf);
411
412     D3DXMatrixIdentity(pout);
413     pout->u.m[0][0] = 2.0f / w;
414     pout->u.m[1][1] = 2.0f / h;
415     pout->u.m[2][2] = 1.0f / (zn - zf);
416     pout->u.m[3][2] = zn / (zn - zf);
417     return pout;
418 }
419
420 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
421 {
422     TRACE("(%p, %f, %f, %f, %f)\n", pout, fovy, aspect, zn, zf);
423
424     D3DXMatrixIdentity(pout);
425     pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
426     pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
427     pout->u.m[2][2] = zf / (zf - zn);
428     pout->u.m[2][3] = 1.0f;
429     pout->u.m[3][2] = (zf * zn) / (zn - zf);
430     pout->u.m[3][3] = 0.0f;
431     return pout;
432 }
433
434 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
435 {
436     TRACE("(%p, %f, %f, %f, %f)\n", pout, fovy, aspect, zn, zf);
437
438     D3DXMatrixIdentity(pout);
439     pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
440     pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
441     pout->u.m[2][2] = zf / (zn - zf);
442     pout->u.m[2][3] = -1.0f;
443     pout->u.m[3][2] = (zf * zn) / (zn - zf);
444     pout->u.m[3][3] = 0.0f;
445     return pout;
446 }
447
448 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
449 {
450     TRACE("(%p, %f, %f, %f, %f)\n", pout, w, h, zn, zf);
451
452     D3DXMatrixIdentity(pout);
453     pout->u.m[0][0] = 2.0f * zn / w;
454     pout->u.m[1][1] = 2.0f * zn / h;
455     pout->u.m[2][2] = zf / (zf - zn);
456     pout->u.m[3][2] = (zn * zf) / (zn - zf);
457     pout->u.m[2][3] = 1.0f;
458     pout->u.m[3][3] = 0.0f;
459     return pout;
460 }
461
462 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
463 {
464     TRACE("(%p, %f, %f, %f, %f, %f, %f)\n", pout, l, r, b, t, zn, zf);
465
466     D3DXMatrixIdentity(pout);
467     pout->u.m[0][0] = 2.0f * zn / (r - l);
468     pout->u.m[1][1] = -2.0f * zn / (b - t);
469     pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
470     pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
471     pout->u.m[2][2] = - zf / (zn - zf);
472     pout->u.m[3][2] = (zn * zf) / (zn -zf);
473     pout->u.m[2][3] = 1.0f;
474     pout->u.m[3][3] = 0.0f;
475     return pout;
476 }
477
478 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
479 {
480     TRACE("(%p, %f, %f, %f, %f, %f, %f)\n", pout, l, r, b, t, zn, zf);
481
482     D3DXMatrixIdentity(pout);
483     pout->u.m[0][0] = 2.0f * zn / (r - l);
484     pout->u.m[1][1] = -2.0f * zn / (b - t);
485     pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
486     pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
487     pout->u.m[2][2] = zf / (zn - zf);
488     pout->u.m[3][2] = (zn * zf) / (zn -zf);
489     pout->u.m[2][3] = -1.0f;
490     pout->u.m[3][3] = 0.0f;
491     return pout;
492 }
493
494 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
495 {
496     TRACE("(%p, %f, %f, %f, %f)\n", pout, w, h, zn, zf);
497
498     D3DXMatrixIdentity(pout);
499     pout->u.m[0][0] = 2.0f * zn / w;
500     pout->u.m[1][1] = 2.0f * zn / h;
501     pout->u.m[2][2] = zf / (zn - zf);
502     pout->u.m[3][2] = (zn * zf) / (zn - zf);
503     pout->u.m[2][3] = -1.0f;
504     pout->u.m[3][3] = 0.0f;
505     return pout;
506 }
507
508 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, CONST D3DXPLANE *pplane)
509 {
510     D3DXPLANE Nplane;
511
512     TRACE("(%p, %p)\n", pout, pplane);
513
514     D3DXPlaneNormalize(&Nplane, pplane);
515     D3DXMatrixIdentity(pout);
516     pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
517     pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
518     pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
519     pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
520     pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
521     pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
522     pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
523     pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
524     pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
525     pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
526     pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
527     pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
528     return pout;
529 }
530
531 D3DXMATRIX* WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
532 {
533     D3DXVECTOR3 v;
534
535     TRACE("(%p, %p, %f)\n", pout, pv, angle);
536
537     D3DXVec3Normalize(&v,pv);
538     D3DXMatrixIdentity(pout);
539     pout->u.m[0][0] = (1.0f - cos(angle)) * v.x * v.x + cos(angle);
540     pout->u.m[1][0] = (1.0f - cos(angle)) * v.x * v.y - sin(angle) * v.z;
541     pout->u.m[2][0] = (1.0f - cos(angle)) * v.x * v.z + sin(angle) * v.y;
542     pout->u.m[0][1] = (1.0f - cos(angle)) * v.y * v.x + sin(angle) * v.z;
543     pout->u.m[1][1] = (1.0f - cos(angle)) * v.y * v.y + cos(angle);
544     pout->u.m[2][1] = (1.0f - cos(angle)) * v.y * v.z - sin(angle) * v.x;
545     pout->u.m[0][2] = (1.0f - cos(angle)) * v.z * v.x - sin(angle) * v.y;
546     pout->u.m[1][2] = (1.0f - cos(angle)) * v.z * v.y + sin(angle) * v.x;
547     pout->u.m[2][2] = (1.0f - cos(angle)) * v.z * v.z + cos(angle);
548     return pout;
549 }
550
551 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, CONST D3DXQUATERNION *pq)
552 {
553     TRACE("(%p, %p)\n", pout, pq);
554
555     D3DXMatrixIdentity(pout);
556     pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
557     pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
558     pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
559     pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
560     pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
561     pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
562     pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
563     pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
564     pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
565     return pout;
566 }
567
568 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
569 {
570     TRACE("(%p, %f)\n", pout, angle);
571
572     D3DXMatrixIdentity(pout);
573     pout->u.m[1][1] = cos(angle);
574     pout->u.m[2][2] = cos(angle);
575     pout->u.m[1][2] = sin(angle);
576     pout->u.m[2][1] = -sin(angle);
577     return pout;
578 }
579
580 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
581 {
582     TRACE("(%p, %f)\n", pout, angle);
583
584     D3DXMatrixIdentity(pout);
585     pout->u.m[0][0] = cos(angle);
586     pout->u.m[2][2] = cos(angle);
587     pout->u.m[0][2] = -sin(angle);
588     pout->u.m[2][0] = sin(angle);
589     return pout;
590 }
591
592 D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
593 {
594     D3DXMATRIX m;
595
596     TRACE("(%p, %f, %f, %f)\n", pout, yaw, pitch, roll);
597
598     D3DXMatrixIdentity(pout);
599     D3DXMatrixRotationZ(&m, roll);
600     D3DXMatrixMultiply(pout, pout, &m);
601     D3DXMatrixRotationX(&m, pitch);
602     D3DXMatrixMultiply(pout, pout, &m);
603     D3DXMatrixRotationY(&m, yaw);
604     D3DXMatrixMultiply(pout, pout, &m);
605     return pout;
606 }
607
608 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
609 {
610     TRACE("(%p, %f)\n", pout, angle);
611
612     D3DXMatrixIdentity(pout);
613     pout->u.m[0][0] = cos(angle);
614     pout->u.m[1][1] = cos(angle);
615     pout->u.m[0][1] = sin(angle);
616     pout->u.m[1][0] = -sin(angle);
617     return pout;
618 }
619
620 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
621 {
622     TRACE("(%p, %f, %f, %f)\n", pout, sx, sy, sz);
623
624     D3DXMatrixIdentity(pout);
625     pout->u.m[0][0] = sx;
626     pout->u.m[1][1] = sy;
627     pout->u.m[2][2] = sz;
628     return pout;
629 }
630
631 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, CONST D3DXVECTOR4 *plight, CONST D3DXPLANE *pplane)
632 {
633     D3DXPLANE Nplane;
634     FLOAT dot;
635
636     TRACE("(%p, %p, %p)\n", pout, plight, pplane);
637
638     D3DXPlaneNormalize(&Nplane, pplane);
639     dot = D3DXPlaneDot(&Nplane, plight);
640     pout->u.m[0][0] = dot - Nplane.a * plight->x;
641     pout->u.m[0][1] = -Nplane.a * plight->y;
642     pout->u.m[0][2] = -Nplane.a * plight->z;
643     pout->u.m[0][3] = -Nplane.a * plight->w;
644     pout->u.m[1][0] = -Nplane.b * plight->x;
645     pout->u.m[1][1] = dot - Nplane.b * plight->y;
646     pout->u.m[1][2] = -Nplane.b * plight->z;
647     pout->u.m[1][3] = -Nplane.b * plight->w;
648     pout->u.m[2][0] = -Nplane.c * plight->x;
649     pout->u.m[2][1] = -Nplane.c * plight->y;
650     pout->u.m[2][2] = dot - Nplane.c * plight->z;
651     pout->u.m[2][3] = -Nplane.c * plight->w;
652     pout->u.m[3][0] = -Nplane.d * plight->x;
653     pout->u.m[3][1] = -Nplane.d * plight->y;
654     pout->u.m[3][2] = -Nplane.d * plight->z;
655     pout->u.m[3][3] = dot - Nplane.d * plight->w;
656     return pout;
657 }
658
659 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pscalingcenter, CONST D3DXQUATERNION *pscalingrotation, CONST D3DXVECTOR3 *pscaling, CONST D3DXVECTOR3 *protationcenter, CONST D3DXQUATERNION *protation, CONST D3DXVECTOR3 *ptranslation)
660 {
661     D3DXMATRIX m1, m2, m3, m4, m5, m6, m7;
662     D3DXQUATERNION prc;
663     D3DXVECTOR3 psc, pt;
664
665     TRACE("(%p, %p, %p, %p, %p, %p, %p)\n", pout, pscalingcenter, pscalingrotation, pscaling, protationcenter, protation, ptranslation);
666
667     if ( !pscalingcenter )
668     {
669         psc.x = 0.0f;
670         psc.y = 0.0f;
671         psc.z = 0.0f;
672     }
673     else
674     {
675         psc.x = pscalingcenter->x;
676         psc.y = pscalingcenter->y;
677         psc.z = pscalingcenter->z;
678     }
679
680     if ( !protationcenter )
681     {
682         prc.x = 0.0f;
683         prc.y = 0.0f;
684         prc.z = 0.0f;
685     }
686     else
687     {
688         prc.x = protationcenter->x;
689         prc.y = protationcenter->y;
690         prc.z = protationcenter->z;
691     }
692
693     if ( !ptranslation )
694     {
695         pt.x = 0.0f;
696         pt.y = 0.0f;
697         pt.z = 0.0f;
698     }
699     else
700     {
701         pt.x = ptranslation->x;
702         pt.y = ptranslation->y;
703         pt.z = ptranslation->z;
704     }
705
706     D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
707
708     if ( !pscalingrotation )
709     {
710         D3DXMatrixIdentity(&m2);
711         D3DXMatrixIdentity(&m4);
712     }
713     else
714     {
715         D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
716         D3DXMatrixInverse(&m2, NULL, &m4);
717     }
718
719     if ( !pscaling ) D3DXMatrixIdentity(&m3);
720     else D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
721
722     if ( !protation ) D3DXMatrixIdentity(&m6);
723     else D3DXMatrixRotationQuaternion(&m6, protation);
724
725     D3DXMatrixTranslation(&m5, psc.x - prc.x,  psc.y - prc.y,  psc.z - prc.z);
726     D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
727     D3DXMatrixMultiply(&m1, &m1, &m2);
728     D3DXMatrixMultiply(&m1, &m1, &m3);
729     D3DXMatrixMultiply(&m1, &m1, &m4);
730     D3DXMatrixMultiply(&m1, &m1, &m5);
731     D3DXMatrixMultiply(&m1, &m1, &m6);
732     D3DXMatrixMultiply(pout, &m1, &m7);
733     return pout;
734 }
735
736 D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(D3DXMATRIX *pout, CONST D3DXVECTOR2 *pscalingcenter, FLOAT scalingrotation, CONST D3DXVECTOR2 *pscaling, CONST D3DXVECTOR2 *protationcenter, FLOAT rotation, CONST D3DXVECTOR2 *ptranslation)
737 {
738     D3DXQUATERNION rot, sca_rot;
739     D3DXVECTOR3 rot_center, sca, sca_center, trans;
740
741     TRACE("(%p, %p, %f, %p, %p, %f, %p)\n", pout, pscalingcenter, scalingrotation, pscaling, protationcenter, rotation, ptranslation);
742
743     if ( pscalingcenter )
744     {
745         sca_center.x=pscalingcenter->x;
746         sca_center.y=pscalingcenter->y;
747         sca_center.z=0.0f;
748     }
749     else
750     {
751         sca_center.x=0.0f;
752         sca_center.y=0.0f;
753         sca_center.z=0.0f;
754     }
755
756     if ( pscaling )
757     {
758         sca.x=pscaling->x;
759         sca.y=pscaling->y;
760         sca.z=1.0f;
761     }
762     else
763     {
764         sca.x=1.0f;
765         sca.y=1.0f;
766         sca.z=1.0f;
767     }
768
769     if ( protationcenter )
770     {
771         rot_center.x=protationcenter->x;
772         rot_center.y=protationcenter->y;
773         rot_center.z=0.0f;
774     }
775     else
776     {
777         rot_center.x=0.0f;
778         rot_center.y=0.0f;
779         rot_center.z=0.0f;
780     }
781
782     if ( ptranslation )
783     {
784         trans.x=ptranslation->x;
785         trans.y=ptranslation->y;
786         trans.z=0.0f;
787     }
788     else
789     {
790         trans.x=0.0f;
791         trans.y=0.0f;
792         trans.z=0.0f;
793     }
794
795     rot.w=cos(rotation/2.0f);
796     rot.x=0.0f;
797     rot.y=0.0f;
798     rot.z=sin(rotation/2.0f);
799
800     sca_rot.w=cos(scalingrotation/2.0f);
801     sca_rot.x=0.0f;
802     sca_rot.y=0.0f;
803     sca_rot.z=sin(scalingrotation/2.0f);
804
805     D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
806
807     return pout;
808 }
809
810 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
811 {
812     TRACE("(%p, %f, %f, %f)\n", pout, x, y, z);
813
814     D3DXMatrixIdentity(pout);
815     pout->u.m[3][0] = x;
816     pout->u.m[3][1] = y;
817     pout->u.m[3][2] = z;
818     return pout;
819 }
820
821 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
822 {
823     CONST D3DXMATRIX m = *pm;
824     int i,j;
825
826     TRACE("(%p, %p)\n", pout, pm);
827
828     for (i=0; i<4; i++)
829         for (j=0; j<4; j++) pout->u.m[i][j] = m.u.m[j][i];
830
831     return pout;
832 }
833
834 /*_________________D3DXMatrixStack____________________*/
835
836 static const unsigned int INITIAL_STACK_SIZE = 32;
837
838 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, LPD3DXMATRIXSTACK *ppstack)
839 {
840     struct ID3DXMatrixStackImpl *object;
841
842     TRACE("flags %#x, ppstack %p\n", flags, ppstack);
843
844     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
845     if (object == NULL)
846     {
847         *ppstack = NULL;
848         return E_OUTOFMEMORY;
849     }
850     object->ID3DXMatrixStack_iface.lpVtbl = &ID3DXMatrixStack_Vtbl;
851     object->ref = 1;
852
853     object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(*object->stack));
854     if (!object->stack)
855     {
856         HeapFree(GetProcessHeap(), 0, object);
857         *ppstack = NULL;
858         return E_OUTOFMEMORY;
859     }
860
861     object->current = 0;
862     object->stack_size = INITIAL_STACK_SIZE;
863     D3DXMatrixIdentity(&object->stack[0]);
864
865     TRACE("Created matrix stack %p\n", object);
866
867     *ppstack = &object->ID3DXMatrixStack_iface;
868     return D3D_OK;
869 }
870
871 static inline struct ID3DXMatrixStackImpl *impl_from_ID3DXMatrixStack(ID3DXMatrixStack *iface)
872 {
873   return CONTAINING_RECORD(iface, struct ID3DXMatrixStackImpl, ID3DXMatrixStack_iface);
874 }
875
876 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **out)
877 {
878     TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
879
880     if (IsEqualGUID(riid, &IID_ID3DXMatrixStack)
881             || IsEqualGUID(riid, &IID_IUnknown))
882     {
883         ID3DXMatrixStack_AddRef(iface);
884         *out = iface;
885         return S_OK;
886     }
887
888     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
889
890     *out = NULL;
891     return E_NOINTERFACE;
892 }
893
894 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
895 {
896     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
897     ULONG ref = InterlockedIncrement(&This->ref);
898     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
899     return ref;
900 }
901
902 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack *iface)
903 {
904     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
905     ULONG ref = InterlockedDecrement(&This->ref);
906     if (!ref)
907     {
908         HeapFree(GetProcessHeap(), 0, This->stack);
909         HeapFree(GetProcessHeap(), 0, This);
910     }
911     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
912     return ref;
913 }
914
915 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
916 {
917     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
918
919     TRACE("iface %p\n", iface);
920
921     return &This->stack[This->current];
922 }
923
924 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
925 {
926     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
927
928     TRACE("iface %p\n", iface);
929
930     D3DXMatrixIdentity(&This->stack[This->current]);
931
932     return D3D_OK;
933 }
934
935 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
936 {
937     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
938
939     TRACE("iface %p\n", iface);
940
941     This->stack[This->current] = *pm;
942
943     return D3D_OK;
944 }
945
946 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
947 {
948     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
949
950     TRACE("iface %p\n", iface);
951
952     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], pm);
953
954     return D3D_OK;
955 }
956
957 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
958 {
959     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
960
961     TRACE("iface %p\n", iface);
962
963     D3DXMatrixMultiply(&This->stack[This->current], pm, &This->stack[This->current]);
964
965     return D3D_OK;
966 }
967
968 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
969 {
970     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
971
972     TRACE("iface %p\n", iface);
973
974     /* Popping the last element on the stack returns D3D_OK, but does nothing. */
975     if (!This->current) return D3D_OK;
976
977     if (This->current <= This->stack_size / 4 && This->stack_size >= INITIAL_STACK_SIZE * 2)
978     {
979         unsigned int new_size;
980         D3DXMATRIX *new_stack;
981
982         new_size = This->stack_size / 2;
983         new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
984         if (new_stack)
985         {
986             This->stack_size = new_size;
987             This->stack = new_stack;
988         }
989     }
990
991     --This->current;
992
993     return D3D_OK;
994 }
995
996 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
997 {
998     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
999
1000     TRACE("iface %p\n", iface);
1001
1002     if (This->current == This->stack_size - 1)
1003     {
1004         unsigned int new_size;
1005         D3DXMATRIX *new_stack;
1006
1007         if (This->stack_size > UINT_MAX / 2) return E_OUTOFMEMORY;
1008
1009         new_size = This->stack_size * 2;
1010         new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1011         if (!new_stack) return E_OUTOFMEMORY;
1012
1013         This->stack_size = new_size;
1014         This->stack = new_stack;
1015     }
1016
1017     ++This->current;
1018     This->stack[This->current] = This->stack[This->current - 1];
1019
1020     return D3D_OK;
1021 }
1022
1023 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
1024 {
1025     D3DXMATRIX temp;
1026     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1027
1028     TRACE("iface %p\n", iface);
1029
1030     D3DXMatrixRotationAxis(&temp, pv, angle);
1031     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1032
1033     return D3D_OK;
1034 }
1035
1036 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
1037 {
1038     D3DXMATRIX temp;
1039     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1040
1041     TRACE("iface %p\n", iface);
1042
1043     D3DXMatrixRotationAxis(&temp, pv, angle);
1044     D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1045
1046     return D3D_OK;
1047 }
1048
1049 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1050 {
1051     D3DXMATRIX temp;
1052     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1053
1054     TRACE("iface %p\n", iface);
1055
1056     D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1057     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1058
1059     return D3D_OK;
1060 }
1061
1062 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1063 {
1064     D3DXMATRIX temp;
1065     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1066
1067     TRACE("iface %p\n", iface);
1068
1069     D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1070     D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1071
1072     return D3D_OK;
1073 }
1074
1075 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1076 {
1077     D3DXMATRIX temp;
1078     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1079
1080     TRACE("iface %p\n", iface);
1081
1082     D3DXMatrixScaling(&temp, x, y, z);
1083     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1084
1085     return D3D_OK;
1086 }
1087
1088 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1089 {
1090     D3DXMATRIX temp;
1091     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1092
1093     TRACE("iface %p\n", iface);
1094
1095     D3DXMatrixScaling(&temp, x, y, z);
1096     D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1097
1098     return D3D_OK;
1099 }
1100
1101 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1102 {
1103     D3DXMATRIX temp;
1104     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1105
1106     TRACE("iface %p\n", iface);
1107
1108     D3DXMatrixTranslation(&temp, x, y, z);
1109     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1110
1111     return D3D_OK;
1112 }
1113
1114 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1115 {
1116     D3DXMATRIX temp;
1117     struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1118
1119     TRACE("iface %p\n", iface);
1120
1121     D3DXMatrixTranslation(&temp, x, y, z);
1122     D3DXMatrixMultiply(&This->stack[This->current], &temp,&This->stack[This->current]);
1123
1124     return D3D_OK;
1125 }
1126
1127 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
1128 {
1129     ID3DXMatrixStackImpl_QueryInterface,
1130     ID3DXMatrixStackImpl_AddRef,
1131     ID3DXMatrixStackImpl_Release,
1132     ID3DXMatrixStackImpl_Pop,
1133     ID3DXMatrixStackImpl_Push,
1134     ID3DXMatrixStackImpl_LoadIdentity,
1135     ID3DXMatrixStackImpl_LoadMatrix,
1136     ID3DXMatrixStackImpl_MultMatrix,
1137     ID3DXMatrixStackImpl_MultMatrixLocal,
1138     ID3DXMatrixStackImpl_RotateAxis,
1139     ID3DXMatrixStackImpl_RotateAxisLocal,
1140     ID3DXMatrixStackImpl_RotateYawPitchRoll,
1141     ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
1142     ID3DXMatrixStackImpl_Scale,
1143     ID3DXMatrixStackImpl_ScaleLocal,
1144     ID3DXMatrixStackImpl_Translate,
1145     ID3DXMatrixStackImpl_TranslateLocal,
1146     ID3DXMatrixStackImpl_GetTop
1147 };
1148
1149 /*_________________D3DXPLANE________________*/
1150
1151 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, CONST D3DXVECTOR3 *pvpoint, CONST D3DXVECTOR3 *pvnormal)
1152 {
1153     TRACE("(%p, %p, %p)\n", pout, pvpoint, pvnormal);
1154
1155     pout->a = pvnormal->x;
1156     pout->b = pvnormal->y;
1157     pout->c = pvnormal->z;
1158     pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
1159     return pout;
1160 }
1161
1162 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3)
1163 {
1164     D3DXVECTOR3 edge1, edge2, normal, Nnormal;
1165
1166     TRACE("(%p, %p, %p, %p)\n", pout, pv1, pv2, pv3);
1167
1168     edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
1169     edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
1170     D3DXVec3Subtract(&edge1, pv2, pv1);
1171     D3DXVec3Subtract(&edge2, pv3, pv1);
1172     D3DXVec3Cross(&normal, &edge1, &edge2);
1173     D3DXVec3Normalize(&Nnormal, &normal);
1174     D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
1175     return pout;
1176 }
1177
1178 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, CONST D3DXPLANE *pp, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
1179 {
1180     D3DXVECTOR3 direction, normal;
1181     FLOAT dot, temp;
1182
1183     TRACE("(%p, %p, %p, %p)\n", pout, pp, pv1, pv2);
1184
1185     normal.x = pp->a;
1186     normal.y = pp->b;
1187     normal.z = pp->c;
1188     direction.x = pv2->x - pv1->x;
1189     direction.y = pv2->y - pv1->y;
1190     direction.z = pv2->z - pv1->z;
1191     dot = D3DXVec3Dot(&normal, &direction);
1192     if ( !dot ) return NULL;
1193     temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
1194     pout->x = pv1->x - temp * direction.x;
1195     pout->y = pv1->y - temp * direction.y;
1196     pout->z = pv1->z - temp * direction.z;
1197     return pout;
1198 }
1199
1200 D3DXPLANE* WINAPI D3DXPlaneNormalize(D3DXPLANE *pout, CONST D3DXPLANE *pp)
1201 {
1202     D3DXPLANE out;
1203     FLOAT norm;
1204
1205     TRACE("(%p, %p)\n", pout, pp);
1206
1207     norm = sqrt(pp->a * pp->a + pp->b * pp->b + pp->c * pp->c);
1208     if ( norm )
1209     {
1210      out.a = pp->a / norm;
1211      out.b = pp->b / norm;
1212      out.c = pp->c / norm;
1213      out.d = pp->d / norm;
1214     }
1215     else
1216     {
1217      out.a = 0.0f;
1218      out.b = 0.0f;
1219      out.c = 0.0f;
1220      out.d = 0.0f;
1221     }
1222     *pout = out;
1223     return pout;
1224 }
1225
1226 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, CONST D3DXPLANE *pplane, CONST D3DXMATRIX *pm)
1227 {
1228     CONST D3DXPLANE plane = *pplane;
1229
1230     TRACE("(%p, %p, %p)\n", pout, pplane, pm);
1231
1232     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;
1233     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;
1234     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;
1235     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;
1236     return pout;
1237 }
1238
1239 D3DXPLANE* WINAPI D3DXPlaneTransformArray(D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1240 {
1241     UINT i;
1242
1243     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1244
1245     for (i = 0; i < elements; ++i) {
1246         D3DXPlaneTransform(
1247             (D3DXPLANE*)((char*)out + outstride * i),
1248             (CONST D3DXPLANE*)((const char*)in + instride * i),
1249             matrix);
1250     }
1251     return out;
1252 }
1253
1254 /*_________________D3DXQUATERNION________________*/
1255
1256 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
1257 {
1258     D3DXQUATERNION temp1, temp2;
1259
1260     TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pq1, pq2, pq3, f, g);
1261
1262     D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
1263     return pout;
1264 }
1265
1266 D3DXQUATERNION* WINAPI D3DXQuaternionExp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1267 {
1268     FLOAT norm;
1269
1270     TRACE("(%p, %p)\n", pout, pq);
1271
1272     norm = sqrt(pq->x * pq->x + pq->y * pq->y + pq->z * pq->z);
1273     if (norm )
1274     {
1275      pout->x = sin(norm) * pq->x / norm;
1276      pout->y = sin(norm) * pq->y / norm;
1277      pout->z = sin(norm) * pq->z / norm;
1278      pout->w = cos(norm);
1279     }
1280     else
1281     {
1282      pout->x = 0.0f;
1283      pout->y = 0.0f;
1284      pout->z = 0.0f;
1285      pout->w = 1.0f;
1286     }
1287     return pout;
1288 }
1289
1290 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1291 {
1292     D3DXQUATERNION out;
1293     FLOAT norm;
1294
1295     TRACE("(%p, %p)\n", pout, pq);
1296
1297     norm = D3DXQuaternionLengthSq(pq);
1298
1299     out.x = -pq->x / norm;
1300     out.y = -pq->y / norm;
1301     out.z = -pq->z / norm;
1302     out.w = pq->w / norm;
1303
1304     *pout =out;
1305     return pout;
1306 }
1307
1308 D3DXQUATERNION* WINAPI D3DXQuaternionLn(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1309 {
1310     FLOAT t;
1311
1312     TRACE("(%p, %p)\n", pout, pq);
1313
1314     if ( (pq->w >= 1.0f) || (pq->w == -1.0f) )
1315         t = 1.0f;
1316     else
1317         t = acos( pq->w ) / sqrt( 1.0f - pq->w * pq->w );
1318
1319     pout->x = t * pq->x;
1320     pout->y = t * pq->y;
1321     pout->z = t * pq->z;
1322     pout->w = 0.0f;
1323
1324     return pout;
1325 }
1326
1327 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
1328 {
1329     D3DXQUATERNION out;
1330
1331     TRACE("(%p, %p, %p)\n", pout, pq1, pq2);
1332
1333     out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
1334     out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
1335     out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
1336     out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
1337     *pout = out;
1338     return pout;
1339 }
1340
1341 D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1342 {
1343     D3DXQUATERNION out;
1344     FLOAT norm;
1345
1346     TRACE("(%p, %p)\n", pout, pq);
1347
1348     norm = D3DXQuaternionLength(pq);
1349
1350     out.x = pq->x / norm;
1351     out.y = pq->y / norm;
1352     out.z = pq->z / norm;
1353     out.w = pq->w / norm;
1354
1355     *pout=out;
1356
1357     return pout;
1358 }
1359
1360 D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
1361 {
1362     D3DXVECTOR3 temp;
1363
1364     TRACE("(%p, %p, %f)\n", pout, pv, angle);
1365
1366     D3DXVec3Normalize(&temp, pv);
1367     pout->x = sin( angle / 2.0f ) * temp.x;
1368     pout->y = sin( angle / 2.0f ) * temp.y;
1369     pout->z = sin( angle / 2.0f ) * temp.z;
1370     pout->w = cos( angle / 2.0f );
1371     return pout;
1372 }
1373
1374 D3DXQUATERNION* WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *pout, CONST D3DXMATRIX *pm)
1375 {
1376     int i, maxi;
1377     FLOAT maxdiag, S, trace;
1378
1379     TRACE("(%p, %p)\n", pout, pm);
1380
1381     trace = pm->u.m[0][0] + pm->u.m[1][1] + pm->u.m[2][2] + 1.0f;
1382     if ( trace > 1.0f)
1383     {
1384      pout->x = ( pm->u.m[1][2] - pm->u.m[2][1] ) / ( 2.0f * sqrt(trace) );
1385      pout->y = ( pm->u.m[2][0] - pm->u.m[0][2] ) / ( 2.0f * sqrt(trace) );
1386      pout->z = ( pm->u.m[0][1] - pm->u.m[1][0] ) / ( 2.0f * sqrt(trace) );
1387      pout->w = sqrt(trace) / 2.0f;
1388      return pout;
1389      }
1390     maxi = 0;
1391     maxdiag = pm->u.m[0][0];
1392     for (i=1; i<3; i++)
1393     {
1394      if ( pm->u.m[i][i] > maxdiag )
1395      {
1396       maxi = i;
1397       maxdiag = pm->u.m[i][i];
1398      }
1399     }
1400     switch( maxi )
1401     {
1402      case 0:
1403        S = 2.0f * sqrt(1.0f + pm->u.m[0][0] - pm->u.m[1][1] - pm->u.m[2][2]);
1404        pout->x = 0.25f * S;
1405        pout->y = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1406        pout->z = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1407        pout->w = ( pm->u.m[1][2] - pm->u.m[2][1] ) / S;
1408      break;
1409      case 1:
1410        S = 2.0f * sqrt(1.0f + pm->u.m[1][1] - pm->u.m[0][0] - pm->u.m[2][2]);
1411        pout->x = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1412        pout->y = 0.25f * S;
1413        pout->z = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1414        pout->w = ( pm->u.m[2][0] - pm->u.m[0][2] ) / S;
1415      break;
1416      case 2:
1417        S = 2.0f * sqrt(1.0f + pm->u.m[2][2] - pm->u.m[0][0] - pm->u.m[1][1]);
1418        pout->x = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1419        pout->y = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1420        pout->z = 0.25f * S;
1421        pout->w = ( pm->u.m[0][1] - pm->u.m[1][0] ) / S;
1422      break;
1423     }
1424     return pout;
1425 }
1426
1427 D3DXQUATERNION* WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
1428 {
1429     TRACE("(%p, %f, %f, %f)\n", pout, yaw, pitch, roll);
1430
1431     pout->x = sin( yaw / 2.0f) * cos(pitch / 2.0f) * sin(roll / 2.0f) + cos(yaw / 2.0f) * sin(pitch / 2.0f) * cos(roll / 2.0f);
1432     pout->y = sin( yaw / 2.0f) * cos(pitch / 2.0f) * cos(roll / 2.0f) - cos(yaw / 2.0f) * sin(pitch / 2.0f) * sin(roll / 2.0f);
1433     pout->z = cos(yaw / 2.0f) * cos(pitch / 2.0f) * sin(roll / 2.0f) - sin( yaw / 2.0f) * sin(pitch / 2.0f) * cos(roll / 2.0f);
1434     pout->w = cos( yaw / 2.0f) * cos(pitch / 2.0f) * cos(roll / 2.0f) + sin(yaw / 2.0f) * sin(pitch / 2.0f) * sin(roll / 2.0f);
1435     return pout;
1436 }
1437
1438 D3DXQUATERNION* WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, FLOAT t)
1439 {
1440     FLOAT dot, epsilon, temp, theta, u;
1441
1442     TRACE("(%p, %p, %p, %f)\n", pout, pq1, pq2, t);
1443
1444     epsilon = 1.0f;
1445     temp = 1.0f - t;
1446     u = t;
1447     dot = D3DXQuaternionDot(pq1, pq2);
1448     if ( dot < 0.0f )
1449     {
1450         epsilon = -1.0f;
1451         dot = -dot;
1452     }
1453     if( 1.0f - dot > 0.001f )
1454     {
1455         theta = acos(dot);
1456         temp  = sin(theta * temp) / sin(theta);
1457         u = sin(theta * u) / sin(theta);
1458     }
1459     pout->x = temp * pq1->x + epsilon * u * pq2->x;
1460     pout->y = temp * pq1->y + epsilon * u * pq2->y;
1461     pout->z = temp * pq1->z + epsilon * u * pq2->z;
1462     pout->w = temp * pq1->w + epsilon * u * pq2->w;
1463     return pout;
1464 }
1465
1466 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, CONST D3DXQUATERNION *pq4, FLOAT t)
1467 {
1468     D3DXQUATERNION temp1, temp2;
1469
1470     TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pq1, pq2, pq3, pq4, t);
1471
1472     D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1473     return pout;
1474 }
1475
1476 static D3DXQUATERNION add_diff(CONST D3DXQUATERNION *q1, CONST D3DXQUATERNION *q2, CONST FLOAT add)
1477 {
1478     D3DXQUATERNION temp;
1479
1480     temp.x = q1->x + add * q2->x;
1481     temp.y = q1->y + add * q2->y;
1482     temp.z = q1->z + add * q2->z;
1483     temp.w = q1->w + add * q2->w;
1484
1485     return temp;
1486 }
1487
1488 void WINAPI D3DXQuaternionSquadSetup(D3DXQUATERNION *paout, D3DXQUATERNION *pbout, D3DXQUATERNION *pcout, CONST D3DXQUATERNION *pq0, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3)
1489 {
1490     D3DXQUATERNION q, temp1, temp2, temp3, zero;
1491
1492     TRACE("(%p, %p, %p, %p, %p, %p, %p)\n", paout, pbout, pcout, pq0, pq1, pq2, pq3);
1493
1494     zero.x = 0.0f;
1495     zero.y = 0.0f;
1496     zero.z = 0.0f;
1497     zero.w = 0.0f;
1498
1499     if ( D3DXQuaternionDot(pq0, pq1) <  0.0f )
1500         temp2 = add_diff(&zero, pq0, -1.0f);
1501     else
1502         temp2 = *pq0;
1503
1504     if ( D3DXQuaternionDot(pq1, pq2) < 0.0f )
1505         *pcout = add_diff(&zero, pq2, -1.0f);
1506     else
1507         *pcout = *pq2;
1508
1509     if ( D3DXQuaternionDot(pcout, pq3) < 0.0f )
1510         temp3 = add_diff(&zero, pq3, -1.0f);
1511     else
1512         temp3 = *pq3;
1513
1514     D3DXQuaternionInverse(&temp1, pq1);
1515     D3DXQuaternionMultiply(&temp2, &temp1, &temp2);
1516     D3DXQuaternionLn(&temp2, &temp2);
1517     D3DXQuaternionMultiply(&q, &temp1, pcout);
1518     D3DXQuaternionLn(&q, &q);
1519     temp1 = add_diff(&temp2, &q, 1.0f);
1520     temp1.x *= -0.25f;
1521     temp1.y *= -0.25f;
1522     temp1.z *= -0.25f;
1523     temp1.w *= -0.25f;
1524     D3DXQuaternionExp(&temp1, &temp1);
1525     D3DXQuaternionMultiply(paout, pq1, &temp1);
1526
1527     D3DXQuaternionInverse(&temp1, pcout);
1528     D3DXQuaternionMultiply(&temp2, &temp1, pq1);
1529     D3DXQuaternionLn(&temp2, &temp2);
1530     D3DXQuaternionMultiply(&q, &temp1, &temp3);
1531     D3DXQuaternionLn(&q, &q);
1532     temp1 = add_diff(&temp2, &q, 1.0f);
1533     temp1.x *= -0.25f;
1534     temp1.y *= -0.25f;
1535     temp1.z *= -0.25f;
1536     temp1.w *= -0.25f;
1537     D3DXQuaternionExp(&temp1, &temp1);
1538     D3DXQuaternionMultiply(pbout, pcout, &temp1);
1539
1540     return;
1541 }
1542
1543 void WINAPI D3DXQuaternionToAxisAngle(CONST D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1544 {
1545     TRACE("(%p, %p, %p)\n", pq, paxis, pangle);
1546
1547     paxis->x = pq->x;
1548     paxis->y = pq->y;
1549     paxis->z = pq->z;
1550     *pangle = 2.0f * acos(pq->w);
1551 }
1552
1553 /*_________________D3DXVec2_____________________*/
1554
1555 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1556 {
1557     TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pv1, pv2, pv3, f, g);
1558
1559     pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1560     pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1561     return pout;
1562 }
1563
1564 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv0, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT s)
1565 {
1566     TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv0, pv1, pv2, pv3, s);
1567
1568     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);
1569     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);
1570     return pout;
1571 }
1572
1573 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pt1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pt2, FLOAT s)
1574 {
1575     FLOAT h1, h2, h3, h4;
1576
1577     TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv1, pt1, pv2, pt2, s);
1578
1579     h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1580     h2 = s * s * s - 2.0f * s * s + s;
1581     h3 = -2.0f * s * s * s + 3.0f * s * s;
1582     h4 = s * s * s - s * s;
1583
1584     pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1585     pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1586     return pout;
1587 }
1588
1589 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
1590 {
1591     FLOAT norm;
1592
1593     TRACE("(%p, %p)\n", pout, pv);
1594
1595     norm = D3DXVec2Length(pv);
1596     if ( !norm )
1597     {
1598         pout->x = 0.0f;
1599         pout->y = 0.0f;
1600     }
1601     else
1602     {
1603         pout->x = pv->x / norm;
1604         pout->y = pv->y / norm;
1605     }
1606
1607     return pout;
1608 }
1609
1610 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1611 {
1612     TRACE("(%p, %p, %p)\n", pout, pv, pm);
1613
1614     pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y  + pm->u.m[3][0];
1615     pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y  + pm->u.m[3][1];
1616     pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y  + pm->u.m[3][2];
1617     pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y  + pm->u.m[3][3];
1618     return pout;
1619 }
1620
1621 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1622 {
1623     UINT i;
1624
1625     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1626
1627     for (i = 0; i < elements; ++i) {
1628         D3DXVec2Transform(
1629             (D3DXVECTOR4*)((char*)out + outstride * i),
1630             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1631             matrix);
1632     }
1633     return out;
1634 }
1635
1636 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1637 {
1638     D3DXVECTOR2 v;
1639     FLOAT norm;
1640
1641     TRACE("(%p, %p, %p)\n", pout, pv, pm);
1642
1643     v = *pv;
1644     norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1645
1646     pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1647     pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1648
1649     return pout;
1650 }
1651
1652 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1653 {
1654     UINT i;
1655
1656     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1657
1658     for (i = 0; i < elements; ++i) {
1659         D3DXVec2TransformCoord(
1660             (D3DXVECTOR2*)((char*)out + outstride * i),
1661             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1662             matrix);
1663     }
1664     return out;
1665 }
1666
1667 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1668 {
1669     CONST D3DXVECTOR2 v = *pv;
1670     pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1671     pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1672     return pout;
1673 }
1674
1675 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride, CONST D3DXMATRIX *matrix, UINT elements)
1676 {
1677     UINT i;
1678
1679     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1680
1681     for (i = 0; i < elements; ++i) {
1682         D3DXVec2TransformNormal(
1683             (D3DXVECTOR2*)((char*)out + outstride * i),
1684             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1685             matrix);
1686     }
1687     return out;
1688 }
1689
1690 /*_________________D3DXVec3_____________________*/
1691
1692 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1693 {
1694     TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pv1, pv2, pv3, f, g);
1695
1696     pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1697     pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1698     pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1699     return pout;
1700 }
1701
1702 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
1703 {
1704     TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv0, pv1, pv2, pv3, s);
1705
1706     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);
1707     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);
1708     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);
1709     return pout;
1710 }
1711
1712 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
1713 {
1714     FLOAT h1, h2, h3, h4;
1715
1716     TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv1, pt1, pv2, pt2, s);
1717
1718     h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1719     h2 = s * s * s - 2.0f * s * s + s;
1720     h3 = -2.0f * s * s * s + 3.0f * s * s;
1721     h4 = s * s * s - s * s;
1722
1723     pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1724     pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1725     pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1726     return pout;
1727 }
1728
1729 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
1730 {
1731     FLOAT norm;
1732
1733     TRACE("(%p, %p)\n", pout, pv);
1734
1735     norm = D3DXVec3Length(pv);
1736     if ( !norm )
1737     {
1738         pout->x = 0.0f;
1739         pout->y = 0.0f;
1740         pout->z = 0.0f;
1741     }
1742     else
1743     {
1744         pout->x = pv->x / norm;
1745         pout->y = pv->y / norm;
1746         pout->z = pv->z / norm;
1747     }
1748
1749     return pout;
1750 }
1751
1752 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1753 {
1754     D3DXMATRIX m;
1755     D3DXVECTOR3 out;
1756
1757     TRACE("(%p, %p, %p, %p, %p, %p)\n", pout, pv, pviewport, pprojection, pview, pworld);
1758
1759     D3DXMatrixMultiply(&m, pworld, pview);
1760     D3DXMatrixMultiply(&m, &m, pprojection);
1761     D3DXVec3TransformCoord(&out, pv, &m);
1762     out.x = pviewport->X +  ( 1.0f + out.x ) * pviewport->Width / 2.0f;
1763     out.y = pviewport->Y +  ( 1.0f - out.y ) * pviewport->Height / 2.0f;
1764     out.z = pviewport->MinZ + out.z * ( pviewport->MaxZ - pviewport->MinZ );
1765     *pout = out;
1766     return pout;
1767 }
1768
1769 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)
1770 {
1771     UINT i;
1772
1773     TRACE("(%p, %u, %p, %u, %p, %p, %p, %p, %u)\n", out, outstride, in, instride, viewport, projection, view, world, elements);
1774
1775     for (i = 0; i < elements; ++i) {
1776         D3DXVec3Project(
1777             (D3DXVECTOR3*)((char*)out + outstride * i),
1778             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1779             viewport, projection, view, world);
1780     }
1781     return out;
1782 }
1783
1784 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1785 {
1786     TRACE("(%p, %p, %p)\n", pout, pv, pm);
1787
1788     pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z + pm->u.m[3][0];
1789     pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z + pm->u.m[3][1];
1790     pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z + pm->u.m[3][2];
1791     pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[2][3] * pv->z + pm->u.m[3][3];
1792     return pout;
1793 }
1794
1795 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1796 {
1797     UINT i;
1798
1799     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1800
1801     for (i = 0; i < elements; ++i) {
1802         D3DXVec3Transform(
1803             (D3DXVECTOR4*)((char*)out + outstride * i),
1804             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1805             matrix);
1806     }
1807     return out;
1808 }
1809
1810 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1811 {
1812     D3DXVECTOR3 out;
1813     FLOAT norm;
1814
1815     TRACE("(%p, %p, %p)\n", pout, pv, pm);
1816
1817     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];
1818
1819     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;
1820     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;
1821     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;
1822
1823     *pout = out;
1824
1825     return pout;
1826 }
1827
1828 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1829 {
1830     UINT i;
1831
1832     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1833
1834     for (i = 0; i < elements; ++i) {
1835         D3DXVec3TransformCoord(
1836             (D3DXVECTOR3*)((char*)out + outstride * i),
1837             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1838             matrix);
1839     }
1840     return out;
1841 }
1842
1843 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1844 {
1845     CONST D3DXVECTOR3 v = *pv;
1846
1847     TRACE("(%p, %p, %p)\n", pout, pv, pm);
1848
1849     pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1850     pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1851     pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1852     return pout;
1853
1854 }
1855
1856 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1857 {
1858     UINT i;
1859
1860     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1861
1862     for (i = 0; i < elements; ++i) {
1863         D3DXVec3TransformNormal(
1864             (D3DXVECTOR3*)((char*)out + outstride * i),
1865             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1866             matrix);
1867     }
1868     return out;
1869 }
1870
1871 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1872 {
1873     D3DXMATRIX m;
1874     D3DXVECTOR3 out;
1875
1876     TRACE("(%p, %p, %p, %p, %p, %p)\n", pout, pv, pviewport, pprojection, pview, pworld);
1877
1878     if (pworld) {
1879         D3DXMatrixMultiply(&m, pworld, pview);
1880         D3DXMatrixMultiply(&m, &m, pprojection);
1881     } else {
1882         D3DXMatrixMultiply(&m, pview, pprojection);
1883     }
1884     D3DXMatrixInverse(&m, NULL, &m);
1885     out.x = 2.0f * ( pv->x - pviewport->X ) / pviewport->Width - 1.0f;
1886     out.y = 1.0f - 2.0f * ( pv->y - pviewport->Y ) / pviewport->Height;
1887     out.z = ( pv->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
1888     D3DXVec3TransformCoord(&out, &out, &m);
1889     *pout = out;
1890     return pout;
1891 }
1892
1893 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)
1894 {
1895     UINT i;
1896
1897     TRACE("(%p, %u, %p, %u, %p, %p, %p, %p, %u)\n", out, outstride, in, instride, viewport, projection, view, world, elements);
1898
1899     for (i = 0; i < elements; ++i) {
1900         D3DXVec3Unproject(
1901             (D3DXVECTOR3*)((char*)out + outstride * i),
1902             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1903             viewport, projection, view, world);
1904     }
1905     return out;
1906 }
1907
1908 /*_________________D3DXVec4_____________________*/
1909
1910 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
1911 {
1912     TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pv1, pv2, pv3, f, g);
1913
1914     pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1915     pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1916     pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1917     pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
1918     return pout;
1919 }
1920
1921 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv0, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT s)
1922 {
1923     TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv0, pv1, pv2, pv3, s);
1924
1925     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);
1926     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);
1927     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);
1928     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);
1929     return pout;
1930 }
1931
1932 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
1933 {
1934     D3DXVECTOR4 out;
1935
1936     TRACE("(%p, %p, %p, %p)\n", pout, pv1, pv2, pv3);
1937
1938     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);
1939     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));
1940     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);
1941     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));
1942     *pout = out;
1943     return pout;
1944 }
1945
1946 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pt1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pt2, FLOAT s)
1947 {
1948     FLOAT h1, h2, h3, h4;
1949
1950     TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv1, pt1, pv2, pt2, s);
1951
1952     h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1953     h2 = s * s * s - 2.0f * s * s + s;
1954     h3 = -2.0f * s * s * s + 3.0f * s * s;
1955     h4 = s * s * s - s * s;
1956
1957     pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1958     pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1959     pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1960     pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
1961     return pout;
1962 }
1963
1964 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
1965 {
1966     FLOAT norm;
1967
1968     TRACE("(%p, %p)\n", pout, pv);
1969
1970     norm = D3DXVec4Length(pv);
1971
1972     pout->x = pv->x / norm;
1973     pout->y = pv->y / norm;
1974     pout->z = pv->z / norm;
1975     pout->w = pv->w / norm;
1976
1977     return pout;
1978 }
1979
1980 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
1981 {
1982     D3DXVECTOR4 out;
1983
1984     TRACE("(%p, %p, %p)\n", pout, pv, pm);
1985
1986     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;
1987     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;
1988     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;
1989     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;
1990     *pout = out;
1991     return pout;
1992 }
1993
1994 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1995 {
1996     UINT i;
1997
1998     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1999
2000     for (i = 0; i < elements; ++i) {
2001         D3DXVec4Transform(
2002             (D3DXVECTOR4*)((char*)out + outstride * i),
2003             (CONST D3DXVECTOR4*)((const char*)in + instride * i),
2004             matrix);
2005     }
2006     return out;
2007 }
2008
2009 static inline unsigned short float_32_to_16(const float in)
2010 {
2011     int exp = 0, origexp;
2012     float tmp = fabs(in);
2013     int sign = (copysignf(1, in) < 0);
2014     unsigned int mantissa;
2015     unsigned short ret;
2016
2017     /* Deal with special numbers */
2018     if (isinf(in)) return (sign ? 0xffff : 0x7fff);
2019     if (isnan(in)) return (sign ? 0xffff : 0x7fff);
2020     if (in == 0.0f) return (sign ? 0x8000 : 0x0000);
2021
2022     if (tmp < powf(2, 10))
2023     {
2024         do
2025         {
2026             tmp *= 2.0f;
2027             exp--;
2028         } while (tmp < powf(2, 10));
2029     }
2030     else if (tmp >= powf(2, 11))
2031     {
2032         do
2033         {
2034             tmp /= 2.0f;
2035             exp++;
2036         } while (tmp >= powf(2, 11));
2037     }
2038
2039     exp += 10;  /* Normalize the mantissa */
2040     exp += 15;  /* Exponent is encoded with excess 15 */
2041
2042     origexp = exp;
2043
2044     mantissa = (unsigned int) tmp;
2045     if ((tmp - mantissa == 0.5f && mantissa % 2 == 1) || /* round half to even */
2046         (tmp - mantissa > 0.5f))
2047     {
2048         mantissa++; /* round to nearest, away from zero */
2049     }
2050     if (mantissa == 2048)
2051     {
2052         mantissa = 1024;
2053         exp++;
2054     }
2055
2056     if (exp > 31)
2057     {
2058         /* too big */
2059         ret = 0x7fff; /* INF */
2060     }
2061     else if (exp <= 0)
2062     {
2063         unsigned int rounding = 0;
2064
2065         /* Denormalized half float */
2066
2067         /* return 0x0000 (=0.0) for numbers too small to represent in half floats */
2068         if (exp < -11)
2069             return (sign ? 0x8000 : 0x0000);
2070
2071         exp = origexp;
2072
2073         /* the 13 extra bits from single precision are used for rounding */
2074         mantissa = (unsigned int)(tmp * powf(2, 13));
2075         mantissa >>= 1 - exp; /* denormalize */
2076
2077         mantissa -= ~(mantissa >> 13) & 1; /* round half to even */
2078         /* remove 13 least significant bits to get half float precision */
2079         mantissa >>= 12;
2080         rounding = mantissa & 1;
2081         mantissa >>= 1;
2082
2083         ret = mantissa + rounding;
2084     }
2085     else
2086     {
2087         ret = (exp << 10) | (mantissa & 0x3ff);
2088     }
2089
2090     ret |= ((sign ? 1 : 0) << 15); /* Add the sign */
2091     return ret;
2092 }
2093
2094 D3DXFLOAT16 *WINAPI D3DXFloat32To16Array(D3DXFLOAT16 *pout, CONST FLOAT *pin, UINT n)
2095 {
2096     unsigned int i;
2097
2098     TRACE("(%p, %p, %u)\n", pout, pin, n);
2099
2100     for (i = 0; i < n; ++i)
2101     {
2102         pout[i].value = float_32_to_16(pin[i]);
2103     }
2104
2105     return pout;
2106 }
2107
2108 /* Native d3dx9's D3DXFloat16to32Array lacks support for NaN and Inf. Specifically, e = 16 is treated as a
2109  * regular number - e.g., 0x7fff is converted to 131008.0 and 0xffff to -131008.0. */
2110 static inline float float_16_to_32(const unsigned short in)
2111 {
2112     const unsigned short s = (in & 0x8000);
2113     const unsigned short e = (in & 0x7C00) >> 10;
2114     const unsigned short m = in & 0x3FF;
2115     const float sgn = (s ? -1.0f : 1.0f);
2116
2117     if (e == 0)
2118     {
2119         if (m == 0) return sgn * 0.0f; /* +0.0 or -0.0 */
2120         else return sgn * powf(2, -14.0f) * (m / 1024.0f);
2121     }
2122     else
2123     {
2124         return sgn * powf(2, e - 15.0f) * (1.0f + (m / 1024.0f));
2125     }
2126 }
2127
2128 FLOAT *WINAPI D3DXFloat16To32Array(FLOAT *pout, CONST D3DXFLOAT16 *pin, UINT n)
2129 {
2130     unsigned int i;
2131
2132     TRACE("(%p, %p, %u)\n", pout, pin, n);
2133
2134     for (i = 0; i < n; ++i)
2135     {
2136         pout[i] = float_16_to_32(pin[i].value);
2137     }
2138
2139     return pout;
2140 }
2141
2142 /*_________________D3DXSH________________*/
2143
2144 FLOAT* WINAPI D3DXSHAdd(FLOAT *out, UINT order, const FLOAT *a, const FLOAT *b)
2145 {
2146     UINT i;
2147
2148     TRACE("out %p, order %u, a %p, b %p\n", out, order, a, b);
2149
2150     for (i = 0; i < order * order; i++)
2151         out[i] = a[i] + b[i];
2152
2153     return out;
2154 }
2155
2156 FLOAT WINAPI D3DXSHDot(UINT order, CONST FLOAT *a, CONST FLOAT *b)
2157 {
2158     FLOAT s;
2159     UINT i;
2160
2161     TRACE("order %u, a %p, b %p\n", order, a, b);
2162
2163     s = a[0] * b[0];
2164     for (i = 1; i < order * order; i++)
2165         s += a[i] * b[i];
2166
2167     return s;
2168 }
2169
2170 FLOAT* WINAPI D3DXSHEvalDirection(FLOAT *out, UINT order, CONST D3DXVECTOR3 *dir)
2171 {
2172
2173     TRACE("(%p, %u, %p)\n", out, order, dir);
2174
2175     if ( (order < D3DXSH_MINORDER) || (order > D3DXSH_MAXORDER) )
2176         return out;
2177
2178     out[0] = 0.5f / sqrt(D3DX_PI);
2179     out[1] = -0.5f / sqrt(D3DX_PI / 3.0f) * dir->y;
2180     out[2] = 0.5f / sqrt(D3DX_PI / 3.0f) * dir->z;
2181     out[3] = -0.5f / sqrt(D3DX_PI / 3.0f) * dir->x;
2182     if ( order == 2 )
2183         return out;
2184
2185     out[4] = 0.5f / sqrt(D3DX_PI / 15.0f) * dir->x * dir->y;
2186     out[5] = -0.5f / sqrt(D3DX_PI / 15.0f) * dir->y * dir->z;
2187     out[6] = 0.25f / sqrt(D3DX_PI / 5.0f) * ( 3.0f * dir->z * dir->z - 1.0f );
2188     out[7] = -0.5f / sqrt(D3DX_PI / 15.0f) * dir->x * dir->z;
2189     out[8] = 0.25f / sqrt(D3DX_PI / 15.0f) * ( dir->x * dir->x - dir->y * dir->y );
2190     if ( order == 3 )
2191         return out;
2192
2193     out[9] = -sqrt(70.0f / D3DX_PI) / 8.0f * dir->y * (3.0f * dir->x * dir->x - dir->y * dir->y );
2194     out[10] = sqrt(105.0f / D3DX_PI) / 2.0f * dir->x * dir->y * dir->z;
2195     out[11] = -sqrt(42.0 / D3DX_PI) / 8.0f * dir->y * ( -1.0f + 5.0f * dir->z * dir->z );
2196     out[12] = sqrt(7.0f / D3DX_PI) / 4.0f * dir->z * ( 5.0f * dir->z * dir->z - 3.0f );
2197     out[13] = sqrt(42.0 / D3DX_PI) / 8.0f * dir->x * ( 1.0f - 5.0f * dir->z * dir->z );
2198     out[14] = sqrt(105.0f / D3DX_PI) / 4.0f * dir->z * ( dir->x * dir->x - dir->y * dir->y );
2199     out[15] = -sqrt(70.0f / D3DX_PI) / 8.0f * dir->x * ( dir->x * dir->x - 3.0f * dir->y * dir->y );
2200     if ( order == 4 )
2201         return out;
2202
2203     out[16] = 0.75f * sqrt(35.0f / D3DX_PI) * dir->x * dir->y * (dir->x * dir->x - dir->y * dir->y );
2204     out[17] = 3.0f * dir->z * out[9];
2205     out[18] = 0.75f * sqrt(5.0f / D3DX_PI) * dir->x * dir->y * ( 7.0f * dir->z * dir->z - 1.0f );
2206     out[19] = 0.375f * sqrt(10.0f / D3DX_PI) * dir->y * dir->z * ( 3.0f - 7.0f * dir->z * dir->z );
2207     out[20] = 3.0f / ( 16.0f * sqrt(D3DX_PI) ) * ( 35.0f * dir->z * dir->z * dir->z * dir->z - 30.f * dir->z * dir->z + 3.0f );
2208     out[21] = 0.375f * sqrt(10.0f / D3DX_PI) * dir->x * dir->z * ( 3.0f - 7.0f * dir->z * dir->z );
2209     out[22] = 0.375f * sqrt(5.0f / D3DX_PI) * ( dir->x * dir->x - dir->y * dir->y ) * ( 7.0f * dir->z * dir->z - 1.0f);
2210     out[23] = 3.0 * dir->z * out[15];
2211     out[24] = 3.0f / 16.0f * sqrt(35.0f / D3DX_PI) * ( dir->x * dir->x * dir->x * dir->x- 6.0f * dir->x * dir->x * dir->y * dir->y + dir->y * dir->y * dir->y * dir->y );
2212     if ( order == 5 )
2213         return out;
2214
2215     out[25] = -3.0f/ 32.0f * sqrt(154.0f / D3DX_PI) * dir->y * ( 5.0f * dir->x * dir->x * dir->x * dir->x - 10.0f * dir->x * dir->x * dir->y * dir->y + dir->y * dir->y * dir->y * dir->y );
2216     out[26] = 0.75f * sqrt(385.0f / D3DX_PI) * dir->x * dir->y * dir->z * ( dir->x * dir->x - dir->y * dir->y );
2217     out[27] = sqrt(770.0f / D3DX_PI) / 32.0f * dir->y * ( 3.0f * dir->x * dir->x - dir->y * dir->y ) * ( 1.0f - 9.0f * dir->z * dir->z );
2218     out[28] = sqrt(1155.0f / D3DX_PI) / 4.0f * dir->x * dir->y * dir->z * ( 3.0f * dir->z * dir->z - 1.0f);
2219     out[29] = sqrt(165.0f / D3DX_PI) / 16.0f * dir->y * ( 14.0f * dir->z * dir->z - 21.0f * dir->z * dir->z * dir->z * dir->z - 1.0f );
2220     out[30] = sqrt(11.0f / D3DX_PI) / 16.0f * dir->z * ( 63.0f * dir->z * dir->z * dir->z * dir->z - 70.0f * dir->z * dir->z + 15.0f );
2221     out[31] = sqrt(165.0f / D3DX_PI) / 16.0f * dir->x * ( 14.0f * dir->z * dir->z - 21.0f * dir->z * dir->z * dir->z * dir->z - 1.0f );
2222     out[32] = sqrt(1155.0f / D3DX_PI) / 8.0f * dir->z * ( dir->x * dir->x - dir->y * dir->y ) * ( 3.0f * dir->z * dir->z - 1.0f );
2223     out[33] = sqrt(770.0f / D3DX_PI) / 32.0f * dir->x * ( dir->x * dir->x - 3.0f * dir->y * dir->y ) * ( 1.0f - 9.0f * dir->z * dir->z );
2224     out[34] = 3.0f / 16.0f * sqrt(385.0f / D3DX_PI) * dir->z * ( dir->x * dir->x * dir->x * dir->x - 6.0 * dir->x * dir->x * dir->y * dir->y + dir->y * dir->y * dir->y * dir->y );
2225     out[35] = -3.0f/ 32.0f * sqrt(154.0f / D3DX_PI) * dir->x * ( dir->x * dir->x * dir->x * dir->x - 10.0f * dir->x * dir->x * dir->y * dir->y + 5.0f * dir->y * dir->y * dir->y * dir->y );
2226
2227     return out;
2228 }
2229
2230 HRESULT WINAPI D3DXSHEvalDirectionalLight(UINT order, CONST D3DXVECTOR3 *dir, FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *Rout, FLOAT *Gout, FLOAT *Bout)
2231 {
2232     FLOAT s, temp;
2233     UINT j;
2234
2235     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);
2236
2237     s = 0.75f;
2238     if ( order > 2 )
2239         s += 5.0f / 16.0f;
2240     if ( order > 4 )
2241         s -= 3.0f / 32.0f;
2242     s /= D3DX_PI;
2243
2244     D3DXSHEvalDirection(Rout, order, dir);
2245     for (j = 0; j < order * order; j++)
2246     {
2247         temp = Rout[j] / s;
2248
2249         Rout[j] = Rintensity * temp;
2250         if ( Gout )
2251             Gout[j] = Gintensity * temp;
2252         if ( Bout )
2253             Bout[j] = Bintensity * temp;
2254     }
2255
2256     return D3D_OK;
2257 }
2258
2259 FLOAT* WINAPI D3DXSHMultiply2(FLOAT *out, CONST FLOAT *a, CONST FLOAT *b)
2260 {
2261     FLOAT ta, tb;
2262
2263     TRACE("(%p, %p, %p)\n", out, a, b);
2264
2265     ta = 0.28209479f * a[0];
2266     tb = 0.28209479f * b[0];
2267
2268     out[0]= 0.28209479f * D3DXSHDot(2, a, b);
2269     out[1] = ta * b[1] + tb * a[1];
2270     out[2] = ta * b[2] + tb * a[2];
2271     out[3] = ta * b[3] + tb * a[3];
2272
2273     return out;
2274 }
2275
2276 FLOAT* WINAPI D3DXSHMultiply3(FLOAT *out, CONST FLOAT *a, CONST FLOAT *b)
2277 {
2278     FLOAT t, ta, tb;
2279
2280     TRACE("(%p, %p, %p)\n", out, a, b);
2281
2282     out[0]= 0.28209479f * a[0] * b[0];
2283
2284     ta = 0.28209479f * a[0] - 0.12615662f * a[6] - 0.21850968f * a[8];
2285     tb = 0.28209479f * b[0] - 0.12615662f * b[6] - 0.21850968f * b[8];
2286     out[1] = ta * b[1] + tb * a[1];
2287     t = a[1] * b[1];
2288     out[0] += 0.28209479f * t;
2289     out[6] = -0.12615662f * t;
2290     out[8] = -0.21850968f * t;
2291
2292     ta = 0.21850968f * a[5];
2293     tb = 0.21850968f * b[5];
2294     out[1] += ta * b[2] + tb * a[2];
2295     out[2] = ta * b[1] + tb * a[1];
2296     t = a[1] * b[2] +a[2] * b[1];
2297     out[5] = 0.21850968f * t;
2298
2299     ta = 0.21850968f * a[4];
2300     tb = 0.21850968f * b[4];
2301     out[1] += ta * b[3] + tb * a[3];
2302     out[3]  = ta * b[1] + tb * a[1];
2303     t = a[1] * b[3] + a[3] * b[1];
2304     out[4] = 0.21850968f * t;
2305
2306     ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2307     tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2308     out[2] += ta * b[2] + tb * a[2];
2309     t = a[2] * b[2];
2310     out[0] += 0.28209480f * t;
2311     out[6] += 0.25231326f * t;
2312
2313     ta = 0.21850969f * a[7];
2314     tb = 0.21850969f * b[7];
2315     out[2] += ta * b[3] + tb * a[3];
2316     out[3] += ta * b[2] + tb * a[2];
2317     t = a[2] * b[3] + a[3] * b[2];
2318     out[7] = 0.21850969f * t;
2319
2320     ta = 0.28209479f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2321     tb = 0.28209479f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2322     out[3] += ta * b[3] + tb * a[3];
2323     t = a[3] * b[3];
2324     out[0] += 0.28209479f * t;
2325     out[6] -= 0.12615663f * t;
2326     out[8] += 0.21850969f * t;
2327
2328     ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2329     tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2330     out[4] += ta * b[4] + tb * a[4];
2331     t = a[4] * b[4];
2332     out[0] += 0.28209479f * t;
2333     out[6] -= 0.18022375f * t;
2334
2335     ta = 0.15607835f * a[7];
2336     tb = 0.15607835f * b[7];
2337     out[4] += ta * b[5] + tb * a[5];
2338     out[5] += ta * b[4] + tb * a[4];
2339     t = a[4] * b[5] + a[5] * b[4];
2340     out[7] += 0.15607834f * t;
2341
2342     ta = 0.28209479f * a[0] + 0.09011186 * a[6] - 0.15607835f * a[8];
2343     tb = 0.28209479f * b[0] + 0.09011186 * b[6] - 0.15607835f * b[8];
2344     out[5] += ta * b[5] + tb * a[5];
2345     t = a[5] * b[5];
2346     out[0] += 0.28209479f * t;
2347     out[6] += 0.09011186f * t;
2348     out[8] -= 0.15607835f * t;
2349
2350     ta = 0.28209480f * a[0];
2351     tb = 0.28209480f * b[0];
2352     out[6] += ta * b[6] + tb * a[6];
2353     t = a[6] * b[6];
2354     out[0] += 0.28209480f * t;
2355     out[6] += 0.18022376f * t;
2356
2357     ta = 0.28209479f * a[0] + 0.09011186 * a[6] + 0.15607835f * a[8];
2358     tb = 0.28209479f * b[0] + 0.09011186 * b[6] + 0.15607835f * b[8];
2359     out[7] += ta * b[7] + tb * a[7];
2360     t = a[7] * b[7];
2361     out[0] += 0.28209479f * t;
2362     out[6] += 0.09011186f * t;
2363     out[8] += 0.15607835f * t;
2364
2365     ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2366     tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2367     out[8] += ta * b[8] + tb * a[8];
2368     t = a[8] * b[8];
2369     out[0] += 0.28209479f * t;
2370     out[6] -= 0.18022375f * t;
2371
2372     return out;
2373 }
2374
2375 static void rotate_X(FLOAT *out, UINT order, FLOAT a, FLOAT *in)
2376 {
2377     out[0] = in[0];
2378     if ( order < 2 )
2379         return;
2380
2381     out[1] = a * in[2];
2382     out[2] = -a * in[1];
2383     out[3] = in[3];
2384     if ( order == 2 )
2385         return;
2386
2387     out[4] = a * in[7];
2388     out[5] = -in[5];
2389     out[6] = -0.5f * in[6] - 0.8660253882f * in[8];
2390     out[7] = -a * in[4];
2391     out[8] = -0.8660253882f * in[6] + 0.5f * in[8];
2392     out[9] = -a * 0.7905694842f * in[12] + a * 0.6123724580f * in[14];
2393     if ( order == 3 )
2394         return;
2395
2396     out[10] = -in[10];
2397     out[11] = -a * 0.6123724580f * in[12] - a * 0.7905694842f * in[14];
2398     out[12] = a * 0.7905694842f * in[9] + a * 0.6123724580f * in[11];
2399     out[13] = -0.25f * in[13] - 0.9682458639f * in[15];
2400     out[14] = -a * 0.6123724580f * in[9] + a * 0.7905694842f * in[11];
2401     out[15] = -0.9682458639f * in[13] + 0.25f * in[15];
2402     if ( order == 4 )
2403         return;
2404
2405     out[16] = -a * 0.9354143739f * in[21] + a * 0.3535533845f * in[23];
2406     out[17] = -0.75f * in[17] + 0.6614378095f * in[19];
2407     out[18] = -a * 0.3535533845f * in[21] - a * 0.9354143739f * in[23];
2408     out[19] = 0.6614378095f * in[17] + 0.75f * in[19];
2409     out[20] = 0.375f * in[20] + 0.5590170026f * in[22] + 0.7395099998f * in[24];
2410     out[21] = a * 0.9354143739f * in[16] + a * 0.3535533845f * in[18];
2411     out[22] = 0.5590170026f * in[20] + 0.5f * in[22] - 0.6614378691f * in[24];
2412     out[23] = -a * 0.3535533845f * in[16] + a * 0.9354143739f * in[18];
2413     out[24] = 0.7395099998f * in[20] - 0.6614378691f * in[22] + 0.125f * in[24];
2414     if ( order == 5 )
2415         return;
2416
2417     out[25] = a * 0.7015607357f * in[30] - a * 0.6846531630f * in[32] + a * 0.1976423711f * in[34];
2418     out[26] = -0.5f * in[26] + 0.8660253882f * in[28];
2419     out[27] = a * 0.5229125023f * in[30] + a * 0.3061861992f * in[32] - a * 0.7954951525 * in[34];
2420     out[28] = 0.8660253882f * in[26] + 0.5f * in[28];
2421     out[29] = a * 0.4841229022f * in[30] + a * 0.6614378691f * in[32] + a * 0.5728219748f * in[34];
2422     out[30] = -a * 0.7015607357f * in[25] - a * 0.5229125023f * in[27] - a * 0.4841229022f * in[29];
2423     out[31] = 0.125f * in[31] + 0.4050463140f * in[33] + 0.9057110548f * in[35];
2424     out[32] = a * 0.6846531630f * in[25] - a * 0.3061861992f * in[27] - a * 0.6614378691f * in[29];
2425     out[33] = 0.4050463140f * in[31] + 0.8125f * in[33] - 0.4192627370f * in[35];
2426     out[34] = -a * 0.1976423711f * in[25] + a * 0.7954951525f * in[27] - a * 0.5728219748f * in[29];
2427     out[35] = 0.9057110548f * in[31] - 0.4192627370f * in[33] + 0.0624999329f * in[35];
2428
2429 }
2430
2431 FLOAT* WINAPI D3DXSHRotate(FLOAT *out, UINT order, CONST D3DXMATRIX *matrix, CONST FLOAT *in)
2432 {
2433     FLOAT alpha, beta, gamma, sinb, temp[36];
2434
2435     TRACE("out %p, order %u, matrix %p, in %p\n", out, order, matrix, in);
2436
2437     out[0] = in[0];
2438
2439     if ( ( order > D3DXSH_MAXORDER ) || ( order < D3DXSH_MINORDER ) )
2440         return out;
2441
2442     /* TODO: Implement handy computations for order <= 3. They are faster than the general algorithm. */
2443     if ( order < 4 )
2444         WARN("Using general algorithm for order = %u\n", order);
2445
2446     if ( fabsf( matrix->u.m[2][2] ) != 1.0f )
2447     {
2448         sinb = sqrtf( 1.0f - matrix->u.m[2][2] * matrix->u.m[2][2] );
2449         alpha = atan2f(matrix->u.m[2][1] / sinb, matrix->u.m[2][0] / sinb );
2450         beta = atan2f( sinb, matrix->u.m[2][2] );
2451         gamma = atan2f( matrix->u.m[1][2] / sinb, -matrix->u.m[0][2] / sinb );
2452     }
2453     else
2454     {
2455         alpha = atan2f( matrix->u.m[0][1], matrix->u.m[0][0] );
2456         beta = 0.0f;
2457         gamma = 0.0f;
2458     }
2459
2460     D3DXSHRotateZ(out, order, gamma, in);
2461     rotate_X(temp, order, 1.0f, out);
2462     D3DXSHRotateZ(out, order, beta, temp);
2463     rotate_X(temp, order, -1.0f, out);
2464     D3DXSHRotateZ(out, order, alpha, temp);
2465
2466     return out;
2467 }
2468
2469 FLOAT * WINAPI D3DXSHRotateZ(FLOAT *out, UINT order, FLOAT angle, CONST FLOAT *in)
2470 {
2471     FLOAT c1a, c2a, c3a, c4a, c5a, s1a, s2a, s3a, s4a, s5a;
2472
2473     TRACE("out %p, order %u, angle %f, in %p\n", out, order, angle, in);
2474
2475     c1a = cosf(angle);
2476     s1a = sinf(angle);
2477     out[0] = in[0];
2478     out[1] = c1a * in[1] + s1a * in[3];
2479     out[2] = in[2];
2480     out[3] = c1a * in[3] - s1a * in[1];
2481     if (order <= D3DXSH_MINORDER)
2482         return out;
2483
2484     c2a = cosf(2.0f * angle);
2485     s2a = sinf(2.0f * angle);
2486     out[4] = c2a * in[4] + s2a * in[8];
2487     out[5] = c1a * in[5] + s1a * in[7];
2488     out[6] = in[6];
2489     out[7] = c1a * in[7] - s1a * in[5];
2490     out[8] = c2a * in[8] - s2a * in[4];
2491     if (order == 3)
2492         return out;
2493
2494     c3a = cosf(3.0f * angle);
2495     s3a = sinf(3.0f * angle);
2496     out[9] = c3a * in[9] + s3a * in[15];
2497     out[10] = c2a * in[10] + s2a * in[14];
2498     out[11] = c1a * in[11] + s1a * in[13];
2499     out[12] = in[12];
2500     out[13] = c1a * in[13] - s1a * in[11];
2501     out[14] = c2a * in[14] - s2a * in[10];
2502     out[15] = c3a * in[15] - s3a * in[9];
2503     if (order == 4)
2504         return out;
2505
2506     c4a = cosf(4.0f * angle);
2507     s4a = sinf(4.0f * angle);
2508     out[16] = c4a * in[16] + s4a * in[24];
2509     out[17] = c3a * in[17] + s3a * in[23];
2510     out[18] = c2a * in[18] + s2a * in[22];
2511     out[19] = c1a * in[19] + s1a * in[21];
2512     out[20] = in[20];
2513     out[21] = c1a * in[21] - s1a * in[19];
2514     out[22] = c2a * in[22] - s2a * in[18];
2515     out[23] = c3a * in[23] - s3a * in[17];
2516     out[24] = c4a * in[24] - s4a * in[16];
2517     if (order == 5)
2518         return out;
2519
2520     c5a = cosf(5.0f * angle);
2521     s5a = sinf(5.0f * angle);
2522     out[25] = c5a * in[25] + s5a * in[35];
2523     out[26] = c4a * in[26] + s4a * in[34];
2524     out[27] = c3a * in[27] + s3a * in[33];
2525     out[28] = c2a * in[28] + s2a * in[32];
2526     out[29] = c1a * in[29] + s1a * in[31];
2527     out[30] = in[30];
2528     out[31] = c1a * in[31] - s1a * in[29];
2529     out[32] = c2a * in[32] - s2a * in[28];
2530     out[33] = c3a * in[33] - s3a * in[27];
2531     out[34] = c4a * in[34] - s4a * in[26];
2532     out[35] = c5a * in[35] - s5a * in[25];
2533
2534     return out;
2535 }
2536
2537 FLOAT* WINAPI D3DXSHScale(FLOAT *out, UINT order, CONST FLOAT *a, CONST FLOAT scale)
2538 {
2539     UINT i;
2540
2541     TRACE("out %p, order %u, a %p, scale %f\n", out, order, a, scale);
2542
2543     for (i = 0; i < order * order; i++)
2544         out[i] = a[i] * scale;
2545
2546     return out;
2547 }