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