d3dx8: Implement D3DXMatrixAffineTransformation2D.
[wine] / dlls / d3dx9_36 / math.c
1 /*
2  * Mathematical operations specific to D3DX9.
3  *
4  * Copyright (C) 2008 Philip Nilsson
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define NONAMELESSUNION
22
23 #include "config.h"
24 #include "windef.h"
25 #include "wingdi.h"
26 #include "wine/debug.h"
27 #include "d3dx9.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
30
31 /*************************************************************************
32  * D3DXMatrixAffineTransformation2D
33  */
34 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation2D(
35     D3DXMATRIX *pout, FLOAT scaling,
36     CONST D3DXVECTOR2 *protationcenter, FLOAT rotation,
37     CONST D3DXVECTOR2 *ptranslation)
38 {
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     D3DXMatrixAffineTransformation(pout, scaling, &rot_center, &rot, &trans);
74
75     return pout;
76 }
77
78 /*************************************************************************
79  * D3DXMatrixDecompose
80  */
81 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, D3DXMATRIX *pm)
82 {
83     D3DXMATRIX normalized;
84     D3DXVECTOR3 vec;
85
86     if (!pm)
87     {
88      return D3DERR_INVALIDCALL;
89     }
90
91     /*Compute the scaling part.*/
92     vec.x=pm->u.m[0][0];
93     vec.y=pm->u.m[0][1];
94     vec.z=pm->u.m[0][2];
95     poutscale->x=D3DXVec3Length(&vec);
96
97     vec.x=pm->u.m[1][0];
98     vec.y=pm->u.m[1][1];
99     vec.z=pm->u.m[1][2];
100     poutscale->y=D3DXVec3Length(&vec);
101
102     vec.x=pm->u.m[2][0];
103     vec.y=pm->u.m[2][1];
104     vec.z=pm->u.m[2][2];
105     poutscale->z=D3DXVec3Length(&vec);
106
107     /*Compute the translation part.*/
108     pouttranslation->x=pm->u.m[3][0];
109     pouttranslation->y=pm->u.m[3][1];
110     pouttranslation->z=pm->u.m[3][2];
111
112     /*Let's calculate the rotation now*/
113     if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) )
114     {
115      return D3DERR_INVALIDCALL;
116     }
117
118     normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
119     normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
120     normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
121     normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
122     normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
123     normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
124     normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
125     normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
126     normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
127
128     D3DXQuaternionRotationMatrix(poutrotation,&normalized);
129     return S_OK;
130 }
131
132 /*************************************************************************
133  * D3DXPlaneTransformArray
134  */
135 D3DXPLANE* WINAPI D3DXPlaneTransformArray(
136     D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride,
137     CONST D3DXMATRIX* matrix, UINT elements)
138 {
139     UINT i;
140     TRACE("\n");
141     for (i = 0; i < elements; ++i) {
142         D3DXPlaneTransform(
143             (D3DXPLANE*)((char*)out + outstride * i),
144             (CONST D3DXPLANE*)((const char*)in + instride * i),
145             matrix);
146     }
147     return out;
148 }
149
150 /*************************************************************************
151  * D3DXVec2TransformArray
152  *
153  * Transform an array of vectors by a matrix.
154  */
155 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(
156     D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride,
157     CONST D3DXMATRIX* matrix, UINT elements)
158 {
159     UINT i;
160     TRACE("\n");
161     for (i = 0; i < elements; ++i) {
162         D3DXVec2Transform(
163             (D3DXVECTOR4*)((char*)out + outstride * i),
164             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
165             matrix);
166     }
167     return out;
168 }
169
170 /*************************************************************************
171  * D3DXVec2TransformCoordArray
172  */
173 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(
174     D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride,
175     CONST D3DXMATRIX* matrix, UINT elements)
176 {
177     UINT i;
178     TRACE("\n");
179     for (i = 0; i < elements; ++i) {
180         D3DXVec2TransformCoord(
181             (D3DXVECTOR2*)((char*)out + outstride * i),
182             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
183             matrix);
184     }
185     return out;
186 }
187
188 /*************************************************************************
189  * D3DXVec2TransformNormalArray
190  */
191 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(
192     D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride,
193     CONST D3DXMATRIX *matrix, UINT elements)
194 {
195     UINT i;
196     TRACE("\n");
197     for (i = 0; i < elements; ++i) {
198         D3DXVec2TransformNormal(
199             (D3DXVECTOR2*)((char*)out + outstride * i),
200             (CONST D3DXVECTOR2*)((const char*)in + instride * i),
201             matrix);
202     }
203     return out;
204 }
205
206 /*************************************************************************
207  * D3DXVec3ProjectArray
208  *
209  * Projects an array of vectors to the screen.
210  */
211 D3DXVECTOR3* WINAPI D3DXVec3ProjectArray(
212     D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
213     CONST D3DVIEWPORT9* viewport, CONST D3DXMATRIX* projection,
214     CONST D3DXMATRIX* view, CONST D3DXMATRIX* world, UINT elements)
215 {
216     UINT i;
217     TRACE("\n");
218     for (i = 0; i < elements; ++i) {
219         D3DXVec3Project(
220             (D3DXVECTOR3*)((char*)out + outstride * i),
221             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
222             viewport, projection, view, world);
223     }
224     return out;
225 }
226
227 /*************************************************************************
228  * D3DXVec3TransformArray
229  */
230 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(
231     D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
232     CONST D3DXMATRIX* matrix, UINT elements)
233 {
234     UINT i;
235     TRACE("\n");
236     for (i = 0; i < elements; ++i) {
237         D3DXVec3Transform(
238             (D3DXVECTOR4*)((char*)out + outstride * i),
239             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
240             matrix);
241     }
242     return out;
243 }
244
245 /*************************************************************************
246  * D3DXVec3TransformCoordArray
247  */
248 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(
249     D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
250     CONST D3DXMATRIX* matrix, UINT elements)
251 {
252     UINT i;
253     TRACE("\n");
254     for (i = 0; i < elements; ++i) {
255         D3DXVec3TransformCoord(
256             (D3DXVECTOR3*)((char*)out + outstride * i),
257             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
258             matrix);
259     }
260     return out;
261 }
262
263 /*************************************************************************
264  * D3DXVec3TransformNormalArray
265  */
266 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(
267     D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
268     CONST D3DXMATRIX* matrix, UINT elements)
269 {
270     UINT i;
271     TRACE("\n");
272     for (i = 0; i < elements; ++i) {
273         D3DXVec3TransformNormal(
274             (D3DXVECTOR3*)((char*)out + outstride * i),
275             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
276             matrix);
277     }
278     return out;
279 }
280
281 /*************************************************************************
282  * D3DXVec3UnprojectArray
283  */
284 D3DXVECTOR3* WINAPI D3DXVec3UnprojectArray(
285     D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
286     CONST D3DVIEWPORT9* viewport, CONST D3DXMATRIX* projection,
287     CONST D3DXMATRIX* view, CONST D3DXMATRIX* world, UINT elements)
288 {
289     UINT i;
290     TRACE("\n");
291     for (i = 0; i < elements; ++i) {
292         D3DXVec3Unproject(
293             (D3DXVECTOR3*)((char*)out + outstride * i),
294             (CONST D3DXVECTOR3*)((const char*)in + instride * i),
295             viewport, projection, view, world);
296     }
297     return out;
298 }
299
300 /*************************************************************************
301  * D3DXVec4TransformArray
302  */
303 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(
304     D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride,
305     CONST D3DXMATRIX* matrix, UINT elements)
306 {
307     UINT i;
308     TRACE("\n");
309     for (i = 0; i < elements; ++i) {
310         D3DXVec4Transform(
311             (D3DXVECTOR4*)((char*)out + outstride * i),
312             (CONST D3DXVECTOR4*)((const char*)in + instride * i),
313             matrix);
314     }
315     return out;
316 }