mcicda: Exclude unused headers.
[wine] / dlls / d3drm / math.c
1 /*
2  * Copyright 2007 David Adam
3  * Copyright 2007 Vijay Kiran Kamuju
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #define NONAMELESSUNION
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <assert.h>
26 #include <math.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "d3drmdef.h"
32
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(d3drm);
36
37 /* Product of 2 quaternions */
38 LPD3DRMQUATERNION WINAPI D3DRMQuaternionMultiply(LPD3DRMQUATERNION q, LPD3DRMQUATERNION a, LPD3DRMQUATERNION b)
39 {
40     D3DVECTOR cross_product;
41     D3DRMVectorCrossProduct(&cross_product, &a->v, &b->v);
42     q->s = a->s * b->s - D3DRMVectorDotProduct(&a->v, &b->v);
43     q->v.u1.x = a->s * b->v.u1.x + b->s * a->v.u1.x + cross_product.u1.x;
44     q->v.u2.y = a->s * b->v.u2.y + b->s * a->v.u2.y + cross_product.u2.y;
45     q->v.u3.z = a->s * b->v.u3.z + b->s * a->v.u3.z + cross_product.u3.z;
46     return q;
47 }
48
49 /* Matrix for the Rotation that a unit quaternion represents */
50 void WINAPI D3DRMMatrixFromQuaternion(D3DRMMATRIX4D m, LPD3DRMQUATERNION q)
51 {
52     D3DVALUE w,x,y,z;
53     w = q->s;
54     x = q->v.u1.x;
55     y = q->v.u2.y;
56     z = q->v.u3.z;
57     m[0][0] = 1.0-2.0*(y*y+z*z);
58     m[1][1] = 1.0-2.0*(x*x+z*z);
59     m[2][2] = 1.0-2.0*(x*x+y*y);
60     m[1][0] = 2.0*(x*y+z*w);
61     m[0][1] = 2.0*(x*y-z*w);
62     m[2][0] = 2.0*(x*z-y*w);
63     m[0][2] = 2.0*(x*z+y*w);
64     m[2][1] = 2.0*(y*z+x*w);
65     m[1][2] = 2.0*(y*z-x*w);
66     m[3][0] = 0.0;
67     m[3][1] = 0.0;
68     m[3][2] = 0.0;
69     m[0][3] = 0.0;
70     m[1][3] = 0.0;
71     m[2][3] = 0.0;
72     m[3][3] = 1.0;
73 }
74
75 /* Return a unit quaternion that represents a rotation of an angle around an axis */
76 LPD3DRMQUATERNION WINAPI D3DRMQuaternionFromRotation(LPD3DRMQUATERNION q, LPD3DVECTOR v, D3DVALUE theta)
77 {
78     q->s = cos(theta/2.0);
79     D3DRMVectorScale(&q->v, D3DRMVectorNormalize(v), sin(theta/2.0));
80     return q;
81 }
82
83 /* Interpolation between two quaternions */
84 LPD3DRMQUATERNION WINAPI D3DRMQuaternionSlerp(LPD3DRMQUATERNION q, LPD3DRMQUATERNION a, LPD3DRMQUATERNION b, D3DVALUE alpha)
85 {
86     D3DVALUE epsilon=1.0;
87     D3DVECTOR sca1,sca2;
88     if (a->s * b->s + D3DRMVectorDotProduct(&a->v, &b->v) < 0.0) epsilon = -1.0;
89     q->s = (1.0 - alpha) * a->s + epsilon * alpha * b->s;
90     D3DRMVectorAdd(&q->v, D3DRMVectorScale(&sca1, &a->v, 1.0 - alpha),
91                    D3DRMVectorScale(&sca2, &b->v, epsilon * alpha));
92     return q;
93 }
94
95 /* Add Two Vectors */
96 LPD3DVECTOR WINAPI D3DRMVectorAdd(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
97 {
98     d->u1.x=s1->u1.x + s2->u1.x;
99     d->u2.y=s1->u2.y + s2->u2.y;
100     d->u3.z=s1->u3.z + s2->u3.z;
101     return d;
102 }
103
104 /* Subtract Two Vectors */
105 LPD3DVECTOR WINAPI D3DRMVectorSubtract(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
106 {
107     d->u1.x=s1->u1.x - s2->u1.x;
108     d->u2.y=s1->u2.y - s2->u2.y;
109     d->u3.z=s1->u3.z - s2->u3.z;
110     return d;
111 }
112
113 /* Cross Product of Two Vectors */
114 LPD3DVECTOR WINAPI D3DRMVectorCrossProduct(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
115 {
116     d->u1.x=s1->u2.y * s2->u3.z - s1->u3.z * s2->u2.y;
117     d->u2.y=s1->u3.z * s2->u1.x - s1->u1.x * s2->u3.z;
118     d->u3.z=s1->u1.x * s2->u2.y - s1->u2.y * s2->u1.x;
119     return d;
120 }
121
122 /* Dot Product of Two vectors */
123 D3DVALUE WINAPI D3DRMVectorDotProduct(LPD3DVECTOR s1, LPD3DVECTOR s2)
124 {
125     D3DVALUE dot_product;
126     dot_product=s1->u1.x * s2->u1.x + s1->u2.y * s2->u2.y + s1->u3.z * s2->u3.z;
127     return dot_product;
128 }
129
130 /* Norm of a vector */
131 D3DVALUE WINAPI D3DRMVectorModulus(LPD3DVECTOR v)
132 {
133     D3DVALUE result;
134     result=sqrt(v->u1.x * v->u1.x + v->u2.y * v->u2.y + v->u3.z * v->u3.z);
135     return result;
136 }
137
138 /* Normalize a vector.  Returns (1,0,0) if INPUT is the NULL vector. */
139 LPD3DVECTOR WINAPI D3DRMVectorNormalize(LPD3DVECTOR u)
140 {
141     D3DVALUE modulus = D3DRMVectorModulus(u);
142     if(modulus)
143     {
144         D3DRMVectorScale(u,u,1.0/modulus);
145     }
146     else
147     {
148         u->u1.x=1.0;
149         u->u2.y=0.0;
150         u->u3.z=0.0;
151     }
152     return u;
153 }
154
155 /* Returns a random unit vector */
156 LPD3DVECTOR WINAPI D3DRMVectorRandom(LPD3DVECTOR d)
157 {
158     d->u1.x = rand();
159     d->u2.y = rand();
160     d->u3.z = rand();
161     D3DRMVectorNormalize(d);
162     return d;
163 }
164
165 /* Reflection of a vector on a surface */
166 LPD3DVECTOR WINAPI D3DRMVectorReflect(LPD3DVECTOR r, LPD3DVECTOR ray, LPD3DVECTOR norm)
167 {
168     D3DVECTOR sca;
169     D3DRMVectorSubtract(r, D3DRMVectorScale(&sca, norm, 2.0*D3DRMVectorDotProduct(ray,norm)), ray);
170     return r;
171 }
172
173 /* Rotation of a vector */
174 LPD3DVECTOR WINAPI D3DRMVectorRotate(LPD3DVECTOR r, LPD3DVECTOR v, LPD3DVECTOR axis, D3DVALUE theta)
175 {
176     D3DRMQUATERNION quaternion,quaternion1, quaternion2, quaternion3, resultq;
177     D3DVECTOR NORM;
178
179     quaternion1.s = cos(theta*.5);
180     quaternion2.s = cos(theta*.5);
181     NORM = *D3DRMVectorNormalize(axis);
182     D3DRMVectorScale(&quaternion1.v, &NORM, sin(theta * .5));
183     D3DRMVectorScale(&quaternion2.v, &NORM, -sin(theta * .5));
184     quaternion3.s = 0.0;
185     quaternion3.v = *v;
186     D3DRMQuaternionMultiply(&quaternion, &quaternion1, &quaternion3);
187     D3DRMQuaternionMultiply(&resultq, &quaternion, &quaternion2);
188     *r = *D3DRMVectorNormalize(&resultq.v);
189     return r;
190 }
191
192 /* Scale a vector */
193 LPD3DVECTOR WINAPI D3DRMVectorScale(LPD3DVECTOR d, LPD3DVECTOR s, D3DVALUE factor)
194 {
195     d->u1.x=factor * s->u1.x;
196     d->u2.y=factor * s->u2.y;
197     d->u3.z=factor * s->u3.z;
198     return d;
199 }