push 955563f995be8b0942dbb757ceb5b3a4c8ccafbf
[wine/hacks.git] / dlls / d3dx9_36 / math.c
blobe73530376beb072f48ffa5a9fda4e32093a610d1
1 /*
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
24 #include "config.h"
25 #include "windef.h"
26 #include "wingdi.h"
27 #include "d3dx9.h"
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;
39 D3DXQUATERNION rot;
40 D3DXVECTOR3 rot_center, trans;
42 rot.w=cos(rotation/2.0f);
43 rot.x=0.0f;
44 rot.y=0.0f;
45 rot.z=sin(rotation/2.0f);
47 if ( protationcenter )
49 rot_center.x=protationcenter->x;
50 rot_center.y=protationcenter->y;
51 rot_center.z=0.0f;
53 else
55 rot_center.x=0.0f;
56 rot_center.y=0.0f;
57 rot_center.z=0.0f;
60 if ( ptranslation )
62 trans.x=ptranslation->x;
63 trans.y=ptranslation->y;
64 trans.z=0.0f;
66 else
68 trans.x=0.0f;
69 trans.y=0.0f;
70 trans.z=0.0f;
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);
84 return pout;
87 /*************************************************************************
88 * D3DXMatrixDecompose
90 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, D3DXMATRIX *pm)
92 D3DXMATRIX normalized;
93 D3DXVECTOR3 vec;
95 /*Compute the scaling part.*/
96 vec.x=pm->u.m[0][0];
97 vec.y=pm->u.m[0][1];
98 vec.z=pm->u.m[0][2];
99 poutscale->x=D3DXVec3Length(&vec);
101 vec.x=pm->u.m[1][0];
102 vec.y=pm->u.m[1][1];
103 vec.z=pm->u.m[1][2];
104 poutscale->y=D3DXVec3Length(&vec);
106 vec.x=pm->u.m[2][0];
107 vec.y=pm->u.m[2][1];
108 vec.z=pm->u.m[2][2];
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);
133 return S_OK;
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;
152 sca_center.z=0.0f;
154 else
156 sca_center.x=0.0f;
157 sca_center.y=0.0f;
158 sca_center.z=0.0f;
161 if ( pscaling )
163 sca.x=pscaling->x;
164 sca.y=pscaling->y;
165 sca.z=1.0f;
167 else
169 sca.x=1.0f;
170 sca.y=1.0f;
171 sca.z=1.0f;
174 if ( protationcenter )
176 rot_center.x=protationcenter->x;
177 rot_center.y=protationcenter->y;
178 rot_center.z=0.0f;
180 else
182 rot_center.x=0.0f;
183 rot_center.y=0.0f;
184 rot_center.z=0.0f;
187 if ( ptranslation )
189 trans.x=ptranslation->x;
190 trans.y=ptranslation->y;
191 trans.z=0.0f;
193 else
195 trans.x=0.0f;
196 trans.y=0.0f;
197 trans.z=0.0f;
200 rot.w=cos(rotation/2.0f);
201 rot.x=0.0f;
202 rot.y=0.0f;
203 rot.z=sin(rotation/2.0f);
205 sca_rot.w=cos(scalingrotation/2.0f);
206 sca_rot.x=0.0f;
207 sca_rot.y=0.0f;
208 sca_rot.z=sin(scalingrotation/2.0f);
210 D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
212 return pout;
215 /*************************************************************************
216 * D3DXPlaneTransformArray
218 D3DXPLANE* WINAPI D3DXPlaneTransformArray(
219 D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride,
220 CONST D3DXMATRIX* matrix, UINT elements)
222 UINT i;
224 for (i = 0; i < elements; ++i) {
225 D3DXPlaneTransform(
226 (D3DXPLANE*)((char*)out + outstride * i),
227 (CONST D3DXPLANE*)((const char*)in + instride * i),
228 matrix);
230 return out;
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)
242 UINT i;
244 for (i = 0; i < elements; ++i) {
245 D3DXVec2Transform(
246 (D3DXVECTOR4*)((char*)out + outstride * i),
247 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
248 matrix);
250 return out;
253 /*************************************************************************
254 * D3DXVec2TransformCoordArray
256 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(
257 D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride,
258 CONST D3DXMATRIX* matrix, UINT elements)
260 UINT i;
262 for (i = 0; i < elements; ++i) {
263 D3DXVec2TransformCoord(
264 (D3DXVECTOR2*)((char*)out + outstride * i),
265 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
266 matrix);
268 return out;
271 /*************************************************************************
272 * D3DXVec2TransformNormalArray
274 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(
275 D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride,
276 CONST D3DXMATRIX *matrix, UINT elements)
278 UINT i;
280 for (i = 0; i < elements; ++i) {
281 D3DXVec2TransformNormal(
282 (D3DXVECTOR2*)((char*)out + outstride * i),
283 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
284 matrix);
286 return out;
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)
299 UINT i;
301 for (i = 0; i < elements; ++i) {
302 D3DXVec3Project(
303 (D3DXVECTOR3*)((char*)out + outstride * i),
304 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
305 viewport, projection, view, world);
307 return out;
310 /*************************************************************************
311 * D3DXVec3TransformArray
313 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(
314 D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
315 CONST D3DXMATRIX* matrix, UINT elements)
317 UINT i;
319 for (i = 0; i < elements; ++i) {
320 D3DXVec3Transform(
321 (D3DXVECTOR4*)((char*)out + outstride * i),
322 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
323 matrix);
325 return out;
328 /*************************************************************************
329 * D3DXVec3TransformCoordArray
331 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(
332 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
333 CONST D3DXMATRIX* matrix, UINT elements)
335 UINT i;
337 for (i = 0; i < elements; ++i) {
338 D3DXVec3TransformCoord(
339 (D3DXVECTOR3*)((char*)out + outstride * i),
340 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
341 matrix);
343 return out;
346 /*************************************************************************
347 * D3DXVec3TransformNormalArray
349 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(
350 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
351 CONST D3DXMATRIX* matrix, UINT elements)
353 UINT i;
355 for (i = 0; i < elements; ++i) {
356 D3DXVec3TransformNormal(
357 (D3DXVECTOR3*)((char*)out + outstride * i),
358 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
359 matrix);
361 return out;
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)
372 UINT i;
374 for (i = 0; i < elements; ++i) {
375 D3DXVec3Unproject(
376 (D3DXVECTOR3*)((char*)out + outstride * i),
377 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
378 viewport, projection, view, world);
380 return out;
383 /*************************************************************************
384 * D3DXVec4TransformArray
386 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(
387 D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride,
388 CONST D3DXMATRIX* matrix, UINT elements)
390 UINT i;
392 for (i = 0; i < elements; ++i) {
393 D3DXVec4Transform(
394 (D3DXVECTOR4*)((char*)out + outstride * i),
395 (CONST D3DXVECTOR4*)((const char*)in + instride * i),
396 matrix);
398 return out;