收集几个d3d算法

2016-6-14 admin 备忘

http://source.winehq.org/git/wine.git/blob/16a8efa410859fcaadc80b931fb1949a086ece4d:/dlls/d3dx8/math.c
 

[wine.git] / dlls / d3dx8 / math.c
1 /*
2 * Copyright 2007 David Adam
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <assert.h>
23
24 #define NONAMELESSUNION
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "d3dx8.h"
30
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(d3dx8);
34
35 /*_________________D3DXColor____________________*/
36
37 D3DXCOLOR* WINAPI D3DXColorAdjustContrast(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
38 {
39 pout->r = 0.5f + s * (pc->r - 0.5f);
40 pout->g = 0.5f + s * (pc->g - 0.5f);
41 pout->b = 0.5f + s * (pc->b - 0.5f);
42 pout->a = pc->a;
43 return pout;
44 }
45
46 D3DXCOLOR* WINAPI D3DXColorAdjustSaturation(D3DXCOLOR *pout, CONST D3DXCOLOR *pc, FLOAT s)
47 {
48 FLOAT grey;
49
50 grey = pc->r * 0.2125f + pc->g * 0.7154f + pc->b * 0.0721f;
51 pout->r = grey + s * (pc->r - grey);
52 pout->g = grey + s * (pc->g - grey);
53 pout->b = grey + s * (pc->b - grey);
54 pout->a = pc->a;
55 return pout;
56 }
57
58 /*_________________D3DXMatrix____________________*/
59
60 D3DXMATRIX* WINAPI D3DXMatrixAffineTransformation(D3DXMATRIX *pout, float scaling, D3DXVECTOR3 *rotationcenter, D3DXQUATERNION *rotation, D3DXVECTOR3 *translation)
61 {
62 D3DXMATRIX m1, m2, m3, m4, m5, p1, p2, p3;
63
64 D3DXMatrixScaling(&m1, scaling, scaling, scaling);
65 if ( !rotationcenter )
66 {
67 D3DXMatrixIdentity(&m2);
68 D3DXMatrixIdentity(&m4);
69 }
70 else
71 {
72 D3DXMatrixTranslation(&m2, -rotationcenter->x, -rotationcenter->y, -rotationcenter->z);
73 D3DXMatrixTranslation(&m4, rotationcenter->x, rotationcenter->y, rotationcenter->z);
74 }
75 if ( !rotation )
76 {
77 D3DXMatrixIdentity(&m3);
78 }
79 else
80 {
81 D3DXMatrixRotationQuaternion(&m3, rotation);
82 }
83 if ( !translation )
84 {
85 D3DXMatrixIdentity(&m5);
86 }
87 else
88 {
89 D3DXMatrixTranslation(&m5, translation->x, translation->y, translation->z);
90 }
91 D3DXMatrixMultiply(&p1, &m1, &m2);
92 D3DXMatrixMultiply(&p2, &p1, &m3);
93 D3DXMatrixMultiply(&p3, &p2, &m4);
94 D3DXMatrixMultiply(pout, &p3, &m5);
95 return pout;
96 }
97
98 FLOAT WINAPI D3DXMatrixfDeterminant(CONST D3DXMATRIX *pm)
99 {
100 D3DXVECTOR4 minor, v1, v2, v3;
101 FLOAT det;
102
103 v1.x = pm->u.m[0][0]; v1.y = pm->u.m[1][0]; v1.z = pm->u.m[2][0]; v1.w = pm->u.m[3][0];
104 v2.x = pm->u.m[0][1]; v2.y = pm->u.m[1][1]; v2.z = pm->u.m[2][1]; v2.w = pm->u.m[3][1];
105 v3.x = pm->u.m[0][2]; v3.y = pm->u.m[1][2]; v3.z = pm->u.m[2][2]; v3.w = pm->u.m[3][2];
106 D3DXVec4Cross(&minor,&v1,&v2,&v3);
107 det = - (pm->u.m[0][3] * minor.x + pm->u.m[1][3] * minor.y + pm->u.m[2][3] * minor.z + pm->u.m[3][3] * minor.w);
108 return det;
109 }
110
111 D3DXMATRIX* WINAPI D3DXMatrixInverse(D3DXMATRIX *pout, FLOAT *pdeterminant, CONST D3DXMATRIX *pm)
112 {
113 int a, i, j;
114 D3DXVECTOR4 v, vec[3];
115 FLOAT cofactor, det;
116
117 det = D3DXMatrixfDeterminant(pm);
118 if ( !det ) return NULL;
119 if ( pdeterminant ) *pdeterminant = det;
120 for (i=0; i<4; i++)
121 {
122 for (j=0; j<4; j++)
123 {
124 if (j != i )
125 {
126 a = j;
127 if ( j > i ) a = a-1;
128 vec[a].x = pm->u.m[j][0];
129 vec[a].y = pm->u.m[j][1];
130 vec[a].z = pm->u.m[j][2];
131 vec[a].w = pm->u.m[j][3];
132 }
133 }
134 D3DXVec4Cross(&v, &vec[0], &vec[1], &vec[2]);
135 for (j=0; j<4; j++)
136 {
137 switch(j)
138 {
139 case 0: cofactor = v.x; break;
140 case 1: cofactor = v.y; break;
141 case 2: cofactor = v.z; break;
142 case 3: cofactor = v.w; break;
143 }
144 pout->u.m[j][i] = pow(-1.0f, i) * cofactor / det;
145 }
146 }
147 return pout;
148 }
149
150 D3DXMATRIX* WINAPI D3DXMatrixLookAtLH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
151 {
152 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
153
154 D3DXVec3Subtract(&vec2, pat, peye);
155 D3DXVec3Normalize(&vec, &vec2);
156 D3DXVec3Cross(&right, pup, &vec);
157 D3DXVec3Cross(&up, &vec, &right);
158 D3DXVec3Normalize(&rightn, &right);
159 D3DXVec3Normalize(&upn, &up);
160 pout->u.m[0][0] = rightn.x;
161 pout->u.m[1][0] = rightn.y;
162 pout->u.m[2][0] = rightn.z;
163 pout->u.m[3][0] = -D3DXVec3Dot(&rightn,peye);
164 pout->u.m[0][1] = upn.x;
165 pout->u.m[1][1] = upn.y;
166 pout->u.m[2][1] = upn.z;
167 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
168 pout->u.m[0][2] = vec.x;
169 pout->u.m[1][2] = vec.y;
170 pout->u.m[2][2] = vec.z;
171 pout->u.m[3][2] = -D3DXVec3Dot(&vec, peye);
172 pout->u.m[0][3] = 0.0f;
173 pout->u.m[1][3] = 0.0f;
174 pout->u.m[2][3] = 0.0f;
175 pout->u.m[3][3] = 1.0f;
176 return pout;
177 }
178
179 D3DXMATRIX* WINAPI D3DXMatrixLookAtRH(D3DXMATRIX *pout, CONST D3DXVECTOR3 *peye, CONST D3DXVECTOR3 *pat, CONST D3DXVECTOR3 *pup)
180 {
181 D3DXVECTOR3 right, rightn, up, upn, vec, vec2;
182
183 D3DXVec3Subtract(&vec2, pat, peye);
184 D3DXVec3Normalize(&vec, &vec2);
185 D3DXVec3Cross(&right, pup, &vec);
186 D3DXVec3Cross(&up, &vec, &right);
187 D3DXVec3Normalize(&rightn, &right);
188 D3DXVec3Normalize(&upn, &up);
189 pout->u.m[0][0] = -rightn.x;
190 pout->u.m[1][0] = -rightn.y;
191 pout->u.m[2][0] = -rightn.z;
192 pout->u.m[3][0] = D3DXVec3Dot(&rightn,peye);
193 pout->u.m[0][1] = upn.x;
194 pout->u.m[1][1] = upn.y;
195 pout->u.m[2][1] = upn.z;
196 pout->u.m[3][1] = -D3DXVec3Dot(&upn, peye);
197 pout->u.m[0][2] = -vec.x;
198 pout->u.m[1][2] = -vec.y;
199 pout->u.m[2][2] = -vec.z;
200 pout->u.m[3][2] = D3DXVec3Dot(&vec, peye);
201 pout->u.m[0][3] = 0.0f;
202 pout->u.m[1][3] = 0.0f;
203 pout->u.m[2][3] = 0.0f;
204 pout->u.m[3][3] = 1.0f;
205 return pout;
206 }
207
208 D3DXMATRIX* WINAPI D3DXMatrixMultiply(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
209 {
210 int i,j;
211
212 for (i=0; i<4; i++)
213 {
214 for (j=0; j<4; j++)
215 {
216 pout->u.m[i][j] = pm1->u.m[i][0] * pm2->u.m[0][j] + pm1->u.m[i][1] * pm2->u.m[1][j] + pm1->u.m[i][2] * pm2->u.m[2][j] + pm1->u.m[i][3] * pm2->u.m[3][j];
217 }
218 }
219 return pout;
220 }
221
222 D3DXMATRIX* WINAPI D3DXMatrixMultiplyTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm1, CONST D3DXMATRIX *pm2)
223 {
224 D3DXMATRIX temp;
225
226 D3DXMatrixMultiply(&temp, pm1, pm2);
227 D3DXMatrixTranspose(pout, &temp);
228 return pout;
229 }
230
231 D3DXMATRIX* WINAPI D3DXMatrixOrthoLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
232 {
233 D3DXMatrixIdentity(pout);
234 pout->u.m[0][0] = 2.0f / w;
235 pout->u.m[1][1] = 2.0f / h;
236 pout->u.m[2][2] = 1.0f / (zf - zn);
237 pout->u.m[3][2] = zn / (zn - zf);
238 return pout;
239 }
240
241 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
242 {
243 D3DXMatrixIdentity(pout);
244 pout->u.m[0][0] = 2.0f / (r - l);
245 pout->u.m[1][1] = 2.0f / (t - b);
246 pout->u.m[2][2] = 1.0f / (zf -zn);
247 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
248 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
249 pout->u.m[3][2] = zn / (zn -zf);
250 return pout;
251 }
252
253 D3DXMATRIX* WINAPI D3DXMatrixOrthoOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
254 {
255 D3DXMatrixIdentity(pout);
256 pout->u.m[0][0] = 2.0f / (r - l);
257 pout->u.m[1][1] = 2.0f / (t - b);
258 pout->u.m[2][2] = 1.0f / (zn -zf);
259 pout->u.m[3][0] = -1.0f -2.0f *l / (r - l);
260 pout->u.m[3][1] = 1.0f + 2.0f * t / (b - t);
261 pout->u.m[3][2] = zn / (zn -zf);
262 return pout;
263 }
264
265 D3DXMATRIX* WINAPI D3DXMatrixOrthoRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
266 {
267 D3DXMatrixIdentity(pout);
268 pout->u.m[0][0] = 2.0f / w;
269 pout->u.m[1][1] = 2.0f / h;
270 pout->u.m[2][2] = 1.0f / (zn - zf);
271 pout->u.m[3][2] = zn / (zn - zf);
272 return pout;
273 }
274
275 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovLH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
276 {
277 D3DXMatrixIdentity(pout);
278 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
279 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
280 pout->u.m[2][2] = zf / (zf - zn);
281 pout->u.m[2][3] = 1.0f;
282 pout->u.m[3][2] = (zf * zn) / (zn - zf);
283 pout->u.m[3][3] = 0.0f;
284 return pout;
285 }
286
287 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveFovRH(D3DXMATRIX *pout, FLOAT fovy, FLOAT aspect, FLOAT zn, FLOAT zf)
288 {
289 D3DXMatrixIdentity(pout);
290 pout->u.m[0][0] = 1.0f / (aspect * tan(fovy/2.0f));
291 pout->u.m[1][1] = 1.0f / tan(fovy/2.0f);
292 pout->u.m[2][2] = zf / (zn - zf);
293 pout->u.m[2][3] = -1.0f;
294 pout->u.m[3][2] = (zf * zn) / (zn - zf);
295 pout->u.m[3][3] = 0.0f;
296 return pout;
297 }
298
299 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveLH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
300 {
301 D3DXMatrixIdentity(pout);
302 pout->u.m[0][0] = 2.0f * zn / w;
303 pout->u.m[1][1] = 2.0f * zn / h;
304 pout->u.m[2][2] = zf / (zf - zn);
305 pout->u.m[3][2] = (zn * zf) / (zn - zf);
306 pout->u.m[2][3] = 1.0f;
307 pout->u.m[3][3] = 0.0f;
308 return pout;
309 }
310
311 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterLH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
312 {
313 D3DXMatrixIdentity(pout);
314 pout->u.m[0][0] = 2.0f * zn / (r - l);
315 pout->u.m[1][1] = -2.0f * zn / (b - t);
316 pout->u.m[2][0] = -1.0f - 2.0f * l / (r - l);
317 pout->u.m[2][1] = 1.0f + 2.0f * t / (b - t);
318 pout->u.m[2][2] = - zf / (zn - zf);
319 pout->u.m[3][2] = (zn * zf) / (zn -zf);
320 pout->u.m[2][3] = 1.0f;
321 pout->u.m[3][3] = 0.0f;
322 return pout;
323 }
324
325 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveOffCenterRH(D3DXMATRIX *pout, FLOAT l, FLOAT r, FLOAT b, FLOAT t, FLOAT zn, FLOAT zf)
326 {
327 D3DXMatrixIdentity(pout);
328 pout->u.m[0][0] = 2.0f * zn / (r - l);
329 pout->u.m[1][1] = -2.0f * zn / (b - t);
330 pout->u.m[2][0] = 1.0f + 2.0f * l / (r - l);
331 pout->u.m[2][1] = -1.0f -2.0f * t / (b - t);
332 pout->u.m[2][2] = zf / (zn - zf);
333 pout->u.m[3][2] = (zn * zf) / (zn -zf);
334 pout->u.m[2][3] = -1.0f;
335 pout->u.m[3][3] = 0.0f;
336 return pout;
337 }
338
339 D3DXMATRIX* WINAPI D3DXMatrixPerspectiveRH(D3DXMATRIX *pout, FLOAT w, FLOAT h, FLOAT zn, FLOAT zf)
340 {
341 D3DXMatrixIdentity(pout);
342 pout->u.m[0][0] = 2.0f * zn / w;
343 pout->u.m[1][1] = 2.0f * zn / h;
344 pout->u.m[2][2] = zf / (zn - zf);
345 pout->u.m[3][2] = (zn * zf) / (zn - zf);
346 pout->u.m[2][3] = -1.0f;
347 pout->u.m[3][3] = 0.0f;
348 return pout;
349 }
350
351 D3DXMATRIX* WINAPI D3DXMatrixReflect(D3DXMATRIX *pout, CONST D3DXPLANE *pplane)
352 {
353 D3DXPLANE Nplane;
354
355 D3DXPlaneNormalize(&Nplane, pplane);
356 D3DXMatrixIdentity(pout);
357 pout->u.m[0][0] = 1.0f - 2.0f * Nplane.a * Nplane.a;
358 pout->u.m[0][1] = -2.0f * Nplane.a * Nplane.b;
359 pout->u.m[0][2] = -2.0f * Nplane.a * Nplane.c;
360 pout->u.m[1][0] = -2.0f * Nplane.a * Nplane.b;
361 pout->u.m[1][1] = 1.0f - 2.0f * Nplane.b * Nplane.b;
362 pout->u.m[1][2] = -2.0f * Nplane.b * Nplane.c;
363 pout->u.m[2][0] = -2.0f * Nplane.c * Nplane.a;
364 pout->u.m[2][1] = -2.0f * Nplane.c * Nplane.b;
365 pout->u.m[2][2] = 1.0f - 2.0f * Nplane.c * Nplane.c;
366 pout->u.m[3][0] = -2.0f * Nplane.d * Nplane.a;
367 pout->u.m[3][1] = -2.0f * Nplane.d * Nplane.b;
368 pout->u.m[3][2] = -2.0f * Nplane.d * Nplane.c;
369 return pout;
370 }
371
372 D3DXMATRIX* WINAPI D3DXMatrixRotationAxis(D3DXMATRIX *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
373 {
374 D3DXVECTOR3 v;
375
376 D3DXVec3Normalize(&v,pv);
377 D3DXMatrixIdentity(pout);
378 pout->u.m[0][0] = (1.0f - cos(angle)) * v.x * v.x + cos(angle);
379 pout->u.m[1][0] = (1.0f - cos(angle)) * v.x * v.y - sin(angle) * v.z;
380 pout->u.m[2][0] = (1.0f - cos(angle)) * v.x * v.z + sin(angle) * v.y;
381 pout->u.m[0][1] = (1.0f - cos(angle)) * v.y * v.x + sin(angle) * v.z;
382 pout->u.m[1][1] = (1.0f - cos(angle)) * v.y * v.y + cos(angle);
383 pout->u.m[2][1] = (1.0f - cos(angle)) * v.y * v.z - sin(angle) * v.x;
384 pout->u.m[0][2] = (1.0f - cos(angle)) * v.z * v.x - sin(angle) * v.y;
385 pout->u.m[1][2] = (1.0f - cos(angle)) * v.z * v.y + sin(angle) * v.x;
386 pout->u.m[2][2] = (1.0f - cos(angle)) * v.z * v.z + cos(angle);
387 return pout;
388 }
389
390 D3DXMATRIX* WINAPI D3DXMatrixRotationQuaternion(D3DXMATRIX *pout, CONST D3DXQUATERNION *pq)
391 {
392 D3DXMatrixIdentity(pout);
393 pout->u.m[0][0] = 1.0f - 2.0f * (pq->y * pq->y + pq->z * pq->z);
394 pout->u.m[0][1] = 2.0f * (pq->x *pq->y + pq->z * pq->w);
395 pout->u.m[0][2] = 2.0f * (pq->x * pq->z - pq->y * pq->w);
396 pout->u.m[1][0] = 2.0f * (pq->x * pq->y - pq->z * pq->w);
397 pout->u.m[1][1] = 1.0f - 2.0f * (pq->x * pq->x + pq->z * pq->z);
398 pout->u.m[1][2] = 2.0f * (pq->y *pq->z + pq->x *pq->w);
399 pout->u.m[2][0] = 2.0f * (pq->x * pq->z + pq->y * pq->w);
400 pout->u.m[2][1] = 2.0f * (pq->y *pq->z - pq->x *pq->w);
401 pout->u.m[2][2] = 1.0f - 2.0f * (pq->x * pq->x + pq->y * pq->y);
402 return pout;
403 }
404
405 D3DXMATRIX* WINAPI D3DXMatrixRotationX(D3DXMATRIX *pout, FLOAT angle)
406 {
407 D3DXMatrixIdentity(pout);
408 pout->u.m[1][1] = cos(angle);
409 pout->u.m[2][2] = cos(angle);
410 pout->u.m[1][2] = sin(angle);
411 pout->u.m[2][1] = -sin(angle);
412 return pout;
413 }
414
415 D3DXMATRIX* WINAPI D3DXMatrixRotationY(D3DXMATRIX *pout, FLOAT angle)
416 {
417 D3DXMatrixIdentity(pout);
418 pout->u.m[0][0] = cos(angle);
419 pout->u.m[2][2] = cos(angle);
420 pout->u.m[0][2] = -sin(angle);
421 pout->u.m[2][0] = sin(angle);
422 return pout;
423 }
424
425 D3DXMATRIX* WINAPI D3DXMatrixRotationYawPitchRoll(D3DXMATRIX *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
426 {
427 D3DXMATRIX m, pout1, pout2, pout3;
428
429 D3DXMatrixIdentity(&pout3);
430 D3DXMatrixRotationZ(&m,roll);
431 D3DXMatrixMultiply(&pout2,&pout3,&m);
432 D3DXMatrixRotationX(&m,pitch);
433 D3DXMatrixMultiply(&pout1,&pout2,&m);
434 D3DXMatrixRotationY(&m,yaw);
435 D3DXMatrixMultiply(pout,&pout1,&m);
436 return pout;
437 }
438 D3DXMATRIX* WINAPI D3DXMatrixRotationZ(D3DXMATRIX *pout, FLOAT angle)
439 {
440 D3DXMatrixIdentity(pout);
441 pout->u.m[0][0] = cos(angle);
442 pout->u.m[1][1] = cos(angle);
443 pout->u.m[0][1] = sin(angle);
444 pout->u.m[1][0] = -sin(angle);
445 return pout;
446 }
447
448 D3DXMATRIX* WINAPI D3DXMatrixScaling(D3DXMATRIX *pout, FLOAT sx, FLOAT sy, FLOAT sz)
449 {
450 D3DXMatrixIdentity(pout);
451 pout->u.m[0][0] = sx;
452 pout->u.m[1][1] = sy;
453 pout->u.m[2][2] = sz;
454 return pout;
455 }
456
457 D3DXMATRIX* WINAPI D3DXMatrixShadow(D3DXMATRIX *pout, CONST D3DXVECTOR4 *plight, CONST D3DXPLANE *pplane)
458 {
459 D3DXPLANE Nplane;
460 FLOAT dot;
461
462 D3DXPlaneNormalize(&Nplane, pplane);
463 dot = D3DXPlaneDot(&Nplane, plight);
464 pout->u.m[0][0] = dot - Nplane.a * plight->x;
465 pout->u.m[0][1] = -Nplane.a * plight->y;
466 pout->u.m[0][2] = -Nplane.a * plight->z;
467 pout->u.m[0][3] = -Nplane.a * plight->w;
468 pout->u.m[1][0] = -Nplane.b * plight->x;
469 pout->u.m[1][1] = dot - Nplane.b * plight->y;
470 pout->u.m[1][2] = -Nplane.b * plight->z;
471 pout->u.m[1][3] = -Nplane.b * plight->w;
472 pout->u.m[2][0] = -Nplane.c * plight->x;
473 pout->u.m[2][1] = -Nplane.c * plight->y;
474 pout->u.m[2][2] = dot - Nplane.c * plight->z;
475 pout->u.m[2][3] = -Nplane.c * plight->w;
476 pout->u.m[3][0] = -Nplane.d * plight->x;
477 pout->u.m[3][1] = -Nplane.d * plight->y;
478 pout->u.m[3][2] = -Nplane.d * plight->z;
479 pout->u.m[3][3] = dot - Nplane.d * plight->w;
480 return pout;
481 }
482
483 D3DXMATRIX* WINAPI D3DXMatrixTranslation(D3DXMATRIX *pout, FLOAT x, FLOAT y, FLOAT z)
484 {
485 D3DXMatrixIdentity(pout);
486 pout->u.m[3][0] = x;
487 pout->u.m[3][1] = y;
488 pout->u.m[3][2] = z;
489 return pout;
490 }
491
492 D3DXMATRIX* WINAPI D3DXMatrixTranspose(D3DXMATRIX *pout, CONST D3DXMATRIX *pm)
493 {
494 int i,j;
495
496 for (i=0; i<4; i++)
497 {
498 for (j=0; j<4; j++)
499 {
500 pout->u.m[i][j] = pm->u.m[j][i];
501 }
502 }
503 return pout;
504 }
505
506 /*_________________D3DXPLANE________________*/
507
508 D3DXPLANE* WINAPI D3DXPlaneFromPointNormal(D3DXPLANE *pout, CONST D3DXVECTOR3 *pvpoint, CONST D3DXVECTOR3 *pvnormal)
509 {
510 pout->a = pvnormal->x;
511 pout->b = pvnormal->y;
512 pout->c = pvnormal->z;
513 pout->d = -D3DXVec3Dot(pvpoint, pvnormal);
514 return pout;
515 }
516
517 D3DXPLANE* WINAPI D3DXPlaneFromPoints(D3DXPLANE *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3)
518 {
519 D3DXVECTOR3 edge1, edge2, normal, Nnormal;
520
521 edge1.x = 0.0f; edge1.y = 0.0f; edge1.z = 0.0f;
522 edge2.x = 0.0f; edge2.y = 0.0f; edge2.z = 0.0f;
523 D3DXVec3Subtract(&edge1, pv2, pv1);
524 D3DXVec3Subtract(&edge2, pv3, pv1);
525 D3DXVec3Cross(&normal, &edge1, &edge2);
526 D3DXVec3Normalize(&Nnormal, &normal);
527 D3DXPlaneFromPointNormal(pout, pv1, &Nnormal);
528 return pout;
529 }
530
531 D3DXVECTOR3* WINAPI D3DXPlaneIntersectLine(D3DXVECTOR3 *pout, CONST D3DXPLANE *pp, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2)
532 {
533 D3DXVECTOR3 direction, normal;
534 FLOAT dot, temp;
535
536 normal.x = pp->a;
537 normal.y = pp->b;
538 normal.z = pp->c;
539 direction.x = pv2->x - pv1->x;
540 direction.y = pv2->y - pv1->y;
541 direction.z = pv2->z - pv1->z;
542 dot = D3DXVec3Dot(&normal, &direction);
543 if ( !dot ) return NULL;
544 temp = ( pp->d + D3DXVec3Dot(&normal, pv1) ) / dot;
545 pout->x = pv1->x - temp * direction.x;
546 pout->y = pv1->y - temp * direction.y;
547 pout->z = pv1->z - temp * direction.z;
548 return pout;
549 }
550
551 D3DXPLANE* WINAPI D3DXPlaneNormalize(D3DXPLANE *pout, CONST D3DXPLANE *pp)
552 {
553 FLOAT norm;
554
555 norm = sqrt(pp->a * pp->a + pp->b * pp->b + pp->c * pp->c);
556 if ( norm )
557 {
558 pout->a = pp->a / norm;
559 pout->b = pp->b / norm;
560 pout->c = pp->c / norm;
561 pout->d = pp->d / norm;
562 }
563 else
564 {
565 pout->a = 0.0f;
566 pout->b = 0.0f;
567 pout->c = 0.0f;
568 pout->d = 0.0f;
569 }
570 return pout;
571 }
572
573 D3DXPLANE* WINAPI D3DXPlaneTransform(D3DXPLANE *pout, CONST D3DXPLANE *pplane, CONST D3DXMATRIX *pm)
574 {
575 pout->a = pm->u.m[0][0] * pplane->a + pm->u.m[1][0] * pplane->b + pm->u.m[2][0] * pplane->c + pm->u.m[3][0] * pplane->d;
576 pout->b = pm->u.m[0][1] * pplane->a + pm->u.m[1][1] * pplane->b + pm->u.m[2][1] * pplane->c + pm->u.m[3][1] * pplane->d;
577 pout->c = pm->u.m[0][2] * pplane->a + pm->u.m[1][2] * pplane->b + pm->u.m[2][2] * pplane->c + pm->u.m[3][2] * pplane->d;
578 pout->d = pm->u.m[0][3] * pplane->a + pm->u.m[1][3] * pplane->b + pm->u.m[2][3] * pplane->c + pm->u.m[3][3] * pplane->d;
579 return pout;
580 }
581
582 /*_________________D3DXQUATERNION________________*/
583
584 D3DXQUATERNION* WINAPI D3DXQuaternionBaryCentric(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, FLOAT f, FLOAT g)
585 {
586 D3DXQUATERNION temp1, temp2;
587 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq2, f + g), D3DXQuaternionSlerp(&temp2, pq1, pq3, f+g), g / (f + g));
588 return pout;
589 }
590
591 D3DXQUATERNION* WINAPI D3DXQuaternionInverse(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
592 {
593 D3DXQUATERNION temp;
594 FLOAT norm;
595
596 norm = D3DXQuaternionLengthSq(pq);
597 if ( !norm )
598 {
599 pout->x = 0.0f;
600 pout->y = 0.0f;
601 pout->z = 0.0f;
602 pout->w = 0.0f;
603 }
604 else
605 {
606 D3DXQuaternionConjugate(&temp, pq);
607 pout->x = temp.x / norm;
608 pout->y = temp.y / norm;
609 pout->z = temp.z / norm;
610 pout->w = temp.w / norm;
611 }
612 return pout;
613 }
614
615 D3DXQUATERNION* WINAPI D3DXQuaternionMultiply(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2)
616 {
617 pout->x = pq2->w * pq1->x + pq2->x * pq1->w + pq2->y * pq1->z - pq2->z * pq1->y;
618 pout->y = pq2->w * pq1->y - pq2->x * pq1->z + pq2->y * pq1->w + pq2->z * pq1->x;
619 pout->z = pq2->w * pq1->z + pq2->x * pq1->y - pq2->y * pq1->x + pq2->z * pq1->w;
620 pout->w = pq2->w * pq1->w - pq2->x * pq1->x - pq2->y * pq1->y - pq2->z * pq1->z;
621 return pout;
622 }
623
624 D3DXQUATERNION* WINAPI D3DXQuaternionNormalize(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq)
625 {
626 FLOAT norm;
627
628 norm = D3DXQuaternionLength(pq);
629 if ( !norm )
630 {
631 pout->x = 0.0f;
632 pout->y = 0.0f;
633 pout->z = 0.0f;
634 pout->w = 0.0f;
635 }
636 else
637 {
638 pout->x = pq->x / norm;
639 pout->y = pq->y / norm;
640 pout->z = pq->z / norm;
641 pout->w = pq->w / norm;
642 }
643 return pout;
644 }
645
646 D3DXQUATERNION* WINAPI D3DXQuaternionRotationAxis(D3DXQUATERNION *pout, CONST D3DXVECTOR3 *pv, FLOAT angle)
647 {
648 D3DXVECTOR3 temp;
649
650 D3DXVec3Normalize(&temp, pv);
651 pout->x = sin( angle / 2.0f ) * temp.x;
652 pout->y = sin( angle / 2.0f ) * temp.y;
653 pout->z = sin( angle / 2.0f ) * temp.z;
654 pout->w = cos( angle / 2.0f );
655 return pout;
656 }
657
658 D3DXQUATERNION* WINAPI D3DXQuaternionRotationMatrix(D3DXQUATERNION *pout, CONST D3DXMATRIX *pm)
659 {
660 int i, maxi;
661 FLOAT maxdiag, S, trace;
662
663 trace = pm->u.m[0][0] + pm->u.m[1][1] + pm->u.m[2][2] + 1.0f;
664 if ( trace > 0.0f)
665 {
666 pout->x = ( pm->u.m[1][2] - pm->u.m[2][1] ) / ( 2.0f * sqrt(trace) );
667 pout->y = ( pm->u.m[2][0] - pm->u.m[0][2] ) / ( 2.0f * sqrt(trace) );
668 pout->z = ( pm->u.m[0][1] - pm->u.m[1][0] ) / ( 2.0f * sqrt(trace) );
669 pout->w = sqrt(trace) / 2.0f;
670 return pout;
671 }
672 maxi = 0;
673 maxdiag = pm->u.m[0][0];
674 for (i=1; i<3; i++)
675 {
676 if ( pm->u.m[i][i] > maxdiag )
677 {
678 maxi = i;
679 maxdiag = pm->u.m[i][i];
680 }
681 }
682 switch( maxi )
683 {
684 case 0:
685 S = 2.0f * sqrt(1.0f + pm->u.m[0][0] - pm->u.m[1][1] - pm->u.m[2][2]);
686 pout->x = 0.25f * S;
687 pout->y = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
688 pout->z = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
689 pout->w = ( pm->u.m[1][2] - pm->u.m[2][1] ) / S;
690 break;
691 case 1:
692 S = 2.0f * sqrt(1.0f + pm->u.m[1][1] - pm->u.m[0][0] - pm->u.m[2][2]);
693 pout->x = ( pm->u.m[0][1] + pm->u.m[1][0] ) / S;
694 pout->y = 0.25f * S;
695 pout->z = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
696 pout->w = ( pm->u.m[2][0] - pm->u.m[0][2] ) / S;
697 break;
698 case 2:
699 S = 2.0f * sqrt(1.0f + pm->u.m[2][2] - pm->u.m[0][0] - pm->u.m[1][1]);
700 pout->x = ( pm->u.m[0][2] + pm->u.m[2][0] ) / S;
701 pout->y = ( pm->u.m[1][2] + pm->u.m[2][1] ) / S;
702 pout->z = 0.25f * S;
703 pout->w = ( pm->u.m[0][1] - pm->u.m[1][0] ) / S;
704 break;
705 }
706 return pout;
707 }
708
709 D3DXQUATERNION* WINAPI D3DXQuaternionRotationYawPitchRoll(D3DXQUATERNION *pout, FLOAT yaw, FLOAT pitch, FLOAT roll)
710 {
711 pout->x = sin( yaw / 2.0f) * cos(pitch / 2.0f) * sin(roll / 2.0f) + cos(yaw / 2.0f) * sin(pitch / 2.0f) * cos(roll / 2.0f);
712 pout->y = sin( yaw / 2.0f) * cos(pitch / 2.0f) * cos(roll / 2.0f) - cos(yaw / 2.0f) * sin(pitch / 2.0f) * sin(roll / 2.0f);
713 pout->z = cos(yaw / 2.0f) * cos(pitch / 2.0f) * sin(roll / 2.0f) - sin( yaw / 2.0f) * sin(pitch / 2.0f) * cos(roll / 2.0f);
714 pout->w = cos( yaw / 2.0f) * cos(pitch / 2.0f) * cos(roll / 2.0f) + sin(yaw / 2.0f) * sin(pitch / 2.0f) * sin(roll / 2.0f);
715 return pout;
716 }
717
718 D3DXQUATERNION* WINAPI D3DXQuaternionSlerp(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, FLOAT t)
719 {
720 FLOAT dot, epsilon;
721
722 epsilon = 1.0f;
723 dot = D3DXQuaternionDot(pq1, pq2);
724 if ( dot < 0.0f) epsilon = -1.0f;
725 pout->x = (1.0f - t) * pq1->x + epsilon * t * pq2->x;
726 pout->y = (1.0f - t) * pq1->y + epsilon * t * pq2->y;
727 pout->z = (1.0f - t) * pq1->z + epsilon * t * pq2->z;
728 pout->w = (1.0f - t) * pq1->w + epsilon * t * pq2->w;
729 return pout;
730 }
731
732 D3DXQUATERNION* WINAPI D3DXQuaternionSquad(D3DXQUATERNION *pout, CONST D3DXQUATERNION *pq1, CONST D3DXQUATERNION *pq2, CONST D3DXQUATERNION *pq3, CONST D3DXQUATERNION *pq4, FLOAT t)
733 {
734 D3DXQUATERNION temp1, temp2;
735
736 D3DXQuaternionSlerp(pout, D3DXQuaternionSlerp(&temp1, pq1, pq4, t), D3DXQuaternionSlerp(&temp2, pq2, pq3, t), 2.0f * t * (1.0f - t));
737 return pout;
738 }
739
740 void WINAPI D3DXQuaternionToAxisAngle(CONST D3DXQUATERNION *pq, D3DXVECTOR3 *paxis, FLOAT *pangle)
741 {
742 FLOAT norm;
743
744 *pangle = 0.0f;
745 norm = D3DXQuaternionLength(pq);
746 if ( norm )
747 {
748 paxis->x = pq->x / norm;
749 paxis->y = pq->y / norm;
750 paxis->z = pq->z / norm;
751 if ( fabs( pq->w ) <= 1.0f ) *pangle = 2.0f * acos(pq->w);
752 }
753 else
754 {
755 paxis->x = 1.0f;
756 paxis->y = 0.0f;
757 paxis->z = 0.0f;
758 }
759 }
760
761 /*_________________D3DXVec2_____________________*/
762
763 D3DXVECTOR2* WINAPI D3DXVec2BaryCentric(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT f, FLOAT g)
764 {
765 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
766 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
767 return pout;
768 }
769
770 D3DXVECTOR2* WINAPI D3DXVec2CatmullRom(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv0, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pv3, FLOAT s)
771 {
772 pout->x = 0.5f * (2.0f * pv1->x + (pv2->x - pv0->x) *s + (2.0f *pv0->x - 5.0f * pv1->x + 4.0f * pv2->x - pv3->x) * s * s + (pv3->x -3.0f * pv2->x + 3.0f * pv1->x - pv0->x) * s * s * s);
773 pout->y = 0.5f * (2.0f * pv1->y + (pv2->y - pv0->y) *s + (2.0f *pv0->y - 5.0f * pv1->y + 4.0f * pv2->y - pv3->y) * s * s + (pv3->y -3.0f * pv2->y + 3.0f * pv1->y - pv0->y) * s * s * s);
774 return pout;
775 }
776
777 D3DXVECTOR2* WINAPI D3DXVec2Hermite(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv1, CONST D3DXVECTOR2 *pt1, CONST D3DXVECTOR2 *pv2, CONST D3DXVECTOR2 *pt2, FLOAT s)
778 {
779 FLOAT h1, h2, h3, h4;
780
781 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
782 h2 = s * s * s - 2.0f * s * s + s;
783 h3 = -2.0f * s * s * s + 3.0f * s * s;
784 h4 = s * s * s - s * s;
785
786 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
787 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
788 return pout;
789 }
790
791 D3DXVECTOR2* WINAPI D3DXVec2Normalize(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv)
792 {
793 FLOAT norm;
794
795 norm = D3DXVec2Length(pv);
796 if ( !norm )
797 {
798 pout->x = 0.0f;
799 pout->y = 0.0f;
800 }
801 else
802 {
803 pout->x = pv->x / norm;
804 pout->y = pv->y / norm;
805 }
806 return pout;
807 }
808
809 D3DXVECTOR4* WINAPI D3DXVec2Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
810 {
811 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0];
812 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1];
813 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[3][2];
814 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
815 return pout;
816 }
817
818 D3DXVECTOR2* WINAPI D3DXVec2TransformCoord(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
819 {
820 FLOAT norm;
821
822 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[3][3];
823 if ( norm )
824 {
825 pout->x = (pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[3][0]) / norm;
826 pout->y = (pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[3][1]) / norm;
827 }
828 else
829 {
830 pout->x = 0.0f;
831 pout->y = 0.0f;
832 }
833 return pout;
834 }
835
836 D3DXVECTOR2* WINAPI D3DXVec2TransformNormal(D3DXVECTOR2 *pout, CONST D3DXVECTOR2 *pv, CONST D3DXMATRIX *pm)
837 {
838 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y;
839 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y;
840 return pout;
841 }
842
843 /*_________________D3DXVec3_____________________*/
844
845 D3DXVECTOR3* WINAPI D3DXVec3BaryCentric(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT f, FLOAT g)
846 {
847 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
848 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
849 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
850 return pout;
851 }
852
853 D3DXVECTOR3* WINAPI D3DXVec3CatmullRom( D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv0, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pv3, FLOAT s)
854 {
855 pout->x = 0.5f * (2.0f * pv1->x + (pv2->x - pv0->x) *s + (2.0f *pv0->x - 5.0f * pv1->x + 4.0f * pv2->x - pv3->x) * s * s + (pv3->x -3.0f * pv2->x + 3.0f * pv1->x - pv0->x) * s * s * s);
856 pout->y = 0.5f * (2.0f * pv1->y + (pv2->y - pv0->y) *s + (2.0f *pv0->y - 5.0f * pv1->y + 4.0f * pv2->y - pv3->y) * s * s + (pv3->y -3.0f * pv2->y + 3.0f * pv1->y - pv0->y) * s * s * s);
857 pout->z = 0.5f * (2.0f * pv1->z + (pv2->z - pv0->z) *s + (2.0f *pv0->z - 5.0f * pv1->z + 4.0f * pv2->z - pv3->z) * s * s + (pv3->z -3.0f * pv2->z + 3.0f * pv1->z - pv0->z) * s * s * s);
858 return pout;
859 }
860
861 D3DXVECTOR3* WINAPI D3DXVec3Hermite(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv1, CONST D3DXVECTOR3 *pt1, CONST D3DXVECTOR3 *pv2, CONST D3DXVECTOR3 *pt2, FLOAT s)
862 {
863 FLOAT h1, h2, h3, h4;
864
865 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
866 h2 = s * s * s - 2.0f * s * s + s;
867 h3 = -2.0f * s * s * s + 3.0f * s * s;
868 h4 = s * s * s - s * s;
869
870 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
871 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
872 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
873 return pout;
874 }
875
876 D3DXVECTOR3* WINAPI D3DXVec3Normalize(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv)
877 {
878 FLOAT norm;
879
880 norm = D3DXVec3Length(pv);
881 if ( !norm )
882 {
883 pout->x = 0.0f;
884 pout->y = 0.0f;
885 pout->z = 0.0f;
886 }
887 else
888 {
889 pout->x = pv->x / norm;
890 pout->y = pv->y / norm;
891 pout->z = pv->z / norm;
892 }
893 return pout;
894 }
895
896 D3DXVECTOR3* WINAPI D3DXVec3Project(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT8 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
897 {
898 D3DXMATRIX m1, m2;
899 D3DXVECTOR3 vec;
900
901 D3DXMatrixMultiply(&m1, pworld, pview);
902 D3DXMatrixMultiply(&m2, &m1, pprojection);
903 D3DXVec3TransformCoord(&vec, pv, &m2);
904 pout->x = pviewport->X + ( 1.0f + vec.x ) * pviewport->Width / 2.0f;
905 pout->y = pviewport->Y + ( 1.0f - vec.y ) * pviewport->Height / 2.0f;
906 pout->z = pviewport->MinZ + vec.z * ( pviewport->MaxZ - pviewport->MinZ );
907 return pout;
908 }
909
910 D3DXVECTOR4* WINAPI D3DXVec3Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
911 {
912 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z + pm->u.m[3][0];
913 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z + pm->u.m[3][1];
914 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z + pm->u.m[3][2];
915 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[2][3] * pv->z + pm->u.m[3][3];
916 return pout;
917 }
918
919 D3DXVECTOR3* WINAPI D3DXVec3TransformCoord(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
920 {
921 FLOAT norm;
922
923 norm = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[2][3] *pv->z + pm->u.m[3][3];
924
925 if ( norm )
926 {
927 pout->x = (pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z + pm->u.m[3][0]) / norm;
928 pout->y = (pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z + pm->u.m[3][1]) / norm;
929 pout->z = (pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z + pm->u.m[3][2]) / norm;
930 }
931 else
932 {
933 pout->x = 0.0f;
934 pout->y = 0.0f;
935 pout->z = 0.0f;
936 }
937 return pout;
938 }
939
940 D3DXVECTOR3* WINAPI D3DXVec3TransformNormal(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DXMATRIX *pm)
941 {
942 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z;
943 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z;
944 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z;
945 return pout;
946
947 }
948
949 D3DXVECTOR3* WINAPI D3DXVec3Unproject(D3DXVECTOR3 *pout, CONST D3DXVECTOR3 *pv, CONST D3DVIEWPORT8 *pviewport, CONST D3DXMATRIX *pprojection, CONST D3DXMATRIX *pview, CONST D3DXMATRIX *pworld)
950 {
951 D3DXMATRIX m1, m2, m3;
952 D3DXVECTOR3 vec;
953
954 D3DXMatrixMultiply(&m1, pworld, pview);
955 D3DXMatrixMultiply(&m2, &m1, pprojection);
956 D3DXMatrixInverse(&m3, NULL, &m2);
957 vec.x = 2.0f * ( pv->x - pviewport->X ) / pviewport->Width - 1.0f;
958 vec.y = 1.0f - 2.0f * ( pv->y - pviewport->Y ) / pviewport->Height;
959 vec.z = ( pv->z - pviewport->MinZ) / ( pviewport->MaxZ - pviewport->MinZ );
960 D3DXVec3TransformCoord(pout, &vec, &m3);
961 return pout;
962 }
963
964 /*_________________D3DXVec4_____________________*/
965
966 D3DXVECTOR4* WINAPI D3DXVec4BaryCentric(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT f, FLOAT g)
967 {
968 pout->x = (1.0f-f-g) * (pv1->x) + f * (pv2->x) + g * (pv3->x);
969 pout->y = (1.0f-f-g) * (pv1->y) + f * (pv2->y) + g * (pv3->y);
970 pout->z = (1.0f-f-g) * (pv1->z) + f * (pv2->z) + g * (pv3->z);
971 pout->w = (1.0f-f-g) * (pv1->w) + f * (pv2->w) + g * (pv3->w);
972 return pout;
973 }
974
975 D3DXVECTOR4* WINAPI D3DXVec4CatmullRom(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv0, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3, FLOAT s)
976 {
977 pout->x = 0.5f * (2.0f * pv1->x + (pv2->x - pv0->x) *s + (2.0f *pv0->x - 5.0f * pv1->x + 4.0f * pv2->x - pv3->x) * s * s + (pv3->x -3.0f * pv2->x + 3.0f * pv1->x - pv0->x) * s * s * s);
978 pout->y = 0.5f * (2.0f * pv1->y + (pv2->y - pv0->y) *s + (2.0f *pv0->y - 5.0f * pv1->y + 4.0f * pv2->y - pv3->y) * s * s + (pv3->y -3.0f * pv2->y + 3.0f * pv1->y - pv0->y) * s * s * s);
979 pout->z = 0.5f * (2.0f * pv1->z + (pv2->z - pv0->z) *s + (2.0f *pv0->z - 5.0f * pv1->z + 4.0f * pv2->z - pv3->z) * s * s + (pv3->z -3.0f * pv2->z + 3.0f * pv1->z - pv0->z) * s * s * s);
980 pout->w = 0.5f * (2.0f * pv1->w + (pv2->w - pv0->w) *s + (2.0f *pv0->w - 5.0f * pv1->w + 4.0f * pv2->w - pv3->w) * s * s + (pv3->w -3.0f * pv2->w + 3.0f * pv1->w - pv0->w) * s * s * s);
981 return pout;
982 }
983
984 D3DXVECTOR4* WINAPI D3DXVec4Cross(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pv3)
985 {
986 pout->x = pv1->y * (pv2->z * pv3->w - pv3->z * pv2->w) - pv1->z * (pv2->y * pv3->w - pv3->y * pv2->w) + pv1->w * (pv2->y * pv3->z - pv2->z *pv3->y);
987 pout->y = -(pv1->x * (pv2->z * pv3->w - pv3->z * pv2->w) - pv1->z * (pv2->x * pv3->w - pv3->x * pv2->w) + pv1->w * (pv2->x * pv3->z - pv3->x * pv2->z));
988 pout->z = pv1->x * (pv2->y * pv3->w - pv3->y * pv2->w) - pv1->y * (pv2->x *pv3->w - pv3->x * pv2->w) + pv1->w * (pv2->x * pv3->y - pv3->x * pv2->y);
989 pout->w = -(pv1->x * (pv2->y * pv3->z - pv3->y * pv2->z) - pv1->y * (pv2->x * pv3->z - pv3->x *pv2->z) + pv1->z * (pv2->x * pv3->y - pv3->x * pv2->y));
990 return pout;
991 }
992
993 D3DXVECTOR4* WINAPI D3DXVec4Hermite(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv1, CONST D3DXVECTOR4 *pt1, CONST D3DXVECTOR4 *pv2, CONST D3DXVECTOR4 *pt2, FLOAT s)
994 {
995 FLOAT h1, h2, h3, h4;
996
997 h1 = 2.0f * s * s * s - 3.0f * s * s + 1.0f;
998 h2 = s * s * s - 2.0f * s * s + s;
999 h3 = -2.0f * s * s * s + 3.0f * s * s;
1000 h4 = s * s * s - s * s;
1002 pout->x = h1 * (pv1->x) + h2 * (pt1->x) + h3 * (pv2->x) + h4 * (pt2->x);
1003 pout->y = h1 * (pv1->y) + h2 * (pt1->y) + h3 * (pv2->y) + h4 * (pt2->y);
1004 pout->z = h1 * (pv1->z) + h2 * (pt1->z) + h3 * (pv2->z) + h4 * (pt2->z);
1005 pout->w = h1 * (pv1->w) + h2 * (pt1->w) + h3 * (pv2->w) + h4 * (pt2->w);
1006 return pout;
1007 }
1009 D3DXVECTOR4* WINAPI D3DXVec4Normalize(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv)
1010 {
1011 FLOAT norm;
1013 norm = D3DXVec4Length(pv);
1014 if ( !norm )
1015 {
1016 pout->x = 0.0f;
1017 pout->y = 0.0f;
1018 pout->z = 0.0f;
1019 pout->w = 0.0f;
1020 }
1021 else
1022 {
1023 pout->x = pv->x / norm;
1024 pout->y = pv->y / norm;
1025 pout->z = pv->z / norm;
1026 pout->w = pv->w / norm;
1027 }
1028 return pout;
1029 }
1031 D3DXVECTOR4* WINAPI D3DXVec4Transform(D3DXVECTOR4 *pout, CONST D3DXVECTOR4 *pv, CONST D3DXMATRIX *pm)
1032 {
1033 pout->x = pm->u.m[0][0] * pv->x + pm->u.m[1][0] * pv->y + pm->u.m[2][0] * pv->z + pm->u.m[3][0] * pv->w;
1034 pout->y = pm->u.m[0][1] * pv->x + pm->u.m[1][1] * pv->y + pm->u.m[2][1] * pv->z + pm->u.m[3][1] * pv->w;
1035 pout->z = pm->u.m[0][2] * pv->x + pm->u.m[1][2] * pv->y + pm->u.m[2][2] * pv->z + pm->u.m[3][2] * pv->w;
1036 pout->w = pm->u.m[0][3] * pv->x + pm->u.m[1][3] * pv->y + pm->u.m[2][3] * pv->z + pm->u.m[3][3] * pv->w;
1037 return pout;
1038 }

标签: 计算机图形学 算法

发表评论:

Powered by emlog | |

浙ICP备17021512号 |浙公网安备 33010602008237号