gdi.exe16: Don't bother using the register storage class specifier.
[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     D3DXVECTOR2 out;
1592     FLOAT norm;
1593
1594     TRACE("(%p, %p)\n", pout, pv);
1595
1596     norm = D3DXVec2Length(pv);
1597     if ( !norm )
1598     {
1599      out.x = 0.0f;
1600      out.y = 0.0f;
1601     }
1602     else
1603     {
1604      out.x = pv->x / norm;
1605      out.y = pv->y / norm;
1606     }
1607     *pout=out;
1608     return pout;
1609 }
1610
1611 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1612 {
1613     TRACE("(%p, %p, %p)\n", pout, pv, pm);
1614
1615     pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y  + pm->u.m[3][0];
1616     pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y  + pm->u.m[3][1];
1617     pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y  + pm->u.m[3][2];
1618     pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y  + pm->u.m[3][3];
1619     return pout;
1620 }
1621
1622 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1623 {
1624     UINT i;
1625
1626     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1627
1628     for (i = 0; i < elements; ++i) {
1629         D3DXVec2Transform(
1630             (D3DXVECTOR4*)((char*)out + outstride * i),
1631             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1632             matrix);
1633     }
1634     return out;
1635 }
1636
1637 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1638 {
1639     D3DXVECTOR2 v;
1640     FLOAT norm;
1641
1642     TRACE("(%p, %p, %p)\n", pout, pv, pm);
1643
1644     v = *pv;
1645     norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1646
1647     pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1648     pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1649
1650     return pout;
1651 }
1652
1653 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1654 {
1655     UINT i;
1656
1657     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1658
1659     for (i = 0; i < elements; ++i) {
1660         D3DXVec2TransformCoord(
1661             (D3DXVECTOR2*)((char*)out + outstride * i),
1662             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1663             matrix);
1664     }
1665     return out;
1666 }
1667
1668 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1669 {
1670     CONST D3DXVECTOR2 v = *pv;
1671     pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1672     pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1673     return pout;
1674 }
1675
1676 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride, CONST D3DXMATRIX *matrix, UINT elements)
1677 {
1678     UINT i;
1679
1680     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1681
1682     for (i = 0; i < elements; ++i) {
1683         D3DXVec2TransformNormal(
1684             (D3DXVECTOR2*)((char*)out + outstride * i),
1685             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1686             matrix);
1687     }
1688     return out;
1689 }
1690
1691 /*_________________D3DXVec3_____________________*/
1692
1693 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1694 {
1695     TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pv1, pv2, pv3, f, g);
1696
1697     pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1698     pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1699     pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1700     return pout;
1701 }
1702
1703 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
1704 {
1705     TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv0, pv1, pv2, pv3, s);
1706
1707     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);
1708     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);
1709     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);
1710     return pout;
1711 }
1712
1713 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
1714 {
1715     FLOAT h1, h2, h3, h4;
1716
1717     TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv1, pt1, pv2, pt2, s);
1718
1719     h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1720     h2 = s * s * s - 2.0f * s * s + s;
1721     h3 = -2.0f * s * s * s + 3.0f * s * s;
1722     h4 = s * s * s - s * s;
1723
1724     pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1725     pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1726     pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1727     return pout;
1728 }
1729
1730 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
1731 {
1732     D3DXVECTOR3 out;
1733     FLOAT norm;
1734
1735     TRACE("(%p, %p)\n", pout, pv);
1736
1737     norm = D3DXVec3Length(pv);
1738     if ( !norm )
1739     {
1740      out.x = 0.0f;
1741      out.y = 0.0f;
1742      out.z = 0.0f;
1743     }
1744     else
1745     {
1746      out.x = pv->x / norm;
1747      out.y = pv->y / norm;
1748      out.z = pv->z / norm;
1749     }
1750     *pout = out;
1751     return pout;
1752 }
1753
1754 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1755 {
1756     D3DXMATRIX m;
1757     D3DXVECTOR3 out;
1758
1759     TRACE("(%p, %p, %p, %p, %p, %p)\n", pout, pv, pviewport, pprojection, pview, pworld);
1760
1761     D3DXMatrixMultiply(&m, pworld, pview);
1762     D3DXMatrixMultiply(&m, &m, pprojection);
1763     D3DXVec3TransformCoord(&out, pv, &m);
1764     out.x = pviewport->X +  ( 1.0f + out.x ) * pviewport->Width / 2.0f;
1765     out.y = pviewport->Y +  ( 1.0f - out.y ) * pviewport->Height / 2.0f;
1766     out.z = pviewport->MinZ + out.z * ( pviewport->MaxZ - pviewport->MinZ );
1767     *pout = out;
1768     return pout;
1769 }
1770
1771 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)
1772 {
1773     UINT i;
1774
1775     TRACE("(%p, %u, %p, %u, %p, %p, %p, %p, %u)\n", out, outstride, in, instride, viewport, projection, view, world, elements);
1776
1777     for (i = 0; i < elements; ++i) {
1778         D3DXVec3Project(
1779             (D3DXVECTOR3*)((char*)out + outstride * i),
1780             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1781             viewport, projection, view, world);
1782     }
1783     return out;
1784 }
1785
1786 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1787 {
1788     TRACE("(%p, %p, %p)\n", pout, pv, pm);
1789
1790     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];
1791     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];
1792     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];
1793     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];
1794     return pout;
1795 }
1796
1797 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1798 {
1799     UINT i;
1800
1801     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1802
1803     for (i = 0; i < elements; ++i) {
1804         D3DXVec3Transform(
1805             (D3DXVECTOR4*)((char*)out + outstride * i),
1806             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1807             matrix);
1808     }
1809     return out;
1810 }
1811
1812 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1813 {
1814     D3DXVECTOR3 out;
1815     FLOAT norm;
1816
1817     TRACE("(%p, %p, %p)\n", pout, pv, pm);
1818
1819     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];
1820
1821     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;
1822     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;
1823     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;
1824
1825     *pout = out;
1826
1827     return pout;
1828 }
1829
1830 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1831 {
1832     UINT i;
1833
1834     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1835
1836     for (i = 0; i < elements; ++i) {
1837         D3DXVec3TransformCoord(
1838             (D3DXVECTOR3*)((char*)out + outstride * i),
1839             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1840             matrix);
1841     }
1842     return out;
1843 }
1844
1845 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1846 {
1847     CONST D3DXVECTOR3 v = *pv;
1848
1849     TRACE("(%p, %p, %p)\n", pout, pv, pm);
1850
1851     pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1852     pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1853     pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1854     return pout;
1855
1856 }
1857
1858 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1859 {
1860     UINT i;
1861
1862     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1863
1864     for (i = 0; i < elements; ++i) {
1865         D3DXVec3TransformNormal(
1866             (D3DXVECTOR3*)((char*)out + outstride * i),
1867             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1868             matrix);
1869     }
1870     return out;
1871 }
1872
1873 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1874 {
1875     D3DXMATRIX m;
1876     D3DXVECTOR3 out;
1877
1878     TRACE("(%p, %p, %p, %p, %p, %p)\n", pout, pv, pviewport, pprojection, pview, pworld);
1879
1880     if (pworld) {
1881         D3DXMatrixMultiply(&m, pworld, pview);
1882         D3DXMatrixMultiply(&m, &m, pprojection);
1883     } else {
1884         D3DXMatrixMultiply(&m, pview, pprojection);
1885     }
1886     D3DXMatrixInverse(&m, NULL, &m);
1887     out.x = 2.0f * ( pv->x - pviewport->X ) / pviewport->Width - 1.0f;
1888     out.y = 1.0f - 2.0f * ( pv->y - pviewport->Y ) / pviewport->Height;
1889     out.z = ( pv->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
1890     D3DXVec3TransformCoord(&out, &out, &m);
1891     *pout = out;
1892     return pout;
1893 }
1894
1895 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)
1896 {
1897     UINT i;
1898
1899     TRACE("(%p, %u, %p, %u, %p, %p, %p, %p, %u)\n", out, outstride, in, instride, viewport, projection, view, world, elements);
1900
1901     for (i = 0; i < elements; ++i) {
1902         D3DXVec3Unproject(
1903             (D3DXVECTOR3*)((char*)out + outstride * i),
1904             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1905             viewport, projection, view, world);
1906     }
1907     return out;
1908 }
1909
1910 /*_________________D3DXVec4_____________________*/
1911
1912 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
1913 {
1914     TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pv1, pv2, pv3, f, g);
1915
1916     pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1917     pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1918     pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1919     pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
1920     return pout;
1921 }
1922
1923 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv0, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT s)
1924 {
1925     TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv0, pv1, pv2, pv3, s);
1926
1927     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);
1928     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);
1929     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);
1930     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);
1931     return pout;
1932 }
1933
1934 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
1935 {
1936     D3DXVECTOR4 out;
1937
1938     TRACE("(%p, %p, %p, %p)\n", pout, pv1, pv2, pv3);
1939
1940     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);
1941     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));
1942     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);
1943     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));
1944     *pout = out;
1945     return pout;
1946 }
1947
1948 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pt1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pt2, FLOAT s)
1949 {
1950     FLOAT h1, h2, h3, h4;
1951
1952     TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv1, pt1, pv2, pt2, s);
1953
1954     h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1955     h2 = s * s * s - 2.0f * s * s + s;
1956     h3 = -2.0f * s * s * s + 3.0f * s * s;
1957     h4 = s * s * s - s * s;
1958
1959     pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1960     pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1961     pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1962     pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
1963     return pout;
1964 }
1965
1966 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
1967 {
1968     D3DXVECTOR4 out;
1969     FLOAT norm;
1970
1971     TRACE("(%p, %p)\n", pout, pv);
1972
1973     norm = D3DXVec4Length(pv);
1974
1975     out.x = pv->x / norm;
1976     out.y = pv->y / norm;
1977     out.z = pv->z / norm;
1978     out.w = pv->w / norm;
1979
1980     *pout = out;
1981     return pout;
1982 }
1983
1984 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
1985 {
1986     D3DXVECTOR4 out;
1987
1988     TRACE("(%p, %p, %p)\n", pout, pv, pm);
1989
1990     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;
1991     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;
1992     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;
1993     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;
1994     *pout = out;
1995     return pout;
1996 }
1997
1998 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1999 {
2000     UINT i;
2001
2002     TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
2003
2004     for (i = 0; i < elements; ++i) {
2005         D3DXVec4Transform(
2006             (D3DXVECTOR4*)((char*)out + outstride * i),
2007             (CONST D3DXVECTOR4*)((const char*)in + instride * i),
2008             matrix);
2009     }
2010     return out;
2011 }
2012
2013 static inline unsigned short float_32_to_16(const float in)
2014 {
2015     int exp = 0, origexp;
2016     float tmp = fabs(in);
2017     int sign = (copysignf(1, in) < 0);
2018     unsigned int mantissa;
2019     unsigned short ret;
2020
2021     /* Deal with special numbers */
2022     if (isinf(in)) return (sign ? 0xffff : 0x7fff);
2023     if (isnan(in)) return (sign ? 0xffff : 0x7fff);
2024     if (in == 0.0f) return (sign ? 0x8000 : 0x0000);
2025
2026     if (tmp < powf(2, 10))
2027     {
2028         do
2029         {
2030             tmp *= 2.0f;
2031             exp--;
2032         } while (tmp < powf(2, 10));
2033     }
2034     else if (tmp >= powf(2, 11))
2035     {
2036         do
2037         {
2038             tmp /= 2.0f;
2039             exp++;
2040         } while (tmp >= powf(2, 11));
2041     }
2042
2043     exp += 10;  /* Normalize the mantissa */
2044     exp += 15;  /* Exponent is encoded with excess 15 */
2045
2046     origexp = exp;
2047
2048     mantissa = (unsigned int) tmp;
2049     if ((tmp - mantissa == 0.5f && mantissa % 2 == 1) || /* round half to even */
2050         (tmp - mantissa > 0.5f))
2051     {
2052         mantissa++; /* round to nearest, away from zero */
2053     }
2054     if (mantissa == 2048)
2055     {
2056         mantissa = 1024;
2057         exp++;
2058     }
2059
2060     if (exp > 31)
2061     {
2062         /* too big */
2063         ret = 0x7fff; /* INF */
2064     }
2065     else if (exp <= 0)
2066     {
2067         unsigned int rounding = 0;
2068
2069         /* Denormalized half float */
2070
2071         /* return 0x0000 (=0.0) for numbers too small to represent in half floats */
2072         if (exp < -11)
2073             return (sign ? 0x8000 : 0x0000);
2074
2075         exp = origexp;
2076
2077         /* the 13 extra bits from single precision are used for rounding */
2078         mantissa = (unsigned int)(tmp * powf(2, 13));
2079         mantissa >>= 1 - exp; /* denormalize */
2080
2081         mantissa -= ~(mantissa >> 13) & 1; /* round half to even */
2082         /* remove 13 least significant bits to get half float precision */
2083         mantissa >>= 12;
2084         rounding = mantissa & 1;
2085         mantissa >>= 1;
2086
2087         ret = mantissa + rounding;
2088     }
2089     else
2090     {
2091         ret = (exp << 10) | (mantissa & 0x3ff);
2092     }
2093
2094     ret |= ((sign ? 1 : 0) << 15); /* Add the sign */
2095     return ret;
2096 }
2097
2098 D3DXFLOAT16 *WINAPI D3DXFloat32To16Array(D3DXFLOAT16 *pout, CONST FLOAT *pin, UINT n)
2099 {
2100     unsigned int i;
2101
2102     TRACE("(%p, %p, %u)\n", pout, pin, n);
2103
2104     for (i = 0; i < n; ++i)
2105     {
2106         pout[i].value = float_32_to_16(pin[i]);
2107     }
2108
2109     return pout;
2110 }
2111
2112 /* Native d3dx9's D3DXFloat16to32Array lacks support for NaN and Inf. Specifically, e = 16 is treated as a
2113  * regular number - e.g., 0x7fff is converted to 131008.0 and 0xffff to -131008.0. */
2114 static inline float float_16_to_32(const unsigned short in)
2115 {
2116     const unsigned short s = (in & 0x8000);
2117     const unsigned short e = (in & 0x7C00) >> 10;
2118     const unsigned short m = in & 0x3FF;
2119     const float sgn = (s ? -1.0f : 1.0f);
2120
2121     if (e == 0)
2122     {
2123         if (m == 0) return sgn * 0.0f; /* +0.0 or -0.0 */
2124         else return sgn * powf(2, -14.0f) * (m / 1024.0f);
2125     }
2126     else
2127     {
2128         return sgn * powf(2, e - 15.0f) * (1.0f + (m / 1024.0f));
2129     }
2130 }
2131
2132 FLOAT *WINAPI D3DXFloat16To32Array(FLOAT *pout, CONST D3DXFLOAT16 *pin, UINT n)
2133 {
2134     unsigned int i;
2135
2136     TRACE("(%p, %p, %u)\n", pout, pin, n);
2137
2138     for (i = 0; i < n; ++i)
2139     {
2140         pout[i] = float_16_to_32(pin[i].value);
2141     }
2142
2143     return pout;
2144 }
2145
2146 /*_________________D3DXSH________________*/
2147
2148 FLOAT* WINAPI D3DXSHAdd(FLOAT *out, UINT order, const FLOAT *a, const FLOAT *b)
2149 {
2150     UINT i;
2151
2152     TRACE("out %p, order %u, a %p, b %p\n", out, order, a, b);
2153
2154     for (i = 0; i < order * order; i++)
2155         out[i] = a[i] + b[i];
2156
2157     return out;
2158 }
2159
2160 FLOAT WINAPI D3DXSHDot(UINT order, CONST FLOAT *a, CONST FLOAT *b)
2161 {
2162     FLOAT s;
2163     UINT i;
2164
2165     TRACE("order %u, a %p, b %p\n", order, a, b);
2166
2167     s = a[0] * b[0];
2168     for (i = 1; i < order * order; i++)
2169         s += a[i] * b[i];
2170
2171     return s;
2172 }
2173
2174 FLOAT* WINAPI D3DXSHEvalDirection(FLOAT *out, UINT order, CONST D3DXVECTOR3 *dir)
2175 {
2176
2177     TRACE("(%p, %u, %p)\n", out, order, dir);
2178
2179     if ( (order < D3DXSH_MINORDER) || (order > D3DXSH_MAXORDER) )
2180         return out;
2181
2182     out[0] = 0.5f / sqrt(D3DX_PI);
2183     out[1] = -0.5f / sqrt(D3DX_PI / 3.0f) * dir->y;
2184     out[2] = 0.5f / sqrt(D3DX_PI / 3.0f) * dir->z;
2185     out[3] = -0.5f / sqrt(D3DX_PI / 3.0f) * dir->x;
2186     if ( order == 2 )
2187         return out;
2188
2189     out[4] = 0.5f / sqrt(D3DX_PI / 15.0f) * dir->x * dir->y;
2190     out[5] = -0.5f / sqrt(D3DX_PI / 15.0f) * dir->y * dir->z;
2191     out[6] = 0.25f / sqrt(D3DX_PI / 5.0f) * ( 3.0f * dir->z * dir->z - 1.0f );
2192     out[7] = -0.5f / sqrt(D3DX_PI / 15.0f) * dir->x * dir->z;
2193     out[8] = 0.25f / sqrt(D3DX_PI / 15.0f) * ( dir->x * dir->x - dir->y * dir->y );
2194     if ( order == 3 )
2195         return out;
2196
2197     out[9] = -sqrt(70.0f / D3DX_PI) / 8.0f * dir->y * (3.0f * dir->x * dir->x - dir->y * dir->y );
2198     out[10] = sqrt(105.0f / D3DX_PI) / 2.0f * dir->x * dir->y * dir->z;
2199     out[11] = -sqrt(42.0 / D3DX_PI) / 8.0f * dir->y * ( -1.0f + 5.0f * dir->z * dir->z );
2200     out[12] = sqrt(7.0f / D3DX_PI) / 4.0f * dir->z * ( 5.0f * dir->z * dir->z - 3.0f );
2201     out[13] = sqrt(42.0 / D3DX_PI) / 8.0f * dir->x * ( 1.0f - 5.0f * dir->z * dir->z );
2202     out[14] = sqrt(105.0f / D3DX_PI) / 4.0f * dir->z * ( dir->x * dir->x - dir->y * dir->y );
2203     out[15] = -sqrt(70.0f / D3DX_PI) / 8.0f * dir->x * ( dir->x * dir->x - 3.0f * dir->y * dir->y );
2204     if ( order == 4 )
2205         return out;
2206
2207     out[16] = 0.75f * sqrt(35.0f / D3DX_PI) * dir->x * dir->y * (dir->x * dir->x - dir->y * dir->y );
2208     out[17] = 3.0f * dir->z * out[9];
2209     out[18] = 0.75f * sqrt(5.0f / D3DX_PI) * dir->x * dir->y * ( 7.0f * dir->z * dir->z - 1.0f );
2210     out[19] = 0.375f * sqrt(10.0f / D3DX_PI) * dir->y * dir->z * ( 3.0f - 7.0f * dir->z * dir->z );
2211     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 );
2212     out[21] = 0.375f * sqrt(10.0f / D3DX_PI) * dir->x * dir->z * ( 3.0f - 7.0f * dir->z * dir->z );
2213     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);
2214     out[23] = 3.0 * dir->z * out[15];
2215     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 );
2216     if ( order == 5 )
2217         return out;
2218
2219     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 );
2220     out[26] = 0.75f * sqrt(385.0f / D3DX_PI) * dir->x * dir->y * dir->z * ( dir->x * dir->x - dir->y * dir->y );
2221     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 );
2222     out[28] = sqrt(1155.0f / D3DX_PI) / 4.0f * dir->x * dir->y * dir->z * ( 3.0f * dir->z * dir->z - 1.0f);
2223     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 );
2224     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 );
2225     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 );
2226     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 );
2227     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 );
2228     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 );
2229     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 );
2230
2231     return out;
2232 }
2233
2234 FLOAT* WINAPI D3DXSHMultiply2(FLOAT *out, CONST FLOAT *a, CONST FLOAT *b)
2235 {
2236     FLOAT ta, tb;
2237
2238     TRACE("(%p, %p, %p)\n", out, a, b);
2239
2240     ta = 0.28209479f * a[0];
2241     tb = 0.28209479f * b[0];
2242
2243     out[0]= 0.28209479f * D3DXSHDot(2, a, b);
2244     out[1] = ta * b[1] + tb * a[1];
2245     out[2] = ta * b[2] + tb * a[2];
2246     out[3] = ta * b[3] + tb * a[3];
2247
2248     return out;
2249 }
2250
2251 FLOAT* WINAPI D3DXSHMultiply3(FLOAT *out, CONST FLOAT *a, CONST FLOAT *b)
2252 {
2253     FLOAT t, ta, tb;
2254
2255     TRACE("(%p, %p, %p)\n", out, a, b);
2256
2257     out[0]= 0.28209479f * a[0] * b[0];
2258
2259     ta = 0.28209479f * a[0] - 0.12615662f * a[6] - 0.21850968f * a[8];
2260     tb = 0.28209479f * b[0] - 0.12615662f * b[6] - 0.21850968f * b[8];
2261     out[1] = ta * b[1] + tb * a[1];
2262     t = a[1] * b[1];
2263     out[0] += 0.28209479f * t;
2264     out[6] = -0.12615662f * t;
2265     out[8] = -0.21850968f * t;
2266
2267     ta = 0.21850968f * a[5];
2268     tb = 0.21850968f * b[5];
2269     out[1] += ta * b[2] + tb * a[2];
2270     out[2] = ta * b[1] + tb * a[1];
2271     t = a[1] * b[2] +a[2] * b[1];
2272     out[5] = 0.21850968f * t;
2273
2274     ta = 0.21850968f * a[4];
2275     tb = 0.21850968f * b[4];
2276     out[1] += ta * b[3] + tb * a[3];
2277     out[3]  = ta * b[1] + tb * a[1];
2278     t = a[1] * b[3] + a[3] * b[1];
2279     out[4] = 0.21850968f * t;
2280
2281     ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2282     tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2283     out[2] += ta * b[2] + tb * a[2];
2284     t = a[2] * b[2];
2285     out[0] += 0.28209480f * t;
2286     out[6] += 0.25231326f * t;
2287
2288     ta = 0.21850969f * a[7];
2289     tb = 0.21850969f * b[7];
2290     out[2] += ta * b[3] + tb * a[3];
2291     out[3] += ta * b[2] + tb * a[2];
2292     t = a[2] * b[3] + a[3] * b[2];
2293     out[7] = 0.21850969f * t;
2294
2295     ta = 0.28209479f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2296     tb = 0.28209479f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2297     out[3] += ta * b[3] + tb * a[3];
2298     t = a[3] * b[3];
2299     out[0] += 0.28209479f * t;
2300     out[6] -= 0.12615663f * t;
2301     out[8] += 0.21850969f * t;
2302
2303     ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2304     tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2305     out[4] += ta * b[4] + tb * a[4];
2306     t = a[4] * b[4];
2307     out[0] += 0.28209479f * t;
2308     out[6] -= 0.18022375f * t;
2309
2310     ta = 0.15607835f * a[7];
2311     tb = 0.15607835f * b[7];
2312     out[4] += ta * b[5] + tb * a[5];
2313     out[5] += ta * b[4] + tb * a[4];
2314     t = a[4] * b[5] + a[5] * b[4];
2315     out[7] += 0.15607834f * t;
2316
2317     ta = 0.28209479f * a[0] + 0.09011186 * a[6] - 0.15607835f * a[8];
2318     tb = 0.28209479f * b[0] + 0.09011186 * b[6] - 0.15607835f * b[8];
2319     out[5] += ta * b[5] + tb * a[5];
2320     t = a[5] * b[5];
2321     out[0] += 0.28209479f * t;
2322     out[6] += 0.09011186f * t;
2323     out[8] -= 0.15607835f * t;
2324
2325     ta = 0.28209480f * a[0];
2326     tb = 0.28209480f * b[0];
2327     out[6] += ta * b[6] + tb * a[6];
2328     t = a[6] * b[6];
2329     out[0] += 0.28209480f * t;
2330     out[6] += 0.18022376f * t;
2331
2332     ta = 0.28209479f * a[0] + 0.09011186 * a[6] + 0.15607835f * a[8];
2333     tb = 0.28209479f * b[0] + 0.09011186 * b[6] + 0.15607835f * b[8];
2334     out[7] += ta * b[7] + tb * a[7];
2335     t = a[7] * b[7];
2336     out[0] += 0.28209479f * t;
2337     out[6] += 0.09011186f * t;
2338     out[8] += 0.15607835f * t;
2339
2340     ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2341     tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2342     out[8] += ta * b[8] + tb * a[8];
2343     t = a[8] * b[8];
2344     out[0] += 0.28209479f * t;
2345     out[6] -= 0.18022375f * t;
2346
2347     return out;
2348 }
2349
2350 FLOAT* WINAPI D3DXSHRotateZ(FLOAT *out, UINT order, FLOAT angle, CONST FLOAT *in)
2351 {
2352     FLOAT c1a, c2a, c3a, c4a, c5a, s1a, s2a, s3a, s4a, s5a;
2353
2354     TRACE("%p, %u, %f, %p)\n", out, order, angle, in);
2355
2356     c1a = cos( angle );
2357     c2a = cos( 2.0f * angle );
2358     c3a = cos( 3.0f * angle );
2359     c4a = cos( 4.0f * angle );
2360     c5a = cos( 5.0f * angle );
2361     s1a = sin( angle );
2362     s2a = sin( 2.0f * angle );
2363     s3a = sin( 3.0f * angle );
2364     s4a = sin( 4.0f * angle );
2365     s5a = sin( 5.0f * angle );
2366
2367     out[0] = in[0];
2368     out[1] = c1a * in[1] + s1a * in[3];
2369     out[2] = in[2];
2370     out[3] =  c1a * in[3] - s1a * in[1];
2371     if ( order <= D3DXSH_MINORDER )
2372         return out;
2373
2374     out[4] = c2a * in[4] + s2a * in[8];
2375     out[5] = c1a * in[5] + s1a * in[7];
2376     out[6] = in[6];
2377     out[7] = c1a * in[7] - s1a * in[5];
2378     out[8] = c2a * in[8] - s2a * in[4];
2379     if ( order == 3 )
2380         return out;
2381
2382     out[9] = c3a * in[9] + s3a * in[15];
2383     out[10] = c2a * in[10] + s2a * in[14];
2384     out[11] = c1a * in[11] + s1a * in[13];
2385     out[12] = in[12];
2386     out[13] = c1a * in[13] - s1a * in[11];
2387     out[14] = c2a * in[14] - s2a * in[10];
2388     out[15] = c3a * in[15] - s3a * in[9];
2389     if ( order == 4 )
2390         return out;
2391
2392     out[16] = c4a * in[16] + s4a * in[24];
2393     out[17] = c3a * in[17] + s3a * in[23];
2394     out[18] = c2a * in[18] + s2a * in[22];
2395     out[19] = c1a * in[19] + s1a * in[21];
2396     out[20] = in[20];
2397     out[21] = c1a * in[21] - s1a * in[19];
2398     out[22] = c2a * in[22] - s2a * in[18];
2399     out[23] = c3a * in[23] - s3a * in[17];
2400     out[24] = c4a * in[24] - s4a * in[16];
2401     if ( order == 5 )
2402         return out;
2403
2404     out[25] = c5a * in[25] + s5a * in[35];
2405     out[26] = c4a * in[26] + s4a * in[34];
2406     out[27] = c3a * in[27] + s3a * in[33];
2407     out[28] = c2a * in[28] + s2a * in[32];
2408     out[29] = c1a * in[29] + s1a * in[31];
2409     out[30] = in[30];
2410     out[31] = c1a * in[31] - s1a * in[29];
2411     out[32] = c2a * in[32] - s2a * in[28];
2412     out[33] = c3a * in[33] - s3a * in[27];
2413     out[34] = c4a * in[34] - s4a * in[26];
2414     out[35] = c5a * in[35] - s5a * in[25];
2415
2416     return out;
2417 }
2418
2419 FLOAT* WINAPI D3DXSHScale(FLOAT *out, UINT order, CONST FLOAT *a, CONST FLOAT scale)
2420 {
2421     UINT i;
2422
2423     TRACE("out %p, order %u, a %p, scale %f\n", out, order, a, scale);
2424
2425     for (i = 0; i < order * order; i++)
2426         out[i] = a[i] * scale;
2427
2428     return out;
2429 }