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
30 /*************************************************************************
31 * D3DXMatrixAffineTransformation2D
33 D3DXMATRIX
* WINAPI
D3DXMatrixAffineTransformation2D(
34 D3DXMATRIX
*pout
, FLOAT scaling
,
35 CONST D3DXVECTOR2
*protationcenter
, FLOAT rotation
,
36 CONST D3DXVECTOR2
*ptranslation
)
38 D3DXMATRIX m1
, m2
, m3
, m4
, m5
;
40 D3DXVECTOR3 rot_center
, trans
;
42 rot
.w
=cos(rotation
/2.0f
);
45 rot
.z
=sin(rotation
/2.0f
);
47 if ( protationcenter
)
49 rot_center
.x
=protationcenter
->x
;
50 rot_center
.y
=protationcenter
->y
;
62 trans
.x
=ptranslation
->x
;
63 trans
.y
=ptranslation
->y
;
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
);
79 D3DXMatrixMultiply(&m1
, &m1
, &m2
);
80 D3DXMatrixMultiply(&m1
, &m1
, &m3
);
81 D3DXMatrixMultiply(&m1
, &m1
, &m4
);
82 D3DXMatrixMultiply(pout
, &m1
, &m5
);
87 /*************************************************************************
90 HRESULT WINAPI
D3DXMatrixDecompose(D3DXVECTOR3
*poutscale
, D3DXQUATERNION
*poutrotation
, D3DXVECTOR3
*pouttranslation
, D3DXMATRIX
*pm
)
92 D3DXMATRIX normalized
;
95 /*Compute the scaling part.*/
99 poutscale
->x
=D3DXVec3Length(&vec
);
104 poutscale
->y
=D3DXVec3Length(&vec
);
109 poutscale
->z
=D3DXVec3Length(&vec
);
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];
116 /*Let's calculate the rotation now*/
117 if ( (poutscale
->x
== 0.0f
) || (poutscale
->y
== 0.0f
) || (poutscale
->z
== 0.0f
) )
119 return D3DERR_INVALIDCALL
;
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
;
132 D3DXQuaternionRotationMatrix(poutrotation
,&normalized
);
136 /*************************************************************************
137 * D3DXMatrixTransformation2D
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
)
145 D3DXQUATERNION rot
, sca_rot
;
146 D3DXVECTOR3 rot_center
, sca
, sca_center
, trans
;
148 if ( pscalingcenter
)
150 sca_center
.x
=pscalingcenter
->x
;
151 sca_center
.y
=pscalingcenter
->y
;
174 if ( protationcenter
)
176 rot_center
.x
=protationcenter
->x
;
177 rot_center
.y
=protationcenter
->y
;
189 trans
.x
=ptranslation
->x
;
190 trans
.y
=ptranslation
->y
;
200 rot
.w
=cos(rotation
/2.0f
);
203 rot
.z
=sin(rotation
/2.0f
);
205 sca_rot
.w
=cos(scalingrotation
/2.0f
);
208 sca_rot
.z
=sin(scalingrotation
/2.0f
);
210 D3DXMatrixTransformation(pout
, &sca_center
, &sca_rot
, &sca
, &rot_center
, &rot
, &trans
);
215 /*************************************************************************
216 * D3DXPlaneTransformArray
218 D3DXPLANE
* WINAPI
D3DXPlaneTransformArray(
219 D3DXPLANE
* out
, UINT outstride
, CONST D3DXPLANE
* in
, UINT instride
,
220 CONST D3DXMATRIX
* matrix
, UINT elements
)
224 for (i
= 0; i
< elements
; ++i
) {
226 (D3DXPLANE
*)((char*)out
+ outstride
* i
),
227 (CONST D3DXPLANE
*)((const char*)in
+ instride
* i
),
233 /*************************************************************************
234 * D3DXVec2TransformArray
236 * Transform an array of vectors by a matrix.
238 D3DXVECTOR4
* WINAPI
D3DXVec2TransformArray(
239 D3DXVECTOR4
* out
, UINT outstride
, CONST D3DXVECTOR2
* in
, UINT instride
,
240 CONST D3DXMATRIX
* matrix
, UINT elements
)
244 for (i
= 0; i
< elements
; ++i
) {
246 (D3DXVECTOR4
*)((char*)out
+ outstride
* i
),
247 (CONST D3DXVECTOR2
*)((const char*)in
+ instride
* i
),
253 /*************************************************************************
254 * D3DXVec2TransformCoordArray
256 D3DXVECTOR2
* WINAPI
D3DXVec2TransformCoordArray(
257 D3DXVECTOR2
* out
, UINT outstride
, CONST D3DXVECTOR2
* in
, UINT instride
,
258 CONST D3DXMATRIX
* matrix
, UINT elements
)
262 for (i
= 0; i
< elements
; ++i
) {
263 D3DXVec2TransformCoord(
264 (D3DXVECTOR2
*)((char*)out
+ outstride
* i
),
265 (CONST D3DXVECTOR2
*)((const char*)in
+ instride
* i
),
271 /*************************************************************************
272 * D3DXVec2TransformNormalArray
274 D3DXVECTOR2
* WINAPI
D3DXVec2TransformNormalArray(
275 D3DXVECTOR2
* out
, UINT outstride
, CONST D3DXVECTOR2
*in
, UINT instride
,
276 CONST D3DXMATRIX
*matrix
, UINT elements
)
280 for (i
= 0; i
< elements
; ++i
) {
281 D3DXVec2TransformNormal(
282 (D3DXVECTOR2
*)((char*)out
+ outstride
* i
),
283 (CONST D3DXVECTOR2
*)((const char*)in
+ instride
* i
),
289 /*************************************************************************
290 * D3DXVec3ProjectArray
292 * Projects an array of vectors to the screen.
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
)
301 for (i
= 0; i
< elements
; ++i
) {
303 (D3DXVECTOR3
*)((char*)out
+ outstride
* i
),
304 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
305 viewport
, projection
, view
, world
);
310 /*************************************************************************
311 * D3DXVec3TransformArray
313 D3DXVECTOR4
* WINAPI
D3DXVec3TransformArray(
314 D3DXVECTOR4
* out
, UINT outstride
, CONST D3DXVECTOR3
* in
, UINT instride
,
315 CONST D3DXMATRIX
* matrix
, UINT elements
)
319 for (i
= 0; i
< elements
; ++i
) {
321 (D3DXVECTOR4
*)((char*)out
+ outstride
* i
),
322 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
328 /*************************************************************************
329 * D3DXVec3TransformCoordArray
331 D3DXVECTOR3
* WINAPI
D3DXVec3TransformCoordArray(
332 D3DXVECTOR3
* out
, UINT outstride
, CONST D3DXVECTOR3
* in
, UINT instride
,
333 CONST D3DXMATRIX
* matrix
, UINT elements
)
337 for (i
= 0; i
< elements
; ++i
) {
338 D3DXVec3TransformCoord(
339 (D3DXVECTOR3
*)((char*)out
+ outstride
* i
),
340 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
346 /*************************************************************************
347 * D3DXVec3TransformNormalArray
349 D3DXVECTOR3
* WINAPI
D3DXVec3TransformNormalArray(
350 D3DXVECTOR3
* out
, UINT outstride
, CONST D3DXVECTOR3
* in
, UINT instride
,
351 CONST D3DXMATRIX
* matrix
, UINT elements
)
355 for (i
= 0; i
< elements
; ++i
) {
356 D3DXVec3TransformNormal(
357 (D3DXVECTOR3
*)((char*)out
+ outstride
* i
),
358 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
364 /*************************************************************************
365 * D3DXVec3UnprojectArray
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
)
374 for (i
= 0; i
< elements
; ++i
) {
376 (D3DXVECTOR3
*)((char*)out
+ outstride
* i
),
377 (CONST D3DXVECTOR3
*)((const char*)in
+ instride
* i
),
378 viewport
, projection
, view
, world
);
383 /*************************************************************************
384 * D3DXVec4TransformArray
386 D3DXVECTOR4
* WINAPI
D3DXVec4TransformArray(
387 D3DXVECTOR4
* out
, UINT outstride
, CONST D3DXVECTOR4
* in
, UINT instride
,
388 CONST D3DXMATRIX
* matrix
, UINT elements
)
392 for (i
= 0; i
< elements
; ++i
) {
394 (D3DXVECTOR4
*)((char*)out
+ outstride
* i
),
395 (CONST D3DXVECTOR4
*)((const char*)in
+ instride
* i
),