Sunc with trunk revision 58971.
[reactos.git] / dll / directx / wine / d3dx9_36 / math.c
1 /*
2 * Mathematical operations specific to D3DX9.
3 *
4 * Copyright (C) 2008 David Adam
5 * Copyright (C) 2008 Luis Busquets
6 * Copyright (C) 2008 Jérôme Gardou
7 * Copyright (C) 2008 Philip Nilsson
8 * Copyright (C) 2008 Henri Verbeet
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25 #define NONAMELESSUNION
26
27 #include <config.h>
28 //#include "windef.h"
29 //#include "wingdi.h"
30 #include "d3dx9_36_private.h"
31
32 #include <wine/debug.h>
33
34 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
35
36 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl;
37
38 /*_________________D3DXColor____________________*/
39
40 D3DXCOLOR* WINAPI D3DXColorAdjustContrast(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
41 {
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;
47 }
48
49 D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
50 {
51 FLOAT grey;
52
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;
59 }
60
61 /*_________________Misc__________________________*/
62
63 FLOAT WINAPI D3DXFresnelTerm(FLOAT costheta, FLOAT refractionindex)
64 {
65 FLOAT a, d, g, result;
66
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;
73 }
74
75 /*_________________D3DXMatrix____________________*/
76
77 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR3 *rotationcenter, CONST D3DXQUATERNION *rotation, CONST D3DXVECTOR3 *translation)
78 {
79 D3DXMATRIX m1, m2, m3, m4, m5;
80
81 D3DXMatrixScaling(&m1, scaling, scaling, scaling);
82
83 if ( !rotationcenter )
84 {
85 D3DXMatrixIdentity(&m2);
86 D3DXMatrixIdentity(&m4);
87 }
88 else
89 {
90 D3DXMatrixTranslation(&m2, -rotationcenter->x, -rotationcenter->y, -rotationcenter->z);
91 D3DXMatrixTranslation(&m4, rotationcenter->x, rotationcenter->y, rotationcenter->z);
92 }
93
94 if ( !rotation ) D3DXMatrixIdentity(&m3);
95 else D3DXMatrixRotationQuaternion(&m3, rotation);
96
97 if ( !translation ) D3DXMatrixIdentity(&m5);
98 else D3DXMatrixTranslation(&m5, translation->x, translation->y, translation->z);
99
100 D3DXMatrixMultiply(&m1, &m1, &m2);
101 D3DXMatrixMultiply(&m1, &m1, &m3);
102 D3DXMatrixMultiply(&m1, &m1, &m4);
103 D3DXMatrixMultiply(pout, &m1, &m5);
104 return pout;
105 }
106
107 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation2D(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR2 *protationcenter, FLOAT rotation, CONST D3DXVECTOR2 *ptranslation)
108 {
109 D3DXMATRIX m1, m2, m3, m4, m5;
110 D3DXQUATERNION rot;
111 D3DXVECTOR3 rot_center, trans;
112
113 rot.w=cos(rotation/2.0f);
114 rot.x=0.0f;
115 rot.y=0.0f;
116 rot.z=sin(rotation/2.0f);
117
118 if ( protationcenter )
119 {
120 rot_center.x=protationcenter->x;
121 rot_center.y=protationcenter->y;
122 rot_center.z=0.0f;
123 }
124 else
125 {
126 rot_center.x=0.0f;
127 rot_center.y=0.0f;
128 rot_center.z=0.0f;
129 }
130
131 if ( ptranslation )
132 {
133 trans.x=ptranslation->x;
134 trans.y=ptranslation->y;
135 trans.z=0.0f;
136 }
137 else
138 {
139 trans.x=0.0f;
140 trans.y=0.0f;
141 trans.z=0.0f;
142 }
143
144 D3DXMatrixScaling(&m1, scaling, scaling, 1.0f);
145 D3DXMatrixTranslation(&m2, -rot_center.x, -rot_center.y, -rot_center.z);
146 D3DXMatrixTranslation(&m4, rot_center.x, rot_center.y, rot_center.z);
147 D3DXMatrixRotationQuaternion(&m3, &rot);
148 D3DXMatrixTranslation(&m5, trans.x, trans.y, trans.z);
149
150 D3DXMatrixMultiply(&m1, &m1, &m2);
151 D3DXMatrixMultiply(&m1, &m1, &m3);
152 D3DXMatrixMultiply(&m1, &m1, &m4);
153 D3DXMatrixMultiply(pout, &m1, &m5);
154
155 return pout;
156 }
157
158 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, CONST D3DXMATRIX *pm)
159 {
160 D3DXMATRIX normalized;
161 D3DXVECTOR3 vec;
162
163 /*Compute the scaling part.*/
164 vec.x=pm->u.m[0][0];
165 vec.y=pm->u.m[0][1];
166 vec.z=pm->u.m[0][2];
167 poutscale->x=D3DXVec3Length(&vec);
168
169 vec.x=pm->u.m[1][0];
170 vec.y=pm->u.m[1][1];
171 vec.z=pm->u.m[1][2];
172 poutscale->y=D3DXVec3Length(&vec);
173
174 vec.x=pm->u.m[2][0];
175 vec.y=pm->u.m[2][1];
176 vec.z=pm->u.m[2][2];
177 poutscale->z=D3DXVec3Length(&vec);
178
179 /*Compute the translation part.*/
180 pouttranslation->x=pm->u.m[3][0];
181 pouttranslation->y=pm->u.m[3][1];
182 pouttranslation->z=pm->u.m[3][2];
183
184 /*Let's calculate the rotation now*/
185 if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) ) return D3DERR_INVALIDCALL;
186
187 normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
188 normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
189 normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
190 normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
191 normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
192 normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
193 normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
194 normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
195 normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
196
197 D3DXQuaternionRotationMatrix(poutrotation,&normalized);
198 return S_OK;
199 }
200
201 FLOAT WINAPI D3DXMatrixDeterminant(CONST D3DXMATRIX *pm)
202 {
203 D3DXVECTOR4 minor, v1, v2, v3;
204 FLOAT det;
205
206 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];
207 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];
208 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];
209 D3DXVec4Cross(&minor, &v1, &v2, &v3);
210 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);
211 return det;
212 }
213
214 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, CONST D3DXMATRIX *pm)
215 {
216 int a, i, j;
217 D3DXMATRIX out;
218 D3DXVECTOR4 v, vec[3];
219 FLOAT det;
220
221 det = D3DXMatrixDeterminant(pm);
222 if ( !det ) return NULL;
223 if ( pdeterminant ) *pdeterminant = det;
224 for (i=0; i<4; i++)
225 {
226 for (j=0; j<4; j++)
227 {
228 if (j != i )
229 {
230 a = j;
231 if ( j > i ) a = a-1;
232 vec[a].x = pm->u.m[j][0];
233 vec[a].y = pm->u.m[j][1];
234 vec[a].z = pm->u.m[j][2];
235 vec[a].w = pm->u.m[j][3];
236 }
237 }
238 D3DXVec4Cross(&v, &vec[0], &vec[1], &vec[2]);
239 out.u.m[0][i] = pow(-1.0f, i) * v.x / det;
240 out.u.m[1][i] = pow(-1.0f, i) * v.y / det;
241 out.u.m[2][i] = pow(-1.0f, i) * v.z / det;
242 out.u.m[3][i] = pow(-1.0f, i) * v.w / det;
243 }
244
245 *pout = out;
246 return pout;
247 }
248
249 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
250 {
251 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
252
253 D3DXVec3Subtract(&vec2, pat, peye);
254 D3DXVec3Normalize(&vec, &vec2);
255 D3DXVec3Cross(&right, pup, &vec);
256 D3DXVec3Cross(&up, &vec, &right);
257 D3DXVec3Normalize(&rightn, &right);
258 D3DXVec3Normalize(&upn, &up);
259 pout->u.m[0][0] = rightn.x;
260 pout->u.m[1][0] = rightn.y;
261 pout->u.m[2][0] = rightn.z;
262 pout->u.m[3][0] = -D3DXVec3Dot(&rightn,peye);
263 pout->u.m[0][1] = upn.x;
264 pout->u.m[1][1] = upn.y;
265 pout->u.m[2][1] = upn.z;
266 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
267 pout->u.m[0][2] = vec.x;
268 pout->u.m[1][2] = vec.y;
269 pout->u.m[2][2] = vec.z;
270 pout->u.m[3][2] = -D3DXVec3Dot(&vec, peye);
271 pout->u.m[0][3] = 0.0f;
272 pout->u.m[1][3] = 0.0f;
273 pout->u.m[2][3] = 0.0f;
274 pout->u.m[3][3] = 1.0f;
275 return pout;
276 }
277
278 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
279 {
280 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
281
282 D3DXVec3Subtract(&vec2, pat, peye);
283 D3DXVec3Normalize(&vec, &vec2);
284 D3DXVec3Cross(&right, pup, &vec);
285 D3DXVec3Cross(&up, &vec, &right);
286 D3DXVec3Normalize(&rightn, &right);
287 D3DXVec3Normalize(&upn, &up);
288 pout->u.m[0][0] = -rightn.x;
289 pout->u.m[1][0] = -rightn.y;
290 pout->u.m[2][0] = -rightn.z;
291 pout->u.m[3][0] = D3DXVec3Dot(&rightn,peye);
292 pout->u.m[0][1] = upn.x;
293 pout->u.m[1][1] = upn.y;
294 pout->u.m[2][1] = upn.z;
295 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
296 pout->u.m[0][2] = -vec.x;
297 pout->u.m[1][2] = -vec.y;
298 pout->u.m[2][2] = -vec.z;
299 pout->u.m[3][2] = D3DXVec3Dot(&vec, peye);
300 pout->u.m[0][3] = 0.0f;
301 pout->u.m[1][3] = 0.0f;
302 pout->u.m[2][3] = 0.0f;
303 pout->u.m[3][3] = 1.0f;
304 return pout;
305 }
306
307 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
308 {
309 D3DXMATRIX out;
310 int i,j;
311
312 for (i=0; i<4; i++)
313 {
314 for (j=0; j<4; j++)
315 {
316 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];
317 }
318 }
319
320 *pout = out;
321 return pout;
322 }
323
324 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
325 {
326 D3DXMatrixMultiply(pout, pm1, pm2);
327 D3DXMatrixTranspose(pout, pout);
328 return pout;
329 }
330
331 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
332 {
333 D3DXMatrixIdentity(pout);
334 pout->u.m[0][0] = 2.0f / w;
335 pout->u.m[1][1] = 2.0f / h;
336 pout->u.m[2][2] = 1.0f / (zf - zn);
337 pout->u.m[3][2] = zn / (zn - zf);
338 return pout;
339 }
340
341 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
342 {
343 D3DXMatrixIdentity(pout);
344 pout->u.m[0][0] = 2.0f / (r - l);
345 pout->u.m[1][1] = 2.0f / (t - b);
346 pout->u.m[2][2] = 1.0f / (zf -zn);
347 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
348 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
349 pout->u.m[3][2] = zn / (zn -zf);
350 return pout;
351 }
352
353 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
354 {
355 D3DXMatrixIdentity(pout);
356 pout->u.m[0][0] = 2.0f / (r - l);
357 pout->u.m[1][1] = 2.0f / (t - b);
358 pout->u.m[2][2] = 1.0f / (zn -zf);
359 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
360 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
361 pout->u.m[3][2] = zn / (zn -zf);
362 return pout;
363 }
364
365 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
366 {
367 D3DXMatrixIdentity(pout);
368 pout->u.m[0][0] = 2.0f / w;
369 pout->u.m[1][1] = 2.0f / h;
370 pout->u.m[2][2] = 1.0f / (zn - zf);
371 pout->u.m[3][2] = zn / (zn - zf);
372 return pout;
373 }
374
375 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
376 {
377 D3DXMatrixIdentity(pout);
378 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
379 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
380 pout->u.m[2][2] = zf / (zf - zn);
381 pout->u.m[2][3] = 1.0f;
382 pout->u.m[3][2] = (zf * zn) / (zn - zf);
383 pout->u.m[3][3] = 0.0f;
384 return pout;
385 }
386
387 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
388 {
389 D3DXMatrixIdentity(pout);
390 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
391 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
392 pout->u.m[2][2] = zf / (zn - zf);
393 pout->u.m[2][3] = -1.0f;
394 pout->u.m[3][2] = (zf * zn) / (zn - zf);
395 pout->u.m[3][3] = 0.0f;
396 return pout;
397 }
398
399 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
400 {
401 D3DXMatrixIdentity(pout);
402 pout->u.m[0][0] = 2.0f * zn / w;
403 pout->u.m[1][1] = 2.0f * zn / h;
404 pout->u.m[2][2] = zf / (zf - zn);
405 pout->u.m[3][2] = (zn * zf) / (zn - zf);
406 pout->u.m[2][3] = 1.0f;
407 pout->u.m[3][3] = 0.0f;
408 return pout;
409 }
410
411 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
412 {
413 D3DXMatrixIdentity(pout);
414 pout->u.m[0][0] = 2.0f * zn / (r - l);
415 pout->u.m[1][1] = -2.0f * zn / (b - t);
416 pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
417 pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
418 pout->u.m[2][2] = - zf / (zn - zf);
419 pout->u.m[3][2] = (zn * zf) / (zn -zf);
420 pout->u.m[2][3] = 1.0f;
421 pout->u.m[3][3] = 0.0f;
422 return pout;
423 }
424
425 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
426 {
427 D3DXMatrixIdentity(pout);
428 pout->u.m[0][0] = 2.0f * zn / (r - l);
429 pout->u.m[1][1] = -2.0f * zn / (b - t);
430 pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
431 pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
432 pout->u.m[2][2] = zf / (zn - zf);
433 pout->u.m[3][2] = (zn * zf) / (zn -zf);
434 pout->u.m[2][3] = -1.0f;
435 pout->u.m[3][3] = 0.0f;
436 return pout;
437 }
438
439 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
440 {
441 D3DXMatrixIdentity(pout);
442 pout->u.m[0][0] = 2.0f * zn / w;
443 pout->u.m[1][1] = 2.0f * zn / h;
444 pout->u.m[2][2] = zf / (zn - zf);
445 pout->u.m[3][2] = (zn * zf) / (zn - zf);
446 pout->u.m[2][3] = -1.0f;
447 pout->u.m[3][3] = 0.0f;
448 return pout;
449 }
450
451 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, CONST D3DXPLANE *pplane)
452 {
453 D3DXPLANE Nplane;
454
455 D3DXPlaneNormalize(&Nplane, pplane);
456 D3DXMatrixIdentity(pout);
457 pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
458 pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
459 pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
460 pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
461 pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
462 pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
463 pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
464 pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
465 pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
466 pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
467 pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
468 pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
469 return pout;
470 }
471
472 D3DXMATRIX* WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
473 {
474 D3DXVECTOR3 v;
475
476 D3DXVec3Normalize(&v,pv);
477 D3DXMatrixIdentity(pout);
478 pout->u.m[0][0] = (1.0f - cos(angle)) * v.x * v.x + cos(angle);
479 pout->u.m[1][0] = (1.0f - cos(angle)) * v.x * v.y - sin(angle) * v.z;
480 pout->u.m[2][0] = (1.0f - cos(angle)) * v.x * v.z + sin(angle) * v.y;
481 pout->u.m[0][1] = (1.0f - cos(angle)) * v.y * v.x + sin(angle) * v.z;
482 pout->u.m[1][1] = (1.0f - cos(angle)) * v.y * v.y + cos(angle);
483 pout->u.m[2][1] = (1.0f - cos(angle)) * v.y * v.z - sin(angle) * v.x;
484 pout->u.m[0][2] = (1.0f - cos(angle)) * v.z * v.x - sin(angle) * v.y;
485 pout->u.m[1][2] = (1.0f - cos(angle)) * v.z * v.y + sin(angle) * v.x;
486 pout->u.m[2][2] = (1.0f - cos(angle)) * v.z * v.z + cos(angle);
487 return pout;
488 }
489
490 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, CONST D3DXQUATERNION *pq)
491 {
492 D3DXMatrixIdentity(pout);
493 pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
494 pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
495 pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
496 pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
497 pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
498 pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
499 pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
500 pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
501 pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
502 return pout;
503 }
504
505 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
506 {
507 D3DXMatrixIdentity(pout);
508 pout->u.m[1][1] = cos(angle);
509 pout->u.m[2][2] = cos(angle);
510 pout->u.m[1][2] = sin(angle);
511 pout->u.m[2][1] = -sin(angle);
512 return pout;
513 }
514
515 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
516 {
517 D3DXMatrixIdentity(pout);
518 pout->u.m[0][0] = cos(angle);
519 pout->u.m[2][2] = cos(angle);
520 pout->u.m[0][2] = -sin(angle);
521 pout->u.m[2][0] = sin(angle);
522 return pout;
523 }
524
525 D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
526 {
527 D3DXMATRIX m;
528
529 D3DXMatrixIdentity(pout);
530 D3DXMatrixRotationZ(&m, roll);
531 D3DXMatrixMultiply(pout, pout, &m);
532 D3DXMatrixRotationX(&m, pitch);
533 D3DXMatrixMultiply(pout, pout, &m);
534 D3DXMatrixRotationY(&m, yaw);
535 D3DXMatrixMultiply(pout, pout, &m);
536 return pout;
537 }
538 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
539 {
540 D3DXMatrixIdentity(pout);
541 pout->u.m[0][0] = cos(angle);
542 pout->u.m[1][1] = cos(angle);
543 pout->u.m[0][1] = sin(angle);
544 pout->u.m[1][0] = -sin(angle);
545 return pout;
546 }
547
548 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
549 {
550 D3DXMatrixIdentity(pout);
551 pout->u.m[0][0] = sx;
552 pout->u.m[1][1] = sy;
553 pout->u.m[2][2] = sz;
554 return pout;
555 }
556
557 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, CONST D3DXVECTOR4 *plight, CONST D3DXPLANE *pplane)
558 {
559 D3DXPLANE Nplane;
560 FLOAT dot;
561
562 D3DXPlaneNormalize(&Nplane, pplane);
563 dot = D3DXPlaneDot(&Nplane, plight);
564 pout->u.m[0][0] = dot - Nplane.a * plight->x;
565 pout->u.m[0][1] = -Nplane.a * plight->y;
566 pout->u.m[0][2] = -Nplane.a * plight->z;
567 pout->u.m[0][3] = -Nplane.a * plight->w;
568 pout->u.m[1][0] = -Nplane.b * plight->x;
569 pout->u.m[1][1] = dot - Nplane.b * plight->y;
570 pout->u.m[1][2] = -Nplane.b * plight->z;
571 pout->u.m[1][3] = -Nplane.b * plight->w;
572 pout->u.m[2][0] = -Nplane.c * plight->x;
573 pout->u.m[2][1] = -Nplane.c * plight->y;
574 pout->u.m[2][2] = dot - Nplane.c * plight->z;
575 pout->u.m[2][3] = -Nplane.c * plight->w;
576 pout->u.m[3][0] = -Nplane.d * plight->x;
577 pout->u.m[3][1] = -Nplane.d * plight->y;
578 pout->u.m[3][2] = -Nplane.d * plight->z;
579 pout->u.m[3][3] = dot - Nplane.d * plight->w;
580 return pout;
581 }
582
583 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pscalingcenter, CONST D3DXQUATERNION *pscalingrotation, CONST D3DXVECTOR3 *pscaling, CONST D3DXVECTOR3 *protationcenter, CONST D3DXQUATERNION *protation, CONST D3DXVECTOR3 *ptranslation)
584 {
585 D3DXMATRIX m1, m2, m3, m4, m5, m6, m7;
586 D3DXQUATERNION prc;
587 D3DXVECTOR3 psc, pt;
588
589 if ( !pscalingcenter )
590 {
591 psc.x = 0.0f;
592 psc.y = 0.0f;
593 psc.z = 0.0f;
594 }
595 else
596 {
597 psc.x = pscalingcenter->x;
598 psc.y = pscalingcenter->y;
599 psc.z = pscalingcenter->z;
600 }
601
602 if ( !protationcenter )
603 {
604 prc.x = 0.0f;
605 prc.y = 0.0f;
606 prc.z = 0.0f;
607 }
608 else
609 {
610 prc.x = protationcenter->x;
611 prc.y = protationcenter->y;
612 prc.z = protationcenter->z;
613 }
614
615 if ( !ptranslation )
616 {
617 pt.x = 0.0f;
618 pt.y = 0.0f;
619 pt.z = 0.0f;
620 }
621 else
622 {
623 pt.x = ptranslation->x;
624 pt.y = ptranslation->y;
625 pt.z = ptranslation->z;
626 }
627
628 D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
629
630 if ( !pscalingrotation )
631 {
632 D3DXMatrixIdentity(&m2);
633 D3DXMatrixIdentity(&m4);
634 }
635 else
636 {
637 D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
638 D3DXMatrixInverse(&m2, NULL, &m4);
639 }
640
641 if ( !pscaling ) D3DXMatrixIdentity(&m3);
642 else D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
643
644 if ( !protation ) D3DXMatrixIdentity(&m6);
645 else D3DXMatrixRotationQuaternion(&m6, protation);
646
647 D3DXMatrixTranslation(&m5, psc.x - prc.x, psc.y - prc.y, psc.z - prc.z);
648 D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
649 D3DXMatrixMultiply(&m1, &m1, &m2);
650 D3DXMatrixMultiply(&m1, &m1, &m3);
651 D3DXMatrixMultiply(&m1, &m1, &m4);
652 D3DXMatrixMultiply(&m1, &m1, &m5);
653 D3DXMatrixMultiply(&m1, &m1, &m6);
654 D3DXMatrixMultiply(pout, &m1, &m7);
655 return pout;
656 }
657 D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(D3DXMATRIX *pout, CONST D3DXVECTOR2 *pscalingcenter, FLOAT scalingrotation, CONST D3DXVECTOR2 *pscaling, CONST D3DXVECTOR2 *protationcenter, FLOAT rotation, CONST D3DXVECTOR2 *ptranslation)
658 {
659 D3DXQUATERNION rot, sca_rot;
660 D3DXVECTOR3 rot_center, sca, sca_center, trans;
661
662 if ( pscalingcenter )
663 {
664 sca_center.x=pscalingcenter->x;
665 sca_center.y=pscalingcenter->y;
666 sca_center.z=0.0f;
667 }
668 else
669 {
670 sca_center.x=0.0f;
671 sca_center.y=0.0f;
672 sca_center.z=0.0f;
673 }
674
675 if ( pscaling )
676 {
677 sca.x=pscaling->x;
678 sca.y=pscaling->y;
679 sca.z=1.0f;
680 }
681 else
682 {
683 sca.x=1.0f;
684 sca.y=1.0f;
685 sca.z=1.0f;
686 }
687
688 if ( protationcenter )
689 {
690 rot_center.x=protationcenter->x;
691 rot_center.y=protationcenter->y;
692 rot_center.z=0.0f;
693 }
694 else
695 {
696 rot_center.x=0.0f;
697 rot_center.y=0.0f;
698 rot_center.z=0.0f;
699 }
700
701 if ( ptranslation )
702 {
703 trans.x=ptranslation->x;
704 trans.y=ptranslation->y;
705 trans.z=0.0f;
706 }
707 else
708 {
709 trans.x=0.0f;
710 trans.y=0.0f;
711 trans.z=0.0f;
712 }
713
714 rot.w=cos(rotation/2.0f);
715 rot.x=0.0f;
716 rot.y=0.0f;
717 rot.z=sin(rotation/2.0f);
718
719 sca_rot.w=cos(scalingrotation/2.0f);
720 sca_rot.x=0.0f;
721 sca_rot.y=0.0f;
722 sca_rot.z=sin(scalingrotation/2.0f);
723
724 D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
725
726 return pout;
727 }
728
729 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
730 {
731 D3DXMatrixIdentity(pout);
732 pout->u.m[3][0] = x;
733 pout->u.m[3][1] = y;
734 pout->u.m[3][2] = z;
735 return pout;
736 }
737
738 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
739 {
740 CONST D3DXMATRIX m = *pm;
741 int i,j;
742
743 for (i=0; i<4; i++)
744 for (j=0; j<4; j++) pout->u.m[i][j] = m.u.m[j][i];
745
746 return pout;
747 }
748
749 /*_________________D3DXMatrixStack____________________*/
750
751 static const unsigned int INITIAL_STACK_SIZE = 32;
752
753 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, LPD3DXMATRIXSTACK* ppstack)
754 {
755 ID3DXMatrixStackImpl* object;
756
757 TRACE("flags %#x, ppstack %p\n", flags, ppstack);
758
759 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ID3DXMatrixStackImpl));
760 if ( object == NULL )
761 {
762 *ppstack = NULL;
763 return E_OUTOFMEMORY;
764 }
765 object->lpVtbl = &ID3DXMatrixStack_Vtbl;
766 object->ref = 1;
767
768 object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(D3DXMATRIX));
769 if (!object->stack)
770 {
771 HeapFree(GetProcessHeap(), 0, object);
772 *ppstack = NULL;
773 return E_OUTOFMEMORY;
774 }
775
776 object->current = 0;
777 object->stack_size = INITIAL_STACK_SIZE;
778 D3DXMatrixIdentity(&object->stack[0]);
779
780 TRACE("Created matrix stack %p\n", object);
781
782 *ppstack = (LPD3DXMATRIXSTACK)object;
783 return D3D_OK;
784 }
785
786 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **ppobj)
787 {
788 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
789 if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_ID3DXMatrixStack))
790 {
791 ID3DXMatrixStack_AddRef(iface);
792 *ppobj = This;
793 return S_OK;
794 }
795 *ppobj = NULL;
796 ERR("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
797 return E_NOINTERFACE;
798 }
799
800 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
801 {
802 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
803 ULONG ref = InterlockedIncrement(&This->ref);
804 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
805 return ref;
806 }
807
808 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack* iface)
809 {
810 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
811 ULONG ref = InterlockedDecrement(&This->ref);
812 if (!ref)
813 {
814 HeapFree(GetProcessHeap(), 0, This->stack);
815 HeapFree(GetProcessHeap(), 0, This);
816 }
817 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
818 return ref;
819 }
820
821 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
822 {
823 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
824
825 TRACE("iface %p\n", iface);
826
827 return &This->stack[This->current];
828 }
829
830 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
831 {
832 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
833
834 TRACE("iface %p\n", iface);
835
836 D3DXMatrixIdentity(&This->stack[This->current]);
837
838 return D3D_OK;
839 }
840
841 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
842 {
843 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
844
845 TRACE("iface %p\n", iface);
846
847 This->stack[This->current] = *pm;
848
849 return D3D_OK;
850 }
851
852 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
853 {
854 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
855
856 TRACE("iface %p\n", iface);
857
858 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], pm);
859
860 return D3D_OK;
861 }
862
863 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
864 {
865 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
866
867 TRACE("iface %p\n", iface);
868
869 D3DXMatrixMultiply(&This->stack[This->current], pm, &This->stack[This->current]);
870
871 return D3D_OK;
872 }
873
874 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
875 {
876 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
877
878 TRACE("iface %p\n", iface);
879
880 /* Popping the last element on the stack returns D3D_OK, but does nothing. */
881 if (!This->current) return D3D_OK;
882
883 if (This->current <= This->stack_size / 4 && This->stack_size >= INITIAL_STACK_SIZE * 2)
884 {
885 unsigned int new_size;
886 D3DXMATRIX *new_stack;
887
888 new_size = This->stack_size / 2;
889 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(D3DXMATRIX));
890 if (new_stack)
891 {
892 This->stack_size = new_size;
893 This->stack = new_stack;
894 }
895 }
896
897 --This->current;
898
899 return D3D_OK;
900 }
901
902 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
903 {
904 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
905
906 TRACE("iface %p\n", iface);
907
908 if (This->current == This->stack_size - 1)
909 {
910 unsigned int new_size;
911 D3DXMATRIX *new_stack;
912
913 if (This->stack_size > UINT_MAX / 2) return E_OUTOFMEMORY;
914
915 new_size = This->stack_size * 2;
916 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(D3DXMATRIX));
917 if (!new_stack) return E_OUTOFMEMORY;
918
919 This->stack_size = new_size;
920 This->stack = new_stack;
921 }
922
923 ++This->current;
924 This->stack[This->current] = This->stack[This->current - 1];
925
926 return D3D_OK;
927 }
928
929 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
930 {
931 D3DXMATRIX temp;
932 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
933
934 TRACE("iface %p\n", iface);
935
936 D3DXMatrixRotationAxis(&temp, pv, angle);
937 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
938
939 return D3D_OK;
940 }
941
942 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
943 {
944 D3DXMATRIX temp;
945 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
946
947 TRACE("iface %p\n", iface);
948
949 D3DXMatrixRotationAxis(&temp, pv, angle);
950 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
951
952 return D3D_OK;
953 }
954
955 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
956 {
957 D3DXMATRIX temp;
958 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
959
960 TRACE("iface %p\n", iface);
961
962 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
963 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
964
965 return D3D_OK;
966 }
967
968 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
969 {
970 D3DXMATRIX temp;
971 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
972
973 TRACE("iface %p\n", iface);
974
975 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
976 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
977
978 return D3D_OK;
979 }
980
981 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
982 {
983 D3DXMATRIX temp;
984 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
985
986 TRACE("iface %p\n", iface);
987
988 D3DXMatrixScaling(&temp, x, y, z);
989 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
990
991 return D3D_OK;
992 }
993
994 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
995 {
996 D3DXMATRIX temp;
997 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
998
999 TRACE("iface %p\n", iface);
1000
1001 D3DXMatrixScaling(&temp, x, y, z);
1002 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1003
1004 return D3D_OK;
1005 }
1006
1007 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1008 {
1009 D3DXMATRIX temp;
1010 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
1011
1012 TRACE("iface %p\n", iface);
1013
1014 D3DXMatrixTranslation(&temp, x, y, z);
1015 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1016
1017 return D3D_OK;
1018 }
1019
1020 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1021 {
1022 D3DXMATRIX temp;
1023 ID3DXMatrixStackImpl *This = (ID3DXMatrixStackImpl *)iface;
1024
1025 TRACE("iface %p\n", iface);
1026
1027 D3DXMatrixTranslation(&temp, x, y, z);
1028 D3DXMatrixMultiply(&This->stack[This->current], &temp,&This->stack[This->current]);
1029
1030 return D3D_OK;
1031 }
1032
1033 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
1034 {
1035 ID3DXMatrixStackImpl_QueryInterface,
1036 ID3DXMatrixStackImpl_AddRef,
1037 ID3DXMatrixStackImpl_Release,
1038 ID3DXMatrixStackImpl_Pop,
1039 ID3DXMatrixStackImpl_Push,
1040 ID3DXMatrixStackImpl_LoadIdentity,
1041 ID3DXMatrixStackImpl_LoadMatrix,
1042 ID3DXMatrixStackImpl_MultMatrix,
1043 ID3DXMatrixStackImpl_MultMatrixLocal,
1044 ID3DXMatrixStackImpl_RotateAxis,
1045 ID3DXMatrixStackImpl_RotateAxisLocal,
1046 ID3DXMatrixStackImpl_RotateYawPitchRoll,
1047 ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
1048 ID3DXMatrixStackImpl_Scale,
1049 ID3DXMatrixStackImpl_ScaleLocal,
1050 ID3DXMatrixStackImpl_Translate,
1051 ID3DXMatrixStackImpl_TranslateLocal,
1052 ID3DXMatrixStackImpl_GetTop
1053 };
1054
1055 /*_________________D3DXPLANE________________*/
1056
1057 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, CONST D3DXVECTOR3 *pvpoint, CONST D3DXVECTOR3 *pvnormal)
1058 {
1059 pout->a = pvnormal->x;
1060 pout->b = pvnormal->y;
1061 pout->c = pvnormal->z;
1062 pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
1063 return pout;
1064 }
1065
1066 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3)
1067 {
1068 D3DXVECTOR3 edge1, edge2, normal, Nnormal;
1069
1070 edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
1071 edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
1072 D3DXVec3Subtract(&edge1, pv2, pv1);
1073 D3DXVec3Subtract(&edge2, pv3, pv1);
1074 D3DXVec3Cross(&normal, &edge1, &edge2);
1075 D3DXVec3Normalize(&Nnormal, &normal);
1076 D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
1077 return pout;
1078 }
1079
1080 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, CONST D3DXPLANE *pp, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
1081 {
1082 D3DXVECTOR3 direction, normal;
1083 FLOAT dot, temp;
1084
1085 normal.x = pp->a;
1086 normal.y = pp->b;
1087 normal.z = pp->c;
1088 direction.x = pv2->x - pv1->x;
1089 direction.y = pv2->y - pv1->y;
1090 direction.z = pv2->z - pv1->z;
1091 dot = D3DXVec3Dot(&normal, &direction);
1092 if ( !dot ) return NULL;
1093 temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
1094 pout->x = pv1->x - temp * direction.x;
1095 pout->y = pv1->y - temp * direction.y;
1096 pout->z = pv1->z - temp * direction.z;
1097 return pout;
1098 }
1099
1100 D3DXPLANE* WINAPI D3DXPlaneNormalize(D3DXPLANE *pout, CONST D3DXPLANE *pp)
1101 {
1102 D3DXPLANE out;
1103 FLOAT norm;
1104
1105 norm = sqrt(pp->a * pp->a + pp->b * pp->b + pp->c * pp->c);
1106 if ( norm )
1107 {
1108 out.a = pp->a / norm;
1109 out.b = pp->b / norm;
1110 out.c = pp->c / norm;
1111 out.d = pp->d / norm;
1112 }
1113 else
1114 {
1115 out.a = 0.0f;
1116 out.b = 0.0f;
1117 out.c = 0.0f;
1118 out.d = 0.0f;
1119 }
1120 *pout = out;
1121 return pout;
1122 }
1123
1124 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, CONST D3DXPLANE *pplane, CONST D3DXMATRIX *pm)
1125 {
1126 CONST D3DXPLANE plane = *pplane;
1127 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;
1128 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;
1129 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;
1130 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;
1131 return pout;
1132 }
1133
1134 D3DXPLANE* WINAPI D3DXPlaneTransformArray(D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1135 {
1136 UINT i;
1137
1138 for (i = 0; i < elements; ++i) {
1139 D3DXPlaneTransform(
1140 (D3DXPLANE*)((char*)out + outstride * i),
1141 (CONST D3DXPLANE*)((const char*)in + instride * i),
1142 matrix);
1143 }
1144 return out;
1145 }
1146
1147 /*_________________D3DXQUATERNION________________*/
1148
1149 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
1150 {
1151 D3DXQUATERNION temp1, temp2;
1152 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
1153 return pout;
1154 }
1155
1156 D3DXQUATERNION* WINAPI D3DXQuaternionExp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1157 {
1158 FLOAT norm;
1159
1160 norm = sqrt(pq->x * pq->x + pq->y * pq->y + pq->z * pq->z);
1161 if (norm )
1162 {
1163 pout->x = sin(norm) * pq->x / norm;
1164 pout->y = sin(norm) * pq->y / norm;
1165 pout->z = sin(norm) * pq->z / norm;
1166 pout->w = cos(norm);
1167 }
1168 else
1169 {
1170 pout->x = 0.0f;
1171 pout->y = 0.0f;
1172 pout->z = 0.0f;
1173 pout->w = 1.0f;
1174 }
1175 return pout;
1176 }
1177
1178 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1179 {
1180 D3DXQUATERNION out;
1181 FLOAT norm;
1182
1183 norm = D3DXQuaternionLengthSq(pq);
1184
1185 out.x = -pq->x / norm;
1186 out.y = -pq->y / norm;
1187 out.z = -pq->z / norm;
1188 out.w = pq->w / norm;
1189
1190 *pout =out;
1191 return pout;
1192 }
1193
1194 D3DXQUATERNION* WINAPI D3DXQuaternionLn(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1195 {
1196 FLOAT norm, normvec, theta;
1197
1198 norm = D3DXQuaternionLengthSq(pq);
1199 if ( norm > 1.0001f )
1200 {
1201 pout->x = pq->x;
1202 pout->y = pq->y;
1203 pout->z = pq->z;
1204 pout->w = 0.0f;
1205 }
1206 else if( norm > 0.99999f)
1207 {
1208 normvec = sqrt( pq->x * pq->x + pq->y * pq->y + pq->z * pq->z );
1209 theta = atan2(normvec, pq->w) / normvec;
1210 pout->x = theta * pq->x;
1211 pout->y = theta * pq->y;
1212 pout->z = theta * pq->z;
1213 pout->w = 0.0f;
1214 }
1215 else
1216 {
1217 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);
1218 }
1219 return pout;
1220 }
1221
1222 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
1223 {
1224 D3DXQUATERNION out;
1225 out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
1226 out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
1227 out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
1228 out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
1229 *pout = out;
1230 return pout;
1231 }
1232
1233 D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1234 {
1235 D3DXQUATERNION out;
1236 FLOAT norm;
1237
1238 norm = D3DXQuaternionLength(pq);
1239
1240 out.x = pq->x / norm;
1241 out.y = pq->y / norm;
1242 out.z = pq->z / norm;
1243 out.w = pq->w / norm;
1244
1245 *pout=out;
1246
1247 return pout;
1248 }
1249
1250 D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
1251 {
1252 D3DXVECTOR3 temp;
1253
1254 D3DXVec3Normalize(&temp, pv);
1255 pout->x = sin( angle / 2.0f ) * temp.x;
1256 pout->y = sin( angle / 2.0f ) * temp.y;
1257 pout->z = sin( angle / 2.0f ) * temp.z;
1258 pout->w = cos( angle / 2.0f );
1259 return pout;
1260 }
1261
1262 D3DXQUATERNION* WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *pout, CONST D3DXMATRIX *pm)
1263 {
1264 int i, maxi;
1265 FLOAT maxdiag, S, trace;
1266
1267 trace = pm->u.m[0][0] + pm->u.m[1][1] + pm->u.m[2][2] + 1.0f;
1268 if ( trace > 1.0f)
1269 {
1270 pout->x = ( pm->u.m[1][2] - pm->u.m[2][1] ) / ( 2.0f * sqrt(trace) );
1271 pout->y = ( pm->u.m[2][0] - pm->u.m[0][2] ) / ( 2.0f * sqrt(trace) );
1272 pout->z = ( pm->u.m[0][1] - pm->u.m[1][0] ) / ( 2.0f * sqrt(trace) );
1273 pout->w = sqrt(trace) / 2.0f;
1274 return pout;
1275 }
1276 maxi = 0;
1277 maxdiag = pm->u.m[0][0];
1278 for (i=1; i<3; i++)
1279 {
1280 if ( pm->u.m[i][i] > maxdiag )
1281 {
1282 maxi = i;
1283 maxdiag = pm->u.m[i][i];
1284 }
1285 }
1286 switch( maxi )
1287 {
1288 case 0:
1289 S = 2.0f * sqrt(1.0f + pm->u.m[0][0] - pm->u.m[1][1] - pm->u.m[2][2]);
1290 pout->x = 0.25f * S;
1291 pout->y = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1292 pout->z = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1293 pout->w = ( pm->u.m[1][2] - pm->u.m[2][1] ) / S;
1294 break;
1295 case 1:
1296 S = 2.0f * sqrt(1.0f + pm->u.m[1][1] - pm->u.m[0][0] - pm->u.m[2][2]);
1297 pout->x = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1298 pout->y = 0.25f * S;
1299 pout->z = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1300 pout->w = ( pm->u.m[2][0] - pm->u.m[0][2] ) / S;
1301 break;
1302 case 2:
1303 S = 2.0f * sqrt(1.0f + pm->u.m[2][2] - pm->u.m[0][0] - pm->u.m[1][1]);
1304 pout->x = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1305 pout->y = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1306 pout->z = 0.25f * S;
1307 pout->w = ( pm->u.m[0][1] - pm->u.m[1][0] ) / S;
1308 break;
1309 }
1310 return pout;
1311 }
1312
1313 D3DXQUATERNION* WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
1314 {
1315 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);
1316 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);
1317 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);
1318 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);
1319 return pout;
1320 }
1321
1322 D3DXQUATERNION* WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, FLOAT t)
1323 {
1324 FLOAT dot, epsilon, temp, theta, u;
1325
1326 epsilon = 1.0f;
1327 temp = 1.0f - t;
1328 u = t;
1329 dot = D3DXQuaternionDot(pq1, pq2);
1330 if ( dot < 0.0f )
1331 {
1332 epsilon = -1.0f;
1333 dot = -dot;
1334 }
1335 if( 1.0f - dot > 0.001f )
1336 {
1337 theta = acos(dot);
1338 temp = sin(theta * temp) / sin(theta);
1339 u = sin(theta * u) / sin(theta);
1340 }
1341 pout->x = temp * pq1->x + epsilon * u * pq2->x;
1342 pout->y = temp * pq1->y + epsilon * u * pq2->y;
1343 pout->z = temp * pq1->z + epsilon * u * pq2->z;
1344 pout->w = temp * pq1->w + epsilon * u * pq2->w;
1345 return pout;
1346 }
1347
1348 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, CONST D3DXQUATERNION *pq4, FLOAT t)
1349 {
1350 D3DXQUATERNION temp1, temp2;
1351
1352 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1353 return pout;
1354 }
1355
1356 void WINAPI D3DXQuaternionToAxisAngle(CONST D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1357 {
1358 paxis->x = pq->x;
1359 paxis->y = pq->y;
1360 paxis->z = pq->z;
1361 *pangle = 2.0f * acos(pq->w);
1362 }
1363
1364 /*_________________D3DXVec2_____________________*/
1365
1366 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1367 {
1368 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1369 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1370 return pout;
1371 }
1372
1373 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv0, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT s)
1374 {
1375 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);
1376 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);
1377 return pout;
1378 }
1379
1380 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pt1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pt2, FLOAT s)
1381 {
1382 FLOAT h1, h2, h3, h4;
1383
1384 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1385 h2 = s * s * s - 2.0f * s * s + s;
1386 h3 = -2.0f * s * s * s + 3.0f * s * s;
1387 h4 = s * s * s - s * s;
1388
1389 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1390 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1391 return pout;
1392 }
1393
1394 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
1395 {
1396 D3DXVECTOR2 out;
1397 FLOAT norm;
1398
1399 norm = D3DXVec2Length(pv);
1400 if ( !norm )
1401 {
1402 out.x = 0.0f;
1403 out.y = 0.0f;
1404 }
1405 else
1406 {
1407 out.x = pv->x / norm;
1408 out.y = pv->y / norm;
1409 }
1410 *pout=out;
1411 return pout;
1412 }
1413
1414 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1415 {
1416 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
1417 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
1418 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
1419 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1420 return pout;
1421 }
1422
1423 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1424 {
1425 UINT i;
1426
1427 for (i = 0; i < elements; ++i) {
1428 D3DXVec2Transform(
1429 (D3DXVECTOR4*)((char*)out + outstride * i),
1430 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1431 matrix);
1432 }
1433 return out;
1434 }
1435
1436 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1437 {
1438 D3DXVECTOR2 v;
1439 FLOAT norm;
1440
1441 v = *pv;
1442 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1443
1444 pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1445 pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1446
1447 return pout;
1448 }
1449
1450 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1451 {
1452 UINT i;
1453
1454 for (i = 0; i < elements; ++i) {
1455 D3DXVec2TransformCoord(
1456 (D3DXVECTOR2*)((char*)out + outstride * i),
1457 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1458 matrix);
1459 }
1460 return out;
1461 }
1462
1463 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1464 {
1465 CONST D3DXVECTOR2 v = *pv;
1466 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1467 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1468 return pout;
1469 }
1470
1471 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride, CONST D3DXMATRIX *matrix, UINT elements)
1472 {
1473 UINT i;
1474
1475 for (i = 0; i < elements; ++i) {
1476 D3DXVec2TransformNormal(
1477 (D3DXVECTOR2*)((char*)out + outstride * i),
1478 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1479 matrix);
1480 }
1481 return out;
1482 }
1483
1484 /*_________________D3DXVec3_____________________*/
1485
1486 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1487 {
1488 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1489 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1490 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1491 return pout;
1492 }
1493
1494 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
1495 {
1496 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);
1497 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);
1498 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);
1499 return pout;
1500 }
1501
1502 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
1503 {
1504 FLOAT h1, h2, h3, h4;
1505
1506 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1507 h2 = s * s * s - 2.0f * s * s + s;
1508 h3 = -2.0f * s * s * s + 3.0f * s * s;
1509 h4 = s * s * s - s * s;
1510
1511 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1512 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1513 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1514 return pout;
1515 }
1516
1517 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
1518 {
1519 D3DXVECTOR3 out;
1520 FLOAT norm;
1521
1522 norm = D3DXVec3Length(pv);
1523 if ( !norm )
1524 {
1525 out.x = 0.0f;
1526 out.y = 0.0f;
1527 out.z = 0.0f;
1528 }
1529 else
1530 {
1531 out.x = pv->x / norm;
1532 out.y = pv->y / norm;
1533 out.z = pv->z / norm;
1534 }
1535 *pout = out;
1536 return pout;
1537 }
1538
1539 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1540 {
1541 D3DXMATRIX m;
1542 D3DXVECTOR3 out;
1543
1544 D3DXMatrixMultiply(&m, pworld, pview);
1545 D3DXMatrixMultiply(&m, &m, pprojection);
1546 D3DXVec3TransformCoord(&out, pv, &m);
1547 out.x = pviewport->X + ( 1.0f + out.x ) * pviewport->Width / 2.0f;
1548 out.y = pviewport->Y + ( 1.0f - out.y ) * pviewport->Height / 2.0f;
1549 out.z = pviewport->MinZ + out.z * ( pviewport->MaxZ - pviewport->MinZ );
1550 *pout = out;
1551 return pout;
1552 }
1553
1554 D3DXVECTOR3* WINAPI D3DXVec3ProjectArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DVIEWPORT9* viewport, CONST D3DXMATRIX* projection, CONST D3DXMATRIX* view, CONST D3DXMATRIX* world, UINT elements)
1555 {
1556 UINT i;
1557
1558 for (i = 0; i < elements; ++i) {
1559 D3DXVec3Project(
1560 (D3DXVECTOR3*)((char*)out + outstride * i),
1561 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1562 viewport, projection, view, world);
1563 }
1564 return out;
1565 }
1566
1567 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1568 {
1569 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];
1570 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];
1571 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];
1572 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];
1573 return pout;
1574 }
1575
1576 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1577 {
1578 UINT i;
1579
1580 for (i = 0; i < elements; ++i) {
1581 D3DXVec3Transform(
1582 (D3DXVECTOR4*)((char*)out + outstride * i),
1583 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1584 matrix);
1585 }
1586 return out;
1587 }
1588
1589 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1590 {
1591 D3DXVECTOR3 out;
1592 FLOAT norm;
1593
1594 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];
1595
1596 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]) / norm;
1597 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]) / norm;
1598 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]) / norm;
1599
1600 *pout = out;
1601
1602 return pout;
1603 }
1604
1605 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1606 {
1607 UINT i;
1608
1609 for (i = 0; i < elements; ++i) {
1610 D3DXVec3TransformCoord(
1611 (D3DXVECTOR3*)((char*)out + outstride * i),
1612 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1613 matrix);
1614 }
1615 return out;
1616 }
1617
1618 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1619 {
1620 CONST D3DXVECTOR3 v = *pv;
1621 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1622 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1623 pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1624 return pout;
1625
1626 }
1627
1628 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1629 {
1630 UINT i;
1631
1632 for (i = 0; i < elements; ++i) {
1633 D3DXVec3TransformNormal(
1634 (D3DXVECTOR3*)((char*)out + outstride * i),
1635 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1636 matrix);
1637 }
1638 return out;
1639 }
1640
1641 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1642 {
1643 D3DXMATRIX m;
1644 D3DXVECTOR3 out;
1645
1646 D3DXMatrixMultiply(&m, pworld, pview);
1647 D3DXMatrixMultiply(&m, &m, pprojection);
1648 D3DXMatrixInverse(&m, NULL, &m);
1649 out.x = 2.0f * ( pv->x - pviewport->X ) / pviewport->Width - 1.0f;
1650 out.y = 1.0f - 2.0f * ( pv->y - pviewport->Y ) / pviewport->Height;
1651 out.z = ( pv->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
1652 D3DXVec3TransformCoord(&out, &out, &m);
1653 *pout = out;
1654 return pout;
1655 }
1656
1657 D3DXVECTOR3* WINAPI D3DXVec3UnprojectArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DVIEWPORT9* viewport, CONST D3DXMATRIX* projection, CONST D3DXMATRIX* view, CONST D3DXMATRIX* world, UINT elements)
1658 {
1659 UINT i;
1660
1661 for (i = 0; i < elements; ++i) {
1662 D3DXVec3Unproject(
1663 (D3DXVECTOR3*)((char*)out + outstride * i),
1664 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1665 viewport, projection, view, world);
1666 }
1667 return out;
1668 }
1669
1670 /*_________________D3DXVec4_____________________*/
1671
1672 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
1673 {
1674 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1675 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1676 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1677 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
1678 return pout;
1679 }
1680
1681 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv0, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT s)
1682 {
1683 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);
1684 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);
1685 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);
1686 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);
1687 return pout;
1688 }
1689
1690 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
1691 {
1692 D3DXVECTOR4 out;
1693 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);
1694 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));
1695 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);
1696 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));
1697 *pout = out;
1698 return pout;
1699 }
1700
1701 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pt1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pt2, FLOAT s)
1702 {
1703 FLOAT h1, h2, h3, h4;
1704
1705 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1706 h2 = s * s * s - 2.0f * s * s + s;
1707 h3 = -2.0f * s * s * s + 3.0f * s * s;
1708 h4 = s * s * s - s * s;
1709
1710 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1711 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1712 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1713 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
1714 return pout;
1715 }
1716
1717 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
1718 {
1719 D3DXVECTOR4 out;
1720 FLOAT norm;
1721
1722 norm = D3DXVec4Length(pv);
1723
1724 out.x = pv->x / norm;
1725 out.y = pv->y / norm;
1726 out.z = pv->z / norm;
1727 out.w = pv->w / norm;
1728
1729 *pout = out;
1730 return pout;
1731 }
1732
1733 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
1734 {
1735 D3DXVECTOR4 out;
1736 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;
1737 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;
1738 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;
1739 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;
1740 *pout = out;
1741 return pout;
1742 }
1743
1744 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1745 {
1746 UINT i;
1747
1748 for (i = 0; i < elements; ++i) {
1749 D3DXVec4Transform(
1750 (D3DXVECTOR4*)((char*)out + outstride * i),
1751 (CONST D3DXVECTOR4*)((const char*)in + instride * i),
1752 matrix);
1753 }
1754 return out;
1755 }