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;
89 return D3DERR_INVALIDCALL;
92 /*Compute the scaling part.*/
96 poutscale->x=D3DXVec3Length(&vec);
101 poutscale->y=D3DXVec3Length(&vec);
106 poutscale->z=D3DXVec3Length(&vec);
108 /*Compute the translation part.*/
109 pouttranslation->x=pm->u.m[3][0];
110 pouttranslation->y=pm->u.m[3][1];
111 pouttranslation->z=pm->u.m[3][2];
113 /*Let's calculate the rotation now*/
114 if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) )
116 return D3DERR_INVALIDCALL;
119 normalized.u.m[0][0]=pm->u.m[0][0]/poutscale->x;
120 normalized.u.m[0][1]=pm->u.m[0][1]/poutscale->x;
121 normalized.u.m[0][2]=pm->u.m[0][2]/poutscale->x;
122 normalized.u.m[1][0]=pm->u.m[1][0]/poutscale->y;
123 normalized.u.m[1][1]=pm->u.m[1][1]/poutscale->y;
124 normalized.u.m[1][2]=pm->u.m[1][2]/poutscale->y;
125 normalized.u.m[2][0]=pm->u.m[2][0]/poutscale->z;
126 normalized.u.m[2][1]=pm->u.m[2][1]/poutscale->z;
127 normalized.u.m[2][2]=pm->u.m[2][2]/poutscale->z;
129 D3DXQuaternionRotationMatrix(poutrotation,&normalized);
133 /*************************************************************************
134 * D3DXMatrixTransformation2D
136 D3DXMATRIX* WINAPI D3DXMatrixTransformation2D(
137 D3DXMATRIX *pout, CONST D3DXVECTOR2 *pscalingcenter,
138 FLOAT scalingrotation, CONST D3DXVECTOR2 *pscaling,
139 CONST D3DXVECTOR2 *protationcenter, FLOAT rotation,
140 CONST D3DXVECTOR2 *ptranslation)
142 D3DXQUATERNION rot, sca_rot;
143 D3DXVECTOR3 rot_center, sca, sca_center, trans;
145 if ( pscalingcenter )
147 sca_center.x=pscalingcenter->x;
148 sca_center.y=pscalingcenter->y;
171 if ( protationcenter )
173 rot_center.x=protationcenter->x;
174 rot_center.y=protationcenter->y;
186 trans.x=ptranslation->x;
187 trans.y=ptranslation->y;
197 rot.w=cos(rotation/2.0f);
200 rot.z=sin(rotation/2.0f);
202 sca_rot.w=cos(scalingrotation/2.0f);
205 sca_rot.z=sin(scalingrotation/2.0f);
207 D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
212 /*************************************************************************
213 * D3DXPlaneTransformArray
215 D3DXPLANE* WINAPI D3DXPlaneTransformArray(
216 D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride,
217 CONST D3DXMATRIX* matrix, UINT elements)
221 for (i = 0; i < elements; ++i) {
223 (D3DXPLANE*)((char*)out + outstride * i),
224 (CONST D3DXPLANE*)((const char*)in + instride * i),
230 /*************************************************************************
231 * D3DXVec2TransformArray
233 * Transform an array of vectors by a matrix.
235 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(
236 D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride,
237 CONST D3DXMATRIX* matrix, UINT elements)
241 for (i = 0; i < elements; ++i) {
243 (D3DXVECTOR4*)((char*)out + outstride * i),
244 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
250 /*************************************************************************
251 * D3DXVec2TransformCoordArray
253 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(
254 D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride,
255 CONST D3DXMATRIX* matrix, UINT elements)
259 for (i = 0; i < elements; ++i) {
260 D3DXVec2TransformCoord(
261 (D3DXVECTOR2*)((char*)out + outstride * i),
262 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
268 /*************************************************************************
269 * D3DXVec2TransformNormalArray
271 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(
272 D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride,
273 CONST D3DXMATRIX *matrix, UINT elements)
277 for (i = 0; i < elements; ++i) {
278 D3DXVec2TransformNormal(
279 (D3DXVECTOR2*)((char*)out + outstride * i),
280 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
286 /*************************************************************************
287 * D3DXVec3ProjectArray
289 * Projects an array of vectors to the screen.
291 D3DXVECTOR3* WINAPI D3DXVec3ProjectArray(
292 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
293 CONST D3DVIEWPORT9* viewport, CONST D3DXMATRIX* projection,
294 CONST D3DXMATRIX* view, CONST D3DXMATRIX* world, UINT elements)
298 for (i = 0; i < elements; ++i) {
300 (D3DXVECTOR3*)((char*)out + outstride * i),
301 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
302 viewport, projection, view, world);
307 /*************************************************************************
308 * D3DXVec3TransformArray
310 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(
311 D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
312 CONST D3DXMATRIX* matrix, UINT elements)
316 for (i = 0; i < elements; ++i) {
318 (D3DXVECTOR4*)((char*)out + outstride * i),
319 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
325 /*************************************************************************
326 * D3DXVec3TransformCoordArray
328 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(
329 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
330 CONST D3DXMATRIX* matrix, UINT elements)
334 for (i = 0; i < elements; ++i) {
335 D3DXVec3TransformCoord(
336 (D3DXVECTOR3*)((char*)out + outstride * i),
337 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
343 /*************************************************************************
344 * D3DXVec3TransformNormalArray
346 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(
347 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
348 CONST D3DXMATRIX* matrix, UINT elements)
352 for (i = 0; i < elements; ++i) {
353 D3DXVec3TransformNormal(
354 (D3DXVECTOR3*)((char*)out + outstride * i),
355 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
361 /*************************************************************************
362 * D3DXVec3UnprojectArray
364 D3DXVECTOR3* WINAPI D3DXVec3UnprojectArray(
365 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
366 CONST D3DVIEWPORT9* viewport, CONST D3DXMATRIX* projection,
367 CONST D3DXMATRIX* view, CONST D3DXMATRIX* world, UINT elements)
371 for (i = 0; i < elements; ++i) {
373 (D3DXVECTOR3*)((char*)out + outstride * i),
374 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
375 viewport, projection, view, world);
380 /*************************************************************************
381 * D3DXVec4TransformArray
383 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(
384 D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride,
385 CONST D3DXMATRIX* matrix, UINT elements)
389 for (i = 0; i < elements; ++i) {
391 (D3DXVECTOR4*)((char*)out + outstride * i),
392 (CONST D3DXVECTOR4*)((const char*)in + instride * i),