d3drm: Implement D3DRMVectorReflect.
[wine/dibdrv.git] / dlls / d3drm / math.c
blobb0de7d61f73938bed867e2faf29492bf2f563a56
1 /*
2 * Copyright 2007 David Adam
3 * Copyright 2007 Vijay Kiran Kamuju
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <assert.h>
24 #include <math.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "d3drmdef.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3drm);
35 /* Add Two Vectors */
36 LPD3DVECTOR WINAPI D3DRMVectorAdd(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
38 d->x=s1->x + s2->x;
39 d->y=s1->y + s2->y;
40 d->z=s1->z + s2->z;
41 return d;
44 /* Subtract Two Vectors */
45 LPD3DVECTOR WINAPI D3DRMVectorSubtract(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
47 d->x=s1->x - s2->x;
48 d->y=s1->y - s2->y;
49 d->z=s1->z - s2->z;
50 return d;
53 /* Cross Product of Two Vectors */
54 LPD3DVECTOR WINAPI D3DRMVectorCrossProduct(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
56 d->x=s1->y * s2->z - s1->z * s2->y;
57 d->y=s1->z * s2->x - s1->x * s2->z;
58 d->z=s1->x * s2->y - s1->y * s2->x;
59 return d;
62 /* Dot Product of Two vectors */
63 D3DVALUE WINAPI D3DRMVectorDotProduct(LPD3DVECTOR s1, LPD3DVECTOR s2)
65 D3DVALUE dot_product;
66 dot_product=s1->x * s2->x + s1->y * s2->y + s1->z * s2->z;
67 return dot_product;
70 /* Norm of a vector */
71 D3DVALUE WINAPI D3DRMVectorModulus(LPD3DVECTOR v)
73 D3DVALUE result;
74 result=sqrt(v->x * v->x + v->y * v->y + v->z * v->z);
75 return result;
78 /* Normalize a vector. Returns (1,0,0) if INPUT is the NULL vector. */
79 LPD3DVECTOR WINAPI D3DRMVectorNormalize(LPD3DVECTOR u)
81 D3DVALUE modulus = D3DRMVectorModulus(u);
82 if(modulus)
84 D3DRMVectorScale(u,u,1.0/modulus);
86 else
88 u->x=1.0;
89 u->y=0.0;
90 u->z=0.0;
92 return u;
95 /* Returns a random unit vector */
96 LPD3DVECTOR WINAPI D3DRMVectorRandom(LPD3DVECTOR d)
98 d->x = rand();
99 d->y = rand();
100 d->z = rand();
101 D3DRMVectorNormalize(d);
102 return d;
105 /* Reflection of a vector on a surface */
106 LPD3DVECTOR WINAPI D3DRMVectorReflect(LPD3DVECTOR r, LPD3DVECTOR ray, LPD3DVECTOR norm)
108 D3DVECTOR sca;
109 D3DRMVectorSubtract(r, D3DRMVectorScale(&sca, norm, 2.0*D3DRMVectorDotProduct(ray,norm)), ray);
110 return r;
113 /* Scale a vector */
114 LPD3DVECTOR WINAPI D3DRMVectorScale(LPD3DVECTOR d, LPD3DVECTOR s, D3DVALUE factor)
116 d->x=factor * s->x;
117 d->y=factor * s->y;
118 d->z=factor * s->z;
119 return d;