push e8d2f3f1a5d20911a70f6477421a6c3dacf00760
[wine/hacks.git] / dlls / d3dx9_36 / math.c
blobadee217beeb6adc240acc96b0add7eab50d52208
1 /*
2 * Mathematical operations specific to D3DX9.
4 * Copyright (C) 2008 Philip Nilsson
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "windef.h"
23 #include "wingdi.h"
24 #include "wine/debug.h"
25 #include "d3dx9.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
29 /*************************************************************************
30 * D3DXMatrixDecompose
32 HRESULT WINAPI D3DXMatrixDecompose(D3DXVECTOR3 *poutscale, D3DXQUATERNION *poutrotation, D3DXVECTOR3 *pouttranslation, D3DXMATRIX *pm)
34 D3DXMATRIX normalized;
35 D3DXVECTOR3 vec;
37 if (!pm)
39 return D3DERR_INVALIDCALL;
42 /*Compute the scaling part.*/
43 vec.x=pm->m[0][0];
44 vec.y=pm->m[0][1];
45 vec.z=pm->m[0][2];
46 poutscale->x=D3DXVec3Length(&vec);
48 vec.x=pm->m[1][0];
49 vec.y=pm->m[1][1];
50 vec.z=pm->m[1][2];
51 poutscale->y=D3DXVec3Length(&vec);
53 vec.x=pm->m[2][0];
54 vec.y=pm->m[2][1];
55 vec.z=pm->m[2][2];
56 poutscale->z=D3DXVec3Length(&vec);
58 /*Compute the translation part.*/
59 pouttranslation->x=pm->m[3][0];
60 pouttranslation->y=pm->m[3][1];
61 pouttranslation->z=pm->m[3][2];
63 /*Let's calculate the rotation now*/
64 if ( (poutscale->x == 0.0f) || (poutscale->y == 0.0f) || (poutscale->z == 0.0f) )
66 return D3DERR_INVALIDCALL;
69 normalized.m[0][0]=pm->m[0][0]/poutscale->x;
70 normalized.m[0][1]=pm->m[0][1]/poutscale->x;
71 normalized.m[0][2]=pm->m[0][2]/poutscale->x;
72 normalized.m[1][0]=pm->m[1][0]/poutscale->y;
73 normalized.m[1][1]=pm->m[1][1]/poutscale->y;
74 normalized.m[1][2]=pm->m[1][2]/poutscale->y;
75 normalized.m[2][0]=pm->m[2][0]/poutscale->z;
76 normalized.m[2][1]=pm->m[2][1]/poutscale->z;
77 normalized.m[2][2]=pm->m[2][2]/poutscale->z;
79 D3DXQuaternionRotationMatrix(poutrotation,&normalized);
80 return S_OK;
83 /*************************************************************************
84 * D3DXPlaneTransformArray
86 D3DXPLANE* WINAPI D3DXPlaneTransformArray(
87 D3DXPLANE* out, UINT outstride, CONST D3DXPLANE* in, UINT instride,
88 CONST D3DXMATRIX* matrix, UINT elements)
90 UINT i;
91 TRACE("\n");
92 for (i = 0; i < elements; ++i) {
93 D3DXPlaneTransform(
94 (D3DXPLANE*)((char*)out + outstride * i),
95 (CONST D3DXPLANE*)((const char*)in + instride * i),
96 matrix);
98 return out;
101 /*************************************************************************
102 * D3DXVec2TransformArray
104 * Transform an array of vectors by a matrix.
106 D3DXVECTOR4* WINAPI D3DXVec2TransformArray(
107 D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride,
108 CONST D3DXMATRIX* matrix, UINT elements)
110 UINT i;
111 TRACE("\n");
112 for (i = 0; i < elements; ++i) {
113 D3DXVec2Transform(
114 (D3DXVECTOR4*)((char*)out + outstride * i),
115 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
116 matrix);
118 return out;
121 /*************************************************************************
122 * D3DXVec2TransformCoordArray
124 D3DXVECTOR2* WINAPI D3DXVec2TransformCoordArray(
125 D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2* in, UINT instride,
126 CONST D3DXMATRIX* matrix, UINT elements)
128 UINT i;
129 TRACE("\n");
130 for (i = 0; i < elements; ++i) {
131 D3DXVec2TransformCoord(
132 (D3DXVECTOR2*)((char*)out + outstride * i),
133 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
134 matrix);
136 return out;
139 /*************************************************************************
140 * D3DXVec2TransformNormalArray
142 D3DXVECTOR2* WINAPI D3DXVec2TransformNormalArray(
143 D3DXVECTOR2* out, UINT outstride, CONST D3DXVECTOR2 *in, UINT instride,
144 CONST D3DXMATRIX *matrix, UINT elements)
146 UINT i;
147 TRACE("\n");
148 for (i = 0; i < elements; ++i) {
149 D3DXVec2TransformNormal(
150 (D3DXVECTOR2*)((char*)out + outstride * i),
151 (CONST D3DXVECTOR2*)((const char*)in + instride * i),
152 matrix);
154 return out;
157 /*************************************************************************
158 * D3DXVec3ProjectArray
160 * Projects an array of vectors to the screen.
162 D3DXVECTOR3* WINAPI D3DXVec3ProjectArray(
163 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
164 CONST D3DVIEWPORT9* viewport, CONST D3DXMATRIX* projection,
165 CONST D3DXMATRIX* view, CONST D3DXMATRIX* world, UINT elements)
167 UINT i;
168 TRACE("\n");
169 for (i = 0; i < elements; ++i) {
170 D3DXVec3Project(
171 (D3DXVECTOR3*)((char*)out + outstride * i),
172 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
173 viewport, projection, view, world);
175 return out;
178 /*************************************************************************
179 * D3DXVec3TransformArray
181 D3DXVECTOR4* WINAPI D3DXVec3TransformArray(
182 D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
183 CONST D3DXMATRIX* matrix, UINT elements)
185 UINT i;
186 TRACE("\n");
187 for (i = 0; i < elements; ++i) {
188 D3DXVec3Transform(
189 (D3DXVECTOR4*)((char*)out + outstride * i),
190 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
191 matrix);
193 return out;
196 /*************************************************************************
197 * D3DXVec3TransformCoordArray
199 D3DXVECTOR3* WINAPI D3DXVec3TransformCoordArray(
200 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
201 CONST D3DXMATRIX* matrix, UINT elements)
203 UINT i;
204 TRACE("\n");
205 for (i = 0; i < elements; ++i) {
206 D3DXVec3TransformCoord(
207 (D3DXVECTOR3*)((char*)out + outstride * i),
208 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
209 matrix);
211 return out;
214 /*************************************************************************
215 * D3DXVec3TransformNormalArray
217 D3DXVECTOR3* WINAPI D3DXVec3TransformNormalArray(
218 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
219 CONST D3DXMATRIX* matrix, UINT elements)
221 UINT i;
222 TRACE("\n");
223 for (i = 0; i < elements; ++i) {
224 D3DXVec3TransformNormal(
225 (D3DXVECTOR3*)((char*)out + outstride * i),
226 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
227 matrix);
229 return out;
232 /*************************************************************************
233 * D3DXVec3UnprojectArray
235 D3DXVECTOR3* WINAPI D3DXVec3UnprojectArray(
236 D3DXVECTOR3* out, UINT outstride, CONST D3DXVECTOR3* in, UINT instride,
237 CONST D3DVIEWPORT9* viewport, CONST D3DXMATRIX* projection,
238 CONST D3DXMATRIX* view, CONST D3DXMATRIX* world, UINT elements)
240 UINT i;
241 TRACE("\n");
242 for (i = 0; i < elements; ++i) {
243 D3DXVec3Unproject(
244 (D3DXVECTOR3*)((char*)out + outstride * i),
245 (CONST D3DXVECTOR3*)((const char*)in + instride * i),
246 viewport, projection, view, world);
248 return out;
251 /*************************************************************************
252 * D3DXVec4TransformArray
254 D3DXVECTOR4* WINAPI D3DXVec4TransformArray(
255 D3DXVECTOR4* out, UINT outstride, CONST D3DXVECTOR4* in, UINT instride,
256 CONST D3DXMATRIX* matrix, UINT elements)
258 UINT i;
259 TRACE("\n");
260 for (i = 0; i < elements; ++i) {
261 D3DXVec4Transform(
262 (D3DXVECTOR4*)((char*)out + outstride * i),
263 (CONST D3DXVECTOR4*)((const char*)in + instride * i),
264 matrix);
266 return out;