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 #define NONAMELESSUNION
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3drm);
37 /* Determine the red part of a color */
38 D3DVALUE WINAPI D3DRMColorGetRed(D3DCOLOR color)
40 return (RGBA_GETRED(color)/255.0);
43 /* Product of 2 quaternions */
44 LPD3DRMQUATERNION WINAPI D3DRMQuaternionMultiply(LPD3DRMQUATERNION q, LPD3DRMQUATERNION a, LPD3DRMQUATERNION b)
46 D3DVECTOR cross_product;
47 D3DRMVectorCrossProduct(&cross_product, &a->v, &b->v);
48 q->s = a->s * b->s - D3DRMVectorDotProduct(&a->v, &b->v);
49 q->v.u1.x = a->s * b->v.u1.x + b->s * a->v.u1.x + cross_product.u1.x;
50 q->v.u2.y = a->s * b->v.u2.y + b->s * a->v.u2.y + cross_product.u2.y;
51 q->v.u3.z = a->s * b->v.u3.z + b->s * a->v.u3.z + cross_product.u3.z;
55 /* Matrix for the Rotation that a unit quaternion represents */
56 void WINAPI D3DRMMatrixFromQuaternion(D3DRMMATRIX4D m, LPD3DRMQUATERNION q)
63 m[0][0] = 1.0-2.0*(y*y+z*z);
64 m[1][1] = 1.0-2.0*(x*x+z*z);
65 m[2][2] = 1.0-2.0*(x*x+y*y);
66 m[1][0] = 2.0*(x*y+z*w);
67 m[0][1] = 2.0*(x*y-z*w);
68 m[2][0] = 2.0*(x*z-y*w);
69 m[0][2] = 2.0*(x*z+y*w);
70 m[2][1] = 2.0*(y*z+x*w);
71 m[1][2] = 2.0*(y*z-x*w);
81 /* Return a unit quaternion that represents a rotation of an angle around an axis */
82 LPD3DRMQUATERNION WINAPI D3DRMQuaternionFromRotation(LPD3DRMQUATERNION q, LPD3DVECTOR v, D3DVALUE theta)
84 q->s = cos(theta/2.0);
85 D3DRMVectorScale(&q->v, D3DRMVectorNormalize(v), sin(theta/2.0));
89 /* Interpolation between two quaternions */
90 LPD3DRMQUATERNION WINAPI D3DRMQuaternionSlerp(LPD3DRMQUATERNION q, LPD3DRMQUATERNION a, LPD3DRMQUATERNION b, D3DVALUE alpha)
94 if (a->s * b->s + D3DRMVectorDotProduct(&a->v, &b->v) < 0.0) epsilon = -1.0;
95 q->s = (1.0 - alpha) * a->s + epsilon * alpha * b->s;
96 D3DRMVectorAdd(&q->v, D3DRMVectorScale(&sca1, &a->v, 1.0 - alpha),
97 D3DRMVectorScale(&sca2, &b->v, epsilon * alpha));
101 /* Add Two Vectors */
102 LPD3DVECTOR WINAPI D3DRMVectorAdd(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
104 d->u1.x=s1->u1.x + s2->u1.x;
105 d->u2.y=s1->u2.y + s2->u2.y;
106 d->u3.z=s1->u3.z + s2->u3.z;
110 /* Subtract Two Vectors */
111 LPD3DVECTOR WINAPI D3DRMVectorSubtract(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
113 d->u1.x=s1->u1.x - s2->u1.x;
114 d->u2.y=s1->u2.y - s2->u2.y;
115 d->u3.z=s1->u3.z - s2->u3.z;
119 /* Cross Product of Two Vectors */
120 LPD3DVECTOR WINAPI D3DRMVectorCrossProduct(LPD3DVECTOR d, LPD3DVECTOR s1, LPD3DVECTOR s2)
122 d->u1.x=s1->u2.y * s2->u3.z - s1->u3.z * s2->u2.y;
123 d->u2.y=s1->u3.z * s2->u1.x - s1->u1.x * s2->u3.z;
124 d->u3.z=s1->u1.x * s2->u2.y - s1->u2.y * s2->u1.x;
128 /* Dot Product of Two vectors */
129 D3DVALUE WINAPI D3DRMVectorDotProduct(LPD3DVECTOR s1, LPD3DVECTOR s2)
131 D3DVALUE dot_product;
132 dot_product=s1->u1.x * s2->u1.x + s1->u2.y * s2->u2.y + s1->u3.z * s2->u3.z;
136 /* Norm of a vector */
137 D3DVALUE WINAPI D3DRMVectorModulus(LPD3DVECTOR v)
140 result=sqrt(v->u1.x * v->u1.x + v->u2.y * v->u2.y + v->u3.z * v->u3.z);
144 /* Normalize a vector. Returns (1,0,0) if INPUT is the NULL vector. */
145 LPD3DVECTOR WINAPI D3DRMVectorNormalize(LPD3DVECTOR u)
147 D3DVALUE modulus = D3DRMVectorModulus(u);
150 D3DRMVectorScale(u,u,1.0/modulus);
161 /* Returns a random unit vector */
162 LPD3DVECTOR WINAPI D3DRMVectorRandom(LPD3DVECTOR d)
167 D3DRMVectorNormalize(d);
171 /* Reflection of a vector on a surface */
172 LPD3DVECTOR WINAPI D3DRMVectorReflect(LPD3DVECTOR r, LPD3DVECTOR ray, LPD3DVECTOR norm)
175 D3DRMVectorSubtract(r, D3DRMVectorScale(&sca, norm, 2.0*D3DRMVectorDotProduct(ray,norm)), ray);
179 /* Rotation of a vector */
180 LPD3DVECTOR WINAPI D3DRMVectorRotate(LPD3DVECTOR r, LPD3DVECTOR v, LPD3DVECTOR axis, D3DVALUE theta)
182 D3DRMQUATERNION quaternion,quaternion1, quaternion2, quaternion3, resultq;
185 quaternion1.s = cos(theta*.5);
186 quaternion2.s = cos(theta*.5);
187 NORM = *D3DRMVectorNormalize(axis);
188 D3DRMVectorScale(&quaternion1.v, &NORM, sin(theta * .5));
189 D3DRMVectorScale(&quaternion2.v, &NORM, -sin(theta * .5));
192 D3DRMQuaternionMultiply(&quaternion, &quaternion1, &quaternion3);
193 D3DRMQuaternionMultiply(&resultq, &quaternion, &quaternion2);
194 *r = *D3DRMVectorNormalize(&resultq.v);
199 LPD3DVECTOR WINAPI D3DRMVectorScale(LPD3DVECTOR d, LPD3DVECTOR s, D3DVALUE factor)
201 d->u1.x=factor * s->u1.x;
202 d->u2.y=factor * s->u2.y;
203 d->u3.z=factor * s->u3.z;