94d87710f6d4180bb0c9c6aeddba3a69329ab105
[reactos.git] / dll / directx / wine / 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 #include "d3drm_private.h"
21
22 #include <math.h>
23
24 /* Create a RGB color from its components */
25 D3DCOLOR WINAPI D3DRMCreateColorRGB(D3DVALUE red, D3DVALUE green, D3DVALUE blue)
26 {
27 return D3DRMCreateColorRGBA(red, green, blue, 1.0f);
28 }
29 /* Create a RGBA color from its components */
30 D3DCOLOR WINAPI D3DRMCreateColorRGBA(D3DVALUE red, D3DVALUE green, D3DVALUE blue, D3DVALUE alpha)
31 {
32 D3DCOLOR color;
33
34 d3drm_set_color(&color, red, green, blue, alpha);
35
36 return color;
37 }
38
39 /* Determine the alpha part of a color */
40 D3DVALUE WINAPI D3DRMColorGetAlpha(D3DCOLOR color)
41 {
42 return (RGBA_GETALPHA(color)/255.0);
43 }
44
45 /* Determine the blue part of a color */
46 D3DVALUE WINAPI D3DRMColorGetBlue(D3DCOLOR color)
47 {
48 return (RGBA_GETBLUE(color)/255.0);
49 }
50
51 /* Determine the green part of a color */
52 D3DVALUE WINAPI D3DRMColorGetGreen(D3DCOLOR color)
53 {
54 return (RGBA_GETGREEN(color)/255.0);
55 }
56
57 /* Determine the red part of a color */
58 D3DVALUE WINAPI D3DRMColorGetRed(D3DCOLOR color)
59 {
60 return (RGBA_GETRED(color)/255.0);
61 }
62
63 /* Product of 2 quaternions */
64 D3DRMQUATERNION * WINAPI D3DRMQuaternionMultiply(D3DRMQUATERNION *q, D3DRMQUATERNION *a, D3DRMQUATERNION *b)
65 {
66 D3DRMQUATERNION temp;
67 D3DVECTOR cross_product;
68
69 D3DRMVectorCrossProduct(&cross_product, &a->v, &b->v);
70 temp.s = a->s * b->s - D3DRMVectorDotProduct(&a->v, &b->v);
71 temp.v.u1.x = a->s * b->v.u1.x + b->s * a->v.u1.x + cross_product.u1.x;
72 temp.v.u2.y = a->s * b->v.u2.y + b->s * a->v.u2.y + cross_product.u2.y;
73 temp.v.u3.z = a->s * b->v.u3.z + b->s * a->v.u3.z + cross_product.u3.z;
74
75 *q = temp;
76 return q;
77 }
78
79 /* Matrix for the Rotation that a unit quaternion represents */
80 void WINAPI D3DRMMatrixFromQuaternion(D3DRMMATRIX4D m, D3DRMQUATERNION *q)
81 {
82 D3DVALUE w,x,y,z;
83 w = q->s;
84 x = q->v.u1.x;
85 y = q->v.u2.y;
86 z = q->v.u3.z;
87 m[0][0] = 1.0-2.0*(y*y+z*z);
88 m[1][1] = 1.0-2.0*(x*x+z*z);
89 m[2][2] = 1.0-2.0*(x*x+y*y);
90 m[1][0] = 2.0*(x*y+z*w);
91 m[0][1] = 2.0*(x*y-z*w);
92 m[2][0] = 2.0*(x*z-y*w);
93 m[0][2] = 2.0*(x*z+y*w);
94 m[2][1] = 2.0*(y*z+x*w);
95 m[1][2] = 2.0*(y*z-x*w);
96 m[3][0] = 0.0;
97 m[3][1] = 0.0;
98 m[3][2] = 0.0;
99 m[0][3] = 0.0;
100 m[1][3] = 0.0;
101 m[2][3] = 0.0;
102 m[3][3] = 1.0;
103 }
104
105 /* Return a unit quaternion that represents a rotation of an angle around an axis */
106 D3DRMQUATERNION * WINAPI D3DRMQuaternionFromRotation(D3DRMQUATERNION *q, D3DVECTOR *v, D3DVALUE theta)
107 {
108 q->s = cos(theta/2.0);
109 D3DRMVectorScale(&q->v, D3DRMVectorNormalize(v), sin(theta/2.0));
110 return q;
111 }
112
113 /* Interpolation between two quaternions */
114 D3DRMQUATERNION * WINAPI D3DRMQuaternionSlerp(D3DRMQUATERNION *q,
115 D3DRMQUATERNION *a, D3DRMQUATERNION *b, D3DVALUE alpha)
116 {
117 D3DVALUE dot, epsilon, temp, theta, u;
118 D3DVECTOR v1, v2;
119
120 dot = a->s * b->s + D3DRMVectorDotProduct(&a->v, &b->v);
121 epsilon = 1.0f;
122 temp = 1.0f - alpha;
123 u = alpha;
124 if (dot < 0.0)
125 {
126 epsilon = -1.0;
127 dot = -dot;
128 }
129 if( 1.0f - dot > 0.001f )
130 {
131 theta = acos(dot);
132 temp = sin(theta * temp) / sin(theta);
133 u = sin(theta * alpha) / sin(theta);
134 }
135 q->s = temp * a->s + epsilon * u * b->s;
136 D3DRMVectorScale(&v1, &a->v, temp);
137 D3DRMVectorScale(&v2, &b->v, epsilon * u);
138 D3DRMVectorAdd(&q->v, &v1, &v2);
139 return q;
140 }
141
142 /* Add Two Vectors */
143 D3DVECTOR * WINAPI D3DRMVectorAdd(D3DVECTOR *d, D3DVECTOR *s1, D3DVECTOR *s2)
144 {
145 D3DVECTOR temp;
146
147 temp.u1.x=s1->u1.x + s2->u1.x;
148 temp.u2.y=s1->u2.y + s2->u2.y;
149 temp.u3.z=s1->u3.z + s2->u3.z;
150
151 *d = temp;
152 return d;
153 }
154
155 /* Subtract Two Vectors */
156 D3DVECTOR * WINAPI D3DRMVectorSubtract(D3DVECTOR *d, D3DVECTOR *s1, D3DVECTOR *s2)
157 {
158 D3DVECTOR temp;
159
160 temp.u1.x=s1->u1.x - s2->u1.x;
161 temp.u2.y=s1->u2.y - s2->u2.y;
162 temp.u3.z=s1->u3.z - s2->u3.z;
163
164 *d = temp;
165 return d;
166 }
167
168 /* Cross Product of Two Vectors */
169 D3DVECTOR * WINAPI D3DRMVectorCrossProduct(D3DVECTOR *d, D3DVECTOR *s1, D3DVECTOR *s2)
170 {
171 D3DVECTOR temp;
172
173 temp.u1.x=s1->u2.y * s2->u3.z - s1->u3.z * s2->u2.y;
174 temp.u2.y=s1->u3.z * s2->u1.x - s1->u1.x * s2->u3.z;
175 temp.u3.z=s1->u1.x * s2->u2.y - s1->u2.y * s2->u1.x;
176
177 *d = temp;
178 return d;
179 }
180
181 /* Dot Product of Two vectors */
182 D3DVALUE WINAPI D3DRMVectorDotProduct(D3DVECTOR *s1, D3DVECTOR *s2)
183 {
184 D3DVALUE dot_product;
185 dot_product=s1->u1.x * s2->u1.x + s1->u2.y * s2->u2.y + s1->u3.z * s2->u3.z;
186 return dot_product;
187 }
188
189 /* Norm of a vector */
190 D3DVALUE WINAPI D3DRMVectorModulus(D3DVECTOR *v)
191 {
192 D3DVALUE result;
193 result=sqrt(v->u1.x * v->u1.x + v->u2.y * v->u2.y + v->u3.z * v->u3.z);
194 return result;
195 }
196
197 /* Normalize a vector. Returns (1,0,0) if INPUT is the NULL vector. */
198 D3DVECTOR * WINAPI D3DRMVectorNormalize(D3DVECTOR *u)
199 {
200 D3DVALUE modulus = D3DRMVectorModulus(u);
201 if(modulus)
202 {
203 D3DRMVectorScale(u,u,1.0/modulus);
204 }
205 else
206 {
207 u->u1.x=1.0;
208 u->u2.y=0.0;
209 u->u3.z=0.0;
210 }
211 return u;
212 }
213
214 /* Returns a random unit vector */
215 D3DVECTOR * WINAPI D3DRMVectorRandom(D3DVECTOR *d)
216 {
217 d->u1.x = rand();
218 d->u2.y = rand();
219 d->u3.z = rand();
220 D3DRMVectorNormalize(d);
221 return d;
222 }
223
224 /* Reflection of a vector on a surface */
225 D3DVECTOR * WINAPI D3DRMVectorReflect(D3DVECTOR *r, D3DVECTOR *ray, D3DVECTOR *norm)
226 {
227 D3DVECTOR sca, temp;
228 D3DRMVectorSubtract(&temp, D3DRMVectorScale(&sca, norm, 2.0*D3DRMVectorDotProduct(ray,norm)), ray);
229
230 *r = temp;
231 return r;
232 }
233
234 /* Rotation of a vector */
235 D3DVECTOR * WINAPI D3DRMVectorRotate(D3DVECTOR *r, D3DVECTOR *v, D3DVECTOR *axis, D3DVALUE theta)
236 {
237 D3DRMQUATERNION quaternion1, quaternion2, quaternion3;
238 D3DVECTOR norm;
239
240 quaternion1.s = cos(theta * 0.5f);
241 quaternion2.s = cos(theta * 0.5f);
242 norm = *D3DRMVectorNormalize(axis);
243 D3DRMVectorScale(&quaternion1.v, &norm, sin(theta * 0.5f));
244 D3DRMVectorScale(&quaternion2.v, &norm, -sin(theta * 0.5f));
245 quaternion3.s = 0.0;
246 quaternion3.v = *v;
247 D3DRMQuaternionMultiply(&quaternion1, &quaternion1, &quaternion3);
248 D3DRMQuaternionMultiply(&quaternion1, &quaternion1, &quaternion2);
249
250 *r = *D3DRMVectorNormalize(&quaternion1.v);
251 return r;
252 }
253
254 /* Scale a vector */
255 D3DVECTOR * WINAPI D3DRMVectorScale(D3DVECTOR *d, D3DVECTOR *s, D3DVALUE factor)
256 {
257 D3DVECTOR temp;
258
259 temp.u1.x=factor * s->u1.x;
260 temp.u2.y=factor * s->u2.y;
261 temp.u3.z=factor * s->u3.z;
262
263 *d = temp;
264 return d;
265 }