d3dx9: Handle NULL viewport in D3DXVec3Unproject.
[wine.git] / dlls / d3dx9_36 / math.c
blob117bb848c88fa774c57f02d51eb72a6cf1513345
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("costheta %f, refractionindex %f\n", costheta, refractionindex);
86 g = sqrtf(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 *= 0.5f * d * d / (a * a);
92 return result;
95 /*_________________D3DXMatrix____________________*/
97 D3DXMATRIX * WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *out, FLOAT scaling, const D3DXVECTOR3 *rotationcenter,
98 const D3DXQUATERNION *rotation, const D3DXVECTOR3 *translation)
100 TRACE("out %p, scaling %f, rotationcenter %p, rotation %p, translation %p\n",
101 out, scaling, rotationcenter, rotation, translation);
103 D3DXMatrixIdentity(out);
105 if (rotationcenter)
107 out->u.m[3][0] = -rotationcenter->x;
108 out->u.m[3][1] = -rotationcenter->y;
109 out->u.m[3][2] = -rotationcenter->z;
112 if (rotation)
114 FLOAT temp00, temp01, temp02, temp10, temp11, temp12, temp20, temp21, temp22;
116 temp00 = 1.0f - 2.0f * (rotation->y * rotation->y + rotation->z * rotation->z);
117 temp01 = 2.0f * (rotation->x * rotation->y + rotation->z * rotation->w);
118 temp02 = 2.0f * (rotation->x * rotation->z - rotation->y * rotation->w);
119 temp10 = 2.0f * (rotation->x * rotation->y - rotation->z * rotation->w);
120 temp11 = 1.0f - 2.0f * (rotation->x * rotation->x + rotation->z * rotation->z);
121 temp12 = 2.0f * (rotation->y * rotation->z + rotation->x * rotation->w);
122 temp20 = 2.0f * (rotation->x * rotation->z + rotation->y * rotation->w);
123 temp21 = 2.0f * (rotation->y * rotation->z - rotation->x * rotation->w);
124 temp22 = 1.0f - 2.0f * (rotation->x * rotation->x + rotation->y * rotation->y);
126 out->u.m[0][0] = scaling * temp00;
127 out->u.m[0][1] = scaling * temp01;
128 out->u.m[0][2] = scaling * temp02;
129 out->u.m[1][0] = scaling * temp10;
130 out->u.m[1][1] = scaling * temp11;
131 out->u.m[1][2] = scaling * temp12;
132 out->u.m[2][0] = scaling * temp20;
133 out->u.m[2][1] = scaling * temp21;
134 out->u.m[2][2] = scaling * temp22;
136 if (rotationcenter)
138 FLOAT x, y, z;
140 x = out->u.m[3][0];
141 y = out->u.m[3][1];
142 z = out->u.m[3][2];
144 out->u.m[3][0] = x * temp00 + y * temp10 + z * temp20;
145 out->u.m[3][1] = x * temp01 + y * temp11 + z * temp21;
146 out->u.m[3][2] = x * temp02 + y * temp12 + z * temp22;
149 else
151 out->u.m[0][0] = scaling;
152 out->u.m[1][1] = scaling;
153 out->u.m[2][2] = scaling;
156 if (rotationcenter)
158 out->u.m[3][0] += rotationcenter->x;
159 out->u.m[3][1] += rotationcenter->y;
160 out->u.m[3][2] += rotationcenter->z;
163 if (translation)
165 out->u.m[3][0] += translation->x;
166 out->u.m[3][1] += translation->y;
167 out->u.m[3][2] += translation->z;
170 return out;
173 D3DXMATRIX * WINAPI D3DXMatrixAffineTransformation2D(D3DXMATRIX *out, FLOAT scaling,
174 const D3DXVECTOR2 *rotationcenter, FLOAT rotation, const D3DXVECTOR2 *translation)
176 FLOAT tmp1, tmp2, s;
178 TRACE("out %p, scaling %f, rotationcenter %p, rotation %f, translation %p\n",
179 out, scaling, rotationcenter, rotation, translation);
181 s = sinf(rotation / 2.0f);
182 tmp1 = 1.0f - 2.0f * s * s;
183 tmp2 = 2.0 * s * cosf(rotation / 2.0f);
185 D3DXMatrixIdentity(out);
186 out->u.m[0][0] = scaling * tmp1;
187 out->u.m[0][1] = scaling * tmp2;
188 out->u.m[1][0] = -scaling * tmp2;
189 out->u.m[1][1] = scaling * tmp1;
191 if (rotationcenter)
193 FLOAT x, y;
195 x = rotationcenter->x;
196 y = rotationcenter->y;
198 out->u.m[3][0] = y * tmp2 - x * tmp1 + x;
199 out->u.m[3][1] = -x * tmp2 - y * tmp1 + y;
202 if (translation)
204 out->u.m[3][0] += translation->x;
205 out->u.m[3][1] += translation->y;
208 return out;
211 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, CONST D3DXMATRIX *pm)
213 D3DXMATRIX normalized;
214 D3DXVECTOR3 vec;
216 TRACE("(%p, %p, %p, %p)\n", poutscale, poutrotation, pouttranslation, pm);
218 /*Compute the scaling part.*/
219 vec.x=pm->u.m[0][0];
220 vec.y=pm->u.m[0][1];
221 vec.z=pm->u.m[0][2];
222 poutscale->x=D3DXVec3Length(&vec);
224 vec.x=pm->u.m[1][0];
225 vec.y=pm->u.m[1][1];
226 vec.z=pm->u.m[1][2];
227 poutscale->y=D3DXVec3Length(&vec);
229 vec.x=pm->u.m[2][0];
230 vec.y=pm->u.m[2][1];
231 vec.z=pm->u.m[2][2];
232 poutscale->z=D3DXVec3Length(&vec);
234 /*Compute the translation part.*/
235 pouttranslation->x=pm->u.m[3][0];
236 pouttranslation->y=pm->u.m[3][1];
237 pouttranslation->z=pm->u.m[3][2];
239 /*Let's calculate the rotation now*/
240 if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) ) return D3DERR_INVALIDCALL;
242 normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
243 normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
244 normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
245 normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
246 normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
247 normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
248 normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
249 normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
250 normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
252 D3DXQuaternionRotationMatrix(poutrotation,&normalized);
253 return S_OK;
256 FLOAT WINAPI D3DXMatrixDeterminant(CONST D3DXMATRIX *pm)
258 D3DXVECTOR4 minor, v1, v2, v3;
259 FLOAT det;
261 TRACE("(%p)\n", pm);
263 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];
264 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];
265 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];
266 D3DXVec4Cross(&minor, &v1, &v2, &v3);
267 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);
268 return det;
271 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, CONST D3DXMATRIX *pm)
273 int a, i, j;
274 D3DXMATRIX out;
275 D3DXVECTOR4 v, vec[3];
276 FLOAT det;
278 TRACE("(%p, %p, %p)\n", pout, pdeterminant, pm);
280 det = D3DXMatrixDeterminant(pm);
281 if ( !det ) return NULL;
282 if ( pdeterminant ) *pdeterminant = det;
283 for (i=0; i<4; i++)
285 for (j=0; j<4; j++)
287 if (j != i )
289 a = j;
290 if ( j > i ) a = a-1;
291 vec[a].x = pm->u.m[j][0];
292 vec[a].y = pm->u.m[j][1];
293 vec[a].z = pm->u.m[j][2];
294 vec[a].w = pm->u.m[j][3];
297 D3DXVec4Cross(&v, &vec[0], &vec[1], &vec[2]);
298 out.u.m[0][i] = pow(-1.0f, i) * v.x / det;
299 out.u.m[1][i] = pow(-1.0f, i) * v.y / det;
300 out.u.m[2][i] = pow(-1.0f, i) * v.z / det;
301 out.u.m[3][i] = pow(-1.0f, i) * v.w / det;
304 *pout = out;
305 return pout;
308 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
310 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
312 TRACE("(%p, %p, %p, %p)\n", pout, peye, pat, pup);
314 D3DXVec3Subtract(&vec2, pat, peye);
315 D3DXVec3Normalize(&vec, &vec2);
316 D3DXVec3Cross(&right, pup, &vec);
317 D3DXVec3Cross(&up, &vec, &right);
318 D3DXVec3Normalize(&rightn, &right);
319 D3DXVec3Normalize(&upn, &up);
320 pout->u.m[0][0] = rightn.x;
321 pout->u.m[1][0] = rightn.y;
322 pout->u.m[2][0] = rightn.z;
323 pout->u.m[3][0] = -D3DXVec3Dot(&rightn,peye);
324 pout->u.m[0][1] = upn.x;
325 pout->u.m[1][1] = upn.y;
326 pout->u.m[2][1] = upn.z;
327 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
328 pout->u.m[0][2] = vec.x;
329 pout->u.m[1][2] = vec.y;
330 pout->u.m[2][2] = vec.z;
331 pout->u.m[3][2] = -D3DXVec3Dot(&vec, peye);
332 pout->u.m[0][3] = 0.0f;
333 pout->u.m[1][3] = 0.0f;
334 pout->u.m[2][3] = 0.0f;
335 pout->u.m[3][3] = 1.0f;
336 return pout;
339 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
341 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
343 TRACE("(%p, %p, %p, %p)\n", pout, peye, pat, pup);
345 D3DXVec3Subtract(&vec2, pat, peye);
346 D3DXVec3Normalize(&vec, &vec2);
347 D3DXVec3Cross(&right, pup, &vec);
348 D3DXVec3Cross(&up, &vec, &right);
349 D3DXVec3Normalize(&rightn, &right);
350 D3DXVec3Normalize(&upn, &up);
351 pout->u.m[0][0] = -rightn.x;
352 pout->u.m[1][0] = -rightn.y;
353 pout->u.m[2][0] = -rightn.z;
354 pout->u.m[3][0] = D3DXVec3Dot(&rightn,peye);
355 pout->u.m[0][1] = upn.x;
356 pout->u.m[1][1] = upn.y;
357 pout->u.m[2][1] = upn.z;
358 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
359 pout->u.m[0][2] = -vec.x;
360 pout->u.m[1][2] = -vec.y;
361 pout->u.m[2][2] = -vec.z;
362 pout->u.m[3][2] = D3DXVec3Dot(&vec, peye);
363 pout->u.m[0][3] = 0.0f;
364 pout->u.m[1][3] = 0.0f;
365 pout->u.m[2][3] = 0.0f;
366 pout->u.m[3][3] = 1.0f;
367 return pout;
370 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
372 D3DXMATRIX out;
373 int i,j;
375 TRACE("(%p, %p, %p)\n", pout, pm1, pm2);
377 for (i=0; i<4; i++)
379 for (j=0; j<4; j++)
381 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];
385 *pout = out;
386 return pout;
389 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
391 TRACE("%p, %p, %p)\n", pout, pm1, pm2);
393 D3DXMatrixMultiply(pout, pm1, pm2);
394 D3DXMatrixTranspose(pout, pout);
395 return pout;
398 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
400 TRACE("(%p, %f, %f, %f, %f)\n", pout, w, h, zn, zf);
402 D3DXMatrixIdentity(pout);
403 pout->u.m[0][0] = 2.0f / w;
404 pout->u.m[1][1] = 2.0f / h;
405 pout->u.m[2][2] = 1.0f / (zf - zn);
406 pout->u.m[3][2] = zn / (zn - zf);
407 return pout;
410 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
412 TRACE("(%p, %f, %f, %f, %f, %f, %f)\n", pout, l, r, b, t, zn, zf);
414 D3DXMatrixIdentity(pout);
415 pout->u.m[0][0] = 2.0f / (r - l);
416 pout->u.m[1][1] = 2.0f / (t - b);
417 pout->u.m[2][2] = 1.0f / (zf -zn);
418 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
419 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
420 pout->u.m[3][2] = zn / (zn -zf);
421 return pout;
424 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
426 TRACE("(%p, %f, %f, %f, %f, %f, %f)\n", pout, l, r, b, t, zn, zf);
428 D3DXMatrixIdentity(pout);
429 pout->u.m[0][0] = 2.0f / (r - l);
430 pout->u.m[1][1] = 2.0f / (t - b);
431 pout->u.m[2][2] = 1.0f / (zn -zf);
432 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
433 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
434 pout->u.m[3][2] = zn / (zn -zf);
435 return pout;
438 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
440 TRACE("(%p, %f, %f, %f, %f)\n", pout, w, h, zn, zf);
442 D3DXMatrixIdentity(pout);
443 pout->u.m[0][0] = 2.0f / w;
444 pout->u.m[1][1] = 2.0f / h;
445 pout->u.m[2][2] = 1.0f / (zn - zf);
446 pout->u.m[3][2] = zn / (zn - zf);
447 return pout;
450 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
452 TRACE("(%p, %f, %f, %f, %f)\n", pout, fovy, aspect, zn, zf);
454 D3DXMatrixIdentity(pout);
455 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
456 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
457 pout->u.m[2][2] = zf / (zf - zn);
458 pout->u.m[2][3] = 1.0f;
459 pout->u.m[3][2] = (zf * zn) / (zn - zf);
460 pout->u.m[3][3] = 0.0f;
461 return pout;
464 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
466 TRACE("(%p, %f, %f, %f, %f)\n", pout, fovy, aspect, zn, zf);
468 D3DXMatrixIdentity(pout);
469 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
470 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
471 pout->u.m[2][2] = zf / (zn - zf);
472 pout->u.m[2][3] = -1.0f;
473 pout->u.m[3][2] = (zf * zn) / (zn - zf);
474 pout->u.m[3][3] = 0.0f;
475 return pout;
478 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
480 TRACE("(%p, %f, %f, %f, %f)\n", pout, w, h, zn, zf);
482 D3DXMatrixIdentity(pout);
483 pout->u.m[0][0] = 2.0f * zn / w;
484 pout->u.m[1][1] = 2.0f * zn / h;
485 pout->u.m[2][2] = zf / (zf - zn);
486 pout->u.m[3][2] = (zn * zf) / (zn - zf);
487 pout->u.m[2][3] = 1.0f;
488 pout->u.m[3][3] = 0.0f;
489 return pout;
492 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
494 TRACE("(%p, %f, %f, %f, %f, %f, %f)\n", pout, l, r, b, t, zn, zf);
496 D3DXMatrixIdentity(pout);
497 pout->u.m[0][0] = 2.0f * zn / (r - l);
498 pout->u.m[1][1] = -2.0f * zn / (b - t);
499 pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
500 pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
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 D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
510 TRACE("(%p, %f, %f, %f, %f, %f, %f)\n", pout, l, r, b, t, zn, zf);
512 D3DXMatrixIdentity(pout);
513 pout->u.m[0][0] = 2.0f * zn / (r - l);
514 pout->u.m[1][1] = -2.0f * zn / (b - t);
515 pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
516 pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
517 pout->u.m[2][2] = zf / (zn - zf);
518 pout->u.m[3][2] = (zn * zf) / (zn -zf);
519 pout->u.m[2][3] = -1.0f;
520 pout->u.m[3][3] = 0.0f;
521 return pout;
524 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
526 TRACE("(%p, %f, %f, %f, %f)\n", pout, w, h, zn, zf);
528 D3DXMatrixIdentity(pout);
529 pout->u.m[0][0] = 2.0f * zn / w;
530 pout->u.m[1][1] = 2.0f * zn / h;
531 pout->u.m[2][2] = zf / (zn - zf);
532 pout->u.m[3][2] = (zn * zf) / (zn - zf);
533 pout->u.m[2][3] = -1.0f;
534 pout->u.m[3][3] = 0.0f;
535 return pout;
538 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, CONST D3DXPLANE *pplane)
540 D3DXPLANE Nplane;
542 TRACE("(%p, %p)\n", pout, pplane);
544 D3DXPlaneNormalize(&Nplane, pplane);
545 D3DXMatrixIdentity(pout);
546 pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
547 pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
548 pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
549 pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
550 pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
551 pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
552 pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
553 pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
554 pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
555 pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
556 pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
557 pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
558 return pout;
561 D3DXMATRIX * WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *out, const D3DXVECTOR3 *v, FLOAT angle)
563 D3DXVECTOR3 nv;
564 FLOAT sangle, cangle, cdiff;
566 TRACE("out %p, v %p, angle %f\n", out, v, angle);
568 D3DXVec3Normalize(&nv, v);
569 sangle = sinf(angle);
570 cangle = cosf(angle);
571 cdiff = 1.0f - cangle;
573 out->u.m[0][0] = cdiff * nv.x * nv.x + cangle;
574 out->u.m[1][0] = cdiff * nv.x * nv.y - sangle * nv.z;
575 out->u.m[2][0] = cdiff * nv.x * nv.z + sangle * nv.y;
576 out->u.m[3][0] = 0.0f;
577 out->u.m[0][1] = cdiff * nv.y * nv.x + sangle * nv.z;
578 out->u.m[1][1] = cdiff * nv.y * nv.y + cangle;
579 out->u.m[2][1] = cdiff * nv.y * nv.z - sangle * nv.x;
580 out->u.m[3][1] = 0.0f;
581 out->u.m[0][2] = cdiff * nv.z * nv.x - sangle * nv.y;
582 out->u.m[1][2] = cdiff * nv.z * nv.y + sangle * nv.x;
583 out->u.m[2][2] = cdiff * nv.z * nv.z + cangle;
584 out->u.m[3][2] = 0.0f;
585 out->u.m[0][3] = 0.0f;
586 out->u.m[1][3] = 0.0f;
587 out->u.m[2][3] = 0.0f;
588 out->u.m[3][3] = 1.0f;
590 return out;
593 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, CONST D3DXQUATERNION *pq)
595 TRACE("(%p, %p)\n", pout, pq);
597 D3DXMatrixIdentity(pout);
598 pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
599 pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
600 pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
601 pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
602 pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
603 pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
604 pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
605 pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
606 pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
607 return pout;
610 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
612 TRACE("(%p, %f)\n", pout, angle);
614 D3DXMatrixIdentity(pout);
615 pout->u.m[1][1] = cos(angle);
616 pout->u.m[2][2] = cos(angle);
617 pout->u.m[1][2] = sin(angle);
618 pout->u.m[2][1] = -sin(angle);
619 return pout;
622 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
624 TRACE("(%p, %f)\n", pout, angle);
626 D3DXMatrixIdentity(pout);
627 pout->u.m[0][0] = cos(angle);
628 pout->u.m[2][2] = cos(angle);
629 pout->u.m[0][2] = -sin(angle);
630 pout->u.m[2][0] = sin(angle);
631 return pout;
634 D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
636 D3DXMATRIX m;
638 TRACE("(%p, %f, %f, %f)\n", pout, yaw, pitch, roll);
640 D3DXMatrixIdentity(pout);
641 D3DXMatrixRotationZ(&m, roll);
642 D3DXMatrixMultiply(pout, pout, &m);
643 D3DXMatrixRotationX(&m, pitch);
644 D3DXMatrixMultiply(pout, pout, &m);
645 D3DXMatrixRotationY(&m, yaw);
646 D3DXMatrixMultiply(pout, pout, &m);
647 return pout;
650 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
652 TRACE("(%p, %f)\n", pout, angle);
654 D3DXMatrixIdentity(pout);
655 pout->u.m[0][0] = cos(angle);
656 pout->u.m[1][1] = cos(angle);
657 pout->u.m[0][1] = sin(angle);
658 pout->u.m[1][0] = -sin(angle);
659 return pout;
662 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
664 TRACE("(%p, %f, %f, %f)\n", pout, sx, sy, sz);
666 D3DXMatrixIdentity(pout);
667 pout->u.m[0][0] = sx;
668 pout->u.m[1][1] = sy;
669 pout->u.m[2][2] = sz;
670 return pout;
673 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, CONST D3DXVECTOR4 *plight, CONST D3DXPLANE *pplane)
675 D3DXPLANE Nplane;
676 FLOAT dot;
678 TRACE("(%p, %p, %p)\n", pout, plight, pplane);
680 D3DXPlaneNormalize(&Nplane, pplane);
681 dot = D3DXPlaneDot(&Nplane, plight);
682 pout->u.m[0][0] = dot - Nplane.a * plight->x;
683 pout->u.m[0][1] = -Nplane.a * plight->y;
684 pout->u.m[0][2] = -Nplane.a * plight->z;
685 pout->u.m[0][3] = -Nplane.a * plight->w;
686 pout->u.m[1][0] = -Nplane.b * plight->x;
687 pout->u.m[1][1] = dot - Nplane.b * plight->y;
688 pout->u.m[1][2] = -Nplane.b * plight->z;
689 pout->u.m[1][3] = -Nplane.b * plight->w;
690 pout->u.m[2][0] = -Nplane.c * plight->x;
691 pout->u.m[2][1] = -Nplane.c * plight->y;
692 pout->u.m[2][2] = dot - Nplane.c * plight->z;
693 pout->u.m[2][3] = -Nplane.c * plight->w;
694 pout->u.m[3][0] = -Nplane.d * plight->x;
695 pout->u.m[3][1] = -Nplane.d * plight->y;
696 pout->u.m[3][2] = -Nplane.d * plight->z;
697 pout->u.m[3][3] = dot - Nplane.d * plight->w;
698 return pout;
701 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pscalingcenter, CONST D3DXQUATERNION *pscalingrotation, CONST D3DXVECTOR3 *pscaling, CONST D3DXVECTOR3 *protationcenter, CONST D3DXQUATERNION *protation, CONST D3DXVECTOR3 *ptranslation)
703 D3DXMATRIX m1, m2, m3, m4, m5, m6, m7;
704 D3DXQUATERNION prc;
705 D3DXVECTOR3 psc, pt;
707 TRACE("(%p, %p, %p, %p, %p, %p, %p)\n", pout, pscalingcenter, pscalingrotation, pscaling, protationcenter, protation, ptranslation);
709 if ( !pscalingcenter )
711 psc.x = 0.0f;
712 psc.y = 0.0f;
713 psc.z = 0.0f;
715 else
717 psc.x = pscalingcenter->x;
718 psc.y = pscalingcenter->y;
719 psc.z = pscalingcenter->z;
722 if ( !protationcenter )
724 prc.x = 0.0f;
725 prc.y = 0.0f;
726 prc.z = 0.0f;
728 else
730 prc.x = protationcenter->x;
731 prc.y = protationcenter->y;
732 prc.z = protationcenter->z;
735 if ( !ptranslation )
737 pt.x = 0.0f;
738 pt.y = 0.0f;
739 pt.z = 0.0f;
741 else
743 pt.x = ptranslation->x;
744 pt.y = ptranslation->y;
745 pt.z = ptranslation->z;
748 D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
750 if ( !pscalingrotation )
752 D3DXMatrixIdentity(&m2);
753 D3DXMatrixIdentity(&m4);
755 else
757 D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
758 D3DXMatrixInverse(&m2, NULL, &m4);
761 if ( !pscaling ) D3DXMatrixIdentity(&m3);
762 else D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
764 if ( !protation ) D3DXMatrixIdentity(&m6);
765 else D3DXMatrixRotationQuaternion(&m6, protation);
767 D3DXMatrixTranslation(&m5, psc.x - prc.x, psc.y - prc.y, psc.z - prc.z);
768 D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
769 D3DXMatrixMultiply(&m1, &m1, &m2);
770 D3DXMatrixMultiply(&m1, &m1, &m3);
771 D3DXMatrixMultiply(&m1, &m1, &m4);
772 D3DXMatrixMultiply(&m1, &m1, &m5);
773 D3DXMatrixMultiply(&m1, &m1, &m6);
774 D3DXMatrixMultiply(pout, &m1, &m7);
775 return pout;
778 D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(D3DXMATRIX *pout, CONST D3DXVECTOR2 *pscalingcenter, FLOAT scalingrotation, CONST D3DXVECTOR2 *pscaling, CONST D3DXVECTOR2 *protationcenter, FLOAT rotation, CONST D3DXVECTOR2 *ptranslation)
780 D3DXQUATERNION rot, sca_rot;
781 D3DXVECTOR3 rot_center, sca, sca_center, trans;
783 TRACE("(%p, %p, %f, %p, %p, %f, %p)\n", pout, pscalingcenter, scalingrotation, pscaling, protationcenter, rotation, ptranslation);
785 if ( pscalingcenter )
787 sca_center.x=pscalingcenter->x;
788 sca_center.y=pscalingcenter->y;
789 sca_center.z=0.0f;
791 else
793 sca_center.x=0.0f;
794 sca_center.y=0.0f;
795 sca_center.z=0.0f;
798 if ( pscaling )
800 sca.x=pscaling->x;
801 sca.y=pscaling->y;
802 sca.z=1.0f;
804 else
806 sca.x=1.0f;
807 sca.y=1.0f;
808 sca.z=1.0f;
811 if ( protationcenter )
813 rot_center.x=protationcenter->x;
814 rot_center.y=protationcenter->y;
815 rot_center.z=0.0f;
817 else
819 rot_center.x=0.0f;
820 rot_center.y=0.0f;
821 rot_center.z=0.0f;
824 if ( ptranslation )
826 trans.x=ptranslation->x;
827 trans.y=ptranslation->y;
828 trans.z=0.0f;
830 else
832 trans.x=0.0f;
833 trans.y=0.0f;
834 trans.z=0.0f;
837 rot.w=cos(rotation/2.0f);
838 rot.x=0.0f;
839 rot.y=0.0f;
840 rot.z=sin(rotation/2.0f);
842 sca_rot.w=cos(scalingrotation/2.0f);
843 sca_rot.x=0.0f;
844 sca_rot.y=0.0f;
845 sca_rot.z=sin(scalingrotation/2.0f);
847 D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
849 return pout;
852 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
854 TRACE("(%p, %f, %f, %f)\n", pout, x, y, z);
856 D3DXMatrixIdentity(pout);
857 pout->u.m[3][0] = x;
858 pout->u.m[3][1] = y;
859 pout->u.m[3][2] = z;
860 return pout;
863 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
865 CONST D3DXMATRIX m = *pm;
866 int i,j;
868 TRACE("(%p, %p)\n", pout, pm);
870 for (i=0; i<4; i++)
871 for (j=0; j<4; j++) pout->u.m[i][j] = m.u.m[j][i];
873 return pout;
876 /*_________________D3DXMatrixStack____________________*/
878 static const unsigned int INITIAL_STACK_SIZE = 32;
880 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, LPD3DXMATRIXSTACK *ppstack)
882 struct ID3DXMatrixStackImpl *object;
884 TRACE("flags %#x, ppstack %p\n", flags, ppstack);
886 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
887 if (object == NULL)
889 *ppstack = NULL;
890 return E_OUTOFMEMORY;
892 object->ID3DXMatrixStack_iface.lpVtbl = &ID3DXMatrixStack_Vtbl;
893 object->ref = 1;
895 object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(*object->stack));
896 if (!object->stack)
898 HeapFree(GetProcessHeap(), 0, object);
899 *ppstack = NULL;
900 return E_OUTOFMEMORY;
903 object->current = 0;
904 object->stack_size = INITIAL_STACK_SIZE;
905 D3DXMatrixIdentity(&object->stack[0]);
907 TRACE("Created matrix stack %p\n", object);
909 *ppstack = &object->ID3DXMatrixStack_iface;
910 return D3D_OK;
913 static inline struct ID3DXMatrixStackImpl *impl_from_ID3DXMatrixStack(ID3DXMatrixStack *iface)
915 return CONTAINING_RECORD(iface, struct ID3DXMatrixStackImpl, ID3DXMatrixStack_iface);
918 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **out)
920 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
922 if (IsEqualGUID(riid, &IID_ID3DXMatrixStack)
923 || IsEqualGUID(riid, &IID_IUnknown))
925 ID3DXMatrixStack_AddRef(iface);
926 *out = iface;
927 return S_OK;
930 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
932 *out = NULL;
933 return E_NOINTERFACE;
936 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
938 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
939 ULONG ref = InterlockedIncrement(&This->ref);
940 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
941 return ref;
944 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack *iface)
946 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
947 ULONG ref = InterlockedDecrement(&This->ref);
948 if (!ref)
950 HeapFree(GetProcessHeap(), 0, This->stack);
951 HeapFree(GetProcessHeap(), 0, This);
953 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
954 return ref;
957 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
959 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
961 TRACE("iface %p\n", iface);
963 return &This->stack[This->current];
966 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
968 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
970 TRACE("iface %p\n", iface);
972 D3DXMatrixIdentity(&This->stack[This->current]);
974 return D3D_OK;
977 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
979 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
981 TRACE("iface %p\n", iface);
983 This->stack[This->current] = *pm;
985 return D3D_OK;
988 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
990 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
992 TRACE("iface %p\n", iface);
994 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], pm);
996 return D3D_OK;
999 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, CONST D3DXMATRIX *pm)
1001 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1003 TRACE("iface %p\n", iface);
1005 D3DXMatrixMultiply(&This->stack[This->current], pm, &This->stack[This->current]);
1007 return D3D_OK;
1010 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
1012 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1014 TRACE("iface %p\n", iface);
1016 /* Popping the last element on the stack returns D3D_OK, but does nothing. */
1017 if (!This->current) return D3D_OK;
1019 if (This->current <= This->stack_size / 4 && This->stack_size >= INITIAL_STACK_SIZE * 2)
1021 unsigned int new_size;
1022 D3DXMATRIX *new_stack;
1024 new_size = This->stack_size / 2;
1025 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1026 if (new_stack)
1028 This->stack_size = new_size;
1029 This->stack = new_stack;
1033 --This->current;
1035 return D3D_OK;
1038 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
1040 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1042 TRACE("iface %p\n", iface);
1044 if (This->current == This->stack_size - 1)
1046 unsigned int new_size;
1047 D3DXMATRIX *new_stack;
1049 if (This->stack_size > UINT_MAX / 2) return E_OUTOFMEMORY;
1051 new_size = This->stack_size * 2;
1052 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1053 if (!new_stack) return E_OUTOFMEMORY;
1055 This->stack_size = new_size;
1056 This->stack = new_stack;
1059 ++This->current;
1060 This->stack[This->current] = This->stack[This->current - 1];
1062 return D3D_OK;
1065 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
1067 D3DXMATRIX temp;
1068 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1070 TRACE("iface %p\n", iface);
1072 D3DXMatrixRotationAxis(&temp, pv, angle);
1073 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1075 return D3D_OK;
1078 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, CONST D3DXVECTOR3 *pv, FLOAT angle)
1080 D3DXMATRIX temp;
1081 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1083 TRACE("iface %p\n", iface);
1085 D3DXMatrixRotationAxis(&temp, pv, angle);
1086 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1088 return D3D_OK;
1091 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1093 D3DXMATRIX temp;
1094 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1096 TRACE("iface %p\n", iface);
1098 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1099 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1101 return D3D_OK;
1104 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1106 D3DXMATRIX temp;
1107 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1109 TRACE("iface %p\n", iface);
1111 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1112 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1114 return D3D_OK;
1117 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1119 D3DXMATRIX temp;
1120 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1122 TRACE("iface %p\n", iface);
1124 D3DXMatrixScaling(&temp, x, y, z);
1125 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1127 return D3D_OK;
1130 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1132 D3DXMATRIX temp;
1133 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1135 TRACE("iface %p\n", iface);
1137 D3DXMatrixScaling(&temp, x, y, z);
1138 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1140 return D3D_OK;
1143 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1145 D3DXMATRIX temp;
1146 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1148 TRACE("iface %p\n", iface);
1150 D3DXMatrixTranslation(&temp, x, y, z);
1151 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1153 return D3D_OK;
1156 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1158 D3DXMATRIX temp;
1159 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1161 TRACE("iface %p\n", iface);
1163 D3DXMatrixTranslation(&temp, x, y, z);
1164 D3DXMatrixMultiply(&This->stack[This->current], &temp,&This->stack[This->current]);
1166 return D3D_OK;
1169 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
1171 ID3DXMatrixStackImpl_QueryInterface,
1172 ID3DXMatrixStackImpl_AddRef,
1173 ID3DXMatrixStackImpl_Release,
1174 ID3DXMatrixStackImpl_Pop,
1175 ID3DXMatrixStackImpl_Push,
1176 ID3DXMatrixStackImpl_LoadIdentity,
1177 ID3DXMatrixStackImpl_LoadMatrix,
1178 ID3DXMatrixStackImpl_MultMatrix,
1179 ID3DXMatrixStackImpl_MultMatrixLocal,
1180 ID3DXMatrixStackImpl_RotateAxis,
1181 ID3DXMatrixStackImpl_RotateAxisLocal,
1182 ID3DXMatrixStackImpl_RotateYawPitchRoll,
1183 ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
1184 ID3DXMatrixStackImpl_Scale,
1185 ID3DXMatrixStackImpl_ScaleLocal,
1186 ID3DXMatrixStackImpl_Translate,
1187 ID3DXMatrixStackImpl_TranslateLocal,
1188 ID3DXMatrixStackImpl_GetTop
1191 /*_________________D3DXPLANE________________*/
1193 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, CONST D3DXVECTOR3 *pvpoint, CONST D3DXVECTOR3 *pvnormal)
1195 TRACE("(%p, %p, %p)\n", pout, pvpoint, pvnormal);
1197 pout->a = pvnormal->x;
1198 pout->b = pvnormal->y;
1199 pout->c = pvnormal->z;
1200 pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
1201 return pout;
1204 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3)
1206 D3DXVECTOR3 edge1, edge2, normal, Nnormal;
1208 TRACE("(%p, %p, %p, %p)\n", pout, pv1, pv2, pv3);
1210 edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
1211 edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
1212 D3DXVec3Subtract(&edge1, pv2, pv1);
1213 D3DXVec3Subtract(&edge2, pv3, pv1);
1214 D3DXVec3Cross(&normal, &edge1, &edge2);
1215 D3DXVec3Normalize(&Nnormal, &normal);
1216 D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
1217 return pout;
1220 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, CONST D3DXPLANE *pp, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
1222 D3DXVECTOR3 direction, normal;
1223 FLOAT dot, temp;
1225 TRACE("(%p, %p, %p, %p)\n", pout, pp, pv1, pv2);
1227 normal.x = pp->a;
1228 normal.y = pp->b;
1229 normal.z = pp->c;
1230 direction.x = pv2->x - pv1->x;
1231 direction.y = pv2->y - pv1->y;
1232 direction.z = pv2->z - pv1->z;
1233 dot = D3DXVec3Dot(&normal, &direction);
1234 if ( !dot ) return NULL;
1235 temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
1236 pout->x = pv1->x - temp * direction.x;
1237 pout->y = pv1->y - temp * direction.y;
1238 pout->z = pv1->z - temp * direction.z;
1239 return pout;
1242 D3DXPLANE * WINAPI D3DXPlaneNormalize(D3DXPLANE *out, const D3DXPLANE *p)
1244 FLOAT norm;
1246 TRACE("out %p, p %p\n", out, p);
1248 norm = sqrtf(p->a * p->a + p->b * p->b + p->c * p->c);
1249 if (norm)
1251 out->a = p->a / norm;
1252 out->b = p->b / norm;
1253 out->c = p->c / norm;
1254 out->d = p->d / norm;
1256 else
1258 out->a = 0.0f;
1259 out->b = 0.0f;
1260 out->c = 0.0f;
1261 out->d = 0.0f;
1264 return out;
1267 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, CONST D3DXPLANE *pplane, CONST D3DXMATRIX *pm)
1269 CONST D3DXPLANE plane = *pplane;
1271 TRACE("(%p, %p, %p)\n", pout, pplane, pm);
1273 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;
1274 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;
1275 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;
1276 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;
1277 return pout;
1280 D3DXPLANE* WINAPI D3DXPlaneTransformArray(D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1282 UINT i;
1284 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1286 for (i = 0; i < elements; ++i) {
1287 D3DXPlaneTransform(
1288 (D3DXPLANE*)((char*)out + outstride * i),
1289 (CONST D3DXPLANE*)((const char*)in + instride * i),
1290 matrix);
1292 return out;
1295 /*_________________D3DXQUATERNION________________*/
1297 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
1299 D3DXQUATERNION temp1, temp2;
1301 TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pq1, pq2, pq3, f, g);
1303 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
1304 return pout;
1307 D3DXQUATERNION * WINAPI D3DXQuaternionExp(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1309 FLOAT norm;
1311 TRACE("out %p, q %p\n", out, q);
1313 norm = sqrtf(q->x * q->x + q->y * q->y + q->z * q->z);
1314 if (norm)
1316 out->x = sinf(norm) * q->x / norm;
1317 out->y = sinf(norm) * q->y / norm;
1318 out->z = sinf(norm) * q->z / norm;
1319 out->w = cosf(norm);
1321 else
1323 out->x = 0.0f;
1324 out->y = 0.0f;
1325 out->z = 0.0f;
1326 out->w = 1.0f;
1329 return out;
1332 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
1334 D3DXQUATERNION out;
1335 FLOAT norm;
1337 TRACE("(%p, %p)\n", pout, pq);
1339 norm = D3DXQuaternionLengthSq(pq);
1341 out.x = -pq->x / norm;
1342 out.y = -pq->y / norm;
1343 out.z = -pq->z / norm;
1344 out.w = pq->w / norm;
1346 *pout =out;
1347 return pout;
1350 D3DXQUATERNION * WINAPI D3DXQuaternionLn(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1352 FLOAT t;
1354 TRACE("out %p, q %p\n", out, q);
1356 if ((q->w >= 1.0f) || (q->w == -1.0f))
1357 t = 1.0f;
1358 else
1359 t = acosf(q->w) / sqrtf(1.0f - q->w * q->w);
1361 out->x = t * q->x;
1362 out->y = t * q->y;
1363 out->z = t * q->z;
1364 out->w = 0.0f;
1366 return out;
1369 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
1371 D3DXQUATERNION out;
1373 TRACE("(%p, %p, %p)\n", pout, pq1, pq2);
1375 out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
1376 out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
1377 out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
1378 out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
1379 *pout = out;
1380 return pout;
1383 D3DXQUATERNION * WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1385 FLOAT norm;
1387 TRACE("out %p, q %p\n", out, q);
1389 norm = D3DXQuaternionLength(q);
1391 out->x = q->x / norm;
1392 out->y = q->y / norm;
1393 out->z = q->z / norm;
1394 out->w = q->w / norm;
1396 return out;
1399 D3DXQUATERNION * WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *out, const D3DXVECTOR3 *v, FLOAT angle)
1401 D3DXVECTOR3 temp;
1403 TRACE("out %p, v %p, angle %f\n", out, v, angle);
1405 D3DXVec3Normalize(&temp, v);
1407 out->x = sinf(angle / 2.0f) * temp.x;
1408 out->y = sinf(angle / 2.0f) * temp.y;
1409 out->z = sinf(angle / 2.0f) * temp.z;
1410 out->w = cosf(angle / 2.0f);
1412 return out;
1415 D3DXQUATERNION * WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *out, const D3DXMATRIX *m)
1417 FLOAT s, trace;
1419 TRACE("out %p, m %p\n", out, m);
1421 trace = m->u.m[0][0] + m->u.m[1][1] + m->u.m[2][2] + 1.0f;
1422 if (trace > 1.0f)
1424 s = 2.0f * sqrtf(trace);
1425 out->x = (m->u.m[1][2] - m->u.m[2][1]) / s;
1426 out->y = (m->u.m[2][0] - m->u.m[0][2]) / s;
1427 out->z = (m->u.m[0][1] - m->u.m[1][0]) / s;
1428 out->w = 0.25f * s;
1430 else
1432 int i, maxi = 0;
1434 for (i = 1; i < 3; i++)
1436 if (m->u.m[i][i] > m->u.m[maxi][maxi])
1437 maxi = i;
1440 switch (maxi)
1442 case 0:
1443 s = 2.0f * sqrtf(1.0f + m->u.m[0][0] - m->u.m[1][1] - m->u.m[2][2]);
1444 out->x = 0.25f * s;
1445 out->y = (m->u.m[0][1] + m->u.m[1][0]) / s;
1446 out->z = (m->u.m[0][2] + m->u.m[2][0]) / s;
1447 out->w = (m->u.m[1][2] - m->u.m[2][1]) / s;
1448 break;
1450 case 1:
1451 s = 2.0f * sqrtf(1.0f + m->u.m[1][1] - m->u.m[0][0] - m->u.m[2][2]);
1452 out->x = (m->u.m[0][1] + m->u.m[1][0]) / s;
1453 out->y = 0.25f * s;
1454 out->z = (m->u.m[1][2] + m->u.m[2][1]) / s;
1455 out->w = (m->u.m[2][0] - m->u.m[0][2]) / s;
1456 break;
1458 case 2:
1459 s = 2.0f * sqrtf(1.0f + m->u.m[2][2] - m->u.m[0][0] - m->u.m[1][1]);
1460 out->x = (m->u.m[0][2] + m->u.m[2][0]) / s;
1461 out->y = (m->u.m[1][2] + m->u.m[2][1]) / s;
1462 out->z = 0.25f * s;
1463 out->w = (m->u.m[0][1] - m->u.m[1][0]) / s;
1464 break;
1468 return out;
1471 D3DXQUATERNION * WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *out, FLOAT yaw, FLOAT pitch, FLOAT roll)
1473 FLOAT syaw, cyaw, spitch, cpitch, sroll, croll;
1475 TRACE("out %p, yaw %f, pitch %f, roll %f\n", out, yaw, pitch, roll);
1477 syaw = sinf(yaw / 2.0f);
1478 cyaw = cosf(yaw / 2.0f);
1479 spitch = sinf(pitch / 2.0f);
1480 cpitch = cosf(pitch / 2.0f);
1481 sroll = sinf(roll / 2.0f);
1482 croll = cosf(roll / 2.0f);
1484 out->x = syaw * cpitch * sroll + cyaw * spitch * croll;
1485 out->y = syaw * cpitch * croll - cyaw * spitch * sroll;
1486 out->z = cyaw * cpitch * sroll - syaw * spitch * croll;
1487 out->w = cyaw * cpitch * croll + syaw * spitch * sroll;
1489 return out;
1492 D3DXQUATERNION * WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *out, const D3DXQUATERNION *q1,
1493 const D3DXQUATERNION *q2, FLOAT t)
1495 FLOAT dot, temp;
1497 TRACE("out %p, q1 %p, q2 %p, t %f\n", out, q1, q2, t);
1499 temp = 1.0f - t;
1500 dot = D3DXQuaternionDot(q1, q2);
1501 if (dot < 0.0f)
1503 t = -t;
1504 dot = -dot;
1507 if (1.0f - dot > 0.001f)
1509 FLOAT theta = acosf(dot);
1511 temp = sinf(theta * temp) / sinf(theta);
1512 t = sinf(theta * t) / sinf(theta);
1515 out->x = temp * q1->x + t * q2->x;
1516 out->y = temp * q1->y + t * q2->y;
1517 out->z = temp * q1->z + t * q2->z;
1518 out->w = temp * q1->w + t * q2->w;
1520 return out;
1523 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, CONST D3DXQUATERNION *pq4, FLOAT t)
1525 D3DXQUATERNION temp1, temp2;
1527 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pq1, pq2, pq3, pq4, t);
1529 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1530 return pout;
1533 static D3DXQUATERNION add_diff(CONST D3DXQUATERNION *q1, CONST D3DXQUATERNION *q2, CONST FLOAT add)
1535 D3DXQUATERNION temp;
1537 temp.x = q1->x + add * q2->x;
1538 temp.y = q1->y + add * q2->y;
1539 temp.z = q1->z + add * q2->z;
1540 temp.w = q1->w + add * q2->w;
1542 return temp;
1545 void WINAPI D3DXQuaternionSquadSetup(D3DXQUATERNION *paout, D3DXQUATERNION *pbout, D3DXQUATERNION *pcout, CONST D3DXQUATERNION *pq0, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3)
1547 D3DXQUATERNION q, temp1, temp2, temp3, zero;
1549 TRACE("(%p, %p, %p, %p, %p, %p, %p)\n", paout, pbout, pcout, pq0, pq1, pq2, pq3);
1551 zero.x = 0.0f;
1552 zero.y = 0.0f;
1553 zero.z = 0.0f;
1554 zero.w = 0.0f;
1556 if ( D3DXQuaternionDot(pq0, pq1) < 0.0f )
1557 temp2 = add_diff(&zero, pq0, -1.0f);
1558 else
1559 temp2 = *pq0;
1561 if ( D3DXQuaternionDot(pq1, pq2) < 0.0f )
1562 *pcout = add_diff(&zero, pq2, -1.0f);
1563 else
1564 *pcout = *pq2;
1566 if ( D3DXQuaternionDot(pcout, pq3) < 0.0f )
1567 temp3 = add_diff(&zero, pq3, -1.0f);
1568 else
1569 temp3 = *pq3;
1571 D3DXQuaternionInverse(&temp1, pq1);
1572 D3DXQuaternionMultiply(&temp2, &temp1, &temp2);
1573 D3DXQuaternionLn(&temp2, &temp2);
1574 D3DXQuaternionMultiply(&q, &temp1, pcout);
1575 D3DXQuaternionLn(&q, &q);
1576 temp1 = add_diff(&temp2, &q, 1.0f);
1577 temp1.x *= -0.25f;
1578 temp1.y *= -0.25f;
1579 temp1.z *= -0.25f;
1580 temp1.w *= -0.25f;
1581 D3DXQuaternionExp(&temp1, &temp1);
1582 D3DXQuaternionMultiply(paout, pq1, &temp1);
1584 D3DXQuaternionInverse(&temp1, pcout);
1585 D3DXQuaternionMultiply(&temp2, &temp1, pq1);
1586 D3DXQuaternionLn(&temp2, &temp2);
1587 D3DXQuaternionMultiply(&q, &temp1, &temp3);
1588 D3DXQuaternionLn(&q, &q);
1589 temp1 = add_diff(&temp2, &q, 1.0f);
1590 temp1.x *= -0.25f;
1591 temp1.y *= -0.25f;
1592 temp1.z *= -0.25f;
1593 temp1.w *= -0.25f;
1594 D3DXQuaternionExp(&temp1, &temp1);
1595 D3DXQuaternionMultiply(pbout, pcout, &temp1);
1597 return;
1600 void WINAPI D3DXQuaternionToAxisAngle(CONST D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1602 TRACE("(%p, %p, %p)\n", pq, paxis, pangle);
1604 paxis->x = pq->x;
1605 paxis->y = pq->y;
1606 paxis->z = pq->z;
1607 *pangle = 2.0f * acos(pq->w);
1610 /*_________________D3DXVec2_____________________*/
1612 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1614 TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pv1, pv2, pv3, f, g);
1616 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1617 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1618 return pout;
1621 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv0, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT s)
1623 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv0, pv1, pv2, pv3, s);
1625 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);
1626 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);
1627 return pout;
1630 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pt1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pt2, FLOAT s)
1632 FLOAT h1, h2, h3, h4;
1634 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv1, pt1, pv2, pt2, s);
1636 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1637 h2 = s * s * s - 2.0f * s * s + s;
1638 h3 = -2.0f * s * s * s + 3.0f * s * s;
1639 h4 = s * s * s - s * s;
1641 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1642 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1643 return pout;
1646 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
1648 FLOAT norm;
1650 TRACE("(%p, %p)\n", pout, pv);
1652 norm = D3DXVec2Length(pv);
1653 if ( !norm )
1655 pout->x = 0.0f;
1656 pout->y = 0.0f;
1658 else
1660 pout->x = pv->x / norm;
1661 pout->y = pv->y / norm;
1664 return pout;
1667 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1669 TRACE("(%p, %p, %p)\n", pout, pv, pm);
1671 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
1672 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
1673 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
1674 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1675 return pout;
1678 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1680 UINT i;
1682 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1684 for (i = 0; i < elements; ++i) {
1685 D3DXVec2Transform(
1686 (D3DXVECTOR4*)((char*)out + outstride * i),
1687 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1688 matrix);
1690 return out;
1693 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1695 D3DXVECTOR2 v;
1696 FLOAT norm;
1698 TRACE("(%p, %p, %p)\n", pout, pv, pm);
1700 v = *pv;
1701 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1703 pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1704 pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1706 return pout;
1709 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1711 UINT i;
1713 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1715 for (i = 0; i < elements; ++i) {
1716 D3DXVec2TransformCoord(
1717 (D3DXVECTOR2*)((char*)out + outstride * i),
1718 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1719 matrix);
1721 return out;
1724 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
1726 CONST D3DXVECTOR2 v = *pv;
1727 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1728 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1729 return pout;
1732 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride, CONST D3DXMATRIX *matrix, UINT elements)
1734 UINT i;
1736 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1738 for (i = 0; i < elements; ++i) {
1739 D3DXVec2TransformNormal(
1740 (D3DXVECTOR2*)((char*)out + outstride * i),
1741 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
1742 matrix);
1744 return out;
1747 /*_________________D3DXVec3_____________________*/
1749 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1751 TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pv1, pv2, pv3, f, g);
1753 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1754 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1755 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1756 return pout;
1759 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
1761 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv0, pv1, pv2, pv3, s);
1763 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);
1764 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);
1765 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);
1766 return pout;
1769 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
1771 FLOAT h1, h2, h3, h4;
1773 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv1, pt1, pv2, pt2, s);
1775 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1776 h2 = s * s * s - 2.0f * s * s + s;
1777 h3 = -2.0f * s * s * s + 3.0f * s * s;
1778 h4 = s * s * s - s * s;
1780 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1781 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1782 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1783 return pout;
1786 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
1788 FLOAT norm;
1790 TRACE("(%p, %p)\n", pout, pv);
1792 norm = D3DXVec3Length(pv);
1793 if ( !norm )
1795 pout->x = 0.0f;
1796 pout->y = 0.0f;
1797 pout->z = 0.0f;
1799 else
1801 pout->x = pv->x / norm;
1802 pout->y = pv->y / norm;
1803 pout->z = pv->z / norm;
1806 return pout;
1809 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1811 D3DXMATRIX m;
1812 D3DXVECTOR3 out;
1814 TRACE("(%p, %p, %p, %p, %p, %p)\n", pout, pv, pviewport, pprojection, pview, pworld);
1816 D3DXMatrixMultiply(&m, pworld, pview);
1817 D3DXMatrixMultiply(&m, &m, pprojection);
1818 D3DXVec3TransformCoord(&out, pv, &m);
1819 out.x = pviewport->X + ( 1.0f + out.x ) * pviewport->Width / 2.0f;
1820 out.y = pviewport->Y + ( 1.0f - out.y ) * pviewport->Height / 2.0f;
1821 out.z = pviewport->MinZ + out.z * ( pviewport->MaxZ - pviewport->MinZ );
1822 *pout = out;
1823 return pout;
1826 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)
1828 UINT i;
1830 TRACE("(%p, %u, %p, %u, %p, %p, %p, %p, %u)\n", out, outstride, in, instride, viewport, projection, view, world, elements);
1832 for (i = 0; i < elements; ++i) {
1833 D3DXVec3Project(
1834 (D3DXVECTOR3*)((char*)out + outstride * i),
1835 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1836 viewport, projection, view, world);
1838 return out;
1841 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1843 TRACE("(%p, %p, %p)\n", pout, pv, pm);
1845 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];
1846 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];
1847 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];
1848 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];
1849 return pout;
1852 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1854 UINT i;
1856 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1858 for (i = 0; i < elements; ++i) {
1859 D3DXVec3Transform(
1860 (D3DXVECTOR4*)((char*)out + outstride * i),
1861 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1862 matrix);
1864 return out;
1867 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1869 D3DXVECTOR3 out;
1870 FLOAT norm;
1872 TRACE("(%p, %p, %p)\n", pout, pv, pm);
1874 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];
1876 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;
1877 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;
1878 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;
1880 *pout = out;
1882 return pout;
1885 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1887 UINT i;
1889 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1891 for (i = 0; i < elements; ++i) {
1892 D3DXVec3TransformCoord(
1893 (D3DXVECTOR3*)((char*)out + outstride * i),
1894 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1895 matrix);
1897 return out;
1900 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
1902 CONST D3DXVECTOR3 v = *pv;
1904 TRACE("(%p, %p, %p)\n", pout, pv, pm);
1906 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1907 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1908 pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1909 return pout;
1913 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
1915 UINT i;
1917 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
1919 for (i = 0; i < elements; ++i) {
1920 D3DXVec3TransformNormal(
1921 (D3DXVECTOR3*)((char*)out + outstride * i),
1922 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1923 matrix);
1925 return out;
1928 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT9 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
1930 D3DXMATRIX m;
1932 TRACE("(%p, %p, %p, %p, %p, %p)\n", pout, pv, pviewport, pprojection, pview, pworld);
1934 if (pworld)
1936 D3DXMatrixMultiply(&m, pworld, pview);
1937 D3DXMatrixMultiply(&m, &m, pprojection);
1939 else
1941 D3DXMatrixMultiply(&m, pview, pprojection);
1943 D3DXMatrixInverse(&m, NULL, &m);
1944 *pout = *pv;
1945 if (pviewport)
1947 pout->x = 2.0f * ( pout->x - pviewport->X ) / pviewport->Width - 1.0f;
1948 pout->y = 1.0f - 2.0f * ( pout->y - pviewport->Y ) / pviewport->Height;
1949 pout->z = ( pout->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
1951 D3DXVec3TransformCoord(pout, pout, &m);
1952 return pout;
1955 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)
1957 UINT i;
1959 TRACE("(%p, %u, %p, %u, %p, %p, %p, %p, %u)\n", out, outstride, in, instride, viewport, projection, view, world, elements);
1961 for (i = 0; i < elements; ++i) {
1962 D3DXVec3Unproject(
1963 (D3DXVECTOR3*)((char*)out + outstride * i),
1964 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
1965 viewport, projection, view, world);
1967 return out;
1970 /*_________________D3DXVec4_____________________*/
1972 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
1974 TRACE("(%p, %p, %p, %p, %f, %f)\n", pout, pv1, pv2, pv3, f, g);
1976 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1977 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1978 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1979 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
1980 return pout;
1983 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv0, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT s)
1985 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv0, pv1, pv2, pv3, s);
1987 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);
1988 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);
1989 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);
1990 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);
1991 return pout;
1994 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
1996 D3DXVECTOR4 out;
1998 TRACE("(%p, %p, %p, %p)\n", pout, pv1, pv2, pv3);
2000 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);
2001 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));
2002 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);
2003 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));
2004 *pout = out;
2005 return pout;
2008 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pt1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pt2, FLOAT s)
2010 FLOAT h1, h2, h3, h4;
2012 TRACE("(%p, %p, %p, %p, %p, %f)\n", pout, pv1, pt1, pv2, pt2, s);
2014 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
2015 h2 = s * s * s - 2.0f * s * s + s;
2016 h3 = -2.0f * s * s * s + 3.0f * s * s;
2017 h4 = s * s * s - s * s;
2019 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
2020 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
2021 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
2022 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
2023 return pout;
2026 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
2028 FLOAT norm;
2030 TRACE("(%p, %p)\n", pout, pv);
2032 norm = D3DXVec4Length(pv);
2034 pout->x = pv->x / norm;
2035 pout->y = pv->y / norm;
2036 pout->z = pv->z / norm;
2037 pout->w = pv->w / norm;
2039 return pout;
2042 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
2044 D3DXVECTOR4 out;
2046 TRACE("(%p, %p, %p)\n", pout, pv, pm);
2048 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;
2049 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;
2050 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;
2051 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;
2052 *pout = out;
2053 return pout;
2056 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride, CONST D3DXMATRIX* matrix, UINT elements)
2058 UINT i;
2060 TRACE("(%p, %u, %p, %u, %p, %u)\n", out, outstride, in, instride, matrix, elements);
2062 for (i = 0; i < elements; ++i) {
2063 D3DXVec4Transform(
2064 (D3DXVECTOR4*)((char*)out + outstride * i),
2065 (CONST D3DXVECTOR4*)((const char*)in + instride * i),
2066 matrix);
2068 return out;
2071 static inline unsigned short float_32_to_16(const float in)
2073 int exp = 0, origexp;
2074 float tmp = fabs(in);
2075 int sign = (copysignf(1, in) < 0);
2076 unsigned int mantissa;
2077 unsigned short ret;
2079 /* Deal with special numbers */
2080 if (isinf(in)) return (sign ? 0xffff : 0x7fff);
2081 if (isnan(in)) return (sign ? 0xffff : 0x7fff);
2082 if (in == 0.0f) return (sign ? 0x8000 : 0x0000);
2084 if (tmp < powf(2, 10))
2088 tmp *= 2.0f;
2089 exp--;
2090 } while (tmp < powf(2, 10));
2092 else if (tmp >= powf(2, 11))
2096 tmp /= 2.0f;
2097 exp++;
2098 } while (tmp >= powf(2, 11));
2101 exp += 10; /* Normalize the mantissa */
2102 exp += 15; /* Exponent is encoded with excess 15 */
2104 origexp = exp;
2106 mantissa = (unsigned int) tmp;
2107 if ((tmp - mantissa == 0.5f && mantissa % 2 == 1) || /* round half to even */
2108 (tmp - mantissa > 0.5f))
2110 mantissa++; /* round to nearest, away from zero */
2112 if (mantissa == 2048)
2114 mantissa = 1024;
2115 exp++;
2118 if (exp > 31)
2120 /* too big */
2121 ret = 0x7fff; /* INF */
2123 else if (exp <= 0)
2125 unsigned int rounding = 0;
2127 /* Denormalized half float */
2129 /* return 0x0000 (=0.0) for numbers too small to represent in half floats */
2130 if (exp < -11)
2131 return (sign ? 0x8000 : 0x0000);
2133 exp = origexp;
2135 /* the 13 extra bits from single precision are used for rounding */
2136 mantissa = (unsigned int)(tmp * powf(2, 13));
2137 mantissa >>= 1 - exp; /* denormalize */
2139 mantissa -= ~(mantissa >> 13) & 1; /* round half to even */
2140 /* remove 13 least significant bits to get half float precision */
2141 mantissa >>= 12;
2142 rounding = mantissa & 1;
2143 mantissa >>= 1;
2145 ret = mantissa + rounding;
2147 else
2149 ret = (exp << 10) | (mantissa & 0x3ff);
2152 ret |= ((sign ? 1 : 0) << 15); /* Add the sign */
2153 return ret;
2156 D3DXFLOAT16 *WINAPI D3DXFloat32To16Array(D3DXFLOAT16 *pout, CONST FLOAT *pin, UINT n)
2158 unsigned int i;
2160 TRACE("(%p, %p, %u)\n", pout, pin, n);
2162 for (i = 0; i < n; ++i)
2164 pout[i].value = float_32_to_16(pin[i]);
2167 return pout;
2170 /* Native d3dx9's D3DXFloat16to32Array lacks support for NaN and Inf. Specifically, e = 16 is treated as a
2171 * regular number - e.g., 0x7fff is converted to 131008.0 and 0xffff to -131008.0. */
2172 static inline float float_16_to_32(const unsigned short in)
2174 const unsigned short s = (in & 0x8000);
2175 const unsigned short e = (in & 0x7C00) >> 10;
2176 const unsigned short m = in & 0x3FF;
2177 const float sgn = (s ? -1.0f : 1.0f);
2179 if (e == 0)
2181 if (m == 0) return sgn * 0.0f; /* +0.0 or -0.0 */
2182 else return sgn * powf(2, -14.0f) * (m / 1024.0f);
2184 else
2186 return sgn * powf(2, e - 15.0f) * (1.0f + (m / 1024.0f));
2190 FLOAT *WINAPI D3DXFloat16To32Array(FLOAT *pout, CONST D3DXFLOAT16 *pin, UINT n)
2192 unsigned int i;
2194 TRACE("(%p, %p, %u)\n", pout, pin, n);
2196 for (i = 0; i < n; ++i)
2198 pout[i] = float_16_to_32(pin[i].value);
2201 return pout;
2204 /*_________________D3DXSH________________*/
2206 FLOAT* WINAPI D3DXSHAdd(FLOAT *out, UINT order, const FLOAT *a, const FLOAT *b)
2208 UINT i;
2210 TRACE("out %p, order %u, a %p, b %p\n", out, order, a, b);
2212 for (i = 0; i < order * order; i++)
2213 out[i] = a[i] + b[i];
2215 return out;
2218 FLOAT WINAPI D3DXSHDot(UINT order, CONST FLOAT *a, CONST FLOAT *b)
2220 FLOAT s;
2221 UINT i;
2223 TRACE("order %u, a %p, b %p\n", order, a, b);
2225 s = a[0] * b[0];
2226 for (i = 1; i < order * order; i++)
2227 s += a[i] * b[i];
2229 return s;
2232 FLOAT* WINAPI D3DXSHEvalDirection(FLOAT *out, UINT order, CONST D3DXVECTOR3 *dir)
2235 TRACE("(%p, %u, %p)\n", out, order, dir);
2237 if ( (order < D3DXSH_MINORDER) || (order > D3DXSH_MAXORDER) )
2238 return out;
2240 out[0] = 0.5f / sqrt(D3DX_PI);
2241 out[1] = -0.5f / sqrt(D3DX_PI / 3.0f) * dir->y;
2242 out[2] = 0.5f / sqrt(D3DX_PI / 3.0f) * dir->z;
2243 out[3] = -0.5f / sqrt(D3DX_PI / 3.0f) * dir->x;
2244 if ( order == 2 )
2245 return out;
2247 out[4] = 0.5f / sqrt(D3DX_PI / 15.0f) * dir->x * dir->y;
2248 out[5] = -0.5f / sqrt(D3DX_PI / 15.0f) * dir->y * dir->z;
2249 out[6] = 0.25f / sqrt(D3DX_PI / 5.0f) * ( 3.0f * dir->z * dir->z - 1.0f );
2250 out[7] = -0.5f / sqrt(D3DX_PI / 15.0f) * dir->x * dir->z;
2251 out[8] = 0.25f / sqrt(D3DX_PI / 15.0f) * ( dir->x * dir->x - dir->y * dir->y );
2252 if ( order == 3 )
2253 return out;
2255 out[9] = -sqrt(70.0f / D3DX_PI) / 8.0f * dir->y * (3.0f * dir->x * dir->x - dir->y * dir->y );
2256 out[10] = sqrt(105.0f / D3DX_PI) / 2.0f * dir->x * dir->y * dir->z;
2257 out[11] = -sqrt(42.0 / D3DX_PI) / 8.0f * dir->y * ( -1.0f + 5.0f * dir->z * dir->z );
2258 out[12] = sqrt(7.0f / D3DX_PI) / 4.0f * dir->z * ( 5.0f * dir->z * dir->z - 3.0f );
2259 out[13] = sqrt(42.0 / D3DX_PI) / 8.0f * dir->x * ( 1.0f - 5.0f * dir->z * dir->z );
2260 out[14] = sqrt(105.0f / D3DX_PI) / 4.0f * dir->z * ( dir->x * dir->x - dir->y * dir->y );
2261 out[15] = -sqrt(70.0f / D3DX_PI) / 8.0f * dir->x * ( dir->x * dir->x - 3.0f * dir->y * dir->y );
2262 if ( order == 4 )
2263 return out;
2265 out[16] = 0.75f * sqrt(35.0f / D3DX_PI) * dir->x * dir->y * (dir->x * dir->x - dir->y * dir->y );
2266 out[17] = 3.0f * dir->z * out[9];
2267 out[18] = 0.75f * sqrt(5.0f / D3DX_PI) * dir->x * dir->y * ( 7.0f * dir->z * dir->z - 1.0f );
2268 out[19] = 0.375f * sqrt(10.0f / D3DX_PI) * dir->y * dir->z * ( 3.0f - 7.0f * dir->z * dir->z );
2269 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 );
2270 out[21] = 0.375f * sqrt(10.0f / D3DX_PI) * dir->x * dir->z * ( 3.0f - 7.0f * dir->z * dir->z );
2271 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);
2272 out[23] = 3.0 * dir->z * out[15];
2273 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 );
2274 if ( order == 5 )
2275 return out;
2277 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 );
2278 out[26] = 0.75f * sqrt(385.0f / D3DX_PI) * dir->x * dir->y * dir->z * ( dir->x * dir->x - dir->y * dir->y );
2279 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 );
2280 out[28] = sqrt(1155.0f / D3DX_PI) / 4.0f * dir->x * dir->y * dir->z * ( 3.0f * dir->z * dir->z - 1.0f);
2281 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 );
2282 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 );
2283 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 );
2284 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 );
2285 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 );
2286 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 );
2287 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 );
2289 return out;
2292 HRESULT WINAPI D3DXSHEvalDirectionalLight(UINT order, CONST D3DXVECTOR3 *dir, FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *Rout, FLOAT *Gout, FLOAT *Bout)
2294 FLOAT s, temp;
2295 UINT j;
2297 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);
2299 s = 0.75f;
2300 if ( order > 2 )
2301 s += 5.0f / 16.0f;
2302 if ( order > 4 )
2303 s -= 3.0f / 32.0f;
2304 s /= D3DX_PI;
2306 D3DXSHEvalDirection(Rout, order, dir);
2307 for (j = 0; j < order * order; j++)
2309 temp = Rout[j] / s;
2311 Rout[j] = Rintensity * temp;
2312 if ( Gout )
2313 Gout[j] = Gintensity * temp;
2314 if ( Bout )
2315 Bout[j] = Bintensity * temp;
2318 return D3D_OK;
2321 FLOAT * WINAPI D3DXSHMultiply2(FLOAT *out, const FLOAT *a, const FLOAT *b)
2323 FLOAT ta, tb;
2325 TRACE("out %p, a %p, b %p\n", out, a, b);
2327 ta = 0.28209479f * a[0];
2328 tb = 0.28209479f * b[0];
2330 out[0] = 0.28209479f * D3DXSHDot(2, a, b);
2331 out[1] = ta * b[1] + tb * a[1];
2332 out[2] = ta * b[2] + tb * a[2];
2333 out[3] = ta * b[3] + tb * a[3];
2335 return out;
2338 FLOAT * WINAPI D3DXSHMultiply3(FLOAT *out, const FLOAT *a, const FLOAT *b)
2340 FLOAT t, ta, tb;
2342 TRACE("out %p, a %p, b %p\n", out, a, b);
2344 out[0] = 0.28209479f * a[0] * b[0];
2346 ta = 0.28209479f * a[0] - 0.12615662f * a[6] - 0.21850968f * a[8];
2347 tb = 0.28209479f * b[0] - 0.12615662f * b[6] - 0.21850968f * b[8];
2348 out[1] = ta * b[1] + tb * a[1];
2349 t = a[1] * b[1];
2350 out[0] += 0.28209479f * t;
2351 out[6] = -0.12615662f * t;
2352 out[8] = -0.21850968f * t;
2354 ta = 0.21850968f * a[5];
2355 tb = 0.21850968f * b[5];
2356 out[1] += ta * b[2] + tb * a[2];
2357 out[2] = ta * b[1] + tb * a[1];
2358 t = a[1] * b[2] +a[2] * b[1];
2359 out[5] = 0.21850968f * t;
2361 ta = 0.21850968f * a[4];
2362 tb = 0.21850968f * b[4];
2363 out[1] += ta * b[3] + tb * a[3];
2364 out[3] = ta * b[1] + tb * a[1];
2365 t = a[1] * b[3] + a[3] * b[1];
2366 out[4] = 0.21850968f * t;
2368 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2369 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2370 out[2] += ta * b[2] + tb * a[2];
2371 t = a[2] * b[2];
2372 out[0] += 0.28209480f * t;
2373 out[6] += 0.25231326f * t;
2375 ta = 0.21850969f * a[7];
2376 tb = 0.21850969f * b[7];
2377 out[2] += ta * b[3] + tb * a[3];
2378 out[3] += ta * b[2] + tb * a[2];
2379 t = a[2] * b[3] + a[3] * b[2];
2380 out[7] = 0.21850969f * t;
2382 ta = 0.28209479f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2383 tb = 0.28209479f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2384 out[3] += ta * b[3] + tb * a[3];
2385 t = a[3] * b[3];
2386 out[0] += 0.28209479f * t;
2387 out[6] -= 0.12615663f * t;
2388 out[8] += 0.21850969f * t;
2390 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2391 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2392 out[4] += ta * b[4] + tb * a[4];
2393 t = a[4] * b[4];
2394 out[0] += 0.28209479f * t;
2395 out[6] -= 0.18022375f * t;
2397 ta = 0.15607835f * a[7];
2398 tb = 0.15607835f * b[7];
2399 out[4] += ta * b[5] + tb * a[5];
2400 out[5] += ta * b[4] + tb * a[4];
2401 t = a[4] * b[5] + a[5] * b[4];
2402 out[7] += 0.15607834f * t;
2404 ta = 0.28209479f * a[0] + 0.09011186 * a[6] - 0.15607835f * a[8];
2405 tb = 0.28209479f * b[0] + 0.09011186 * b[6] - 0.15607835f * b[8];
2406 out[5] += ta * b[5] + tb * a[5];
2407 t = a[5] * b[5];
2408 out[0] += 0.28209479f * t;
2409 out[6] += 0.09011186f * t;
2410 out[8] -= 0.15607835f * t;
2412 ta = 0.28209480f * a[0];
2413 tb = 0.28209480f * b[0];
2414 out[6] += ta * b[6] + tb * a[6];
2415 t = a[6] * b[6];
2416 out[0] += 0.28209480f * t;
2417 out[6] += 0.18022376f * t;
2419 ta = 0.28209479f * a[0] + 0.09011186 * a[6] + 0.15607835f * a[8];
2420 tb = 0.28209479f * b[0] + 0.09011186 * b[6] + 0.15607835f * b[8];
2421 out[7] += ta * b[7] + tb * a[7];
2422 t = a[7] * b[7];
2423 out[0] += 0.28209479f * t;
2424 out[6] += 0.09011186f * t;
2425 out[8] += 0.15607835f * t;
2427 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2428 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2429 out[8] += ta * b[8] + tb * a[8];
2430 t = a[8] * b[8];
2431 out[0] += 0.28209479f * t;
2432 out[6] -= 0.18022375f * t;
2434 return out;
2437 FLOAT * WINAPI D3DXSHMultiply4(FLOAT *out, CONST FLOAT *a, CONST FLOAT *b)
2439 FLOAT ta, tb, t;
2441 TRACE("out %p, a %p, b %p\n", out, a, b);
2443 out[0] = 0.28209479f * a[0] * b[0];
2445 ta = 0.28209479f * a[0] - 0.12615663f * a[6] - 0.21850969f * a[8];
2446 tb = 0.28209479f * b[0] - 0.12615663f * b[6] - 0.21850969f * b[8];
2447 out[1] = ta * b[1] + tb * a[1];
2448 t = a[1] * b[1];
2449 out[0] += 0.28209479f * t;
2450 out[6] = -0.12615663f * t;
2451 out[8] = -0.21850969f * t;
2453 ta = 0.21850969f * a[3] - 0.05839917f * a[13] - 0.22617901f * a[15];
2454 tb = 0.21850969f * b[3] - 0.05839917f * b[13] - 0.22617901f * b[15];
2455 out[1] += ta * b[4] + tb * a[4];
2456 out[4] = ta * b[1] + tb * a[1];
2457 t = a[1] * b[4] + a[4] * b[1];
2458 out[3] = 0.21850969f * t;
2459 out[13] = -0.05839917f * t;
2460 out[15] = -0.22617901f * t;
2462 ta = 0.21850969f * a[2] - 0.14304817f * a[12] - 0.18467439f * a[14];
2463 tb = 0.21850969f * b[2] - 0.14304817f * b[12] - 0.18467439f * b[14];
2464 out[1] += ta * b[5] + tb * a[5];
2465 out[5] = ta * b[1] + tb * a[1];
2466 t = a[1] * b[5] + a[5] * b[1];
2467 out[2] = 0.21850969f * t;
2468 out[12] = -0.14304817f * t;
2469 out[14] = -0.18467439f * t;
2471 ta = 0.20230066f * a[11];
2472 tb = 0.20230066f * b[11];
2473 out[1] += ta * b[6] + tb * a[6];
2474 out[6] += ta * b[1] + tb * a[1];
2475 t = a[1] * b[6] + a[6] * b[1];
2476 out[11] = 0.20230066f * t;
2478 ta = 0.22617901f * a[9] + 0.05839917f * a[11];
2479 tb = 0.22617901f * b[9] + 0.05839917f * b[11];
2480 out[1] += ta * b[8] + tb * a[8];
2481 out[8] += ta * b[1] + tb * a[1];
2482 t = a[1] * b[8] + a[8] * b[1];
2483 out[9] = 0.22617901f * t;
2484 out[11] += 0.05839917f * t;
2486 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2487 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2488 out[2] += ta * b[2] + tb * a[2];
2489 t = a[2] * b[2];
2490 out[0] += 0.28209480f * t;
2491 out[6] += 0.25231326f * t;
2493 ta = 0.24776671f * a[12];
2494 tb = 0.24776671f * b[12];
2495 out[2] += ta * b[6] + tb * a[6];
2496 out[6] += ta * b[2] + tb * a[2];
2497 t = a[2] * b[6] + a[6] * b[2];
2498 out[12] += 0.24776671f * t;
2500 ta = 0.28209480f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2501 tb = 0.28209480f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2502 out[3] += ta * b[3] + tb * a[3];
2503 t = a[3] * b[3];
2504 out[0] += 0.28209480f * t;
2505 out[6] -= 0.12615663f * t;
2506 out[8] += 0.21850969f * t;
2508 ta = 0.20230066f * a[13];
2509 tb = 0.20230066f * b[13];
2510 out[3] += ta * b[6] + tb * a[6];
2511 out[6] += ta * b[3] + tb * a[3];
2512 t = a[3] * b[6] + a[6] * b[3];
2513 out[13] += 0.20230066f * t;
2515 ta = 0.21850969f * a[2] - 0.14304817f * a[12] + 0.18467439f * a[14];
2516 tb = 0.21850969f * b[2] - 0.14304817f * b[12] + 0.18467439f * b[14];
2517 out[3] += ta * b[7] + tb * a[7];
2518 out[7] = ta * b[3] + tb * a[3];
2519 t = a[3] * b[7] + a[7] * b[3];
2520 out[2] += 0.21850969f * t;
2521 out[12] -= 0.14304817f * t;
2522 out[14] += 0.18467439f * t;
2524 ta = -0.05839917f * a[13] + 0.22617901f * a[15];
2525 tb = -0.05839917f * b[13] + 0.22617901f * b[15];
2526 out[3] += ta * b[8] + tb * a[8];
2527 out[8] += ta * b[3] + tb * a[3];
2528 t = a[3] * b[8] + a[8] * b[3];
2529 out[13] -= 0.05839917f * t;
2530 out[15] += 0.22617901f * t;
2532 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2533 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2534 out[4] += ta * b[4] + tb * a[4];
2535 t = a[4] * b[4];
2536 out[0] += 0.28209479f * t;
2537 out[6] -= 0.18022375f * t;
2539 ta = 0.15607835f * a[7];
2540 tb = 0.15607835f * b[7];
2541 out[4] += ta * b[5] + tb * a[5];
2542 out[5] += ta * b[4] + tb * a[4];
2543 t = a[4] * b[5] + a[5] * b[4];
2544 out[7] += 0.15607835f * t;
2546 ta = 0.22617901f * a[3] - 0.09403160f * a[13];
2547 tb = 0.22617901f * b[3] - 0.09403160f * b[13];
2548 out[4] += ta * b[9] + tb * a[9];
2549 out[9] += ta * b[4] + tb * a[4];
2550 t = a[4] * b[9] + a[9] * b[4];
2551 out[3] += 0.22617901f * t;
2552 out[13] -= 0.09403160f * t;
2554 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2555 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2556 out[4] += ta * b[10] + tb * a [10];
2557 out[10] = ta * b[4] + tb * a[4];
2558 t = a[4] * b[10] + a[10] * b[4];
2559 out[2] += 0.18467439f * t;
2560 out[12] -= 0.18806319f * t;
2562 ta = -0.05839917f * a[3] + 0.14567312f * a[13] + 0.09403160f * a[15];
2563 tb = -0.05839917f * b[3] + 0.14567312f * b[13] + 0.09403160f * b[15];
2564 out[4] += ta * b[11] + tb * a[11];
2565 out[11] += ta * b[4] + tb * a[4];
2566 t = a[4] * b[11] + a[11] * b[4];
2567 out[3] -= 0.05839917f * t;
2568 out[13] += 0.14567312f * t;
2569 out[15] += 0.09403160f * t;
2571 ta = 0.28209479f * a[0] + 0.09011186f * a[6] - 0.15607835f * a[8];
2572 tb = 0.28209479f * b[0] + 0.09011186f * b[6] - 0.15607835f * b[8];
2573 out[5] += ta * b[5] + tb * a[5];
2574 t = a[5] * b[5];
2575 out[0] += 0.28209479f * t;
2576 out[6] += 0.09011186f * t;
2577 out[8] -= 0.15607835f * t;
2579 ta = 0.14867701f * a[14];
2580 tb = 0.14867701f * b[14];
2581 out[5] += ta * b[9] + tb * a[9];
2582 out[9] += ta * b[5] + tb * a[5];
2583 t = a[5] * b[9] + a[9] * b[5];
2584 out[14] += 0.14867701f * t;
2586 ta = 0.18467439f * a[3] + 0.11516472f * a[13] - 0.14867701f * a[15];
2587 tb = 0.18467439f * b[3] + 0.11516472f * b[13] - 0.14867701f * b[15];
2588 out[5] += ta * b[10] + tb * a[10];
2589 out[10] += ta * b[5] + tb * a[5];
2590 t = a[5] * b[10] + a[10] * b[5];
2591 out[3] += 0.18467439f * t;
2592 out[13] += 0.11516472f * t;
2593 out[15] -= 0.14867701f * t;
2595 ta = 0.23359668f * a[2] + 0.05947080f * a[12] - 0.11516472f * a[14];
2596 tb = 0.23359668f * b[2] + 0.05947080f * b[12] - 0.11516472f * b[14];
2597 out[5] += ta * b[11] + tb * a[11];
2598 out[11] += ta * b[5] + tb * a[5];
2599 t = a[5] * b[11] + a[11] * b[5];
2600 out[2] += 0.23359668f * t;
2601 out[12] += 0.05947080f * t;
2602 out[14] -= 0.11516472f * t;
2604 ta = 0.28209479f * a[0];
2605 tb = 0.28209479f * b[0];
2606 out[6] += ta * b[6] + tb * a[6];
2607 t = a[6] * b[6];
2608 out[0] += 0.28209479f * t;
2609 out[6] += 0.18022376f * t;
2611 ta = 0.09011186f * a[6] + 0.28209479f * a[0] + 0.15607835f * a[8];
2612 tb = 0.09011186f * b[6] + 0.28209479f * b[0] + 0.15607835f * b[8];
2613 out[7] += ta * b[7] + tb * a[7];
2614 t = a[7] * b[7];
2615 out[6] += 0.09011186f * t;
2616 out[0] += 0.28209479f * t;
2617 out[8] += 0.15607835f * t;
2619 ta = 0.14867701f * a[9] + 0.18467439f * a[1] + 0.11516472f * a[11];
2620 tb = 0.14867701f * b[9] + 0.18467439f * b[1] + 0.11516472f * b[11];
2621 out[7] += ta * b[10] + tb * a[10];
2622 out[10] += ta * b[7] + tb * a[7];
2623 t = a[7] * b[10] + a[10] * b[7];
2624 out[9] += 0.14867701f * t;
2625 out[1] += 0.18467439f * t;
2626 out[11] += 0.11516472f * t;
2628 ta = 0.05947080f * a[12] + 0.23359668f * a[2] + 0.11516472f * a[14];
2629 tb = 0.05947080f * b[12] + 0.23359668f * b[2] + 0.11516472f * b[14];
2630 out[7] += ta * b[13] + tb * a[13];
2631 out[13] += ta * b[7]+ tb * a[7];
2632 t = a[7] * b[13] + a[13] * b[7];
2633 out[12] += 0.05947080f * t;
2634 out[2] += 0.23359668f * t;
2635 out[14] += 0.11516472f * t;
2637 ta = 0.14867701f * a[15];
2638 tb = 0.14867701f * b[15];
2639 out[7] += ta * b[14] + tb * a[14];
2640 out[14] += ta * b[7] + tb * a[7];
2641 t = a[7] * b[14] + a[14] * b[7];
2642 out[15] += 0.14867701f * t;
2644 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2645 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2646 out[8] += ta * b[8] + tb * a[8];
2647 t = a[8] * b[8];
2648 out[0] += 0.28209479f * t;
2649 out[6] -= 0.18022375f * t;
2651 ta = -0.09403160f * a[11];
2652 tb = -0.09403160f * b[11];
2653 out[8] += ta * b[9] + tb * a[9];
2654 out[9] += ta * b[8] + tb * a[8];
2655 t = a[8] * b[9] + a[9] * b[8];
2656 out[11] -= 0.09403160f * t;
2658 ta = -0.09403160f * a[15];
2659 tb = -0.09403160f * b[15];
2660 out[8] += ta * b[13] + tb * a[13];
2661 out[13] += ta * b[8] + tb * a[8];
2662 t = a[8] * b[13] + a[13] * b[8];
2663 out[15] -= 0.09403160f * t;
2665 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2666 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2667 out[8] += ta * b[14] + tb * a[14];
2668 out[14] += ta * b[8] + tb * a[8];
2669 t = a[8] * b[14] + a[14] * b[8];
2670 out[2] += 0.18467439f * t;
2671 out[12] -= 0.18806319f * t;
2673 ta = -0.21026104f * a[6] + 0.28209479f * a[0];
2674 tb = -0.21026104f * b[6] + 0.28209479f * b[0];
2675 out[9] += ta * b[9] + tb * a[9];
2676 t = a[9] * b[9];
2677 out[6] -= 0.21026104f * t;
2678 out[0] += 0.28209479f * t;
2680 ta = 0.28209479f * a[0];
2681 tb = 0.28209479f * b[0];
2682 out[10] += ta * b[10] + tb * a[10];
2683 t = a[10] * b[10];
2684 out[0] += 0.28209479f * t;
2686 ta = 0.28209479f * a[0] + 0.12615663f * a[6] - 0.14567312f * a[8];
2687 tb = 0.28209479f * b[0] + 0.12615663f * b[6] - 0.14567312f * b[8];
2688 out[11] += ta * b[11] + tb * a[11];
2689 t = a[11] * b[11];
2690 out[0] += 0.28209479f * t;
2691 out[6] += 0.12615663f * t;
2692 out[8] -= 0.14567312f * t;
2694 ta = 0.28209479f * a[0] + 0.16820885f * a[6];
2695 tb = 0.28209479f * b[0] + 0.16820885f * b[6];
2696 out[12] += ta * b[12] + tb * a[12];
2697 t = a[12] * b[12];
2698 out[0] += 0.28209479f * t;
2699 out[6] += 0.16820885f * t;
2701 ta =0.28209479f * a[0] + 0.14567312f * a[8] + 0.12615663f * a[6];
2702 tb =0.28209479f * b[0] + 0.14567312f * b[8] + 0.12615663f * b[6];
2703 out[13] += ta * b[13] + tb * a[13];
2704 t = a[13] * b[13];
2705 out[0] += 0.28209479f * t;
2706 out[8] += 0.14567312f * t;
2707 out[6] += 0.12615663f * t;
2709 ta = 0.28209479f * a[0];
2710 tb = 0.28209479f * b[0];
2711 out[14] += ta * b[14] + tb * a[14];
2712 t = a[14] * b[14];
2713 out[0] += 0.28209479f * t;
2715 ta = 0.28209479f * a[0] - 0.21026104f * a[6];
2716 tb = 0.28209479f * b[0] - 0.21026104f * b[6];
2717 out[15] += ta * b[15] + tb * a[15];
2718 t = a[15] * b[15];
2719 out[0] += 0.28209479f * t;
2720 out[6] -= 0.21026104f * t;
2722 return out;
2725 static void rotate_X(FLOAT *out, UINT order, FLOAT a, FLOAT *in)
2727 out[0] = in[0];
2728 if ( order < 2 )
2729 return;
2731 out[1] = a * in[2];
2732 out[2] = -a * in[1];
2733 out[3] = in[3];
2734 if ( order == 2 )
2735 return;
2737 out[4] = a * in[7];
2738 out[5] = -in[5];
2739 out[6] = -0.5f * in[6] - 0.8660253882f * in[8];
2740 out[7] = -a * in[4];
2741 out[8] = -0.8660253882f * in[6] + 0.5f * in[8];
2742 out[9] = -a * 0.7905694842f * in[12] + a * 0.6123724580f * in[14];
2743 if ( order == 3 )
2744 return;
2746 out[10] = -in[10];
2747 out[11] = -a * 0.6123724580f * in[12] - a * 0.7905694842f * in[14];
2748 out[12] = a * 0.7905694842f * in[9] + a * 0.6123724580f * in[11];
2749 out[13] = -0.25f * in[13] - 0.9682458639f * in[15];
2750 out[14] = -a * 0.6123724580f * in[9] + a * 0.7905694842f * in[11];
2751 out[15] = -0.9682458639f * in[13] + 0.25f * in[15];
2752 if ( order == 4 )
2753 return;
2755 out[16] = -a * 0.9354143739f * in[21] + a * 0.3535533845f * in[23];
2756 out[17] = -0.75f * in[17] + 0.6614378095f * in[19];
2757 out[18] = -a * 0.3535533845f * in[21] - a * 0.9354143739f * in[23];
2758 out[19] = 0.6614378095f * in[17] + 0.75f * in[19];
2759 out[20] = 0.375f * in[20] + 0.5590170026f * in[22] + 0.7395099998f * in[24];
2760 out[21] = a * 0.9354143739f * in[16] + a * 0.3535533845f * in[18];
2761 out[22] = 0.5590170026f * in[20] + 0.5f * in[22] - 0.6614378691f * in[24];
2762 out[23] = -a * 0.3535533845f * in[16] + a * 0.9354143739f * in[18];
2763 out[24] = 0.7395099998f * in[20] - 0.6614378691f * in[22] + 0.125f * in[24];
2764 if ( order == 5 )
2765 return;
2767 out[25] = a * 0.7015607357f * in[30] - a * 0.6846531630f * in[32] + a * 0.1976423711f * in[34];
2768 out[26] = -0.5f * in[26] + 0.8660253882f * in[28];
2769 out[27] = a * 0.5229125023f * in[30] + a * 0.3061861992f * in[32] - a * 0.7954951525 * in[34];
2770 out[28] = 0.8660253882f * in[26] + 0.5f * in[28];
2771 out[29] = a * 0.4841229022f * in[30] + a * 0.6614378691f * in[32] + a * 0.5728219748f * in[34];
2772 out[30] = -a * 0.7015607357f * in[25] - a * 0.5229125023f * in[27] - a * 0.4841229022f * in[29];
2773 out[31] = 0.125f * in[31] + 0.4050463140f * in[33] + 0.9057110548f * in[35];
2774 out[32] = a * 0.6846531630f * in[25] - a * 0.3061861992f * in[27] - a * 0.6614378691f * in[29];
2775 out[33] = 0.4050463140f * in[31] + 0.8125f * in[33] - 0.4192627370f * in[35];
2776 out[34] = -a * 0.1976423711f * in[25] + a * 0.7954951525f * in[27] - a * 0.5728219748f * in[29];
2777 out[35] = 0.9057110548f * in[31] - 0.4192627370f * in[33] + 0.0624999329f * in[35];
2781 FLOAT* WINAPI D3DXSHRotate(FLOAT *out, UINT order, CONST D3DXMATRIX *matrix, CONST FLOAT *in)
2783 FLOAT alpha, beta, gamma, sinb, temp[36];
2785 TRACE("out %p, order %u, matrix %p, in %p\n", out, order, matrix, in);
2787 out[0] = in[0];
2789 if ( ( order > D3DXSH_MAXORDER ) || ( order < D3DXSH_MINORDER ) )
2790 return out;
2792 /* TODO: Implement handy computations for order <= 3. They are faster than the general algorithm. */
2793 if ( order < 4 )
2794 WARN("Using general algorithm for order = %u\n", order);
2796 if ( fabsf( matrix->u.m[2][2] ) != 1.0f )
2798 sinb = sqrtf( 1.0f - matrix->u.m[2][2] * matrix->u.m[2][2] );
2799 alpha = atan2f(matrix->u.m[2][1] / sinb, matrix->u.m[2][0] / sinb );
2800 beta = atan2f( sinb, matrix->u.m[2][2] );
2801 gamma = atan2f( matrix->u.m[1][2] / sinb, -matrix->u.m[0][2] / sinb );
2803 else
2805 alpha = atan2f( matrix->u.m[0][1], matrix->u.m[0][0] );
2806 beta = 0.0f;
2807 gamma = 0.0f;
2810 D3DXSHRotateZ(out, order, gamma, in);
2811 rotate_X(temp, order, 1.0f, out);
2812 D3DXSHRotateZ(out, order, beta, temp);
2813 rotate_X(temp, order, -1.0f, out);
2814 D3DXSHRotateZ(out, order, alpha, temp);
2816 return out;
2819 FLOAT * WINAPI D3DXSHRotateZ(FLOAT *out, UINT order, FLOAT angle, const FLOAT *in)
2821 UINT i, sum = 0;
2822 FLOAT c[5], s[5];
2824 TRACE("out %p, order %u, angle %f, in %p\n", out, order, angle, in);
2826 order = min(max(order, D3DXSH_MINORDER), D3DXSH_MAXORDER);
2828 out[0] = in[0];
2830 for (i = 1; i < order; i++)
2832 UINT j;
2834 c[i - 1] = cosf(i * angle);
2835 s[i - 1] = sinf(i * angle);
2836 sum += i * 2;
2838 out[sum - i] = c[i - 1] * in[sum - i];
2839 out[sum - i] += s[i - 1] * in[sum + i];
2840 for (j = i - 1; j > 0; j--)
2842 out[sum - j] = 0.0f;
2843 out[sum - j] = c[j - 1] * in[sum - j];
2844 out[sum - j] += s[j - 1] * in[sum + j];
2847 if (in == out)
2848 out[sum] = 0.0f;
2849 else
2850 out[sum] = in[sum];
2852 for (j = 1; j < i; j++)
2854 out[sum + j] = 0.0f;
2855 out[sum + j] = -s[j - 1] * in[sum - j];
2856 out[sum + j] += c[j - 1] * in[sum + j];
2858 out[sum + i] = -s[i - 1] * in[sum - i];
2859 out[sum + i] += c[i - 1] * in[sum + i];
2862 return out;
2865 FLOAT* WINAPI D3DXSHScale(FLOAT *out, UINT order, CONST FLOAT *a, CONST FLOAT scale)
2867 UINT i;
2869 TRACE("out %p, order %u, a %p, scale %f\n", out, order, a, scale);
2871 for (i = 0; i < order * order; i++)
2872 out[i] = a[i] * scale;
2874 return out;