1bfb8cc3db5700500f3267a041607ac5ebf1639f
[reactos.git] / dll / directx / wine / d3dx9_36 / math.c
1 /*
2 * Mathematical operations specific to D3DX9.
3 *
4 * Copyright (C) 2008 David Adam
5 * Copyright (C) 2008 Luis Busquets
6 * Copyright (C) 2008 Jérôme Gardou
7 * Copyright (C) 2008 Philip Nilsson
8 * Copyright (C) 2008 Henri Verbeet
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25 #include "d3dx9_36_private.h"
26
27 struct ID3DXMatrixStackImpl
28 {
29 ID3DXMatrixStack ID3DXMatrixStack_iface;
30 LONG ref;
31
32 unsigned int current;
33 unsigned int stack_size;
34 D3DXMATRIX *stack;
35 };
36
37 static const unsigned int INITIAL_STACK_SIZE = 32;
38
39 /*_________________D3DXColor____________________*/
40
41 D3DXCOLOR* WINAPI D3DXColorAdjustContrast(D3DXCOLOR *pout, const D3DXCOLOR *pc, FLOAT s)
42 {
43 TRACE("pout %p, pc %p, s %f\n", pout, pc, s);
44
45 pout->r = 0.5f + s * (pc->r - 0.5f);
46 pout->g = 0.5f + s * (pc->g - 0.5f);
47 pout->b = 0.5f + s * (pc->b - 0.5f);
48 pout->a = pc->a;
49 return pout;
50 }
51
52 D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, const D3DXCOLOR *pc, FLOAT s)
53 {
54 FLOAT grey;
55
56 TRACE("pout %p, pc %p, s %f\n", pout, pc, s);
57
58 grey = pc->r * 0.2125f + pc->g * 0.7154f + pc->b * 0.0721f;
59 pout->r = grey + s * (pc->r - grey);
60 pout->g = grey + s * (pc->g - grey);
61 pout->b = grey + s * (pc->b - grey);
62 pout->a = pc->a;
63 return pout;
64 }
65
66 /*_________________Misc__________________________*/
67
68 FLOAT WINAPI D3DXFresnelTerm(FLOAT costheta, FLOAT refractionindex)
69 {
70 FLOAT a, d, g, result;
71
72 TRACE("costheta %f, refractionindex %f\n", costheta, refractionindex);
73
74 g = sqrtf(refractionindex * refractionindex + costheta * costheta - 1.0f);
75 a = g + costheta;
76 d = g - costheta;
77 result = (costheta * a - 1.0f) * (costheta * a - 1.0f) / ((costheta * d + 1.0f) * (costheta * d + 1.0f)) + 1.0f;
78 result *= 0.5f * d * d / (a * a);
79
80 return result;
81 }
82
83 /*_________________D3DXMatrix____________________*/
84
85 D3DXMATRIX * WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *out, FLOAT scaling, const D3DXVECTOR3 *rotationcenter,
86 const D3DXQUATERNION *rotation, const D3DXVECTOR3 *translation)
87 {
88 TRACE("out %p, scaling %f, rotationcenter %p, rotation %p, translation %p\n",
89 out, scaling, rotationcenter, rotation, translation);
90
91 D3DXMatrixIdentity(out);
92
93 if (rotation)
94 {
95 FLOAT temp00, temp01, temp02, temp10, temp11, temp12, temp20, temp21, temp22;
96
97 temp00 = 1.0f - 2.0f * (rotation->y * rotation->y + rotation->z * rotation->z);
98 temp01 = 2.0f * (rotation->x * rotation->y + rotation->z * rotation->w);
99 temp02 = 2.0f * (rotation->x * rotation->z - rotation->y * rotation->w);
100 temp10 = 2.0f * (rotation->x * rotation->y - rotation->z * rotation->w);
101 temp11 = 1.0f - 2.0f * (rotation->x * rotation->x + rotation->z * rotation->z);
102 temp12 = 2.0f * (rotation->y * rotation->z + rotation->x * rotation->w);
103 temp20 = 2.0f * (rotation->x * rotation->z + rotation->y * rotation->w);
104 temp21 = 2.0f * (rotation->y * rotation->z - rotation->x * rotation->w);
105 temp22 = 1.0f - 2.0f * (rotation->x * rotation->x + rotation->y * rotation->y);
106
107 out->u.m[0][0] = scaling * temp00;
108 out->u.m[0][1] = scaling * temp01;
109 out->u.m[0][2] = scaling * temp02;
110 out->u.m[1][0] = scaling * temp10;
111 out->u.m[1][1] = scaling * temp11;
112 out->u.m[1][2] = scaling * temp12;
113 out->u.m[2][0] = scaling * temp20;
114 out->u.m[2][1] = scaling * temp21;
115 out->u.m[2][2] = scaling * temp22;
116
117 if (rotationcenter)
118 {
119 out->u.m[3][0] = rotationcenter->x * (1.0f - temp00) - rotationcenter->y * temp10
120 - rotationcenter->z * temp20;
121 out->u.m[3][1] = rotationcenter->y * (1.0f - temp11) - rotationcenter->x * temp01
122 - rotationcenter->z * temp21;
123 out->u.m[3][2] = rotationcenter->z * (1.0f - temp22) - rotationcenter->x * temp02
124 - rotationcenter->y * temp12;
125 }
126 }
127 else
128 {
129 out->u.m[0][0] = scaling;
130 out->u.m[1][1] = scaling;
131 out->u.m[2][2] = scaling;
132 }
133
134 if (translation)
135 {
136 out->u.m[3][0] += translation->x;
137 out->u.m[3][1] += translation->y;
138 out->u.m[3][2] += translation->z;
139 }
140
141 return out;
142 }
143
144 D3DXMATRIX * WINAPI D3DXMatrixAffineTransformation2D(D3DXMATRIX *out, FLOAT scaling,
145 const D3DXVECTOR2 *rotationcenter, FLOAT rotation, const D3DXVECTOR2 *translation)
146 {
147 FLOAT tmp1, tmp2, s;
148
149 TRACE("out %p, scaling %f, rotationcenter %p, rotation %f, translation %p\n",
150 out, scaling, rotationcenter, rotation, translation);
151
152 s = sinf(rotation / 2.0f);
153 tmp1 = 1.0f - 2.0f * s * s;
154 tmp2 = 2.0f * s * cosf(rotation / 2.0f);
155
156 D3DXMatrixIdentity(out);
157 out->u.m[0][0] = scaling * tmp1;
158 out->u.m[0][1] = scaling * tmp2;
159 out->u.m[1][0] = -scaling * tmp2;
160 out->u.m[1][1] = scaling * tmp1;
161
162 if (rotationcenter)
163 {
164 FLOAT x, y;
165
166 x = rotationcenter->x;
167 y = rotationcenter->y;
168
169 out->u.m[3][0] = y * tmp2 - x * tmp1 + x;
170 out->u.m[3][1] = -x * tmp2 - y * tmp1 + y;
171 }
172
173 if (translation)
174 {
175 out->u.m[3][0] += translation->x;
176 out->u.m[3][1] += translation->y;
177 }
178
179 return out;
180 }
181
182 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, const D3DXMATRIX *pm)
183 {
184 D3DXMATRIX normalized;
185 D3DXVECTOR3 vec;
186
187 TRACE("poutscale %p, poutrotation %p, pouttranslation %p, pm %p\n", poutscale, poutrotation, pouttranslation, pm);
188
189 /*Compute the scaling part.*/
190 vec.x=pm->u.m[0][0];
191 vec.y=pm->u.m[0][1];
192 vec.z=pm->u.m[0][2];
193 poutscale->x=D3DXVec3Length(&vec);
194
195 vec.x=pm->u.m[1][0];
196 vec.y=pm->u.m[1][1];
197 vec.z=pm->u.m[1][2];
198 poutscale->y=D3DXVec3Length(&vec);
199
200 vec.x=pm->u.m[2][0];
201 vec.y=pm->u.m[2][1];
202 vec.z=pm->u.m[2][2];
203 poutscale->z=D3DXVec3Length(&vec);
204
205 /*Compute the translation part.*/
206 pouttranslation->x=pm->u.m[3][0];
207 pouttranslation->y=pm->u.m[3][1];
208 pouttranslation->z=pm->u.m[3][2];
209
210 /*Let's calculate the rotation now*/
211 if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) ) return D3DERR_INVALIDCALL;
212
213 normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
214 normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
215 normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
216 normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
217 normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
218 normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
219 normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
220 normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
221 normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
222
223 D3DXQuaternionRotationMatrix(poutrotation,&normalized);
224 return S_OK;
225 }
226
227 FLOAT WINAPI D3DXMatrixDeterminant(const D3DXMATRIX *pm)
228 {
229 FLOAT t[3], v[4];
230
231 TRACE("pm %p\n", pm);
232
233 t[0] = pm->u.m[2][2] * pm->u.m[3][3] - pm->u.m[2][3] * pm->u.m[3][2];
234 t[1] = pm->u.m[1][2] * pm->u.m[3][3] - pm->u.m[1][3] * pm->u.m[3][2];
235 t[2] = pm->u.m[1][2] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][2];
236 v[0] = pm->u.m[1][1] * t[0] - pm->u.m[2][1] * t[1] + pm->u.m[3][1] * t[2];
237 v[1] = -pm->u.m[1][0] * t[0] + pm->u.m[2][0] * t[1] - pm->u.m[3][0] * t[2];
238
239 t[0] = pm->u.m[1][0] * pm->u.m[2][1] - pm->u.m[2][0] * pm->u.m[1][1];
240 t[1] = pm->u.m[1][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[1][1];
241 t[2] = pm->u.m[2][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[2][1];
242 v[2] = pm->u.m[3][3] * t[0] - pm->u.m[2][3] * t[1] + pm->u.m[1][3] * t[2];
243 v[3] = -pm->u.m[3][2] * t[0] + pm->u.m[2][2] * t[1] - pm->u.m[1][2] * t[2];
244
245 return pm->u.m[0][0] * v[0] + pm->u.m[0][1] * v[1] +
246 pm->u.m[0][2] * v[2] + pm->u.m[0][3] * v[3];
247 }
248
249 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, const D3DXMATRIX *pm)
250 {
251 FLOAT det, t[3], v[16];
252 UINT i, j;
253
254 TRACE("pout %p, pdeterminant %p, pm %p\n", pout, pdeterminant, pm);
255
256 t[0] = pm->u.m[2][2] * pm->u.m[3][3] - pm->u.m[2][3] * pm->u.m[3][2];
257 t[1] = pm->u.m[1][2] * pm->u.m[3][3] - pm->u.m[1][3] * pm->u.m[3][2];
258 t[2] = pm->u.m[1][2] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][2];
259 v[0] = pm->u.m[1][1] * t[0] - pm->u.m[2][1] * t[1] + pm->u.m[3][1] * t[2];
260 v[4] = -pm->u.m[1][0] * t[0] + pm->u.m[2][0] * t[1] - pm->u.m[3][0] * t[2];
261
262 t[0] = pm->u.m[1][0] * pm->u.m[2][1] - pm->u.m[2][0] * pm->u.m[1][1];
263 t[1] = pm->u.m[1][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[1][1];
264 t[2] = pm->u.m[2][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[2][1];
265 v[8] = pm->u.m[3][3] * t[0] - pm->u.m[2][3] * t[1] + pm->u.m[1][3] * t[2];
266 v[12] = -pm->u.m[3][2] * t[0] + pm->u.m[2][2] * t[1] - pm->u.m[1][2] * t[2];
267
268 det = pm->u.m[0][0] * v[0] + pm->u.m[0][1] * v[4] +
269 pm->u.m[0][2] * v[8] + pm->u.m[0][3] * v[12];
270 if (det == 0.0f)
271 return NULL;
272 if (pdeterminant)
273 *pdeterminant = det;
274
275 t[0] = pm->u.m[2][2] * pm->u.m[3][3] - pm->u.m[2][3] * pm->u.m[3][2];
276 t[1] = pm->u.m[0][2] * pm->u.m[3][3] - pm->u.m[0][3] * pm->u.m[3][2];
277 t[2] = pm->u.m[0][2] * pm->u.m[2][3] - pm->u.m[0][3] * pm->u.m[2][2];
278 v[1] = -pm->u.m[0][1] * t[0] + pm->u.m[2][1] * t[1] - pm->u.m[3][1] * t[2];
279 v[5] = pm->u.m[0][0] * t[0] - pm->u.m[2][0] * t[1] + pm->u.m[3][0] * t[2];
280
281 t[0] = pm->u.m[0][0] * pm->u.m[2][1] - pm->u.m[2][0] * pm->u.m[0][1];
282 t[1] = pm->u.m[3][0] * pm->u.m[0][1] - pm->u.m[0][0] * pm->u.m[3][1];
283 t[2] = pm->u.m[2][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[2][1];
284 v[9] = -pm->u.m[3][3] * t[0] - pm->u.m[2][3] * t[1]- pm->u.m[0][3] * t[2];
285 v[13] = pm->u.m[3][2] * t[0] + pm->u.m[2][2] * t[1] + pm->u.m[0][2] * t[2];
286
287 t[0] = pm->u.m[1][2] * pm->u.m[3][3] - pm->u.m[1][3] * pm->u.m[3][2];
288 t[1] = pm->u.m[0][2] * pm->u.m[3][3] - pm->u.m[0][3] * pm->u.m[3][2];
289 t[2] = pm->u.m[0][2] * pm->u.m[1][3] - pm->u.m[0][3] * pm->u.m[1][2];
290 v[2] = pm->u.m[0][1] * t[0] - pm->u.m[1][1] * t[1] + pm->u.m[3][1] * t[2];
291 v[6] = -pm->u.m[0][0] * t[0] + pm->u.m[1][0] * t[1] - pm->u.m[3][0] * t[2];
292
293 t[0] = pm->u.m[0][0] * pm->u.m[1][1] - pm->u.m[1][0] * pm->u.m[0][1];
294 t[1] = pm->u.m[3][0] * pm->u.m[0][1] - pm->u.m[0][0] * pm->u.m[3][1];
295 t[2] = pm->u.m[1][0] * pm->u.m[3][1] - pm->u.m[3][0] * pm->u.m[1][1];
296 v[10] = pm->u.m[3][3] * t[0] + pm->u.m[1][3] * t[1] + pm->u.m[0][3] * t[2];
297 v[14] = -pm->u.m[3][2] * t[0] - pm->u.m[1][2] * t[1] - pm->u.m[0][2] * t[2];
298
299 t[0] = pm->u.m[1][2] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][2];
300 t[1] = pm->u.m[0][2] * pm->u.m[2][3] - pm->u.m[0][3] * pm->u.m[2][2];
301 t[2] = pm->u.m[0][2] * pm->u.m[1][3] - pm->u.m[0][3] * pm->u.m[1][2];
302 v[3] = -pm->u.m[0][1] * t[0] + pm->u.m[1][1] * t[1] - pm->u.m[2][1] * t[2];
303 v[7] = pm->u.m[0][0] * t[0] - pm->u.m[1][0] * t[1] + pm->u.m[2][0] * t[2];
304
305 v[11] = -pm->u.m[0][0] * (pm->u.m[1][1] * pm->u.m[2][3] - pm->u.m[1][3] * pm->u.m[2][1]) +
306 pm->u.m[1][0] * (pm->u.m[0][1] * pm->u.m[2][3] - pm->u.m[0][3] * pm->u.m[2][1]) -
307 pm->u.m[2][0] * (pm->u.m[0][1] * pm->u.m[1][3] - pm->u.m[0][3] * pm->u.m[1][1]);
308
309 v[15] = pm->u.m[0][0] * (pm->u.m[1][1] * pm->u.m[2][2] - pm->u.m[1][2] * pm->u.m[2][1]) -
310 pm->u.m[1][0] * (pm->u.m[0][1] * pm->u.m[2][2] - pm->u.m[0][2] * pm->u.m[2][1]) +
311 pm->u.m[2][0] * (pm->u.m[0][1] * pm->u.m[1][2] - pm->u.m[0][2] * pm->u.m[1][1]);
312
313 det = 1.0f / det;
314
315 for (i = 0; i < 4; i++)
316 for (j = 0; j < 4; j++)
317 pout->u.m[i][j] = v[4 * i + j] * det;
318
319 return pout;
320 }
321
322 D3DXMATRIX * WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *out, const D3DXVECTOR3 *eye, const D3DXVECTOR3 *at,
323 const D3DXVECTOR3 *up)
324 {
325 D3DXVECTOR3 right, upn, vec;
326
327 TRACE("out %p, eye %p, at %p, up %p\n", out, eye, at, up);
328
329 D3DXVec3Subtract(&vec, at, eye);
330 D3DXVec3Normalize(&vec, &vec);
331 D3DXVec3Cross(&right, up, &vec);
332 D3DXVec3Cross(&upn, &vec, &right);
333 D3DXVec3Normalize(&right, &right);
334 D3DXVec3Normalize(&upn, &upn);
335 out->u.m[0][0] = right.x;
336 out->u.m[1][0] = right.y;
337 out->u.m[2][0] = right.z;
338 out->u.m[3][0] = -D3DXVec3Dot(&right, eye);
339 out->u.m[0][1] = upn.x;
340 out->u.m[1][1] = upn.y;
341 out->u.m[2][1] = upn.z;
342 out->u.m[3][1] = -D3DXVec3Dot(&upn, eye);
343 out->u.m[0][2] = vec.x;
344 out->u.m[1][2] = vec.y;
345 out->u.m[2][2] = vec.z;
346 out->u.m[3][2] = -D3DXVec3Dot(&vec, eye);
347 out->u.m[0][3] = 0.0f;
348 out->u.m[1][3] = 0.0f;
349 out->u.m[2][3] = 0.0f;
350 out->u.m[3][3] = 1.0f;
351
352 return out;
353 }
354
355 D3DXMATRIX * WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *out, const D3DXVECTOR3 *eye, const D3DXVECTOR3 *at,
356 const D3DXVECTOR3 *up)
357 {
358 D3DXVECTOR3 right, upn, vec;
359
360 TRACE("out %p, eye %p, at %p, up %p\n", out, eye, at, up);
361
362 D3DXVec3Subtract(&vec, at, eye);
363 D3DXVec3Normalize(&vec, &vec);
364 D3DXVec3Cross(&right, up, &vec);
365 D3DXVec3Cross(&upn, &vec, &right);
366 D3DXVec3Normalize(&right, &right);
367 D3DXVec3Normalize(&upn, &upn);
368 out->u.m[0][0] = -right.x;
369 out->u.m[1][0] = -right.y;
370 out->u.m[2][0] = -right.z;
371 out->u.m[3][0] = D3DXVec3Dot(&right, eye);
372 out->u.m[0][1] = upn.x;
373 out->u.m[1][1] = upn.y;
374 out->u.m[2][1] = upn.z;
375 out->u.m[3][1] = -D3DXVec3Dot(&upn, eye);
376 out->u.m[0][2] = -vec.x;
377 out->u.m[1][2] = -vec.y;
378 out->u.m[2][2] = -vec.z;
379 out->u.m[3][2] = D3DXVec3Dot(&vec, eye);
380 out->u.m[0][3] = 0.0f;
381 out->u.m[1][3] = 0.0f;
382 out->u.m[2][3] = 0.0f;
383 out->u.m[3][3] = 1.0f;
384
385 return out;
386 }
387
388 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, const D3DXMATRIX *pm1, const D3DXMATRIX *pm2)
389 {
390 D3DXMATRIX out;
391 int i,j;
392
393 TRACE("pout %p, pm1 %p, pm2 %p\n", pout, pm1, pm2);
394
395 for (i=0; i<4; i++)
396 {
397 for (j=0; j<4; j++)
398 {
399 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];
400 }
401 }
402
403 *pout = out;
404 return pout;
405 }
406
407 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, const D3DXMATRIX *pm1, const D3DXMATRIX *pm2)
408 {
409 D3DXMATRIX temp;
410 int i, j;
411
412 TRACE("pout %p, pm1 %p, pm2 %p\n", pout, pm1, pm2);
413
414 for (i = 0; i < 4; i++)
415 for (j = 0; j < 4; j++)
416 temp.u.m[j][i] = 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];
417
418 *pout = temp;
419 return pout;
420 }
421
422 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
423 {
424 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
425
426 D3DXMatrixIdentity(pout);
427 pout->u.m[0][0] = 2.0f / w;
428 pout->u.m[1][1] = 2.0f / h;
429 pout->u.m[2][2] = 1.0f / (zf - zn);
430 pout->u.m[3][2] = zn / (zn - zf);
431 return pout;
432 }
433
434 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
435 {
436 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
437
438 D3DXMatrixIdentity(pout);
439 pout->u.m[0][0] = 2.0f / (r - l);
440 pout->u.m[1][1] = 2.0f / (t - b);
441 pout->u.m[2][2] = 1.0f / (zf -zn);
442 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
443 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
444 pout->u.m[3][2] = zn / (zn -zf);
445 return pout;
446 }
447
448 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
449 {
450 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
451
452 D3DXMatrixIdentity(pout);
453 pout->u.m[0][0] = 2.0f / (r - l);
454 pout->u.m[1][1] = 2.0f / (t - b);
455 pout->u.m[2][2] = 1.0f / (zn -zf);
456 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
457 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
458 pout->u.m[3][2] = zn / (zn -zf);
459 return pout;
460 }
461
462 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
463 {
464 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
465
466 D3DXMatrixIdentity(pout);
467 pout->u.m[0][0] = 2.0f / w;
468 pout->u.m[1][1] = 2.0f / h;
469 pout->u.m[2][2] = 1.0f / (zn - zf);
470 pout->u.m[3][2] = zn / (zn - zf);
471 return pout;
472 }
473
474 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
475 {
476 TRACE("pout %p, fovy %f, aspect %f, zn %f, zf %f\n", pout, fovy, aspect, zn, zf);
477
478 D3DXMatrixIdentity(pout);
479 pout->u.m[0][0] = 1.0f / (aspect * tanf(fovy/2.0f));
480 pout->u.m[1][1] = 1.0f / tanf(fovy/2.0f);
481 pout->u.m[2][2] = zf / (zf - zn);
482 pout->u.m[2][3] = 1.0f;
483 pout->u.m[3][2] = (zf * zn) / (zn - zf);
484 pout->u.m[3][3] = 0.0f;
485 return pout;
486 }
487
488 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
489 {
490 TRACE("pout %p, fovy %f, aspect %f, zn %f, zf %f\n", pout, fovy, aspect, zn, zf);
491
492 D3DXMatrixIdentity(pout);
493 pout->u.m[0][0] = 1.0f / (aspect * tanf(fovy/2.0f));
494 pout->u.m[1][1] = 1.0f / tanf(fovy/2.0f);
495 pout->u.m[2][2] = zf / (zn - zf);
496 pout->u.m[2][3] = -1.0f;
497 pout->u.m[3][2] = (zf * zn) / (zn - zf);
498 pout->u.m[3][3] = 0.0f;
499 return pout;
500 }
501
502 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
503 {
504 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
505
506 D3DXMatrixIdentity(pout);
507 pout->u.m[0][0] = 2.0f * zn / w;
508 pout->u.m[1][1] = 2.0f * zn / h;
509 pout->u.m[2][2] = zf / (zf - zn);
510 pout->u.m[3][2] = (zn * zf) / (zn - zf);
511 pout->u.m[2][3] = 1.0f;
512 pout->u.m[3][3] = 0.0f;
513 return pout;
514 }
515
516 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
517 {
518 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
519
520 D3DXMatrixIdentity(pout);
521 pout->u.m[0][0] = 2.0f * zn / (r - l);
522 pout->u.m[1][1] = -2.0f * zn / (b - t);
523 pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
524 pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
525 pout->u.m[2][2] = - zf / (zn - zf);
526 pout->u.m[3][2] = (zn * zf) / (zn -zf);
527 pout->u.m[2][3] = 1.0f;
528 pout->u.m[3][3] = 0.0f;
529 return pout;
530 }
531
532 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
533 {
534 TRACE("pout %p, l %f, r %f, b %f, t %f, zn %f, zf %f\n", pout, l, r, b, t, zn, zf);
535
536 D3DXMatrixIdentity(pout);
537 pout->u.m[0][0] = 2.0f * zn / (r - l);
538 pout->u.m[1][1] = -2.0f * zn / (b - t);
539 pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
540 pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
541 pout->u.m[2][2] = zf / (zn - zf);
542 pout->u.m[3][2] = (zn * zf) / (zn -zf);
543 pout->u.m[2][3] = -1.0f;
544 pout->u.m[3][3] = 0.0f;
545 return pout;
546 }
547
548 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
549 {
550 TRACE("pout %p, w %f, h %f, zn %f, zf %f\n", pout, w, h, zn, zf);
551
552 D3DXMatrixIdentity(pout);
553 pout->u.m[0][0] = 2.0f * zn / w;
554 pout->u.m[1][1] = 2.0f * zn / h;
555 pout->u.m[2][2] = zf / (zn - zf);
556 pout->u.m[3][2] = (zn * zf) / (zn - zf);
557 pout->u.m[2][3] = -1.0f;
558 pout->u.m[3][3] = 0.0f;
559 return pout;
560 }
561
562 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, const D3DXPLANE *pplane)
563 {
564 D3DXPLANE Nplane;
565
566 TRACE("pout %p, pplane %p\n", pout, pplane);
567
568 D3DXPlaneNormalize(&Nplane, pplane);
569 D3DXMatrixIdentity(pout);
570 pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
571 pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
572 pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
573 pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
574 pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
575 pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
576 pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
577 pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
578 pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
579 pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
580 pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
581 pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
582 return pout;
583 }
584
585 D3DXMATRIX * WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *out, const D3DXVECTOR3 *v, FLOAT angle)
586 {
587 D3DXVECTOR3 nv;
588 FLOAT sangle, cangle, cdiff;
589
590 TRACE("out %p, v %p, angle %f\n", out, v, angle);
591
592 D3DXVec3Normalize(&nv, v);
593 sangle = sinf(angle);
594 cangle = cosf(angle);
595 cdiff = 1.0f - cangle;
596
597 out->u.m[0][0] = cdiff * nv.x * nv.x + cangle;
598 out->u.m[1][0] = cdiff * nv.x * nv.y - sangle * nv.z;
599 out->u.m[2][0] = cdiff * nv.x * nv.z + sangle * nv.y;
600 out->u.m[3][0] = 0.0f;
601 out->u.m[0][1] = cdiff * nv.y * nv.x + sangle * nv.z;
602 out->u.m[1][1] = cdiff * nv.y * nv.y + cangle;
603 out->u.m[2][1] = cdiff * nv.y * nv.z - sangle * nv.x;
604 out->u.m[3][1] = 0.0f;
605 out->u.m[0][2] = cdiff * nv.z * nv.x - sangle * nv.y;
606 out->u.m[1][2] = cdiff * nv.z * nv.y + sangle * nv.x;
607 out->u.m[2][2] = cdiff * nv.z * nv.z + cangle;
608 out->u.m[3][2] = 0.0f;
609 out->u.m[0][3] = 0.0f;
610 out->u.m[1][3] = 0.0f;
611 out->u.m[2][3] = 0.0f;
612 out->u.m[3][3] = 1.0f;
613
614 return out;
615 }
616
617 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, const D3DXQUATERNION *pq)
618 {
619 TRACE("pout %p, pq %p\n", pout, pq);
620
621 D3DXMatrixIdentity(pout);
622 pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
623 pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
624 pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
625 pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
626 pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
627 pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
628 pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
629 pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
630 pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
631 return pout;
632 }
633
634 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
635 {
636 TRACE("pout %p, angle %f\n", pout, angle);
637
638 D3DXMatrixIdentity(pout);
639 pout->u.m[1][1] = cosf(angle);
640 pout->u.m[2][2] = cosf(angle);
641 pout->u.m[1][2] = sinf(angle);
642 pout->u.m[2][1] = -sinf(angle);
643 return pout;
644 }
645
646 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
647 {
648 TRACE("pout %p, angle %f\n", pout, angle);
649
650 D3DXMatrixIdentity(pout);
651 pout->u.m[0][0] = cosf(angle);
652 pout->u.m[2][2] = cosf(angle);
653 pout->u.m[0][2] = -sinf(angle);
654 pout->u.m[2][0] = sinf(angle);
655 return pout;
656 }
657
658 D3DXMATRIX * WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *out, FLOAT yaw, FLOAT pitch, FLOAT roll)
659 {
660 FLOAT sroll, croll, spitch, cpitch, syaw, cyaw;
661
662 TRACE("out %p, yaw %f, pitch %f, roll %f\n", out, yaw, pitch, roll);
663
664 sroll = sinf(roll);
665 croll = cosf(roll);
666 spitch = sinf(pitch);
667 cpitch = cosf(pitch);
668 syaw = sinf(yaw);
669 cyaw = cosf(yaw);
670
671 out->u.m[0][0] = sroll * spitch * syaw + croll * cyaw;
672 out->u.m[0][1] = sroll * cpitch;
673 out->u.m[0][2] = sroll * spitch * cyaw - croll * syaw;
674 out->u.m[0][3] = 0.0f;
675 out->u.m[1][0] = croll * spitch * syaw - sroll * cyaw;
676 out->u.m[1][1] = croll * cpitch;
677 out->u.m[1][2] = croll * spitch * cyaw + sroll * syaw;
678 out->u.m[1][3] = 0.0f;
679 out->u.m[2][0] = cpitch * syaw;
680 out->u.m[2][1] = -spitch;
681 out->u.m[2][2] = cpitch * cyaw;
682 out->u.m[2][3] = 0.0f;
683 out->u.m[3][0] = 0.0f;
684 out->u.m[3][1] = 0.0f;
685 out->u.m[3][2] = 0.0f;
686 out->u.m[3][3] = 1.0f;
687
688 return out;
689 }
690
691 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
692 {
693 TRACE("pout %p, angle %f\n", pout, angle);
694
695 D3DXMatrixIdentity(pout);
696 pout->u.m[0][0] = cosf(angle);
697 pout->u.m[1][1] = cosf(angle);
698 pout->u.m[0][1] = sinf(angle);
699 pout->u.m[1][0] = -sinf(angle);
700 return pout;
701 }
702
703 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
704 {
705 TRACE("pout %p, sx %f, sy %f, sz %f\n", pout, sx, sy, sz);
706
707 D3DXMatrixIdentity(pout);
708 pout->u.m[0][0] = sx;
709 pout->u.m[1][1] = sy;
710 pout->u.m[2][2] = sz;
711 return pout;
712 }
713
714 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, const D3DXVECTOR4 *plight, const D3DXPLANE *pplane)
715 {
716 D3DXPLANE Nplane;
717 FLOAT dot;
718
719 TRACE("pout %p, plight %p, pplane %p\n", pout, plight, pplane);
720
721 D3DXPlaneNormalize(&Nplane, pplane);
722 dot = D3DXPlaneDot(&Nplane, plight);
723 pout->u.m[0][0] = dot - Nplane.a * plight->x;
724 pout->u.m[0][1] = -Nplane.a * plight->y;
725 pout->u.m[0][2] = -Nplane.a * plight->z;
726 pout->u.m[0][3] = -Nplane.a * plight->w;
727 pout->u.m[1][0] = -Nplane.b * plight->x;
728 pout->u.m[1][1] = dot - Nplane.b * plight->y;
729 pout->u.m[1][2] = -Nplane.b * plight->z;
730 pout->u.m[1][3] = -Nplane.b * plight->w;
731 pout->u.m[2][0] = -Nplane.c * plight->x;
732 pout->u.m[2][1] = -Nplane.c * plight->y;
733 pout->u.m[2][2] = dot - Nplane.c * plight->z;
734 pout->u.m[2][3] = -Nplane.c * plight->w;
735 pout->u.m[3][0] = -Nplane.d * plight->x;
736 pout->u.m[3][1] = -Nplane.d * plight->y;
737 pout->u.m[3][2] = -Nplane.d * plight->z;
738 pout->u.m[3][3] = dot - Nplane.d * plight->w;
739 return pout;
740 }
741
742 D3DXMATRIX* WINAPI D3DXMatrixTransformation(D3DXMATRIX *pout, const D3DXVECTOR3 *pscalingcenter, const D3DXQUATERNION *pscalingrotation, const D3DXVECTOR3 *pscaling, const D3DXVECTOR3 *protationcenter, const D3DXQUATERNION *protation, const D3DXVECTOR3 *ptranslation)
743 {
744 D3DXMATRIX m1, m2, m3, m4, m5, m6, m7;
745 D3DXQUATERNION prc;
746 D3DXVECTOR3 psc, pt;
747
748 TRACE("pout %p, pscalingcenter %p, pscalingrotation %p, pscaling %p, protationcentr %p, protation %p, ptranslation %p\n",
749 pout, pscalingcenter, pscalingrotation, pscaling, protationcenter, protation, ptranslation);
750
751 if ( !pscalingcenter )
752 {
753 psc.x = 0.0f;
754 psc.y = 0.0f;
755 psc.z = 0.0f;
756 }
757 else
758 {
759 psc.x = pscalingcenter->x;
760 psc.y = pscalingcenter->y;
761 psc.z = pscalingcenter->z;
762 }
763
764 if ( !protationcenter )
765 {
766 prc.x = 0.0f;
767 prc.y = 0.0f;
768 prc.z = 0.0f;
769 }
770 else
771 {
772 prc.x = protationcenter->x;
773 prc.y = protationcenter->y;
774 prc.z = protationcenter->z;
775 }
776
777 if ( !ptranslation )
778 {
779 pt.x = 0.0f;
780 pt.y = 0.0f;
781 pt.z = 0.0f;
782 }
783 else
784 {
785 pt.x = ptranslation->x;
786 pt.y = ptranslation->y;
787 pt.z = ptranslation->z;
788 }
789
790 D3DXMatrixTranslation(&m1, -psc.x, -psc.y, -psc.z);
791
792 if ( !pscalingrotation )
793 {
794 D3DXMatrixIdentity(&m2);
795 D3DXMatrixIdentity(&m4);
796 }
797 else
798 {
799 D3DXMatrixRotationQuaternion(&m4, pscalingrotation);
800 D3DXMatrixInverse(&m2, NULL, &m4);
801 }
802
803 if ( !pscaling ) D3DXMatrixIdentity(&m3);
804 else D3DXMatrixScaling(&m3, pscaling->x, pscaling->y, pscaling->z);
805
806 if ( !protation ) D3DXMatrixIdentity(&m6);
807 else D3DXMatrixRotationQuaternion(&m6, protation);
808
809 D3DXMatrixTranslation(&m5, psc.x - prc.x, psc.y - prc.y, psc.z - prc.z);
810 D3DXMatrixTranslation(&m7, prc.x + pt.x, prc.y + pt.y, prc.z + pt.z);
811 D3DXMatrixMultiply(&m1, &m1, &m2);
812 D3DXMatrixMultiply(&m1, &m1, &m3);
813 D3DXMatrixMultiply(&m1, &m1, &m4);
814 D3DXMatrixMultiply(&m1, &m1, &m5);
815 D3DXMatrixMultiply(&m1, &m1, &m6);
816 D3DXMatrixMultiply(pout, &m1, &m7);
817 return pout;
818 }
819
820 D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(D3DXMATRIX *pout, const D3DXVECTOR2 *pscalingcenter, FLOAT scalingrotation, const D3DXVECTOR2 *pscaling, const D3DXVECTOR2 *protationcenter, FLOAT rotation, const D3DXVECTOR2 *ptranslation)
821 {
822 D3DXQUATERNION rot, sca_rot;
823 D3DXVECTOR3 rot_center, sca, sca_center, trans;
824
825 TRACE("pout %p, pscalingcenter %p, scalingrotation %f, pscaling %p, protztioncenter %p, rotation %f, ptranslation %p\n",
826 pout, pscalingcenter, scalingrotation, pscaling, protationcenter, rotation, ptranslation);
827
828 if ( pscalingcenter )
829 {
830 sca_center.x=pscalingcenter->x;
831 sca_center.y=pscalingcenter->y;
832 sca_center.z=0.0f;
833 }
834 else
835 {
836 sca_center.x=0.0f;
837 sca_center.y=0.0f;
838 sca_center.z=0.0f;
839 }
840
841 if ( pscaling )
842 {
843 sca.x=pscaling->x;
844 sca.y=pscaling->y;
845 sca.z=1.0f;
846 }
847 else
848 {
849 sca.x=1.0f;
850 sca.y=1.0f;
851 sca.z=1.0f;
852 }
853
854 if ( protationcenter )
855 {
856 rot_center.x=protationcenter->x;
857 rot_center.y=protationcenter->y;
858 rot_center.z=0.0f;
859 }
860 else
861 {
862 rot_center.x=0.0f;
863 rot_center.y=0.0f;
864 rot_center.z=0.0f;
865 }
866
867 if ( ptranslation )
868 {
869 trans.x=ptranslation->x;
870 trans.y=ptranslation->y;
871 trans.z=0.0f;
872 }
873 else
874 {
875 trans.x=0.0f;
876 trans.y=0.0f;
877 trans.z=0.0f;
878 }
879
880 rot.w=cosf(rotation/2.0f);
881 rot.x=0.0f;
882 rot.y=0.0f;
883 rot.z=sinf(rotation/2.0f);
884
885 sca_rot.w=cosf(scalingrotation/2.0f);
886 sca_rot.x=0.0f;
887 sca_rot.y=0.0f;
888 sca_rot.z=sinf(scalingrotation/2.0f);
889
890 D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
891
892 return pout;
893 }
894
895 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
896 {
897 TRACE("pout %p, x %f, y %f, z %f\n", pout, x, y, z);
898
899 D3DXMatrixIdentity(pout);
900 pout->u.m[3][0] = x;
901 pout->u.m[3][1] = y;
902 pout->u.m[3][2] = z;
903 return pout;
904 }
905
906 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, const D3DXMATRIX *pm)
907 {
908 const D3DXMATRIX m = *pm;
909 int i,j;
910
911 TRACE("pout %p, pm %p\n", pout, pm);
912
913 for (i=0; i<4; i++)
914 for (j=0; j<4; j++) pout->u.m[i][j] = m.u.m[j][i];
915
916 return pout;
917 }
918
919 /*_________________D3DXMatrixStack____________________*/
920
921
922 static inline struct ID3DXMatrixStackImpl *impl_from_ID3DXMatrixStack(ID3DXMatrixStack *iface)
923 {
924 return CONTAINING_RECORD(iface, struct ID3DXMatrixStackImpl, ID3DXMatrixStack_iface);
925 }
926
927 static HRESULT WINAPI ID3DXMatrixStackImpl_QueryInterface(ID3DXMatrixStack *iface, REFIID riid, void **out)
928 {
929 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
930
931 if (IsEqualGUID(riid, &IID_ID3DXMatrixStack)
932 || IsEqualGUID(riid, &IID_IUnknown))
933 {
934 ID3DXMatrixStack_AddRef(iface);
935 *out = iface;
936 return S_OK;
937 }
938
939 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
940
941 *out = NULL;
942 return E_NOINTERFACE;
943 }
944
945 static ULONG WINAPI ID3DXMatrixStackImpl_AddRef(ID3DXMatrixStack *iface)
946 {
947 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
948 ULONG ref = InterlockedIncrement(&This->ref);
949 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
950 return ref;
951 }
952
953 static ULONG WINAPI ID3DXMatrixStackImpl_Release(ID3DXMatrixStack *iface)
954 {
955 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
956 ULONG ref = InterlockedDecrement(&This->ref);
957 if (!ref)
958 {
959 HeapFree(GetProcessHeap(), 0, This->stack);
960 HeapFree(GetProcessHeap(), 0, This);
961 }
962 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
963 return ref;
964 }
965
966 static D3DXMATRIX* WINAPI ID3DXMatrixStackImpl_GetTop(ID3DXMatrixStack *iface)
967 {
968 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
969
970 TRACE("iface %p\n", iface);
971
972 return &This->stack[This->current];
973 }
974
975 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadIdentity(ID3DXMatrixStack *iface)
976 {
977 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
978
979 TRACE("iface %p\n", iface);
980
981 D3DXMatrixIdentity(&This->stack[This->current]);
982
983 return D3D_OK;
984 }
985
986 static HRESULT WINAPI ID3DXMatrixStackImpl_LoadMatrix(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
987 {
988 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
989
990 TRACE("iface %p, pm %p\n", iface, pm);
991
992 This->stack[This->current] = *pm;
993
994 return D3D_OK;
995 }
996
997 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrix(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
998 {
999 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1000
1001 TRACE("iface %p, pm %p\n", iface, pm);
1002
1003 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], pm);
1004
1005 return D3D_OK;
1006 }
1007
1008 static HRESULT WINAPI ID3DXMatrixStackImpl_MultMatrixLocal(ID3DXMatrixStack *iface, const D3DXMATRIX *pm)
1009 {
1010 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1011
1012 TRACE("iface %p, pm %p\n", iface, pm);
1013
1014 D3DXMatrixMultiply(&This->stack[This->current], pm, &This->stack[This->current]);
1015
1016 return D3D_OK;
1017 }
1018
1019 static HRESULT WINAPI ID3DXMatrixStackImpl_Pop(ID3DXMatrixStack *iface)
1020 {
1021 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1022
1023 TRACE("iface %p\n", iface);
1024
1025 /* Popping the last element on the stack returns D3D_OK, but does nothing. */
1026 if (!This->current) return D3D_OK;
1027
1028 if (This->current <= This->stack_size / 4 && This->stack_size >= INITIAL_STACK_SIZE * 2)
1029 {
1030 unsigned int new_size;
1031 D3DXMATRIX *new_stack;
1032
1033 new_size = This->stack_size / 2;
1034 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1035 if (new_stack)
1036 {
1037 This->stack_size = new_size;
1038 This->stack = new_stack;
1039 }
1040 }
1041
1042 --This->current;
1043
1044 return D3D_OK;
1045 }
1046
1047 static HRESULT WINAPI ID3DXMatrixStackImpl_Push(ID3DXMatrixStack *iface)
1048 {
1049 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1050
1051 TRACE("iface %p\n", iface);
1052
1053 if (This->current == This->stack_size - 1)
1054 {
1055 unsigned int new_size;
1056 D3DXMATRIX *new_stack;
1057
1058 if (This->stack_size > UINT_MAX / 2) return E_OUTOFMEMORY;
1059
1060 new_size = This->stack_size * 2;
1061 new_stack = HeapReAlloc(GetProcessHeap(), 0, This->stack, new_size * sizeof(*new_stack));
1062 if (!new_stack) return E_OUTOFMEMORY;
1063
1064 This->stack_size = new_size;
1065 This->stack = new_stack;
1066 }
1067
1068 ++This->current;
1069 This->stack[This->current] = This->stack[This->current - 1];
1070
1071 return D3D_OK;
1072 }
1073
1074 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxis(ID3DXMatrixStack *iface, const D3DXVECTOR3 *pv, FLOAT angle)
1075 {
1076 D3DXMATRIX temp;
1077 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1078
1079 TRACE("iface %p, pv %p, angle %f\n", iface, pv, angle);
1080
1081 D3DXMatrixRotationAxis(&temp, pv, angle);
1082 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1083
1084 return D3D_OK;
1085 }
1086
1087 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateAxisLocal(ID3DXMatrixStack *iface, const D3DXVECTOR3 *pv, FLOAT angle)
1088 {
1089 D3DXMATRIX temp;
1090 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1091
1092 TRACE("iface %p, pv %p, angle %f\n", iface, pv, angle);
1093
1094 D3DXMatrixRotationAxis(&temp, pv, angle);
1095 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1096
1097 return D3D_OK;
1098 }
1099
1100 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRoll(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1101 {
1102 D3DXMATRIX temp;
1103 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1104
1105 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1106
1107 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1108 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1109
1110 return D3D_OK;
1111 }
1112
1113 static HRESULT WINAPI ID3DXMatrixStackImpl_RotateYawPitchRollLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1114 {
1115 D3DXMATRIX temp;
1116 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1117
1118 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1119
1120 D3DXMatrixRotationYawPitchRoll(&temp, x, y, z);
1121 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1122
1123 return D3D_OK;
1124 }
1125
1126 static HRESULT WINAPI ID3DXMatrixStackImpl_Scale(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1127 {
1128 D3DXMATRIX temp;
1129 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1130
1131 TRACE("iface %p,x %f, y %f, z %f\n", iface, x, y, z);
1132
1133 D3DXMatrixScaling(&temp, x, y, z);
1134 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1135
1136 return D3D_OK;
1137 }
1138
1139 static HRESULT WINAPI ID3DXMatrixStackImpl_ScaleLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1140 {
1141 D3DXMATRIX temp;
1142 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1143
1144 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1145
1146 D3DXMatrixScaling(&temp, x, y, z);
1147 D3DXMatrixMultiply(&This->stack[This->current], &temp, &This->stack[This->current]);
1148
1149 return D3D_OK;
1150 }
1151
1152 static HRESULT WINAPI ID3DXMatrixStackImpl_Translate(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1153 {
1154 D3DXMATRIX temp;
1155 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1156
1157 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1158
1159 D3DXMatrixTranslation(&temp, x, y, z);
1160 D3DXMatrixMultiply(&This->stack[This->current], &This->stack[This->current], &temp);
1161
1162 return D3D_OK;
1163 }
1164
1165 static HRESULT WINAPI ID3DXMatrixStackImpl_TranslateLocal(ID3DXMatrixStack *iface, FLOAT x, FLOAT y, FLOAT z)
1166 {
1167 D3DXMATRIX temp;
1168 struct ID3DXMatrixStackImpl *This = impl_from_ID3DXMatrixStack(iface);
1169
1170 TRACE("iface %p, x %f, y %f, z %f\n", iface, x, y, z);
1171
1172 D3DXMatrixTranslation(&temp, x, y, z);
1173 D3DXMatrixMultiply(&This->stack[This->current], &temp,&This->stack[This->current]);
1174
1175 return D3D_OK;
1176 }
1177
1178 static const ID3DXMatrixStackVtbl ID3DXMatrixStack_Vtbl =
1179 {
1180 ID3DXMatrixStackImpl_QueryInterface,
1181 ID3DXMatrixStackImpl_AddRef,
1182 ID3DXMatrixStackImpl_Release,
1183 ID3DXMatrixStackImpl_Pop,
1184 ID3DXMatrixStackImpl_Push,
1185 ID3DXMatrixStackImpl_LoadIdentity,
1186 ID3DXMatrixStackImpl_LoadMatrix,
1187 ID3DXMatrixStackImpl_MultMatrix,
1188 ID3DXMatrixStackImpl_MultMatrixLocal,
1189 ID3DXMatrixStackImpl_RotateAxis,
1190 ID3DXMatrixStackImpl_RotateAxisLocal,
1191 ID3DXMatrixStackImpl_RotateYawPitchRoll,
1192 ID3DXMatrixStackImpl_RotateYawPitchRollLocal,
1193 ID3DXMatrixStackImpl_Scale,
1194 ID3DXMatrixStackImpl_ScaleLocal,
1195 ID3DXMatrixStackImpl_Translate,
1196 ID3DXMatrixStackImpl_TranslateLocal,
1197 ID3DXMatrixStackImpl_GetTop
1198 };
1199
1200 HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, ID3DXMatrixStack **stack)
1201 {
1202 struct ID3DXMatrixStackImpl *object;
1203
1204 TRACE("flags %#x, stack %p.\n", flags, stack);
1205
1206 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
1207 {
1208 *stack = NULL;
1209 return E_OUTOFMEMORY;
1210 }
1211 object->ID3DXMatrixStack_iface.lpVtbl = &ID3DXMatrixStack_Vtbl;
1212 object->ref = 1;
1213
1214 if (!(object->stack = HeapAlloc(GetProcessHeap(), 0, INITIAL_STACK_SIZE * sizeof(*object->stack))))
1215 {
1216 HeapFree(GetProcessHeap(), 0, object);
1217 *stack = NULL;
1218 return E_OUTOFMEMORY;
1219 }
1220
1221 object->current = 0;
1222 object->stack_size = INITIAL_STACK_SIZE;
1223 D3DXMatrixIdentity(&object->stack[0]);
1224
1225 TRACE("Created matrix stack %p.\n", object);
1226
1227 *stack = &object->ID3DXMatrixStack_iface;
1228 return D3D_OK;
1229 }
1230
1231 /*_________________D3DXPLANE________________*/
1232
1233 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, const D3DXVECTOR3 *pvpoint, const D3DXVECTOR3 *pvnormal)
1234 {
1235 TRACE("pout %p, pvpoint %p, pvnormal %p\n", pout, pvpoint, pvnormal);
1236
1237 pout->a = pvnormal->x;
1238 pout->b = pvnormal->y;
1239 pout->c = pvnormal->z;
1240 pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
1241 return pout;
1242 }
1243
1244 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3)
1245 {
1246 D3DXVECTOR3 edge1, edge2, normal, Nnormal;
1247
1248 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p\n", pout, pv1, pv2, pv3);
1249
1250 edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
1251 edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
1252 D3DXVec3Subtract(&edge1, pv2, pv1);
1253 D3DXVec3Subtract(&edge2, pv3, pv1);
1254 D3DXVec3Cross(&normal, &edge1, &edge2);
1255 D3DXVec3Normalize(&Nnormal, &normal);
1256 D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
1257 return pout;
1258 }
1259
1260 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, const D3DXPLANE *pp, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2)
1261 {
1262 D3DXVECTOR3 direction, normal;
1263 FLOAT dot, temp;
1264
1265 TRACE("pout %p, pp %p, pv1 %p, pv2 %p\n", pout, pp, pv1, pv2);
1266
1267 normal.x = pp->a;
1268 normal.y = pp->b;
1269 normal.z = pp->c;
1270 direction.x = pv2->x - pv1->x;
1271 direction.y = pv2->y - pv1->y;
1272 direction.z = pv2->z - pv1->z;
1273 dot = D3DXVec3Dot(&normal, &direction);
1274 if ( !dot ) return NULL;
1275 temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
1276 pout->x = pv1->x - temp * direction.x;
1277 pout->y = pv1->y - temp * direction.y;
1278 pout->z = pv1->z - temp * direction.z;
1279 return pout;
1280 }
1281
1282 D3DXPLANE * WINAPI D3DXPlaneNormalize(D3DXPLANE *out, const D3DXPLANE *p)
1283 {
1284 FLOAT norm;
1285
1286 TRACE("out %p, p %p\n", out, p);
1287
1288 norm = sqrtf(p->a * p->a + p->b * p->b + p->c * p->c);
1289 if (norm)
1290 {
1291 out->a = p->a / norm;
1292 out->b = p->b / norm;
1293 out->c = p->c / norm;
1294 out->d = p->d / norm;
1295 }
1296 else
1297 {
1298 out->a = 0.0f;
1299 out->b = 0.0f;
1300 out->c = 0.0f;
1301 out->d = 0.0f;
1302 }
1303
1304 return out;
1305 }
1306
1307 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, const D3DXPLANE *pplane, const D3DXMATRIX *pm)
1308 {
1309 const D3DXPLANE plane = *pplane;
1310
1311 TRACE("pout %p, pplane %p, pm %p\n", pout, pplane, pm);
1312
1313 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;
1314 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;
1315 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;
1316 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;
1317 return pout;
1318 }
1319
1320 D3DXPLANE* WINAPI D3DXPlaneTransformArray(D3DXPLANE* out, UINT outstride, const D3DXPLANE* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1321 {
1322 UINT i;
1323
1324 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1325
1326 for (i = 0; i < elements; ++i) {
1327 D3DXPlaneTransform(
1328 (D3DXPLANE*)((char*)out + outstride * i),
1329 (const D3DXPLANE*)((const char*)in + instride * i),
1330 matrix);
1331 }
1332 return out;
1333 }
1334
1335 /*_________________D3DXQUATERNION________________*/
1336
1337 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
1338 {
1339 D3DXQUATERNION temp1, temp2;
1340
1341 TRACE("pout %p, pq1 %p, pq2 %p, pq3 %p, f %f, g %f\n", pout, pq1, pq2, pq3, f, g);
1342
1343 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
1344 return pout;
1345 }
1346
1347 D3DXQUATERNION * WINAPI D3DXQuaternionExp(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1348 {
1349 FLOAT norm;
1350
1351 TRACE("out %p, q %p\n", out, q);
1352
1353 norm = sqrtf(q->x * q->x + q->y * q->y + q->z * q->z);
1354 if (norm)
1355 {
1356 out->x = sinf(norm) * q->x / norm;
1357 out->y = sinf(norm) * q->y / norm;
1358 out->z = sinf(norm) * q->z / norm;
1359 out->w = cosf(norm);
1360 }
1361 else
1362 {
1363 out->x = 0.0f;
1364 out->y = 0.0f;
1365 out->z = 0.0f;
1366 out->w = 1.0f;
1367 }
1368
1369 return out;
1370 }
1371
1372 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, const D3DXQUATERNION *pq)
1373 {
1374 D3DXQUATERNION out;
1375 FLOAT norm;
1376
1377 TRACE("pout %p, pq %p\n", pout, pq);
1378
1379 norm = D3DXQuaternionLengthSq(pq);
1380
1381 out.x = -pq->x / norm;
1382 out.y = -pq->y / norm;
1383 out.z = -pq->z / norm;
1384 out.w = pq->w / norm;
1385
1386 *pout =out;
1387 return pout;
1388 }
1389
1390 D3DXQUATERNION * WINAPI D3DXQuaternionLn(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1391 {
1392 FLOAT t;
1393
1394 TRACE("out %p, q %p\n", out, q);
1395
1396 if ((q->w >= 1.0f) || (q->w == -1.0f))
1397 t = 1.0f;
1398 else
1399 t = acosf(q->w) / sqrtf(1.0f - q->w * q->w);
1400
1401 out->x = t * q->x;
1402 out->y = t * q->y;
1403 out->z = t * q->z;
1404 out->w = 0.0f;
1405
1406 return out;
1407 }
1408
1409 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2)
1410 {
1411 D3DXQUATERNION out;
1412
1413 TRACE("pout %p, pq1 %p, pq2 %p\n", pout, pq1, pq2);
1414
1415 out.x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
1416 out.y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
1417 out.z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
1418 out.w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
1419 *pout = out;
1420 return pout;
1421 }
1422
1423 D3DXQUATERNION * WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *out, const D3DXQUATERNION *q)
1424 {
1425 FLOAT norm;
1426
1427 TRACE("out %p, q %p\n", out, q);
1428
1429 norm = D3DXQuaternionLength(q);
1430
1431 out->x = q->x / norm;
1432 out->y = q->y / norm;
1433 out->z = q->z / norm;
1434 out->w = q->w / norm;
1435
1436 return out;
1437 }
1438
1439 D3DXQUATERNION * WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *out, const D3DXVECTOR3 *v, FLOAT angle)
1440 {
1441 D3DXVECTOR3 temp;
1442
1443 TRACE("out %p, v %p, angle %f\n", out, v, angle);
1444
1445 D3DXVec3Normalize(&temp, v);
1446
1447 out->x = sinf(angle / 2.0f) * temp.x;
1448 out->y = sinf(angle / 2.0f) * temp.y;
1449 out->z = sinf(angle / 2.0f) * temp.z;
1450 out->w = cosf(angle / 2.0f);
1451
1452 return out;
1453 }
1454
1455 D3DXQUATERNION * WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *out, const D3DXMATRIX *m)
1456 {
1457 FLOAT s, trace;
1458
1459 TRACE("out %p, m %p\n", out, m);
1460
1461 trace = m->u.m[0][0] + m->u.m[1][1] + m->u.m[2][2] + 1.0f;
1462 if (trace > 1.0f)
1463 {
1464 s = 2.0f * sqrtf(trace);
1465 out->x = (m->u.m[1][2] - m->u.m[2][1]) / s;
1466 out->y = (m->u.m[2][0] - m->u.m[0][2]) / s;
1467 out->z = (m->u.m[0][1] - m->u.m[1][0]) / s;
1468 out->w = 0.25f * s;
1469 }
1470 else
1471 {
1472 int i, maxi = 0;
1473
1474 for (i = 1; i < 3; i++)
1475 {
1476 if (m->u.m[i][i] > m->u.m[maxi][maxi])
1477 maxi = i;
1478 }
1479
1480 switch (maxi)
1481 {
1482 case 0:
1483 s = 2.0f * sqrtf(1.0f + m->u.m[0][0] - m->u.m[1][1] - m->u.m[2][2]);
1484 out->x = 0.25f * s;
1485 out->y = (m->u.m[0][1] + m->u.m[1][0]) / s;
1486 out->z = (m->u.m[0][2] + m->u.m[2][0]) / s;
1487 out->w = (m->u.m[1][2] - m->u.m[2][1]) / s;
1488 break;
1489
1490 case 1:
1491 s = 2.0f * sqrtf(1.0f + m->u.m[1][1] - m->u.m[0][0] - m->u.m[2][2]);
1492 out->x = (m->u.m[0][1] + m->u.m[1][0]) / s;
1493 out->y = 0.25f * s;
1494 out->z = (m->u.m[1][2] + m->u.m[2][1]) / s;
1495 out->w = (m->u.m[2][0] - m->u.m[0][2]) / s;
1496 break;
1497
1498 case 2:
1499 s = 2.0f * sqrtf(1.0f + m->u.m[2][2] - m->u.m[0][0] - m->u.m[1][1]);
1500 out->x = (m->u.m[0][2] + m->u.m[2][0]) / s;
1501 out->y = (m->u.m[1][2] + m->u.m[2][1]) / s;
1502 out->z = 0.25f * s;
1503 out->w = (m->u.m[0][1] - m->u.m[1][0]) / s;
1504 break;
1505 }
1506 }
1507
1508 return out;
1509 }
1510
1511 D3DXQUATERNION * WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *out, FLOAT yaw, FLOAT pitch, FLOAT roll)
1512 {
1513 FLOAT syaw, cyaw, spitch, cpitch, sroll, croll;
1514
1515 TRACE("out %p, yaw %f, pitch %f, roll %f\n", out, yaw, pitch, roll);
1516
1517 syaw = sinf(yaw / 2.0f);
1518 cyaw = cosf(yaw / 2.0f);
1519 spitch = sinf(pitch / 2.0f);
1520 cpitch = cosf(pitch / 2.0f);
1521 sroll = sinf(roll / 2.0f);
1522 croll = cosf(roll / 2.0f);
1523
1524 out->x = syaw * cpitch * sroll + cyaw * spitch * croll;
1525 out->y = syaw * cpitch * croll - cyaw * spitch * sroll;
1526 out->z = cyaw * cpitch * sroll - syaw * spitch * croll;
1527 out->w = cyaw * cpitch * croll + syaw * spitch * sroll;
1528
1529 return out;
1530 }
1531
1532 D3DXQUATERNION * WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *out, const D3DXQUATERNION *q1,
1533 const D3DXQUATERNION *q2, FLOAT t)
1534 {
1535 FLOAT dot, temp;
1536
1537 TRACE("out %p, q1 %p, q2 %p, t %f\n", out, q1, q2, t);
1538
1539 temp = 1.0f - t;
1540 dot = D3DXQuaternionDot(q1, q2);
1541 if (dot < 0.0f)
1542 {
1543 t = -t;
1544 dot = -dot;
1545 }
1546
1547 if (1.0f - dot > 0.001f)
1548 {
1549 FLOAT theta = acosf(dot);
1550
1551 temp = sinf(theta * temp) / sinf(theta);
1552 t = sinf(theta * t) / sinf(theta);
1553 }
1554
1555 out->x = temp * q1->x + t * q2->x;
1556 out->y = temp * q1->y + t * q2->y;
1557 out->z = temp * q1->z + t * q2->z;
1558 out->w = temp * q1->w + t * q2->w;
1559
1560 return out;
1561 }
1562
1563 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3, const D3DXQUATERNION *pq4, FLOAT t)
1564 {
1565 D3DXQUATERNION temp1, temp2;
1566
1567 TRACE("pout %p, pq1 %p, pq2 %p, pq3 %p, pq4 %p, t %f\n", pout, pq1, pq2, pq3, pq4, t);
1568
1569 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
1570 return pout;
1571 }
1572
1573 static D3DXQUATERNION add_diff(const D3DXQUATERNION *q1, const D3DXQUATERNION *q2, const FLOAT add)
1574 {
1575 D3DXQUATERNION temp;
1576
1577 temp.x = q1->x + add * q2->x;
1578 temp.y = q1->y + add * q2->y;
1579 temp.z = q1->z + add * q2->z;
1580 temp.w = q1->w + add * q2->w;
1581
1582 return temp;
1583 }
1584
1585 void WINAPI D3DXQuaternionSquadSetup(D3DXQUATERNION *paout, D3DXQUATERNION *pbout, D3DXQUATERNION *pcout, const D3DXQUATERNION *pq0, const D3DXQUATERNION *pq1, const D3DXQUATERNION *pq2, const D3DXQUATERNION *pq3)
1586 {
1587 D3DXQUATERNION q, temp1, temp2, temp3, zero;
1588
1589 TRACE("paout %p, pbout %p, pcout %p, pq0 %p, pq1 %p, pq2 %p, pq3 %p\n", paout, pbout, pcout, pq0, pq1, pq2, pq3);
1590
1591 zero.x = 0.0f;
1592 zero.y = 0.0f;
1593 zero.z = 0.0f;
1594 zero.w = 0.0f;
1595
1596 if ( D3DXQuaternionDot(pq0, pq1) < 0.0f )
1597 temp2 = add_diff(&zero, pq0, -1.0f);
1598 else
1599 temp2 = *pq0;
1600
1601 if ( D3DXQuaternionDot(pq1, pq2) < 0.0f )
1602 *pcout = add_diff(&zero, pq2, -1.0f);
1603 else
1604 *pcout = *pq2;
1605
1606 if ( D3DXQuaternionDot(pcout, pq3) < 0.0f )
1607 temp3 = add_diff(&zero, pq3, -1.0f);
1608 else
1609 temp3 = *pq3;
1610
1611 D3DXQuaternionInverse(&temp1, pq1);
1612 D3DXQuaternionMultiply(&temp2, &temp1, &temp2);
1613 D3DXQuaternionLn(&temp2, &temp2);
1614 D3DXQuaternionMultiply(&q, &temp1, pcout);
1615 D3DXQuaternionLn(&q, &q);
1616 temp1 = add_diff(&temp2, &q, 1.0f);
1617 temp1.x *= -0.25f;
1618 temp1.y *= -0.25f;
1619 temp1.z *= -0.25f;
1620 temp1.w *= -0.25f;
1621 D3DXQuaternionExp(&temp1, &temp1);
1622 D3DXQuaternionMultiply(paout, pq1, &temp1);
1623
1624 D3DXQuaternionInverse(&temp1, pcout);
1625 D3DXQuaternionMultiply(&temp2, &temp1, pq1);
1626 D3DXQuaternionLn(&temp2, &temp2);
1627 D3DXQuaternionMultiply(&q, &temp1, &temp3);
1628 D3DXQuaternionLn(&q, &q);
1629 temp1 = add_diff(&temp2, &q, 1.0f);
1630 temp1.x *= -0.25f;
1631 temp1.y *= -0.25f;
1632 temp1.z *= -0.25f;
1633 temp1.w *= -0.25f;
1634 D3DXQuaternionExp(&temp1, &temp1);
1635 D3DXQuaternionMultiply(pbout, pcout, &temp1);
1636
1637 return;
1638 }
1639
1640 void WINAPI D3DXQuaternionToAxisAngle(const D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
1641 {
1642 TRACE("pq %p, paxis %p, pangle %p\n", pq, paxis, pangle);
1643
1644 paxis->x = pq->x;
1645 paxis->y = pq->y;
1646 paxis->z = pq->z;
1647 *pangle = 2.0f * acosf(pq->w);
1648 }
1649
1650 /*_________________D3DXVec2_____________________*/
1651
1652 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
1653 {
1654 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
1655
1656 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1657 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1658 return pout;
1659 }
1660
1661 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv0, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pv3, FLOAT s)
1662 {
1663 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
1664
1665 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);
1666 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);
1667 return pout;
1668 }
1669
1670 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv1, const D3DXVECTOR2 *pt1, const D3DXVECTOR2 *pv2, const D3DXVECTOR2 *pt2, FLOAT s)
1671 {
1672 FLOAT h1, h2, h3, h4;
1673
1674 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
1675
1676 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1677 h2 = s * s * s - 2.0f * s * s + s;
1678 h3 = -2.0f * s * s * s + 3.0f * s * s;
1679 h4 = s * s * s - s * s;
1680
1681 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1682 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1683 return pout;
1684 }
1685
1686 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv)
1687 {
1688 FLOAT norm;
1689
1690 TRACE("pout %p, pv %p\n", pout, pv);
1691
1692 norm = D3DXVec2Length(pv);
1693 if ( !norm )
1694 {
1695 pout->x = 0.0f;
1696 pout->y = 0.0f;
1697 }
1698 else
1699 {
1700 pout->x = pv->x / norm;
1701 pout->y = pv->y / norm;
1702 }
1703
1704 return pout;
1705 }
1706
1707 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1708 {
1709 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1710
1711 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
1712 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
1713 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
1714 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1715 return pout;
1716 }
1717
1718 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR2* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1719 {
1720 UINT i;
1721
1722 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1723
1724 for (i = 0; i < elements; ++i) {
1725 D3DXVec2Transform(
1726 (D3DXVECTOR4*)((char*)out + outstride * i),
1727 (const D3DXVECTOR2*)((const char*)in + instride * i),
1728 matrix);
1729 }
1730 return out;
1731 }
1732
1733 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1734 {
1735 D3DXVECTOR2 v;
1736 FLOAT norm;
1737
1738 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1739
1740 v = *pv;
1741 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
1742
1743 pout->x = (pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[3][0]) / norm;
1744 pout->y = (pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[3][1]) / norm;
1745
1746 return pout;
1747 }
1748
1749 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(D3DXVECTOR2* out, UINT outstride, const D3DXVECTOR2* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1750 {
1751 UINT i;
1752
1753 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1754
1755 for (i = 0; i < elements; ++i) {
1756 D3DXVec2TransformCoord(
1757 (D3DXVECTOR2*)((char*)out + outstride * i),
1758 (const D3DXVECTOR2*)((const char*)in + instride * i),
1759 matrix);
1760 }
1761 return out;
1762 }
1763
1764 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, const D3DXVECTOR2 *pv, const D3DXMATRIX *pm)
1765 {
1766 const D3DXVECTOR2 v = *pv;
1767
1768 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1769
1770 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y;
1771 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y;
1772 return pout;
1773 }
1774
1775 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(D3DXVECTOR2* out, UINT outstride, const D3DXVECTOR2 *in, UINT instride, const D3DXMATRIX *matrix, UINT elements)
1776 {
1777 UINT i;
1778
1779 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1780
1781 for (i = 0; i < elements; ++i) {
1782 D3DXVec2TransformNormal(
1783 (D3DXVECTOR2*)((char*)out + outstride * i),
1784 (const D3DXVECTOR2*)((const char*)in + instride * i),
1785 matrix);
1786 }
1787 return out;
1788 }
1789
1790 /*_________________D3DXVec3_____________________*/
1791
1792 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
1793 {
1794 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
1795
1796 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
1797 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
1798 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
1799 return pout;
1800 }
1801
1802 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv0, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pv3, FLOAT s)
1803 {
1804 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
1805
1806 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);
1807 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);
1808 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);
1809 return pout;
1810 }
1811
1812 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv1, const D3DXVECTOR3 *pt1, const D3DXVECTOR3 *pv2, const D3DXVECTOR3 *pt2, FLOAT s)
1813 {
1814 FLOAT h1, h2, h3, h4;
1815
1816 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
1817
1818 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
1819 h2 = s * s * s - 2.0f * s * s + s;
1820 h3 = -2.0f * s * s * s + 3.0f * s * s;
1821 h4 = s * s * s - s * s;
1822
1823 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1824 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1825 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1826 return pout;
1827 }
1828
1829 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv)
1830 {
1831 FLOAT norm;
1832
1833 TRACE("pout %p, pv %p\n", pout, pv);
1834
1835 norm = D3DXVec3Length(pv);
1836 if ( !norm )
1837 {
1838 pout->x = 0.0f;
1839 pout->y = 0.0f;
1840 pout->z = 0.0f;
1841 }
1842 else
1843 {
1844 pout->x = pv->x / norm;
1845 pout->y = pv->y / norm;
1846 pout->z = pv->z / norm;
1847 }
1848
1849 return pout;
1850 }
1851
1852 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DVIEWPORT9 *pviewport, const D3DXMATRIX *pprojection, const D3DXMATRIX *pview, const D3DXMATRIX *pworld)
1853 {
1854 D3DXMATRIX m;
1855
1856 TRACE("pout %p, pv %p, pviewport %p, pprojection %p, pview %p, pworld %p\n", pout, pv, pviewport, pprojection, pview, pworld);
1857
1858 D3DXMatrixIdentity(&m);
1859 if (pworld) D3DXMatrixMultiply(&m, &m, pworld);
1860 if (pview) D3DXMatrixMultiply(&m, &m, pview);
1861 if (pprojection) D3DXMatrixMultiply(&m, &m, pprojection);
1862
1863 D3DXVec3TransformCoord(pout, pv, &m);
1864
1865 if (pviewport)
1866 {
1867 pout->x = pviewport->X + ( 1.0f + pout->x ) * pviewport->Width / 2.0f;
1868 pout->y = pviewport->Y + ( 1.0f - pout->y ) * pviewport->Height / 2.0f;
1869 pout->z = pviewport->MinZ + pout->z * ( pviewport->MaxZ - pviewport->MinZ );
1870 }
1871 return pout;
1872 }
1873
1874 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)
1875 {
1876 UINT i;
1877
1878 TRACE("out %p, outstride %u, in %p, instride %u, viewport %p, projection %p, view %p, world %p, elements %u\n",
1879 out, outstride, in, instride, viewport, projection, view, world, elements);
1880
1881 for (i = 0; i < elements; ++i) {
1882 D3DXVec3Project(
1883 (D3DXVECTOR3*)((char*)out + outstride * i),
1884 (const D3DXVECTOR3*)((const char*)in + instride * i),
1885 viewport, projection, view, world);
1886 }
1887 return out;
1888 }
1889
1890 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1891 {
1892 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1893
1894 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];
1895 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];
1896 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];
1897 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];
1898 return pout;
1899 }
1900
1901 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1902 {
1903 UINT i;
1904
1905 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1906
1907 for (i = 0; i < elements; ++i) {
1908 D3DXVec3Transform(
1909 (D3DXVECTOR4*)((char*)out + outstride * i),
1910 (const D3DXVECTOR3*)((const char*)in + instride * i),
1911 matrix);
1912 }
1913 return out;
1914 }
1915
1916 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1917 {
1918 D3DXVECTOR3 out;
1919 FLOAT norm;
1920
1921 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1922
1923 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];
1924
1925 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;
1926 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;
1927 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;
1928
1929 *pout = out;
1930
1931 return pout;
1932 }
1933
1934 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(D3DXVECTOR3* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1935 {
1936 UINT i;
1937
1938 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1939
1940 for (i = 0; i < elements; ++i) {
1941 D3DXVec3TransformCoord(
1942 (D3DXVECTOR3*)((char*)out + outstride * i),
1943 (const D3DXVECTOR3*)((const char*)in + instride * i),
1944 matrix);
1945 }
1946 return out;
1947 }
1948
1949 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DXMATRIX *pm)
1950 {
1951 const D3DXVECTOR3 v = *pv;
1952
1953 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
1954
1955 pout->x = pm->u.m[0][0] * v.x + pm->u.m[1][0] * v.y + pm->u.m[2][0] * v.z;
1956 pout->y = pm->u.m[0][1] * v.x + pm->u.m[1][1] * v.y + pm->u.m[2][1] * v.z;
1957 pout->z = pm->u.m[0][2] * v.x + pm->u.m[1][2] * v.y + pm->u.m[2][2] * v.z;
1958 return pout;
1959
1960 }
1961
1962 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(D3DXVECTOR3* out, UINT outstride, const D3DXVECTOR3* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
1963 {
1964 UINT i;
1965
1966 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
1967
1968 for (i = 0; i < elements; ++i) {
1969 D3DXVec3TransformNormal(
1970 (D3DXVECTOR3*)((char*)out + outstride * i),
1971 (const D3DXVECTOR3*)((const char*)in + instride * i),
1972 matrix);
1973 }
1974 return out;
1975 }
1976
1977 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, const D3DXVECTOR3 *pv, const D3DVIEWPORT9 *pviewport, const D3DXMATRIX *pprojection, const D3DXMATRIX *pview, const D3DXMATRIX *pworld)
1978 {
1979 D3DXMATRIX m;
1980
1981 TRACE("pout %p, pv %p, pviewport %p, pprojection %p, pview %p, pworlds %p\n", pout, pv, pviewport, pprojection, pview, pworld);
1982
1983 D3DXMatrixIdentity(&m);
1984 if (pworld) D3DXMatrixMultiply(&m, &m, pworld);
1985 if (pview) D3DXMatrixMultiply(&m, &m, pview);
1986 if (pprojection) D3DXMatrixMultiply(&m, &m, pprojection);
1987 D3DXMatrixInverse(&m, NULL, &m);
1988
1989 *pout = *pv;
1990 if (pviewport)
1991 {
1992 pout->x = 2.0f * ( pout->x - pviewport->X ) / pviewport->Width - 1.0f;
1993 pout->y = 1.0f - 2.0f * ( pout->y - pviewport->Y ) / pviewport->Height;
1994 pout->z = ( pout->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
1995 }
1996 D3DXVec3TransformCoord(pout, pout, &m);
1997 return pout;
1998 }
1999
2000 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)
2001 {
2002 UINT i;
2003
2004 TRACE("out %p, outstride %u, in %p, instride %u, viewport %p, projection %p, view %p, world %p, elements %u\n",
2005 out, outstride, in, instride, viewport, projection, view, world, elements);
2006
2007 for (i = 0; i < elements; ++i) {
2008 D3DXVec3Unproject(
2009 (D3DXVECTOR3*)((char*)out + outstride * i),
2010 (const D3DXVECTOR3*)((const char*)in + instride * i),
2011 viewport, projection, view, world);
2012 }
2013 return out;
2014 }
2015
2016 /*_________________D3DXVec4_____________________*/
2017
2018 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
2019 {
2020 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p, f %f, g %f\n", pout, pv1, pv2, pv3, f, g);
2021
2022 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
2023 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
2024 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
2025 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
2026 return pout;
2027 }
2028
2029 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv0, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3, FLOAT s)
2030 {
2031 TRACE("pout %p, pv0 %p, pv1 %p, pv2 %p, pv3 %p, s %f\n", pout, pv0, pv1, pv2, pv3, s);
2032
2033 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);
2034 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);
2035 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);
2036 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);
2037 return pout;
2038 }
2039
2040 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pv3)
2041 {
2042 D3DXVECTOR4 out;
2043
2044 TRACE("pout %p, pv1 %p, pv2 %p, pv3 %p\n", pout, pv1, pv2, pv3);
2045
2046 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);
2047 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));
2048 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);
2049 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));
2050 *pout = out;
2051 return pout;
2052 }
2053
2054 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv1, const D3DXVECTOR4 *pt1, const D3DXVECTOR4 *pv2, const D3DXVECTOR4 *pt2, FLOAT s)
2055 {
2056 FLOAT h1, h2, h3, h4;
2057
2058 TRACE("pout %p, pv1 %p, pt1 %p, pv2 %p, pt2 %p, s %f\n", pout, pv1, pt1, pv2, pt2, s);
2059
2060 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
2061 h2 = s * s * s - 2.0f * s * s + s;
2062 h3 = -2.0f * s * s * s + 3.0f * s * s;
2063 h4 = s * s * s - s * s;
2064
2065 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
2066 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
2067 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
2068 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
2069 return pout;
2070 }
2071
2072 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv)
2073 {
2074 FLOAT norm;
2075
2076 TRACE("pout %p, pv %p\n", pout, pv);
2077
2078 norm = D3DXVec4Length(pv);
2079
2080 pout->x = pv->x / norm;
2081 pout->y = pv->y / norm;
2082 pout->z = pv->z / norm;
2083 pout->w = pv->w / norm;
2084
2085 return pout;
2086 }
2087
2088 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, const D3DXVECTOR4 *pv, const D3DXMATRIX *pm)
2089 {
2090 D3DXVECTOR4 out;
2091
2092 TRACE("pout %p, pv %p, pm %p\n", pout, pv, pm);
2093
2094 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;
2095 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;
2096 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;
2097 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;
2098 *pout = out;
2099 return pout;
2100 }
2101
2102 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(D3DXVECTOR4* out, UINT outstride, const D3DXVECTOR4* in, UINT instride, const D3DXMATRIX* matrix, UINT elements)
2103 {
2104 UINT i;
2105
2106 TRACE("out %p, outstride %u, in %p, instride %u, matrix %p, elements %u\n", out, outstride, in, instride, matrix, elements);
2107
2108 for (i = 0; i < elements; ++i) {
2109 D3DXVec4Transform(
2110 (D3DXVECTOR4*)((char*)out + outstride * i),
2111 (const D3DXVECTOR4*)((const char*)in + instride * i),
2112 matrix);
2113 }
2114 return out;
2115 }
2116
2117 unsigned short float_32_to_16(const float in)
2118 {
2119 int exp = 0, origexp;
2120 float tmp = fabsf(in);
2121 int sign = (copysignf(1, in) < 0);
2122 unsigned int mantissa;
2123 unsigned short ret;
2124
2125 /* Deal with special numbers */
2126 if (isinf(in)) return (sign ? 0xffff : 0x7fff);
2127 if (isnan(in)) return (sign ? 0xffff : 0x7fff);
2128 if (in == 0.0f) return (sign ? 0x8000 : 0x0000);
2129
2130 if (tmp < (float)(1u << 10))
2131 {
2132 do
2133 {
2134 tmp *= 2.0f;
2135 exp--;
2136 } while (tmp < (float)(1u << 10));
2137 }
2138 else if (tmp >= (float)(1u << 11))
2139 {
2140 do
2141 {
2142 tmp /= 2.0f;
2143 exp++;
2144 } while (tmp >= (float)(1u << 11));
2145 }
2146
2147 exp += 10; /* Normalize the mantissa */
2148 exp += 15; /* Exponent is encoded with excess 15 */
2149
2150 origexp = exp;
2151
2152 mantissa = (unsigned int) tmp;
2153 if ((tmp - mantissa == 0.5f && mantissa % 2 == 1) || /* round half to even */
2154 (tmp - mantissa > 0.5f))
2155 {
2156 mantissa++; /* round to nearest, away from zero */
2157 }
2158 if (mantissa == 2048)
2159 {
2160 mantissa = 1024;
2161 exp++;
2162 }
2163
2164 if (exp > 31)
2165 {
2166 /* too big */
2167 ret = 0x7fff; /* INF */
2168 }
2169 else if (exp <= 0)
2170 {
2171 unsigned int rounding = 0;
2172
2173 /* Denormalized half float */
2174
2175 /* return 0x0000 (=0.0) for numbers too small to represent in half floats */
2176 if (exp < -11)
2177 return (sign ? 0x8000 : 0x0000);
2178
2179 exp = origexp;
2180
2181 /* the 13 extra bits from single precision are used for rounding */
2182 mantissa = (unsigned int)(tmp * (1u << 13));
2183 mantissa >>= 1 - exp; /* denormalize */
2184
2185 mantissa -= ~(mantissa >> 13) & 1; /* round half to even */
2186 /* remove 13 least significant bits to get half float precision */
2187 mantissa >>= 12;
2188 rounding = mantissa & 1;
2189 mantissa >>= 1;
2190
2191 ret = mantissa + rounding;
2192 }
2193 else
2194 {
2195 ret = (exp << 10) | (mantissa & 0x3ff);
2196 }
2197
2198 ret |= ((sign ? 1 : 0) << 15); /* Add the sign */
2199 return ret;
2200 }
2201
2202 D3DXFLOAT16 *WINAPI D3DXFloat32To16Array(D3DXFLOAT16 *pout, const FLOAT *pin, UINT n)
2203 {
2204 unsigned int i;
2205
2206 TRACE("pout %p, pin %p, n %u\n", pout, pin, n);
2207
2208 for (i = 0; i < n; ++i)
2209 {
2210 pout[i].value = float_32_to_16(pin[i]);
2211 }
2212
2213 return pout;
2214 }
2215
2216 /* Native d3dx9's D3DXFloat16to32Array lacks support for NaN and Inf. Specifically, e = 16 is treated as a
2217 * regular number - e.g., 0x7fff is converted to 131008.0 and 0xffff to -131008.0. */
2218 float float_16_to_32(const unsigned short in)
2219 {
2220 const unsigned short s = (in & 0x8000);
2221 const unsigned short e = (in & 0x7C00) >> 10;
2222 const unsigned short m = in & 0x3FF;
2223 const float sgn = (s ? -1.0f : 1.0f);
2224
2225 if (e == 0)
2226 {
2227 if (m == 0) return sgn * 0.0f; /* +0.0 or -0.0 */
2228 else return sgn * powf(2, -14.0f) * (m / 1024.0f);
2229 }
2230 else
2231 {
2232 return sgn * powf(2, e - 15.0f) * (1.0f + (m / 1024.0f));
2233 }
2234 }
2235
2236 FLOAT *WINAPI D3DXFloat16To32Array(FLOAT *pout, const D3DXFLOAT16 *pin, UINT n)
2237 {
2238 unsigned int i;
2239
2240 TRACE("pout %p, pin %p, n %u\n", pout, pin, n);
2241
2242 for (i = 0; i < n; ++i)
2243 {
2244 pout[i] = float_16_to_32(pin[i].value);
2245 }
2246
2247 return pout;
2248 }
2249
2250 /*_________________D3DXSH________________*/
2251
2252 FLOAT* WINAPI D3DXSHAdd(FLOAT *out, UINT order, const FLOAT *a, const FLOAT *b)
2253 {
2254 UINT i;
2255
2256 TRACE("out %p, order %u, a %p, b %p\n", out, order, a, b);
2257
2258 for (i = 0; i < order * order; i++)
2259 out[i] = a[i] + b[i];
2260
2261 return out;
2262 }
2263
2264 FLOAT WINAPI D3DXSHDot(UINT order, const FLOAT *a, const FLOAT *b)
2265 {
2266 FLOAT s;
2267 UINT i;
2268
2269 TRACE("order %u, a %p, b %p\n", order, a, b);
2270
2271 s = a[0] * b[0];
2272 for (i = 1; i < order * order; i++)
2273 s += a[i] * b[i];
2274
2275 return s;
2276 }
2277
2278 static void weightedcapintegrale(FLOAT *out, UINT order, FLOAT angle)
2279 {
2280 FLOAT coeff[3];
2281
2282 coeff[0] = cosf(angle);
2283
2284 out[0] = 2.0f * D3DX_PI * (1.0f - coeff[0]);
2285 out[1] = D3DX_PI * sinf(angle) * sinf(angle);
2286 if (order <= 2)
2287 return;
2288
2289 out[2] = coeff[0] * out[1];
2290 if (order == 3)
2291 return;
2292
2293 coeff[1] = coeff[0] * coeff[0];
2294 coeff[2] = coeff[1] * coeff[1];
2295
2296 out[3] = D3DX_PI * (-1.25f * coeff[2] + 1.5f * coeff[1] - 0.25f);
2297 if (order == 4)
2298 return;
2299
2300 out[4] = -0.25f * D3DX_PI * coeff[0] * (7.0f * coeff[2] - 10.0f * coeff[1] + 3.0f);
2301 if (order == 5)
2302 return;
2303
2304 out[5] = D3DX_PI * (-2.625f * coeff[2] * coeff[1] + 4.375f * coeff[2] - 1.875f * coeff[1] + 0.125f);
2305 }
2306
2307 HRESULT WINAPI D3DXSHEvalConeLight(UINT order, const D3DXVECTOR3 *dir, FLOAT radius,
2308 FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *rout, FLOAT *gout, FLOAT *bout)
2309 {
2310 FLOAT cap[6], clamped_angle, norm, scale, temp;
2311 UINT i, index, j;
2312
2313 TRACE("order %u, dir %p, radius %f, red %f, green %f, blue %f, rout %p, gout %p, bout %p\n",
2314 order, dir, radius, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2315
2316 if (radius <= 0.0f)
2317 return D3DXSHEvalDirectionalLight(order, dir, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2318
2319 clamped_angle = (radius > D3DX_PI / 2.0f) ? (D3DX_PI / 2.0f) : radius;
2320 norm = sinf(clamped_angle) * sinf(clamped_angle);
2321
2322 if (order > D3DXSH_MAXORDER)
2323 {
2324 WARN("Order clamped at D3DXSH_MAXORDER\n");
2325 order = D3DXSH_MAXORDER;
2326 }
2327
2328 weightedcapintegrale(cap, order, radius);
2329 D3DXSHEvalDirection(rout, order, dir);
2330
2331 for (i = 0; i < order; i++)
2332 {
2333 scale = cap[i] / norm;
2334
2335 for (j = 0; j < 2 * i + 1; j++)
2336 {
2337 index = i * i + j;
2338 temp = rout[index] * scale;
2339
2340 rout[index] = temp * Rintensity;
2341 if (gout)
2342 gout[index] = temp * Gintensity;
2343 if (bout)
2344 bout[index] = temp * Bintensity;
2345 }
2346 }
2347
2348 return D3D_OK;
2349 }
2350
2351 FLOAT* WINAPI D3DXSHEvalDirection(FLOAT *out, UINT order, const D3DXVECTOR3 *dir)
2352 {
2353 const FLOAT dirxx = dir->x * dir->x;
2354 const FLOAT dirxy = dir->x * dir->y;
2355 const FLOAT dirxz = dir->x * dir->z;
2356 const FLOAT diryy = dir->y * dir->y;
2357 const FLOAT diryz = dir->y * dir->z;
2358 const FLOAT dirzz = dir->z * dir->z;
2359 const FLOAT dirxxxx = dirxx * dirxx;
2360 const FLOAT diryyyy = diryy * diryy;
2361 const FLOAT dirzzzz = dirzz * dirzz;
2362 const FLOAT dirxyxy = dirxy * dirxy;
2363
2364 TRACE("out %p, order %u, dir %p\n", out, order, dir);
2365
2366 if ((order < D3DXSH_MINORDER) || (order > D3DXSH_MAXORDER))
2367 return out;
2368
2369 out[0] = 0.5f / sqrtf(D3DX_PI);
2370 out[1] = -0.5f / sqrtf(D3DX_PI / 3.0f) * dir->y;
2371 out[2] = 0.5f / sqrtf(D3DX_PI / 3.0f) * dir->z;
2372 out[3] = -0.5f / sqrtf(D3DX_PI / 3.0f) * dir->x;
2373 if (order == 2)
2374 return out;
2375
2376 out[4] = 0.5f / sqrtf(D3DX_PI / 15.0f) * dirxy;
2377 out[5] = -0.5f / sqrtf(D3DX_PI / 15.0f) * diryz;
2378 out[6] = 0.25f / sqrtf(D3DX_PI / 5.0f) * (3.0f * dirzz - 1.0f);
2379 out[7] = -0.5f / sqrtf(D3DX_PI / 15.0f) * dirxz;
2380 out[8] = 0.25f / sqrtf(D3DX_PI / 15.0f) * (dirxx - diryy);
2381 if (order == 3)
2382 return out;
2383
2384 out[9] = -sqrtf(70.0f / D3DX_PI) / 8.0f * dir->y * (3.0f * dirxx - diryy);
2385 out[10] = sqrtf(105.0f / D3DX_PI) / 2.0f * dirxy * dir->z;
2386 out[11] = -sqrtf(42.0f / D3DX_PI) / 8.0f * dir->y * (-1.0f + 5.0f * dirzz);
2387 out[12] = sqrtf(7.0f / D3DX_PI) / 4.0f * dir->z * (5.0f * dirzz - 3.0f);
2388 out[13] = sqrtf(42.0f / D3DX_PI) / 8.0f * dir->x * (1.0f - 5.0f * dirzz);
2389 out[14] = sqrtf(105.0f / D3DX_PI) / 4.0f * dir->z * (dirxx - diryy);
2390 out[15] = -sqrtf(70.0f / D3DX_PI) / 8.0f * dir->x * (dirxx - 3.0f * diryy);
2391 if (order == 4)
2392 return out;
2393
2394 out[16] = 0.75f * sqrtf(35.0f / D3DX_PI) * dirxy * (dirxx - diryy);
2395 out[17] = 3.0f * dir->z * out[9];
2396 out[18] = 0.75f * sqrtf(5.0f / D3DX_PI) * dirxy * (7.0f * dirzz - 1.0f);
2397 out[19] = 0.375f * sqrtf(10.0f / D3DX_PI) * diryz * (3.0f - 7.0f * dirzz);
2398 out[20] = 3.0f / (16.0f * sqrtf(D3DX_PI)) * (35.0f * dirzzzz - 30.f * dirzz + 3.0f);
2399 out[21] = 0.375f * sqrtf(10.0f / D3DX_PI) * dirxz * (3.0f - 7.0f * dirzz);
2400 out[22] = 0.375f * sqrtf(5.0f / D3DX_PI) * (dirxx - diryy) * (7.0f * dirzz - 1.0f);
2401 out[23] = 3.0f * dir->z * out[15];
2402 out[24] = 3.0f / 16.0f * sqrtf(35.0f / D3DX_PI) * (dirxxxx - 6.0f * dirxyxy + diryyyy);
2403 if (order == 5)
2404 return out;
2405
2406 out[25] = -3.0f/ 32.0f * sqrtf(154.0f / D3DX_PI) * dir->y * (5.0f * dirxxxx - 10.0f * dirxyxy + diryyyy);
2407 out[26] = 0.75f * sqrtf(385.0f / D3DX_PI) * dirxy * dir->z * (dirxx - diryy);
2408 out[27] = sqrtf(770.0f / D3DX_PI) / 32.0f * dir->y * (3.0f * dirxx - diryy) * (1.0f - 9.0f * dirzz);
2409 out[28] = sqrtf(1155.0f / D3DX_PI) / 4.0f * dirxy * dir->z * (3.0f * dirzz - 1.0f);
2410 out[29] = sqrtf(165.0f / D3DX_PI) / 16.0f * dir->y * (14.0f * dirzz - 21.0f * dirzzzz - 1.0f);
2411 out[30] = sqrtf(11.0f / D3DX_PI) / 16.0f * dir->z * (63.0f * dirzzzz - 70.0f * dirzz + 15.0f);
2412 out[31] = sqrtf(165.0f / D3DX_PI) / 16.0f * dir->x * (14.0f * dirzz - 21.0f * dirzzzz - 1.0f);
2413 out[32] = sqrtf(1155.0f / D3DX_PI) / 8.0f * dir->z * (dirxx - diryy) * (3.0f * dirzz - 1.0f);
2414 out[33] = sqrtf(770.0f / D3DX_PI) / 32.0f * dir->x * (dirxx - 3.0f * diryy) * (1.0f - 9.0f * dirzz);
2415 out[34] = 3.0f / 16.0f * sqrtf(385.0f / D3DX_PI) * dir->z * (dirxxxx - 6.0f * dirxyxy + diryyyy);
2416 out[35] = -3.0f/ 32.0f * sqrtf(154.0f / D3DX_PI) * dir->x * (dirxxxx - 10.0f * dirxyxy + 5.0f * diryyyy);
2417
2418 return out;
2419 }
2420
2421 HRESULT WINAPI D3DXSHEvalDirectionalLight(UINT order, const D3DXVECTOR3 *dir, FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *Rout, FLOAT *Gout, FLOAT *Bout)
2422 {
2423 FLOAT s, temp;
2424 UINT j;
2425
2426 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);
2427
2428 s = 0.75f;
2429 if ( order > 2 )
2430 s += 5.0f / 16.0f;
2431 if ( order > 4 )
2432 s -= 3.0f / 32.0f;
2433 s /= D3DX_PI;
2434
2435 D3DXSHEvalDirection(Rout, order, dir);
2436 for (j = 0; j < order * order; j++)
2437 {
2438 temp = Rout[j] / s;
2439
2440 Rout[j] = Rintensity * temp;
2441 if ( Gout )
2442 Gout[j] = Gintensity * temp;
2443 if ( Bout )
2444 Bout[j] = Bintensity * temp;
2445 }
2446
2447 return D3D_OK;
2448 }
2449
2450 HRESULT WINAPI D3DXSHEvalHemisphereLight(UINT order, const D3DXVECTOR3 *dir, D3DXCOLOR top, D3DXCOLOR bottom,
2451 FLOAT *rout, FLOAT *gout, FLOAT *bout)
2452 {
2453 FLOAT a[2], temp[4];
2454 UINT i, j;
2455
2456 TRACE("order %u, dir %p, rout %p, gout %p, bout %p\n", order, dir, rout, gout, bout);
2457
2458 D3DXSHEvalDirection(temp, 2, dir);
2459
2460 a[0] = (top.r + bottom.r) * 3.0f * D3DX_PI;
2461 a[1] = (top.r - bottom.r) * D3DX_PI;
2462 for (i = 0; i < order; i++)
2463 for (j = 0; j < 2 * i + 1; j++)
2464 if (i < 2)
2465 rout[i * i + j] = temp[i * i + j] * a[i];
2466 else
2467 rout[i * i + j] = 0.0f;
2468
2469 if (gout)
2470 {
2471 a[0] = (top.g + bottom.g) * 3.0f * D3DX_PI;
2472 a[1] = (top.g - bottom.g) * D3DX_PI;
2473 for (i = 0; i < order; i++)
2474 for (j = 0; j < 2 * i + 1; j++)
2475 if (i < 2)
2476 gout[i * i + j] = temp[i * i + j] * a[i];
2477 else
2478 gout[i * i + j] = 0.0f;
2479 }
2480
2481 if (bout)
2482 {
2483 a[0] = (top.b + bottom.b) * 3.0f * D3DX_PI;
2484 a[1] = (top.b - bottom.b) * D3DX_PI;
2485 for (i = 0; i < order; i++)
2486 for (j = 0; j < 2 * i + 1; j++)
2487 if (i < 2)
2488 bout[i * i + j] = temp[i * i + j] * a[i];
2489 else
2490 bout[i * i + j] = 0.0f;
2491 }
2492
2493 return D3D_OK;
2494 }
2495
2496 HRESULT WINAPI D3DXSHEvalSphericalLight(UINT order, const D3DXVECTOR3 *dir, FLOAT radius,
2497 FLOAT Rintensity, FLOAT Gintensity, FLOAT Bintensity, FLOAT *rout, FLOAT *gout, FLOAT *bout)
2498 {
2499 D3DXVECTOR3 normal;
2500 FLOAT cap[6], clamped_angle, dist, temp;
2501 UINT i, index, j;
2502
2503 TRACE("order %u, dir %p, radius %f, red %f, green %f, blue %f, rout %p, gout %p, bout %p\n",
2504 order, dir, radius, Rintensity, Gintensity, Bintensity, rout, gout, bout);
2505
2506 if (order > D3DXSH_MAXORDER)
2507 {
2508 WARN("Order clamped at D3DXSH_MAXORDER\n");
2509 order = D3DXSH_MAXORDER;
2510 }
2511
2512 if (radius < 0.0f)
2513 radius = -radius;
2514
2515 dist = D3DXVec3Length(dir);
2516 clamped_angle = (dist <= radius) ? D3DX_PI / 2.0f : asinf(radius / dist);
2517
2518 weightedcapintegrale(cap, order, clamped_angle);
2519 D3DXVec3Normalize(&normal, dir);
2520 D3DXSHEvalDirection(rout, order, &normal);
2521
2522 for (i = 0; i < order; i++)
2523 for (j = 0; j < 2 * i + 1; j++)
2524 {
2525 index = i * i + j;
2526 temp = rout[index] * cap[i];
2527
2528 rout[index] = temp * Rintensity;
2529 if (gout)
2530 gout[index] = temp * Gintensity;
2531 if (bout)
2532 bout[index] = temp * Bintensity;
2533 }
2534
2535 return D3D_OK;
2536 }
2537
2538 FLOAT * WINAPI D3DXSHMultiply2(FLOAT *out, const FLOAT *a, const FLOAT *b)
2539 {
2540 FLOAT ta, tb;
2541
2542 TRACE("out %p, a %p, b %p\n", out, a, b);
2543
2544 ta = 0.28209479f * a[0];
2545 tb = 0.28209479f * b[0];
2546
2547 out[0] = 0.28209479f * D3DXSHDot(2, a, b);
2548 out[1] = ta * b[1] + tb * a[1];
2549 out[2] = ta * b[2] + tb * a[2];
2550 out[3] = ta * b[3] + tb * a[3];
2551
2552 return out;
2553 }
2554
2555 FLOAT * WINAPI D3DXSHMultiply3(FLOAT *out, const FLOAT *a, const FLOAT *b)
2556 {
2557 FLOAT t, ta, tb;
2558
2559 TRACE("out %p, a %p, b %p\n", out, a, b);
2560
2561 out[0] = 0.28209479f * a[0] * b[0];
2562
2563 ta = 0.28209479f * a[0] - 0.12615662f * a[6] - 0.21850968f * a[8];
2564 tb = 0.28209479f * b[0] - 0.12615662f * b[6] - 0.21850968f * b[8];
2565 out[1] = ta * b[1] + tb * a[1];
2566 t = a[1] * b[1];
2567 out[0] += 0.28209479f * t;
2568 out[6] = -0.12615662f * t;
2569 out[8] = -0.21850968f * t;
2570
2571 ta = 0.21850968f * a[5];
2572 tb = 0.21850968f * b[5];
2573 out[1] += ta * b[2] + tb * a[2];
2574 out[2] = ta * b[1] + tb * a[1];
2575 t = a[1] * b[2] +a[2] * b[1];
2576 out[5] = 0.21850968f * t;
2577
2578 ta = 0.21850968f * a[4];
2579 tb = 0.21850968f * b[4];
2580 out[1] += ta * b[3] + tb * a[3];
2581 out[3] = ta * b[1] + tb * a[1];
2582 t = a[1] * b[3] + a[3] * b[1];
2583 out[4] = 0.21850968f * t;
2584
2585 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2586 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2587 out[2] += ta * b[2] + tb * a[2];
2588 t = a[2] * b[2];
2589 out[0] += 0.28209480f * t;
2590 out[6] += 0.25231326f * t;
2591
2592 ta = 0.21850969f * a[7];
2593 tb = 0.21850969f * b[7];
2594 out[2] += ta * b[3] + tb * a[3];
2595 out[3] += ta * b[2] + tb * a[2];
2596 t = a[2] * b[3] + a[3] * b[2];
2597 out[7] = 0.21850969f * t;
2598
2599 ta = 0.28209479f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2600 tb = 0.28209479f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2601 out[3] += ta * b[3] + tb * a[3];
2602 t = a[3] * b[3];
2603 out[0] += 0.28209479f * t;
2604 out[6] -= 0.12615663f * t;
2605 out[8] += 0.21850969f * t;
2606
2607 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2608 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2609 out[4] += ta * b[4] + tb * a[4];
2610 t = a[4] * b[4];
2611 out[0] += 0.28209479f * t;
2612 out[6] -= 0.18022375f * t;
2613
2614 ta = 0.15607835f * a[7];
2615 tb = 0.15607835f * b[7];
2616 out[4] += ta * b[5] + tb * a[5];
2617 out[5] += ta * b[4] + tb * a[4];
2618 t = a[4] * b[5] + a[5] * b[4];
2619 out[7] += 0.15607834f * t;
2620
2621 ta = 0.28209479f * a[0] + 0.09011186f * a[6] - 0.15607835f * a[8];
2622 tb = 0.28209479f * b[0] + 0.09011186f * b[6] - 0.15607835f * b[8];
2623 out[5] += ta * b[5] + tb * a[5];
2624 t = a[5] * b[5];
2625 out[0] += 0.28209479f * t;
2626 out[6] += 0.09011186f * t;
2627 out[8] -= 0.15607835f * t;
2628
2629 ta = 0.28209480f * a[0];
2630 tb = 0.28209480f * b[0];
2631 out[6] += ta * b[6] + tb * a[6];
2632 t = a[6] * b[6];
2633 out[0] += 0.28209480f * t;
2634 out[6] += 0.18022376f * t;
2635
2636 ta = 0.28209479f * a[0] + 0.09011186f * a[6] + 0.15607835f * a[8];
2637 tb = 0.28209479f * b[0] + 0.09011186f * b[6] + 0.15607835f * b[8];
2638 out[7] += ta * b[7] + tb * a[7];
2639 t = a[7] * b[7];
2640 out[0] += 0.28209479f * t;
2641 out[6] += 0.09011186f * t;
2642 out[8] += 0.15607835f * t;
2643
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;
2650
2651 return out;
2652 }
2653
2654 FLOAT * WINAPI D3DXSHMultiply4(FLOAT *out, const FLOAT *a, const FLOAT *b)
2655 {
2656 FLOAT ta, tb, t;
2657
2658 TRACE("out %p, a %p, b %p\n", out, a, b);
2659
2660 out[0] = 0.28209479f * a[0] * b[0];
2661
2662 ta = 0.28209479f * a[0] - 0.12615663f * a[6] - 0.21850969f * a[8];
2663 tb = 0.28209479f * b[0] - 0.12615663f * b[6] - 0.21850969f * b[8];
2664 out[1] = ta * b[1] + tb * a[1];
2665 t = a[1] * b[1];
2666 out[0] += 0.28209479f * t;
2667 out[6] = -0.12615663f * t;
2668 out[8] = -0.21850969f * t;
2669
2670 ta = 0.21850969f * a[3] - 0.05839917f * a[13] - 0.22617901f * a[15];
2671 tb = 0.21850969f * b[3] - 0.05839917f * b[13] - 0.22617901f * b[15];
2672 out[1] += ta * b[4] + tb * a[4];
2673 out[4] = ta * b[1] + tb * a[1];
2674 t = a[1] * b[4] + a[4] * b[1];
2675 out[3] = 0.21850969f * t;
2676 out[13] = -0.05839917f * t;
2677 out[15] = -0.22617901f * t;
2678
2679 ta = 0.21850969f * a[2] - 0.14304817f * a[12] - 0.18467439f * a[14];
2680 tb = 0.21850969f * b[2] - 0.14304817f * b[12] - 0.18467439f * b[14];
2681 out[1] += ta * b[5] + tb * a[5];
2682 out[5] = ta * b[1] + tb * a[1];
2683 t = a[1] * b[5] + a[5] * b[1];
2684 out[2] = 0.21850969f * t;
2685 out[12] = -0.14304817f * t;
2686 out[14] = -0.18467439f * t;
2687
2688 ta = 0.20230066f * a[11];
2689 tb = 0.20230066f * b[11];
2690 out[1] += ta * b[6] + tb * a[6];
2691 out[6] += ta * b[1] + tb * a[1];
2692 t = a[1] * b[6] + a[6] * b[1];
2693 out[11] = 0.20230066f * t;
2694
2695 ta = 0.22617901f * a[9] + 0.05839917f * a[11];
2696 tb = 0.22617901f * b[9] + 0.05839917f * b[11];
2697 out[1] += ta * b[8] + tb * a[8];
2698 out[8] += ta * b[1] + tb * a[1];
2699 t = a[1] * b[8] + a[8] * b[1];
2700 out[9] = 0.22617901f * t;
2701 out[11] += 0.05839917f * t;
2702
2703 ta = 0.28209480f * a[0] + 0.25231326f * a[6];
2704 tb = 0.28209480f * b[0] + 0.25231326f * b[6];
2705 out[2] += ta * b[2] + tb * a[2];
2706 t = a[2] * b[2];
2707 out[0] += 0.28209480f * t;
2708 out[6] += 0.25231326f * t;
2709
2710 ta = 0.24776671f * a[12];
2711 tb = 0.24776671f * b[12];
2712 out[2] += ta * b[6] + tb * a[6];
2713 out[6] += ta * b[2] + tb * a[2];
2714 t = a[2] * b[6] + a[6] * b[2];
2715 out[12] += 0.24776671f * t;
2716
2717 ta = 0.28209480f * a[0] - 0.12615663f * a[6] + 0.21850969f * a[8];
2718 tb = 0.28209480f * b[0] - 0.12615663f * b[6] + 0.21850969f * b[8];
2719 out[3] += ta * b[3] + tb * a[3];
2720 t = a[3] * b[3];
2721 out[0] += 0.28209480f * t;
2722 out[6] -= 0.12615663f * t;
2723 out[8] += 0.21850969f * t;
2724
2725 ta = 0.20230066f * a[13];
2726 tb = 0.20230066f * b[13];
2727 out[3] += ta * b[6] + tb * a[6];
2728 out[6] += ta * b[3] + tb * a[3];
2729 t = a[3] * b[6] + a[6] * b[3];
2730 out[13] += 0.20230066f * t;
2731
2732 ta = 0.21850969f * a[2] - 0.14304817f * a[12] + 0.18467439f * a[14];
2733 tb = 0.21850969f * b[2] - 0.14304817f * b[12] + 0.18467439f * b[14];
2734 out[3] += ta * b[7] + tb * a[7];
2735 out[7] = ta * b[3] + tb * a[3];
2736 t = a[3] * b[7] + a[7] * b[3];
2737 out[2] += 0.21850969f * t;
2738 out[12] -= 0.14304817f * t;
2739 out[14] += 0.18467439f * t;
2740
2741 ta = -0.05839917f * a[13] + 0.22617901f * a[15];
2742 tb = -0.05839917f * b[13] + 0.22617901f * b[15];
2743 out[3] += ta * b[8] + tb * a[8];
2744 out[8] += ta * b[3] + tb * a[3];
2745 t = a[3] * b[8] + a[8] * b[3];
2746 out[13] -= 0.05839917f * t;
2747 out[15] += 0.22617901f * t;
2748
2749 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2750 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2751 out[4] += ta * b[4] + tb * a[4];
2752 t = a[4] * b[4];
2753 out[0] += 0.28209479f * t;
2754 out[6] -= 0.18022375f * t;
2755
2756 ta = 0.15607835f * a[7];
2757 tb = 0.15607835f * b[7];
2758 out[4] += ta * b[5] + tb * a[5];
2759 out[5] += ta * b[4] + tb * a[4];
2760 t = a[4] * b[5] + a[5] * b[4];
2761 out[7] += 0.15607835f * t;
2762
2763 ta = 0.22617901f * a[3] - 0.09403160f * a[13];
2764 tb = 0.22617901f * b[3] - 0.09403160f * b[13];
2765 out[4] += ta * b[9] + tb * a[9];
2766 out[9] += ta * b[4] + tb * a[4];
2767 t = a[4] * b[9] + a[9] * b[4];
2768 out[3] += 0.22617901f * t;
2769 out[13] -= 0.09403160f * t;
2770
2771 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2772 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2773 out[4] += ta * b[10] + tb * a [10];
2774 out[10] = ta * b[4] + tb * a[4];
2775 t = a[4] * b[10] + a[10] * b[4];
2776 out[2] += 0.18467439f * t;
2777 out[12] -= 0.18806319f * t;
2778
2779 ta = -0.05839917f * a[3] + 0.14567312f * a[13] + 0.09403160f * a[15];
2780 tb = -0.05839917f * b[3] + 0.14567312f * b[13] + 0.09403160f * b[15];
2781 out[4] += ta * b[11] + tb * a[11];
2782 out[11] += ta * b[4] + tb * a[4];
2783 t = a[4] * b[11] + a[11] * b[4];
2784 out[3] -= 0.05839917f * t;
2785 out[13] += 0.14567312f * t;
2786 out[15] += 0.09403160f * t;
2787
2788 ta = 0.28209479f * a[0] + 0.09011186f * a[6] - 0.15607835f * a[8];
2789 tb = 0.28209479f * b[0] + 0.09011186f * b[6] - 0.15607835f * b[8];
2790 out[5] += ta * b[5] + tb * a[5];
2791 t = a[5] * b[5];
2792 out[0] += 0.28209479f * t;
2793 out[6] += 0.09011186f * t;
2794 out[8] -= 0.15607835f * t;
2795
2796 ta = 0.14867701f * a[14];
2797 tb = 0.14867701f * b[14];
2798 out[5] += ta * b[9] + tb * a[9];
2799 out[9] += ta * b[5] + tb * a[5];
2800 t = a[5] * b[9] + a[9] * b[5];
2801 out[14] += 0.14867701f * t;
2802
2803 ta = 0.18467439f * a[3] + 0.11516472f * a[13] - 0.14867701f * a[15];
2804 tb = 0.18467439f * b[3] + 0.11516472f * b[13] - 0.14867701f * b[15];
2805 out[5] += ta * b[10] + tb * a[10];
2806 out[10] += ta * b[5] + tb * a[5];
2807 t = a[5] * b[10] + a[10] * b[5];
2808 out[3] += 0.18467439f * t;
2809 out[13] += 0.11516472f * t;
2810 out[15] -= 0.14867701f * t;
2811
2812 ta = 0.23359668f * a[2] + 0.05947080f * a[12] - 0.11516472f * a[14];
2813 tb = 0.23359668f * b[2] + 0.05947080f * b[12] - 0.11516472f * b[14];
2814 out[5] += ta * b[11] + tb * a[11];
2815 out[11] += ta * b[5] + tb * a[5];
2816 t = a[5] * b[11] + a[11] * b[5];
2817 out[2] += 0.23359668f * t;
2818 out[12] += 0.05947080f * t;
2819 out[14] -= 0.11516472f * t;
2820
2821 ta = 0.28209479f * a[0];
2822 tb = 0.28209479f * b[0];
2823 out[6] += ta * b[6] + tb * a[6];
2824 t = a[6] * b[6];
2825 out[0] += 0.28209479f * t;
2826 out[6] += 0.18022376f * t;
2827
2828 ta = 0.09011186f * a[6] + 0.28209479f * a[0] + 0.15607835f * a[8];
2829 tb = 0.09011186f * b[6] + 0.28209479f * b[0] + 0.15607835f * b[8];
2830 out[7] += ta * b[7] + tb * a[7];
2831 t = a[7] * b[7];
2832 out[6] += 0.09011186f * t;
2833 out[0] += 0.28209479f * t;
2834 out[8] += 0.15607835f * t;
2835
2836 ta = 0.14867701f * a[9] + 0.18467439f * a[1] + 0.11516472f * a[11];
2837 tb = 0.14867701f * b[9] + 0.18467439f * b[1] + 0.11516472f * b[11];
2838 out[7] += ta * b[10] + tb * a[10];
2839 out[10] += ta * b[7] + tb * a[7];
2840 t = a[7] * b[10] + a[10] * b[7];
2841 out[9] += 0.14867701f * t;
2842 out[1] += 0.18467439f * t;
2843 out[11] += 0.11516472f * t;
2844
2845 ta = 0.05947080f * a[12] + 0.23359668f * a[2] + 0.11516472f * a[14];
2846 tb = 0.05947080f * b[12] + 0.23359668f * b[2] + 0.11516472f * b[14];
2847 out[7] += ta * b[13] + tb * a[13];
2848 out[13] += ta * b[7]+ tb * a[7];
2849 t = a[7] * b[13] + a[13] * b[7];
2850 out[12] += 0.05947080f * t;
2851 out[2] += 0.23359668f * t;
2852 out[14] += 0.11516472f * t;
2853
2854 ta = 0.14867701f * a[15];
2855 tb = 0.14867701f * b[15];
2856 out[7] += ta * b[14] + tb * a[14];
2857 out[14] += ta * b[7] + tb * a[7];
2858 t = a[7] * b[14] + a[14] * b[7];
2859 out[15] += 0.14867701f * t;
2860
2861 ta = 0.28209479f * a[0] - 0.18022375f * a[6];
2862 tb = 0.28209479f * b[0] - 0.18022375f * b[6];
2863 out[8] += ta * b[8] + tb * a[8];
2864 t = a[8] * b[8];
2865 out[0] += 0.28209479f * t;
2866 out[6] -= 0.18022375f * t;
2867
2868 ta = -0.09403160f * a[11];
2869 tb = -0.09403160f * b[11];
2870 out[8] += ta * b[9] + tb * a[9];
2871 out[9] += ta * b[8] + tb * a[8];
2872 t = a[8] * b[9] + a[9] * b[8];
2873 out[11] -= 0.09403160f * t;
2874
2875 ta = -0.09403160f * a[15];
2876 tb = -0.09403160f * b[15];
2877 out[8] += ta * b[13] + tb * a[13];
2878 out[13] += ta * b[8] + tb * a[8];
2879 t = a[8] * b[13] + a[13] * b[8];
2880 out[15] -= 0.09403160f * t;
2881
2882 ta = 0.18467439f * a[2] - 0.18806319f * a[12];
2883 tb = 0.18467439f * b[2] - 0.18806319f * b[12];
2884 out[8] += ta * b[14] + tb * a[14];
2885 out[14] += ta * b[8] + tb * a[8];
2886 t = a[8] * b[14] + a[14] * b[8];
2887 out[2] += 0.18467439f * t;
2888 out[12] -= 0.18806319f * t;
2889
2890 ta = -0.21026104f * a[6] + 0.28209479f * a[0];
2891 tb = -0.21026104f * b[6] + 0.28209479f * b[0];
2892 out[9] += ta * b[9] + tb * a[9];
2893 t = a[9] * b[9];
2894 out[6] -= 0.21026104f * t;
2895 out[0] += 0.28209479f * t;
2896
2897 ta = 0.28209479f * a[0];
2898 tb = 0.28209479f * b[0];
2899 out[10] += ta * b[10] + tb * a[10];
2900 t = a[10] * b[10];
2901 out[0] += 0.28209479f * t;
2902
2903 ta = 0.28209479f * a[0] + 0.12615663f * a[6] - 0.14567312f * a[8];
2904 tb = 0.28209479f * b[0] + 0.12615663f * b[6] - 0.14567312f * b[8];
2905 out[11] += ta * b[11] + tb * a[11];
2906 t = a[11] * b[11];
2907 out[0] += 0.28209479f * t;
2908 out[6] += 0.12615663f * t;
2909 out[8] -= 0.14567312f * t;
2910
2911 ta = 0.28209479f * a[0] + 0.16820885f * a[6];
2912 tb = 0.28209479f * b[0] + 0.16820885f * b[6];
2913 out[12] += ta * b[12] + tb * a[12];
2914 t = a[12] * b[12];
2915 out[0] += 0.28209479f * t;
2916 out[6] += 0.16820885f * t;
2917
2918 ta =0.28209479f * a[0] + 0.14567312f * a[8] + 0.12615663f * a[6];
2919 tb =0.28209479f * b[0] + 0.14567312f * b[8] + 0.12615663f * b[6];
2920 out[13] += ta * b[13] + tb * a[13];
2921 t = a[13] * b[13];
2922 out[0] += 0.28209479f * t;
2923 out[8] += 0.14567312f * t;
2924 out[6] += 0.12615663f * t;
2925
2926 ta = 0.28209479f * a[0];
2927 tb = 0.28209479f * b[0];
2928 out[14] += ta * b[14] + tb * a[14];
2929 t = a[14] * b[14];
2930 out[0] += 0.28209479f * t;
2931
2932 ta = 0.28209479f * a[0] - 0.21026104f * a[6];
2933 tb = 0.28209479f * b[0] - 0.21026104f * b[6];
2934 out[15] += ta * b[15] + tb * a[15];
2935 t = a[15] * b[15];
2936 out[0] += 0.28209479f * t;
2937 out[6] -= 0.21026104f * t;
2938
2939 return out;
2940 }
2941
2942 static void rotate_X(FLOAT *out, UINT order, FLOAT a, FLOAT *in)
2943 {
2944 out[0] = in[0];
2945
2946 out[1] = a * in[2];
2947 out[2] = -a * in[1];
2948 out[3] = in[3];
2949
2950 out[4] = a * in[7];
2951 out[5] = -in[5];
2952 out[6] = -0.5f * in[6] - 0.8660253882f * in[8];
2953 out[7] = -a * in[4];
2954 out[8] = -0.8660253882f * in[6] + 0.5f * in[8];
2955 out[9] = -a * 0.7905694842f * in[12] + a * 0.6123724580f * in[14];
2956
2957 out[10] = -in[10];
2958 out[11] = -a * 0.6123724580f * in[12] - a * 0.7905694842f * in[14];
2959 out[12] = a * 0.7905694842f * in[9] + a * 0.6123724580f * in[11];
2960 out[13] = -0.25f * in[13] - 0.9682458639f * in[15];
2961 out[14] = -a * 0.6123724580f * in[9] + a * 0.7905694842f * in[11];
2962 out[15] = -0.9682458639f * in[13] + 0.25f * in[15];
2963 if (order == 4)
2964 return;
2965
2966 out[16] = -a * 0.9354143739f * in[21] + a * 0.3535533845f * in[23];
2967 out[17] = -0.75f * in[17] + 0.6614378095f * in[19];
2968 out[18] = -a * 0.3535533845f * in[21] - a * 0.9354143739f * in[23];
2969 out[19] = 0.6614378095f * in[17] + 0.75f * in[19];
2970 out[20] = 0.375f * in[20] + 0.5590170026f * in[22] + 0.7395099998f * in[24];
2971 out[21] = a * 0.9354143739f * in[16] + a * 0.3535533845f * in[18];
2972 out[22] = 0.5590170026f * in[20] + 0.5f * in[22] - 0.6614378691f * in[24];
2973 out[23] = -a * 0.3535533845f * in[16] + a * 0.9354143739f * in[18];
2974 out[24] = 0.7395099998f * in[20] - 0.6614378691f * in[22] + 0.125f * in[24];
2975 if (order == 5)
2976 return;
2977
2978 out[25] = a * 0.7015607357f * in[30] - a * 0.6846531630f * in[32] + a * 0.1976423711f * in[34];
2979 out[26] = -0.5f * in[26] + 0.8660253882f * in[28];
2980 out[27] = a * 0.5229125023f * in[30] + a * 0.3061861992f * in[32] - a * 0.7954951525f * in[34];
2981 out[28] = 0.8660253882f * in[26] + 0.5f * in[28];
2982 out[29] = a * 0.4841229022f * in[30] + a * 0.6614378691f * in[32] + a * 0.5728219748f * in[34];
2983 out[30] = -a * 0.7015607357f * in[25] - a * 0.5229125023f * in[27] - a * 0.4841229022f * in[29];
2984 out[31] = 0.125f * in[31] + 0.4050463140f * in[33] + 0.9057110548f * in[35];
2985 out[32] = a * 0.6846531630f * in[25] - a * 0.3061861992f * in[27] - a * 0.6614378691f * in[29];
2986 out[33] = 0.4050463140f * in[31] + 0.8125f * in[33] - 0.4192627370f * in[35];
2987 out[34] = -a * 0.1976423711f * in[25] + a * 0.7954951525f * in[27] - a * 0.5728219748f * in[29];
2988 out[35] = 0.9057110548f * in[31] - 0.4192627370f * in[33] + 0.0624999329f * in[35];
2989 }
2990
2991 FLOAT* WINAPI D3DXSHRotate(FLOAT *out, UINT order, const D3DXMATRIX *matrix, const FLOAT *in)
2992 {
2993 FLOAT alpha, beta, gamma, sinb, temp[36], temp1[36];
2994
2995 TRACE("out %p, order %u, matrix %p, in %p\n", out, order, matrix, in);
2996
2997 out[0] = in[0];
2998
2999 if ((order > D3DXSH_MAXORDER) || (order < D3DXSH_MINORDER))
3000 return out;
3001
3002 if (order <= 3)
3003 {
3004 out[1] = matrix->u.m[1][1] * in[1] - matrix->u.m[2][1] * in[2] + matrix->u.m[0][1] * in[3];
3005 out[2] = -matrix->u.m[1][2] * in[1] + matrix->u.m[2][2] * in[2] - matrix->u.m[0][2] * in[3];
3006 out[3] = matrix->u.m[1][0] * in[1] - matrix->u.m[2][0] * in[2] + matrix->u.m[0][0] * in[3];
3007
3008 if (order == 3)
3009 {
3010 FLOAT coeff[]={
3011 matrix->u.m[1][0] * matrix->u.m[0][0], matrix->u.m[1][1] * matrix->u.m[0][1],
3012 matrix->u.m[1][1] * matrix->u.m[2][1], matrix->u.m[1][0] * matrix->u.m[2][0],
3013 matrix->u.m[2][0] * matrix->u.m[2][0], matrix->u.m[2][1] * matrix->u.m[2][1],
3014 matrix->u.m[0][0] * matrix->u.m[2][0], matrix->u.m[0][1] * matrix->u.m[2][1],
3015 matrix->u.m[0][1] * matrix->u.m[0][1], matrix->u.m[1][0] * matrix->u.m[1][0],
3016 matrix->u.m[1][1] * matrix->u.m[1][1], matrix->u.m[0][0] * matrix->u.m[0][0], };
3017
3018 out[4] = (matrix->u.m[1][1] * matrix->u.m[0][0] + matrix->u.m[0][1] * matrix->u.m[1][0]) * in[4];
3019 out[4] -= (matrix->u.m[1][0] * matrix->u.m[2][1] + matrix->u.m[1][1] * matrix->u.m[2][0]) * in[5];
3020 out[4] += 1.7320508076f * matrix->u.m[2][0] * matrix->u.m[2][1] * in[6];
3021 out[4] -= (matrix->u.m[0][1] * matrix->u.m[2][0] + matrix->u.m[0][0] * matrix->u.m[2][1]) * in[7];
3022 out[4] += (matrix->u.m[0][0] * matrix->u.m[0][1] - matrix->u.m[1][0] * matrix->u.m[1][1]) * in[8];
3023
3024 out[5] = (matrix->u.m[1][1] * matrix->u.m[2][2] + matrix->u.m[1][2] * matrix->u.m[2][1]) * in[5];
3025 out[5] -= (matrix->u.m[1][1] * matrix->u.m[0][2] + matrix->u.m[1][2] * matrix->u.m[0][1]) * in[4];
3026 out[5] -= 1.7320508076f * matrix->u.m[2][2] * matrix->u.m[2][1] * in[6];
3027 out[5] += (matrix->u.m[0][2] * matrix->u.m[2][1] + matrix->u.m[0][1] * matrix->u.m[2][2]) * in[7];
3028 out[5] -= (matrix->u.m[0][1] * matrix->u.m[0][2] - matrix->u.m[1][1] * matrix->u.m[1][2]) * in[8];
3029
3030 out[6] = (matrix->u.m[2][2] * matrix->u.m[2][2] - 0.5f * (coeff[4] + coeff[5])) * in[6];
3031 out[6] -= (0.5773502692f * (coeff[0] + coeff[1]) - 1.1547005384f * matrix->u.m[1][2] * matrix->u.m[0][2]) * in[4];
3032 out[6] += (0.5773502692f * (coeff[2] + coeff[3]) - 1.1547005384f * matrix->u.m[1][2] * matrix->u.m[2][2]) * in[5];
3033 out[6] += (0.5773502692f * (coeff[6] + coeff[7]) - 1.1547005384f * matrix->u.m[0][2] * matrix->u.m[2][2]) * in[7];
3034 out[6] += (0.2886751347f * (coeff[9] - coeff[8] + coeff[10] - coeff[11]) - 0.5773502692f *
3035 (matrix->u.m[1][2] * matrix->u.m[1][2] - matrix->u.m[0][2] * matrix->u.m[0][2])) * in[8];
3036
3037 out[7] = (matrix->u.m[0][0] * matrix->u.m[2][2] + matrix->u.m[0][2] * matrix->u.m[2][0]) * in[7];
3038 out[7] -= (matrix->u.m[1][0] * matrix->u.m[0][2] + matrix->u.m[1][2] * matrix->u.m[0][0]) * in[4];
3039 out[7] += (matrix->u.m[1][0] * matrix->u.m[2][2] + matrix->u.m[1][2] * matrix->u.m[2][0]) * in[5];
3040 out[7] -= 1.7320508076f * matrix->u.m[2][2] * matrix->u.m[2][0] * in[6];
3041 out[7] -= (matrix->u.m[0][0] * matrix->u.m[0][2] - matrix->u.m[1][0] * matrix->u.m[1][2]) * in[8];
3042
3043 out[8] = 0.5f * (coeff[11] - coeff[8] - coeff[9] + coeff[10]) * in[8];
3044 out[8] += (coeff[0] - coeff[1]) * in[4];
3045 out[8] += (coeff[2] - coeff[3]) * in[5];
3046 out[8] += 0.86602540f * (coeff[4] - coeff[5]) * in[6];
3047 out[8] += (coeff[7] - coeff[6]) * in[7];
3048 }
3049
3050 return out;
3051 }
3052
3053 if (fabsf(matrix->u.m[2][2]) != 1.0f)
3054 {
3055 sinb = sqrtf(1.0f - matrix->u.m[2][2] * matrix->u.m[2][2]);
3056 alpha = atan2f(matrix->u.m[2][1] / sinb, matrix->u.m[2][0] / sinb);
3057 beta = atan2f(sinb, matrix->u.m[2][2]);
3058 gamma = atan2f(matrix->u.m[1][2] / sinb, -matrix->u.m[0][2] / sinb);
3059 }
3060 else
3061 {
3062 alpha = atan2f(matrix->u.m[0][1], matrix->u.m[0][0]);
3063 beta = 0.0f;
3064 gamma = 0.0f;
3065 }
3066
3067 D3DXSHRotateZ(temp, order, gamma, in);
3068 rotate_X(temp1, order, 1.0f, temp);
3069 D3DXSHRotateZ(temp, order, beta, temp1);
3070 rotate_X(temp1, order, -1.0f, temp);
3071 D3DXSHRotateZ(out, order, alpha, temp1);
3072
3073 return out;
3074 }
3075
3076 FLOAT * WINAPI D3DXSHRotateZ(FLOAT *out, UINT order, FLOAT angle, const FLOAT *in)
3077 {
3078 UINT i, sum = 0;
3079 FLOAT c[5], s[5];
3080
3081 TRACE("out %p, order %u, angle %f, in %p\n", out, order, angle, in);
3082
3083 order = min(max(order, D3DXSH_MINORDER), D3DXSH_MAXORDER);
3084
3085 out[0] = in[0];
3086
3087 for (i = 1; i < order; i++)
3088 {
3089 UINT j;
3090
3091 c[i - 1] = cosf(i * angle);
3092 s[i - 1] = sinf(i * angle);
3093 sum += i * 2;
3094
3095 out[sum - i] = c[i - 1] * in[sum - i];
3096 out[sum - i] += s[i - 1] * in[sum + i];
3097 for (j = i - 1; j > 0; j--)
3098 {
3099 out[sum - j] = 0.0f;
3100 out[sum - j] = c[j - 1] * in[sum - j];
3101 out[sum - j] += s[j - 1] * in[sum + j];
3102 }
3103
3104 if (in == out)
3105 out[sum] = 0.0f;
3106 else
3107 out[sum] = in[sum];
3108
3109 for (j = 1; j < i; j++)
3110 {
3111 out[sum + j] = 0.0f;
3112 out[sum + j] = -s[j - 1] * in[sum - j];
3113 out[sum + j] += c[j - 1] * in[sum + j];
3114 }
3115 out[sum + i] = -s[i - 1] * in[sum - i];
3116 out[sum + i] += c[i - 1] * in[sum + i];
3117 }
3118
3119 return out;
3120 }
3121
3122 FLOAT* WINAPI D3DXSHScale(FLOAT *out, UINT order, const FLOAT *a, const FLOAT scale)
3123 {
3124 UINT i;
3125
3126 TRACE("out %p, order %u, a %p, scale %f\n", out, order, a, scale);
3127
3128 for (i = 0; i < order * order; i++)
3129 out[i] = a[i] * scale;
3130
3131 return out;
3132 }