d3dx9: Fix return values.
[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 "windef.h"
29 #include "wingdi.h"
30 #include "d3dx9_36_private.h"
31
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
35
36 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl;
37
38 typedef struct ID3DXMatrixStackImpl
39 {
40   ID3DXMatrixStack ID3DXMatrixStack_iface;
41   LONG ref;
42
43   unsigned int current;
44   unsigned int stack_size;
45   D3DXMATRIX *stack;
46 } ID3DXMatrixStackImpl;
47
48
49 /*_________________D3DXColor____________________*/
50
51 D3DXCOLOR* WINAPI D3DXColorAdjustContrast(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
52 {
53     pout->r = 0.5f + s * (pc->r - 0.5f);
54     pout->g = 0.5f + s * (pc->g - 0.5f);
55     pout->b = 0.5f + s * (pc->b - 0.5f);
56     pout->a = pc->a;
57     return pout;
58 }
59
60 D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
61 {
62     FLOAT grey;
63
64     grey = pc->r * 0.2125f + pc->g * 0.7154f + pc->b * 0.0721f;
65     pout->r = grey + s * (pc->r - grey);
66     pout->g = grey + s * (pc->g - grey);
67     pout->b = grey + s * (pc->b - grey);
68     pout->a = pc->a;
69     return pout;
70 }
71
72 /*_________________Misc__________________________*/
73
74 FLOAT WINAPI D3DXFresnelTerm(FLOAT costheta, FLOAT refractionindex)
75 {
76     FLOAT a, d, g, result;
77
78     g = sqrt(refractionindex * refractionindex + costheta * costheta - 1.0f);
79     a = g + costheta;
80     d = g - costheta;
81     result = ( costheta * a - 1.0f ) * ( costheta * a - 1.0f ) / ( ( costheta * d + 1.0f ) * ( costheta * d + 1.0f ) ) + 1.0f;
82     result = result * 0.5f * d * d / ( a * a );
83     return result;
84 }
85
86 /*_________________D3DXMatrix____________________*/
87
88 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR3 *rotationcenter, CONST D3DXQUATERNION *rotation, CONST D3DXVECTOR3 *translation)
89 {
90     D3DXMATRIX m1, m2, m3, m4, m5;
91
92     D3DXMatrixScaling(&m1, scaling, scaling, scaling);
93
94     if ( !rotationcenter )
95     {
96         D3DXMatrixIdentity(&m2);
97         D3DXMatrixIdentity(&m4);
98     }
99     else
100     {
101         D3DXMatrixTranslation(&m2, -rotationcenter->x, -rotationcenter->y, -rotationcenter->z);
102         D3DXMatrixTranslation(&m4, rotationcenter->x, rotationcenter->y, rotationcenter->z);
103     }
104
105     if ( !rotation ) D3DXMatrixIdentity(&m3);
106     else D3DXMatrixRotationQuaternion(&m3, rotation);
107
108     if ( !translation ) D3DXMatrixIdentity(&m5);
109     else D3DXMatrixTranslation(&m5, translation->x, translation->y, translation->z);
110
111     D3DXMatrixMultiply(&m1, &m1, &m2);
112     D3DXMatrixMultiply(&m1, &m1, &m3);
113     D3DXMatrixMultiply(&m1, &m1, &m4);
114     D3DXMatrixMultiply(pout, &m1, &m5);
115     return pout;
116 }
117
118 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation2D(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR2 *protationcenter, FLOAT rotation, CONST D3DXVECTOR2 *ptranslation)
119 {
120     D3DXMATRIX m1, m2, m3, m4, m5;
121     D3DXQUATERNION rot;
122     D3DXVECTOR3 rot_center, trans;
123
124     rot.w=cos(rotation/2.0f);
125     rot.x=0.0f;
126     rot.y=0.0f;
127     rot.z=sin(rotation/2.0f);
128
129     if ( protationcenter )
130     {
131         rot_center.x=protationcenter->x;
132         rot_center.y=protationcenter->y;
133         rot_center.z=0.0f;
134     }
135     else
136     {
137         rot_center.x=0.0f;
138         rot_center.y=0.0f;
139         rot_center.z=0.0f;
140     }
141
142     if ( ptranslation )
143     {
144         trans.x=ptranslation->x;
145         trans.y=ptranslation->y;
146         trans.z=0.0f;
147     }
148     else
149     {
150         trans.x=0.0f;
151         trans.y=0.0f;
152         trans.z=0.0f;
153     }
154
155     D3DXMatrixScaling(&m1, scaling, scaling, 1.0f);
156     D3DXMatrixTranslation(&m2, -rot_center.x, -rot_center.y, -rot_center.z);
157     D3DXMatrixTranslation(&m4, rot_center.x, rot_center.y, rot_center.z);
158     D3DXMatrixRotationQuaternion(&m3, &rot);
159     D3DXMatrixTranslation(&m5, trans.x, trans.y, trans.z);
160
161     D3DXMatrixMultiply(&m1, &m1, &m2);
162     D3DXMatrixMultiply(&m1, &m1, &m3);
163     D3DXMatrixMultiply(&m1, &m1, &m4);
164     D3DXMatrixMultiply(pout, &m1, &m5);
165
166     return pout;
167 }
168
169 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, CONST D3DXMATRIX *pm)
170 {
171     D3DXMATRIX normalized;
172     D3DXVECTOR3 vec;
173
174     /*Compute the scaling part.*/
175     vec.x=pm->u.m[0][0];
176     vec.y=pm->u.m[0][1];
177     vec.z=pm->u.m[0][2];
178     poutscale->x=D3DXVec3Length(&vec);
179
180     vec.x=pm->u.m[1][0];
181     vec.y=pm->u.m[1][1];
182     vec.z=pm->u.m[1][2];
183     poutscale->y=D3DXVec3Length(&vec);
184
185     vec.x=pm->u.m[2][0];
186     vec.y=pm->u.m[2][1];
187     vec.z=pm->u.m[2][2];
188     poutscale->z=D3DXVec3Length(&vec);
189
190     /*Compute the translation part.*/
191     pouttranslation->x=pm->u.m[3][0];
192     pouttranslation->y=pm->u.m[3][1];
193     pouttranslation->z=pm->u.m[3][2];
194
195     /*Let's calculate the rotation now*/
196     if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) ) return D3DERR_INVALIDCALL;
197
198     normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
199     normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
200     normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
201     normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
202     normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
203     normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
204     normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
205     normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
206     normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
207
208     D3DXQuaternionRotationMatrix(poutrotation,&normalized);
209     return S_OK;
210 }
211
212 FLOAT WINAPI D3DXMatrixDeterminant(CONST D3DXMATRIX *pm)
213 {
214     D3DXVECTOR4 minor, v1, v2, v3;
215     FLOAT det;
216
217     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];
218     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];
219     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];
220     D3DXVec4Cross(&minor, &v1, &v2, &v3);
221     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);
222     return det;
223 }
224
225 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, CONST D3DXMATRIX *pm)
226 {
227     int a, i, j;
228     D3DXMATRIX out;
229     D3DXVECTOR4 v, vec[3];
230     FLOAT det;
231
232     det = D3DXMatrixDeterminant(pm);
233     if ( !det ) return NULL;
234     if ( pdeterminant ) *pdeterminant = det;
235     for (i=0; i<4; i++)
236     {
237         for (j=0; j<4; j++)
238         {
239             if (j != i )
240             {
241                 a = j;
242                 if ( j > i ) a = a-1;
243                 vec[a].x = pm->u.m[j][0];
244                 vec[a].y = pm->u.m[j][1];
245                 vec[a].z = pm->u.m[j][2];
246                 vec[a].w = pm->u.m[j][3];
247             }
248         }
249     D3DXVec4Cross(&v, &vec[0], &vec[1], &vec[2]);
250     out.u.m[0][i] = pow(-1.0f, i) * v.x / det;
251     out.u.m[1][i] = pow(-1.0f, i) * v.y / det;
252     out.u.m[2][i] = pow(-1.0f, i) * v.z / det;
253     out.u.m[3][i] = pow(-1.0f, i) * v.w / det;
254    }
255
256    *pout = out;
257    return pout;
258 }
259
260 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
261 {
262     D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
263
264     D3DXVec3Subtract(&vec2, pat, peye);
265     D3DXVec3Normalize(&vec, &vec2);
266     D3DXVec3Cross(&right, pup, &vec);
267     D3DXVec3Cross(&up, &vec, &right);
268     D3DXVec3Normalize(&rightn, &right);
269     D3DXVec3Normalize(&upn, &up);
270     pout->u.m[0][0] = rightn.x;
271     pout->u.m[1][0] = rightn.y;
272     pout->u.m[2][0] = rightn.z;
273     pout->u.m[3][0] = -D3DXVec3Dot(&rightn,peye);
274     pout->u.m[0][1] = upn.x;
275     pout->u.m[1][1] = upn.y;
276     pout->u.m[2][1] = upn.z;
277     pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
278     pout->u.m[0][2] = vec.x;
279     pout->u.m[1][2] = vec.y;
280     pout->u.m[2][2] = vec.z;
281     pout->u.m[3][2] = -D3DXVec3Dot(&vec, peye);
282     pout->u.m[0][3] = 0.0f;
283     pout->u.m[1][3] = 0.0f;
284     pout->u.m[2][3] = 0.0f;
285     pout->u.m[3][3] = 1.0f;
286     return pout;
287 }
288
289 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
290 {
291     D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
292
293     D3DXVec3Subtract(&vec2, pat, peye);
294     D3DXVec3Normalize(&vec, &vec2);
295     D3DXVec3Cross(&right, pup, &vec);
296     D3DXVec3Cross(&up, &vec, &right);
297     D3DXVec3Normalize(&rightn, &right);
298     D3DXVec3Normalize(&upn, &up);
299     pout->u.m[0][0] = -rightn.x;
300     pout->u.m[1][0] = -rightn.y;
301     pout->u.m[2][0] = -rightn.z;
302     pout->u.m[3][0] = D3DXVec3Dot(&rightn,peye);
303     pout->u.m[0][1] = upn.x;
304     pout->u.m[1][1] = upn.y;
305     pout->u.m[2][1] = upn.z;
306     pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
307     pout->u.m[0][2] = -vec.x;
308     pout->u.m[1][2] = -vec.y;
309     pout->u.m[2][2] = -vec.z;
310     pout->u.m[3][2] = D3DXVec3Dot(&vec, peye);
311     pout->u.m[0][3] = 0.0f;
312     pout->u.m[1][3] = 0.0f;
313     pout->u.m[2][3] = 0.0f;
314     pout->u.m[3][3] = 1.0f;
315     return pout;
316 }
317
318 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
319 {
320     D3DXMATRIX out;
321     int i,j;
322
323     for (i=0; i<4; i++)
324     {
325         for (j=0; j<4; j++)
326         {
327             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];
328         }
329     }
330
331     *pout = out;
332     return pout;
333 }
334
335 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
336 {
337     D3DXMatrixMultiply(pout, pm1, pm2);
338     D3DXMatrixTranspose(pout, pout);
339     return pout;
340 }
341
342 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
343 {
344     D3DXMatrixIdentity(pout);
345     pout->u.m[0][0] = 2.0f / w;
346     pout->u.m[1][1] = 2.0f / h;
347     pout->u.m[2][2] = 1.0f / (zf - zn);
348     pout->u.m[3][2] = zn / (zn - zf);
349     return pout;
350 }
351
352 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
353 {
354     D3DXMatrixIdentity(pout);
355     pout->u.m[0][0] = 2.0f / (r - l);
356     pout->u.m[1][1] = 2.0f / (t - b);
357     pout->u.m[2][2] = 1.0f / (zf -zn);
358     pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
359     pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
360     pout->u.m[3][2] = zn / (zn -zf);
361     return pout;
362 }
363
364 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
365 {
366     D3DXMatrixIdentity(pout);
367     pout->u.m[0][0] = 2.0f / (r - l);
368     pout->u.m[1][1] = 2.0f / (t - b);
369     pout->u.m[2][2] = 1.0f / (zn -zf);
370     pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
371     pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
372     pout->u.m[3][2] = zn / (zn -zf);
373     return pout;
374 }
375
376 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
377 {
378     D3DXMatrixIdentity(pout);
379     pout->u.m[0][0] = 2.0f / w;
380     pout->u.m[1][1] = 2.0f / h;
381     pout->u.m[2][2] = 1.0f / (zn - zf);
382     pout->u.m[3][2] = zn / (zn - zf);
383     return pout;
384 }
385
386 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
387 {
388     D3DXMatrixIdentity(pout);
389     pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
390     pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
391     pout->u.m[2][2] = zf / (zf - zn);
392     pout->u.m[2][3] = 1.0f;
393     pout->u.m[3][2] = (zf * zn) / (zn - zf);
394     pout->u.m[3][3] = 0.0f;
395     return pout;
396 }
397
398 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
399 {
400     D3DXMatrixIdentity(pout);
401     pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
402     pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
403     pout->u.m[2][2] = zf / (zn - zf);
404     pout->u.m[2][3] = -1.0f;
405     pout->u.m[3][2] = (zf * zn) / (zn - zf);
406     pout->u.m[3][3] = 0.0f;
407     return pout;
408 }
409
410 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
411 {
412     D3DXMatrixIdentity(pout);
413     pout->u.m[0][0] = 2.0f * zn / w;
414     pout->u.m[1][1] = 2.0f * zn / h;
415     pout->u.m[2][2] = zf / (zf - zn);
416     pout->u.m[3][2] = (zn * zf) / (zn - zf);
417     pout->u.m[2][3] = 1.0f;
418     pout->u.m[3][3] = 0.0f;
419     return pout;
420 }
421
422 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
423 {
424     D3DXMatrixIdentity(pout);
425     pout->u.m[0][0] = 2.0f * zn / (r - l);
426     pout->u.m[1][1] = -2.0f * zn / (b - t);
427     pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
428     pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
429     pout->u.m[2][2] = - zf / (zn - zf);
430     pout->u.m[3][2] = (zn * zf) / (zn -zf);
431     pout->u.m[2][3] = 1.0f;
432     pout->u.m[3][3] = 0.0f;
433     return pout;
434 }
435
436 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
437 {
438     D3DXMatrixIdentity(pout);
439     pout->u.m[0][0] = 2.0f * zn / (r - l);
440     pout->u.m[1][1] = -2.0f * zn / (b - t);
441     pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
442     pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
443     pout->u.m[2][2] = zf / (zn - zf);
444     pout->u.m[3][2] = (zn * zf) / (zn -zf);
445     pout->u.m[2][3] = -1.0f;
446     pout->u.m[3][3] = 0.0f;
447     return pout;
448 }
449
450 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT 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 / (zn - zf);
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 D3DXMatrixReflect(D3DXMATRIX *pout, CONST D3DXPLANE *pplane)
463 {
464     D3DXPLANE Nplane;
465
466     D3DXPlaneNormalize(&Nplane, pplane);
467     D3DXMatrixIdentity(pout);
468     pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
469     pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
470     pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
471     pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
472     pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
473     pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
474     pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
475     pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
476     pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
477     pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
478     pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
479     pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
480     return pout;
481 }
482
483 D3DXMATRIX* WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
484 {
485     D3DXVECTOR3 v;
486
487     D3DXVec3Normalize(&v,pv);
488     D3DXMatrixIdentity(pout);
489     pout->u.m[0][0] = (1.0f - cos(angle)) * v.x * v.x + cos(angle);
490     pout->u.m[1][0] = (1.0f - cos(angle)) * v.x * v.y - sin(angle) * v.z;
491     pout->u.m[2][0] = (1.0f - cos(angle)) * v.x * v.z + sin(angle) * v.y;
492     pout->u.m[0][1] = (1.0f - cos(angle)) * v.y * v.x + sin(angle) * v.z;
493     pout->u.m[1][1] = (1.0f - cos(angle)) * v.y * v.y + cos(angle);
494     pout->u.m[2][1] = (1.0f - cos(angle)) * v.y * v.z - sin(angle) * v.x;
495     pout->u.m[0][2] = (1.0f - cos(angle)) * v.z * v.x - sin(angle) * v.y;
496     pout->u.m[1][2] = (1.0f - cos(angle)) * v.z * v.y + sin(angle) * v.x;
497     pout->u.m[2][2] = (1.0f - cos(angle)) * v.z * v.z + cos(angle);
498     return pout;
499 }
500
501 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, CONST D3DXQUATERNION *pq)
502 {
503     D3DXMatrixIdentity(pout);
504     pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
505     pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
506     pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
507     pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
508     pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
509     pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
510     pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
511     pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
512     pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
513     return pout;
514 }
515
516 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
517 {
518     D3DXMatrixIdentity(pout);
519     pout->u.m[1][1] = cos(angle);
520     pout->u.m[2][2] = cos(angle);
521     pout->u.m[1][2] = sin(angle);
522     pout->u.m[2][1] = -sin(angle);
523     return pout;
524 }
525
526 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
527 {
528     D3DXMatrixIdentity(pout);
529     pout->u.m[0][0] = cos(angle);
530     pout->u.m[2][2] = cos(angle);
531     pout->u.m[0][2] = -sin(angle);
532     pout->u.m[2][0] = sin(angle);
533     return pout;
534 }
535
536 D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
537 {
538     D3DXMATRIX m;
539
540     D3DXMatrixIdentity(pout);
541     D3DXMatrixRotationZ(&m, roll);
542     D3DXMatrixMultiply(pout, pout, &m);
543     D3DXMatrixRotationX(&m, pitch);
544     D3DXMatrixMultiply(pout, pout, &m);
545     D3DXMatrixRotationY(&m, yaw);
546     D3DXMatrixMultiply(pout, pout, &m);
547     return pout;
548 }
549 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
550 {
551     D3DXMatrixIdentity(pout);
552     pout->u.m[0][0] = cos(angle);
553     pout->u.m[1][1] = cos(angle);
554     pout->u.m[0][1] = sin(angle);
555     pout->u.m[1][0] = -sin(angle);
556     return pout;
557 }
558
559 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
560 {
561     D3DXMatrixIdentity(pout);
562     pout->u.m[0][0] = sx;
563     pout->u.m[1][1] = sy;
564     pout->u.m[2][2] = sz;
565     return pout;
566 }
567
568 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, CONST D3DXVECTOR4 *plight, CONST D3DXPLANE *pplane)
569 {
570     D3DXPLANE Nplane;
571     FLOAT dot;
572
573     D3DXPlaneNormalize(&Nplane, pplane);
574     dot = D3DXPlaneDot(&Nplane, plight);
575     pout->u.m[0][0] = dot - Nplane.a * plight->x;
576     pout->u.m[0][1] = -Nplane.a * plight->y;
577     pout->u.m[0][2] = -Nplane.a * plight->z;
578     pout->u.m[0][3] = -Nplane.a * plight->w;
579     pout->u.m[1][0] = -Nplane.b * plight->x;
580     pout->u.m[1][1] = dot - Nplane.b * plight->y;
581     pout->u.m[1][2] = -Nplane.b * plight->z;
582     pout->u.m[1][3] = -Nplane.b * plight->w;
583     pout->u.m[2][0] = -Nplane.c * plight->x;
584     pout->u.m[2][1] = -Nplane.c * plight->y;
585     pout->u.m[2][2] = dot - Nplane.c * plight->z;
586     pout->u.m[2][3] = -Nplane.c * plight->w;
587     pout->u.m[3][0] = -Nplane.d * plight->x;
588     pout->u.m[3][1] = -Nplane.d * plight->y;
589     pout->u.m[3][2] = -Nplane.d * plight->z;
590     pout->u.m[3][3] = dot - Nplane.d * plight->w;
591     return pout;
592 }
593
594 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pscalingcenter, CONST D3DXQUATERNION *pscalingrotation, CONST D3DXVECTOR3 *pscaling, CONST D3DXVECTOR3 *protationcenter, CONST D3DXQUATERNION *protation, CONST D3DXVECTOR3 *ptranslation)
595 {
596     D3DXMATRIX m1, m2, m3, m4, m5, m6, m7;
597     D3DXQUATERNION prc;
598     D3DXVECTOR3 psc, pt;
599
600     if ( !pscalingcenter )
601     {
602         psc.x = 0.0f;
603         psc.y = 0.0f;
604         psc.z = 0.0f;
605     }
606     else
607     {
608         psc.x = pscalingcenter->x;
609         psc.y = pscalingcenter->y;
610         psc.z = pscalingcenter->z;
611     }
612
613     if ( !protationcenter )
614     {
615         prc.x = 0.0f;
616         prc.y = 0.0f;
617         prc.z = 0.0f;
618     }
619     else
620     {
621         prc.x = protationcenter->x;
622         prc.y = protationcenter->y;
623         prc.z = protationcenter->z;
624     }
625
626     if ( !ptranslation )
627     {
628         pt.x = 0.0f;
629         pt.y = 0.0f;
630         pt.z = 0.0f;
631     }
632     else
633     {
634         pt.x = ptranslation->x;
635         pt.y = ptranslation->y;
636         pt.z = ptranslation->z;
637     }
638
639     D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
640
641     if ( !pscalingrotation )
642     {
643         D3DXMatrixIdentity(&m2);
644         D3DXMatrixIdentity(&m4);
645     }
646     else
647     {
648         D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
649         D3DXMatrixInverse(&m2, NULL, &m4);
650     }
651
652     if ( !pscaling ) D3DXMatrixIdentity(&m3);
653     else D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
654
655     if ( !protation ) D3DXMatrixIdentity(&m6);
656     else D3DXMatrixRotationQuaternion(&m6, protation);
657
658     D3DXMatrixTranslation(&m5, psc.x - prc.x,  psc.y - prc.y,  psc.z - prc.z);
659     D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
660     D3DXMatrixMultiply(&m1, &m1, &m2);
661     D3DXMatrixMultiply(&m1, &m1, &m3);
662     D3DXMatrixMultiply(&m1, &m1, &m4);
663     D3DXMatrixMultiply(&m1, &m1, &m5);
664     D3DXMatrixMultiply(&m1, &m1, &m6);
665     D3DXMatrixMultiply(pout, &m1, &m7);
666     return pout;
667 }
668 D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(D3DXMATRIX *pout, CONST D3DXVECTOR2 *pscalingcenter, FLOAT scalingrotation, CONST D3DXVECTOR2 *pscaling, CONST D3DXVECTOR2 *protationcenter, FLOAT rotation, CONST D3DXVECTOR2 *ptranslation)
669 {
670     D3DXQUATERNION rot, sca_rot;
671     D3DXVECTOR3 rot_center, sca, sca_center, trans;
672
673     if ( pscalingcenter )
674     {
675         sca_center.x=pscalingcenter->x;
676         sca_center.y=pscalingcenter->y;
677         sca_center.z=0.0f;
678     }
679     else
680     {
681         sca_center.x=0.0f;
682         sca_center.y=0.0f;
683         sca_center.z=0.0f;
684     }
685
686     if ( pscaling )
687     {
688         sca.x=pscaling->x;
689         sca.y=pscaling->y;
690         sca.z=1.0f;
691     }
692     else
693     {
694         sca.x=1.0f;
695         sca.y=1.0f;
696         sca.z=1.0f;
697     }
698
699     if ( protationcenter )
700     {
701         rot_center.x=protationcenter->x;
702         rot_center.y=protationcenter->y;
703         rot_center.z=0.0f;
704     }
705     else
706     {
707         rot_center.x=0.0f;
708         rot_center.y=0.0f;
709         rot_center.z=0.0f;
710     }
711
712     if ( ptranslation )
713     {
714         trans.x=ptranslation->x;
715         trans.y=ptranslation->y;
716         trans.z=0.0f;
717     }
718     else
719     {
720         trans.x=0.0f;
721         trans.y=0.0f;
722         trans.z=0.0f;
723     }
724
725     rot.w=cos(rotation/2.0f);
726     rot.x=0.0f;
727     rot.y=0.0f;
728     rot.z=sin(rotation/2.0f);
729
730     sca_rot.w=cos(scalingrotation/2.0f);
731     sca_rot.x=0.0f;
732     sca_rot.y=0.0f;
733     sca_rot.z=sin(scalingrotation/2.0f);
734
735     D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
736
737     return pout;
738 }
739
740 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
741 {
742     D3DXMatrixIdentity(pout);
743     pout->u.m[3][0] = x;
744     pout->u.m[3][1] = y;
745     pout->u.m[3][2] = z;
746     return pout;
747 }
748
749 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
750 {
751     CONST D3DXMATRIX m = *pm;
752     int i,j;
753
754     for (i=0; i<4; i++)
755         for (j=0; j<4; j++) pout->u.m[i][j] = m.u.m[j][i];
756
757     return pout;
758 }
759
760 /*_________________D3DXMatrixStack____________________*/
761
762 static const unsigned int INITIAL_STACK_SIZE = 32;
763
764 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, LPD3DXMATRIXSTACK* ppstack)
765 {
766     ID3DXMatrixStackImpl* object;
767
768     TRACE("flags %#x, ppstack %p\n", flags, ppstack);
769
770     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXMatrixStackImpl));
771     if ( object == NULL )
772     {
773      *ppstack = NULL;
774      return E_OUTOFMEMORY;
775     }
776     object->ID3DXMatrixStack_iface.lpVtbl = &ID3DXMatrixStack_Vtbl;
777     object->ref = 1;
778
779     object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(D3DXMATRIX));
780     if (!object->stack)
781     {
782         HeapFree(GetProcessHeap(), 0, object);
783         *ppstack = NULL;
784         return E_OUTOFMEMORY;
785     }
786
787     object->current = 0;
788     object->stack_size = INITIAL_STACK_SIZE;
789     D3DXMatrixIdentity(&object->stack[0]);
790
791     TRACE("Created matrix stack %p\n", object);
792
793     *ppstack = &object->ID3DXMatrixStack_iface;
794     return D3D_OK;
795 }
796
797 static inline ID3DXMatrixStackImpl *impl_from_ID3DXMatrixStack(ID3DXMatrixStack *iface)
798 {
799   return CONTAINING_RECORD(iface, ID3DXMatrixStackImpl, ID3DXMatrixStack_iface);
800 }
801
802 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **ppobj)
803 {
804     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
805     if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXMatrixStack))
806     {
807      ID3DXMatrixStack_AddRef(iface);
808      *ppobj = This;
809      return S_OK;
810     }
811     *ppobj = NULL;
812     WARN("(%p)->(%s,%p), not found\n", This, debugstr_guid(riid), ppobj);
813     return E_NOINTERFACE;
814 }
815
816 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
817 {
818     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
819     ULONG ref = InterlockedIncrement(&This->ref);
820     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
821     return ref;
822 }
823
824 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack* iface)
825 {
826     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
827     ULONG ref = InterlockedDecrement(&This->ref);
828     if (!ref)
829     {
830         HeapFree(GetProcessHeap(), 0, This->stack);
831         HeapFree(GetProcessHeap(), 0, This);
832     }
833     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
834     return ref;
835 }
836
837 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
838 {
839     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
840
841     TRACE("iface %p\n", iface);
842
843     return &This->stack[This->current];
844 }
845
846 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
847 {
848     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
849
850     TRACE("iface %p\n", iface);
851
852     D3DXMatrixIdentity(&This->stack[This->current]);
853
854     return D3D_OK;
855 }
856
857 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
858 {
859     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
860
861     TRACE("iface %p\n", iface);
862
863     This->stack[This->current] = *pm;
864
865     return D3D_OK;
866 }
867
868 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
869 {
870     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
871
872     TRACE("iface %p\n", iface);
873
874     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], pm);
875
876     return D3D_OK;
877 }
878
879 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
880 {
881     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
882
883     TRACE("iface %p\n", iface);
884
885     D3DXMatrixMultiply(&This->stack[This->current], pm, &This->stack[This->current]);
886
887     return D3D_OK;
888 }
889
890 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
891 {
892     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
893
894     TRACE("iface %p\n", iface);
895
896     /* Popping the last element on the stack returns D3D_OK, but does nothing. */
897     if (!This->current) return D3D_OK;
898
899     if (This->current <= This->stack_size / 4 && This->stack_size >= INITIAL_STACK_SIZE * 2)
900     {
901         unsigned int new_size;
902         D3DXMATRIX *new_stack;
903
904         new_size = This->stack_size / 2;
905         new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(D3DXMATRIX));
906         if (new_stack)
907         {
908             This->stack_size = new_size;
909             This->stack = new_stack;
910         }
911     }
912
913     --This->current;
914
915     return D3D_OK;
916 }
917
918 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
919 {
920     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
921
922     TRACE("iface %p\n", iface);
923
924     if (This->current == This->stack_size - 1)
925     {
926         unsigned int new_size;
927         D3DXMATRIX *new_stack;
928
929         if (This->stack_size > UINT_MAX / 2) return E_OUTOFMEMORY;
930
931         new_size = This->stack_size * 2;
932         new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(D3DXMATRIX));
933         if (!new_stack) return E_OUTOFMEMORY;
934
935         This->stack_size = new_size;
936         This->stack = new_stack;
937     }
938
939     ++This->current;
940     This->stack[This->current] = This->stack[This->current - 1];
941
942     return D3D_OK;
943 }
944
945 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
946 {
947     D3DXMATRIX temp;
948     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
949
950     TRACE("iface %p\n", iface);
951
952     D3DXMatrixRotationAxis(&temp, pv, angle);
953     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
954
955     return D3D_OK;
956 }
957
958 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
959 {
960     D3DXMATRIX temp;
961     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
962
963     TRACE("iface %p\n", iface);
964
965     D3DXMatrixRotationAxis(&temp, pv, angle);
966     D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
967
968     return D3D_OK;
969 }
970
971 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
972 {
973     D3DXMATRIX temp;
974     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
975
976     TRACE("iface %p\n", iface);
977
978     D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
979     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
980
981     return D3D_OK;
982 }
983
984 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
985 {
986     D3DXMATRIX temp;
987     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
988
989     TRACE("iface %p\n", iface);
990
991     D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
992     D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
993
994     return D3D_OK;
995 }
996
997 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
998 {
999     D3DXMATRIX temp;
1000     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1001
1002     TRACE("iface %p\n", iface);
1003
1004     D3DXMatrixScaling(&temp, x, y, z);
1005     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1006
1007     return D3D_OK;
1008 }
1009
1010 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1011 {
1012     D3DXMATRIX temp;
1013     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1014
1015     TRACE("iface %p\n", iface);
1016
1017     D3DXMatrixScaling(&temp, x, y, z);
1018     D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1019
1020     return D3D_OK;
1021 }
1022
1023 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1024 {
1025     D3DXMATRIX temp;
1026     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1027
1028     TRACE("iface %p\n", iface);
1029
1030     D3DXMatrixTranslation(&temp, x, y, z);
1031     D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1032
1033     return D3D_OK;
1034 }
1035
1036 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1037 {
1038     D3DXMATRIX temp;
1039     ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1040
1041     TRACE("iface %p\n", iface);
1042
1043     D3DXMatrixTranslation(&temp, x, y, z);
1044     D3DXMatrixMultiply(&This->stack[This->current], &temp,&This->stack[This->current]);
1045
1046     return D3D_OK;
1047 }
1048
1049 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
1050 {
1051     ID3DXMatrixStackImpl_QueryInterface,
1052     ID3DXMatrixStackImpl_AddRef,
1053     ID3DXMatrixStackImpl_Release,
1054     ID3DXMatrixStackImpl_Pop,
1055     ID3DXMatrixStackImpl_Push,
1056     ID3DXMatrixStackImpl_LoadIdentity,
1057     ID3DXMatrixStackImpl_LoadMatrix,
1058     ID3DXMatrixStackImpl_MultMatrix,
1059     ID3DXMatrixStackImpl_MultMatrixLocal,
1060     ID3DXMatrixStackImpl_RotateAxis,
1061     ID3DXMatrixStackImpl_RotateAxisLocal,
1062     ID3DXMatrixStackImpl_RotateYawPitchRoll,
1063     ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
1064     ID3DXMatrixStackImpl_Scale,
1065     ID3DXMatrixStackImpl_ScaleLocal,
1066     ID3DXMatrixStackImpl_Translate,
1067     ID3DXMatrixStackImpl_TranslateLocal,
1068     ID3DXMatrixStackImpl_GetTop
1069 };
1070
1071 /*_________________D3DXPLANE________________*/
1072
1073 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, CONST D3DXVECTOR3 *pvpoint, CONST D3DXVECTOR3 *pvnormal)
1074 {
1075     pout->a = pvnormal->x;
1076     pout->b = pvnormal->y;
1077     pout->c = pvnormal->z;
1078     pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
1079     return pout;
1080 }
1081
1082 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3)
1083 {
1084     D3DXVECTOR3 edge1, edge2, normal, Nnormal;
1085
1086     edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
1087     edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
1088     D3DXVec3Subtract(&edge1, pv2, pv1);
1089     D3DXVec3Subtract(&edge2, pv3, pv1);
1090     D3DXVec3Cross(&normal, &edge1, &edge2);
1091     D3DXVec3Normalize(&Nnormal, &normal);
1092     D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
1093     return pout;
1094 }
1095
1096 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, CONST D3DXPLANE *pp, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
1097 {
1098     D3DXVECTOR3 direction, normal;
1099     FLOAT dot, temp;
1100
1101     normal.x = pp->a;
1102     normal.y = pp->b;
1103     normal.z = pp->c;
1104     direction.x = pv2->x - pv1->x;
1105     direction.y = pv2->y - pv1->y;
1106     direction.z = pv2->z - pv1->z;
1107     dot = D3DXVec3Dot(&normal, &direction);
1108     if ( !dot ) return NULL;
1109     temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
1110     pout->x = pv1->x - temp * direction.x;
1111     pout->y = pv1->y - temp * direction.y;
1112     pout->z = pv1->z - temp * direction.z;
1113     return pout;
1114 }
1115
1116 D3DXPLANE* WINAPI D3DXPlaneNormalize(D3DXPLANE *pout, CONST D3DXPLANE *pp)
1117 {
1118     D3DXPLANE out;
1119     FLOAT norm;
1120
1121     norm = sqrt(pp->a * pp->a + pp->b * pp->b + pp->c * pp->c);
1122     if ( norm )
1123     {
1124      out.a = pp->a / norm;
1125      out.b = pp->b / norm;
1126      out.c = pp->c / norm;
1127      out.d = pp->d / norm;
1128     }
1129     else
1130     {
1131      out.a = 0.0f;
1132      out.b = 0.0f;
1133      out.c = 0.0f;
1134      out.d = 0.0f;
1135     }
1136     *pout = out;
1137     return pout;
1138 }
1139
1140 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, CONST D3DXPLANE *pplane, CONST D3DXMATRIX *pm)
1141 {
1142     CONST D3DXPLANE plane = *pplane;
1143     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;
1144     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;
1145     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;
1146     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;
1147     return pout;
1148 }
1149
1150 D3DXPLANE* WINAPI D3DXPlaneTransformArray(D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1151 {
1152     UINT i;
1153
1154     for (i = 0; i < elements; ++i) {
1155         D3DXPlaneTransform(
1156             (D3DXPLANE*)((char*)out + outstride * i),
1157             (CONST D3DXPLANE*)((const char*)in + instride * i),
1158             matrix);
1159     }
1160     return out;
1161 }
1162
1163 /*_________________D3DXQUATERNION________________*/
1164
1165 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
1166 {
1167     D3DXQUATERNION temp1, temp2;
1168     D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
1169     return pout;
1170 }
1171
1172 D3DXQUATERNION* WINAPI D3DXQuaternionExp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1173 {
1174     FLOAT norm;
1175
1176     norm = sqrt(pq->x * pq->x + pq->y * pq->y + pq->z * pq->z);
1177     if (norm )
1178     {
1179      pout->x = sin(norm) * pq->x / norm;
1180      pout->y = sin(norm) * pq->y / norm;
1181      pout->z = sin(norm) * pq->z / norm;
1182      pout->w = cos(norm);
1183     }
1184     else
1185     {
1186      pout->x = 0.0f;
1187      pout->y = 0.0f;
1188      pout->z = 0.0f;
1189      pout->w = 1.0f;
1190     }
1191     return pout;
1192 }
1193
1194 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1195 {
1196     D3DXQUATERNION out;
1197     FLOAT norm;
1198
1199     norm = D3DXQuaternionLengthSq(pq);
1200
1201     out.x = -pq->x / norm;
1202     out.y = -pq->y / norm;
1203     out.z = -pq->z / norm;
1204     out.w = pq->w / norm;
1205
1206     *pout =out;
1207     return pout;
1208 }
1209
1210 D3DXQUATERNION* WINAPI D3DXQuaternionLn(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1211 {
1212     FLOAT norm, normvec, theta;
1213
1214     norm = D3DXQuaternionLengthSq(pq);
1215     if ( norm > 1.0001f )
1216     {
1217      pout->x = pq->x;
1218      pout->y = pq->y;
1219      pout->z = pq->z;
1220      pout->w = 0.0f;
1221     }
1222     else if( norm > 0.99999f)
1223     {
1224      normvec = sqrt( pq->x * pq->x + pq->y * pq->y + pq->z * pq->z );
1225      theta = atan2(normvec, pq->w) / normvec;
1226      pout->x = theta * pq->x;
1227      pout->y = theta * pq->y;
1228      pout->z = theta * pq->z;
1229      pout->w = 0.0f;
1230     }
1231     else
1232     {
1233      FIXME("The quaternion (%f, %f, %f, %f) has a norm <1. This should not happen. Windows returns a result anyway. This case is not implemented yet.\n", pq->x, pq->y, pq->z, pq->w);
1234     }
1235     return pout;
1236 }
1237
1238 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
1239 {
1240     D3DXQUATERNION out;
1241     out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
1242     out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
1243     out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
1244     out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
1245     *pout = out;
1246     return pout;
1247 }
1248
1249 D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1250 {
1251     D3DXQUATERNION out;
1252     FLOAT norm;
1253
1254     norm = D3DXQuaternionLength(pq);
1255
1256     out.x = pq->x / norm;
1257     out.y = pq->y / norm;
1258     out.z = pq->z / norm;
1259     out.w = pq->w / norm;
1260
1261     *pout=out;
1262
1263     return pout;
1264 }
1265
1266 D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
1267 {
1268     D3DXVECTOR3 temp;
1269
1270     D3DXVec3Normalize(&temp, pv);
1271     pout->x = sin( angle / 2.0f ) * temp.x;
1272     pout->y = sin( angle / 2.0f ) * temp.y;
1273     pout->z = sin( angle / 2.0f ) * temp.z;
1274     pout->w = cos( angle / 2.0f );
1275     return pout;
1276 }
1277
1278 D3DXQUATERNION* WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *pout, CONST D3DXMATRIX *pm)
1279 {
1280     int i, maxi;
1281     FLOAT maxdiag, S, trace;
1282
1283     trace = pm->u.m[0][0] + pm->u.m[1][1] + pm->u.m[2][2] + 1.0f;
1284     if ( trace > 1.0f)
1285     {
1286      pout->x = ( pm->u.m[1][2] - pm->u.m[2][1] ) / ( 2.0f * sqrt(trace) );
1287      pout->y = ( pm->u.m[2][0] - pm->u.m[0][2] ) / ( 2.0f * sqrt(trace) );
1288      pout->z = ( pm->u.m[0][1] - pm->u.m[1][0] ) / ( 2.0f * sqrt(trace) );
1289      pout->w = sqrt(trace) / 2.0f;
1290      return pout;
1291      }
1292     maxi = 0;
1293     maxdiag = pm->u.m[0][0];
1294     for (i=1; i<3; i++)
1295     {
1296      if ( pm->u.m[i][i] > maxdiag )
1297      {
1298       maxi = i;
1299       maxdiag = pm->u.m[i][i];
1300      }
1301     }
1302     switch( maxi )
1303     {
1304      case 0:
1305        S = 2.0f * sqrt(1.0f + pm->u.m[0][0] - pm->u.m[1][1] - pm->u.m[2][2]);
1306        pout->x = 0.25f * S;
1307        pout->y = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1308        pout->z = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1309        pout->w = ( pm->u.m[1][2] - pm->u.m[2][1] ) / S;
1310      break;
1311      case 1:
1312        S = 2.0f * sqrt(1.0f + pm->u.m[1][1] - pm->u.m[0][0] - pm->u.m[2][2]);
1313        pout->x = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1314        pout->y = 0.25f * S;
1315        pout->z = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1316        pout->w = ( pm->u.m[2][0] - pm->u.m[0][2] ) / S;
1317      break;
1318      case 2:
1319        S = 2.0f * sqrt(1.0f + pm->u.m[2][2] - pm->u.m[0][0] - pm->u.m[1][1]);
1320        pout->x = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1321        pout->y = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1322        pout->z = 0.25f * S;
1323        pout->w = ( pm->u.m[0][1] - pm->u.m[1][0] ) / S;
1324      break;
1325     }
1326     return pout;
1327 }
1328
1329 D3DXQUATERNION* WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
1330 {
1331     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);
1332     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);
1333     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);
1334     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);
1335     return pout;
1336 }
1337
1338 D3DXQUATERNION* WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, FLOAT t)
1339 {
1340     FLOAT dot, epsilon, temp, theta, u;
1341
1342     epsilon = 1.0f;
1343     temp = 1.0f - t;
1344     u = t;
1345     dot = D3DXQuaternionDot(pq1, pq2);
1346     if ( dot < 0.0f )
1347     {
1348         epsilon = -1.0f;
1349         dot = -dot;
1350     }
1351     if( 1.0f - dot > 0.001f )
1352     {
1353         theta = acos(dot);
1354         temp  = sin(theta * temp) / sin(theta);
1355         u = sin(theta * u) / sin(theta);
1356     }
1357     pout->x = temp * pq1->x + epsilon * u * pq2->x;
1358     pout->y = temp * pq1->y + epsilon * u * pq2->y;
1359     pout->z = temp * pq1->z + epsilon * u * pq2->z;
1360     pout->w = temp * pq1->w + epsilon * u * pq2->w;
1361     return pout;
1362 }
1363
1364 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, CONST D3DXQUATERNION *pq4, FLOAT t)
1365 {
1366     D3DXQUATERNION temp1, temp2;
1367
1368     D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1369     return pout;
1370 }
1371
1372 void WINAPI D3DXQuaternionToAxisAngle(CONST D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1373 {
1374     paxis->x = pq->x;
1375     paxis->y = pq->y;
1376     paxis->z = pq->z;
1377     *pangle = 2.0f * acos(pq->w);
1378 }
1379
1380 /*_________________D3DXVec2_____________________*/
1381
1382 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1383 {
1384     pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1385     pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1386     return pout;
1387 }
1388
1389 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv0, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT s)
1390 {
1391     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);
1392     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);
1393     return pout;
1394 }
1395
1396 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pt1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pt2, FLOAT s)
1397 {
1398     FLOAT h1, h2, h3, h4;
1399
1400     h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1401     h2 = s * s * s - 2.0f * s * s + s;
1402     h3 = -2.0f * s * s * s + 3.0f * s * s;
1403     h4 = s * s * s - s * s;
1404
1405     pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1406     pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1407     return pout;
1408 }
1409
1410 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
1411 {
1412     D3DXVECTOR2 out;
1413     FLOAT norm;
1414
1415     norm = D3DXVec2Length(pv);
1416     if ( !norm )
1417     {
1418      out.x = 0.0f;
1419      out.y = 0.0f;
1420     }
1421     else
1422     {
1423      out.x = pv->x / norm;
1424      out.y = pv->y / norm;
1425     }
1426     *pout=out;
1427     return pout;
1428 }
1429
1430 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1431 {
1432     pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y  + pm->u.m[3][0];
1433     pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y  + pm->u.m[3][1];
1434     pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y  + pm->u.m[3][2];
1435     pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y  + pm->u.m[3][3];
1436     return pout;
1437 }
1438
1439 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1440 {
1441     UINT i;
1442
1443     for (i = 0; i < elements; ++i) {
1444         D3DXVec2Transform(
1445             (D3DXVECTOR4*)((char*)out + outstride * i),
1446             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1447             matrix);
1448     }
1449     return out;
1450 }
1451
1452 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1453 {
1454     D3DXVECTOR2 v;
1455     FLOAT norm;
1456
1457     v = *pv;
1458     norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1459
1460     pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1461     pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1462
1463     return pout;
1464 }
1465
1466 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1467 {
1468     UINT i;
1469
1470     for (i = 0; i < elements; ++i) {
1471         D3DXVec2TransformCoord(
1472             (D3DXVECTOR2*)((char*)out + outstride * i),
1473             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1474             matrix);
1475     }
1476     return out;
1477 }
1478
1479 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1480 {
1481     CONST D3DXVECTOR2 v = *pv;
1482     pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1483     pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1484     return pout;
1485 }
1486
1487 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride, CONST D3DXMATRIX *matrix, UINT elements)
1488 {
1489     UINT i;
1490
1491     for (i = 0; i < elements; ++i) {
1492         D3DXVec2TransformNormal(
1493             (D3DXVECTOR2*)((char*)out + outstride * i),
1494             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1495             matrix);
1496     }
1497     return out;
1498 }
1499
1500 /*_________________D3DXVec3_____________________*/
1501
1502 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1503 {
1504     pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1505     pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1506     pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1507     return pout;
1508 }
1509
1510 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
1511 {
1512     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);
1513     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);
1514     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);
1515     return pout;
1516 }
1517
1518 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
1519 {
1520     FLOAT h1, h2, h3, h4;
1521
1522     h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1523     h2 = s * s * s - 2.0f * s * s + s;
1524     h3 = -2.0f * s * s * s + 3.0f * s * s;
1525     h4 = s * s * s - s * s;
1526
1527     pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1528     pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1529     pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1530     return pout;
1531 }
1532
1533 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
1534 {
1535     D3DXVECTOR3 out;
1536     FLOAT norm;
1537
1538     norm = D3DXVec3Length(pv);
1539     if ( !norm )
1540     {
1541      out.x = 0.0f;
1542      out.y = 0.0f;
1543      out.z = 0.0f;
1544     }
1545     else
1546     {
1547      out.x = pv->x / norm;
1548      out.y = pv->y / norm;
1549      out.z = pv->z / norm;
1550     }
1551     *pout = out;
1552     return pout;
1553 }
1554
1555 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1556 {
1557     D3DXMATRIX m;
1558     D3DXVECTOR3 out;
1559
1560     D3DXMatrixMultiply(&m, pworld, pview);
1561     D3DXMatrixMultiply(&m, &m, pprojection);
1562     D3DXVec3TransformCoord(&out, pv, &m);
1563     out.x = pviewport->X +  ( 1.0f + out.x ) * pviewport->Width / 2.0f;
1564     out.y = pviewport->Y +  ( 1.0f - out.y ) * pviewport->Height / 2.0f;
1565     out.z = pviewport->MinZ + out.z * ( pviewport->MaxZ - pviewport->MinZ );
1566     *pout = out;
1567     return pout;
1568 }
1569
1570 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)
1571 {
1572     UINT i;
1573
1574     for (i = 0; i < elements; ++i) {
1575         D3DXVec3Project(
1576             (D3DXVECTOR3*)((char*)out + outstride * i),
1577             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1578             viewport, projection, view, world);
1579     }
1580     return out;
1581 }
1582
1583 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1584 {
1585     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];
1586     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];
1587     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];
1588     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];
1589     return pout;
1590 }
1591
1592 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1593 {
1594     UINT i;
1595
1596     for (i = 0; i < elements; ++i) {
1597         D3DXVec3Transform(
1598             (D3DXVECTOR4*)((char*)out + outstride * i),
1599             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1600             matrix);
1601     }
1602     return out;
1603 }
1604
1605 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1606 {
1607     D3DXVECTOR3 out;
1608     FLOAT norm;
1609
1610     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];
1611
1612     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;
1613     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;
1614     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;
1615
1616     *pout = out;
1617
1618     return pout;
1619 }
1620
1621 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1622 {
1623     UINT i;
1624
1625     for (i = 0; i < elements; ++i) {
1626         D3DXVec3TransformCoord(
1627             (D3DXVECTOR3*)((char*)out + outstride * i),
1628             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1629             matrix);
1630     }
1631     return out;
1632 }
1633
1634 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1635 {
1636     CONST D3DXVECTOR3 v = *pv;
1637     pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1638     pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1639     pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1640     return pout;
1641
1642 }
1643
1644 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1645 {
1646     UINT i;
1647
1648     for (i = 0; i < elements; ++i) {
1649         D3DXVec3TransformNormal(
1650             (D3DXVECTOR3*)((char*)out + outstride * i),
1651             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1652             matrix);
1653     }
1654     return out;
1655 }
1656
1657 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1658 {
1659     D3DXMATRIX m;
1660     D3DXVECTOR3 out;
1661
1662     D3DXMatrixMultiply(&m, pworld, pview);
1663     D3DXMatrixMultiply(&m, &m, pprojection);
1664     D3DXMatrixInverse(&m, NULL, &m);
1665     out.x = 2.0f * ( pv->x - pviewport->X ) / pviewport->Width - 1.0f;
1666     out.y = 1.0f - 2.0f * ( pv->y - pviewport->Y ) / pviewport->Height;
1667     out.z = ( pv->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
1668     D3DXVec3TransformCoord(&out, &out, &m);
1669     *pout = out;
1670     return pout;
1671 }
1672
1673 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)
1674 {
1675     UINT i;
1676
1677     for (i = 0; i < elements; ++i) {
1678         D3DXVec3Unproject(
1679             (D3DXVECTOR3*)((char*)out + outstride * i),
1680             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1681             viewport, projection, view, world);
1682     }
1683     return out;
1684 }
1685
1686 /*_________________D3DXVec4_____________________*/
1687
1688 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
1689 {
1690     pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1691     pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1692     pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1693     pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
1694     return pout;
1695 }
1696
1697 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv0, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT s)
1698 {
1699     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);
1700     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);
1701     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);
1702     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);
1703     return pout;
1704 }
1705
1706 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
1707 {
1708     D3DXVECTOR4 out;
1709     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);
1710     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));
1711     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);
1712     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));
1713     *pout = out;
1714     return pout;
1715 }
1716
1717 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pt1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pt2, FLOAT s)
1718 {
1719     FLOAT h1, h2, h3, h4;
1720
1721     h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1722     h2 = s * s * s - 2.0f * s * s + s;
1723     h3 = -2.0f * s * s * s + 3.0f * s * s;
1724     h4 = s * s * s - s * s;
1725
1726     pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1727     pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1728     pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1729     pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
1730     return pout;
1731 }
1732
1733 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
1734 {
1735     D3DXVECTOR4 out;
1736     FLOAT norm;
1737
1738     norm = D3DXVec4Length(pv);
1739
1740     out.x = pv->x / norm;
1741     out.y = pv->y / norm;
1742     out.z = pv->z / norm;
1743     out.w = pv->w / norm;
1744
1745     *pout = out;
1746     return pout;
1747 }
1748
1749 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
1750 {
1751     D3DXVECTOR4 out;
1752     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;
1753     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;
1754     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;
1755     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;
1756     *pout = out;
1757     return pout;
1758 }
1759
1760 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1761 {
1762     UINT i;
1763
1764     for (i = 0; i < elements; ++i) {
1765         D3DXVec4Transform(
1766             (D3DXVECTOR4*)((char*)out + outstride * i),
1767             (CONST D3DXVECTOR4*)((const char*)in + instride * i),
1768             matrix);
1769     }
1770     return out;
1771 }