dplayx: NS_GetOtherMagic() is not used, so remove it.
[wine] / dlls / d3dx9_36 / math.c
1 /*
2  * Mathematical operations specific to D3DX9.
3  *
4  * Copyright (C) 2008 David Adam
5  * Copyright (C) 2008 Philip Nilsson
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #define NONAMELESSUNION
23
24 #include "config.h"
25 #include "windef.h"
26 #include "wingdi.h"
27 #include "d3dx9.h"
28
29
30 /*************************************************************************
31  * D3DXMatrixAffineTransformation2D
32  */
33 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation2D(
34     D3DXMATRIX *pout, FLOAT scaling,
35     CONST D3DXVECTOR2 *protationcenter, FLOAT rotation,
36     CONST D3DXVECTOR2 *ptranslation)
37 {
38     D3DXMATRIX m1, m2, m3, m4, m5;
39     D3DXQUATERNION rot;
40     D3DXVECTOR3 rot_center, trans;
41
42     rot.w=cos(rotation/2.0f);
43     rot.x=0.0f;
44     rot.y=0.0f;
45     rot.z=sin(rotation/2.0f);
46
47     if ( protationcenter )
48     {
49         rot_center.x=protationcenter->x;
50         rot_center.y=protationcenter->y;
51         rot_center.z=0.0f;
52     }
53     else
54     {
55         rot_center.x=0.0f;
56         rot_center.y=0.0f;
57         rot_center.z=0.0f;
58     }
59
60     if ( ptranslation )
61     {
62         trans.x=ptranslation->x;
63         trans.y=ptranslation->y;
64         trans.z=0.0f;
65     }
66     else
67     {
68         trans.x=0.0f;
69         trans.y=0.0f;
70         trans.z=0.0f;
71     }
72
73     D3DXMatrixScaling(&m1, scaling, scaling, 1.0f);
74     D3DXMatrixTranslation(&m2, -rot_center.x, -rot_center.y, -rot_center.z);
75     D3DXMatrixTranslation(&m4, rot_center.x, rot_center.y, rot_center.z);
76     D3DXMatrixRotationQuaternion(&m3, &rot);
77     D3DXMatrixTranslation(&m5, trans.x, trans.y, trans.z);
78
79     D3DXMatrixMultiply(&m1, &m1, &m2);
80     D3DXMatrixMultiply(&m1, &m1, &m3);
81     D3DXMatrixMultiply(&m1, &m1, &m4);
82     D3DXMatrixMultiply(pout, &m1, &m5);
83
84     return pout;
85 }
86
87 /*************************************************************************
88  * D3DXMatrixDecompose
89  */
90 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, D3DXMATRIX *pm)
91 {
92     D3DXMATRIX normalized;
93     D3DXVECTOR3 vec;
94
95     /*Compute the scaling part.*/
96     vec.x=pm->u.m[0][0];
97     vec.y=pm->u.m[0][1];
98     vec.z=pm->u.m[0][2];
99     poutscale->x=D3DXVec3Length(&vec);
100
101     vec.x=pm->u.m[1][0];
102     vec.y=pm->u.m[1][1];
103     vec.z=pm->u.m[1][2];
104     poutscale->y=D3DXVec3Length(&vec);
105
106     vec.x=pm->u.m[2][0];
107     vec.y=pm->u.m[2][1];
108     vec.z=pm->u.m[2][2];
109     poutscale->z=D3DXVec3Length(&vec);
110
111     /*Compute the translation part.*/
112     pouttranslation->x=pm->u.m[3][0];
113     pouttranslation->y=pm->u.m[3][1];
114     pouttranslation->z=pm->u.m[3][2];
115
116     /*Let's calculate the rotation now*/
117     if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) )
118     {
119      return D3DERR_INVALIDCALL;
120     }
121
122     normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
123     normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
124     normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
125     normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
126     normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
127     normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
128     normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
129     normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
130     normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
131
132     D3DXQuaternionRotationMatrix(poutrotation,&normalized);
133     return S_OK;
134 }
135
136 /*************************************************************************
137  * D3DXMatrixTransformation2D
138  */
139 D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(
140     D3DXMATRIX *pout, CONST D3DXVECTOR2 *pscalingcenter,
141     FLOAT scalingrotation, CONST D3DXVECTOR2 *pscaling,
142     CONST D3DXVECTOR2 *protationcenter, FLOAT rotation,
143     CONST D3DXVECTOR2 *ptranslation)
144 {
145     D3DXQUATERNION rot, sca_rot;
146     D3DXVECTOR3 rot_center, sca, sca_center, trans;
147
148     if ( pscalingcenter )
149     {
150         sca_center.x=pscalingcenter->x;
151         sca_center.y=pscalingcenter->y;
152         sca_center.z=0.0f;
153     }
154     else
155     {
156         sca_center.x=0.0f;
157         sca_center.y=0.0f;
158         sca_center.z=0.0f;
159     }
160
161     if ( pscaling )
162     {
163         sca.x=pscaling->x;
164         sca.y=pscaling->y;
165         sca.z=1.0f;
166     }
167     else
168     {
169         sca.x=1.0f;
170         sca.y=1.0f;
171         sca.z=1.0f;
172     }
173
174     if ( protationcenter )
175     {
176         rot_center.x=protationcenter->x;
177         rot_center.y=protationcenter->y;
178         rot_center.z=0.0f;
179     }
180     else
181     {
182         rot_center.x=0.0f;
183         rot_center.y=0.0f;
184         rot_center.z=0.0f;
185     }
186
187     if ( ptranslation )
188     {
189         trans.x=ptranslation->x;
190         trans.y=ptranslation->y;
191         trans.z=0.0f;
192     }
193     else
194     {
195         trans.x=0.0f;
196         trans.y=0.0f;
197         trans.z=0.0f;
198     }
199
200     rot.w=cos(rotation/2.0f);
201     rot.x=0.0f;
202     rot.y=0.0f;
203     rot.z=sin(rotation/2.0f);
204
205     sca_rot.w=cos(scalingrotation/2.0f);
206     sca_rot.x=0.0f;
207     sca_rot.y=0.0f;
208     sca_rot.z=sin(scalingrotation/2.0f);
209
210     D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
211
212     return pout;
213 }
214
215 /*************************************************************************
216  * D3DXPlaneTransformArray
217  */
218 D3DXPLANE* WINAPI D3DXPlaneTransformArray(
219     D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride,
220     CONST D3DXMATRIX* matrix, UINT elements)
221 {
222     UINT i;
223
224     for (i = 0; i < elements; ++i) {
225         D3DXPlaneTransform(
226             (D3DXPLANE*)((char*)out + outstride * i),
227             (CONST D3DXPLANE*)((const char*)in + instride * i),
228             matrix);
229     }
230     return out;
231 }
232
233 /*************************************************************************
234  * D3DXVec2TransformArray
235  *
236  * Transform an array of vectors by a matrix.
237  */
238 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(
239     D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride,
240     CONST D3DXMATRIX* matrix, UINT elements)
241 {
242     UINT i;
243
244     for (i = 0; i < elements; ++i) {
245         D3DXVec2Transform(
246             (D3DXVECTOR4*)((char*)out + outstride * i),
247             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
248             matrix);
249     }
250     return out;
251 }
252
253 /*************************************************************************
254  * D3DXVec2TransformCoordArray
255  */
256 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(
257     D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride,
258     CONST D3DXMATRIX* matrix, UINT elements)
259 {
260     UINT i;
261
262     for (i = 0; i < elements; ++i) {
263         D3DXVec2TransformCoord(
264             (D3DXVECTOR2*)((char*)out + outstride * i),
265             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
266             matrix);
267     }
268     return out;
269 }
270
271 /*************************************************************************
272  * D3DXVec2TransformNormalArray
273  */
274 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(
275     D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride,
276     CONST D3DXMATRIX *matrix, UINT elements)
277 {
278     UINT i;
279
280     for (i = 0; i < elements; ++i) {
281         D3DXVec2TransformNormal(
282             (D3DXVECTOR2*)((char*)out + outstride * i),
283             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
284             matrix);
285     }
286     return out;
287 }
288
289 /*************************************************************************
290  * D3DXVec3ProjectArray
291  *
292  * Projects an array of vectors to the screen.
293  */
294 D3DXVECTOR3* WINAPI D3DXVec3ProjectArray(
295     D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
296     CONST D3DVIEWPORT9* viewport, CONST D3DXMATRIX* projection,
297     CONST D3DXMATRIX* view, CONST D3DXMATRIX* world, UINT elements)
298 {
299     UINT i;
300
301     for (i = 0; i < elements; ++i) {
302         D3DXVec3Project(
303             (D3DXVECTOR3*)((char*)out + outstride * i),
304             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
305             viewport, projection, view, world);
306     }
307     return out;
308 }
309
310 /*************************************************************************
311  * D3DXVec3TransformArray
312  */
313 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(
314     D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
315     CONST D3DXMATRIX* matrix, UINT elements)
316 {
317     UINT i;
318
319     for (i = 0; i < elements; ++i) {
320         D3DXVec3Transform(
321             (D3DXVECTOR4*)((char*)out + outstride * i),
322             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
323             matrix);
324     }
325     return out;
326 }
327
328 /*************************************************************************
329  * D3DXVec3TransformCoordArray
330  */
331 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(
332     D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
333     CONST D3DXMATRIX* matrix, UINT elements)
334 {
335     UINT i;
336
337     for (i = 0; i < elements; ++i) {
338         D3DXVec3TransformCoord(
339             (D3DXVECTOR3*)((char*)out + outstride * i),
340             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
341             matrix);
342     }
343     return out;
344 }
345
346 /*************************************************************************
347  * D3DXVec3TransformNormalArray
348  */
349 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(
350     D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
351     CONST D3DXMATRIX* matrix, UINT elements)
352 {
353     UINT i;
354
355     for (i = 0; i < elements; ++i) {
356         D3DXVec3TransformNormal(
357             (D3DXVECTOR3*)((char*)out + outstride * i),
358             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
359             matrix);
360     }
361     return out;
362 }
363
364 /*************************************************************************
365  * D3DXVec3UnprojectArray
366  */
367 D3DXVECTOR3* WINAPI D3DXVec3UnprojectArray(
368     D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
369     CONST D3DVIEWPORT9* viewport, CONST D3DXMATRIX* projection,
370     CONST D3DXMATRIX* view, CONST D3DXMATRIX* world, UINT elements)
371 {
372     UINT i;
373
374     for (i = 0; i < elements; ++i) {
375         D3DXVec3Unproject(
376             (D3DXVECTOR3*)((char*)out + outstride * i),
377             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
378             viewport, projection, view, world);
379     }
380     return out;
381 }
382
383 /*************************************************************************
384  * D3DXVec4TransformArray
385  */
386 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(
387     D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride,
388     CONST D3DXMATRIX* matrix, UINT elements)
389 {
390     UINT i;
391
392     for (i = 0; i < elements; ++i) {
393         D3DXVec4Transform(
394             (D3DXVECTOR4*)((char*)out + outstride * i),
395             (CONST D3DXVECTOR4*)((const char*)in + instride * i),
396             matrix);
397     }
398     return out;
399 }