2 * Copyright 2007 Vijay Kiran Kamuju
3 * Copyright 2007 David Adam
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
21 #include "wine/test.h"
25 #define PI (4*atan(1.0))
26 #define admit_error 0.000001
28 #define expect_mat( expectedmat, gotmat)\
35 if (fabs(expectedmat[i][j]-gotmat[i][j])>admit_error)\
41 ok(equal, "Expected matrix=\n(%f,%f,%f,%f\n %f,%f,%f,%f\n %f,%f,%f,%f\n %f,%f,%f,%f\n)\n\n" \
42 "Got matrix=\n(%f,%f,%f,%f\n %f,%f,%f,%f\n %f,%f,%f,%f\n %f,%f,%f,%f)\n", \
43 expectedmat[0][0],expectedmat[0][1],expectedmat[0][2],expectedmat[0][3], \
44 expectedmat[1][0],expectedmat[1][1],expectedmat[1][2],expectedmat[1][3], \
45 expectedmat[2][0],expectedmat[2][1],expectedmat[2][2],expectedmat[2][3], \
46 expectedmat[3][0],expectedmat[3][1],expectedmat[3][2],expectedmat[3][3], \
47 gotmat[0][0],gotmat[0][1],gotmat[0][2],gotmat[0][3], \
48 gotmat[1][0],gotmat[1][1],gotmat[1][2],gotmat[1][3], \
49 gotmat[2][0],gotmat[2][1],gotmat[2][2],gotmat[2][3], \
50 gotmat[3][0],gotmat[3][1],gotmat[3][2],gotmat[3][3] ); \
53 #define expect_quat(expectedquat,gotquat) \
54 ok( (fabs(expectedquat.v.x-gotquat.v.x)<admit_error) && \
55 (fabs(expectedquat.v.y-gotquat.v.y)<admit_error) && \
56 (fabs(expectedquat.v.z-gotquat.v.z)<admit_error) && \
57 (fabs(expectedquat.s-gotquat.s)<admit_error), \
58 "Expected Quaternion %f %f %f %f , Got Quaternion %f %f %f %f\n", \
59 expectedquat.s,expectedquat.v.x,expectedquat.v.y,expectedquat.v.z, \
60 gotquat.s,gotquat.v.x,gotquat.v.y,gotquat.v.z);
62 #define expect_vec(expectedvec,gotvec) \
63 ok( ((fabs(expectedvec.x-gotvec.x)<admit_error)&&(fabs(expectedvec.y-gotvec.y)<admit_error)&&(fabs(expectedvec.z-gotvec.z)<admit_error)), \
64 "Expected Vector= (%f, %f, %f)\n , Got Vector= (%f, %f, %f)\n", \
65 expectedvec.x,expectedvec.y,expectedvec.z, gotvec.x, gotvec.y, gotvec.z);
67 static void VectorTest(void)
69 D3DVALUE mod,par,theta;
70 D3DVECTOR e,r,u,v,w,axis,casnul,norm,ray;
72 u.x=2.0;u.y=2.0;u.z=1.0;
73 v.x=4.0;v.y=4.0;v.z=0.0;
75 /*______________________VectorAdd_________________________________*/
76 D3DRMVectorAdd(&r,&u,&v);
77 e.x=6.0;e.y=6.0;e.z=1.0;
80 /*_______________________VectorSubtract__________________________*/
81 D3DRMVectorSubtract(&r,&u,&v);
82 e.x=-2.0;e.y=-2.0;e.z=1.0;
85 /*_______________________VectorCrossProduct_______________________*/
86 D3DRMVectorCrossProduct(&r,&u,&v);
87 e.x=-4.0;e.y=4.0;e.z=0.0;
90 /*_______________________VectorDotProduct__________________________*/
91 mod=D3DRMVectorDotProduct(&u,&v);
92 ok((mod == 16.0), "Expected 16.0, Got %f\n",mod);
94 /*_______________________VectorModulus_____________________________*/
95 mod=D3DRMVectorModulus(&u);
96 ok((mod == 3.0), "Expected 3.0, Got %f\n",mod);
98 /*_______________________VectorNormalize___________________________*/
99 D3DRMVectorNormalize(&u);
100 e.x=2.0/3.0;e.y=2.0/3.0;e.z=1.0/3.0;
103 /* If u is the NULL vector, MSDN says that the return vector is NULL. In fact, the returned vector is (1,0,0). The following test case prove it. */
105 casnul.x=0.0; casnul.y=0.0; casnul.z=0.0;
106 D3DRMVectorNormalize(&casnul);
107 e.x=1.0; e.y=0.0; e.z=0.0;
108 expect_vec(e,casnul);
110 /*____________________VectorReflect_________________________________*/
111 ray.x=3.0; ray.y=-4.0; ray.z=5.0;
112 norm.x=1.0; norm.y=-2.0; norm.z=6.0;
113 e.x=79.0; e.y=-160.0; e.z=487.0;
114 D3DRMVectorReflect(&r,&ray,&norm);
117 /*_______________________VectorRotate_______________________________*/
118 w.x=3.0;w.y=4.0;w.z=0.0;
119 axis.x=0.0;axis.y=0.0;axis.z=1.0;
121 D3DRMVectorRotate(&r,&w,&axis,theta);
122 e.x=-0.3-0.4*sqrt(3.0); e.y=0.3*sqrt(3.0)-0.4; e.z=0.0;
125 /* The same formula gives D3DRMVectorRotate, for theta in [-PI/2;+PI/2] or not. The following test proves this fact.*/
127 D3DRMVectorRotate(&r,&w,&axis,-PI/4);
128 e.x=1.4/sqrt(2.0); e.y=0.2/sqrt(2.0); e.z=0.0;
131 /*_______________________VectorScale__________________________*/
133 D3DRMVectorScale(&r,&v,par);
134 e.x=10.0; e.y=10.0; e.z=0.0;
138 static void MatrixTest(void)
141 D3DRMMATRIX4D exp,mat;
143 exp[0][0]=-49.0; exp[0][1]=4.0; exp[0][2]=22.0; exp[0][3]=0.0;
144 exp[1][0]=20.0; exp[1][1]=-39.0; exp[1][2]=20.0; exp[1][3]=0.0;
145 exp[2][0]=10.0; exp[2][1]=28.0; exp[2][2]=-25.0; exp[2][3]=0.0;
146 exp[3][0]=0.0; exp[3][1]=0.0; exp[3][2]=0.0; exp[3][3]=1.0;
147 q.s=1.0; q.v.x=2.0; q.v.y=3.0; q.v.z=4.0;
149 D3DRMMatrixFromQuaternion(mat,&q);
153 static void QuaternionTest(void)
156 D3DVALUE g,h,epsilon,par,theta;
157 D3DRMQUATERNION q,q1,q2,r;
159 /*_________________QuaternionFromRotation___________________*/
160 axis.x=1.0;axis.y=1.0;axis.z=1.0;
162 D3DRMQuaternionFromRotation(&r,&axis,theta);
163 q.s=0.5;q.v.x=0.5;q.v.y=0.5;q.v.z=0.5;
166 /*_________________QuaternionSlerp_________________________*/
167 /* Interpolation slerp is in fact a linear interpolation, not a spherical linear
168 * interpolation. Moreover, if the angle of the two quaternions is in ]PI/2;3PI/2[, QuaternionSlerp
169 * interpolates between the first quaternion and the opposite of the second one. The test proves
170 * these two facts. */
172 q1.s=1.0; q1.v.x=2.0; q1.v.y=3.0; q1.v.z=50.0;
173 q2.s=-4.0; q2.v.x=6.0; q2.v.y=7.0; q2.v.z=8.0;
174 /* The angle between q1 and q2 is in [-PI/2,PI/2]. So, one interpolates between q1 and q2. */
176 g=1.0-par; h=epsilon*par;
177 /* Part of the test proving that the interpolation is linear. */
179 q.v.x=g*q1.v.x+h*q2.v.x;
180 q.v.y=g*q1.v.y+h*q2.v.y;
181 q.v.z=g*q1.v.z+h*q2.v.z;
182 D3DRMQuaternionSlerp(&r,&q1,&q2,par);
185 q1.s=1.0; q1.v.x=2.0; q1.v.y=3.0; q1.v.z=50.0;
186 q2.s=-94.0; q2.v.x=6.0; q2.v.y=7.0; q2.v.z=-8.0;
187 /* The angle between q1 and q2 is not in [-PI/2,PI/2]. So, one interpolates between q1 and -q2. */
189 g=1.0-par; h=epsilon*par;
191 q.v.x=g*q1.v.x+h*q2.v.x;
192 q.v.y=g*q1.v.y+h*q2.v.y;
193 q.v.z=g*q1.v.z+h*q2.v.z;
194 D3DRMQuaternionSlerp(&r,&q1,&q2,par);