2 * Copyright 2007 David Adam
3 * Copyright 2008 Jérôme Gardou
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define NONAMELESSUNION
30 #include "d3dx8_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(d3dx8);
36 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl;
38 /*_________________D3DXColor____________________*/
40 D3DXCOLOR* WINAPI D3DXColorAdjustContrast(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
42 pout->r = 0.5f + s * (pc->r - 0.5f);
43 pout->g = 0.5f + s * (pc->g - 0.5f);
44 pout->b = 0.5f + s * (pc->b - 0.5f);
49 D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
53 grey = pc->r * 0.2125f + pc->g * 0.7154f + pc->b * 0.0721f;
54 pout->r = grey + s * (pc->r - grey);
55 pout->g = grey + s * (pc->g - grey);
56 pout->b = grey + s * (pc->b - grey);
61 /*_________________Misc__________________________*/
63 FLOAT WINAPI D3DXFresnelTerm(FLOAT costheta, FLOAT refractionindex)
65 FLOAT a, d, g, result;
67 g = sqrt(refractionindex * refractionindex + costheta * costheta - 1.0f);
70 result = ( costheta * a - 1.0f ) * ( costheta * a - 1.0f ) / ( ( costheta * d + 1.0f ) * ( costheta * d + 1.0f ) ) + 1.0f;
71 result = result * 0.5f * d * d / ( a * a );
75 /*_________________D3DXMatrix____________________*/
77 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR3 *rotationcenter, CONST D3DXQUATERNION *rotation, CONST D3DXVECTOR3 *translation)
79 D3DXMATRIX m1, m2, m3, m4, m5;
81 D3DXMatrixScaling(&m1, scaling, scaling, scaling);
82 if ( !rotationcenter )
84 D3DXMatrixIdentity(&m2);
85 D3DXMatrixIdentity(&m4);
89 D3DXMatrixTranslation(&m2, -rotationcenter->x, -rotationcenter->y, -rotationcenter->z);
90 D3DXMatrixTranslation(&m4, rotationcenter->x, rotationcenter->y, rotationcenter->z);
94 D3DXMatrixIdentity(&m3);
98 D3DXMatrixRotationQuaternion(&m3, rotation);
102 D3DXMatrixIdentity(&m5);
106 D3DXMatrixTranslation(&m5, translation->x, translation->y, translation->z);
108 D3DXMatrixMultiply(&m1, &m1, &m2);
109 D3DXMatrixMultiply(&m1, &m1, &m3);
110 D3DXMatrixMultiply(&m1, &m1, &m4);
111 D3DXMatrixMultiply(pout, &m1, &m5);
115 FLOAT WINAPI D3DXMatrixfDeterminant(CONST D3DXMATRIX *pm)
117 D3DXVECTOR4 minor, v1, v2, v3;
120 v1.x = pm->u.m[0][0]; v1.y = pm->u.m[1][0]; v1.z = pm->u.m[2][0]; v1.w = pm->u.m[3][0];
121 v2.x = pm->u.m[0][1]; v2.y = pm->u.m[1][1]; v2.z = pm->u.m[2][1]; v2.w = pm->u.m[3][1];
122 v3.x = pm->u.m[0][2]; v3.y = pm->u.m[1][2]; v3.z = pm->u.m[2][2]; v3.w = pm->u.m[3][2];
123 D3DXVec4Cross(&minor, &v1, &v2, &v3);
124 det = - (pm->u.m[0][3] * minor.x + pm->u.m[1][3] * minor.y + pm->u.m[2][3] * minor.z + pm->u.m[3][3] * minor.w);
128 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, CONST D3DXMATRIX *pm)
132 D3DXVECTOR4 v, vec[3];
135 det = D3DXMatrixfDeterminant(pm);
136 if ( !det ) return NULL;
137 if ( pdeterminant ) *pdeterminant = det;
145 if ( j > i ) a = a-1;
146 vec[a].x = pm->u.m[j][0];
147 vec[a].y = pm->u.m[j][1];
148 vec[a].z = pm->u.m[j][2];
149 vec[a].w = pm->u.m[j][3];
152 D3DXVec4Cross(&v, &vec[0], &vec[1], &vec[2]);
153 out.u.m[0][i] = pow(-1.0f, i) * v.x / det;
154 out.u.m[1][i] = pow(-1.0f, i) * v.y / det;
155 out.u.m[2][i] = pow(-1.0f, i) * v.z / det;
156 out.u.m[3][i] = pow(-1.0f, i) * v.w / det;
162 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
164 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
166 D3DXVec3Subtract(&vec2, pat, peye);
167 D3DXVec3Normalize(&vec, &vec2);
168 D3DXVec3Cross(&right, pup, &vec);
169 D3DXVec3Cross(&up, &vec, &right);
170 D3DXVec3Normalize(&rightn, &right);
171 D3DXVec3Normalize(&upn, &up);
172 pout->u.m[0][0] = rightn.x;
173 pout->u.m[1][0] = rightn.y;
174 pout->u.m[2][0] = rightn.z;
175 pout->u.m[3][0] = -D3DXVec3Dot(&rightn,peye);
176 pout->u.m[0][1] = upn.x;
177 pout->u.m[1][1] = upn.y;
178 pout->u.m[2][1] = upn.z;
179 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
180 pout->u.m[0][2] = vec.x;
181 pout->u.m[1][2] = vec.y;
182 pout->u.m[2][2] = vec.z;
183 pout->u.m[3][2] = -D3DXVec3Dot(&vec, peye);
184 pout->u.m[0][3] = 0.0f;
185 pout->u.m[1][3] = 0.0f;
186 pout->u.m[2][3] = 0.0f;
187 pout->u.m[3][3] = 1.0f;
191 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
193 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
195 D3DXVec3Subtract(&vec2, pat, peye);
196 D3DXVec3Normalize(&vec, &vec2);
197 D3DXVec3Cross(&right, pup, &vec);
198 D3DXVec3Cross(&up, &vec, &right);
199 D3DXVec3Normalize(&rightn, &right);
200 D3DXVec3Normalize(&upn, &up);
201 pout->u.m[0][0] = -rightn.x;
202 pout->u.m[1][0] = -rightn.y;
203 pout->u.m[2][0] = -rightn.z;
204 pout->u.m[3][0] = D3DXVec3Dot(&rightn,peye);
205 pout->u.m[0][1] = upn.x;
206 pout->u.m[1][1] = upn.y;
207 pout->u.m[2][1] = upn.z;
208 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
209 pout->u.m[0][2] = -vec.x;
210 pout->u.m[1][2] = -vec.y;
211 pout->u.m[2][2] = -vec.z;
212 pout->u.m[3][2] = D3DXVec3Dot(&vec, peye);
213 pout->u.m[0][3] = 0.0f;
214 pout->u.m[1][3] = 0.0f;
215 pout->u.m[2][3] = 0.0f;
216 pout->u.m[3][3] = 1.0f;
220 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
229 out.u.m[i][j] = pm1->u.m[i][0] * pm2->u.m[0][j] + pm1->u.m[i][1] * pm2->u.m[1][j] + pm1->u.m[i][2] * pm2->u.m[2][j] + pm1->u.m[i][3] * pm2->u.m[3][j];
236 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
238 D3DXMatrixMultiply(pout, pm1, pm2);
239 D3DXMatrixTranspose(pout, pout);
243 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
245 D3DXMatrixIdentity(pout);
246 pout->u.m[0][0] = 2.0f / w;
247 pout->u.m[1][1] = 2.0f / h;
248 pout->u.m[2][2] = 1.0f / (zf - zn);
249 pout->u.m[3][2] = zn / (zn - zf);
253 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
255 D3DXMatrixIdentity(pout);
256 pout->u.m[0][0] = 2.0f / (r - l);
257 pout->u.m[1][1] = 2.0f / (t - b);
258 pout->u.m[2][2] = 1.0f / (zf -zn);
259 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
260 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
261 pout->u.m[3][2] = zn / (zn -zf);
265 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
267 D3DXMatrixIdentity(pout);
268 pout->u.m[0][0] = 2.0f / (r - l);
269 pout->u.m[1][1] = 2.0f / (t - b);
270 pout->u.m[2][2] = 1.0f / (zn -zf);
271 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
272 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
273 pout->u.m[3][2] = zn / (zn -zf);
277 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
279 D3DXMatrixIdentity(pout);
280 pout->u.m[0][0] = 2.0f / w;
281 pout->u.m[1][1] = 2.0f / h;
282 pout->u.m[2][2] = 1.0f / (zn - zf);
283 pout->u.m[3][2] = zn / (zn - zf);
287 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
289 D3DXMatrixIdentity(pout);
290 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
291 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
292 pout->u.m[2][2] = zf / (zf - zn);
293 pout->u.m[2][3] = 1.0f;
294 pout->u.m[3][2] = (zf * zn) / (zn - zf);
295 pout->u.m[3][3] = 0.0f;
299 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
301 D3DXMatrixIdentity(pout);
302 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
303 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
304 pout->u.m[2][2] = zf / (zn - zf);
305 pout->u.m[2][3] = -1.0f;
306 pout->u.m[3][2] = (zf * zn) / (zn - zf);
307 pout->u.m[3][3] = 0.0f;
311 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
313 D3DXMatrixIdentity(pout);
314 pout->u.m[0][0] = 2.0f * zn / w;
315 pout->u.m[1][1] = 2.0f * zn / h;
316 pout->u.m[2][2] = zf / (zf - zn);
317 pout->u.m[3][2] = (zn * zf) / (zn - zf);
318 pout->u.m[2][3] = 1.0f;
319 pout->u.m[3][3] = 0.0f;
323 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
325 D3DXMatrixIdentity(pout);
326 pout->u.m[0][0] = 2.0f * zn / (r - l);
327 pout->u.m[1][1] = -2.0f * zn / (b - t);
328 pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
329 pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
330 pout->u.m[2][2] = - zf / (zn - zf);
331 pout->u.m[3][2] = (zn * zf) / (zn -zf);
332 pout->u.m[2][3] = 1.0f;
333 pout->u.m[3][3] = 0.0f;
337 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
339 D3DXMatrixIdentity(pout);
340 pout->u.m[0][0] = 2.0f * zn / (r - l);
341 pout->u.m[1][1] = -2.0f * zn / (b - t);
342 pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
343 pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
344 pout->u.m[2][2] = zf / (zn - zf);
345 pout->u.m[3][2] = (zn * zf) / (zn -zf);
346 pout->u.m[2][3] = -1.0f;
347 pout->u.m[3][3] = 0.0f;
351 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
353 D3DXMatrixIdentity(pout);
354 pout->u.m[0][0] = 2.0f * zn / w;
355 pout->u.m[1][1] = 2.0f * zn / h;
356 pout->u.m[2][2] = zf / (zn - zf);
357 pout->u.m[3][2] = (zn * zf) / (zn - zf);
358 pout->u.m[2][3] = -1.0f;
359 pout->u.m[3][3] = 0.0f;
363 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, CONST D3DXPLANE *pplane)
367 D3DXPlaneNormalize(&Nplane, pplane);
368 D3DXMatrixIdentity(pout);
369 pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
370 pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
371 pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
372 pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
373 pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
374 pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
375 pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
376 pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
377 pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
378 pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
379 pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
380 pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
384 D3DXMATRIX* WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
388 D3DXVec3Normalize(&v,pv);
389 D3DXMatrixIdentity(pout);
390 pout->u.m[0][0] = (1.0f - cos(angle)) * v.x * v.x + cos(angle);
391 pout->u.m[1][0] = (1.0f - cos(angle)) * v.x * v.y - sin(angle) * v.z;
392 pout->u.m[2][0] = (1.0f - cos(angle)) * v.x * v.z + sin(angle) * v.y;
393 pout->u.m[0][1] = (1.0f - cos(angle)) * v.y * v.x + sin(angle) * v.z;
394 pout->u.m[1][1] = (1.0f - cos(angle)) * v.y * v.y + cos(angle);
395 pout->u.m[2][1] = (1.0f - cos(angle)) * v.y * v.z - sin(angle) * v.x;
396 pout->u.m[0][2] = (1.0f - cos(angle)) * v.z * v.x - sin(angle) * v.y;
397 pout->u.m[1][2] = (1.0f - cos(angle)) * v.z * v.y + sin(angle) * v.x;
398 pout->u.m[2][2] = (1.0f - cos(angle)) * v.z * v.z + cos(angle);
402 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, CONST D3DXQUATERNION *pq)
404 D3DXMatrixIdentity(pout);
405 pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
406 pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
407 pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
408 pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
409 pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
410 pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
411 pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
412 pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
413 pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
417 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
419 D3DXMatrixIdentity(pout);
420 pout->u.m[1][1] = cos(angle);
421 pout->u.m[2][2] = cos(angle);
422 pout->u.m[1][2] = sin(angle);
423 pout->u.m[2][1] = -sin(angle);
427 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
429 D3DXMatrixIdentity(pout);
430 pout->u.m[0][0] = cos(angle);
431 pout->u.m[2][2] = cos(angle);
432 pout->u.m[0][2] = -sin(angle);
433 pout->u.m[2][0] = sin(angle);
437 D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
441 D3DXMatrixIdentity(pout);
442 D3DXMatrixRotationZ(&m, roll);
443 D3DXMatrixMultiply(pout, pout, &m);
444 D3DXMatrixRotationX(&m, pitch);
445 D3DXMatrixMultiply(pout, pout, &m);
446 D3DXMatrixRotationY(&m, yaw);
447 D3DXMatrixMultiply(pout, pout, &m);
450 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
452 D3DXMatrixIdentity(pout);
453 pout->u.m[0][0] = cos(angle);
454 pout->u.m[1][1] = cos(angle);
455 pout->u.m[0][1] = sin(angle);
456 pout->u.m[1][0] = -sin(angle);
460 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
462 D3DXMatrixIdentity(pout);
463 pout->u.m[0][0] = sx;
464 pout->u.m[1][1] = sy;
465 pout->u.m[2][2] = sz;
469 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, CONST D3DXVECTOR4 *plight, CONST D3DXPLANE *pplane)
474 D3DXPlaneNormalize(&Nplane, pplane);
475 dot = D3DXPlaneDot(&Nplane, plight);
476 pout->u.m[0][0] = dot - Nplane.a * plight->x;
477 pout->u.m[0][1] = -Nplane.a * plight->y;
478 pout->u.m[0][2] = -Nplane.a * plight->z;
479 pout->u.m[0][3] = -Nplane.a * plight->w;
480 pout->u.m[1][0] = -Nplane.b * plight->x;
481 pout->u.m[1][1] = dot - Nplane.b * plight->y;
482 pout->u.m[1][2] = -Nplane.b * plight->z;
483 pout->u.m[1][3] = -Nplane.b * plight->w;
484 pout->u.m[2][0] = -Nplane.c * plight->x;
485 pout->u.m[2][1] = -Nplane.c * plight->y;
486 pout->u.m[2][2] = dot - Nplane.c * plight->z;
487 pout->u.m[2][3] = -Nplane.c * plight->w;
488 pout->u.m[3][0] = -Nplane.d * plight->x;
489 pout->u.m[3][1] = -Nplane.d * plight->y;
490 pout->u.m[3][2] = -Nplane.d * plight->z;
491 pout->u.m[3][3] = dot - Nplane.d * plight->w;
495 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pscalingcenter, CONST D3DXQUATERNION *pscalingrotation, CONST D3DXVECTOR3 *pscaling, CONST D3DXVECTOR3 *protationcenter, CONST D3DXQUATERNION *protation, CONST D3DXVECTOR3 *ptranslation)
497 D3DXMATRIX m1, m2, m3, m4, m5, m6, m7;
501 if ( !pscalingcenter )
509 psc.x = pscalingcenter->x;
510 psc.y = pscalingcenter->y;
511 psc.z = pscalingcenter->z;
513 if ( !protationcenter )
521 prc.x = protationcenter->x;
522 prc.y = protationcenter->y;
523 prc.z = protationcenter->z;
533 pt.x = ptranslation->x;
534 pt.y = ptranslation->y;
535 pt.z = ptranslation->z;
537 D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
538 if ( !pscalingrotation )
540 D3DXMatrixIdentity(&m2);
541 D3DXMatrixIdentity(&m4);
545 D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
546 D3DXMatrixInverse(&m2, NULL, &m4);
550 D3DXMatrixIdentity(&m3);
554 D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
558 D3DXMatrixIdentity(&m6);
562 D3DXMatrixRotationQuaternion(&m6, protation);
564 D3DXMatrixTranslation(&m5, psc.x - prc.x, psc.y - prc.y, psc.z - prc.z);
565 D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
566 D3DXMatrixMultiply(&m1, &m1, &m2);
567 D3DXMatrixMultiply(&m1, &m1, &m3);
568 D3DXMatrixMultiply(&m1, &m1, &m4);
569 D3DXMatrixMultiply(&m1, &m1, &m5);
570 D3DXMatrixMultiply(&m1, &m1, &m6);
571 D3DXMatrixMultiply(pout, &m1, &m7);
575 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
577 D3DXMatrixIdentity(pout);
584 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
586 CONST D3DXMATRIX m = *pm;
593 pout->u.m[i][j] = m.u.m[j][i];
599 /*_________________D3DXMatrixStack____________________*/
601 static const unsigned int INITIAL_STACK_SIZE = 32;
603 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, LPD3DXMATRIXSTACK* ppstack)
605 ID3DXMatrixStackImpl* object;
607 TRACE("flags %#x, ppstack %p\n", flags, ppstack);
609 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXMatrixStackImpl));
610 if ( object == NULL )
613 return E_OUTOFMEMORY;
615 object->lpVtbl = &ID3DXMatrixStack_Vtbl;
618 object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(D3DXMATRIX));
621 HeapFree(GetProcessHeap(), 0, object);
623 return E_OUTOFMEMORY;
627 object->stack_size = INITIAL_STACK_SIZE;
628 D3DXMatrixIdentity(&object->stack[0]);
630 TRACE("Created matrix stack %p\n", object);
632 *ppstack = (LPD3DXMATRIXSTACK)object;
636 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **ppobj)
638 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
639 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXMatrixStack))
641 ID3DXMatrixStack_AddRef(iface);
646 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
647 return E_NOINTERFACE;
650 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
652 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
653 ULONG ref = InterlockedIncrement(&This->ref);
654 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
658 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack* iface)
660 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
661 ULONG ref = InterlockedDecrement(&This->ref);
664 HeapFree(GetProcessHeap(), 0, This->stack);
665 HeapFree(GetProcessHeap(), 0, This);
667 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
671 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
673 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
675 TRACE("iface %p\n", iface);
677 return &This->stack[This->current];
680 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
682 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
684 TRACE("iface %p\n", iface);
686 D3DXMatrixIdentity(&This->stack[This->current]);
691 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
693 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
695 TRACE("iface %p\n", iface);
697 if (!pm) return D3DERR_INVALIDCALL;
698 This->stack[This->current] = *pm;
703 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
705 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
707 TRACE("iface %p\n", iface);
709 if (!pm) return D3DERR_INVALIDCALL;
710 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], pm);
715 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
717 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
719 TRACE("iface %p\n", iface);
721 if (!pm) return D3DERR_INVALIDCALL;
722 D3DXMatrixMultiply(&This->stack[This->current], pm, &This->stack[This->current]);
727 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
729 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
731 TRACE("iface %p\n", iface);
733 /* Popping the last element on the stack returns D3D_OK, but does nothing. */
734 if (!This->current) return D3D_OK;
736 if (This->current <= This->stack_size / 4 && This->stack_size >= INITIAL_STACK_SIZE * 2)
738 unsigned int new_size;
739 D3DXMATRIX *new_stack;
741 new_size = This->stack_size / 2;
742 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(D3DXMATRIX));
745 This->stack_size = new_size;
746 This->stack = new_stack;
755 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
757 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
759 TRACE("iface %p\n", iface);
761 if (This->current == This->stack_size - 1)
763 unsigned int new_size;
764 D3DXMATRIX *new_stack;
766 if (This->stack_size > UINT_MAX / 2) return E_OUTOFMEMORY;
768 new_size = This->stack_size * 2;
769 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(D3DXMATRIX));
770 if (!new_stack) return E_OUTOFMEMORY;
772 This->stack_size = new_size;
773 This->stack = new_stack;
777 This->stack[This->current] = This->stack[This->current - 1];
782 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
785 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
787 TRACE("iface %p\n", iface);
789 if (!pv) return D3DERR_INVALIDCALL;
790 D3DXMatrixRotationAxis(&temp, pv, angle);
791 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
796 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
799 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
801 TRACE("iface %p\n", iface);
803 if (!pv) return D3DERR_INVALIDCALL;
804 D3DXMatrixRotationAxis(&temp, pv, angle);
805 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
810 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
813 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
815 TRACE("iface %p\n", iface);
817 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
818 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
823 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
826 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
828 TRACE("iface %p\n", iface);
830 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
831 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
836 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
839 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
841 TRACE("iface %p\n", iface);
843 D3DXMatrixScaling(&temp, x, y, z);
844 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
849 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
852 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
854 TRACE("iface %p\n", iface);
856 D3DXMatrixScaling(&temp, x, y, z);
857 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
862 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
865 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
867 TRACE("iface %p\n", iface);
869 D3DXMatrixTranslation(&temp, x, y, z);
870 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
875 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
878 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
880 TRACE("iface %p\n", iface);
882 D3DXMatrixTranslation(&temp, x, y, z);
883 D3DXMatrixMultiply(&This->stack[This->current], &temp,&This->stack[This->current]);
888 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
890 ID3DXMatrixStackImpl_QueryInterface,
891 ID3DXMatrixStackImpl_AddRef,
892 ID3DXMatrixStackImpl_Release,
893 ID3DXMatrixStackImpl_Pop,
894 ID3DXMatrixStackImpl_Push,
895 ID3DXMatrixStackImpl_LoadIdentity,
896 ID3DXMatrixStackImpl_LoadMatrix,
897 ID3DXMatrixStackImpl_MultMatrix,
898 ID3DXMatrixStackImpl_MultMatrixLocal,
899 ID3DXMatrixStackImpl_RotateAxis,
900 ID3DXMatrixStackImpl_RotateAxisLocal,
901 ID3DXMatrixStackImpl_RotateYawPitchRoll,
902 ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
903 ID3DXMatrixStackImpl_Scale,
904 ID3DXMatrixStackImpl_ScaleLocal,
905 ID3DXMatrixStackImpl_Translate,
906 ID3DXMatrixStackImpl_TranslateLocal,
907 ID3DXMatrixStackImpl_GetTop
910 /*_________________D3DXPLANE________________*/
912 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, CONST D3DXVECTOR3 *pvpoint, CONST D3DXVECTOR3 *pvnormal)
914 pout->a = pvnormal->x;
915 pout->b = pvnormal->y;
916 pout->c = pvnormal->z;
917 pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
921 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3)
923 D3DXVECTOR3 edge1, edge2, normal, Nnormal;
925 edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
926 edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
927 D3DXVec3Subtract(&edge1, pv2, pv1);
928 D3DXVec3Subtract(&edge2, pv3, pv1);
929 D3DXVec3Cross(&normal, &edge1, &edge2);
930 D3DXVec3Normalize(&Nnormal, &normal);
931 D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
935 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, CONST D3DXPLANE *pp, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
937 D3DXVECTOR3 direction, normal;
943 direction.x = pv2->x - pv1->x;
944 direction.y = pv2->y - pv1->y;
945 direction.z = pv2->z - pv1->z;
946 dot = D3DXVec3Dot(&normal, &direction);
947 if ( !dot ) return NULL;
948 temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
949 pout->x = pv1->x - temp * direction.x;
950 pout->y = pv1->y - temp * direction.y;
951 pout->z = pv1->z - temp * direction.z;
955 D3DXPLANE* WINAPI D3DXPlaneNormalize(D3DXPLANE *pout, CONST D3DXPLANE *pp)
960 norm = sqrt(pp->a * pp->a + pp->b * pp->b + pp->c * pp->c);
963 out.a = pp->a / norm;
964 out.b = pp->b / norm;
965 out.c = pp->c / norm;
966 out.d = pp->d / norm;
979 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, CONST D3DXPLANE *pplane, CONST D3DXMATRIX *pm)
981 CONST D3DXPLANE plane = *pplane;
982 pout->a = pm->u.m[0][0] * plane.a + pm->u.m[1][0] * plane.b + pm->u.m[2][0] * plane.c + pm->u.m[3][0] * plane.d;
983 pout->b = pm->u.m[0][1] * plane.a + pm->u.m[1][1] * plane.b + pm->u.m[2][1] * plane.c + pm->u.m[3][1] * plane.d;
984 pout->c = pm->u.m[0][2] * plane.a + pm->u.m[1][2] * plane.b + pm->u.m[2][2] * plane.c + pm->u.m[3][2] * plane.d;
985 pout->d = pm->u.m[0][3] * plane.a + pm->u.m[1][3] * plane.b + pm->u.m[2][3] * plane.c + pm->u.m[3][3] * plane.d;
989 /*_________________D3DXQUATERNION________________*/
991 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
993 D3DXQUATERNION temp1, temp2;
994 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
998 D3DXQUATERNION* WINAPI D3DXQuaternionExp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1002 norm = sqrt(pq->x * pq->x + pq->y * pq->y + pq->z * pq->z);
1005 pout->x = sin(norm) * pq->x / norm;
1006 pout->y = sin(norm) * pq->y / norm;
1007 pout->z = sin(norm) * pq->z / norm;
1008 pout->w = cos(norm);
1020 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1024 norm = D3DXQuaternionLengthSq(pq);
1034 pout->x = -pq->x / norm;
1035 pout->y = -pq->y / norm;
1036 pout->z = -pq->z / norm;
1037 pout->w = pq->w / norm;
1042 D3DXQUATERNION* WINAPI D3DXQuaternionLn(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1044 FLOAT norm, normvec, theta;
1046 norm = D3DXQuaternionLengthSq(pq);
1047 if ( norm > 1.0001f )
1054 else if( norm > 0.99999f)
1056 normvec = sqrt( pq->x * pq->x + pq->y * pq->y + pq->z * pq->z );
1057 theta = atan2(normvec, pq->w) / normvec;
1058 pout->x = theta * pq->x;
1059 pout->y = theta * pq->y;
1060 pout->z = theta * pq->z;
1065 FIXME("The quaternion (%f, %f, %f, %f) has a norm <1. This should not happen. Windows returns a result anyway. This case is not implemented yet.\n", pq->x, pq->y, pq->z, pq->w);
1070 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
1073 out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
1074 out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
1075 out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
1076 out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
1081 D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1086 norm = D3DXQuaternionLength(pq);
1096 out.x = pq->x / norm;
1097 out.y = pq->y / norm;
1098 out.z = pq->z / norm;
1099 out.w = pq->w / norm;
1105 D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
1109 D3DXVec3Normalize(&temp, pv);
1110 pout->x = sin( angle / 2.0f ) * temp.x;
1111 pout->y = sin( angle / 2.0f ) * temp.y;
1112 pout->z = sin( angle / 2.0f ) * temp.z;
1113 pout->w = cos( angle / 2.0f );
1117 D3DXQUATERNION* WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *pout, CONST D3DXMATRIX *pm)
1120 FLOAT maxdiag, S, trace;
1122 trace = pm->u.m[0][0] + pm->u.m[1][1] + pm->u.m[2][2] + 1.0f;
1125 pout->x = ( pm->u.m[1][2] - pm->u.m[2][1] ) / ( 2.0f * sqrt(trace) );
1126 pout->y = ( pm->u.m[2][0] - pm->u.m[0][2] ) / ( 2.0f * sqrt(trace) );
1127 pout->z = ( pm->u.m[0][1] - pm->u.m[1][0] ) / ( 2.0f * sqrt(trace) );
1128 pout->w = sqrt(trace) / 2.0f;
1132 maxdiag = pm->u.m[0][0];
1135 if ( pm->u.m[i][i] > maxdiag )
1138 maxdiag = pm->u.m[i][i];
1144 S = 2.0f * sqrt(1.0f + pm->u.m[0][0] - pm->u.m[1][1] - pm->u.m[2][2]);
1145 pout->x = 0.25f * S;
1146 pout->y = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1147 pout->z = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1148 pout->w = ( pm->u.m[1][2] - pm->u.m[2][1] ) / S;
1151 S = 2.0f * sqrt(1.0f + pm->u.m[1][1] - pm->u.m[0][0] - pm->u.m[2][2]);
1152 pout->x = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1153 pout->y = 0.25f * S;
1154 pout->z = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1155 pout->w = ( pm->u.m[2][0] - pm->u.m[0][2] ) / S;
1158 S = 2.0f * sqrt(1.0f + pm->u.m[2][2] - pm->u.m[0][0] - pm->u.m[1][1]);
1159 pout->x = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1160 pout->y = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1161 pout->z = 0.25f * S;
1162 pout->w = ( pm->u.m[0][1] - pm->u.m[1][0] ) / S;
1168 D3DXQUATERNION* WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
1170 pout->x = sin( yaw / 2.0f) * cos(pitch / 2.0f) * sin(roll / 2.0f) + cos(yaw / 2.0f) * sin(pitch / 2.0f) * cos(roll / 2.0f);
1171 pout->y = sin( yaw / 2.0f) * cos(pitch / 2.0f) * cos(roll / 2.0f) - cos(yaw / 2.0f) * sin(pitch / 2.0f) * sin(roll / 2.0f);
1172 pout->z = cos(yaw / 2.0f) * cos(pitch / 2.0f) * sin(roll / 2.0f) - sin( yaw / 2.0f) * sin(pitch / 2.0f) * cos(roll / 2.0f);
1173 pout->w = cos( yaw / 2.0f) * cos(pitch / 2.0f) * cos(roll / 2.0f) + sin(yaw / 2.0f) * sin(pitch / 2.0f) * sin(roll / 2.0f);
1177 D3DXQUATERNION* WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, FLOAT t)
1182 dot = D3DXQuaternionDot(pq1, pq2);
1183 if ( dot < 0.0f) epsilon = -1.0f;
1184 pout->x = (1.0f - t) * pq1->x + epsilon * t * pq2->x;
1185 pout->y = (1.0f - t) * pq1->y + epsilon * t * pq2->y;
1186 pout->z = (1.0f - t) * pq1->z + epsilon * t * pq2->z;
1187 pout->w = (1.0f - t) * pq1->w + epsilon * t * pq2->w;
1191 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, CONST D3DXQUATERNION *pq4, FLOAT t)
1193 D3DXQUATERNION temp1, temp2;
1195 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1199 void WINAPI D3DXQuaternionToAxisAngle(CONST D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1204 norm = D3DXQuaternionLength(pq);
1207 paxis->x = pq->x / norm;
1208 paxis->y = pq->y / norm;
1209 paxis->z = pq->z / norm;
1210 if ( fabs( pq->w ) <= 1.0f ) *pangle = 2.0f * acos(pq->w);
1220 /*_________________D3DXVec2_____________________*/
1222 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1224 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1225 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1229 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv0, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT s)
1231 pout->x = 0.5f * (2.0f * pv1->x + (pv2->x - pv0->x) *s + (2.0f *pv0->x - 5.0f * pv1->x + 4.0f * pv2->x - pv3->x) * s * s + (pv3->x -3.0f * pv2->x + 3.0f * pv1->x - pv0->x) * s * s * s);
1232 pout->y = 0.5f * (2.0f * pv1->y + (pv2->y - pv0->y) *s + (2.0f *pv0->y - 5.0f * pv1->y + 4.0f * pv2->y - pv3->y) * s * s + (pv3->y -3.0f * pv2->y + 3.0f * pv1->y - pv0->y) * s * s * s);
1236 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pt1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pt2, FLOAT s)
1238 FLOAT h1, h2, h3, h4;
1240 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1241 h2 = s * s * s - 2.0f * s * s + s;
1242 h3 = -2.0f * s * s * s + 3.0f * s * s;
1243 h4 = s * s * s - s * s;
1245 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1246 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1250 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
1255 norm = D3DXVec2Length(pv);
1263 out.x = pv->x / norm;
1264 out.y = pv->y / norm;
1270 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1272 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
1273 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
1274 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
1275 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1279 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1283 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1286 CONST D3DXVECTOR2 v = *pv;
1287 pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1288 pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1298 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1300 CONST D3DXVECTOR2 v = *pv;
1301 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1302 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1306 /*_________________D3DXVec3_____________________*/
1308 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1310 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1311 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1312 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1316 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
1318 pout->x = 0.5f * (2.0f * pv1->x + (pv2->x - pv0->x) *s + (2.0f *pv0->x - 5.0f * pv1->x + 4.0f * pv2->x - pv3->x) * s * s + (pv3->x -3.0f * pv2->x + 3.0f * pv1->x - pv0->x) * s * s * s);
1319 pout->y = 0.5f * (2.0f * pv1->y + (pv2->y - pv0->y) *s + (2.0f *pv0->y - 5.0f * pv1->y + 4.0f * pv2->y - pv3->y) * s * s + (pv3->y -3.0f * pv2->y + 3.0f * pv1->y - pv0->y) * s * s * s);
1320 pout->z = 0.5f * (2.0f * pv1->z + (pv2->z - pv0->z) *s + (2.0f *pv0->z - 5.0f * pv1->z + 4.0f * pv2->z - pv3->z) * s * s + (pv3->z -3.0f * pv2->z + 3.0f * pv1->z - pv0->z) * s * s * s);
1324 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
1326 FLOAT h1, h2, h3, h4;
1328 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1329 h2 = s * s * s - 2.0f * s * s + s;
1330 h3 = -2.0f * s * s * s + 3.0f * s * s;
1331 h4 = s * s * s - s * s;
1333 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1334 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1335 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1339 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
1344 norm = D3DXVec3Length(pv);
1353 out.x = pv->x / norm;
1354 out.y = pv->y / norm;
1355 out.z = pv->z / norm;
1361 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT8 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1366 D3DXMatrixMultiply(&m, pworld, pview);
1367 D3DXMatrixMultiply(&m, &m, pprojection);
1368 D3DXVec3TransformCoord(&out, pv, &m);
1369 out.x = pviewport->X + ( 1.0f + out.x ) * pviewport->Width / 2.0f;
1370 out.y = pviewport->Y + ( 1.0f - out.y ) * pviewport->Height / 2.0f;
1371 out.z = pviewport->MinZ + out.z * ( pviewport->MaxZ - pviewport->MinZ );
1376 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1378 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z + pm->u.m[3][0];
1379 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z + pm->u.m[3][1];
1380 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z + pm->u.m[3][2];
1381 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[2][3] * pv->z + pm->u.m[3][3];
1385 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1390 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[2][3] *pv->z + pm->u.m[3][3];
1394 CONST D3DXVECTOR3 v = *pv;
1395 out.x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z + pm->u.m[3][0]) / norm;
1396 out.y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z + pm->u.m[3][1]) / norm;
1397 out.z = (pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z + pm->u.m[3][2]) / norm;
1409 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1411 CONST D3DXVECTOR3 v = *pv;
1412 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1413 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1414 pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1419 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT8 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1424 D3DXMatrixMultiply(&m, pworld, pview);
1425 D3DXMatrixMultiply(&m, &m, pprojection);
1426 D3DXMatrixInverse(&m, NULL, &m);
1427 out.x = 2.0f * ( pv->x - pviewport->X ) / pviewport->Width - 1.0f;
1428 out.y = 1.0f - 2.0f * ( pv->y - pviewport->Y ) / pviewport->Height;
1429 out.z = ( pv->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
1430 D3DXVec3TransformCoord(&out, &out, &m);
1435 /*_________________D3DXVec4_____________________*/
1437 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
1439 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1440 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1441 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1442 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
1446 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv0, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT s)
1448 pout->x = 0.5f * (2.0f * pv1->x + (pv2->x - pv0->x) *s + (2.0f *pv0->x - 5.0f * pv1->x + 4.0f * pv2->x - pv3->x) * s * s + (pv3->x -3.0f * pv2->x + 3.0f * pv1->x - pv0->x) * s * s * s);
1449 pout->y = 0.5f * (2.0f * pv1->y + (pv2->y - pv0->y) *s + (2.0f *pv0->y - 5.0f * pv1->y + 4.0f * pv2->y - pv3->y) * s * s + (pv3->y -3.0f * pv2->y + 3.0f * pv1->y - pv0->y) * s * s * s);
1450 pout->z = 0.5f * (2.0f * pv1->z + (pv2->z - pv0->z) *s + (2.0f *pv0->z - 5.0f * pv1->z + 4.0f * pv2->z - pv3->z) * s * s + (pv3->z -3.0f * pv2->z + 3.0f * pv1->z - pv0->z) * s * s * s);
1451 pout->w = 0.5f * (2.0f * pv1->w + (pv2->w - pv0->w) *s + (2.0f *pv0->w - 5.0f * pv1->w + 4.0f * pv2->w - pv3->w) * s * s + (pv3->w -3.0f * pv2->w + 3.0f * pv1->w - pv0->w) * s * s * s);
1455 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
1458 out.x = pv1->y * (pv2->z * pv3->w - pv3->z * pv2->w) - pv1->z * (pv2->y * pv3->w - pv3->y * pv2->w) + pv1->w * (pv2->y * pv3->z - pv2->z *pv3->y);
1459 out.y = -(pv1->x * (pv2->z * pv3->w - pv3->z * pv2->w) - pv1->z * (pv2->x * pv3->w - pv3->x * pv2->w) + pv1->w * (pv2->x * pv3->z - pv3->x * pv2->z));
1460 out.z = pv1->x * (pv2->y * pv3->w - pv3->y * pv2->w) - pv1->y * (pv2->x *pv3->w - pv3->x * pv2->w) + pv1->w * (pv2->x * pv3->y - pv3->x * pv2->y);
1461 out.w = -(pv1->x * (pv2->y * pv3->z - pv3->y * pv2->z) - pv1->y * (pv2->x * pv3->z - pv3->x *pv2->z) + pv1->z * (pv2->x * pv3->y - pv3->x * pv2->y));
1466 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pt1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pt2, FLOAT s)
1468 FLOAT h1, h2, h3, h4;
1470 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1471 h2 = s * s * s - 2.0f * s * s + s;
1472 h3 = -2.0f * s * s * s + 3.0f * s * s;
1473 h4 = s * s * s - s * s;
1475 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1476 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1477 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1478 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
1482 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
1487 norm = D3DXVec4Length(pv);
1497 out.x = pv->x / norm;
1498 out.y = pv->y / norm;
1499 out.z = pv->z / norm;
1500 out.w = pv->w / norm;
1506 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
1509 out.x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z + pm->u.m[3][0] * pv->w;
1510 out.y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z + pm->u.m[3][1] * pv->w;
1511 out.z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z + pm->u.m[3][2] * pv->w;
1512 out.w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[2][3] * pv->z + pm->u.m[3][3] * pv->w;