push d1f5df181c120dbe494f7c89b454752c2e0dcc04
[wine/hacks.git] / dlls / d3dx8 / math.c
blobb355c9cc309f02ef33ae9e9778f6bf39739b0b9e
1 /*
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
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <assert.h>
25 #define NONAMELESSUNION
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
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);
45 pout->a = pc->a;
46 return pout;
49 D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
51 FLOAT grey;
53 grey = pc->r * 0.2125f + pc->g * 0.7154f + pc->b * 0.0721f;
54 pout->r = grey + s * (pc->r - grey);
55 pout->g = grey + s * (pc->g - grey);
56 pout->b = grey + s * (pc->b - grey);
57 pout->a = pc->a;
58 return pout;
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);
68 a = g + costheta;
69 d = g - costheta;
70 result = ( costheta * a - 1.0f ) * ( costheta * a - 1.0f ) / ( ( costheta * d + 1.0f ) * ( costheta * d + 1.0f ) ) + 1.0f;
71 result = result * 0.5f * d * d / ( a * a );
72 return result;
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);
87 else
89 D3DXMatrixTranslation(&m2, -rotationcenter->x, -rotationcenter->y, -rotationcenter->z);
90 D3DXMatrixTranslation(&m4, rotationcenter->x, rotationcenter->y, rotationcenter->z);
92 if ( !rotation )
94 D3DXMatrixIdentity(&m3);
96 else
98 D3DXMatrixRotationQuaternion(&m3, rotation);
100 if ( !translation )
102 D3DXMatrixIdentity(&m5);
104 else
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);
112 return pout;
115 FLOAT WINAPI D3DXMatrixfDeterminant(CONST D3DXMATRIX *pm)
117 D3DXVECTOR4 minor, v1, v2, v3;
118 FLOAT det;
120 v1.x = pm->u.m[0][0]; v1.y = pm->u.m[1][0]; v1.z = pm->u.m[2][0]; v1.w = pm->u.m[3][0];
121 v2.x = pm->u.m[0][1]; v2.y = pm->u.m[1][1]; v2.z = pm->u.m[2][1]; v2.w = pm->u.m[3][1];
122 v3.x = pm->u.m[0][2]; v3.y = pm->u.m[1][2]; v3.z = pm->u.m[2][2]; v3.w = pm->u.m[3][2];
123 D3DXVec4Cross(&minor, &v1, &v2, &v3);
124 det = - (pm->u.m[0][3] * minor.x + pm->u.m[1][3] * minor.y + pm->u.m[2][3] * minor.z + pm->u.m[3][3] * minor.w);
125 return det;
128 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, CONST D3DXMATRIX *pm)
130 int a, i, j;
131 D3DXMATRIX out;
132 D3DXVECTOR4 v, vec[3];
133 FLOAT det;
135 det = D3DXMatrixfDeterminant(pm);
136 if ( !det ) return NULL;
137 if ( pdeterminant ) *pdeterminant = det;
138 for (i=0; i<4; i++)
140 for (j=0; j<4; j++)
142 if (j != i )
144 a = j;
145 if ( j > i ) a = a-1;
146 vec[a].x = pm->u.m[j][0];
147 vec[a].y = pm->u.m[j][1];
148 vec[a].z = pm->u.m[j][2];
149 vec[a].w = pm->u.m[j][3];
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;
158 *pout = out;
159 return pout;
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;
188 return pout;
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;
217 return pout;
220 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
222 D3DXMATRIX out;
223 int i,j;
225 for (i=0; i<4; i++)
227 for (j=0; j<4; j++)
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];
232 *pout = out;
233 return pout;
236 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
238 D3DXMatrixMultiply(pout, pm1, pm2);
239 D3DXMatrixTranspose(pout, pout);
240 return 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);
250 return pout;
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);
262 return pout;
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);
274 return pout;
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);
284 return pout;
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;
296 return pout;
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;
308 return pout;
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;
320 return pout;
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;
334 return pout;
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;
348 return pout;
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;
360 return pout;
363 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, CONST D3DXPLANE *pplane)
365 D3DXPLANE Nplane;
367 D3DXPlaneNormalize(&Nplane, pplane);
368 D3DXMatrixIdentity(pout);
369 pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
370 pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
371 pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
372 pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
373 pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
374 pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
375 pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
376 pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
377 pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
378 pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
379 pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
380 pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
381 return pout;
384 D3DXMATRIX* WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
386 D3DXVECTOR3 v;
388 D3DXVec3Normalize(&v,pv);
389 D3DXMatrixIdentity(pout);
390 pout->u.m[0][0] = (1.0f - cos(angle)) * v.x * v.x + cos(angle);
391 pout->u.m[1][0] = (1.0f - cos(angle)) * v.x * v.y - sin(angle) * v.z;
392 pout->u.m[2][0] = (1.0f - cos(angle)) * v.x * v.z + sin(angle) * v.y;
393 pout->u.m[0][1] = (1.0f - cos(angle)) * v.y * v.x + sin(angle) * v.z;
394 pout->u.m[1][1] = (1.0f - cos(angle)) * v.y * v.y + cos(angle);
395 pout->u.m[2][1] = (1.0f - cos(angle)) * v.y * v.z - sin(angle) * v.x;
396 pout->u.m[0][2] = (1.0f - cos(angle)) * v.z * v.x - sin(angle) * v.y;
397 pout->u.m[1][2] = (1.0f - cos(angle)) * v.z * v.y + sin(angle) * v.x;
398 pout->u.m[2][2] = (1.0f - cos(angle)) * v.z * v.z + cos(angle);
399 return pout;
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);
414 return pout;
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);
424 return pout;
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);
434 return pout;
437 D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
439 D3DXMATRIX m;
441 D3DXMatrixIdentity(pout);
442 D3DXMatrixRotationZ(&m, roll);
443 D3DXMatrixMultiply(pout, pout, &m);
444 D3DXMatrixRotationX(&m, pitch);
445 D3DXMatrixMultiply(pout, pout, &m);
446 D3DXMatrixRotationY(&m, yaw);
447 D3DXMatrixMultiply(pout, pout, &m);
448 return pout;
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);
457 return pout;
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;
466 return pout;
469 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, CONST D3DXVECTOR4 *plight, CONST D3DXPLANE *pplane)
471 D3DXPLANE Nplane;
472 FLOAT dot;
474 D3DXPlaneNormalize(&Nplane, pplane);
475 dot = D3DXPlaneDot(&Nplane, plight);
476 pout->u.m[0][0] = dot - Nplane.a * plight->x;
477 pout->u.m[0][1] = -Nplane.a * plight->y;
478 pout->u.m[0][2] = -Nplane.a * plight->z;
479 pout->u.m[0][3] = -Nplane.a * plight->w;
480 pout->u.m[1][0] = -Nplane.b * plight->x;
481 pout->u.m[1][1] = dot - Nplane.b * plight->y;
482 pout->u.m[1][2] = -Nplane.b * plight->z;
483 pout->u.m[1][3] = -Nplane.b * plight->w;
484 pout->u.m[2][0] = -Nplane.c * plight->x;
485 pout->u.m[2][1] = -Nplane.c * plight->y;
486 pout->u.m[2][2] = dot - Nplane.c * plight->z;
487 pout->u.m[2][3] = -Nplane.c * plight->w;
488 pout->u.m[3][0] = -Nplane.d * plight->x;
489 pout->u.m[3][1] = -Nplane.d * plight->y;
490 pout->u.m[3][2] = -Nplane.d * plight->z;
491 pout->u.m[3][3] = dot - Nplane.d * plight->w;
492 return pout;
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;
498 D3DXQUATERNION prc;
499 D3DXVECTOR3 psc, pt;
501 if ( !pscalingcenter )
503 psc.x = 0.0f;
504 psc.y = 0.0f;
505 psc.z = 0.0f;
507 else
509 psc.x = pscalingcenter->x;
510 psc.y = pscalingcenter->y;
511 psc.z = pscalingcenter->z;
513 if ( !protationcenter )
515 prc.x = 0.0f;
516 prc.y = 0.0f;
517 prc.z = 0.0f;
519 else
521 prc.x = protationcenter->x;
522 prc.y = protationcenter->y;
523 prc.z = protationcenter->z;
525 if ( !ptranslation )
527 pt.x = 0.0f;
528 pt.y = 0.0f;
529 pt.z = 0.0f;
531 else
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);
543 else
545 D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
546 D3DXMatrixInverse(&m2, NULL, &m4);
548 if ( !pscaling )
550 D3DXMatrixIdentity(&m3);
552 else
554 D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
556 if ( !protation )
558 D3DXMatrixIdentity(&m6);
560 else
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);
572 return pout;
575 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
577 D3DXMatrixIdentity(pout);
578 pout->u.m[3][0] = x;
579 pout->u.m[3][1] = y;
580 pout->u.m[3][2] = z;
581 return pout;
584 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
586 CONST D3DXMATRIX m = *pm;
587 int i,j;
589 for (i=0; i<4; i++)
591 for (j=0; j<4; j++)
593 pout->u.m[i][j] = m.u.m[j][i];
596 return pout;
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 )
612 *ppstack = NULL;
613 return E_OUTOFMEMORY;
615 object->lpVtbl = &ID3DXMatrixStack_Vtbl;
616 object->ref = 1;
618 object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(D3DXMATRIX));
619 if (!object->stack)
621 HeapFree(GetProcessHeap(), 0, object);
622 *ppstack = NULL;
623 return E_OUTOFMEMORY;
626 object->current = 0;
627 object->stack_size = INITIAL_STACK_SIZE;
628 D3DXMatrixIdentity(&object->stack[0]);
630 TRACE("Created matrix stack %p\n", object);
632 *ppstack = (LPD3DXMATRIXSTACK)object;
633 return D3D_OK;
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);
642 *ppobj = This;
643 return S_OK;
645 *ppobj = NULL;
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);
655 return ref;
658 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack* iface)
660 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
661 ULONG ref = InterlockedDecrement(&This->ref);
662 if (!ref)
664 HeapFree(GetProcessHeap(), 0, This->stack);
665 HeapFree(GetProcessHeap(), 0, This);
667 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
668 return 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]);
688 return D3D_OK;
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;
700 return D3D_OK;
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);
712 return D3D_OK;
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]);
724 return D3D_OK;
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));
743 if (new_stack)
745 This->stack_size = new_size;
746 This->stack = new_stack;
750 --This->current;
752 return D3D_OK;
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;
776 ++This->current;
777 This->stack[This->current] = This->stack[This->current - 1];
779 return D3D_OK;
782 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
784 D3DXMATRIX temp;
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);
793 return D3D_OK;
796 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
798 D3DXMATRIX temp;
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]);
807 return D3D_OK;
810 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
812 D3DXMATRIX temp;
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);
820 return D3D_OK;
823 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
825 D3DXMATRIX temp;
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]);
833 return D3D_OK;
836 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
838 D3DXMATRIX temp;
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);
846 return D3D_OK;
849 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
851 D3DXMATRIX temp;
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]);
859 return D3D_OK;
862 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
864 D3DXMATRIX temp;
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);
872 return D3D_OK;
875 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
877 D3DXMATRIX temp;
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]);
885 return D3D_OK;
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);
918 return pout;
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);
932 return pout;
935 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, CONST D3DXPLANE *pp, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
937 D3DXVECTOR3 direction, normal;
938 FLOAT dot, temp;
940 normal.x = pp->a;
941 normal.y = pp->b;
942 normal.z = pp->c;
943 direction.x = pv2->x - pv1->x;
944 direction.y = pv2->y - pv1->y;
945 direction.z = pv2->z - pv1->z;
946 dot = D3DXVec3Dot(&normal, &direction);
947 if ( !dot ) return NULL;
948 temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
949 pout->x = pv1->x - temp * direction.x;
950 pout->y = pv1->y - temp * direction.y;
951 pout->z = pv1->z - temp * direction.z;
952 return pout;
955 D3DXPLANE* WINAPI D3DXPlaneNormalize(D3DXPLANE *pout, CONST D3DXPLANE *pp)
957 D3DXPLANE out;
958 FLOAT norm;
960 norm = sqrt(pp->a * pp->a + pp->b * pp->b + pp->c * pp->c);
961 if ( norm )
963 out.a = pp->a / norm;
964 out.b = pp->b / norm;
965 out.c = pp->c / norm;
966 out.d = pp->d / norm;
968 else
970 out.a = 0.0f;
971 out.b = 0.0f;
972 out.c = 0.0f;
973 out.d = 0.0f;
975 *pout = out;
976 return pout;
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;
986 return pout;
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));
995 return pout;
998 D3DXQUATERNION* WINAPI D3DXQuaternionExp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1000 FLOAT norm;
1002 norm = sqrt(pq->x * pq->x + pq->y * pq->y + pq->z * pq->z);
1003 if (norm )
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);
1010 else
1012 pout->x = 0.0f;
1013 pout->y = 0.0f;
1014 pout->z = 0.0f;
1015 pout->w = 1.0f;
1017 return pout;
1020 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1022 FLOAT norm;
1024 norm = D3DXQuaternionLengthSq(pq);
1025 if ( !norm )
1027 pout->x = 0.0f;
1028 pout->y = 0.0f;
1029 pout->z = 0.0f;
1030 pout->w = 0.0f;
1032 else
1034 pout->x = -pq->x / norm;
1035 pout->y = -pq->y / norm;
1036 pout->z = -pq->z / norm;
1037 pout->w = pq->w / norm;
1039 return pout;
1042 D3DXQUATERNION* WINAPI D3DXQuaternionLn(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1044 FLOAT norm, normvec, theta;
1046 norm = D3DXQuaternionLengthSq(pq);
1047 if ( norm > 1.0001f )
1049 pout->x = pq->x;
1050 pout->y = pq->y;
1051 pout->z = pq->z;
1052 pout->w = 0.0f;
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;
1061 pout->w = 0.0f;
1063 else
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);
1067 return pout;
1070 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
1072 D3DXQUATERNION out;
1073 out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
1074 out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
1075 out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
1076 out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
1077 *pout = out;
1078 return pout;
1081 D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1083 D3DXQUATERNION out;
1084 FLOAT norm;
1086 norm = D3DXQuaternionLength(pq);
1087 if ( !norm )
1089 out.x = 0.0f;
1090 out.y = 0.0f;
1091 out.z = 0.0f;
1092 out.w = 0.0f;
1094 else
1096 out.x = pq->x / norm;
1097 out.y = pq->y / norm;
1098 out.z = pq->z / norm;
1099 out.w = pq->w / norm;
1101 *pout=out;
1102 return pout;
1105 D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
1107 D3DXVECTOR3 temp;
1109 D3DXVec3Normalize(&temp, pv);
1110 pout->x = sin( angle / 2.0f ) * temp.x;
1111 pout->y = sin( angle / 2.0f ) * temp.y;
1112 pout->z = sin( angle / 2.0f ) * temp.z;
1113 pout->w = cos( angle / 2.0f );
1114 return pout;
1117 D3DXQUATERNION* WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *pout, CONST D3DXMATRIX *pm)
1119 int i, maxi;
1120 FLOAT maxdiag, S, trace;
1122 trace = pm->u.m[0][0] + pm->u.m[1][1] + pm->u.m[2][2] + 1.0f;
1123 if ( trace > 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;
1129 return pout;
1131 maxi = 0;
1132 maxdiag = pm->u.m[0][0];
1133 for (i=1; i<3; i++)
1135 if ( pm->u.m[i][i] > maxdiag )
1137 maxi = i;
1138 maxdiag = pm->u.m[i][i];
1141 switch( maxi )
1143 case 0:
1144 S = 2.0f * sqrt(1.0f + pm->u.m[0][0] - pm->u.m[1][1] - pm->u.m[2][2]);
1145 pout->x = 0.25f * S;
1146 pout->y = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1147 pout->z = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1148 pout->w = ( pm->u.m[1][2] - pm->u.m[2][1] ) / S;
1149 break;
1150 case 1:
1151 S = 2.0f * sqrt(1.0f + pm->u.m[1][1] - pm->u.m[0][0] - pm->u.m[2][2]);
1152 pout->x = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1153 pout->y = 0.25f * S;
1154 pout->z = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1155 pout->w = ( pm->u.m[2][0] - pm->u.m[0][2] ) / S;
1156 break;
1157 case 2:
1158 S = 2.0f * sqrt(1.0f + pm->u.m[2][2] - pm->u.m[0][0] - pm->u.m[1][1]);
1159 pout->x = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1160 pout->y = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1161 pout->z = 0.25f * S;
1162 pout->w = ( pm->u.m[0][1] - pm->u.m[1][0] ) / S;
1163 break;
1165 return pout;
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);
1174 return pout;
1177 D3DXQUATERNION* WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, FLOAT t)
1179 FLOAT dot, epsilon;
1181 epsilon = 1.0f;
1182 dot = D3DXQuaternionDot(pq1, pq2);
1183 if ( dot < 0.0f) epsilon = -1.0f;
1184 pout->x = (1.0f - t) * pq1->x + epsilon * t * pq2->x;
1185 pout->y = (1.0f - t) * pq1->y + epsilon * t * pq2->y;
1186 pout->z = (1.0f - t) * pq1->z + epsilon * t * pq2->z;
1187 pout->w = (1.0f - t) * pq1->w + epsilon * t * pq2->w;
1188 return pout;
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));
1196 return pout;
1199 void WINAPI D3DXQuaternionToAxisAngle(CONST D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1201 FLOAT norm;
1203 *pangle = 0.0f;
1204 norm = D3DXQuaternionLength(pq);
1205 if ( norm )
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);
1212 else
1214 paxis->x = 1.0f;
1215 paxis->y = 0.0f;
1216 paxis->z = 0.0f;
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);
1226 return pout;
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);
1233 return pout;
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);
1247 return pout;
1250 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
1252 D3DXVECTOR2 out;
1253 FLOAT norm;
1255 norm = D3DXVec2Length(pv);
1256 if ( !norm )
1258 out.x = 0.0f;
1259 out.y = 0.0f;
1261 else
1263 out.x = pv->x / norm;
1264 out.y = pv->y / norm;
1266 *pout=out;
1267 return pout;
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];
1276 return pout;
1279 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1281 FLOAT norm;
1283 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1284 if ( norm )
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;
1290 else
1292 pout->x = 0.0f;
1293 pout->y = 0.0f;
1295 return pout;
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;
1303 return pout;
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);
1313 return pout;
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);
1321 return pout;
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);
1336 return pout;
1339 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
1341 D3DXVECTOR3 out;
1342 FLOAT norm;
1344 norm = D3DXVec3Length(pv);
1345 if ( !norm )
1347 out.x = 0.0f;
1348 out.y = 0.0f;
1349 out.z = 0.0f;
1351 else
1353 out.x = pv->x / norm;
1354 out.y = pv->y / norm;
1355 out.z = pv->z / norm;
1357 *pout = out;
1358 return pout;
1361 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT8 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1363 D3DXMATRIX m;
1364 D3DXVECTOR3 out;
1366 D3DXMatrixMultiply(&m, pworld, pview);
1367 D3DXMatrixMultiply(&m, &m, pprojection);
1368 D3DXVec3TransformCoord(&out, pv, &m);
1369 out.x = pviewport->X + ( 1.0f + out.x ) * pviewport->Width / 2.0f;
1370 out.y = pviewport->Y + ( 1.0f - out.y ) * pviewport->Height / 2.0f;
1371 out.z = pviewport->MinZ + out.z * ( pviewport->MaxZ - pviewport->MinZ );
1372 *pout = out;
1373 return pout;
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];
1382 return pout;
1385 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1387 D3DXVECTOR3 out;
1388 FLOAT norm;
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];
1392 if ( norm )
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;
1399 else
1401 out.x = 0.0f;
1402 out.y = 0.0f;
1403 out.z = 0.0f;
1405 *pout = out;
1406 return pout;
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;
1415 return pout;
1419 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT8 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1421 D3DXMATRIX m;
1422 D3DXVECTOR3 out;
1424 D3DXMatrixMultiply(&m, pworld, pview);
1425 D3DXMatrixMultiply(&m, &m, pprojection);
1426 D3DXMatrixInverse(&m, NULL, &m);
1427 out.x = 2.0f * ( pv->x - pviewport->X ) / pviewport->Width - 1.0f;
1428 out.y = 1.0f - 2.0f * ( pv->y - pviewport->Y ) / pviewport->Height;
1429 out.z = ( pv->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
1430 D3DXVec3TransformCoord(&out, &out, &m);
1431 *pout = out;
1432 return pout;
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);
1443 return pout;
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);
1452 return pout;
1455 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
1457 D3DXVECTOR4 out;
1458 out.x = pv1->y * (pv2->z * pv3->w - pv3->z * pv2->w) - pv1->z * (pv2->y * pv3->w - pv3->y * pv2->w) + pv1->w * (pv2->y * pv3->z - pv2->z *pv3->y);
1459 out.y = -(pv1->x * (pv2->z * pv3->w - pv3->z * pv2->w) - pv1->z * (pv2->x * pv3->w - pv3->x * pv2->w) + pv1->w * (pv2->x * pv3->z - pv3->x * pv2->z));
1460 out.z = pv1->x * (pv2->y * pv3->w - pv3->y * pv2->w) - pv1->y * (pv2->x *pv3->w - pv3->x * pv2->w) + pv1->w * (pv2->x * pv3->y - pv3->x * pv2->y);
1461 out.w = -(pv1->x * (pv2->y * pv3->z - pv3->y * pv2->z) - pv1->y * (pv2->x * pv3->z - pv3->x *pv2->z) + pv1->z * (pv2->x * pv3->y - pv3->x * pv2->y));
1462 *pout = out;
1463 return pout;
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);
1479 return pout;
1482 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
1484 D3DXVECTOR4 out;
1485 FLOAT norm;
1487 norm = D3DXVec4Length(pv);
1488 if ( !norm )
1490 out.x = 0.0f;
1491 out.y = 0.0f;
1492 out.z = 0.0f;
1493 out.w = 0.0f;
1495 else
1497 out.x = pv->x / norm;
1498 out.y = pv->y / norm;
1499 out.z = pv->z / norm;
1500 out.w = pv->w / norm;
1502 *pout = out;
1503 return pout;
1506 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
1508 D3DXVECTOR4 out;
1509 out.x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z + pm->u.m[3][0] * pv->w;
1510 out.y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z + pm->u.m[3][1] * pv->w;
1511 out.z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z + pm->u.m[3][2] * pv->w;
1512 out.w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[2][3] * pv->z + pm->u.m[3][3] * pv->w;
1513 *pout = out;
1514 return pout;