push d1f5df181c120dbe494f7c89b454752c2e0dcc04
[wine/hacks.git] / dlls / d3dx9_36 / math.c
blob2bf301b260281949e1d5044f32c55ddc05875b4a
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 "wine/debug.h"
28 #include "d3dx9.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;
41 D3DXQUATERNION rot;
42 D3DXVECTOR3 rot_center, trans;
44 rot.w=cos(rotation/2.0f);
45 rot.x=0.0f;
46 rot.y=0.0f;
47 rot.z=sin(rotation/2.0f);
49 if ( protationcenter )
51 rot_center.x=protationcenter->x;
52 rot_center.y=protationcenter->y;
53 rot_center.z=0.0f;
55 else
57 rot_center.x=0.0f;
58 rot_center.y=0.0f;
59 rot_center.z=0.0f;
62 if ( ptranslation )
64 trans.x=ptranslation->x;
65 trans.y=ptranslation->y;
66 trans.z=0.0f;
68 else
70 trans.x=0.0f;
71 trans.y=0.0f;
72 trans.z=0.0f;
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);
86 return pout;
89 /*************************************************************************
90 * D3DXMatrixDecompose
92 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, D3DXMATRIX *pm)
94 D3DXMATRIX normalized;
95 D3DXVECTOR3 vec;
97 /*Compute the scaling part.*/
98 vec.x=pm->u.m[0][0];
99 vec.y=pm->u.m[0][1];
100 vec.z=pm->u.m[0][2];
101 poutscale->x=D3DXVec3Length(&vec);
103 vec.x=pm->u.m[1][0];
104 vec.y=pm->u.m[1][1];
105 vec.z=pm->u.m[1][2];
106 poutscale->y=D3DXVec3Length(&vec);
108 vec.x=pm->u.m[2][0];
109 vec.y=pm->u.m[2][1];
110 vec.z=pm->u.m[2][2];
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);
135 return S_OK;
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;
154 sca_center.z=0.0f;
156 else
158 sca_center.x=0.0f;
159 sca_center.y=0.0f;
160 sca_center.z=0.0f;
163 if ( pscaling )
165 sca.x=pscaling->x;
166 sca.y=pscaling->y;
167 sca.z=1.0f;
169 else
171 sca.x=1.0f;
172 sca.y=1.0f;
173 sca.z=1.0f;
176 if ( protationcenter )
178 rot_center.x=protationcenter->x;
179 rot_center.y=protationcenter->y;
180 rot_center.z=0.0f;
182 else
184 rot_center.x=0.0f;
185 rot_center.y=0.0f;
186 rot_center.z=0.0f;
189 if ( ptranslation )
191 trans.x=ptranslation->x;
192 trans.y=ptranslation->y;
193 trans.z=0.0f;
195 else
197 trans.x=0.0f;
198 trans.y=0.0f;
199 trans.z=0.0f;
202 rot.w=cos(rotation/2.0f);
203 rot.x=0.0f;
204 rot.y=0.0f;
205 rot.z=sin(rotation/2.0f);
207 sca_rot.w=cos(scalingrotation/2.0f);
208 sca_rot.x=0.0f;
209 sca_rot.y=0.0f;
210 sca_rot.z=sin(scalingrotation/2.0f);
212 D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
214 return pout;
217 /*************************************************************************
218 * D3DXPlaneTransformArray
220 D3DXPLANE* WINAPI D3DXPlaneTransformArray(
221 D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride,
222 CONST D3DXMATRIX* matrix, UINT elements)
224 UINT i;
226 for (i = 0; i < elements; ++i) {
227 D3DXPlaneTransform(
228 (D3DXPLANE*)((char*)out + outstride * i),
229 (CONST D3DXPLANE*)((const char*)in + instride * i),
230 matrix);
232 return out;
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)
244 UINT i;
246 for (i = 0; i < elements; ++i) {
247 D3DXVec2Transform(
248 (D3DXVECTOR4*)((char*)out + outstride * i),
249 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
250 matrix);
252 return out;
255 /*************************************************************************
256 * D3DXVec2TransformCoordArray
258 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(
259 D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride,
260 CONST D3DXMATRIX* matrix, UINT elements)
262 UINT i;
264 for (i = 0; i < elements; ++i) {
265 D3DXVec2TransformCoord(
266 (D3DXVECTOR2*)((char*)out + outstride * i),
267 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
268 matrix);
270 return out;
273 /*************************************************************************
274 * D3DXVec2TransformNormalArray
276 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(
277 D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride,
278 CONST D3DXMATRIX *matrix, UINT elements)
280 UINT i;
282 for (i = 0; i < elements; ++i) {
283 D3DXVec2TransformNormal(
284 (D3DXVECTOR2*)((char*)out + outstride * i),
285 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
286 matrix);
288 return out;
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)
301 UINT i;
303 for (i = 0; i < elements; ++i) {
304 D3DXVec3Project(
305 (D3DXVECTOR3*)((char*)out + outstride * i),
306 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
307 viewport, projection, view, world);
309 return out;
312 /*************************************************************************
313 * D3DXVec3TransformArray
315 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(
316 D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
317 CONST D3DXMATRIX* matrix, UINT elements)
319 UINT i;
321 for (i = 0; i < elements; ++i) {
322 D3DXVec3Transform(
323 (D3DXVECTOR4*)((char*)out + outstride * i),
324 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
325 matrix);
327 return out;
330 /*************************************************************************
331 * D3DXVec3TransformCoordArray
333 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(
334 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
335 CONST D3DXMATRIX* matrix, UINT elements)
337 UINT i;
339 for (i = 0; i < elements; ++i) {
340 D3DXVec3TransformCoord(
341 (D3DXVECTOR3*)((char*)out + outstride * i),
342 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
343 matrix);
345 return out;
348 /*************************************************************************
349 * D3DXVec3TransformNormalArray
351 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(
352 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
353 CONST D3DXMATRIX* matrix, UINT elements)
355 UINT i;
357 for (i = 0; i < elements; ++i) {
358 D3DXVec3TransformNormal(
359 (D3DXVECTOR3*)((char*)out + outstride * i),
360 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
361 matrix);
363 return out;
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)
374 UINT i;
376 for (i = 0; i < elements; ++i) {
377 D3DXVec3Unproject(
378 (D3DXVECTOR3*)((char*)out + outstride * i),
379 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
380 viewport, projection, view, world);
382 return out;
385 /*************************************************************************
386 * D3DXVec4TransformArray
388 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(
389 D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride,
390 CONST D3DXMATRIX* matrix, UINT elements)
392 UINT i;
394 for (i = 0; i < elements; ++i) {
395 D3DXVec4Transform(
396 (D3DXVECTOR4*)((char*)out + outstride * i),
397 (CONST D3DXVECTOR4*)((const char*)in + instride * i),
398 matrix);
400 return out;