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