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