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
29 /* Create a RGB color from its components */
30 D3DCOLOR WINAPI
D3DRMCreateColorRGB(D3DVALUE red
, D3DVALUE green
, D3DVALUE blue
)
32 return (D3DRMCreateColorRGBA(red
, green
, blue
, 255.0));
34 /* Create a RGBA color from its components */
35 D3DCOLOR WINAPI
D3DRMCreateColorRGBA(D3DVALUE red
, D3DVALUE green
, D3DVALUE blue
, D3DVALUE alpha
)
37 int Red
, Green
, Blue
, Alpha
;
39 Green
=floor(green
*255);
41 Alpha
=floor(alpha
*255);
44 if (green
< 0) Green
=0;
45 if (green
> 1) Green
=255;
47 if (blue
> 1) Blue
=255;
48 if (alpha
< 0) Alpha
=0;
49 if (alpha
> 1) Alpha
=255;
50 return (RGBA_MAKE(Red
, Green
, Blue
, Alpha
));
53 /* Determine the alpha part of a color */
54 D3DVALUE WINAPI
D3DRMColorGetAlpha(D3DCOLOR color
)
56 return (RGBA_GETALPHA(color
)/255.0);
59 /* Determine the blue part of a color */
60 D3DVALUE WINAPI
D3DRMColorGetBlue(D3DCOLOR color
)
62 return (RGBA_GETBLUE(color
)/255.0);
65 /* Determine the green part of a color */
66 D3DVALUE WINAPI
D3DRMColorGetGreen(D3DCOLOR color
)
68 return (RGBA_GETGREEN(color
)/255.0);
71 /* Determine the red part of a color */
72 D3DVALUE WINAPI
D3DRMColorGetRed(D3DCOLOR color
)
74 return (RGBA_GETRED(color
)/255.0);
77 /* Product of 2 quaternions */
78 D3DRMQUATERNION
* WINAPI
D3DRMQuaternionMultiply(D3DRMQUATERNION
*q
, D3DRMQUATERNION
*a
, D3DRMQUATERNION
*b
)
81 D3DVECTOR cross_product
;
83 D3DRMVectorCrossProduct(&cross_product
, &a
->v
, &b
->v
);
84 temp
.s
= a
->s
* b
->s
- D3DRMVectorDotProduct(&a
->v
, &b
->v
);
85 temp
.v
.u1
.x
= a
->s
* b
->v
.u1
.x
+ b
->s
* a
->v
.u1
.x
+ cross_product
.u1
.x
;
86 temp
.v
.u2
.y
= a
->s
* b
->v
.u2
.y
+ b
->s
* a
->v
.u2
.y
+ cross_product
.u2
.y
;
87 temp
.v
.u3
.z
= a
->s
* b
->v
.u3
.z
+ b
->s
* a
->v
.u3
.z
+ cross_product
.u3
.z
;
93 /* Matrix for the Rotation that a unit quaternion represents */
94 void WINAPI
D3DRMMatrixFromQuaternion(D3DRMMATRIX4D m
, D3DRMQUATERNION
*q
)
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
);
119 /* Return a unit quaternion that represents a rotation of an angle around an axis */
120 D3DRMQUATERNION
* WINAPI
D3DRMQuaternionFromRotation(D3DRMQUATERNION
*q
, D3DVECTOR
*v
, D3DVALUE theta
)
122 q
->s
= cos(theta
/2.0);
123 D3DRMVectorScale(&q
->v
, D3DRMVectorNormalize(v
), sin(theta
/2.0));
127 /* Interpolation between two quaternions */
128 D3DRMQUATERNION
* WINAPI
D3DRMQuaternionSlerp(D3DRMQUATERNION
*q
,
129 D3DRMQUATERNION
*a
, D3DRMQUATERNION
*b
, D3DVALUE alpha
)
131 D3DVALUE dot
, epsilon
, temp
, theta
, u
;
134 dot
= a
->s
* b
->s
+ D3DRMVectorDotProduct(&a
->v
, &b
->v
);
143 if( 1.0f
- dot
> 0.001f
)
146 temp
= sin(theta
* temp
) / sin(theta
);
147 u
= sin(theta
* alpha
) / sin(theta
);
149 q
->s
= temp
* a
->s
+ epsilon
* u
* b
->s
;
150 D3DRMVectorScale(&v1
, &a
->v
, temp
);
151 D3DRMVectorScale(&v2
, &b
->v
, epsilon
* u
);
152 D3DRMVectorAdd(&q
->v
, &v1
, &v2
);
156 /* Add Two Vectors */
157 D3DVECTOR
* WINAPI
D3DRMVectorAdd(D3DVECTOR
*d
, D3DVECTOR
*s1
, D3DVECTOR
*s2
)
161 temp
.u1
.x
=s1
->u1
.x
+ s2
->u1
.x
;
162 temp
.u2
.y
=s1
->u2
.y
+ s2
->u2
.y
;
163 temp
.u3
.z
=s1
->u3
.z
+ s2
->u3
.z
;
169 /* Subtract Two Vectors */
170 D3DVECTOR
* WINAPI
D3DRMVectorSubtract(D3DVECTOR
*d
, D3DVECTOR
*s1
, D3DVECTOR
*s2
)
174 temp
.u1
.x
=s1
->u1
.x
- s2
->u1
.x
;
175 temp
.u2
.y
=s1
->u2
.y
- s2
->u2
.y
;
176 temp
.u3
.z
=s1
->u3
.z
- s2
->u3
.z
;
182 /* Cross Product of Two Vectors */
183 D3DVECTOR
* WINAPI
D3DRMVectorCrossProduct(D3DVECTOR
*d
, D3DVECTOR
*s1
, D3DVECTOR
*s2
)
187 temp
.u1
.x
=s1
->u2
.y
* s2
->u3
.z
- s1
->u3
.z
* s2
->u2
.y
;
188 temp
.u2
.y
=s1
->u3
.z
* s2
->u1
.x
- s1
->u1
.x
* s2
->u3
.z
;
189 temp
.u3
.z
=s1
->u1
.x
* s2
->u2
.y
- s1
->u2
.y
* s2
->u1
.x
;
195 /* Dot Product of Two vectors */
196 D3DVALUE WINAPI
D3DRMVectorDotProduct(D3DVECTOR
*s1
, D3DVECTOR
*s2
)
198 D3DVALUE dot_product
;
199 dot_product
=s1
->u1
.x
* s2
->u1
.x
+ s1
->u2
.y
* s2
->u2
.y
+ s1
->u3
.z
* s2
->u3
.z
;
203 /* Norm of a vector */
204 D3DVALUE WINAPI
D3DRMVectorModulus(D3DVECTOR
*v
)
207 result
=sqrt(v
->u1
.x
* v
->u1
.x
+ v
->u2
.y
* v
->u2
.y
+ v
->u3
.z
* v
->u3
.z
);
211 /* Normalize a vector. Returns (1,0,0) if INPUT is the NULL vector. */
212 D3DVECTOR
* WINAPI
D3DRMVectorNormalize(D3DVECTOR
*u
)
214 D3DVALUE modulus
= D3DRMVectorModulus(u
);
217 D3DRMVectorScale(u
,u
,1.0/modulus
);
228 /* Returns a random unit vector */
229 D3DVECTOR
* WINAPI
D3DRMVectorRandom(D3DVECTOR
*d
)
234 D3DRMVectorNormalize(d
);
238 /* Reflection of a vector on a surface */
239 D3DVECTOR
* WINAPI
D3DRMVectorReflect(D3DVECTOR
*r
, D3DVECTOR
*ray
, D3DVECTOR
*norm
)
242 D3DRMVectorSubtract(&temp
, D3DRMVectorScale(&sca
, norm
, 2.0*D3DRMVectorDotProduct(ray
,norm
)), ray
);
248 /* Rotation of a vector */
249 D3DVECTOR
* WINAPI
D3DRMVectorRotate(D3DVECTOR
*r
, D3DVECTOR
*v
, D3DVECTOR
*axis
, D3DVALUE theta
)
251 D3DRMQUATERNION quaternion1
, quaternion2
, quaternion3
;
254 quaternion1
.s
= cos(theta
* 0.5f
);
255 quaternion2
.s
= cos(theta
* 0.5f
);
256 norm
= *D3DRMVectorNormalize(axis
);
257 D3DRMVectorScale(&quaternion1
.v
, &norm
, sin(theta
* 0.5f
));
258 D3DRMVectorScale(&quaternion2
.v
, &norm
, -sin(theta
* 0.5f
));
261 D3DRMQuaternionMultiply(&quaternion1
, &quaternion1
, &quaternion3
);
262 D3DRMQuaternionMultiply(&quaternion1
, &quaternion1
, &quaternion2
);
264 *r
= *D3DRMVectorNormalize(&quaternion1
.v
);
269 D3DVECTOR
* WINAPI
D3DRMVectorScale(D3DVECTOR
*d
, D3DVECTOR
*s
, D3DVALUE factor
)
273 temp
.u1
.x
=factor
* s
->u1
.x
;
274 temp
.u2
.y
=factor
* s
->u2
.y
;
275 temp
.u3
.z
=factor
* s
->u3
.z
;