2 * Mathematical operations specific to D3DX9.
4 * Copyright (C) 2008 David Adam
5 * Copyright (C) 2008 Philip Nilsson
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.
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.
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
22 #define NONAMELESSUNION
27 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3dx
);
32 /*************************************************************************
33 * D3DXMatrixAffineTransformation2D
35 D3DXMATRIX
* WINAPI
D3DXMatrixAffineTransformation2D(
36 D3DXMATRIX
*pout
, FLOAT scaling
,
37 CONST D3DXVECTOR2
*protationcenter
, FLOAT rotation
,
38 CONST D3DXVECTOR2
*ptranslation
)
41 D3DXVECTOR3 rot_center
, trans
;
43 rot
.w
=cos(rotation
/2.0f
);
46 rot
.z
=sin(rotation
/2.0f
);
48 if ( protationcenter
)
50 rot_center
.x
=protationcenter
->x
;
51 rot_center
.y
=protationcenter
->y
;
63 trans
.x
=ptranslation
->x
;
64 trans
.y
=ptranslation
->y
;
74 D3DXMatrixAffineTransformation(pout
, scaling
, &rot_center
, &rot
, &trans
);
79 /*************************************************************************
82 HRESULT WINAPI
D3DXMatrixDecompose(D3DXVECTOR3
*poutscale
, D3DXQUATERNION
*poutrotation
, D3DXVECTOR3
*pouttranslation
, D3DXMATRIX
*pm
)
84 D3DXMATRIX normalized
;
87 /*Compute the scaling part.*/
91 poutscale
->x
=D3DXVec3Length(&vec
);
96 poutscale
->y
=D3DXVec3Length(&vec
);
101 poutscale
->z
=D3DXVec3Length(&vec
);
103 /*Compute the translation part.*/
104 pouttranslation
->x
=pm
->u
.m
[3][0];
105 pouttranslation
->y
=pm
->u
.m
[3][1];
106 pouttranslation
->z
=pm
->u
.m
[3][2];
108 /*Let's calculate the rotation now*/
109 if ( (poutscale
->x
== 0.0f
) || (poutscale
->y
== 0.0f
) || (poutscale
->z
== 0.0f
) )
111 return D3DERR_INVALIDCALL
;
114 normalized
.u
.m
[0][0]=pm
->u
.m
[0][0]/poutscale
->x
;
115 normalized
.u
.m
[0][1]=pm
->u
.m
[0][1]/poutscale
->x
;
116 normalized
.u
.m
[0][2]=pm
->u
.m
[0][2]/poutscale
->x
;
117 normalized
.u
.m
[1][0]=pm
->u
.m
[1][0]/poutscale
->y
;
118 normalized
.u
.m
[1][1]=pm
->u
.m
[1][1]/poutscale
->y
;
119 normalized
.u
.m
[1][2]=pm
->u
.m
[1][2]/poutscale
->y
;
120 normalized
.u
.m
[2][0]=pm
->u
.m
[2][0]/poutscale
->z
;
121 normalized
.u
.m
[2][1]=pm
->u
.m
[2][1]/poutscale
->z
;
122 normalized
.u
.m
[2][2]=pm
->u
.m
[2][2]/poutscale
->z
;
124 D3DXQuaternionRotationMatrix(poutrotation
,&normalized
);
128 /*************************************************************************
129 * D3DXMatrixTransformation2D
131 D3DXMATRIX
* WINAPI
D3DXMatrixTransformation2D(
132 D3DXMATRIX
*pout
, CONST D3DXVECTOR2
*pscalingcenter
,
133 FLOAT scalingrotation
, CONST D3DXVECTOR2
*pscaling
,
134 CONST D3DXVECTOR2
*protationcenter
, FLOAT rotation
,
135 CONST D3DXVECTOR2
*ptranslation
)
137 D3DXQUATERNION rot
, sca_rot
;
138 D3DXVECTOR3 rot_center
, sca
, sca_center
, trans
;
140 if ( pscalingcenter
)
142 sca_center
.x
=pscalingcenter
->x
;
143 sca_center
.y
=pscalingcenter
->y
;
166 if ( protationcenter
)
168 rot_center
.x
=protationcenter
->x
;
169 rot_center
.y
=protationcenter
->y
;
181 trans
.x
=ptranslation
->x
;
182 trans
.y
=ptranslation
->y
;
192 rot
.w
=cos(rotation
/2.0f
);
195 rot
.z
=sin(rotation
/2.0f
);
197 sca_rot
.w
=cos(scalingrotation
/2.0f
);
200 sca_rot
.z
=sin(scalingrotation
/2.0f
);
202 D3DXMatrixTransformation(pout
, &sca_center
, &sca_rot
, &sca
, &rot_center
, &rot
, &trans
);
207 /*************************************************************************
208 * D3DXPlaneTransformArray
210 D3DXPLANE
* WINAPI
D3DXPlaneTransformArray(
211 D3DXPLANE
* out
, UINT outstride
, CONST D3DXPLANE
* in
, UINT instride
,
212 CONST D3DXMATRIX
* matrix
, UINT elements
)
216 for (i
= 0; i
< elements
; ++i
) {
218 (D3DXPLANE
*)((char*)out
+ outstride
* i
),
219 (CONST D3DXPLANE
*)((const char*)in
+ instride
* i
),
225 /*************************************************************************
226 * D3DXVec2TransformArray
228 * Transform an array of vectors by a matrix.
230 D3DXVECTOR4
* WINAPI
D3DXVec2TransformArray(
231 D3DXVECTOR4
* out
, UINT outstride
, CONST D3DXVECTOR2
* in
, UINT instride
,
232 CONST D3DXMATRIX
* matrix
, UINT elements
)
236 for (i
= 0; i
< elements
; ++i
) {
238 (D3DXVECTOR4
*)((char*)out
+ outstride
* i
),
239 (CONST D3DXVECTOR2
*)((const char*)in
+ instride
* i
),
245 /*************************************************************************
246 * D3DXVec2TransformCoordArray
248 D3DXVECTOR2
* WINAPI
D3DXVec2TransformCoordArray(
249 D3DXVECTOR2
* out
, UINT outstride
, CONST D3DXVECTOR2
* in
, UINT instride
,
250 CONST D3DXMATRIX
* matrix
, UINT elements
)
254 for (i
= 0; i
< elements
; ++i
) {
255 D3DXVec2TransformCoord(
256 (D3DXVECTOR2
*)((char*)out
+ outstride
* i
),
257 (CONST D3DXVECTOR2
*)((const char*)in
+ instride
* i
),
263 /*************************************************************************
264 * D3DXVec2TransformNormalArray
266 D3DXVECTOR2
* WINAPI
D3DXVec2TransformNormalArray(
267 D3DXVECTOR2
* out
, UINT outstride
, CONST D3DXVECTOR2
*in
, UINT instride
,
268 CONST D3DXMATRIX
*matrix
, UINT elements
)
272 for (i
= 0; i
< elements
; ++i
) {
273 D3DXVec2TransformNormal(
274 (D3DXVECTOR2
*)((char*)out
+ outstride
* i
),
275 (CONST D3DXVECTOR2
*)((const char*)in
+ instride
* i
),
281 /*************************************************************************
282 * D3DXVec3ProjectArray
284 * Projects an array of vectors to the screen.
286 D3DXVECTOR3
* WINAPI
D3DXVec3ProjectArray(
287 D3DXVECTOR3
* out
, UINT outstride
, CONST D3DXVECTOR3
* in
, UINT instride
,
288 CONST D3DVIEWPORT9
* viewport
, CONST D3DXMATRIX
* projection
,
289 CONST D3DXMATRIX
* view
, CONST D3DXMATRIX
* world
, UINT elements
)
293 for (i
= 0; i
< elements
; ++i
) {
295 (D3DXVECTOR3
*)((char*)out
+ outstride
* i
),
296 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
297 viewport
, projection
, view
, world
);
302 /*************************************************************************
303 * D3DXVec3TransformArray
305 D3DXVECTOR4
* WINAPI
D3DXVec3TransformArray(
306 D3DXVECTOR4
* out
, UINT outstride
, CONST D3DXVECTOR3
* in
, UINT instride
,
307 CONST D3DXMATRIX
* matrix
, UINT elements
)
311 for (i
= 0; i
< elements
; ++i
) {
313 (D3DXVECTOR4
*)((char*)out
+ outstride
* i
),
314 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
320 /*************************************************************************
321 * D3DXVec3TransformCoordArray
323 D3DXVECTOR3
* WINAPI
D3DXVec3TransformCoordArray(
324 D3DXVECTOR3
* out
, UINT outstride
, CONST D3DXVECTOR3
* in
, UINT instride
,
325 CONST D3DXMATRIX
* matrix
, UINT elements
)
329 for (i
= 0; i
< elements
; ++i
) {
330 D3DXVec3TransformCoord(
331 (D3DXVECTOR3
*)((char*)out
+ outstride
* i
),
332 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
338 /*************************************************************************
339 * D3DXVec3TransformNormalArray
341 D3DXVECTOR3
* WINAPI
D3DXVec3TransformNormalArray(
342 D3DXVECTOR3
* out
, UINT outstride
, CONST D3DXVECTOR3
* in
, UINT instride
,
343 CONST D3DXMATRIX
* matrix
, UINT elements
)
347 for (i
= 0; i
< elements
; ++i
) {
348 D3DXVec3TransformNormal(
349 (D3DXVECTOR3
*)((char*)out
+ outstride
* i
),
350 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
356 /*************************************************************************
357 * D3DXVec3UnprojectArray
359 D3DXVECTOR3
* WINAPI
D3DXVec3UnprojectArray(
360 D3DXVECTOR3
* out
, UINT outstride
, CONST D3DXVECTOR3
* in
, UINT instride
,
361 CONST D3DVIEWPORT9
* viewport
, CONST D3DXMATRIX
* projection
,
362 CONST D3DXMATRIX
* view
, CONST D3DXMATRIX
* world
, UINT elements
)
366 for (i
= 0; i
< elements
; ++i
) {
368 (D3DXVECTOR3
*)((char*)out
+ outstride
* i
),
369 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
370 viewport
, projection
, view
, world
);
375 /*************************************************************************
376 * D3DXVec4TransformArray
378 D3DXVECTOR4
* WINAPI
D3DXVec4TransformArray(
379 D3DXVECTOR4
* out
, UINT outstride
, CONST D3DXVECTOR4
* in
, UINT instride
,
380 CONST D3DXMATRIX
* matrix
, UINT elements
)
384 for (i
= 0; i
< elements
; ++i
) {
386 (D3DXVECTOR4
*)((char*)out
+ outstride
* i
),
387 (CONST D3DXVECTOR4
*)((const char*)in
+ instride
* i
),