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