user32: Fix a message test that would only pass on wine.
[wine.git] / dlls / d3dx8 / math.c
blob6573c684a34a256ebf49cadc93f00f2174a4c1ea
1 /*
2 * Copyright 2007 David Adam
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.
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.
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
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <assert.h>
24 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "d3dx8_private.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3dx8);
35 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl;
37 /*_________________D3DXColor____________________*/
39 D3DXCOLOR* WINAPI D3DXColorAdjustContrast(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
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;
48 D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
50 FLOAT grey;
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;
60 /*_________________D3DXMatrix____________________*/
62 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR3 *rotationcenter, CONST D3DXQUATERNION *rotation, CONST D3DXVECTOR3 *translation)
64 D3DXMATRIX m1, m2, m3, m4, m5, p1, p2, p3;
66 D3DXMatrixScaling(&m1, scaling, scaling, scaling);
67 if ( !rotationcenter )
69 D3DXMatrixIdentity(&m2);
70 D3DXMatrixIdentity(&m4);
72 else
74 D3DXMatrixTranslation(&m2, -rotationcenter->x, -rotationcenter->y, -rotationcenter->z);
75 D3DXMatrixTranslation(&m4, rotationcenter->x, rotationcenter->y, rotationcenter->z);
77 if ( !rotation )
79 D3DXMatrixIdentity(&m3);
81 else
83 D3DXMatrixRotationQuaternion(&m3, rotation);
85 if ( !translation )
87 D3DXMatrixIdentity(&m5);
89 else
91 D3DXMatrixTranslation(&m5, translation->x, translation->y, translation->z);
93 D3DXMatrixMultiply(&p1, &m1, &m2);
94 D3DXMatrixMultiply(&p2, &p1, &m3);
95 D3DXMatrixMultiply(&p3, &p2, &m4);
96 D3DXMatrixMultiply(pout, &p3, &m5);
97 return pout;
100 FLOAT WINAPI D3DXMatrixfDeterminant(CONST D3DXMATRIX *pm)
102 D3DXVECTOR4 minor, v1, v2, v3;
103 FLOAT det;
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;
113 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, CONST D3DXMATRIX *pm)
115 int a, i, j;
116 D3DXVECTOR4 v, vec[3];
117 FLOAT cofactor, det;
119 det = D3DXMatrixfDeterminant(pm);
120 if ( !det ) return NULL;
121 if ( pdeterminant ) *pdeterminant = det;
122 for (i=0; i<4; i++)
124 for (j=0; j<4; j++)
126 if (j != i )
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];
136 D3DXVec4Cross(&v, &vec[0], &vec[1], &vec[2]);
137 for (j=0; j<4; j++)
139 switch(j)
141 case 0: cofactor = v.x; break;
142 case 1: cofactor = v.y; break;
143 case 2: cofactor = v.z; break;
144 case 3: cofactor = v.w; break;
146 pout->u.m[j][i] = pow(-1.0f, i) * cofactor / det;
149 return pout;
152 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
154 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
156 D3DXVec3Subtract(&vec2, pat, peye);
157 D3DXVec3Normalize(&vec, &vec2);
158 D3DXVec3Cross(&right, pup, &vec);
159 D3DXVec3Cross(&up, &vec, &right);
160 D3DXVec3Normalize(&rightn, &right);
161 D3DXVec3Normalize(&upn, &up);
162 pout->u.m[0][0] = rightn.x;
163 pout->u.m[1][0] = rightn.y;
164 pout->u.m[2][0] = rightn.z;
165 pout->u.m[3][0] = -D3DXVec3Dot(&rightn,peye);
166 pout->u.m[0][1] = upn.x;
167 pout->u.m[1][1] = upn.y;
168 pout->u.m[2][1] = upn.z;
169 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
170 pout->u.m[0][2] = vec.x;
171 pout->u.m[1][2] = vec.y;
172 pout->u.m[2][2] = vec.z;
173 pout->u.m[3][2] = -D3DXVec3Dot(&vec, peye);
174 pout->u.m[0][3] = 0.0f;
175 pout->u.m[1][3] = 0.0f;
176 pout->u.m[2][3] = 0.0f;
177 pout->u.m[3][3] = 1.0f;
178 return pout;
181 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
183 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
185 D3DXVec3Subtract(&vec2, pat, peye);
186 D3DXVec3Normalize(&vec, &vec2);
187 D3DXVec3Cross(&right, pup, &vec);
188 D3DXVec3Cross(&up, &vec, &right);
189 D3DXVec3Normalize(&rightn, &right);
190 D3DXVec3Normalize(&upn, &up);
191 pout->u.m[0][0] = -rightn.x;
192 pout->u.m[1][0] = -rightn.y;
193 pout->u.m[2][0] = -rightn.z;
194 pout->u.m[3][0] = D3DXVec3Dot(&rightn,peye);
195 pout->u.m[0][1] = upn.x;
196 pout->u.m[1][1] = upn.y;
197 pout->u.m[2][1] = upn.z;
198 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
199 pout->u.m[0][2] = -vec.x;
200 pout->u.m[1][2] = -vec.y;
201 pout->u.m[2][2] = -vec.z;
202 pout->u.m[3][2] = D3DXVec3Dot(&vec, peye);
203 pout->u.m[0][3] = 0.0f;
204 pout->u.m[1][3] = 0.0f;
205 pout->u.m[2][3] = 0.0f;
206 pout->u.m[3][3] = 1.0f;
207 return pout;
210 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
212 int i,j;
214 for (i=0; i<4; i++)
216 for (j=0; j<4; j++)
218 pout->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];
221 return pout;
224 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
226 D3DXMATRIX temp;
228 D3DXMatrixMultiply(&temp, pm1, pm2);
229 D3DXMatrixTranspose(pout, &temp);
230 return pout;
233 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
235 D3DXMatrixIdentity(pout);
236 pout->u.m[0][0] = 2.0f / w;
237 pout->u.m[1][1] = 2.0f / h;
238 pout->u.m[2][2] = 1.0f / (zf - zn);
239 pout->u.m[3][2] = zn / (zn - zf);
240 return pout;
243 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
245 D3DXMatrixIdentity(pout);
246 pout->u.m[0][0] = 2.0f / (r - l);
247 pout->u.m[1][1] = 2.0f / (t - b);
248 pout->u.m[2][2] = 1.0f / (zf -zn);
249 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
250 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
251 pout->u.m[3][2] = zn / (zn -zf);
252 return pout;
255 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
257 D3DXMatrixIdentity(pout);
258 pout->u.m[0][0] = 2.0f / (r - l);
259 pout->u.m[1][1] = 2.0f / (t - b);
260 pout->u.m[2][2] = 1.0f / (zn -zf);
261 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
262 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
263 pout->u.m[3][2] = zn / (zn -zf);
264 return pout;
267 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
269 D3DXMatrixIdentity(pout);
270 pout->u.m[0][0] = 2.0f / w;
271 pout->u.m[1][1] = 2.0f / h;
272 pout->u.m[2][2] = 1.0f / (zn - zf);
273 pout->u.m[3][2] = zn / (zn - zf);
274 return pout;
277 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
279 D3DXMatrixIdentity(pout);
280 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
281 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
282 pout->u.m[2][2] = zf / (zf - zn);
283 pout->u.m[2][3] = 1.0f;
284 pout->u.m[3][2] = (zf * zn) / (zn - zf);
285 pout->u.m[3][3] = 0.0f;
286 return pout;
289 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
291 D3DXMatrixIdentity(pout);
292 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
293 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
294 pout->u.m[2][2] = zf / (zn - zf);
295 pout->u.m[2][3] = -1.0f;
296 pout->u.m[3][2] = (zf * zn) / (zn - zf);
297 pout->u.m[3][3] = 0.0f;
298 return pout;
301 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
303 D3DXMatrixIdentity(pout);
304 pout->u.m[0][0] = 2.0f * zn / w;
305 pout->u.m[1][1] = 2.0f * zn / h;
306 pout->u.m[2][2] = zf / (zf - zn);
307 pout->u.m[3][2] = (zn * zf) / (zn - zf);
308 pout->u.m[2][3] = 1.0f;
309 pout->u.m[3][3] = 0.0f;
310 return pout;
313 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
315 D3DXMatrixIdentity(pout);
316 pout->u.m[0][0] = 2.0f * zn / (r - l);
317 pout->u.m[1][1] = -2.0f * zn / (b - t);
318 pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
319 pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
320 pout->u.m[2][2] = - zf / (zn - zf);
321 pout->u.m[3][2] = (zn * zf) / (zn -zf);
322 pout->u.m[2][3] = 1.0f;
323 pout->u.m[3][3] = 0.0f;
324 return pout;
327 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
329 D3DXMatrixIdentity(pout);
330 pout->u.m[0][0] = 2.0f * zn / (r - l);
331 pout->u.m[1][1] = -2.0f * zn / (b - t);
332 pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
333 pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
334 pout->u.m[2][2] = zf / (zn - zf);
335 pout->u.m[3][2] = (zn * zf) / (zn -zf);
336 pout->u.m[2][3] = -1.0f;
337 pout->u.m[3][3] = 0.0f;
338 return pout;
341 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
343 D3DXMatrixIdentity(pout);
344 pout->u.m[0][0] = 2.0f * zn / w;
345 pout->u.m[1][1] = 2.0f * zn / h;
346 pout->u.m[2][2] = zf / (zn - zf);
347 pout->u.m[3][2] = (zn * zf) / (zn - zf);
348 pout->u.m[2][3] = -1.0f;
349 pout->u.m[3][3] = 0.0f;
350 return pout;
353 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, CONST D3DXPLANE *pplane)
355 D3DXPLANE Nplane;
357 D3DXPlaneNormalize(&Nplane, pplane);
358 D3DXMatrixIdentity(pout);
359 pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
360 pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
361 pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
362 pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
363 pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
364 pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
365 pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
366 pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
367 pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
368 pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
369 pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
370 pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
371 return pout;
374 D3DXMATRIX* WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
376 D3DXVECTOR3 v;
378 D3DXVec3Normalize(&v,pv);
379 D3DXMatrixIdentity(pout);
380 pout->u.m[0][0] = (1.0f - cos(angle)) * v.x * v.x + cos(angle);
381 pout->u.m[1][0] = (1.0f - cos(angle)) * v.x * v.y - sin(angle) * v.z;
382 pout->u.m[2][0] = (1.0f - cos(angle)) * v.x * v.z + sin(angle) * v.y;
383 pout->u.m[0][1] = (1.0f - cos(angle)) * v.y * v.x + sin(angle) * v.z;
384 pout->u.m[1][1] = (1.0f - cos(angle)) * v.y * v.y + cos(angle);
385 pout->u.m[2][1] = (1.0f - cos(angle)) * v.y * v.z - sin(angle) * v.x;
386 pout->u.m[0][2] = (1.0f - cos(angle)) * v.z * v.x - sin(angle) * v.y;
387 pout->u.m[1][2] = (1.0f - cos(angle)) * v.z * v.y + sin(angle) * v.x;
388 pout->u.m[2][2] = (1.0f - cos(angle)) * v.z * v.z + cos(angle);
389 return pout;
392 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, CONST D3DXQUATERNION *pq)
394 D3DXMatrixIdentity(pout);
395 pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
396 pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
397 pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
398 pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
399 pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
400 pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
401 pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
402 pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
403 pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
404 return pout;
407 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
409 D3DXMatrixIdentity(pout);
410 pout->u.m[1][1] = cos(angle);
411 pout->u.m[2][2] = cos(angle);
412 pout->u.m[1][2] = sin(angle);
413 pout->u.m[2][1] = -sin(angle);
414 return pout;
417 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
419 D3DXMatrixIdentity(pout);
420 pout->u.m[0][0] = cos(angle);
421 pout->u.m[2][2] = cos(angle);
422 pout->u.m[0][2] = -sin(angle);
423 pout->u.m[2][0] = sin(angle);
424 return pout;
427 D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
429 D3DXMATRIX m, pout1, pout2, pout3;
431 D3DXMatrixIdentity(&pout3);
432 D3DXMatrixRotationZ(&m,roll);
433 D3DXMatrixMultiply(&pout2,&pout3,&m);
434 D3DXMatrixRotationX(&m,pitch);
435 D3DXMatrixMultiply(&pout1,&pout2,&m);
436 D3DXMatrixRotationY(&m,yaw);
437 D3DXMatrixMultiply(pout,&pout1,&m);
438 return pout;
440 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
442 D3DXMatrixIdentity(pout);
443 pout->u.m[0][0] = cos(angle);
444 pout->u.m[1][1] = cos(angle);
445 pout->u.m[0][1] = sin(angle);
446 pout->u.m[1][0] = -sin(angle);
447 return pout;
450 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
452 D3DXMatrixIdentity(pout);
453 pout->u.m[0][0] = sx;
454 pout->u.m[1][1] = sy;
455 pout->u.m[2][2] = sz;
456 return pout;
459 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, CONST D3DXVECTOR4 *plight, CONST D3DXPLANE *pplane)
461 D3DXPLANE Nplane;
462 FLOAT dot;
464 D3DXPlaneNormalize(&Nplane, pplane);
465 dot = D3DXPlaneDot(&Nplane, plight);
466 pout->u.m[0][0] = dot - Nplane.a * plight->x;
467 pout->u.m[0][1] = -Nplane.a * plight->y;
468 pout->u.m[0][2] = -Nplane.a * plight->z;
469 pout->u.m[0][3] = -Nplane.a * plight->w;
470 pout->u.m[1][0] = -Nplane.b * plight->x;
471 pout->u.m[1][1] = dot - Nplane.b * plight->y;
472 pout->u.m[1][2] = -Nplane.b * plight->z;
473 pout->u.m[1][3] = -Nplane.b * plight->w;
474 pout->u.m[2][0] = -Nplane.c * plight->x;
475 pout->u.m[2][1] = -Nplane.c * plight->y;
476 pout->u.m[2][2] = dot - Nplane.c * plight->z;
477 pout->u.m[2][3] = -Nplane.c * plight->w;
478 pout->u.m[3][0] = -Nplane.d * plight->x;
479 pout->u.m[3][1] = -Nplane.d * plight->y;
480 pout->u.m[3][2] = -Nplane.d * plight->z;
481 pout->u.m[3][3] = dot - Nplane.d * plight->w;
482 return pout;
485 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pscalingcenter, CONST D3DXQUATERNION *pscalingrotation, CONST D3DXVECTOR3 *pscaling, CONST D3DXVECTOR3 *protationcenter, CONST D3DXQUATERNION *protation, CONST D3DXVECTOR3 *ptranslation)
487 D3DXMATRIX m1, m2, m3, m4, m5, m6, m7, p1, p2, p3, p4, p5;
488 D3DXQUATERNION prc;
489 D3DXVECTOR3 psc, pt;
491 if ( !pscalingcenter )
493 psc.x = 0.0f;
494 psc.y = 0.0f;
495 psc.z = 0.0f;
497 else
499 psc.x = pscalingcenter->x;
500 psc.y = pscalingcenter->y;
501 psc.z = pscalingcenter->z;
503 if ( !protationcenter )
505 prc.x = 0.0f;
506 prc.y = 0.0f;
507 prc.z = 0.0f;
509 else
511 prc.x = protationcenter->x;
512 prc.y = protationcenter->y;
513 prc.z = protationcenter->z;
515 if ( !ptranslation )
517 pt.x = 0.0f;
518 pt.y = 0.0f;
519 pt.z = 0.0f;
521 else
523 pt.x = ptranslation->x;
524 pt.y = ptranslation->y;
525 pt.z = ptranslation->z;
527 D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
528 if ( !pscalingrotation )
530 D3DXMatrixIdentity(&m2);
531 D3DXMatrixIdentity(&m4);
533 else
535 D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
536 D3DXMatrixInverse(&m2, NULL, &m4);
538 if ( !pscaling )
540 D3DXMatrixIdentity(&m3);
542 else
544 D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
546 if ( !protation )
548 D3DXMatrixIdentity(&m6);
550 else
552 D3DXMatrixRotationQuaternion(&m6, protation);
554 D3DXMatrixTranslation(&m5, psc.x - prc.x, psc.y - prc.y, psc.z - prc.z);
555 D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
556 D3DXMatrixMultiply(&p1, &m1, &m2);
557 D3DXMatrixMultiply(&p2, &p1, &m3);
558 D3DXMatrixMultiply(&p3, &p2, &m4);
559 D3DXMatrixMultiply(&p4, &p3, &m5);
560 D3DXMatrixMultiply(&p5, &p4, &m6);
561 D3DXMatrixMultiply(pout, &p5, &m7);
562 return pout;
565 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
567 D3DXMatrixIdentity(pout);
568 pout->u.m[3][0] = x;
569 pout->u.m[3][1] = y;
570 pout->u.m[3][2] = z;
571 return pout;
574 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
576 int i,j;
578 for (i=0; i<4; i++)
580 for (j=0; j<4; j++)
582 pout->u.m[i][j] = pm->u.m[j][i];
585 return pout;
588 /*_________________D3DXMatrixStack____________________*/
590 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, LPD3DXMATRIXSTACK* ppstack)
592 ID3DXMatrixStackImpl* object;
594 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXMatrixStackImpl));
595 if ( object == NULL )
597 *ppstack = NULL;
598 return E_OUTOFMEMORY;
600 object->lpVtbl = &ID3DXMatrixStack_Vtbl;
601 object->ref = 1;
602 object->current = 0;
603 *ppstack = (LPD3DXMATRIXSTACK)object;
604 return D3D_OK;
607 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **ppobj)
609 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
610 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXMatrixStack))
612 ID3DXMatrixStack_AddRef(iface);
613 *ppobj = This;
614 return S_OK;
616 *ppobj = NULL;
617 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
618 return E_NOINTERFACE;
621 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
623 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
624 ULONG ref = InterlockedIncrement(&This->ref);
625 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
626 return ref;
629 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack* iface)
631 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
632 ULONG ref = InterlockedDecrement(&This->ref);
633 if ( !ref ) HeapFree(GetProcessHeap(), 0, This);
634 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
635 return ref;
638 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
640 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
641 FIXME("(%p) : stub\n",This);
642 return NULL;
645 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
647 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
648 FIXME("(%p) : stub\n",This);
649 return D3D_OK;
652 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, LPD3DXMATRIX pm)
654 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
655 FIXME("(%p) : stub\n",This);
656 return D3D_OK;
659 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, LPD3DXMATRIX pm)
661 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
662 FIXME("(%p) : stub\n",This);
663 return D3D_OK;
666 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, LPD3DXMATRIX pm)
668 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
669 FIXME("(%p) : stub\n",This);
670 return D3D_OK;
673 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
675 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
676 FIXME("(%p) : stub\n",This);
677 return D3D_OK;
680 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
682 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
683 FIXME("(%p) : stub\n",This);
684 return D3D_OK;
687 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, LPD3DXVECTOR3 pv, FLOAT angle)
689 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
690 FIXME("(%p) : stub\n",This);
691 return D3D_OK;
694 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, LPD3DXVECTOR3 pv, FLOAT angle)
696 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
697 FIXME("(%p) : stub\n",This);
698 return D3D_OK;
701 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
703 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
704 FIXME("(%p) : stub\n",This);
705 return D3D_OK;
708 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
710 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
711 FIXME("(%p) : stub\n",This);
712 return D3D_OK;
715 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
717 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
718 FIXME("(%p) : stub\n",This);
719 return D3D_OK;
722 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
724 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
725 FIXME("(%p) : stub\n",This);
726 return D3D_OK;
729 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
731 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
732 FIXME("(%p) : stub\n",This);
733 return D3D_OK;
736 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
738 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
739 FIXME("(%p) : stub\n",This);
740 return D3D_OK;
743 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
745 ID3DXMatrixStackImpl_QueryInterface,
746 ID3DXMatrixStackImpl_AddRef,
747 ID3DXMatrixStackImpl_Release,
748 ID3DXMatrixStackImpl_Pop,
749 ID3DXMatrixStackImpl_Push,
750 ID3DXMatrixStackImpl_LoadIdentity,
751 ID3DXMatrixStackImpl_LoadMatrix,
752 ID3DXMatrixStackImpl_MultMatrix,
753 ID3DXMatrixStackImpl_MultMatrixLocal,
754 ID3DXMatrixStackImpl_RotateAxis,
755 ID3DXMatrixStackImpl_RotateAxisLocal,
756 ID3DXMatrixStackImpl_RotateYawPitchRoll,
757 ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
758 ID3DXMatrixStackImpl_Scale,
759 ID3DXMatrixStackImpl_ScaleLocal,
760 ID3DXMatrixStackImpl_Translate,
761 ID3DXMatrixStackImpl_TranslateLocal,
762 ID3DXMatrixStackImpl_GetTop
765 /*_________________D3DXPLANE________________*/
767 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, CONST D3DXVECTOR3 *pvpoint, CONST D3DXVECTOR3 *pvnormal)
769 pout->a = pvnormal->x;
770 pout->b = pvnormal->y;
771 pout->c = pvnormal->z;
772 pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
773 return pout;
776 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3)
778 D3DXVECTOR3 edge1, edge2, normal, Nnormal;
780 edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
781 edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
782 D3DXVec3Subtract(&edge1, pv2, pv1);
783 D3DXVec3Subtract(&edge2, pv3, pv1);
784 D3DXVec3Cross(&normal, &edge1, &edge2);
785 D3DXVec3Normalize(&Nnormal, &normal);
786 D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
787 return pout;
790 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, CONST D3DXPLANE *pp, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
792 D3DXVECTOR3 direction, normal;
793 FLOAT dot, temp;
795 normal.x = pp->a;
796 normal.y = pp->b;
797 normal.z = pp->c;
798 direction.x = pv2->x - pv1->x;
799 direction.y = pv2->y - pv1->y;
800 direction.z = pv2->z - pv1->z;
801 dot = D3DXVec3Dot(&normal, &direction);
802 if ( !dot ) return NULL;
803 temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
804 pout->x = pv1->x - temp * direction.x;
805 pout->y = pv1->y - temp * direction.y;
806 pout->z = pv1->z - temp * direction.z;
807 return pout;
810 D3DXPLANE* WINAPI D3DXPlaneNormalize(D3DXPLANE *pout, CONST D3DXPLANE *pp)
812 FLOAT norm;
814 norm = sqrt(pp->a * pp->a + pp->b * pp->b + pp->c * pp->c);
815 if ( norm )
817 pout->a = pp->a / norm;
818 pout->b = pp->b / norm;
819 pout->c = pp->c / norm;
820 pout->d = pp->d / norm;
822 else
824 pout->a = 0.0f;
825 pout->b = 0.0f;
826 pout->c = 0.0f;
827 pout->d = 0.0f;
829 return pout;
832 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, CONST D3DXPLANE *pplane, CONST D3DXMATRIX *pm)
834 pout->a = pm->u.m[0][0] * pplane->a + pm->u.m[1][0] * pplane->b + pm->u.m[2][0] * pplane->c + pm->u.m[3][0] * pplane->d;
835 pout->b = pm->u.m[0][1] * pplane->a + pm->u.m[1][1] * pplane->b + pm->u.m[2][1] * pplane->c + pm->u.m[3][1] * pplane->d;
836 pout->c = pm->u.m[0][2] * pplane->a + pm->u.m[1][2] * pplane->b + pm->u.m[2][2] * pplane->c + pm->u.m[3][2] * pplane->d;
837 pout->d = pm->u.m[0][3] * pplane->a + pm->u.m[1][3] * pplane->b + pm->u.m[2][3] * pplane->c + pm->u.m[3][3] * pplane->d;
838 return pout;
841 /*_________________D3DXQUATERNION________________*/
843 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
845 D3DXQUATERNION temp1, temp2;
846 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
847 return pout;
850 D3DXQUATERNION* WINAPI D3DXQuaternionExp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
852 FLOAT norm;
854 norm = sqrt(pq->x * pq->x + pq->y * pq->y + pq->z * pq->z);
855 if (norm )
857 pout->x = sin(norm) * pq->x / norm;
858 pout->y = sin(norm) * pq->y / norm;
859 pout->z = sin(norm) * pq->z / norm;
860 pout->w = cos(norm);
862 else
864 pout->x = 0.0f;
865 pout->y = 0.0f;
866 pout->z = 0.0f;
867 pout->w = 1.0f;
869 return pout;
872 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
874 D3DXQUATERNION temp;
875 FLOAT norm;
877 temp.x = 0.0f;
878 temp.y = 0.0f;
879 temp.z = 0.0f;
880 temp.w = 0.0f;
882 norm = D3DXQuaternionLengthSq(pq);
883 if ( !norm )
885 pout->x = 0.0f;
886 pout->y = 0.0f;
887 pout->z = 0.0f;
888 pout->w = 0.0f;
890 else
892 D3DXQuaternionConjugate(&temp, pq);
893 pout->x = temp.x / norm;
894 pout->y = temp.y / norm;
895 pout->z = temp.z / norm;
896 pout->w = temp.w / norm;
898 return pout;
901 D3DXQUATERNION* WINAPI D3DXQuaternionLn(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
903 FLOAT norm, normvec, theta;
905 norm = D3DXQuaternionLengthSq(pq);
906 if ( norm > 1.0001f )
908 pout->x = pq->x;
909 pout->y = pq->y;
910 pout->z = pq->z;
911 pout->w = 0.0f;
913 else if( norm > 0.99999f)
915 normvec = sqrt( pq->x * pq->x + pq->y * pq->y + pq->z * pq->z );
916 theta = atan2(normvec, pq->w) / normvec;
917 pout->x = theta * pq->x;
918 pout->y = theta * pq->y;
919 pout->z = theta * pq->z;
920 pout->w = 0.0f;
922 else
924 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);
926 return pout;
929 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
931 pout->x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
932 pout->y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
933 pout->z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
934 pout->w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
935 return pout;
938 D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
940 FLOAT norm;
942 norm = D3DXQuaternionLength(pq);
943 if ( !norm )
945 pout->x = 0.0f;
946 pout->y = 0.0f;
947 pout->z = 0.0f;
948 pout->w = 0.0f;
950 else
952 pout->x = pq->x / norm;
953 pout->y = pq->y / norm;
954 pout->z = pq->z / norm;
955 pout->w = pq->w / norm;
957 return pout;
960 D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
962 D3DXVECTOR3 temp;
964 D3DXVec3Normalize(&temp, pv);
965 pout->x = sin( angle / 2.0f ) * temp.x;
966 pout->y = sin( angle / 2.0f ) * temp.y;
967 pout->z = sin( angle / 2.0f ) * temp.z;
968 pout->w = cos( angle / 2.0f );
969 return pout;
972 D3DXQUATERNION* WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *pout, CONST D3DXMATRIX *pm)
974 int i, maxi;
975 FLOAT maxdiag, S, trace;
977 trace = pm->u.m[0][0] + pm->u.m[1][1] + pm->u.m[2][2] + 1.0f;
978 if ( trace > 0.0f)
980 pout->x = ( pm->u.m[1][2] - pm->u.m[2][1] ) / ( 2.0f * sqrt(trace) );
981 pout->y = ( pm->u.m[2][0] - pm->u.m[0][2] ) / ( 2.0f * sqrt(trace) );
982 pout->z = ( pm->u.m[0][1] - pm->u.m[1][0] ) / ( 2.0f * sqrt(trace) );
983 pout->w = sqrt(trace) / 2.0f;
984 return pout;
986 maxi = 0;
987 maxdiag = pm->u.m[0][0];
988 for (i=1; i<3; i++)
990 if ( pm->u.m[i][i] > maxdiag )
992 maxi = i;
993 maxdiag = pm->u.m[i][i];
996 switch( maxi )
998 case 0:
999 S = 2.0f * sqrt(1.0f + pm->u.m[0][0] - pm->u.m[1][1] - pm->u.m[2][2]);
1000 pout->x = 0.25f * S;
1001 pout->y = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1002 pout->z = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1003 pout->w = ( pm->u.m[1][2] - pm->u.m[2][1] ) / S;
1004 break;
1005 case 1:
1006 S = 2.0f * sqrt(1.0f + pm->u.m[1][1] - pm->u.m[0][0] - pm->u.m[2][2]);
1007 pout->x = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1008 pout->y = 0.25f * S;
1009 pout->z = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1010 pout->w = ( pm->u.m[2][0] - pm->u.m[0][2] ) / S;
1011 break;
1012 case 2:
1013 S = 2.0f * sqrt(1.0f + pm->u.m[2][2] - pm->u.m[0][0] - pm->u.m[1][1]);
1014 pout->x = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1015 pout->y = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1016 pout->z = 0.25f * S;
1017 pout->w = ( pm->u.m[0][1] - pm->u.m[1][0] ) / S;
1018 break;
1020 return pout;
1023 D3DXQUATERNION* WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
1025 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);
1026 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);
1027 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);
1028 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);
1029 return pout;
1032 D3DXQUATERNION* WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, FLOAT t)
1034 FLOAT dot, epsilon;
1036 epsilon = 1.0f;
1037 dot = D3DXQuaternionDot(pq1, pq2);
1038 if ( dot < 0.0f) epsilon = -1.0f;
1039 pout->x = (1.0f - t) * pq1->x + epsilon * t * pq2->x;
1040 pout->y = (1.0f - t) * pq1->y + epsilon * t * pq2->y;
1041 pout->z = (1.0f - t) * pq1->z + epsilon * t * pq2->z;
1042 pout->w = (1.0f - t) * pq1->w + epsilon * t * pq2->w;
1043 return pout;
1046 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, CONST D3DXQUATERNION *pq4, FLOAT t)
1048 D3DXQUATERNION temp1, temp2;
1050 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1051 return pout;
1054 void WINAPI D3DXQuaternionToAxisAngle(CONST D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1056 FLOAT norm;
1058 *pangle = 0.0f;
1059 norm = D3DXQuaternionLength(pq);
1060 if ( norm )
1062 paxis->x = pq->x / norm;
1063 paxis->y = pq->y / norm;
1064 paxis->z = pq->z / norm;
1065 if ( fabs( pq->w ) <= 1.0f ) *pangle = 2.0f * acos(pq->w);
1067 else
1069 paxis->x = 1.0f;
1070 paxis->y = 0.0f;
1071 paxis->z = 0.0f;
1075 /*_________________D3DXVec2_____________________*/
1077 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1079 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1080 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1081 return pout;
1084 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv0, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT s)
1086 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);
1087 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);
1088 return pout;
1091 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pt1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pt2, FLOAT s)
1093 FLOAT h1, h2, h3, h4;
1095 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1096 h2 = s * s * s - 2.0f * s * s + s;
1097 h3 = -2.0f * s * s * s + 3.0f * s * s;
1098 h4 = s * s * s - s * s;
1100 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1101 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1102 return pout;
1105 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
1107 FLOAT norm;
1109 norm = D3DXVec2Length(pv);
1110 if ( !norm )
1112 pout->x = 0.0f;
1113 pout->y = 0.0f;
1115 else
1117 pout->x = pv->x / norm;
1118 pout->y = pv->y / norm;
1120 return pout;
1123 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1125 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
1126 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
1127 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
1128 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1129 return pout;
1132 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1134 FLOAT norm;
1136 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1137 if ( norm )
1139 pout->x = (pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0]) / norm;
1140 pout->y = (pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1]) / norm;
1142 else
1144 pout->x = 0.0f;
1145 pout->y = 0.0f;
1147 return pout;
1150 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1152 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y;
1153 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y;
1154 return pout;
1157 /*_________________D3DXVec3_____________________*/
1159 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1161 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1162 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1163 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1164 return pout;
1167 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
1169 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);
1170 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);
1171 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);
1172 return pout;
1175 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
1177 FLOAT h1, h2, h3, h4;
1179 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1180 h2 = s * s * s - 2.0f * s * s + s;
1181 h3 = -2.0f * s * s * s + 3.0f * s * s;
1182 h4 = s * s * s - s * s;
1184 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1185 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1186 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1187 return pout;
1190 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
1192 FLOAT norm;
1194 norm = D3DXVec3Length(pv);
1195 if ( !norm )
1197 pout->x = 0.0f;
1198 pout->y = 0.0f;
1199 pout->z = 0.0f;
1201 else
1203 pout->x = pv->x / norm;
1204 pout->y = pv->y / norm;
1205 pout->z = pv->z / norm;
1207 return pout;
1210 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT8 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1212 D3DXMATRIX m1, m2;
1213 D3DXVECTOR3 vec;
1215 D3DXMatrixMultiply(&m1, pworld, pview);
1216 D3DXMatrixMultiply(&m2, &m1, pprojection);
1217 D3DXVec3TransformCoord(&vec, pv, &m2);
1218 pout->x = pviewport->X + ( 1.0f + vec.x ) * pviewport->Width / 2.0f;
1219 pout->y = pviewport->Y + ( 1.0f - vec.y ) * pviewport->Height / 2.0f;
1220 pout->z = pviewport->MinZ + vec.z * ( pviewport->MaxZ - pviewport->MinZ );
1221 return pout;
1224 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1226 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];
1227 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];
1228 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];
1229 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];
1230 return pout;
1233 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1235 FLOAT norm;
1237 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];
1239 if ( norm )
1241 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]) / norm;
1242 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]) / norm;
1243 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]) / norm;
1245 else
1247 pout->x = 0.0f;
1248 pout->y = 0.0f;
1249 pout->z = 0.0f;
1251 return pout;
1254 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1256 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z;
1257 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z;
1258 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z;
1259 return pout;
1263 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT8 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1265 D3DXMATRIX m1, m2, m3;
1266 D3DXVECTOR3 vec;
1268 D3DXMatrixMultiply(&m1, pworld, pview);
1269 D3DXMatrixMultiply(&m2, &m1, pprojection);
1270 D3DXMatrixInverse(&m3, NULL, &m2);
1271 vec.x = 2.0f * ( pv->x - pviewport->X ) / pviewport->Width - 1.0f;
1272 vec.y = 1.0f - 2.0f * ( pv->y - pviewport->Y ) / pviewport->Height;
1273 vec.z = ( pv->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
1274 D3DXVec3TransformCoord(pout, &vec, &m3);
1275 return pout;
1278 /*_________________D3DXVec4_____________________*/
1280 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
1282 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1283 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1284 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1285 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
1286 return pout;
1289 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv0, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT s)
1291 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);
1292 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);
1293 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);
1294 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);
1295 return pout;
1298 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
1300 pout->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);
1301 pout->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));
1302 pout->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);
1303 pout->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));
1304 return pout;
1307 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pt1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pt2, FLOAT s)
1309 FLOAT h1, h2, h3, h4;
1311 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1312 h2 = s * s * s - 2.0f * s * s + s;
1313 h3 = -2.0f * s * s * s + 3.0f * s * s;
1314 h4 = s * s * s - s * s;
1316 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1317 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1318 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1319 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
1320 return pout;
1323 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
1325 FLOAT norm;
1327 norm = D3DXVec4Length(pv);
1328 if ( !norm )
1330 pout->x = 0.0f;
1331 pout->y = 0.0f;
1332 pout->z = 0.0f;
1333 pout->w = 0.0f;
1335 else
1337 pout->x = pv->x / norm;
1338 pout->y = pv->y / norm;
1339 pout->z = pv->z / norm;
1340 pout->w = pv->w / norm;
1342 return pout;
1345 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
1347 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] * pv->w;
1348 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] * pv->w;
1349 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] * pv->w;
1350 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] * pv->w;
1351 return pout;