d3dx8: Implement D3DXPlaneNormalize.
[wine/winequartzdrv.git] / dlls / d3dx8 / math.c
blob9a9def9e9e5f2a2a4bce62e70b0a1c23c18c3e63
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.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3dx8);
35 /*_________________D3DXMatrix____________________*/
37 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *pout, float scaling, D3DXVECTOR3 *rotationcenter, D3DXQUATERNION *rotation, D3DXVECTOR3 *translation)
39 D3DXMATRIX m1, m2, m3, m4, m5, p1, p2, p3;
41 D3DXMatrixScaling(&m1, scaling, scaling, scaling);
42 if ( !rotationcenter )
44 D3DXMatrixIdentity(&m2);
45 D3DXMatrixIdentity(&m4);
47 else
49 D3DXMatrixTranslation(&m2, -rotationcenter->x, -rotationcenter->y, -rotationcenter->z);
50 D3DXMatrixTranslation(&m4, rotationcenter->x, rotationcenter->y, rotationcenter->z);
52 if ( !rotation )
54 D3DXMatrixIdentity(&m3);
56 else
58 D3DXMatrixRotationQuaternion(&m3, rotation);
60 if ( !translation )
62 D3DXMatrixIdentity(&m5);
64 else
66 D3DXMatrixTranslation(&m5, translation->x, translation->y, translation->z);
68 D3DXMatrixMultiply(&p1, &m1, &m2);
69 D3DXMatrixMultiply(&p2, &p1, &m3);
70 D3DXMatrixMultiply(&p3, &p2, &m4);
71 D3DXMatrixMultiply(pout, &p3, &m5);
72 return pout;
75 FLOAT WINAPI D3DXMatrixfDeterminant(CONST D3DXMATRIX *pm)
77 D3DXVECTOR4 minor, v1, v2, v3;
78 FLOAT det;
80 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];
81 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];
82 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];
83 D3DXVec4Cross(&minor,&v1,&v2,&v3);
84 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);
85 return det;
88 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, CONST D3DXMATRIX *pm)
90 int a, i, j;
91 D3DXVECTOR4 v, vec[3];
92 FLOAT cofactor, det;
94 det = D3DXMatrixfDeterminant(pm);
95 if ( !det ) return NULL;
96 if ( pdeterminant ) *pdeterminant = det;
97 for (i=0; i<4; i++)
99 for (j=0; j<4; j++)
101 if (j != i )
103 a = j;
104 if ( j > i ) a = a-1;
105 vec[a].x = pm->u.m[j][0];
106 vec[a].y = pm->u.m[j][1];
107 vec[a].z = pm->u.m[j][2];
108 vec[a].w = pm->u.m[j][3];
111 D3DXVec4Cross(&v, &vec[0], &vec[1], &vec[2]);
112 for (j=0; j<4; j++)
114 switch(j)
116 case 0: cofactor = v.x; break;
117 case 1: cofactor = v.y; break;
118 case 2: cofactor = v.z; break;
119 case 3: cofactor = v.w; break;
121 pout->u.m[j][i] = pow(-1.0f, i) * cofactor / det;
124 return pout;
127 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
129 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
131 D3DXVec3Subtract(&vec2, pat, peye);
132 D3DXVec3Normalize(&vec, &vec2);
133 D3DXVec3Cross(&right, pup, &vec);
134 D3DXVec3Cross(&up, &vec, &right);
135 D3DXVec3Normalize(&rightn, &right);
136 D3DXVec3Normalize(&upn, &up);
137 pout->u.m[0][0] = rightn.x;
138 pout->u.m[1][0] = rightn.y;
139 pout->u.m[2][0] = rightn.z;
140 pout->u.m[3][0] = -D3DXVec3Dot(&rightn,peye);
141 pout->u.m[0][1] = upn.x;
142 pout->u.m[1][1] = upn.y;
143 pout->u.m[2][1] = upn.z;
144 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
145 pout->u.m[0][2] = vec.x;
146 pout->u.m[1][2] = vec.y;
147 pout->u.m[2][2] = vec.z;
148 pout->u.m[3][2] = -D3DXVec3Dot(&vec, peye);
149 pout->u.m[0][3] = 0.0f;
150 pout->u.m[1][3] = 0.0f;
151 pout->u.m[2][3] = 0.0f;
152 pout->u.m[3][3] = 1.0f;
153 return pout;
156 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
158 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
160 D3DXVec3Subtract(&vec2, pat, peye);
161 D3DXVec3Normalize(&vec, &vec2);
162 D3DXVec3Cross(&right, pup, &vec);
163 D3DXVec3Cross(&up, &vec, &right);
164 D3DXVec3Normalize(&rightn, &right);
165 D3DXVec3Normalize(&upn, &up);
166 pout->u.m[0][0] = -rightn.x;
167 pout->u.m[1][0] = -rightn.y;
168 pout->u.m[2][0] = -rightn.z;
169 pout->u.m[3][0] = D3DXVec3Dot(&rightn,peye);
170 pout->u.m[0][1] = upn.x;
171 pout->u.m[1][1] = upn.y;
172 pout->u.m[2][1] = upn.z;
173 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
174 pout->u.m[0][2] = -vec.x;
175 pout->u.m[1][2] = -vec.y;
176 pout->u.m[2][2] = -vec.z;
177 pout->u.m[3][2] = D3DXVec3Dot(&vec, peye);
178 pout->u.m[0][3] = 0.0f;
179 pout->u.m[1][3] = 0.0f;
180 pout->u.m[2][3] = 0.0f;
181 pout->u.m[3][3] = 1.0f;
182 return pout;
185 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
187 int i,j;
189 for (i=0; i<4; i++)
191 for (j=0; j<4; j++)
193 pout->u.m[i][j] = pm1->u.m[i][0] * pm2->u.m[0][j] + pm1->u.m[i][1] * pm2->u.m[1][j] + pm1->u.m[i][2] * pm2->u.m[2][j] + pm1->u.m[i][3] * pm2->u.m[3][j];
196 return pout;
199 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
201 D3DXMATRIX temp;
203 D3DXMatrixMultiply(&temp, pm1, pm2);
204 D3DXMatrixTranspose(pout, &temp);
205 return pout;
208 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
210 D3DXMatrixIdentity(pout);
211 pout->u.m[0][0] = 2.0f / w;
212 pout->u.m[1][1] = 2.0f / h;
213 pout->u.m[2][2] = 1.0f / (zf - zn);
214 pout->u.m[3][2] = zn / (zn - zf);
215 return pout;
218 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
220 D3DXMatrixIdentity(pout);
221 pout->u.m[0][0] = 2.0f / (r - l);
222 pout->u.m[1][1] = 2.0f / (t - b);
223 pout->u.m[2][2] = 1.0f / (zf -zn);
224 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
225 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
226 pout->u.m[3][2] = zn / (zn -zf);
227 return pout;
230 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
232 D3DXMatrixIdentity(pout);
233 pout->u.m[0][0] = 2.0f / (r - l);
234 pout->u.m[1][1] = 2.0f / (t - b);
235 pout->u.m[2][2] = 1.0f / (zn -zf);
236 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
237 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
238 pout->u.m[3][2] = zn / (zn -zf);
239 return pout;
242 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
244 D3DXMatrixIdentity(pout);
245 pout->u.m[0][0] = 2.0f / w;
246 pout->u.m[1][1] = 2.0f / h;
247 pout->u.m[2][2] = 1.0f / (zn - zf);
248 pout->u.m[3][2] = zn / (zn - zf);
249 return pout;
252 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
254 D3DXMatrixIdentity(pout);
255 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
256 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
257 pout->u.m[2][2] = zf / (zf - zn);
258 pout->u.m[2][3] = 1.0f;
259 pout->u.m[3][2] = (zf * zn) / (zn - zf);
260 pout->u.m[3][3] = 0.0f;
261 return pout;
264 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
266 D3DXMatrixIdentity(pout);
267 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
268 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
269 pout->u.m[2][2] = zf / (zn - zf);
270 pout->u.m[2][3] = -1.0f;
271 pout->u.m[3][2] = (zf * zn) / (zn - zf);
272 pout->u.m[3][3] = 0.0f;
273 return pout;
276 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
278 D3DXMatrixIdentity(pout);
279 pout->u.m[0][0] = 2.0f * zn / w;
280 pout->u.m[1][1] = 2.0f * zn / h;
281 pout->u.m[2][2] = zf / (zf - zn);
282 pout->u.m[3][2] = (zn * zf) / (zn - zf);
283 pout->u.m[2][3] = 1.0f;
284 pout->u.m[3][3] = 0.0f;
285 return pout;
288 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
290 D3DXMatrixIdentity(pout);
291 pout->u.m[0][0] = 2.0f * zn / (r - l);
292 pout->u.m[1][1] = -2.0f * zn / (b - t);
293 pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
294 pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
295 pout->u.m[2][2] = - zf / (zn - zf);
296 pout->u.m[3][2] = (zn * zf) / (zn -zf);
297 pout->u.m[2][3] = 1.0f;
298 pout->u.m[3][3] = 0.0f;
299 return pout;
302 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
304 D3DXMatrixIdentity(pout);
305 pout->u.m[0][0] = 2.0f * zn / (r - l);
306 pout->u.m[1][1] = -2.0f * zn / (b - t);
307 pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
308 pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
309 pout->u.m[2][2] = zf / (zn - zf);
310 pout->u.m[3][2] = (zn * zf) / (zn -zf);
311 pout->u.m[2][3] = -1.0f;
312 pout->u.m[3][3] = 0.0f;
313 return pout;
316 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
318 D3DXMatrixIdentity(pout);
319 pout->u.m[0][0] = 2.0f * zn / w;
320 pout->u.m[1][1] = 2.0f * zn / h;
321 pout->u.m[2][2] = zf / (zn - zf);
322 pout->u.m[3][2] = (zn * zf) / (zn - zf);
323 pout->u.m[2][3] = -1.0f;
324 pout->u.m[3][3] = 0.0f;
325 return pout;
328 D3DXMATRIX* WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
330 D3DXVECTOR3 v;
332 D3DXVec3Normalize(&v,pv);
333 D3DXMatrixIdentity(pout);
334 pout->u.m[0][0] = (1.0f - cos(angle)) * v.x * v.x + cos(angle);
335 pout->u.m[1][0] = (1.0f - cos(angle)) * v.x * v.y - sin(angle) * v.z;
336 pout->u.m[2][0] = (1.0f - cos(angle)) * v.x * v.z + sin(angle) * v.y;
337 pout->u.m[0][1] = (1.0f - cos(angle)) * v.y * v.x + sin(angle) * v.z;
338 pout->u.m[1][1] = (1.0f - cos(angle)) * v.y * v.y + cos(angle);
339 pout->u.m[2][1] = (1.0f - cos(angle)) * v.y * v.z - sin(angle) * v.x;
340 pout->u.m[0][2] = (1.0f - cos(angle)) * v.z * v.x - sin(angle) * v.y;
341 pout->u.m[1][2] = (1.0f - cos(angle)) * v.z * v.y + sin(angle) * v.x;
342 pout->u.m[2][2] = (1.0f - cos(angle)) * v.z * v.z + cos(angle);
343 return pout;
346 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, CONST D3DXQUATERNION *pq)
348 D3DXMatrixIdentity(pout);
349 pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
350 pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
351 pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
352 pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
353 pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
354 pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
355 pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
356 pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
357 pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
358 return pout;
361 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
363 D3DXMatrixIdentity(pout);
364 pout->u.m[1][1] = cos(angle);
365 pout->u.m[2][2] = cos(angle);
366 pout->u.m[1][2] = sin(angle);
367 pout->u.m[2][1] = -sin(angle);
368 return pout;
371 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
373 D3DXMatrixIdentity(pout);
374 pout->u.m[0][0] = cos(angle);
375 pout->u.m[2][2] = cos(angle);
376 pout->u.m[0][2] = -sin(angle);
377 pout->u.m[2][0] = sin(angle);
378 return pout;
381 D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
383 D3DXMATRIX m, pout1, pout2, pout3;
385 D3DXMatrixIdentity(&pout3);
386 D3DXMatrixRotationZ(&m,roll);
387 D3DXMatrixMultiply(&pout2,&pout3,&m);
388 D3DXMatrixRotationX(&m,pitch);
389 D3DXMatrixMultiply(&pout1,&pout2,&m);
390 D3DXMatrixRotationY(&m,yaw);
391 D3DXMatrixMultiply(pout,&pout1,&m);
392 return pout;
394 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
396 D3DXMatrixIdentity(pout);
397 pout->u.m[0][0] = cos(angle);
398 pout->u.m[1][1] = cos(angle);
399 pout->u.m[0][1] = sin(angle);
400 pout->u.m[1][0] = -sin(angle);
401 return pout;
404 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
406 D3DXMatrixIdentity(pout);
407 pout->u.m[0][0] = sx;
408 pout->u.m[1][1] = sy;
409 pout->u.m[2][2] = sz;
410 return pout;
413 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
415 D3DXMatrixIdentity(pout);
416 pout->u.m[3][0] = x;
417 pout->u.m[3][1] = y;
418 pout->u.m[3][2] = z;
419 return pout;
422 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
424 int i,j;
426 for (i=0; i<4; i++)
428 for (j=0; j<4; j++)
430 pout->u.m[i][j] = pm->u.m[j][i];
433 return pout;
436 /*_________________D3DXPLANE________________*/
438 D3DXPLANE* WINAPI D3DXPlaneNormalize(D3DXPLANE *pout, CONST D3DXPLANE *pp)
440 FLOAT norm;
442 norm = sqrt(pp->a * pp->a + pp->b * pp->b + pp->c * pp->c);
443 if ( norm )
445 pout->a = pp->a / norm;
446 pout->b = pp->b / norm;
447 pout->c = pp->c / norm;
448 pout->d = pp->d / norm;
450 else
452 pout->a = 0.0f;
453 pout->b = 0.0f;
454 pout->c = 0.0f;
455 pout->d = 0.0f;
457 return pout;
460 /*_________________D3DXQUATERNION________________*/
462 D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
464 FLOAT norm;
466 norm = D3DXQuaternionLength(pq);
467 if ( !norm )
469 pout->x = 0.0f;
470 pout->y = 0.0f;
471 pout->z = 0.0f;
472 pout->w = 0.0f;
474 else
476 pout->x = pq->x / norm;
477 pout->y = pq->y / norm;
478 pout->z = pq->z / norm;
479 pout->w = pq->w / norm;
481 return pout;
483 /*_________________D3DXVec2_____________________*/
485 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
487 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
488 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
489 return pout;
492 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv0, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT s)
494 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);
495 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);
496 return pout;
499 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pt1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pt2, FLOAT s)
501 FLOAT h1, h2, h3, h4;
503 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
504 h2 = s * s * s - 2.0f * s * s + s;
505 h3 = -2.0f * s * s * s + 3.0f * s * s;
506 h4 = s * s * s - s * s;
508 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
509 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
510 return pout;
513 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
515 FLOAT norm;
517 norm = D3DXVec2Length(pv);
518 if ( !norm )
520 pout->x = 0.0f;
521 pout->y = 0.0f;
523 else
525 pout->x = pv->x / norm;
526 pout->y = pv->y / norm;
528 return pout;
531 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
533 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
534 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
535 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
536 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
537 return pout;
540 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
542 FLOAT norm;
544 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
545 if ( norm )
547 pout->x = (pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0]) / norm;
548 pout->y = (pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1]) / norm;
550 else
552 pout->x = 0.0f;
553 pout->y = 0.0f;
555 return pout;
558 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
560 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y;
561 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y;
562 return pout;
565 /*_________________D3DXVec3_____________________*/
567 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
569 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
570 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
571 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
572 return pout;
575 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
577 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);
578 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);
579 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);
580 return pout;
583 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
585 FLOAT h1, h2, h3, h4;
587 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
588 h2 = s * s * s - 2.0f * s * s + s;
589 h3 = -2.0f * s * s * s + 3.0f * s * s;
590 h4 = s * s * s - s * s;
592 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
593 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
594 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
595 return pout;
598 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
600 FLOAT norm;
602 norm = D3DXVec3Length(pv);
603 if ( !norm )
605 pout->x = 0.0f;
606 pout->y = 0.0f;
607 pout->z = 0.0f;
609 else
611 pout->x = pv->x / norm;
612 pout->y = pv->y / norm;
613 pout->z = pv->z / norm;
615 return pout;
618 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT8 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
620 D3DXMATRIX m1, m2;
621 D3DXVECTOR3 vec;
623 D3DXMatrixMultiply(&m1, pworld, pview);
624 D3DXMatrixMultiply(&m2, &m1, pprojection);
625 D3DXVec3TransformCoord(&vec, pv, &m2);
626 pout->x = pviewport->X + ( 1.0f + vec.x ) * pviewport->Width / 2.0f;
627 pout->y = pviewport->Y + ( 1.0f - vec.y ) * pviewport->Height / 2.0f;
628 pout->z = pviewport->MinZ + vec.z * ( pviewport->MaxZ - pviewport->MinZ );
629 return pout;
632 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
634 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];
635 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];
636 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];
637 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];
638 return pout;
641 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
643 FLOAT norm;
645 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];
647 if ( norm )
649 pout->x = (pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z + pm->u.m[3][0]) / norm;
650 pout->y = (pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z + pm->u.m[3][1]) / norm;
651 pout->z = (pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z + pm->u.m[3][2]) / norm;
653 else
655 pout->x = 0.0f;
656 pout->y = 0.0f;
657 pout->z = 0.0f;
659 return pout;
662 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
664 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z;
665 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z;
666 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z;
667 return pout;
671 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT8 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
673 D3DXMATRIX m1, m2, m3;
674 D3DXVECTOR3 vec;
676 D3DXMatrixMultiply(&m1, pworld, pview);
677 D3DXMatrixMultiply(&m2, &m1, pprojection);
678 D3DXMatrixInverse(&m3, NULL, &m2);
679 vec.x = 2.0f * ( pv->x - pviewport->X ) / pviewport->Width - 1.0f;
680 vec.y = 1.0f - 2.0f * ( pv->y - pviewport->Y ) / pviewport->Height;
681 vec.z = ( pv->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
682 D3DXVec3TransformCoord(pout, &vec, &m3);
683 return pout;
686 /*_________________D3DXVec4_____________________*/
688 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
690 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
691 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
692 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
693 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
694 return pout;
697 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv0, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT s)
699 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);
700 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);
701 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);
702 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);
703 return pout;
706 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
708 pout->x = pv1->y * (pv2->z * pv3->w - pv3->z * pv2->w) - pv1->z * (pv2->y * pv3->w - pv3->y * pv2->w) + pv1->w * (pv2->y * pv3->z - pv2->z *pv3->y);
709 pout->y = -(pv1->x * (pv2->z * pv3->w - pv3->z * pv2->w) - pv1->z * (pv2->x * pv3->w - pv3->x * pv2->w) + pv1->w * (pv2->x * pv3->z - pv3->x * pv2->z));
710 pout->z = pv1->x * (pv2->y * pv3->w - pv3->y * pv2->w) - pv1->y * (pv2->x *pv3->w - pv3->x * pv2->w) + pv1->w * (pv2->x * pv3->y - pv3->x * pv2->y);
711 pout->w = -(pv1->x * (pv2->y * pv3->z - pv3->y * pv2->z) - pv1->y * (pv2->x * pv3->z - pv3->x *pv2->z) + pv1->z * (pv2->x * pv3->y - pv3->x * pv2->y));
712 return pout;
715 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pt1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pt2, FLOAT s)
717 FLOAT h1, h2, h3, h4;
719 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
720 h2 = s * s * s - 2.0f * s * s + s;
721 h3 = -2.0f * s * s * s + 3.0f * s * s;
722 h4 = s * s * s - s * s;
724 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
725 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
726 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
727 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
728 return pout;
731 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
733 FLOAT norm;
735 norm = D3DXVec4Length(pv);
736 if ( !norm )
738 pout->x = 0.0f;
739 pout->y = 0.0f;
740 pout->z = 0.0f;
741 pout->w = 0.0f;
743 else
745 pout->x = pv->x / norm;
746 pout->y = pv->y / norm;
747 pout->z = pv->z / norm;
748 pout->w = pv->w / norm;
750 return pout;
753 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
755 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z + pm->u.m[3][0] * pv->w;
756 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z + pm->u.m[3][1] * pv->w;
757 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z + pm->u.m[3][2] * pv->w;
758 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[2][3] * pv->z + pm->u.m[3][3] * pv->w;
759 return pout;