d3d9: Initialize the test rectangle correctly.
[wine/multimedia.git] / dlls / d3dx8 / math.c
blob1f14d5c03b207f471f3d8db771fa219c5491cd92
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>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "d3dx8.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3dx8);
32 /*_________________D3DXMatrix____________________*/
34 FLOAT WINAPI D3DXMatrixfDeterminant(CONST D3DXMATRIX *pm)
36 D3DXVECTOR4 minor, v1, v2, v3;
37 FLOAT det;
39 v1.x = pm->m[0][0]; v1.y = pm->m[1][0]; v1.z = pm->m[2][0]; v1.w = pm->m[3][0];
40 v2.x = pm->m[0][1]; v2.y = pm->m[1][1]; v2.z = pm->m[2][1]; v2.w = pm->m[3][1];
41 v3.x = pm->m[0][2]; v3.y = pm->m[1][2]; v3.z = pm->m[2][2]; v3.w = pm->m[3][2];
42 D3DXVec4Cross(&minor,&v1,&v2,&v3);
43 det = - (pm->m[0][3] * minor.x + pm->m[1][3] * minor.y + pm->m[2][3] * minor.z + pm->m[3][3] * minor.w);
44 return det;
47 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
49 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
51 D3DXVec3Subtract(&vec2, pat, peye);
52 D3DXVec3Normalize(&vec, &vec2);
53 D3DXVec3Cross(&right, pup, &vec);
54 D3DXVec3Cross(&up, &vec, &right);
55 D3DXVec3Normalize(&rightn, &right);
56 D3DXVec3Normalize(&upn, &up);
57 pout->m[0][0] = rightn.x;
58 pout->m[1][0] = rightn.y;
59 pout->m[2][0] = rightn.z;
60 pout->m[3][0] = -D3DXVec3Dot(&rightn,peye);
61 pout->m[0][1] = upn.x;
62 pout->m[1][1] = upn.y;
63 pout->m[2][1] = upn.z;
64 pout->m[3][1] = -D3DXVec3Dot(&upn, peye);
65 pout->m[0][2] = vec.x;
66 pout->m[1][2] = vec.y;
67 pout->m[2][2] = vec.z;
68 pout->m[3][2] = -D3DXVec3Dot(&vec, peye);
69 pout->m[0][3] = 0.0f;
70 pout->m[1][3] = 0.0f;
71 pout->m[2][3] = 0.0f;
72 pout->m[3][3] = 1.0f;
73 return pout;
76 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
78 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
80 D3DXVec3Subtract(&vec2, pat, peye);
81 D3DXVec3Normalize(&vec, &vec2);
82 D3DXVec3Cross(&right, pup, &vec);
83 D3DXVec3Cross(&up, &vec, &right);
84 D3DXVec3Normalize(&rightn, &right);
85 D3DXVec3Normalize(&upn, &up);
86 pout->m[0][0] = -rightn.x;
87 pout->m[1][0] = -rightn.y;
88 pout->m[2][0] = -rightn.z;
89 pout->m[3][0] = D3DXVec3Dot(&rightn,peye);
90 pout->m[0][1] = upn.x;
91 pout->m[1][1] = upn.y;
92 pout->m[2][1] = upn.z;
93 pout->m[3][1] = -D3DXVec3Dot(&upn, peye);
94 pout->m[0][2] = -vec.x;
95 pout->m[1][2] = -vec.y;
96 pout->m[2][2] = -vec.z;
97 pout->m[3][2] = D3DXVec3Dot(&vec, peye);
98 pout->m[0][3] = 0.0f;
99 pout->m[1][3] = 0.0f;
100 pout->m[2][3] = 0.0f;
101 pout->m[3][3] = 1.0f;
102 return pout;
105 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
107 int i,j;
109 for (i=0; i<4; i++)
111 for (j=0; j<4; j++)
113 pout->m[i][j] = pm1->m[i][0] * pm2->m[0][j] + pm1->m[i][1] * pm2->m[1][j] + pm1->m[i][2] * pm2->m[2][j] + pm1->m[i][3] * pm2->m[3][j];
116 return pout;
119 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
121 D3DXMatrixIdentity(pout);
122 pout->m[0][0] = 2.0f / w;
123 pout->m[1][1] = 2.0f / h;
124 pout->m[2][2] = 1.0f / (zf - zn);
125 pout->m[3][2] = zn / (zn - zf);
126 return pout;
129 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
131 D3DXMatrixIdentity(pout);
132 pout->m[0][0] = 2.0f / (r - l);
133 pout->m[1][1] = 2.0f / (t - b);
134 pout->m[2][2] = 1.0f / (zf -zn);
135 pout->m[3][0] = -1.0f -2.0f *l / (r - l);
136 pout->m[3][1] = 1.0f + 2.0f * t / (b - t);
137 pout->m[3][2] = zn / (zn -zf);
138 return pout;
141 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
143 D3DXMatrixIdentity(pout);
144 pout->m[0][0] = 2.0f / (r - l);
145 pout->m[1][1] = 2.0f / (t - b);
146 pout->m[2][2] = 1.0f / (zn -zf);
147 pout->m[3][0] = -1.0f -2.0f *l / (r - l);
148 pout->m[3][1] = 1.0f + 2.0f * t / (b - t);
149 pout->m[3][2] = zn / (zn -zf);
150 return pout;
153 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
155 D3DXMatrixIdentity(pout);
156 pout->m[0][0] = 2.0f / w;
157 pout->m[1][1] = 2.0f / h;
158 pout->m[2][2] = 1.0f / (zn - zf);
159 pout->m[3][2] = zn / (zn - zf);
160 return pout;
163 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
165 D3DXMatrixIdentity(pout);
166 pout->m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
167 pout->m[1][1] = 1.0f / tan(fovy/2.0f);
168 pout->m[2][2] = zf / (zf - zn);
169 pout->m[2][3] = 1.0f;
170 pout->m[3][2] = (zf * zn) / (zn - zf);
171 pout->m[3][3] = 0.0f;
172 return pout;
175 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
177 D3DXMatrixIdentity(pout);
178 pout->m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
179 pout->m[1][1] = 1.0f / tan(fovy/2.0f);
180 pout->m[2][2] = zf / (zn - zf);
181 pout->m[2][3] = -1.0f;
182 pout->m[3][2] = (zf * zn) / (zn - zf);
183 pout->m[3][3] = 0.0f;
184 return pout;
187 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
189 D3DXMatrixIdentity(pout);
190 pout->m[0][0] = 2.0f * zn / w;
191 pout->m[1][1] = 2.0f * zn / h;
192 pout->m[2][2] = zf / (zf - zn);
193 pout->m[3][2] = (zn * zf) / (zn - zf);
194 pout->m[2][3] = 1.0f;
195 pout->m[3][3] = 0.0f;
196 return pout;
199 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
201 D3DXMatrixIdentity(pout);
202 pout->m[0][0] = 2.0f * zn / (r - l);
203 pout->m[1][1] = -2.0f * zn / (b - t);
204 pout->m[2][0] = -1.0f - 2.0f * l / (r - l);
205 pout->m[2][1] = 1.0f + 2.0f * t / (b - t);
206 pout->m[2][2] = - zf / (zn - zf);
207 pout->m[3][2] = (zn * zf) / (zn -zf);
208 pout->m[2][3] = 1.0f;
209 pout->m[3][3] = 0.0f;
210 return pout;
213 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
215 D3DXMatrixIdentity(pout);
216 pout->m[0][0] = 2.0f * zn / (r - l);
217 pout->m[1][1] = -2.0f * zn / (b - t);
218 pout->m[2][0] = 1.0f + 2.0f * l / (r - l);
219 pout->m[2][1] = -1.0f -2.0f * t / (b - t);
220 pout->m[2][2] = zf / (zn - zf);
221 pout->m[3][2] = (zn * zf) / (zn -zf);
222 pout->m[2][3] = -1.0f;
223 pout->m[3][3] = 0.0f;
224 return pout;
227 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
229 D3DXMatrixIdentity(pout);
230 pout->m[0][0] = 2.0f * zn / w;
231 pout->m[1][1] = 2.0f * zn / h;
232 pout->m[2][2] = zf / (zn - zf);
233 pout->m[3][2] = (zn * zf) / (zn - zf);
234 pout->m[2][3] = -1.0f;
235 pout->m[3][3] = 0.0f;
236 return pout;
239 D3DXMATRIX* WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
241 D3DXVECTOR3 v;
243 D3DXVec3Normalize(&v,pv);
244 D3DXMatrixIdentity(pout);
245 pout->m[0][0] = (1.0f - cos(angle)) * v.x * v.x + cos(angle);
246 pout->m[1][0] = (1.0f - cos(angle)) * v.x * v.y - sin(angle) * v.z;
247 pout->m[2][0] = (1.0f - cos(angle)) * v.x * v.z + sin(angle) * v.y;
248 pout->m[0][1] = (1.0f - cos(angle)) * v.y * v.x + sin(angle) * v.z;
249 pout->m[1][1] = (1.0f - cos(angle)) * v.y * v.y + cos(angle);
250 pout->m[2][1] = (1.0f - cos(angle)) * v.y * v.z - sin(angle) * v.x;
251 pout->m[0][2] = (1.0f - cos(angle)) * v.z * v.x - sin(angle) * v.y;
252 pout->m[1][2] = (1.0f - cos(angle)) * v.z * v.y + sin(angle) * v.x;
253 pout->m[2][2] = (1.0f - cos(angle)) * v.z * v.z + cos(angle);
254 return pout;
257 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, CONST D3DXQUATERNION *pq)
259 D3DXMatrixIdentity(pout);
260 pout->m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
261 pout->m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
262 pout->m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
263 pout->m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
264 pout->m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
265 pout->m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
266 pout->m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
267 pout->m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
268 pout->m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
269 return pout;
272 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
274 D3DXMatrixIdentity(pout);
275 pout->m[1][1] = cos(angle);
276 pout->m[2][2] = cos(angle);
277 pout->m[1][2] = sin(angle);
278 pout->m[2][1] = -sin(angle);
279 return pout;
282 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
284 D3DXMatrixIdentity(pout);
285 pout->m[0][0] = cos(angle);
286 pout->m[2][2] = cos(angle);
287 pout->m[0][2] = -sin(angle);
288 pout->m[2][0] = sin(angle);
289 return pout;
292 D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
294 D3DXMATRIX m, pout1, pout2, pout3;
296 D3DXMatrixIdentity(&pout3);
297 D3DXMatrixRotationZ(&m,roll);
298 D3DXMatrixMultiply(&pout2,&pout3,&m);
299 D3DXMatrixRotationX(&m,pitch);
300 D3DXMatrixMultiply(&pout1,&pout2,&m);
301 D3DXMatrixRotationY(&m,yaw);
302 D3DXMatrixMultiply(pout,&pout1,&m);
303 return pout;
305 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
307 D3DXMatrixIdentity(pout);
308 pout->m[0][0] = cos(angle);
309 pout->m[1][1] = cos(angle);
310 pout->m[0][1] = sin(angle);
311 pout->m[1][0] = -sin(angle);
312 return pout;
315 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
317 D3DXMatrixIdentity(pout);
318 pout->m[0][0] = sx;
319 pout->m[1][1] = sy;
320 pout->m[2][2] = sz;
321 return pout;
324 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
326 D3DXMatrixIdentity(pout);
327 pout->m[3][0] = x;
328 pout->m[3][1] = y;
329 pout->m[3][2] = z;
330 return pout;
333 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
335 int i,j;
337 for (i=0; i<4; i++)
339 for (j=0; j<4; j++)
341 pout->m[i][j] = pm->m[j][i];
344 return pout;
347 /*_________________D3DXQUATERNION________________*/
349 D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
351 FLOAT norm;
353 norm = D3DXQuaternionLength(pq);
354 if ( !norm )
356 pout->x = 0.0f;
357 pout->y = 0.0f;
358 pout->z = 0.0f;
359 pout->w = 0.0f;
361 else
363 pout->x = pq->x / norm;
364 pout->y = pq->y / norm;
365 pout->z = pq->z / norm;
366 pout->w = pq->w / norm;
368 return pout;
370 /*_________________D3DXVec2_____________________*/
372 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
374 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
375 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
376 return pout;
379 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv0, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT s)
381 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);
382 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);
383 return pout;
386 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pt1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pt2, FLOAT s)
388 FLOAT h1, h2, h3, h4;
390 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
391 h2 = s * s * s - 2.0f * s * s + s;
392 h3 = -2.0f * s * s * s + 3.0f * s * s;
393 h4 = s * s * s - s * s;
395 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
396 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
397 return pout;
400 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
402 FLOAT norm;
404 norm = D3DXVec2Length(pv);
405 if ( !norm )
407 pout->x = 0.0f;
408 pout->y = 0.0f;
410 else
412 pout->x = pv->x / norm;
413 pout->y = pv->y / norm;
415 return pout;
418 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
420 pout->x = pm->m[0][0] * pv->x + pm->m[1][0] * pv->y + pm->m[3][0];
421 pout->y = pm->m[0][1] * pv->x + pm->m[1][1] * pv->y + pm->m[3][1];
422 pout->z = pm->m[0][2] * pv->x + pm->m[1][2] * pv->y + pm->m[3][2];
423 pout->w = pm->m[0][3] * pv->x + pm->m[1][3] * pv->y + pm->m[3][3];
424 return pout;
427 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
429 FLOAT norm;
431 norm = pm->m[0][3] * pv->x + pm->m[1][3] * pv->y + pm->m[3][3];
432 if ( norm )
434 pout->x = (pm->m[0][0] * pv->x + pm->m[1][0] * pv->y + pm->m[3][0]) / norm;
435 pout->y = (pm->m[0][1] * pv->x + pm->m[1][1] * pv->y + pm->m[3][1]) / norm;
437 else
439 pout->x = 0.0f;
440 pout->y = 0.0f;
442 return pout;
445 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
447 pout->x = pm->m[0][0] * pv->x + pm->m[1][0] * pv->y;
448 pout->y = pm->m[0][1] * pv->x + pm->m[1][1] * pv->y;
449 return pout;
452 /*_________________D3DXVec3_____________________*/
454 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
456 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
457 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
458 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
459 return pout;
462 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
464 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);
465 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);
466 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);
467 return pout;
470 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
472 FLOAT h1, h2, h3, h4;
474 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
475 h2 = s * s * s - 2.0f * s * s + s;
476 h3 = -2.0f * s * s * s + 3.0f * s * s;
477 h4 = s * s * s - s * s;
479 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
480 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
481 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
482 return pout;
485 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
487 FLOAT norm;
489 norm = D3DXVec3Length(pv);
490 if ( !norm )
492 pout->x = 0.0f;
493 pout->y = 0.0f;
494 pout->z = 0.0f;
496 else
498 pout->x = pv->x / norm;
499 pout->y = pv->y / norm;
500 pout->z = pv->z / norm;
502 return pout;
505 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
507 pout->x = pm->m[0][0] * pv->x + pm->m[1][0] * pv->y + pm->m[2][0] * pv->z + pm->m[3][0];
508 pout->y = pm->m[0][1] * pv->x + pm->m[1][1] * pv->y + pm->m[2][1] * pv->z + pm->m[3][1];
509 pout->z = pm->m[0][2] * pv->x + pm->m[1][2] * pv->y + pm->m[2][2] * pv->z + pm->m[3][2];
510 pout->w = pm->m[0][3] * pv->x + pm->m[1][3] * pv->y + pm->m[2][3] * pv->z + pm->m[3][3];
511 return pout;
514 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
516 FLOAT norm;
518 norm = pm->m[0][3] * pv->x + pm->m[1][3] * pv->y + pm->m[2][3] *pv->z + pm->m[3][3];
520 if ( norm )
522 pout->x = (pm->m[0][0] * pv->x + pm->m[1][0] * pv->y + pm->m[2][0] * pv->z + pm->m[3][0]) / norm;
523 pout->y = (pm->m[0][1] * pv->x + pm->m[1][1] * pv->y + pm->m[2][1] * pv->z + pm->m[3][1]) / norm;
524 pout->z = (pm->m[0][2] * pv->x + pm->m[1][2] * pv->y + pm->m[2][2] * pv->z + pm->m[3][2]) / norm;
526 else
528 pout->x = 0.0f;
529 pout->y = 0.0f;
530 pout->z = 0.0f;
532 return pout;
535 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
537 pout->x = pm->m[0][0] * pv->x + pm->m[1][0] * pv->y + pm->m[2][0] * pv->z;
538 pout->y = pm->m[0][1] * pv->x + pm->m[1][1] * pv->y + pm->m[2][1] * pv->z;
539 pout->z = pm->m[0][2] * pv->x + pm->m[1][2] * pv->y + pm->m[2][2] * pv->z;
540 return pout;
544 /*_________________D3DXVec4_____________________*/
546 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
548 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
549 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
550 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
551 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
552 return pout;
555 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv0, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT s)
557 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);
558 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);
559 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);
560 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);
561 return pout;
564 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
566 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);
567 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));
568 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);
569 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));
570 return pout;
573 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pt1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pt2, FLOAT s)
575 FLOAT h1, h2, h3, h4;
577 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
578 h2 = s * s * s - 2.0f * s * s + s;
579 h3 = -2.0f * s * s * s + 3.0f * s * s;
580 h4 = s * s * s - s * s;
582 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
583 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
584 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
585 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
586 return pout;
589 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
591 FLOAT norm;
593 norm = D3DXVec4Length(pv);
594 if ( !norm )
596 pout->x = 0.0f;
597 pout->y = 0.0f;
598 pout->z = 0.0f;
599 pout->w = 0.0f;
601 else
603 pout->x = pv->x / norm;
604 pout->y = pv->y / norm;
605 pout->z = pv->z / norm;
606 pout->w = pv->w / norm;
608 return pout;
611 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
613 pout->x = pm->m[0][0] * pv->x + pm->m[1][0] * pv->y + pm->m[2][0] * pv->z + pm->m[3][0] * pv->w;
614 pout->y = pm->m[0][1] * pv->x + pm->m[1][1] * pv->y + pm->m[2][1] * pv->z + pm->m[3][1] * pv->w;
615 pout->z = pm->m[0][2] * pv->x + pm->m[1][2] * pv->y + pm->m[2][2] * pv->z + pm->m[3][2] * pv->w;
616 pout->w = pm->m[0][3] * pv->x + pm->m[1][3] * pv->y + pm->m[2][3] * pv->z + pm->m[3][3] * pv->w;
617 return pout;