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
)
40 D3DXMATRIX m1
, m2
, m3
, m4
, m5
;
42 D3DXVECTOR3 rot_center
, trans
;
44 rot
.w
=cos(rotation
/2.0f
);
47 rot
.z
=sin(rotation
/2.0f
);
49 if ( protationcenter
)
51 rot_center
.x
=protationcenter
->x
;
52 rot_center
.y
=protationcenter
->y
;
64 trans
.x
=ptranslation
->x
;
65 trans
.y
=ptranslation
->y
;
75 D3DXMatrixScaling(&m1
, scaling
, scaling
, 1.0f
);
76 D3DXMatrixTranslation(&m2
, -rot_center
.x
, -rot_center
.y
, -rot_center
.z
);
77 D3DXMatrixTranslation(&m4
, rot_center
.x
, rot_center
.y
, rot_center
.z
);
78 D3DXMatrixRotationQuaternion(&m3
, &rot
);
79 D3DXMatrixTranslation(&m5
, trans
.x
, trans
.y
, trans
.z
);
81 D3DXMatrixMultiply(&m1
, &m1
, &m2
);
82 D3DXMatrixMultiply(&m1
, &m1
, &m3
);
83 D3DXMatrixMultiply(&m1
, &m1
, &m4
);
84 D3DXMatrixMultiply(pout
, &m1
, &m5
);
89 /*************************************************************************
92 HRESULT WINAPI
D3DXMatrixDecompose(D3DXVECTOR3
*poutscale
, D3DXQUATERNION
*poutrotation
, D3DXVECTOR3
*pouttranslation
, D3DXMATRIX
*pm
)
94 D3DXMATRIX normalized
;
97 /*Compute the scaling part.*/
101 poutscale
->x
=D3DXVec3Length(&vec
);
106 poutscale
->y
=D3DXVec3Length(&vec
);
111 poutscale
->z
=D3DXVec3Length(&vec
);
113 /*Compute the translation part.*/
114 pouttranslation
->x
=pm
->u
.m
[3][0];
115 pouttranslation
->y
=pm
->u
.m
[3][1];
116 pouttranslation
->z
=pm
->u
.m
[3][2];
118 /*Let's calculate the rotation now*/
119 if ( (poutscale
->x
== 0.0f
) || (poutscale
->y
== 0.0f
) || (poutscale
->z
== 0.0f
) )
121 return D3DERR_INVALIDCALL
;
124 normalized
.u
.m
[0][0]=pm
->u
.m
[0][0]/poutscale
->x
;
125 normalized
.u
.m
[0][1]=pm
->u
.m
[0][1]/poutscale
->x
;
126 normalized
.u
.m
[0][2]=pm
->u
.m
[0][2]/poutscale
->x
;
127 normalized
.u
.m
[1][0]=pm
->u
.m
[1][0]/poutscale
->y
;
128 normalized
.u
.m
[1][1]=pm
->u
.m
[1][1]/poutscale
->y
;
129 normalized
.u
.m
[1][2]=pm
->u
.m
[1][2]/poutscale
->y
;
130 normalized
.u
.m
[2][0]=pm
->u
.m
[2][0]/poutscale
->z
;
131 normalized
.u
.m
[2][1]=pm
->u
.m
[2][1]/poutscale
->z
;
132 normalized
.u
.m
[2][2]=pm
->u
.m
[2][2]/poutscale
->z
;
134 D3DXQuaternionRotationMatrix(poutrotation
,&normalized
);
138 /*************************************************************************
139 * D3DXMatrixTransformation2D
141 D3DXMATRIX
* WINAPI
D3DXMatrixTransformation2D(
142 D3DXMATRIX
*pout
, CONST D3DXVECTOR2
*pscalingcenter
,
143 FLOAT scalingrotation
, CONST D3DXVECTOR2
*pscaling
,
144 CONST D3DXVECTOR2
*protationcenter
, FLOAT rotation
,
145 CONST D3DXVECTOR2
*ptranslation
)
147 D3DXQUATERNION rot
, sca_rot
;
148 D3DXVECTOR3 rot_center
, sca
, sca_center
, trans
;
150 if ( pscalingcenter
)
152 sca_center
.x
=pscalingcenter
->x
;
153 sca_center
.y
=pscalingcenter
->y
;
176 if ( protationcenter
)
178 rot_center
.x
=protationcenter
->x
;
179 rot_center
.y
=protationcenter
->y
;
191 trans
.x
=ptranslation
->x
;
192 trans
.y
=ptranslation
->y
;
202 rot
.w
=cos(rotation
/2.0f
);
205 rot
.z
=sin(rotation
/2.0f
);
207 sca_rot
.w
=cos(scalingrotation
/2.0f
);
210 sca_rot
.z
=sin(scalingrotation
/2.0f
);
212 D3DXMatrixTransformation(pout
, &sca_center
, &sca_rot
, &sca
, &rot_center
, &rot
, &trans
);
217 /*************************************************************************
218 * D3DXPlaneTransformArray
220 D3DXPLANE
* WINAPI
D3DXPlaneTransformArray(
221 D3DXPLANE
* out
, UINT outstride
, CONST D3DXPLANE
* in
, UINT instride
,
222 CONST D3DXMATRIX
* matrix
, UINT elements
)
226 for (i
= 0; i
< elements
; ++i
) {
228 (D3DXPLANE
*)((char*)out
+ outstride
* i
),
229 (CONST D3DXPLANE
*)((const char*)in
+ instride
* i
),
235 /*************************************************************************
236 * D3DXVec2TransformArray
238 * Transform an array of vectors by a matrix.
240 D3DXVECTOR4
* WINAPI
D3DXVec2TransformArray(
241 D3DXVECTOR4
* out
, UINT outstride
, CONST D3DXVECTOR2
* in
, UINT instride
,
242 CONST D3DXMATRIX
* matrix
, UINT elements
)
246 for (i
= 0; i
< elements
; ++i
) {
248 (D3DXVECTOR4
*)((char*)out
+ outstride
* i
),
249 (CONST D3DXVECTOR2
*)((const char*)in
+ instride
* i
),
255 /*************************************************************************
256 * D3DXVec2TransformCoordArray
258 D3DXVECTOR2
* WINAPI
D3DXVec2TransformCoordArray(
259 D3DXVECTOR2
* out
, UINT outstride
, CONST D3DXVECTOR2
* in
, UINT instride
,
260 CONST D3DXMATRIX
* matrix
, UINT elements
)
264 for (i
= 0; i
< elements
; ++i
) {
265 D3DXVec2TransformCoord(
266 (D3DXVECTOR2
*)((char*)out
+ outstride
* i
),
267 (CONST D3DXVECTOR2
*)((const char*)in
+ instride
* i
),
273 /*************************************************************************
274 * D3DXVec2TransformNormalArray
276 D3DXVECTOR2
* WINAPI
D3DXVec2TransformNormalArray(
277 D3DXVECTOR2
* out
, UINT outstride
, CONST D3DXVECTOR2
*in
, UINT instride
,
278 CONST D3DXMATRIX
*matrix
, UINT elements
)
282 for (i
= 0; i
< elements
; ++i
) {
283 D3DXVec2TransformNormal(
284 (D3DXVECTOR2
*)((char*)out
+ outstride
* i
),
285 (CONST D3DXVECTOR2
*)((const char*)in
+ instride
* i
),
291 /*************************************************************************
292 * D3DXVec3ProjectArray
294 * Projects an array of vectors to the screen.
296 D3DXVECTOR3
* WINAPI
D3DXVec3ProjectArray(
297 D3DXVECTOR3
* out
, UINT outstride
, CONST D3DXVECTOR3
* in
, UINT instride
,
298 CONST D3DVIEWPORT9
* viewport
, CONST D3DXMATRIX
* projection
,
299 CONST D3DXMATRIX
* view
, CONST D3DXMATRIX
* world
, UINT elements
)
303 for (i
= 0; i
< elements
; ++i
) {
305 (D3DXVECTOR3
*)((char*)out
+ outstride
* i
),
306 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
307 viewport
, projection
, view
, world
);
312 /*************************************************************************
313 * D3DXVec3TransformArray
315 D3DXVECTOR4
* WINAPI
D3DXVec3TransformArray(
316 D3DXVECTOR4
* out
, UINT outstride
, CONST D3DXVECTOR3
* in
, UINT instride
,
317 CONST D3DXMATRIX
* matrix
, UINT elements
)
321 for (i
= 0; i
< elements
; ++i
) {
323 (D3DXVECTOR4
*)((char*)out
+ outstride
* i
),
324 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
330 /*************************************************************************
331 * D3DXVec3TransformCoordArray
333 D3DXVECTOR3
* WINAPI
D3DXVec3TransformCoordArray(
334 D3DXVECTOR3
* out
, UINT outstride
, CONST D3DXVECTOR3
* in
, UINT instride
,
335 CONST D3DXMATRIX
* matrix
, UINT elements
)
339 for (i
= 0; i
< elements
; ++i
) {
340 D3DXVec3TransformCoord(
341 (D3DXVECTOR3
*)((char*)out
+ outstride
* i
),
342 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
348 /*************************************************************************
349 * D3DXVec3TransformNormalArray
351 D3DXVECTOR3
* WINAPI
D3DXVec3TransformNormalArray(
352 D3DXVECTOR3
* out
, UINT outstride
, CONST D3DXVECTOR3
* in
, UINT instride
,
353 CONST D3DXMATRIX
* matrix
, UINT elements
)
357 for (i
= 0; i
< elements
; ++i
) {
358 D3DXVec3TransformNormal(
359 (D3DXVECTOR3
*)((char*)out
+ outstride
* i
),
360 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
366 /*************************************************************************
367 * D3DXVec3UnprojectArray
369 D3DXVECTOR3
* WINAPI
D3DXVec3UnprojectArray(
370 D3DXVECTOR3
* out
, UINT outstride
, CONST D3DXVECTOR3
* in
, UINT instride
,
371 CONST D3DVIEWPORT9
* viewport
, CONST D3DXMATRIX
* projection
,
372 CONST D3DXMATRIX
* view
, CONST D3DXMATRIX
* world
, UINT elements
)
376 for (i
= 0; i
< elements
; ++i
) {
378 (D3DXVECTOR3
*)((char*)out
+ outstride
* i
),
379 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
380 viewport
, projection
, view
, world
);
385 /*************************************************************************
386 * D3DXVec4TransformArray
388 D3DXVECTOR4
* WINAPI
D3DXVec4TransformArray(
389 D3DXVECTOR4
* out
, UINT outstride
, CONST D3DXVECTOR4
* in
, UINT instride
,
390 CONST D3DXMATRIX
* matrix
, UINT elements
)
394 for (i
= 0; i
< elements
; ++i
) {
396 (D3DXVECTOR4
*)((char*)out
+ outstride
* i
),
397 (CONST D3DXVECTOR4
*)((const char*)in
+ instride
* i
),