oleaut32: Move the debug logging of the variable type in SLTG_DoVars.
[wine/gsoc_dplay.git] / dlls / d3dx8 / math.c
blob93a0b49131e553287f48676fb27e4e398a95f3cc
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 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 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;
142 return pout;
145 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
147 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
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;
174 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
176 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
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;
203 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
205 D3DXMATRIX out;
206 int i,j;
208 for (i=0; i<4; i++)
210 for (j=0; j<4; j++)
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];
215 *pout = out;
216 return pout;
219 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
221 D3DXMATRIX temp;
223 D3DXMatrixMultiply(&temp, pm1, pm2);
224 D3DXMatrixTranspose(pout, &temp);
225 return pout;
228 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
230 D3DXMatrixIdentity(pout);
231 pout->u.m[0][0] = 2.0f / w;
232 pout->u.m[1][1] = 2.0f / h;
233 pout->u.m[2][2] = 1.0f / (zf - zn);
234 pout->u.m[3][2] = zn / (zn - zf);
235 return pout;
238 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
240 D3DXMatrixIdentity(pout);
241 pout->u.m[0][0] = 2.0f / (r - l);
242 pout->u.m[1][1] = 2.0f / (t - b);
243 pout->u.m[2][2] = 1.0f / (zf -zn);
244 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
245 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
246 pout->u.m[3][2] = zn / (zn -zf);
247 return pout;
250 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
252 D3DXMatrixIdentity(pout);
253 pout->u.m[0][0] = 2.0f / (r - l);
254 pout->u.m[1][1] = 2.0f / (t - b);
255 pout->u.m[2][2] = 1.0f / (zn -zf);
256 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
257 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
258 pout->u.m[3][2] = zn / (zn -zf);
259 return pout;
262 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
264 D3DXMatrixIdentity(pout);
265 pout->u.m[0][0] = 2.0f / w;
266 pout->u.m[1][1] = 2.0f / h;
267 pout->u.m[2][2] = 1.0f / (zn - zf);
268 pout->u.m[3][2] = zn / (zn - zf);
269 return pout;
272 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
274 D3DXMatrixIdentity(pout);
275 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
276 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
277 pout->u.m[2][2] = zf / (zf - zn);
278 pout->u.m[2][3] = 1.0f;
279 pout->u.m[3][2] = (zf * zn) / (zn - zf);
280 pout->u.m[3][3] = 0.0f;
281 return pout;
284 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
286 D3DXMatrixIdentity(pout);
287 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
288 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
289 pout->u.m[2][2] = zf / (zn - zf);
290 pout->u.m[2][3] = -1.0f;
291 pout->u.m[3][2] = (zf * zn) / (zn - zf);
292 pout->u.m[3][3] = 0.0f;
293 return pout;
296 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
298 D3DXMatrixIdentity(pout);
299 pout->u.m[0][0] = 2.0f * zn / w;
300 pout->u.m[1][1] = 2.0f * zn / h;
301 pout->u.m[2][2] = zf / (zf - zn);
302 pout->u.m[3][2] = (zn * zf) / (zn - zf);
303 pout->u.m[2][3] = 1.0f;
304 pout->u.m[3][3] = 0.0f;
305 return pout;
308 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
310 D3DXMatrixIdentity(pout);
311 pout->u.m[0][0] = 2.0f * zn / (r - l);
312 pout->u.m[1][1] = -2.0f * zn / (b - t);
313 pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
314 pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
315 pout->u.m[2][2] = - zf / (zn - zf);
316 pout->u.m[3][2] = (zn * zf) / (zn -zf);
317 pout->u.m[2][3] = 1.0f;
318 pout->u.m[3][3] = 0.0f;
319 return pout;
322 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
324 D3DXMatrixIdentity(pout);
325 pout->u.m[0][0] = 2.0f * zn / (r - l);
326 pout->u.m[1][1] = -2.0f * zn / (b - t);
327 pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
328 pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
329 pout->u.m[2][2] = zf / (zn - zf);
330 pout->u.m[3][2] = (zn * zf) / (zn -zf);
331 pout->u.m[2][3] = -1.0f;
332 pout->u.m[3][3] = 0.0f;
333 return pout;
336 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
338 D3DXMatrixIdentity(pout);
339 pout->u.m[0][0] = 2.0f * zn / w;
340 pout->u.m[1][1] = 2.0f * zn / h;
341 pout->u.m[2][2] = zf / (zn - zf);
342 pout->u.m[3][2] = (zn * zf) / (zn - zf);
343 pout->u.m[2][3] = -1.0f;
344 pout->u.m[3][3] = 0.0f;
345 return pout;
348 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, CONST D3DXPLANE *pplane)
350 D3DXPLANE Nplane;
352 D3DXPlaneNormalize(&Nplane, pplane);
353 D3DXMatrixIdentity(pout);
354 pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
355 pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
356 pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
357 pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
358 pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
359 pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
360 pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
361 pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
362 pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
363 pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
364 pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
365 pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
366 return pout;
369 D3DXMATRIX* WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
371 D3DXVECTOR3 v;
373 D3DXVec3Normalize(&v,pv);
374 D3DXMatrixIdentity(pout);
375 pout->u.m[0][0] = (1.0f - cos(angle)) * v.x * v.x + cos(angle);
376 pout->u.m[1][0] = (1.0f - cos(angle)) * v.x * v.y - sin(angle) * v.z;
377 pout->u.m[2][0] = (1.0f - cos(angle)) * v.x * v.z + sin(angle) * v.y;
378 pout->u.m[0][1] = (1.0f - cos(angle)) * v.y * v.x + sin(angle) * v.z;
379 pout->u.m[1][1] = (1.0f - cos(angle)) * v.y * v.y + cos(angle);
380 pout->u.m[2][1] = (1.0f - cos(angle)) * v.y * v.z - sin(angle) * v.x;
381 pout->u.m[0][2] = (1.0f - cos(angle)) * v.z * v.x - sin(angle) * v.y;
382 pout->u.m[1][2] = (1.0f - cos(angle)) * v.z * v.y + sin(angle) * v.x;
383 pout->u.m[2][2] = (1.0f - cos(angle)) * v.z * v.z + cos(angle);
384 return pout;
387 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, CONST D3DXQUATERNION *pq)
389 D3DXMatrixIdentity(pout);
390 pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
391 pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
392 pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
393 pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
394 pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
395 pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
396 pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
397 pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
398 pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
399 return pout;
402 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
404 D3DXMatrixIdentity(pout);
405 pout->u.m[1][1] = cos(angle);
406 pout->u.m[2][2] = cos(angle);
407 pout->u.m[1][2] = sin(angle);
408 pout->u.m[2][1] = -sin(angle);
409 return pout;
412 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
414 D3DXMatrixIdentity(pout);
415 pout->u.m[0][0] = cos(angle);
416 pout->u.m[2][2] = cos(angle);
417 pout->u.m[0][2] = -sin(angle);
418 pout->u.m[2][0] = sin(angle);
419 return pout;
422 D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
424 D3DXMATRIX m, pout1, pout2, pout3;
426 D3DXMatrixIdentity(&pout3);
427 D3DXMatrixRotationZ(&m,roll);
428 D3DXMatrixMultiply(&pout2,&pout3,&m);
429 D3DXMatrixRotationX(&m,pitch);
430 D3DXMatrixMultiply(&pout1,&pout2,&m);
431 D3DXMatrixRotationY(&m,yaw);
432 D3DXMatrixMultiply(pout,&pout1,&m);
433 return pout;
435 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
437 D3DXMatrixIdentity(pout);
438 pout->u.m[0][0] = cos(angle);
439 pout->u.m[1][1] = cos(angle);
440 pout->u.m[0][1] = sin(angle);
441 pout->u.m[1][0] = -sin(angle);
442 return pout;
445 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
447 D3DXMatrixIdentity(pout);
448 pout->u.m[0][0] = sx;
449 pout->u.m[1][1] = sy;
450 pout->u.m[2][2] = sz;
451 return pout;
454 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, CONST D3DXVECTOR4 *plight, CONST D3DXPLANE *pplane)
456 D3DXPLANE Nplane;
457 FLOAT dot;
459 D3DXPlaneNormalize(&Nplane, pplane);
460 dot = D3DXPlaneDot(&Nplane, plight);
461 pout->u.m[0][0] = dot - Nplane.a * plight->x;
462 pout->u.m[0][1] = -Nplane.a * plight->y;
463 pout->u.m[0][2] = -Nplane.a * plight->z;
464 pout->u.m[0][3] = -Nplane.a * plight->w;
465 pout->u.m[1][0] = -Nplane.b * plight->x;
466 pout->u.m[1][1] = dot - Nplane.b * plight->y;
467 pout->u.m[1][2] = -Nplane.b * plight->z;
468 pout->u.m[1][3] = -Nplane.b * plight->w;
469 pout->u.m[2][0] = -Nplane.c * plight->x;
470 pout->u.m[2][1] = -Nplane.c * plight->y;
471 pout->u.m[2][2] = dot - Nplane.c * plight->z;
472 pout->u.m[2][3] = -Nplane.c * plight->w;
473 pout->u.m[3][0] = -Nplane.d * plight->x;
474 pout->u.m[3][1] = -Nplane.d * plight->y;
475 pout->u.m[3][2] = -Nplane.d * plight->z;
476 pout->u.m[3][3] = dot - Nplane.d * plight->w;
477 return pout;
480 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pscalingcenter, CONST D3DXQUATERNION *pscalingrotation, CONST D3DXVECTOR3 *pscaling, CONST D3DXVECTOR3 *protationcenter, CONST D3DXQUATERNION *protation, CONST D3DXVECTOR3 *ptranslation)
482 D3DXMATRIX m1, m2, m3, m4, m5, m6, m7, p1, p2, p3, p4, p5;
483 D3DXQUATERNION prc;
484 D3DXVECTOR3 psc, pt;
486 if ( !pscalingcenter )
488 psc.x = 0.0f;
489 psc.y = 0.0f;
490 psc.z = 0.0f;
492 else
494 psc.x = pscalingcenter->x;
495 psc.y = pscalingcenter->y;
496 psc.z = pscalingcenter->z;
498 if ( !protationcenter )
500 prc.x = 0.0f;
501 prc.y = 0.0f;
502 prc.z = 0.0f;
504 else
506 prc.x = protationcenter->x;
507 prc.y = protationcenter->y;
508 prc.z = protationcenter->z;
510 if ( !ptranslation )
512 pt.x = 0.0f;
513 pt.y = 0.0f;
514 pt.z = 0.0f;
516 else
518 pt.x = ptranslation->x;
519 pt.y = ptranslation->y;
520 pt.z = ptranslation->z;
522 D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
523 if ( !pscalingrotation )
525 D3DXMatrixIdentity(&m2);
526 D3DXMatrixIdentity(&m4);
528 else
530 D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
531 D3DXMatrixInverse(&m2, NULL, &m4);
533 if ( !pscaling )
535 D3DXMatrixIdentity(&m3);
537 else
539 D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
541 if ( !protation )
543 D3DXMatrixIdentity(&m6);
545 else
547 D3DXMatrixRotationQuaternion(&m6, protation);
549 D3DXMatrixTranslation(&m5, psc.x - prc.x, psc.y - prc.y, psc.z - prc.z);
550 D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
551 D3DXMatrixMultiply(&p1, &m1, &m2);
552 D3DXMatrixMultiply(&p2, &p1, &m3);
553 D3DXMatrixMultiply(&p3, &p2, &m4);
554 D3DXMatrixMultiply(&p4, &p3, &m5);
555 D3DXMatrixMultiply(&p5, &p4, &m6);
556 D3DXMatrixMultiply(pout, &p5, &m7);
557 return pout;
560 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
562 D3DXMatrixIdentity(pout);
563 pout->u.m[3][0] = x;
564 pout->u.m[3][1] = y;
565 pout->u.m[3][2] = z;
566 return pout;
569 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
571 CONST D3DXMATRIX m = *pm;
572 int i,j;
574 for (i=0; i<4; i++)
576 for (j=0; j<4; j++)
578 pout->u.m[i][j] = m.u.m[j][i];
581 return pout;
584 /*_________________D3DXMatrixStack____________________*/
586 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, LPD3DXMATRIXSTACK* ppstack)
588 ID3DXMatrixStackImpl* object;
590 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXMatrixStackImpl));
591 if ( object == NULL )
593 *ppstack = NULL;
594 return E_OUTOFMEMORY;
596 object->lpVtbl = &ID3DXMatrixStack_Vtbl;
597 object->ref = 1;
598 object->current = 0;
599 *ppstack = (LPD3DXMATRIXSTACK)object;
600 return D3D_OK;
603 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **ppobj)
605 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
606 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXMatrixStack))
608 ID3DXMatrixStack_AddRef(iface);
609 *ppobj = This;
610 return S_OK;
612 *ppobj = NULL;
613 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
614 return E_NOINTERFACE;
617 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
619 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
620 ULONG ref = InterlockedIncrement(&This->ref);
621 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
622 return ref;
625 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack* iface)
627 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
628 ULONG ref = InterlockedDecrement(&This->ref);
629 if ( !ref ) HeapFree(GetProcessHeap(), 0, This);
630 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
631 return ref;
634 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
636 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
637 FIXME("(%p) : stub\n",This);
638 return NULL;
641 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
643 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
644 FIXME("(%p) : stub\n",This);
645 return D3D_OK;
648 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, LPD3DXMATRIX pm)
650 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
651 FIXME("(%p) : stub\n",This);
652 return D3D_OK;
655 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, LPD3DXMATRIX pm)
657 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
658 FIXME("(%p) : stub\n",This);
659 return D3D_OK;
662 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, LPD3DXMATRIX pm)
664 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
665 FIXME("(%p) : stub\n",This);
666 return D3D_OK;
669 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
671 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
672 FIXME("(%p) : stub\n",This);
673 return D3D_OK;
676 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
678 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
679 FIXME("(%p) : stub\n",This);
680 return D3D_OK;
683 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, LPD3DXVECTOR3 pv, FLOAT angle)
685 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
686 FIXME("(%p) : stub\n",This);
687 return D3D_OK;
690 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, LPD3DXVECTOR3 pv, FLOAT angle)
692 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
693 FIXME("(%p) : stub\n",This);
694 return D3D_OK;
697 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
699 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
700 FIXME("(%p) : stub\n",This);
701 return D3D_OK;
704 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
706 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
707 FIXME("(%p) : stub\n",This);
708 return D3D_OK;
711 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
713 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
714 FIXME("(%p) : stub\n",This);
715 return D3D_OK;
718 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
720 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
721 FIXME("(%p) : stub\n",This);
722 return D3D_OK;
725 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
727 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
728 FIXME("(%p) : stub\n",This);
729 return D3D_OK;
732 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
734 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
735 FIXME("(%p) : stub\n",This);
736 return D3D_OK;
739 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
741 ID3DXMatrixStackImpl_QueryInterface,
742 ID3DXMatrixStackImpl_AddRef,
743 ID3DXMatrixStackImpl_Release,
744 ID3DXMatrixStackImpl_Pop,
745 ID3DXMatrixStackImpl_Push,
746 ID3DXMatrixStackImpl_LoadIdentity,
747 ID3DXMatrixStackImpl_LoadMatrix,
748 ID3DXMatrixStackImpl_MultMatrix,
749 ID3DXMatrixStackImpl_MultMatrixLocal,
750 ID3DXMatrixStackImpl_RotateAxis,
751 ID3DXMatrixStackImpl_RotateAxisLocal,
752 ID3DXMatrixStackImpl_RotateYawPitchRoll,
753 ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
754 ID3DXMatrixStackImpl_Scale,
755 ID3DXMatrixStackImpl_ScaleLocal,
756 ID3DXMatrixStackImpl_Translate,
757 ID3DXMatrixStackImpl_TranslateLocal,
758 ID3DXMatrixStackImpl_GetTop
761 /*_________________D3DXPLANE________________*/
763 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, CONST D3DXVECTOR3 *pvpoint, CONST D3DXVECTOR3 *pvnormal)
765 pout->a = pvnormal->x;
766 pout->b = pvnormal->y;
767 pout->c = pvnormal->z;
768 pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
769 return pout;
772 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3)
774 D3DXVECTOR3 edge1, edge2, normal, Nnormal;
776 edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
777 edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
778 D3DXVec3Subtract(&edge1, pv2, pv1);
779 D3DXVec3Subtract(&edge2, pv3, pv1);
780 D3DXVec3Cross(&normal, &edge1, &edge2);
781 D3DXVec3Normalize(&Nnormal, &normal);
782 D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
783 return pout;
786 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, CONST D3DXPLANE *pp, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
788 D3DXVECTOR3 direction, normal;
789 FLOAT dot, temp;
791 normal.x = pp->a;
792 normal.y = pp->b;
793 normal.z = pp->c;
794 direction.x = pv2->x - pv1->x;
795 direction.y = pv2->y - pv1->y;
796 direction.z = pv2->z - pv1->z;
797 dot = D3DXVec3Dot(&normal, &direction);
798 if ( !dot ) return NULL;
799 temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
800 pout->x = pv1->x - temp * direction.x;
801 pout->y = pv1->y - temp * direction.y;
802 pout->z = pv1->z - temp * direction.z;
803 return pout;
806 D3DXPLANE* WINAPI D3DXPlaneNormalize(D3DXPLANE *pout, CONST D3DXPLANE *pp)
808 FLOAT norm;
810 norm = sqrt(pp->a * pp->a + pp->b * pp->b + pp->c * pp->c);
811 if ( norm )
813 pout->a = pp->a / norm;
814 pout->b = pp->b / norm;
815 pout->c = pp->c / norm;
816 pout->d = pp->d / norm;
818 else
820 pout->a = 0.0f;
821 pout->b = 0.0f;
822 pout->c = 0.0f;
823 pout->d = 0.0f;
825 return pout;
828 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, CONST D3DXPLANE *pplane, CONST D3DXMATRIX *pm)
830 CONST D3DXPLANE plane = *pplane;
831 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;
832 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;
833 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;
834 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;
835 return pout;
838 /*_________________D3DXQUATERNION________________*/
840 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
842 D3DXQUATERNION temp1, temp2;
843 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
844 return pout;
847 D3DXQUATERNION* WINAPI D3DXQuaternionExp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
849 FLOAT norm;
851 norm = sqrt(pq->x * pq->x + pq->y * pq->y + pq->z * pq->z);
852 if (norm )
854 pout->x = sin(norm) * pq->x / norm;
855 pout->y = sin(norm) * pq->y / norm;
856 pout->z = sin(norm) * pq->z / norm;
857 pout->w = cos(norm);
859 else
861 pout->x = 0.0f;
862 pout->y = 0.0f;
863 pout->z = 0.0f;
864 pout->w = 1.0f;
866 return pout;
869 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
871 D3DXQUATERNION temp;
872 FLOAT norm;
874 temp.x = 0.0f;
875 temp.y = 0.0f;
876 temp.z = 0.0f;
877 temp.w = 0.0f;
879 norm = D3DXQuaternionLengthSq(pq);
880 if ( !norm )
882 pout->x = 0.0f;
883 pout->y = 0.0f;
884 pout->z = 0.0f;
885 pout->w = 0.0f;
887 else
889 D3DXQuaternionConjugate(&temp, pq);
890 pout->x = temp.x / norm;
891 pout->y = temp.y / norm;
892 pout->z = temp.z / norm;
893 pout->w = temp.w / norm;
895 return pout;
898 D3DXQUATERNION* WINAPI D3DXQuaternionLn(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
900 FLOAT norm, normvec, theta;
902 norm = D3DXQuaternionLengthSq(pq);
903 if ( norm > 1.0001f )
905 pout->x = pq->x;
906 pout->y = pq->y;
907 pout->z = pq->z;
908 pout->w = 0.0f;
910 else if( norm > 0.99999f)
912 normvec = sqrt( pq->x * pq->x + pq->y * pq->y + pq->z * pq->z );
913 theta = atan2(normvec, pq->w) / normvec;
914 pout->x = theta * pq->x;
915 pout->y = theta * pq->y;
916 pout->z = theta * pq->z;
917 pout->w = 0.0f;
919 else
921 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);
923 return pout;
926 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
928 D3DXQUATERNION out;
929 out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
930 out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
931 out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
932 out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
933 *pout = out;
934 return pout;
937 D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
939 FLOAT norm;
941 norm = D3DXQuaternionLength(pq);
942 if ( !norm )
944 pout->x = 0.0f;
945 pout->y = 0.0f;
946 pout->z = 0.0f;
947 pout->w = 0.0f;
949 else
951 pout->x = pq->x / norm;
952 pout->y = pq->y / norm;
953 pout->z = pq->z / norm;
954 pout->w = pq->w / norm;
956 return pout;
959 D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
961 D3DXVECTOR3 temp;
963 D3DXVec3Normalize(&temp, pv);
964 pout->x = sin( angle / 2.0f ) * temp.x;
965 pout->y = sin( angle / 2.0f ) * temp.y;
966 pout->z = sin( angle / 2.0f ) * temp.z;
967 pout->w = cos( angle / 2.0f );
968 return pout;
971 D3DXQUATERNION* WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *pout, CONST D3DXMATRIX *pm)
973 int i, maxi;
974 FLOAT maxdiag, S, trace;
976 trace = pm->u.m[0][0] + pm->u.m[1][1] + pm->u.m[2][2] + 1.0f;
977 if ( trace > 0.0f)
979 pout->x = ( pm->u.m[1][2] - pm->u.m[2][1] ) / ( 2.0f * sqrt(trace) );
980 pout->y = ( pm->u.m[2][0] - pm->u.m[0][2] ) / ( 2.0f * sqrt(trace) );
981 pout->z = ( pm->u.m[0][1] - pm->u.m[1][0] ) / ( 2.0f * sqrt(trace) );
982 pout->w = sqrt(trace) / 2.0f;
983 return pout;
985 maxi = 0;
986 maxdiag = pm->u.m[0][0];
987 for (i=1; i<3; i++)
989 if ( pm->u.m[i][i] > maxdiag )
991 maxi = i;
992 maxdiag = pm->u.m[i][i];
995 switch( maxi )
997 case 0:
998 S = 2.0f * sqrt(1.0f + pm->u.m[0][0] - pm->u.m[1][1] - pm->u.m[2][2]);
999 pout->x = 0.25f * S;
1000 pout->y = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1001 pout->z = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1002 pout->w = ( pm->u.m[1][2] - pm->u.m[2][1] ) / S;
1003 break;
1004 case 1:
1005 S = 2.0f * sqrt(1.0f + pm->u.m[1][1] - pm->u.m[0][0] - pm->u.m[2][2]);
1006 pout->x = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1007 pout->y = 0.25f * S;
1008 pout->z = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1009 pout->w = ( pm->u.m[2][0] - pm->u.m[0][2] ) / S;
1010 break;
1011 case 2:
1012 S = 2.0f * sqrt(1.0f + pm->u.m[2][2] - pm->u.m[0][0] - pm->u.m[1][1]);
1013 pout->x = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1014 pout->y = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1015 pout->z = 0.25f * S;
1016 pout->w = ( pm->u.m[0][1] - pm->u.m[1][0] ) / S;
1017 break;
1019 return pout;
1022 D3DXQUATERNION* WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
1024 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);
1025 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);
1026 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);
1027 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);
1028 return pout;
1031 D3DXQUATERNION* WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, FLOAT t)
1033 FLOAT dot, epsilon;
1035 epsilon = 1.0f;
1036 dot = D3DXQuaternionDot(pq1, pq2);
1037 if ( dot < 0.0f) epsilon = -1.0f;
1038 pout->x = (1.0f - t) * pq1->x + epsilon * t * pq2->x;
1039 pout->y = (1.0f - t) * pq1->y + epsilon * t * pq2->y;
1040 pout->z = (1.0f - t) * pq1->z + epsilon * t * pq2->z;
1041 pout->w = (1.0f - t) * pq1->w + epsilon * t * pq2->w;
1042 return pout;
1045 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, CONST D3DXQUATERNION *pq4, FLOAT t)
1047 D3DXQUATERNION temp1, temp2;
1049 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1050 return pout;
1053 void WINAPI D3DXQuaternionToAxisAngle(CONST D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1055 FLOAT norm;
1057 *pangle = 0.0f;
1058 norm = D3DXQuaternionLength(pq);
1059 if ( norm )
1061 paxis->x = pq->x / norm;
1062 paxis->y = pq->y / norm;
1063 paxis->z = pq->z / norm;
1064 if ( fabs( pq->w ) <= 1.0f ) *pangle = 2.0f * acos(pq->w);
1066 else
1068 paxis->x = 1.0f;
1069 paxis->y = 0.0f;
1070 paxis->z = 0.0f;
1074 /*_________________D3DXVec2_____________________*/
1076 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1078 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1079 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1080 return pout;
1083 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv0, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT s)
1085 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);
1086 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);
1087 return pout;
1090 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pt1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pt2, FLOAT s)
1092 FLOAT h1, h2, h3, h4;
1094 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1095 h2 = s * s * s - 2.0f * s * s + s;
1096 h3 = -2.0f * s * s * s + 3.0f * s * s;
1097 h4 = s * s * s - s * s;
1099 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1100 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1101 return pout;
1104 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
1106 FLOAT norm;
1108 norm = D3DXVec2Length(pv);
1109 if ( !norm )
1111 pout->x = 0.0f;
1112 pout->y = 0.0f;
1114 else
1116 pout->x = pv->x / norm;
1117 pout->y = pv->y / norm;
1119 return pout;
1122 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1124 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
1125 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
1126 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
1127 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1128 return pout;
1131 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1133 FLOAT norm;
1135 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1136 if ( norm )
1138 CONST D3DXVECTOR2 v = *pv;
1139 pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1140 pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.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 CONST D3DXVECTOR2 v = *pv;
1153 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1154 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1155 return pout;
1158 /*_________________D3DXVec3_____________________*/
1160 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1162 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1163 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1164 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1165 return pout;
1168 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
1170 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);
1171 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);
1172 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);
1173 return pout;
1176 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
1178 FLOAT h1, h2, h3, h4;
1180 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1181 h2 = s * s * s - 2.0f * s * s + s;
1182 h3 = -2.0f * s * s * s + 3.0f * s * s;
1183 h4 = s * s * s - s * s;
1185 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1186 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1187 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1188 return pout;
1191 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
1193 FLOAT norm;
1195 norm = D3DXVec3Length(pv);
1196 if ( !norm )
1198 pout->x = 0.0f;
1199 pout->y = 0.0f;
1200 pout->z = 0.0f;
1202 else
1204 pout->x = pv->x / norm;
1205 pout->y = pv->y / norm;
1206 pout->z = pv->z / norm;
1208 return pout;
1211 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT8 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1213 D3DXMATRIX m1, m2;
1214 D3DXVECTOR3 vec;
1216 D3DXMatrixMultiply(&m1, pworld, pview);
1217 D3DXMatrixMultiply(&m2, &m1, pprojection);
1218 D3DXVec3TransformCoord(&vec, pv, &m2);
1219 pout->x = pviewport->X + ( 1.0f + vec.x ) * pviewport->Width / 2.0f;
1220 pout->y = pviewport->Y + ( 1.0f - vec.y ) * pviewport->Height / 2.0f;
1221 pout->z = pviewport->MinZ + vec.z * ( pviewport->MaxZ - pviewport->MinZ );
1222 return pout;
1225 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1227 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];
1228 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];
1229 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];
1230 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];
1231 return pout;
1234 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1236 FLOAT norm;
1238 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];
1240 if ( norm )
1242 CONST D3DXVECTOR3 v = *pv;
1243 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;
1244 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;
1245 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;
1247 else
1249 pout->x = 0.0f;
1250 pout->y = 0.0f;
1251 pout->z = 0.0f;
1253 return pout;
1256 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1258 CONST D3DXVECTOR3 v = *pv;
1259 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1260 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1261 pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1262 return pout;
1266 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT8 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1268 D3DXMATRIX m1, m2, m3;
1269 D3DXVECTOR3 vec;
1271 D3DXMatrixMultiply(&m1, pworld, pview);
1272 D3DXMatrixMultiply(&m2, &m1, pprojection);
1273 D3DXMatrixInverse(&m3, NULL, &m2);
1274 vec.x = 2.0f * ( pv->x - pviewport->X ) / pviewport->Width - 1.0f;
1275 vec.y = 1.0f - 2.0f * ( pv->y - pviewport->Y ) / pviewport->Height;
1276 vec.z = ( pv->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
1277 D3DXVec3TransformCoord(pout, &vec, &m3);
1278 return pout;
1281 /*_________________D3DXVec4_____________________*/
1283 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
1285 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1286 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1287 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1288 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
1289 return pout;
1292 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv0, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT s)
1294 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);
1295 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);
1296 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);
1297 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);
1298 return pout;
1301 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
1303 D3DXVECTOR4 out;
1304 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);
1305 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));
1306 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);
1307 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));
1308 *pout = out;
1309 return pout;
1312 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pt1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pt2, FLOAT s)
1314 FLOAT h1, h2, h3, h4;
1316 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1317 h2 = s * s * s - 2.0f * s * s + s;
1318 h3 = -2.0f * s * s * s + 3.0f * s * s;
1319 h4 = s * s * s - s * s;
1321 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1322 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1323 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1324 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
1325 return pout;
1328 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
1330 FLOAT norm;
1332 norm = D3DXVec4Length(pv);
1333 if ( !norm )
1335 pout->x = 0.0f;
1336 pout->y = 0.0f;
1337 pout->z = 0.0f;
1338 pout->w = 0.0f;
1340 else
1342 pout->x = pv->x / norm;
1343 pout->y = pv->y / norm;
1344 pout->z = pv->z / norm;
1345 pout->w = pv->w / norm;
1347 return pout;
1350 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
1352 D3DXVECTOR4 out;
1353 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;
1354 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;
1355 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;
1356 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;
1357 *pout = out;
1358 return pout;