push 9eb9af089d68d39110a91889d3a673043db63c4b
[wine/hacks.git] / dlls / d3dx9_36 / math.c
bloba00ffc54a1f897fd80fc18458dd53932c3576188
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 D3DXQUATERNION rot;
41 D3DXVECTOR3 rot_center, trans;
43 rot.w=cos(rotation/2.0f);
44 rot.x=0.0f;
45 rot.y=0.0f;
46 rot.z=sin(rotation/2.0f);
48 if ( protationcenter )
50 rot_center.x=protationcenter->x;
51 rot_center.y=protationcenter->y;
52 rot_center.z=0.0f;
54 else
56 rot_center.x=0.0f;
57 rot_center.y=0.0f;
58 rot_center.z=0.0f;
61 if ( ptranslation )
63 trans.x=ptranslation->x;
64 trans.y=ptranslation->y;
65 trans.z=0.0f;
67 else
69 trans.x=0.0f;
70 trans.y=0.0f;
71 trans.z=0.0f;
74 D3DXMatrixAffineTransformation(pout, scaling, &rot_center, &rot, &trans);
76 return pout;
79 /*************************************************************************
80 * D3DXMatrixDecompose
82 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, D3DXMATRIX *pm)
84 D3DXMATRIX normalized;
85 D3DXVECTOR3 vec;
87 if (!pm)
89 return D3DERR_INVALIDCALL;
92 /*Compute the scaling part.*/
93 vec.x=pm->u.m[0][0];
94 vec.y=pm->u.m[0][1];
95 vec.z=pm->u.m[0][2];
96 poutscale->x=D3DXVec3Length(&vec);
98 vec.x=pm->u.m[1][0];
99 vec.y=pm->u.m[1][1];
100 vec.z=pm->u.m[1][2];
101 poutscale->y=D3DXVec3Length(&vec);
103 vec.x=pm->u.m[2][0];
104 vec.y=pm->u.m[2][1];
105 vec.z=pm->u.m[2][2];
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);
130 return S_OK;
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;
149 sca_center.z=0.0f;
151 else
153 sca_center.x=0.0f;
154 sca_center.y=0.0f;
155 sca_center.z=0.0f;
158 if ( pscaling )
160 sca.x=pscaling->x;
161 sca.y=pscaling->y;
162 sca.z=0.0f;
164 else
166 sca.x=0.0f;
167 sca.y=0.0f;
168 sca.z=0.0f;
171 if ( protationcenter )
173 rot_center.x=protationcenter->x;
174 rot_center.y=protationcenter->y;
175 rot_center.z=0.0f;
177 else
179 rot_center.x=0.0f;
180 rot_center.y=0.0f;
181 rot_center.z=0.0f;
184 if ( ptranslation )
186 trans.x=ptranslation->x;
187 trans.y=ptranslation->y;
188 trans.z=0.0f;
190 else
192 trans.x=0.0f;
193 trans.y=0.0f;
194 trans.z=0.0f;
197 rot.w=cos(rotation/2.0f);
198 rot.x=0.0f;
199 rot.y=0.0f;
200 rot.z=sin(rotation/2.0f);
202 sca_rot.w=cos(scalingrotation/2.0f);
203 sca_rot.x=0.0f;
204 sca_rot.y=0.0f;
205 sca_rot.z=sin(scalingrotation/2.0f);
207 D3DXMatrixTransformation(pout, &sca_center, &sca_rot, &sca, &rot_center, &rot, &trans);
209 return pout;
212 /*************************************************************************
213 * D3DXPlaneTransformArray
215 D3DXPLANE* WINAPI D3DXPlaneTransformArray(
216 D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride,
217 CONST D3DXMATRIX* matrix, UINT elements)
219 UINT i;
220 TRACE("\n");
221 for (i = 0; i < elements; ++i) {
222 D3DXPlaneTransform(
223 (D3DXPLANE*)((char*)out + outstride * i),
224 (CONST D3DXPLANE*)((const char*)in + instride * i),
225 matrix);
227 return out;
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)
239 UINT i;
240 TRACE("\n");
241 for (i = 0; i < elements; ++i) {
242 D3DXVec2Transform(
243 (D3DXVECTOR4*)((char*)out + outstride * i),
244 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
245 matrix);
247 return out;
250 /*************************************************************************
251 * D3DXVec2TransformCoordArray
253 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(
254 D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride,
255 CONST D3DXMATRIX* matrix, UINT elements)
257 UINT i;
258 TRACE("\n");
259 for (i = 0; i < elements; ++i) {
260 D3DXVec2TransformCoord(
261 (D3DXVECTOR2*)((char*)out + outstride * i),
262 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
263 matrix);
265 return out;
268 /*************************************************************************
269 * D3DXVec2TransformNormalArray
271 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(
272 D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride,
273 CONST D3DXMATRIX *matrix, UINT elements)
275 UINT i;
276 TRACE("\n");
277 for (i = 0; i < elements; ++i) {
278 D3DXVec2TransformNormal(
279 (D3DXVECTOR2*)((char*)out + outstride * i),
280 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
281 matrix);
283 return out;
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)
296 UINT i;
297 TRACE("\n");
298 for (i = 0; i < elements; ++i) {
299 D3DXVec3Project(
300 (D3DXVECTOR3*)((char*)out + outstride * i),
301 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
302 viewport, projection, view, world);
304 return out;
307 /*************************************************************************
308 * D3DXVec3TransformArray
310 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(
311 D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
312 CONST D3DXMATRIX* matrix, UINT elements)
314 UINT i;
315 TRACE("\n");
316 for (i = 0; i < elements; ++i) {
317 D3DXVec3Transform(
318 (D3DXVECTOR4*)((char*)out + outstride * i),
319 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
320 matrix);
322 return out;
325 /*************************************************************************
326 * D3DXVec3TransformCoordArray
328 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(
329 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
330 CONST D3DXMATRIX* matrix, UINT elements)
332 UINT i;
333 TRACE("\n");
334 for (i = 0; i < elements; ++i) {
335 D3DXVec3TransformCoord(
336 (D3DXVECTOR3*)((char*)out + outstride * i),
337 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
338 matrix);
340 return out;
343 /*************************************************************************
344 * D3DXVec3TransformNormalArray
346 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(
347 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
348 CONST D3DXMATRIX* matrix, UINT elements)
350 UINT i;
351 TRACE("\n");
352 for (i = 0; i < elements; ++i) {
353 D3DXVec3TransformNormal(
354 (D3DXVECTOR3*)((char*)out + outstride * i),
355 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
356 matrix);
358 return out;
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)
369 UINT i;
370 TRACE("\n");
371 for (i = 0; i < elements; ++i) {
372 D3DXVec3Unproject(
373 (D3DXVECTOR3*)((char*)out + outstride * i),
374 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
375 viewport, projection, view, world);
377 return out;
380 /*************************************************************************
381 * D3DXVec4TransformArray
383 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(
384 D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride,
385 CONST D3DXMATRIX* matrix, UINT elements)
387 UINT i;
388 TRACE("\n");
389 for (i = 0; i < elements; ++i) {
390 D3DXVec4Transform(
391 (D3DXVECTOR4*)((char*)out + outstride * i),
392 (CONST D3DXVECTOR4*)((const char*)in + instride * i),
393 matrix);
395 return out;