d3dx9: Use float functions in D3DXQuaternionRotationYawPitchRoll().
[wine.git] / dlls / d3dx9_36 / math.c
blob3f0e8e4a321d9e1a6de13417c7af49e1428b97a4
1 /*
2 * Mathematical operations specific to D3DX9.
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
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.
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.
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
25 #define NONAMELESSUNION
27 #include "config.h"
28 #include "wine/port.h"
30 #include "windef.h"
31 #include "wingdi.h"
32 #include "d3dx9_36_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
38 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl;
40 struct ID3DXMatrixStackImpl
42 ID3DXMatrixStack ID3DXMatrixStack_iface;
43 LONG ref;
45 unsigned int current;
46 unsigned int stack_size;
47 D3DXMATRIX *stack;
51 /*_________________D3DXColor____________________*/
53 D3DXCOLOR* WINAPI D3DXColorAdjustContrast(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
55 TRACE("(%p, %p, %f)\n", pout, pc, s);
57 pout->r = 0.5f + s * (pc->r - 0.5f);
58 pout->g = 0.5f + s * (pc->g - 0.5f);
59 pout->b = 0.5f + s * (pc->b - 0.5f);
60 pout->a = pc->a;
61 return pout;
64 D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
66 FLOAT grey;
68 TRACE("(%p, %p, %f)\n", pout, pc, s);
70 grey = pc->r * 0.2125f + pc->g * 0.7154f + pc->b * 0.0721f;
71 pout->r = grey + s * (pc->r - grey);
72 pout->g = grey + s * (pc->g - grey);
73 pout->b = grey + s * (pc->b - grey);
74 pout->a = pc->a;
75 return pout;
78 /*_________________Misc__________________________*/
80 FLOAT WINAPI D3DXFresnelTerm(FLOAT costheta, FLOAT refractionindex)
82 FLOAT a, d, g, result;
84 TRACE("(%f, %f)\n", costheta, refractionindex);
86 g = sqrt(refractionindex * refractionindex + costheta * costheta - 1.0f);
87 a = g + costheta;
88 d = g - costheta;
89 result = ( costheta * a - 1.0f ) * ( costheta * a - 1.0f ) / ( ( costheta * d + 1.0f ) * ( costheta * d + 1.0f ) ) + 1.0f;
90 result = result * 0.5f * d * d / ( a * a );
91 return result;
94 /*_________________D3DXMatrix____________________*/
96 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR3 *rotationcenter, CONST D3DXQUATERNION *rotation, CONST D3DXVECTOR3 *translation)
98 D3DXMATRIX m1, m2, m3, m4, m5;
100 TRACE("(%p, %f, %p, %p, %p)\n", pout, scaling, rotationcenter, rotation, translation);
102 D3DXMatrixScaling(&m1, scaling, scaling, scaling);
104 if ( !rotationcenter )
106 D3DXMatrixIdentity(&m2);
107 D3DXMatrixIdentity(&m4);
109 else
111 D3DXMatrixTranslation(&m2, -rotationcenter->x, -rotationcenter->y, -rotationcenter->z);
112 D3DXMatrixTranslation(&m4, rotationcenter->x, rotationcenter->y, rotationcenter->z);
115 if ( !rotation ) D3DXMatrixIdentity(&m3);
116 else D3DXMatrixRotationQuaternion(&m3, rotation);
118 if ( !translation ) D3DXMatrixIdentity(&m5);
119 else D3DXMatrixTranslation(&m5, translation->x, translation->y, translation->z);
121 D3DXMatrixMultiply(&m1, &m1, &m2);
122 D3DXMatrixMultiply(&m1, &m1, &m3);
123 D3DXMatrixMultiply(&m1, &m1, &m4);
124 D3DXMatrixMultiply(pout, &m1, &m5);
125 return pout;
128 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation2D(D3DXMATRIX *pout, FLOAT scaling, CONST D3DXVECTOR2 *protationcenter, FLOAT rotation, CONST D3DXVECTOR2 *ptranslation)
130 D3DXMATRIX m1, m2, m3, m4, m5;
131 D3DXQUATERNION rot;
132 D3DXVECTOR3 rot_center, trans;
134 TRACE("(%p, %f, %p, %f, %p)\n", pout, scaling, protationcenter, rotation, ptranslation);
136 rot.w=cos(rotation/2.0f);
137 rot.x=0.0f;
138 rot.y=0.0f;
139 rot.z=sin(rotation/2.0f);
141 if ( protationcenter )
143 rot_center.x=protationcenter->x;
144 rot_center.y=protationcenter->y;
145 rot_center.z=0.0f;
147 else
149 rot_center.x=0.0f;
150 rot_center.y=0.0f;
151 rot_center.z=0.0f;
154 if ( ptranslation )
156 trans.x=ptranslation->x;
157 trans.y=ptranslation->y;
158 trans.z=0.0f;
160 else
162 trans.x=0.0f;
163 trans.y=0.0f;
164 trans.z=0.0f;
167 D3DXMatrixScaling(&m1, scaling, scaling, 1.0f);
168 D3DXMatrixTranslation(&m2, -rot_center.x, -rot_center.y, -rot_center.z);
169 D3DXMatrixTranslation(&m4, rot_center.x, rot_center.y, rot_center.z);
170 D3DXMatrixRotationQuaternion(&m3, &rot);
171 D3DXMatrixTranslation(&m5, trans.x, trans.y, trans.z);
173 D3DXMatrixMultiply(&m1, &m1, &m2);
174 D3DXMatrixMultiply(&m1, &m1, &m3);
175 D3DXMatrixMultiply(&m1, &m1, &m4);
176 D3DXMatrixMultiply(pout, &m1, &m5);
178 return pout;
181 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, CONST D3DXMATRIX *pm)
183 D3DXMATRIX normalized;
184 D3DXVECTOR3 vec;
186 TRACE("(%p, %p, %p, %p)\n", poutscale, poutrotation, pouttranslation, pm);
188 /*Compute the scaling part.*/
189 vec.x=pm->u.m[0][0];
190 vec.y=pm->u.m[0][1];
191 vec.z=pm->u.m[0][2];
192 poutscale->x=D3DXVec3Length(&vec);
194 vec.x=pm->u.m[1][0];
195 vec.y=pm->u.m[1][1];
196 vec.z=pm->u.m[1][2];
197 poutscale->y=D3DXVec3Length(&vec);
199 vec.x=pm->u.m[2][0];
200 vec.y=pm->u.m[2][1];
201 vec.z=pm->u.m[2][2];
202 poutscale->z=D3DXVec3Length(&vec);
204 /*Compute the translation part.*/
205 pouttranslation->x=pm->u.m[3][0];
206 pouttranslation->y=pm->u.m[3][1];
207 pouttranslation->z=pm->u.m[3][2];
209 /*Let's calculate the rotation now*/
210 if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) ) return D3DERR_INVALIDCALL;
212 normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
213 normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
214 normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
215 normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
216 normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
217 normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
218 normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
219 normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
220 normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
222 D3DXQuaternionRotationMatrix(poutrotation,&normalized);
223 return S_OK;
226 FLOAT WINAPI D3DXMatrixDeterminant(CONST D3DXMATRIX *pm)
228 D3DXVECTOR4 minor, v1, v2, v3;
229 FLOAT det;
231 TRACE("(%p)\n", pm);
233 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];
234 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];
235 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];
236 D3DXVec4Cross(&minor, &v1, &v2, &v3);
237 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);
238 return det;
241 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, CONST D3DXMATRIX *pm)
243 int a, i, j;
244 D3DXMATRIX out;
245 D3DXVECTOR4 v, vec[3];
246 FLOAT det;
248 TRACE("(%p, %p, %p)\n", pout, pdeterminant, pm);
250 det = D3DXMatrixDeterminant(pm);
251 if ( !det ) return NULL;
252 if ( pdeterminant ) *pdeterminant = det;
253 for (i=0; i<4; i++)
255 for (j=0; j<4; j++)
257 if (j != i )
259 a = j;
260 if ( j > i ) a = a-1;
261 vec[a].x = pm->u.m[j][0];
262 vec[a].y = pm->u.m[j][1];
263 vec[a].z = pm->u.m[j][2];
264 vec[a].w = pm->u.m[j][3];
267 D3DXVec4Cross(&v, &vec[0], &vec[1], &vec[2]);
268 out.u.m[0][i] = pow(-1.0f, i) * v.x / det;
269 out.u.m[1][i] = pow(-1.0f, i) * v.y / det;
270 out.u.m[2][i] = pow(-1.0f, i) * v.z / det;
271 out.u.m[3][i] = pow(-1.0f, i) * v.w / det;
274 *pout = out;
275 return pout;
278 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
280 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
282 TRACE("(%p, %p, %p, %p)\n", pout, peye, pat, pup);
284 D3DXVec3Subtract(&vec2, pat, peye);
285 D3DXVec3Normalize(&vec, &vec2);
286 D3DXVec3Cross(&right, pup, &vec);
287 D3DXVec3Cross(&up, &vec, &right);
288 D3DXVec3Normalize(&rightn, &right);
289 D3DXVec3Normalize(&upn, &up);
290 pout->u.m[0][0] = rightn.x;
291 pout->u.m[1][0] = rightn.y;
292 pout->u.m[2][0] = rightn.z;
293 pout->u.m[3][0] = -D3DXVec3Dot(&rightn,peye);
294 pout->u.m[0][1] = upn.x;
295 pout->u.m[1][1] = upn.y;
296 pout->u.m[2][1] = upn.z;
297 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
298 pout->u.m[0][2] = vec.x;
299 pout->u.m[1][2] = vec.y;
300 pout->u.m[2][2] = vec.z;
301 pout->u.m[3][2] = -D3DXVec3Dot(&vec, peye);
302 pout->u.m[0][3] = 0.0f;
303 pout->u.m[1][3] = 0.0f;
304 pout->u.m[2][3] = 0.0f;
305 pout->u.m[3][3] = 1.0f;
306 return pout;
309 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
311 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
313 TRACE("(%p, %p, %p, %p)\n", pout, peye, pat, pup);
315 D3DXVec3Subtract(&vec2, pat, peye);
316 D3DXVec3Normalize(&vec, &vec2);
317 D3DXVec3Cross(&right, pup, &vec);
318 D3DXVec3Cross(&up, &vec, &right);
319 D3DXVec3Normalize(&rightn, &right);
320 D3DXVec3Normalize(&upn, &up);
321 pout->u.m[0][0] = -rightn.x;
322 pout->u.m[1][0] = -rightn.y;
323 pout->u.m[2][0] = -rightn.z;
324 pout->u.m[3][0] = D3DXVec3Dot(&rightn,peye);
325 pout->u.m[0][1] = upn.x;
326 pout->u.m[1][1] = upn.y;
327 pout->u.m[2][1] = upn.z;
328 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
329 pout->u.m[0][2] = -vec.x;
330 pout->u.m[1][2] = -vec.y;
331 pout->u.m[2][2] = -vec.z;
332 pout->u.m[3][2] = D3DXVec3Dot(&vec, peye);
333 pout->u.m[0][3] = 0.0f;
334 pout->u.m[1][3] = 0.0f;
335 pout->u.m[2][3] = 0.0f;
336 pout->u.m[3][3] = 1.0f;
337 return pout;
340 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
342 D3DXMATRIX out;
343 int i,j;
345 TRACE("(%p, %p, %p)\n", pout, pm1, pm2);
347 for (i=0; i<4; i++)
349 for (j=0; j<4; j++)
351 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];
355 *pout = out;
356 return pout;
359 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
361 TRACE("%p, %p, %p)\n", pout, pm1, pm2);
363 D3DXMatrixMultiply(pout, pm1, pm2);
364 D3DXMatrixTranspose(pout, pout);
365 return pout;
368 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
370 TRACE("(%p, %f, %f, %f, %f)\n", pout, w, h, zn, zf);
372 D3DXMatrixIdentity(pout);
373 pout->u.m[0][0] = 2.0f / w;
374 pout->u.m[1][1] = 2.0f / h;
375 pout->u.m[2][2] = 1.0f / (zf - zn);
376 pout->u.m[3][2] = zn / (zn - zf);
377 return pout;
380 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
382 TRACE("(%p, %f, %f, %f, %f, %f, %f)\n", pout, l, r, b, t, zn, zf);
384 D3DXMatrixIdentity(pout);
385 pout->u.m[0][0] = 2.0f / (r - l);
386 pout->u.m[1][1] = 2.0f / (t - b);
387 pout->u.m[2][2] = 1.0f / (zf -zn);
388 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
389 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
390 pout->u.m[3][2] = zn / (zn -zf);
391 return pout;
394 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
396 TRACE("(%p, %f, %f, %f, %f, %f, %f)\n", pout, l, r, b, t, zn, zf);
398 D3DXMatrixIdentity(pout);
399 pout->u.m[0][0] = 2.0f / (r - l);
400 pout->u.m[1][1] = 2.0f / (t - b);
401 pout->u.m[2][2] = 1.0f / (zn -zf);
402 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
403 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
404 pout->u.m[3][2] = zn / (zn -zf);
405 return pout;
408 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
410 TRACE("(%p, %f, %f, %f, %f)\n", pout, w, h, zn, zf);
412 D3DXMatrixIdentity(pout);
413 pout->u.m[0][0] = 2.0f / w;
414 pout->u.m[1][1] = 2.0f / h;
415 pout->u.m[2][2] = 1.0f / (zn - zf);
416 pout->u.m[3][2] = zn / (zn - zf);
417 return pout;
420 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
422 TRACE("(%p, %f, %f, %f, %f)\n", pout, fovy, aspect, zn, zf);
424 D3DXMatrixIdentity(pout);
425 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
426 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
427 pout->u.m[2][2] = zf / (zf - zn);
428 pout->u.m[2][3] = 1.0f;
429 pout->u.m[3][2] = (zf * zn) / (zn - zf);
430 pout->u.m[3][3] = 0.0f;
431 return pout;
434 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
436 TRACE("(%p, %f, %f, %f, %f)\n", pout, fovy, aspect, zn, zf);
438 D3DXMatrixIdentity(pout);
439 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
440 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
441 pout->u.m[2][2] = zf / (zn - zf);
442 pout->u.m[2][3] = -1.0f;
443 pout->u.m[3][2] = (zf * zn) / (zn - zf);
444 pout->u.m[3][3] = 0.0f;
445 return pout;
448 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
450 TRACE("(%p, %f, %f, %f, %f)\n", pout, w, h, zn, zf);
452 D3DXMatrixIdentity(pout);
453 pout->u.m[0][0] = 2.0f * zn / w;
454 pout->u.m[1][1] = 2.0f * zn / h;
455 pout->u.m[2][2] = zf / (zf - zn);
456 pout->u.m[3][2] = (zn * zf) / (zn - zf);
457 pout->u.m[2][3] = 1.0f;
458 pout->u.m[3][3] = 0.0f;
459 return pout;
462 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
464 TRACE("(%p, %f, %f, %f, %f, %f, %f)\n", pout, l, r, b, t, zn, zf);
466 D3DXMatrixIdentity(pout);
467 pout->u.m[0][0] = 2.0f * zn / (r - l);
468 pout->u.m[1][1] = -2.0f * zn / (b - t);
469 pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
470 pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
471 pout->u.m[2][2] = - zf / (zn - zf);
472 pout->u.m[3][2] = (zn * zf) / (zn -zf);
473 pout->u.m[2][3] = 1.0f;
474 pout->u.m[3][3] = 0.0f;
475 return pout;
478 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
480 TRACE("(%p, %f, %f, %f, %f, %f, %f)\n", pout, l, r, b, t, zn, zf);
482 D3DXMatrixIdentity(pout);
483 pout->u.m[0][0] = 2.0f * zn / (r - l);
484 pout->u.m[1][1] = -2.0f * zn / (b - t);
485 pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
486 pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
487 pout->u.m[2][2] = zf / (zn - zf);
488 pout->u.m[3][2] = (zn * zf) / (zn -zf);
489 pout->u.m[2][3] = -1.0f;
490 pout->u.m[3][3] = 0.0f;
491 return pout;
494 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
496 TRACE("(%p, %f, %f, %f, %f)\n", pout, w, h, zn, zf);
498 D3DXMatrixIdentity(pout);
499 pout->u.m[0][0] = 2.0f * zn / w;
500 pout->u.m[1][1] = 2.0f * zn / h;
501 pout->u.m[2][2] = zf / (zn - zf);
502 pout->u.m[3][2] = (zn * zf) / (zn - zf);
503 pout->u.m[2][3] = -1.0f;
504 pout->u.m[3][3] = 0.0f;
505 return pout;
508 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, CONST D3DXPLANE *pplane)
510 D3DXPLANE Nplane;
512 TRACE("(%p, %p)\n", pout, pplane);
514 D3DXPlaneNormalize(&Nplane, pplane);
515 D3DXMatrixIdentity(pout);
516 pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
517 pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
518 pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
519 pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
520 pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
521 pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
522 pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
523 pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
524 pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
525 pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
526 pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
527 pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
528 return pout;
531 D3DXMATRIX* WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
533 D3DXVECTOR3 v;
535 TRACE("(%p, %p, %f)\n", pout, pv, angle);
537 D3DXVec3Normalize(&v,pv);
538 D3DXMatrixIdentity(pout);
539 pout->u.m[0][0] = (1.0f - cos(angle)) * v.x * v.x + cos(angle);
540 pout->u.m[1][0] = (1.0f - cos(angle)) * v.x * v.y - sin(angle) * v.z;
541 pout->u.m[2][0] = (1.0f - cos(angle)) * v.x * v.z + sin(angle) * v.y;
542 pout->u.m[0][1] = (1.0f - cos(angle)) * v.y * v.x + sin(angle) * v.z;
543 pout->u.m[1][1] = (1.0f - cos(angle)) * v.y * v.y + cos(angle);
544 pout->u.m[2][1] = (1.0f - cos(angle)) * v.y * v.z - sin(angle) * v.x;
545 pout->u.m[0][2] = (1.0f - cos(angle)) * v.z * v.x - sin(angle) * v.y;
546 pout->u.m[1][2] = (1.0f - cos(angle)) * v.z * v.y + sin(angle) * v.x;
547 pout->u.m[2][2] = (1.0f - cos(angle)) * v.z * v.z + cos(angle);
548 return pout;
551 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, CONST D3DXQUATERNION *pq)
553 TRACE("(%p, %p)\n", pout, pq);
555 D3DXMatrixIdentity(pout);
556 pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
557 pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
558 pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
559 pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
560 pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
561 pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
562 pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
563 pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
564 pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
565 return pout;
568 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
570 TRACE("(%p, %f)\n", pout, angle);
572 D3DXMatrixIdentity(pout);
573 pout->u.m[1][1] = cos(angle);
574 pout->u.m[2][2] = cos(angle);
575 pout->u.m[1][2] = sin(angle);
576 pout->u.m[2][1] = -sin(angle);
577 return pout;
580 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
582 TRACE("(%p, %f)\n", pout, angle);
584 D3DXMatrixIdentity(pout);
585 pout->u.m[0][0] = cos(angle);
586 pout->u.m[2][2] = cos(angle);
587 pout->u.m[0][2] = -sin(angle);
588 pout->u.m[2][0] = sin(angle);
589 return pout;
592 D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
594 D3DXMATRIX m;
596 TRACE("(%p, %f, %f, %f)\n", pout, yaw, pitch, roll);
598 D3DXMatrixIdentity(pout);
599 D3DXMatrixRotationZ(&m, roll);
600 D3DXMatrixMultiply(pout, pout, &m);
601 D3DXMatrixRotationX(&m, pitch);
602 D3DXMatrixMultiply(pout, pout, &m);
603 D3DXMatrixRotationY(&m, yaw);
604 D3DXMatrixMultiply(pout, pout, &m);
605 return pout;
608 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
610 TRACE("(%p, %f)\n", pout, angle);
612 D3DXMatrixIdentity(pout);
613 pout->u.m[0][0] = cos(angle);
614 pout->u.m[1][1] = cos(angle);
615 pout->u.m[0][1] = sin(angle);
616 pout->u.m[1][0] = -sin(angle);
617 return pout;
620 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
622 TRACE("(%p, %f, %f, %f)\n", pout, sx, sy, sz);
624 D3DXMatrixIdentity(pout);
625 pout->u.m[0][0] = sx;
626 pout->u.m[1][1] = sy;
627 pout->u.m[2][2] = sz;
628 return pout;
631 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, CONST D3DXVECTOR4 *plight, CONST D3DXPLANE *pplane)
633 D3DXPLANE Nplane;
634 FLOAT dot;
636 TRACE("(%p, %p, %p)\n", pout, plight, pplane);
638 D3DXPlaneNormalize(&Nplane, pplane);
639 dot = D3DXPlaneDot(&Nplane, plight);
640 pout->u.m[0][0] = dot - Nplane.a * plight->x;
641 pout->u.m[0][1] = -Nplane.a * plight->y;
642 pout->u.m[0][2] = -Nplane.a * plight->z;
643 pout->u.m[0][3] = -Nplane.a * plight->w;
644 pout->u.m[1][0] = -Nplane.b * plight->x;
645 pout->u.m[1][1] = dot - Nplane.b * plight->y;
646 pout->u.m[1][2] = -Nplane.b * plight->z;
647 pout->u.m[1][3] = -Nplane.b * plight->w;
648 pout->u.m[2][0] = -Nplane.c * plight->x;
649 pout->u.m[2][1] = -Nplane.c * plight->y;
650 pout->u.m[2][2] = dot - Nplane.c * plight->z;
651 pout->u.m[2][3] = -Nplane.c * plight->w;
652 pout->u.m[3][0] = -Nplane.d * plight->x;
653 pout->u.m[3][1] = -Nplane.d * plight->y;
654 pout->u.m[3][2] = -Nplane.d * plight->z;
655 pout->u.m[3][3] = dot - Nplane.d * plight->w;
656 return pout;
659 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pscalingcenter, CONST D3DXQUATERNION *pscalingrotation, CONST D3DXVECTOR3 *pscaling, CONST D3DXVECTOR3 *protationcenter, CONST D3DXQUATERNION *protation, CONST D3DXVECTOR3 *ptranslation)
661 D3DXMATRIX m1, m2, m3, m4, m5, m6, m7;
662 D3DXQUATERNION prc;
663 D3DXVECTOR3 psc, pt;
665 TRACE("(%p, %p, %p, %p, %p, %p, %p)\n", pout, pscalingcenter, pscalingrotation, pscaling, protationcenter, protation, ptranslation);
667 if ( !pscalingcenter )
669 psc.x = 0.0f;
670 psc.y = 0.0f;
671 psc.z = 0.0f;
673 else
675 psc.x = pscalingcenter->x;
676 psc.y = pscalingcenter->y;
677 psc.z = pscalingcenter->z;
680 if ( !protationcenter )
682 prc.x = 0.0f;
683 prc.y = 0.0f;
684 prc.z = 0.0f;
686 else
688 prc.x = protationcenter->x;
689 prc.y = protationcenter->y;
690 prc.z = protationcenter->z;
693 if ( !ptranslation )
695 pt.x = 0.0f;
696 pt.y = 0.0f;
697 pt.z = 0.0f;
699 else
701 pt.x = ptranslation->x;
702 pt.y = ptranslation->y;
703 pt.z = ptranslation->z;
706 D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
708 if ( !pscalingrotation )
710 D3DXMatrixIdentity(&m2);
711 D3DXMatrixIdentity(&m4);
713 else
715 D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
716 D3DXMatrixInverse(&m2, NULL, &m4);
719 if ( !pscaling ) D3DXMatrixIdentity(&m3);
720 else D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
722 if ( !protation ) D3DXMatrixIdentity(&m6);
723 else D3DXMatrixRotationQuaternion(&m6, protation);
725 D3DXMatrixTranslation(&m5, psc.x - prc.x, psc.y - prc.y, psc.z - prc.z);
726 D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
727 D3DXMatrixMultiply(&m1, &m1, &m2);
728 D3DXMatrixMultiply(&m1, &m1, &m3);
729 D3DXMatrixMultiply(&m1, &m1, &m4);
730 D3DXMatrixMultiply(&m1, &m1, &m5);
731 D3DXMatrixMultiply(&m1, &m1, &m6);
732 D3DXMatrixMultiply(pout, &m1, &m7);
733 return pout;
736 D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(D3DXMATRIX *pout, CONST D3DXVECTOR2 *pscalingcenter, FLOAT scalingrotation, CONST D3DXVECTOR2 *pscaling, CONST D3DXVECTOR2 *protationcenter, FLOAT rotation, CONST D3DXVECTOR2 *ptranslation)
738 D3DXQUATERNION rot, sca_rot;
739 D3DXVECTOR3 rot_center, sca, sca_center, trans;
741 TRACE("(%p, %p, %f, %p, %p, %f, %p)\n", pout, pscalingcenter, scalingrotation, pscaling, protationcenter, rotation, ptranslation);
743 if ( pscalingcenter )
745 sca_center.x=pscalingcenter->x;
746 sca_center.y=pscalingcenter->y;
747 sca_center.z=0.0f;
749 else
751 sca_center.x=0.0f;
752 sca_center.y=0.0f;
753 sca_center.z=0.0f;
756 if ( pscaling )
758 sca.x=pscaling->x;
759 sca.y=pscaling->y;
760 sca.z=1.0f;
762 else
764 sca.x=1.0f;
765 sca.y=1.0f;
766 sca.z=1.0f;
769 if ( protationcenter )
771 rot_center.x=protationcenter->x;
772 rot_center.y=protationcenter->y;
773 rot_center.z=0.0f;
775 else
777 rot_center.x=0.0f;
778 rot_center.y=0.0f;
779 rot_center.z=0.0f;
782 if ( ptranslation )
784 trans.x=ptranslation->x;
785 trans.y=ptranslation->y;
786 trans.z=0.0f;
788 else
790 trans.x=0.0f;
791 trans.y=0.0f;
792 trans.z=0.0f;
795 rot.w=cos(rotation/2.0f);
796 rot.x=0.0f;
797 rot.y=0.0f;
798 rot.z=sin(rotation/2.0f);
800 sca_rot.w=cos(scalingrotation/2.0f);
801 sca_rot.x=0.0f;
802 sca_rot.y=0.0f;
803 sca_rot.z=sin(scalingrotation/2.0f);
805 D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
807 return pout;
810 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
812 TRACE("(%p, %f, %f, %f)\n", pout, x, y, z);
814 D3DXMatrixIdentity(pout);
815 pout->u.m[3][0] = x;
816 pout->u.m[3][1] = y;
817 pout->u.m[3][2] = z;
818 return pout;
821 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
823 CONST D3DXMATRIX m = *pm;
824 int i,j;
826 TRACE("(%p, %p)\n", pout, pm);
828 for (i=0; i<4; i++)
829 for (j=0; j<4; j++) pout->u.m[i][j] = m.u.m[j][i];
831 return pout;
834 /*_________________D3DXMatrixStack____________________*/
836 static const unsigned int INITIAL_STACK_SIZE = 32;
838 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, LPD3DXMATRIXSTACK *ppstack)
840 struct ID3DXMatrixStackImpl *object;
842 TRACE("flags %#x, ppstack %p\n", flags, ppstack);
844 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
845 if (object == NULL)
847 *ppstack = NULL;
848 return E_OUTOFMEMORY;
850 object->ID3DXMatrixStack_iface.lpVtbl = &ID3DXMatrixStack_Vtbl;
851 object->ref = 1;
853 object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(*object->stack));
854 if (!object->stack)
856 HeapFree(GetProcessHeap(), 0, object);
857 *ppstack = NULL;
858 return E_OUTOFMEMORY;
861 object->current = 0;
862 object->stack_size = INITIAL_STACK_SIZE;
863 D3DXMatrixIdentity(&object->stack[0]);
865 TRACE("Created matrix stack %p\n", object);
867 *ppstack = &object->ID3DXMatrixStack_iface;
868 return D3D_OK;
871 static inline struct ID3DXMatrixStackImpl *impl_from_ID3DXMatrixStack(ID3DXMatrixStack *iface)
873 return CONTAINING_RECORD(iface, struct ID3DXMatrixStackImpl, ID3DXMatrixStack_iface);
876 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **out)
878 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
880 if (IsEqualGUID(riid, &IID_ID3DXMatrixStack)
881 || IsEqualGUID(riid, &IID_IUnknown))
883 ID3DXMatrixStack_AddRef(iface);
884 *out = iface;
885 return S_OK;
888 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
890 *out = NULL;
891 return E_NOINTERFACE;
894 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
896 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
897 ULONG ref = InterlockedIncrement(&This->ref);
898 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
899 return ref;
902 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack *iface)
904 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
905 ULONG ref = InterlockedDecrement(&This->ref);
906 if (!ref)
908 HeapFree(GetProcessHeap(), 0, This->stack);
909 HeapFree(GetProcessHeap(), 0, This);
911 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
912 return ref;
915 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
917 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
919 TRACE("iface %p\n", iface);
921 return &This->stack[This->current];
924 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
926 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
928 TRACE("iface %p\n", iface);
930 D3DXMatrixIdentity(&This->stack[This->current]);
932 return D3D_OK;
935 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
937 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
939 TRACE("iface %p\n", iface);
941 This->stack[This->current] = *pm;
943 return D3D_OK;
946 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
948 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
950 TRACE("iface %p\n", iface);
952 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], pm);
954 return D3D_OK;
957 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
959 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
961 TRACE("iface %p\n", iface);
963 D3DXMatrixMultiply(&This->stack[This->current], pm, &This->stack[This->current]);
965 return D3D_OK;
968 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
970 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
972 TRACE("iface %p\n", iface);
974 /* Popping the last element on the stack returns D3D_OK, but does nothing. */
975 if (!This->current) return D3D_OK;
977 if (This->current <= This->stack_size / 4 && This->stack_size >= INITIAL_STACK_SIZE * 2)
979 unsigned int new_size;
980 D3DXMATRIX *new_stack;
982 new_size = This->stack_size / 2;
983 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
984 if (new_stack)
986 This->stack_size = new_size;
987 This->stack = new_stack;
991 --This->current;
993 return D3D_OK;
996 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
998 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1000 TRACE("iface %p\n", iface);
1002 if (This->current == This->stack_size - 1)
1004 unsigned int new_size;
1005 D3DXMATRIX *new_stack;
1007 if (This->stack_size > UINT_MAX / 2) return E_OUTOFMEMORY;
1009 new_size = This->stack_size * 2;
1010 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1011 if (!new_stack) return E_OUTOFMEMORY;
1013 This->stack_size = new_size;
1014 This->stack = new_stack;
1017 ++This->current;
1018 This->stack[This->current] = This->stack[This->current - 1];
1020 return D3D_OK;
1023 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
1025 D3DXMATRIX temp;
1026 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1028 TRACE("iface %p\n", iface);
1030 D3DXMatrixRotationAxis(&temp, pv, angle);
1031 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1033 return D3D_OK;
1036 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
1038 D3DXMATRIX temp;
1039 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1041 TRACE("iface %p\n", iface);
1043 D3DXMatrixRotationAxis(&temp, pv, angle);
1044 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1046 return D3D_OK;
1049 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1051 D3DXMATRIX temp;
1052 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1054 TRACE("iface %p\n", iface);
1056 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1057 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1059 return D3D_OK;
1062 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1064 D3DXMATRIX temp;
1065 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1067 TRACE("iface %p\n", iface);
1069 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1070 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1072 return D3D_OK;
1075 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1077 D3DXMATRIX temp;
1078 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1080 TRACE("iface %p\n", iface);
1082 D3DXMatrixScaling(&temp, x, y, z);
1083 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1085 return D3D_OK;
1088 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1090 D3DXMATRIX temp;
1091 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1093 TRACE("iface %p\n", iface);
1095 D3DXMatrixScaling(&temp, x, y, z);
1096 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1098 return D3D_OK;
1101 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1103 D3DXMATRIX temp;
1104 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1106 TRACE("iface %p\n", iface);
1108 D3DXMatrixTranslation(&temp, x, y, z);
1109 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1111 return D3D_OK;
1114 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1116 D3DXMATRIX temp;
1117 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1119 TRACE("iface %p\n", iface);
1121 D3DXMatrixTranslation(&temp, x, y, z);
1122 D3DXMatrixMultiply(&This->stack[This->current], &temp,&This->stack[This->current]);
1124 return D3D_OK;
1127 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
1129 ID3DXMatrixStackImpl_QueryInterface,
1130 ID3DXMatrixStackImpl_AddRef,
1131 ID3DXMatrixStackImpl_Release,
1132 ID3DXMatrixStackImpl_Pop,
1133 ID3DXMatrixStackImpl_Push,
1134 ID3DXMatrixStackImpl_LoadIdentity,
1135 ID3DXMatrixStackImpl_LoadMatrix,
1136 ID3DXMatrixStackImpl_MultMatrix,
1137 ID3DXMatrixStackImpl_MultMatrixLocal,
1138 ID3DXMatrixStackImpl_RotateAxis,
1139 ID3DXMatrixStackImpl_RotateAxisLocal,
1140 ID3DXMatrixStackImpl_RotateYawPitchRoll,
1141 ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
1142 ID3DXMatrixStackImpl_Scale,
1143 ID3DXMatrixStackImpl_ScaleLocal,
1144 ID3DXMatrixStackImpl_Translate,
1145 ID3DXMatrixStackImpl_TranslateLocal,
1146 ID3DXMatrixStackImpl_GetTop
1149 /*_________________D3DXPLANE________________*/
1151 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, CONST D3DXVECTOR3 *pvpoint, CONST D3DXVECTOR3 *pvnormal)
1153 TRACE("(%p, %p, %p)\n", pout, pvpoint, pvnormal);
1155 pout->a = pvnormal->x;
1156 pout->b = pvnormal->y;
1157 pout->c = pvnormal->z;
1158 pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
1159 return pout;
1162 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3)
1164 D3DXVECTOR3 edge1, edge2, normal, Nnormal;
1166 TRACE("(%p, %p, %p, %p)\n", pout, pv1, pv2, pv3);
1168 edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
1169 edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
1170 D3DXVec3Subtract(&edge1, pv2, pv1);
1171 D3DXVec3Subtract(&edge2, pv3, pv1);
1172 D3DXVec3Cross(&normal, &edge1, &edge2);
1173 D3DXVec3Normalize(&Nnormal, &normal);
1174 D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
1175 return pout;
1178 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, CONST D3DXPLANE *pp, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
1180 D3DXVECTOR3 direction, normal;
1181 FLOAT dot, temp;
1183 TRACE("(%p, %p, %p, %p)\n", pout, pp, pv1, pv2);
1185 normal.x = pp->a;
1186 normal.y = pp->b;
1187 normal.z = pp->c;
1188 direction.x = pv2->x - pv1->x;
1189 direction.y = pv2->y - pv1->y;
1190 direction.z = pv2->z - pv1->z;
1191 dot = D3DXVec3Dot(&normal, &direction);
1192 if ( !dot ) return NULL;
1193 temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
1194 pout->x = pv1->x - temp * direction.x;
1195 pout->y = pv1->y - temp * direction.y;
1196 pout->z = pv1->z - temp * direction.z;
1197 return pout;
1200 D3DXPLANE* WINAPI D3DXPlaneNormalize(D3DXPLANE *pout, CONST D3DXPLANE *pp)
1202 D3DXPLANE out;
1203 FLOAT norm;
1205 TRACE("(%p, %p)\n", pout, pp);
1207 norm = sqrt(pp->a * pp->a + pp->b * pp->b + pp->c * pp->c);
1208 if ( norm )
1210 out.a = pp->a / norm;
1211 out.b = pp->b / norm;
1212 out.c = pp->c / norm;
1213 out.d = pp->d / norm;
1215 else
1217 out.a = 0.0f;
1218 out.b = 0.0f;
1219 out.c = 0.0f;
1220 out.d = 0.0f;
1222 *pout = out;
1223 return pout;
1226 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, CONST D3DXPLANE *pplane, CONST D3DXMATRIX *pm)
1228 CONST D3DXPLANE plane = *pplane;
1230 TRACE("(%p, %p, %p)\n", pout, pplane, pm);
1232 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;
1233 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;
1234 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;
1235 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;
1236 return pout;
1239 D3DXPLANE* WINAPI D3DXPlaneTransformArray(D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1241 UINT i;
1243 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1245 for (i = 0; i < elements; ++i) {
1246 D3DXPlaneTransform(
1247 (D3DXPLANE*)((char*)out + outstride * i),
1248 (CONST D3DXPLANE*)((const char*)in + instride * i),
1249 matrix);
1251 return out;
1254 /*_________________D3DXQUATERNION________________*/
1256 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
1258 D3DXQUATERNION temp1, temp2;
1260 TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pq1, pq2, pq3, f, g);
1262 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
1263 return pout;
1266 D3DXQUATERNION* WINAPI D3DXQuaternionExp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1268 FLOAT norm;
1270 TRACE("(%p, %p)\n", pout, pq);
1272 norm = sqrt(pq->x * pq->x + pq->y * pq->y + pq->z * pq->z);
1273 if (norm )
1275 pout->x = sin(norm) * pq->x / norm;
1276 pout->y = sin(norm) * pq->y / norm;
1277 pout->z = sin(norm) * pq->z / norm;
1278 pout->w = cos(norm);
1280 else
1282 pout->x = 0.0f;
1283 pout->y = 0.0f;
1284 pout->z = 0.0f;
1285 pout->w = 1.0f;
1287 return pout;
1290 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1292 D3DXQUATERNION out;
1293 FLOAT norm;
1295 TRACE("(%p, %p)\n", pout, pq);
1297 norm = D3DXQuaternionLengthSq(pq);
1299 out.x = -pq->x / norm;
1300 out.y = -pq->y / norm;
1301 out.z = -pq->z / norm;
1302 out.w = pq->w / norm;
1304 *pout =out;
1305 return pout;
1308 D3DXQUATERNION* WINAPI D3DXQuaternionLn(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1310 FLOAT t;
1312 TRACE("(%p, %p)\n", pout, pq);
1314 if ( (pq->w >= 1.0f) || (pq->w == -1.0f) )
1315 t = 1.0f;
1316 else
1317 t = acos( pq->w ) / sqrt( 1.0f - pq->w * pq->w );
1319 pout->x = t * pq->x;
1320 pout->y = t * pq->y;
1321 pout->z = t * pq->z;
1322 pout->w = 0.0f;
1324 return pout;
1327 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
1329 D3DXQUATERNION out;
1331 TRACE("(%p, %p, %p)\n", pout, pq1, pq2);
1333 out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
1334 out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
1335 out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
1336 out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
1337 *pout = out;
1338 return pout;
1341 D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1343 D3DXQUATERNION out;
1344 FLOAT norm;
1346 TRACE("(%p, %p)\n", pout, pq);
1348 norm = D3DXQuaternionLength(pq);
1350 out.x = pq->x / norm;
1351 out.y = pq->y / norm;
1352 out.z = pq->z / norm;
1353 out.w = pq->w / norm;
1355 *pout=out;
1357 return pout;
1360 D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
1362 D3DXVECTOR3 temp;
1364 TRACE("(%p, %p, %f)\n", pout, pv, angle);
1366 D3DXVec3Normalize(&temp, pv);
1367 pout->x = sin( angle / 2.0f ) * temp.x;
1368 pout->y = sin( angle / 2.0f ) * temp.y;
1369 pout->z = sin( angle / 2.0f ) * temp.z;
1370 pout->w = cos( angle / 2.0f );
1371 return pout;
1374 D3DXQUATERNION* WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *pout, CONST D3DXMATRIX *pm)
1376 int i, maxi;
1377 FLOAT maxdiag, S, trace;
1379 TRACE("(%p, %p)\n", pout, pm);
1381 trace = pm->u.m[0][0] + pm->u.m[1][1] + pm->u.m[2][2] + 1.0f;
1382 if ( trace > 1.0f)
1384 pout->x = ( pm->u.m[1][2] - pm->u.m[2][1] ) / ( 2.0f * sqrt(trace) );
1385 pout->y = ( pm->u.m[2][0] - pm->u.m[0][2] ) / ( 2.0f * sqrt(trace) );
1386 pout->z = ( pm->u.m[0][1] - pm->u.m[1][0] ) / ( 2.0f * sqrt(trace) );
1387 pout->w = sqrt(trace) / 2.0f;
1388 return pout;
1390 maxi = 0;
1391 maxdiag = pm->u.m[0][0];
1392 for (i=1; i<3; i++)
1394 if ( pm->u.m[i][i] > maxdiag )
1396 maxi = i;
1397 maxdiag = pm->u.m[i][i];
1400 switch( maxi )
1402 case 0:
1403 S = 2.0f * sqrt(1.0f + pm->u.m[0][0] - pm->u.m[1][1] - pm->u.m[2][2]);
1404 pout->x = 0.25f * S;
1405 pout->y = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1406 pout->z = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1407 pout->w = ( pm->u.m[1][2] - pm->u.m[2][1] ) / S;
1408 break;
1409 case 1:
1410 S = 2.0f * sqrt(1.0f + pm->u.m[1][1] - pm->u.m[0][0] - pm->u.m[2][2]);
1411 pout->x = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
1412 pout->y = 0.25f * S;
1413 pout->z = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1414 pout->w = ( pm->u.m[2][0] - pm->u.m[0][2] ) / S;
1415 break;
1416 case 2:
1417 S = 2.0f * sqrt(1.0f + pm->u.m[2][2] - pm->u.m[0][0] - pm->u.m[1][1]);
1418 pout->x = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
1419 pout->y = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
1420 pout->z = 0.25f * S;
1421 pout->w = ( pm->u.m[0][1] - pm->u.m[1][0] ) / S;
1422 break;
1424 return pout;
1427 D3DXQUATERNION * WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *out, FLOAT yaw, FLOAT pitch, FLOAT roll)
1429 FLOAT syaw, cyaw, spitch, cpitch, sroll, croll;
1431 TRACE("out %p, yaw %f, pitch %f, roll %f\n", out, yaw, pitch, roll);
1433 syaw = sinf(yaw / 2.0f);
1434 cyaw = cosf(yaw / 2.0f);
1435 spitch = sinf(pitch / 2.0f);
1436 cpitch = cosf(pitch / 2.0f);
1437 sroll = sinf(roll / 2.0f);
1438 croll = cosf(roll / 2.0f);
1440 out->x = syaw * cpitch * sroll + cyaw * spitch * croll;
1441 out->y = syaw * cpitch * croll - cyaw * spitch * sroll;
1442 out->z = cyaw * cpitch * sroll - syaw * spitch * croll;
1443 out->w = cyaw * cpitch * croll + syaw * spitch * sroll;
1445 return out;
1448 D3DXQUATERNION * WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *out, const D3DXQUATERNION *q1,
1449 const D3DXQUATERNION *q2, FLOAT t)
1451 FLOAT dot, temp;
1453 TRACE("out %p, q1 %p, q2 %p, t %f\n", out, q1, q2, t);
1455 temp = 1.0f - t;
1456 dot = D3DXQuaternionDot(q1, q2);
1457 if (dot < 0.0f)
1459 t = -t;
1460 dot = -dot;
1463 if (1.0f - dot > 0.001f)
1465 FLOAT theta = acosf(dot);
1467 temp = sinf(theta * temp) / sinf(theta);
1468 t = sinf(theta * t) / sinf(theta);
1471 out->x = temp * q1->x + t * q2->x;
1472 out->y = temp * q1->y + t * q2->y;
1473 out->z = temp * q1->z + t * q2->z;
1474 out->w = temp * q1->w + t * q2->w;
1476 return out;
1479 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, CONST D3DXQUATERNION *pq4, FLOAT t)
1481 D3DXQUATERNION temp1, temp2;
1483 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pq1, pq2, pq3, pq4, t);
1485 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1486 return pout;
1489 static D3DXQUATERNION add_diff(CONST D3DXQUATERNION *q1, CONST D3DXQUATERNION *q2, CONST FLOAT add)
1491 D3DXQUATERNION temp;
1493 temp.x = q1->x + add * q2->x;
1494 temp.y = q1->y + add * q2->y;
1495 temp.z = q1->z + add * q2->z;
1496 temp.w = q1->w + add * q2->w;
1498 return temp;
1501 void WINAPI D3DXQuaternionSquadSetup(D3DXQUATERNION *paout, D3DXQUATERNION *pbout, D3DXQUATERNION *pcout, CONST D3DXQUATERNION *pq0, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3)
1503 D3DXQUATERNION q, temp1, temp2, temp3, zero;
1505 TRACE("(%p, %p, %p, %p, %p, %p, %p)\n", paout, pbout, pcout, pq0, pq1, pq2, pq3);
1507 zero.x = 0.0f;
1508 zero.y = 0.0f;
1509 zero.z = 0.0f;
1510 zero.w = 0.0f;
1512 if ( D3DXQuaternionDot(pq0, pq1) < 0.0f )
1513 temp2 = add_diff(&zero, pq0, -1.0f);
1514 else
1515 temp2 = *pq0;
1517 if ( D3DXQuaternionDot(pq1, pq2) < 0.0f )
1518 *pcout = add_diff(&zero, pq2, -1.0f);
1519 else
1520 *pcout = *pq2;
1522 if ( D3DXQuaternionDot(pcout, pq3) < 0.0f )
1523 temp3 = add_diff(&zero, pq3, -1.0f);
1524 else
1525 temp3 = *pq3;
1527 D3DXQuaternionInverse(&temp1, pq1);
1528 D3DXQuaternionMultiply(&temp2, &temp1, &temp2);
1529 D3DXQuaternionLn(&temp2, &temp2);
1530 D3DXQuaternionMultiply(&q, &temp1, pcout);
1531 D3DXQuaternionLn(&q, &q);
1532 temp1 = add_diff(&temp2, &q, 1.0f);
1533 temp1.x *= -0.25f;
1534 temp1.y *= -0.25f;
1535 temp1.z *= -0.25f;
1536 temp1.w *= -0.25f;
1537 D3DXQuaternionExp(&temp1, &temp1);
1538 D3DXQuaternionMultiply(paout, pq1, &temp1);
1540 D3DXQuaternionInverse(&temp1, pcout);
1541 D3DXQuaternionMultiply(&temp2, &temp1, pq1);
1542 D3DXQuaternionLn(&temp2, &temp2);
1543 D3DXQuaternionMultiply(&q, &temp1, &temp3);
1544 D3DXQuaternionLn(&q, &q);
1545 temp1 = add_diff(&temp2, &q, 1.0f);
1546 temp1.x *= -0.25f;
1547 temp1.y *= -0.25f;
1548 temp1.z *= -0.25f;
1549 temp1.w *= -0.25f;
1550 D3DXQuaternionExp(&temp1, &temp1);
1551 D3DXQuaternionMultiply(pbout, pcout, &temp1);
1553 return;
1556 void WINAPI D3DXQuaternionToAxisAngle(CONST D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1558 TRACE("(%p, %p, %p)\n", pq, paxis, pangle);
1560 paxis->x = pq->x;
1561 paxis->y = pq->y;
1562 paxis->z = pq->z;
1563 *pangle = 2.0f * acos(pq->w);
1566 /*_________________D3DXVec2_____________________*/
1568 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1570 TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pv1, pv2, pv3, f, g);
1572 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1573 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1574 return pout;
1577 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv0, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT s)
1579 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv0, pv1, pv2, pv3, s);
1581 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);
1582 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);
1583 return pout;
1586 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pt1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pt2, FLOAT s)
1588 FLOAT h1, h2, h3, h4;
1590 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv1, pt1, pv2, pt2, s);
1592 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1593 h2 = s * s * s - 2.0f * s * s + s;
1594 h3 = -2.0f * s * s * s + 3.0f * s * s;
1595 h4 = s * s * s - s * s;
1597 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1598 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1599 return pout;
1602 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
1604 FLOAT norm;
1606 TRACE("(%p, %p)\n", pout, pv);
1608 norm = D3DXVec2Length(pv);
1609 if ( !norm )
1611 pout->x = 0.0f;
1612 pout->y = 0.0f;
1614 else
1616 pout->x = pv->x / norm;
1617 pout->y = pv->y / norm;
1620 return pout;
1623 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1625 TRACE("(%p, %p, %p)\n", pout, pv, pm);
1627 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
1628 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
1629 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
1630 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1631 return pout;
1634 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1636 UINT i;
1638 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1640 for (i = 0; i < elements; ++i) {
1641 D3DXVec2Transform(
1642 (D3DXVECTOR4*)((char*)out + outstride * i),
1643 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1644 matrix);
1646 return out;
1649 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1651 D3DXVECTOR2 v;
1652 FLOAT norm;
1654 TRACE("(%p, %p, %p)\n", pout, pv, pm);
1656 v = *pv;
1657 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1659 pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1660 pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1662 return pout;
1665 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1667 UINT i;
1669 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1671 for (i = 0; i < elements; ++i) {
1672 D3DXVec2TransformCoord(
1673 (D3DXVECTOR2*)((char*)out + outstride * i),
1674 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1675 matrix);
1677 return out;
1680 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1682 CONST D3DXVECTOR2 v = *pv;
1683 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1684 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1685 return pout;
1688 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride, CONST D3DXMATRIX *matrix, UINT elements)
1690 UINT i;
1692 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1694 for (i = 0; i < elements; ++i) {
1695 D3DXVec2TransformNormal(
1696 (D3DXVECTOR2*)((char*)out + outstride * i),
1697 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1698 matrix);
1700 return out;
1703 /*_________________D3DXVec3_____________________*/
1705 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1707 TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pv1, pv2, pv3, f, g);
1709 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1710 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1711 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1712 return pout;
1715 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
1717 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv0, pv1, pv2, pv3, s);
1719 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);
1720 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);
1721 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);
1722 return pout;
1725 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
1727 FLOAT h1, h2, h3, h4;
1729 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv1, pt1, pv2, pt2, s);
1731 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1732 h2 = s * s * s - 2.0f * s * s + s;
1733 h3 = -2.0f * s * s * s + 3.0f * s * s;
1734 h4 = s * s * s - s * s;
1736 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1737 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1738 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1739 return pout;
1742 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
1744 FLOAT norm;
1746 TRACE("(%p, %p)\n", pout, pv);
1748 norm = D3DXVec3Length(pv);
1749 if ( !norm )
1751 pout->x = 0.0f;
1752 pout->y = 0.0f;
1753 pout->z = 0.0f;
1755 else
1757 pout->x = pv->x / norm;
1758 pout->y = pv->y / norm;
1759 pout->z = pv->z / norm;
1762 return pout;
1765 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1767 D3DXMATRIX m;
1768 D3DXVECTOR3 out;
1770 TRACE("(%p, %p, %p, %p, %p, %p)\n", pout, pv, pviewport, pprojection, pview, pworld);
1772 D3DXMatrixMultiply(&m, pworld, pview);
1773 D3DXMatrixMultiply(&m, &m, pprojection);
1774 D3DXVec3TransformCoord(&out, pv, &m);
1775 out.x = pviewport->X + ( 1.0f + out.x ) * pviewport->Width / 2.0f;
1776 out.y = pviewport->Y + ( 1.0f - out.y ) * pviewport->Height / 2.0f;
1777 out.z = pviewport->MinZ + out.z * ( pviewport->MaxZ - pviewport->MinZ );
1778 *pout = out;
1779 return pout;
1782 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)
1784 UINT i;
1786 TRACE("(%p, %u, %p, %u, %p, %p, %p, %p, %u)\n", out, outstride, in, instride, viewport, projection, view, world, elements);
1788 for (i = 0; i < elements; ++i) {
1789 D3DXVec3Project(
1790 (D3DXVECTOR3*)((char*)out + outstride * i),
1791 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1792 viewport, projection, view, world);
1794 return out;
1797 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1799 TRACE("(%p, %p, %p)\n", pout, pv, pm);
1801 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];
1802 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];
1803 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];
1804 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];
1805 return pout;
1808 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1810 UINT i;
1812 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1814 for (i = 0; i < elements; ++i) {
1815 D3DXVec3Transform(
1816 (D3DXVECTOR4*)((char*)out + outstride * i),
1817 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1818 matrix);
1820 return out;
1823 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1825 D3DXVECTOR3 out;
1826 FLOAT norm;
1828 TRACE("(%p, %p, %p)\n", pout, pv, pm);
1830 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];
1832 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;
1833 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;
1834 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;
1836 *pout = out;
1838 return pout;
1841 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1843 UINT i;
1845 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1847 for (i = 0; i < elements; ++i) {
1848 D3DXVec3TransformCoord(
1849 (D3DXVECTOR3*)((char*)out + outstride * i),
1850 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1851 matrix);
1853 return out;
1856 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1858 CONST D3DXVECTOR3 v = *pv;
1860 TRACE("(%p, %p, %p)\n", pout, pv, pm);
1862 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1863 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1864 pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1865 return pout;
1869 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1871 UINT i;
1873 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1875 for (i = 0; i < elements; ++i) {
1876 D3DXVec3TransformNormal(
1877 (D3DXVECTOR3*)((char*)out + outstride * i),
1878 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1879 matrix);
1881 return out;
1884 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1886 D3DXMATRIX m;
1887 D3DXVECTOR3 out;
1889 TRACE("(%p, %p, %p, %p, %p, %p)\n", pout, pv, pviewport, pprojection, pview, pworld);
1891 if (pworld) {
1892 D3DXMatrixMultiply(&m, pworld, pview);
1893 D3DXMatrixMultiply(&m, &m, pprojection);
1894 } else {
1895 D3DXMatrixMultiply(&m, pview, pprojection);
1897 D3DXMatrixInverse(&m, NULL, &m);
1898 out.x = 2.0f * ( pv->x - pviewport->X ) / pviewport->Width - 1.0f;
1899 out.y = 1.0f - 2.0f * ( pv->y - pviewport->Y ) / pviewport->Height;
1900 out.z = ( pv->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
1901 D3DXVec3TransformCoord(&out, &out, &m);
1902 *pout = out;
1903 return pout;
1906 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)
1908 UINT i;
1910 TRACE("(%p, %u, %p, %u, %p, %p, %p, %p, %u)\n", out, outstride, in, instride, viewport, projection, view, world, elements);
1912 for (i = 0; i < elements; ++i) {
1913 D3DXVec3Unproject(
1914 (D3DXVECTOR3*)((char*)out + outstride * i),
1915 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1916 viewport, projection, view, world);
1918 return out;
1921 /*_________________D3DXVec4_____________________*/
1923 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
1925 TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pv1, pv2, pv3, f, g);
1927 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1928 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1929 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1930 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
1931 return pout;
1934 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv0, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT s)
1936 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv0, pv1, pv2, pv3, s);
1938 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);
1939 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);
1940 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);
1941 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);
1942 return pout;
1945 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
1947 D3DXVECTOR4 out;
1949 TRACE("(%p, %p, %p, %p)\n", pout, pv1, pv2, pv3);
1951 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);
1952 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));
1953 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);
1954 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));
1955 *pout = out;
1956 return pout;
1959 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pt1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pt2, FLOAT s)
1961 FLOAT h1, h2, h3, h4;
1963 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv1, pt1, pv2, pt2, s);
1965 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1966 h2 = s * s * s - 2.0f * s * s + s;
1967 h3 = -2.0f * s * s * s + 3.0f * s * s;
1968 h4 = s * s * s - s * s;
1970 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1971 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1972 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1973 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
1974 return pout;
1977 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
1979 FLOAT norm;
1981 TRACE("(%p, %p)\n", pout, pv);
1983 norm = D3DXVec4Length(pv);
1985 pout->x = pv->x / norm;
1986 pout->y = pv->y / norm;
1987 pout->z = pv->z / norm;
1988 pout->w = pv->w / norm;
1990 return pout;
1993 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
1995 D3DXVECTOR4 out;
1997 TRACE("(%p, %p, %p)\n", pout, pv, pm);
1999 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;
2000 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;
2001 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;
2002 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;
2003 *pout = out;
2004 return pout;
2007 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
2009 UINT i;
2011 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
2013 for (i = 0; i < elements; ++i) {
2014 D3DXVec4Transform(
2015 (D3DXVECTOR4*)((char*)out + outstride * i),
2016 (CONST D3DXVECTOR4*)((const char*)in + instride * i),
2017 matrix);
2019 return out;
2022 static inline unsigned short float_32_to_16(const float in)
2024 int exp = 0, origexp;
2025 float tmp = fabs(in);
2026 int sign = (copysignf(1, in) < 0);
2027 unsigned int mantissa;
2028 unsigned short ret;
2030 /* Deal with special numbers */
2031 if (isinf(in)) return (sign ? 0xffff : 0x7fff);
2032 if (isnan(in)) return (sign ? 0xffff : 0x7fff);
2033 if (in == 0.0f) return (sign ? 0x8000 : 0x0000);
2035 if (tmp < powf(2, 10))
2039 tmp *= 2.0f;
2040 exp--;
2041 } while (tmp < powf(2, 10));
2043 else if (tmp >= powf(2, 11))
2047 tmp /= 2.0f;
2048 exp++;
2049 } while (tmp >= powf(2, 11));
2052 exp += 10; /* Normalize the mantissa */
2053 exp += 15; /* Exponent is encoded with excess 15 */
2055 origexp = exp;
2057 mantissa = (unsigned int) tmp;
2058 if ((tmp - mantissa == 0.5f && mantissa % 2 == 1) || /* round half to even */
2059 (tmp - mantissa > 0.5f))
2061 mantissa++; /* round to nearest, away from zero */
2063 if (mantissa == 2048)
2065 mantissa = 1024;
2066 exp++;
2069 if (exp > 31)
2071 /* too big */
2072 ret = 0x7fff; /* INF */
2074 else if (exp <= 0)
2076 unsigned int rounding = 0;
2078 /* Denormalized half float */
2080 /* return 0x0000 (=0.0) for numbers too small to represent in half floats */
2081 if (exp < -11)
2082 return (sign ? 0x8000 : 0x0000);
2084 exp = origexp;
2086 /* the 13 extra bits from single precision are used for rounding */
2087 mantissa = (unsigned int)(tmp * powf(2, 13));
2088 mantissa >>= 1 - exp; /* denormalize */
2090 mantissa -= ~(mantissa >> 13) & 1; /* round half to even */
2091 /* remove 13 least significant bits to get half float precision */
2092 mantissa >>= 12;
2093 rounding = mantissa & 1;
2094 mantissa >>= 1;
2096 ret = mantissa + rounding;
2098 else
2100 ret = (exp << 10) | (mantissa & 0x3ff);
2103 ret |= ((sign ? 1 : 0) << 15); /* Add the sign */
2104 return ret;
2107 D3DXFLOAT16 *WINAPI D3DXFloat32To16Array(D3DXFLOAT16 *pout, CONST FLOAT *pin, UINT n)
2109 unsigned int i;
2111 TRACE("(%p, %p, %u)\n", pout, pin, n);
2113 for (i = 0; i < n; ++i)
2115 pout[i].value = float_32_to_16(pin[i]);
2118 return pout;
2121 /* Native d3dx9's D3DXFloat16to32Array lacks support for NaN and Inf. Specifically, e = 16 is treated as a
2122 * regular number - e.g., 0x7fff is converted to 131008.0 and 0xffff to -131008.0. */
2123 static inline float float_16_to_32(const unsigned short in)
2125 const unsigned short s = (in & 0x8000);
2126 const unsigned short e = (in & 0x7C00) >> 10;
2127 const unsigned short m = in & 0x3FF;
2128 const float sgn = (s ? -1.0f : 1.0f);
2130 if (e == 0)
2132 if (m == 0) return sgn * 0.0f; /* +0.0 or -0.0 */
2133 else return sgn * powf(2, -14.0f) * (m / 1024.0f);
2135 else
2137 return sgn * powf(2, e - 15.0f) * (1.0f + (m / 1024.0f));
2141 FLOAT *WINAPI D3DXFloat16To32Array(FLOAT *pout, CONST D3DXFLOAT16 *pin, UINT n)
2143 unsigned int i;
2145 TRACE("(%p, %p, %u)\n", pout, pin, n);
2147 for (i = 0; i < n; ++i)
2149 pout[i] = float_16_to_32(pin[i].value);
2152 return pout;
2155 /*_________________D3DXSH________________*/
2157 FLOAT* WINAPI D3DXSHAdd(FLOAT *out, UINT order, const FLOAT *a, const FLOAT *b)
2159 UINT i;
2161 TRACE("out %p, order %u, a %p, b %p\n", out, order, a, b);
2163 for (i = 0; i < order * order; i++)
2164 out[i] = a[i] + b[i];
2166 return out;
2169 FLOAT WINAPI D3DXSHDot(UINT order, CONST FLOAT *a, CONST FLOAT *b)
2171 FLOAT s;
2172 UINT i;
2174 TRACE("order %u, a %p, b %p\n", order, a, b);
2176 s = a[0] * b[0];
2177 for (i = 1; i < order * order; i++)
2178 s += a[i] * b[i];
2180 return s;
2183 FLOAT* WINAPI D3DXSHEvalDirection(FLOAT *out, UINT order, CONST D3DXVECTOR3 *dir)
2186 TRACE("(%p, %u, %p)\n", out, order, dir);
2188 if ( (order < D3DXSH_MINORDER) || (order > D3DXSH_MAXORDER) )
2189 return out;
2191 out[0] = 0.5f / sqrt(D3DX_PI);
2192 out[1] = -0.5f / sqrt(D3DX_PI / 3.0f) * dir->y;
2193 out[2] = 0.5f / sqrt(D3DX_PI / 3.0f) * dir->z;
2194 out[3] = -0.5f / sqrt(D3DX_PI / 3.0f) * dir->x;
2195 if ( order == 2 )
2196 return out;
2198 out[4] = 0.5f / sqrt(D3DX_PI / 15.0f) * dir->x * dir->y;
2199 out[5] = -0.5f / sqrt(D3DX_PI / 15.0f) * dir->y * dir->z;
2200 out[6] = 0.25f / sqrt(D3DX_PI / 5.0f) * ( 3.0f * dir->z * dir->z - 1.0f );
2201 out[7] = -0.5f / sqrt(D3DX_PI / 15.0f) * dir->x * dir->z;
2202 out[8] = 0.25f / sqrt(D3DX_PI / 15.0f) * ( dir->x * dir->x - dir->y * dir->y );
2203 if ( order == 3 )
2204 return out;
2206 out[9] = -sqrt(70.0f / D3DX_PI) / 8.0f * dir->y * (3.0f * dir->x * dir->x - dir->y * dir->y );
2207 out[10] = sqrt(105.0f / D3DX_PI) / 2.0f * dir->x * dir->y * dir->z;
2208 out[11] = -sqrt(42.0 / D3DX_PI) / 8.0f * dir->y * ( -1.0f + 5.0f * dir->z * dir->z );
2209 out[12] = sqrt(7.0f / D3DX_PI) / 4.0f * dir->z * ( 5.0f * dir->z * dir->z - 3.0f );
2210 out[13] = sqrt(42.0 / D3DX_PI) / 8.0f * dir->x * ( 1.0f - 5.0f * dir->z * dir->z );
2211 out[14] = sqrt(105.0f / D3DX_PI) / 4.0f * dir->z * ( dir->x * dir->x - dir->y * dir->y );
2212 out[15] = -sqrt(70.0f / D3DX_PI) / 8.0f * dir->x * ( dir->x * dir->x - 3.0f * dir->y * dir->y );
2213 if ( order == 4 )
2214 return out;
2216 out[16] = 0.75f * sqrt(35.0f / D3DX_PI) * dir->x * dir->y * (dir->x * dir->x - dir->y * dir->y );
2217 out[17] = 3.0f * dir->z * out[9];
2218 out[18] = 0.75f * sqrt(5.0f / D3DX_PI) * dir->x * dir->y * ( 7.0f * dir->z * dir->z - 1.0f );
2219 out[19] = 0.375f * sqrt(10.0f / D3DX_PI) * dir->y * dir->z * ( 3.0f - 7.0f * dir->z * dir->z );
2220 out[20] = 3.0f / ( 16.0f * sqrt(D3DX_PI) ) * ( 35.0f * dir->z * dir->z * dir->z * dir->z - 30.f * dir->z * dir->z + 3.0f );
2221 out[21] = 0.375f * sqrt(10.0f / D3DX_PI) * dir->x * dir->z * ( 3.0f - 7.0f * dir->z * dir->z );
2222 out[22] = 0.375f * sqrt(5.0f / D3DX_PI) * ( dir->x * dir->x - dir->y * dir->y ) * ( 7.0f * dir->z * dir->z - 1.0f);
2223 out[23] = 3.0 * dir->z * out[15];
2224 out[24] = 3.0f / 16.0f * sqrt(35.0f / D3DX_PI) * ( dir->x * dir->x * dir->x * dir->x- 6.0f * dir->x * dir->x * dir->y * dir->y + dir->y * dir->y * dir->y * dir->y );
2225 if ( order == 5 )
2226 return out;
2228 out[25] = -3.0f/ 32.0f * sqrt(154.0f / D3DX_PI) * dir->y * ( 5.0f * dir->x * dir->x * dir->x * dir->x - 10.0f * dir->x * dir->x * dir->y * dir->y + dir->y * dir->y * dir->y * dir->y );
2229 out[26] = 0.75f * sqrt(385.0f / D3DX_PI) * dir->x * dir->y * dir->z * ( dir->x * dir->x - dir->y * dir->y );
2230 out[27] = sqrt(770.0f / D3DX_PI) / 32.0f * dir->y * ( 3.0f * dir->x * dir->x - dir->y * dir->y ) * ( 1.0f - 9.0f * dir->z * dir->z );
2231 out[28] = sqrt(1155.0f / D3DX_PI) / 4.0f * dir->x * dir->y * dir->z * ( 3.0f * dir->z * dir->z - 1.0f);
2232 out[29] = sqrt(165.0f / D3DX_PI) / 16.0f * dir->y * ( 14.0f * dir->z * dir->z - 21.0f * dir->z * dir->z * dir->z * dir->z - 1.0f );
2233 out[30] = sqrt(11.0f / D3DX_PI) / 16.0f * dir->z * ( 63.0f * dir->z * dir->z * dir->z * dir->z - 70.0f * dir->z * dir->z + 15.0f );
2234 out[31] = sqrt(165.0f / D3DX_PI) / 16.0f * dir->x * ( 14.0f * dir->z * dir->z - 21.0f * dir->z * dir->z * dir->z * dir->z - 1.0f );
2235 out[32] = sqrt(1155.0f / D3DX_PI) / 8.0f * dir->z * ( dir->x * dir->x - dir->y * dir->y ) * ( 3.0f * dir->z * dir->z - 1.0f );
2236 out[33] = sqrt(770.0f / D3DX_PI) / 32.0f * dir->x * ( dir->x * dir->x - 3.0f * dir->y * dir->y ) * ( 1.0f - 9.0f * dir->z * dir->z );
2237 out[34] = 3.0f / 16.0f * sqrt(385.0f / D3DX_PI) * dir->z * ( dir->x * dir->x * dir->x * dir->x - 6.0 * dir->x * dir->x * dir->y * dir->y + dir->y * dir->y * dir->y * dir->y );
2238 out[35] = -3.0f/ 32.0f * sqrt(154.0f / D3DX_PI) * dir->x * ( dir->x * dir->x * dir->x * dir->x - 10.0f * dir->x * dir->x * dir->y * dir->y + 5.0f * dir->y * dir->y * dir->y * dir->y );
2240 return out;
2243 HRESULT WINAPI D3DXSHEvalDirectionalLight(UINT order, CONST D3DXVECTOR3 *dir, FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *Rout, FLOAT *Gout, FLOAT *Bout)
2245 FLOAT s, temp;
2246 UINT j;
2248 TRACE("Order %u, Vector %p, Red %f, Green %f, Blue %f, Rout %p, Gout %p, Bout %p\n", order, dir, Rintensity, Gintensity, Bintensity, Rout, Gout, Bout);
2250 s = 0.75f;
2251 if ( order > 2 )
2252 s += 5.0f / 16.0f;
2253 if ( order > 4 )
2254 s -= 3.0f / 32.0f;
2255 s /= D3DX_PI;
2257 D3DXSHEvalDirection(Rout, order, dir);
2258 for (j = 0; j < order * order; j++)
2260 temp = Rout[j] / s;
2262 Rout[j] = Rintensity * temp;
2263 if ( Gout )
2264 Gout[j] = Gintensity * temp;
2265 if ( Bout )
2266 Bout[j] = Bintensity * temp;
2269 return D3D_OK;
2272 FLOAT* WINAPI D3DXSHMultiply2(FLOAT *out, CONST FLOAT *a, CONST FLOAT *b)
2274 FLOAT ta, tb;
2276 TRACE("(%p, %p, %p)\n", out, a, b);
2278 ta = 0.28209479f * a[0];
2279 tb = 0.28209479f * b[0];
2281 out[0]= 0.28209479f * D3DXSHDot(2, a, b);
2282 out[1] = ta * b[1] + tb * a[1];
2283 out[2] = ta * b[2] + tb * a[2];
2284 out[3] = ta * b[3] + tb * a[3];
2286 return out;
2289 FLOAT* WINAPI D3DXSHMultiply3(FLOAT *out, CONST FLOAT *a, CONST FLOAT *b)
2291 FLOAT t, ta, tb;
2293 TRACE("(%p, %p, %p)\n", out, a, b);
2295 out[0]= 0.28209479f * a[0] * b[0];
2297 ta = 0.28209479f * a[0] - 0.12615662f * a[6] - 0.21850968f * a[8];
2298 tb = 0.28209479f * b[0] - 0.12615662f * b[6] - 0.21850968f * b[8];
2299 out[1] = ta * b[1] + tb * a[1];
2300 t = a[1] * b[1];
2301 out[0] += 0.28209479f * t;
2302 out[6] = -0.12615662f * t;
2303 out[8] = -0.21850968f * t;
2305 ta = 0.21850968f * a[5];
2306 tb = 0.21850968f * b[5];
2307 out[1] += ta * b[2] + tb * a[2];
2308 out[2] = ta * b[1] + tb * a[1];
2309 t = a[1] * b[2] +a[2] * b[1];
2310 out[5] = 0.21850968f * t;
2312 ta = 0.21850968f * a[4];
2313 tb = 0.21850968f * b[4];
2314 out[1] += ta * b[3] + tb * a[3];
2315 out[3] = ta * b[1] + tb * a[1];
2316 t = a[1] * b[3] + a[3] * b[1];
2317 out[4] = 0.21850968f * t;
2319 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2320 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2321 out[2] += ta * b[2] + tb * a[2];
2322 t = a[2] * b[2];
2323 out[0] += 0.28209480f * t;
2324 out[6] += 0.25231326f * t;
2326 ta = 0.21850969f * a[7];
2327 tb = 0.21850969f * b[7];
2328 out[2] += ta * b[3] + tb * a[3];
2329 out[3] += ta * b[2] + tb * a[2];
2330 t = a[2] * b[3] + a[3] * b[2];
2331 out[7] = 0.21850969f * t;
2333 ta = 0.28209479f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2334 tb = 0.28209479f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2335 out[3] += ta * b[3] + tb * a[3];
2336 t = a[3] * b[3];
2337 out[0] += 0.28209479f * t;
2338 out[6] -= 0.12615663f * t;
2339 out[8] += 0.21850969f * t;
2341 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2342 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2343 out[4] += ta * b[4] + tb * a[4];
2344 t = a[4] * b[4];
2345 out[0] += 0.28209479f * t;
2346 out[6] -= 0.18022375f * t;
2348 ta = 0.15607835f * a[7];
2349 tb = 0.15607835f * b[7];
2350 out[4] += ta * b[5] + tb * a[5];
2351 out[5] += ta * b[4] + tb * a[4];
2352 t = a[4] * b[5] + a[5] * b[4];
2353 out[7] += 0.15607834f * t;
2355 ta = 0.28209479f * a[0] + 0.09011186 * a[6] - 0.15607835f * a[8];
2356 tb = 0.28209479f * b[0] + 0.09011186 * b[6] - 0.15607835f * b[8];
2357 out[5] += ta * b[5] + tb * a[5];
2358 t = a[5] * b[5];
2359 out[0] += 0.28209479f * t;
2360 out[6] += 0.09011186f * t;
2361 out[8] -= 0.15607835f * t;
2363 ta = 0.28209480f * a[0];
2364 tb = 0.28209480f * b[0];
2365 out[6] += ta * b[6] + tb * a[6];
2366 t = a[6] * b[6];
2367 out[0] += 0.28209480f * t;
2368 out[6] += 0.18022376f * t;
2370 ta = 0.28209479f * a[0] + 0.09011186 * a[6] + 0.15607835f * a[8];
2371 tb = 0.28209479f * b[0] + 0.09011186 * b[6] + 0.15607835f * b[8];
2372 out[7] += ta * b[7] + tb * a[7];
2373 t = a[7] * b[7];
2374 out[0] += 0.28209479f * t;
2375 out[6] += 0.09011186f * t;
2376 out[8] += 0.15607835f * t;
2378 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2379 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2380 out[8] += ta * b[8] + tb * a[8];
2381 t = a[8] * b[8];
2382 out[0] += 0.28209479f * t;
2383 out[6] -= 0.18022375f * t;
2385 return out;
2388 static void rotate_X(FLOAT *out, UINT order, FLOAT a, FLOAT *in)
2390 out[0] = in[0];
2391 if ( order < 2 )
2392 return;
2394 out[1] = a * in[2];
2395 out[2] = -a * in[1];
2396 out[3] = in[3];
2397 if ( order == 2 )
2398 return;
2400 out[4] = a * in[7];
2401 out[5] = -in[5];
2402 out[6] = -0.5f * in[6] - 0.8660253882f * in[8];
2403 out[7] = -a * in[4];
2404 out[8] = -0.8660253882f * in[6] + 0.5f * in[8];
2405 out[9] = -a * 0.7905694842f * in[12] + a * 0.6123724580f * in[14];
2406 if ( order == 3 )
2407 return;
2409 out[10] = -in[10];
2410 out[11] = -a * 0.6123724580f * in[12] - a * 0.7905694842f * in[14];
2411 out[12] = a * 0.7905694842f * in[9] + a * 0.6123724580f * in[11];
2412 out[13] = -0.25f * in[13] - 0.9682458639f * in[15];
2413 out[14] = -a * 0.6123724580f * in[9] + a * 0.7905694842f * in[11];
2414 out[15] = -0.9682458639f * in[13] + 0.25f * in[15];
2415 if ( order == 4 )
2416 return;
2418 out[16] = -a * 0.9354143739f * in[21] + a * 0.3535533845f * in[23];
2419 out[17] = -0.75f * in[17] + 0.6614378095f * in[19];
2420 out[18] = -a * 0.3535533845f * in[21] - a * 0.9354143739f * in[23];
2421 out[19] = 0.6614378095f * in[17] + 0.75f * in[19];
2422 out[20] = 0.375f * in[20] + 0.5590170026f * in[22] + 0.7395099998f * in[24];
2423 out[21] = a * 0.9354143739f * in[16] + a * 0.3535533845f * in[18];
2424 out[22] = 0.5590170026f * in[20] + 0.5f * in[22] - 0.6614378691f * in[24];
2425 out[23] = -a * 0.3535533845f * in[16] + a * 0.9354143739f * in[18];
2426 out[24] = 0.7395099998f * in[20] - 0.6614378691f * in[22] + 0.125f * in[24];
2427 if ( order == 5 )
2428 return;
2430 out[25] = a * 0.7015607357f * in[30] - a * 0.6846531630f * in[32] + a * 0.1976423711f * in[34];
2431 out[26] = -0.5f * in[26] + 0.8660253882f * in[28];
2432 out[27] = a * 0.5229125023f * in[30] + a * 0.3061861992f * in[32] - a * 0.7954951525 * in[34];
2433 out[28] = 0.8660253882f * in[26] + 0.5f * in[28];
2434 out[29] = a * 0.4841229022f * in[30] + a * 0.6614378691f * in[32] + a * 0.5728219748f * in[34];
2435 out[30] = -a * 0.7015607357f * in[25] - a * 0.5229125023f * in[27] - a * 0.4841229022f * in[29];
2436 out[31] = 0.125f * in[31] + 0.4050463140f * in[33] + 0.9057110548f * in[35];
2437 out[32] = a * 0.6846531630f * in[25] - a * 0.3061861992f * in[27] - a * 0.6614378691f * in[29];
2438 out[33] = 0.4050463140f * in[31] + 0.8125f * in[33] - 0.4192627370f * in[35];
2439 out[34] = -a * 0.1976423711f * in[25] + a * 0.7954951525f * in[27] - a * 0.5728219748f * in[29];
2440 out[35] = 0.9057110548f * in[31] - 0.4192627370f * in[33] + 0.0624999329f * in[35];
2444 FLOAT* WINAPI D3DXSHRotate(FLOAT *out, UINT order, CONST D3DXMATRIX *matrix, CONST FLOAT *in)
2446 FLOAT alpha, beta, gamma, sinb, temp[36];
2448 TRACE("out %p, order %u, matrix %p, in %p\n", out, order, matrix, in);
2450 out[0] = in[0];
2452 if ( ( order > D3DXSH_MAXORDER ) || ( order < D3DXSH_MINORDER ) )
2453 return out;
2455 /* TODO: Implement handy computations for order <= 3. They are faster than the general algorithm. */
2456 if ( order < 4 )
2457 WARN("Using general algorithm for order = %u\n", order);
2459 if ( fabsf( matrix->u.m[2][2] ) != 1.0f )
2461 sinb = sqrtf( 1.0f - matrix->u.m[2][2] * matrix->u.m[2][2] );
2462 alpha = atan2f(matrix->u.m[2][1] / sinb, matrix->u.m[2][0] / sinb );
2463 beta = atan2f( sinb, matrix->u.m[2][2] );
2464 gamma = atan2f( matrix->u.m[1][2] / sinb, -matrix->u.m[0][2] / sinb );
2466 else
2468 alpha = atan2f( matrix->u.m[0][1], matrix->u.m[0][0] );
2469 beta = 0.0f;
2470 gamma = 0.0f;
2473 D3DXSHRotateZ(out, order, gamma, in);
2474 rotate_X(temp, order, 1.0f, out);
2475 D3DXSHRotateZ(out, order, beta, temp);
2476 rotate_X(temp, order, -1.0f, out);
2477 D3DXSHRotateZ(out, order, alpha, temp);
2479 return out;
2482 FLOAT * WINAPI D3DXSHRotateZ(FLOAT *out, UINT order, FLOAT angle, CONST FLOAT *in)
2484 FLOAT c1a, c2a, c3a, c4a, c5a, s1a, s2a, s3a, s4a, s5a;
2486 TRACE("out %p, order %u, angle %f, in %p\n", out, order, angle, in);
2488 c1a = cosf(angle);
2489 s1a = sinf(angle);
2490 out[0] = in[0];
2491 out[1] = c1a * in[1] + s1a * in[3];
2492 out[2] = in[2];
2493 out[3] = c1a * in[3] - s1a * in[1];
2494 if (order <= D3DXSH_MINORDER)
2495 return out;
2497 c2a = cosf(2.0f * angle);
2498 s2a = sinf(2.0f * angle);
2499 out[4] = c2a * in[4] + s2a * in[8];
2500 out[5] = c1a * in[5] + s1a * in[7];
2501 out[6] = in[6];
2502 out[7] = c1a * in[7] - s1a * in[5];
2503 out[8] = c2a * in[8] - s2a * in[4];
2504 if (order == 3)
2505 return out;
2507 c3a = cosf(3.0f * angle);
2508 s3a = sinf(3.0f * angle);
2509 out[9] = c3a * in[9] + s3a * in[15];
2510 out[10] = c2a * in[10] + s2a * in[14];
2511 out[11] = c1a * in[11] + s1a * in[13];
2512 out[12] = in[12];
2513 out[13] = c1a * in[13] - s1a * in[11];
2514 out[14] = c2a * in[14] - s2a * in[10];
2515 out[15] = c3a * in[15] - s3a * in[9];
2516 if (order == 4)
2517 return out;
2519 c4a = cosf(4.0f * angle);
2520 s4a = sinf(4.0f * angle);
2521 out[16] = c4a * in[16] + s4a * in[24];
2522 out[17] = c3a * in[17] + s3a * in[23];
2523 out[18] = c2a * in[18] + s2a * in[22];
2524 out[19] = c1a * in[19] + s1a * in[21];
2525 out[20] = in[20];
2526 out[21] = c1a * in[21] - s1a * in[19];
2527 out[22] = c2a * in[22] - s2a * in[18];
2528 out[23] = c3a * in[23] - s3a * in[17];
2529 out[24] = c4a * in[24] - s4a * in[16];
2530 if (order == 5)
2531 return out;
2533 c5a = cosf(5.0f * angle);
2534 s5a = sinf(5.0f * angle);
2535 out[25] = c5a * in[25] + s5a * in[35];
2536 out[26] = c4a * in[26] + s4a * in[34];
2537 out[27] = c3a * in[27] + s3a * in[33];
2538 out[28] = c2a * in[28] + s2a * in[32];
2539 out[29] = c1a * in[29] + s1a * in[31];
2540 out[30] = in[30];
2541 out[31] = c1a * in[31] - s1a * in[29];
2542 out[32] = c2a * in[32] - s2a * in[28];
2543 out[33] = c3a * in[33] - s3a * in[27];
2544 out[34] = c4a * in[34] - s4a * in[26];
2545 out[35] = c5a * in[35] - s5a * in[25];
2547 return out;
2550 FLOAT* WINAPI D3DXSHScale(FLOAT *out, UINT order, CONST FLOAT *a, CONST FLOAT scale)
2552 UINT i;
2554 TRACE("out %p, order %u, a %p, scale %f\n", out, order, a, scale);
2556 for (i = 0; i < order * order; i++)
2557 out[i] = a[i] * scale;
2559 return out;