Release 1.5.29.
[wine] / dlls / d3drm / tests / vector.c
1 /*
2  * Copyright 2007 Vijay Kiran Kamuju
3  * Copyright 2007 David Adam
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 <math.h>
21
22 #include "d3drmdef.h"
23
24 #include "wine/test.h"
25
26 #define PI (4.0f*atanf(1.0f))
27 #define admit_error 0.000001f
28
29 #define expect_mat( expectedmat, gotmat)\
30 { \
31     int i,j,equal=1; \
32     for (i=0; i<4; i++)\
33         {\
34          for (j=0; j<4; j++)\
35              {\
36               if (fabs(expectedmat[i][j]-gotmat[i][j])>admit_error)\
37                  {\
38                   equal=0;\
39                  }\
40              }\
41         }\
42     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" \
43        "Got matrix=\n(%f,%f,%f,%f\n %f,%f,%f,%f\n %f,%f,%f,%f\n %f,%f,%f,%f)\n", \
44        expectedmat[0][0],expectedmat[0][1],expectedmat[0][2],expectedmat[0][3], \
45        expectedmat[1][0],expectedmat[1][1],expectedmat[1][2],expectedmat[1][3], \
46        expectedmat[2][0],expectedmat[2][1],expectedmat[2][2],expectedmat[2][3], \
47        expectedmat[3][0],expectedmat[3][1],expectedmat[3][2],expectedmat[3][3], \
48        gotmat[0][0],gotmat[0][1],gotmat[0][2],gotmat[0][3], \
49        gotmat[1][0],gotmat[1][1],gotmat[1][2],gotmat[1][3], \
50        gotmat[2][0],gotmat[2][1],gotmat[2][2],gotmat[2][3], \
51        gotmat[3][0],gotmat[3][1],gotmat[3][2],gotmat[3][3] ); \
52 }
53
54 #define expect_quat(expectedquat,gotquat) \
55   ok( (fabs(U1(expectedquat.v).x-U1(gotquat.v).x)<admit_error) &&     \
56       (fabs(U2(expectedquat.v).y-U2(gotquat.v).y)<admit_error) &&     \
57       (fabs(U3(expectedquat.v).z-U3(gotquat.v).z)<admit_error) &&     \
58       (fabs(expectedquat.s-gotquat.s)<admit_error), \
59   "Expected Quaternion %f %f %f %f , Got Quaternion %f %f %f %f\n", \
60       expectedquat.s,U1(expectedquat.v).x,U2(expectedquat.v).y,U3(expectedquat.v).z, \
61       gotquat.s,U1(gotquat.v).x,U2(gotquat.v).y,U3(gotquat.v).z);
62
63 #define expect_vec(expectedvec,gotvec) \
64   ok( ((fabs(U1(expectedvec).x-U1(gotvec).x)<admit_error)&&(fabs(U2(expectedvec).y-U2(gotvec).y)<admit_error)&&(fabs(U3(expectedvec).z-U3(gotvec).z)<admit_error)), \
65   "Expected Vector= (%f, %f, %f)\n , Got Vector= (%f, %f, %f)\n", \
66   U1(expectedvec).x,U2(expectedvec).y,U3(expectedvec).z, U1(gotvec).x, U2(gotvec).y, U3(gotvec).z);
67
68 static HMODULE d3drm_handle = 0;
69
70 static void (WINAPI * pD3DRMMatrixFromQuaternion)(D3DRMMATRIX4D, D3DRMQUATERNION *);
71 static D3DVECTOR *(WINAPI *pD3DRMVectorAdd)(D3DVECTOR *, D3DVECTOR *, D3DVECTOR *);
72 static D3DVECTOR *(WINAPI *pD3DRMVectorCrossProduct)(D3DVECTOR *, D3DVECTOR *, D3DVECTOR *);
73 static D3DVALUE (WINAPI *pD3DRMVectorDotProduct)(D3DVECTOR *, D3DVECTOR *);
74 static D3DVALUE (WINAPI *pD3DRMVectorModulus)(D3DVECTOR *);
75 static D3DVECTOR *(WINAPI *pD3DRMVectorNormalize)(D3DVECTOR *);
76 static D3DVECTOR *(WINAPI *pD3DRMVectorReflect)(D3DVECTOR *, D3DVECTOR *, D3DVECTOR *);
77 static D3DVECTOR *(WINAPI *pD3DRMVectorRotate)(D3DVECTOR *, D3DVECTOR *, D3DVECTOR *, D3DVALUE);
78 static D3DVECTOR *(WINAPI *pD3DRMVectorScale)(D3DVECTOR *, D3DVECTOR *, D3DVALUE);
79 static D3DVECTOR *(WINAPI *pD3DRMVectorSubtract)(D3DVECTOR *, D3DVECTOR *, D3DVECTOR *);
80 static D3DRMQUATERNION *(WINAPI *pD3DRMQuaternionFromRotation)(D3DRMQUATERNION*, D3DVECTOR *, D3DVALUE);
81 static D3DRMQUATERNION *(WINAPI * pD3DRMQuaternionSlerp)(D3DRMQUATERNION *,
82         D3DRMQUATERNION *, D3DRMQUATERNION *, D3DVALUE);
83 static D3DCOLOR (WINAPI * pD3DRMCreateColorRGB)(D3DVALUE, D3DVALUE, D3DVALUE);
84 static D3DCOLOR (WINAPI * pD3DRMCreateColorRGBA)(D3DVALUE, D3DVALUE, D3DVALUE, D3DVALUE);
85 static D3DVALUE (WINAPI * pD3DRMColorGetAlpha)(D3DCOLOR);
86 static D3DVALUE (WINAPI * pD3DRMColorGetBlue)(D3DCOLOR);
87 static D3DVALUE (WINAPI * pD3DRMColorGetGreen)(D3DCOLOR);
88 static D3DVALUE (WINAPI * pD3DRMColorGetRed)(D3DCOLOR);
89
90 #define D3DRM_GET_PROC(func) \
91     p ## func = (void*)GetProcAddress(d3drm_handle, #func); \
92     if(!p ## func) { \
93       trace("GetProcAddress(%s) failed\n", #func); \
94       FreeLibrary(d3drm_handle); \
95       return FALSE; \
96     }
97
98 static BOOL InitFunctionPtrs(void)
99 {
100     d3drm_handle = LoadLibraryA("d3drm.dll");
101
102     if(!d3drm_handle)
103     {
104         skip("Could not load d3drm.dll\n");
105         return FALSE;
106     }
107
108     D3DRM_GET_PROC(D3DRMMatrixFromQuaternion)
109     D3DRM_GET_PROC(D3DRMVectorAdd)
110     D3DRM_GET_PROC(D3DRMVectorCrossProduct)
111     D3DRM_GET_PROC(D3DRMVectorDotProduct)
112     D3DRM_GET_PROC(D3DRMVectorModulus)
113     D3DRM_GET_PROC(D3DRMVectorNormalize)
114     D3DRM_GET_PROC(D3DRMVectorReflect)
115     D3DRM_GET_PROC(D3DRMVectorRotate)
116     D3DRM_GET_PROC(D3DRMVectorScale)
117     D3DRM_GET_PROC(D3DRMVectorSubtract)
118     D3DRM_GET_PROC(D3DRMQuaternionFromRotation)
119     D3DRM_GET_PROC(D3DRMQuaternionSlerp)
120     D3DRM_GET_PROC(D3DRMCreateColorRGB)
121     D3DRM_GET_PROC(D3DRMCreateColorRGBA)
122     D3DRM_GET_PROC(D3DRMColorGetAlpha)
123     D3DRM_GET_PROC(D3DRMColorGetBlue)
124     D3DRM_GET_PROC(D3DRMColorGetGreen)
125     D3DRM_GET_PROC(D3DRMColorGetRed)
126
127     return TRUE;
128 }
129
130
131 static void VectorTest(void)
132 {
133     D3DVALUE mod,par,theta;
134     D3DVECTOR e,r,u,v,w,axis,casnul,norm,ray,self;
135
136     U1(u).x=2.0f; U2(u).y=2.0f; U3(u).z=1.0f;
137     U1(v).x=4.0f; U2(v).y=4.0f; U3(v).z=0.0f;
138
139
140 /*______________________VectorAdd_________________________________*/
141     pD3DRMVectorAdd(&r,&u,&v);
142     U1(e).x=6.0f; U2(e).y=6.0f; U3(e).z=1.0f;
143     expect_vec(e,r);
144
145     U1(self).x=9.0f; U2(self).y=18.0f; U3(self).z=27.0f;
146     pD3DRMVectorAdd(&self,&self,&u);
147     U1(e).x=11.0f; U2(e).y=20.0f; U3(e).z=28.0f;
148     expect_vec(e,self);
149
150 /*_______________________VectorSubtract__________________________*/
151     pD3DRMVectorSubtract(&r,&u,&v);
152     U1(e).x=-2.0f; U2(e).y=-2.0f; U3(e).z=1.0f;
153     expect_vec(e,r);
154
155     U1(self).x=9.0f; U2(self).y=18.0f; U3(self).z=27.0f;
156     pD3DRMVectorSubtract(&self,&self,&u);
157     U1(e).x=7.0f; U2(e).y=16.0f; U3(e).z=26.0f;
158     expect_vec(e,self);
159
160 /*_______________________VectorCrossProduct_______________________*/
161     pD3DRMVectorCrossProduct(&r,&u,&v);
162     U1(e).x=-4.0f; U2(e).y=4.0f; U3(e).z=0.0f;
163     expect_vec(e,r);
164
165     U1(self).x=9.0f; U2(self).y=18.0f; U3(self).z=27.0f;
166     pD3DRMVectorCrossProduct(&self,&self,&u);
167     U1(e).x=-36.0f; U2(e).y=45.0f; U3(e).z=-18.0f;
168     expect_vec(e,self);
169
170 /*_______________________VectorDotProduct__________________________*/
171     mod=pD3DRMVectorDotProduct(&u,&v);
172     ok((mod == 16.0f), "Expected 16.0f, Got %f\n", mod);
173
174 /*_______________________VectorModulus_____________________________*/
175     mod=pD3DRMVectorModulus(&u);
176     ok((mod == 3.0f), "Expected 3.0f, Got %f\n", mod);
177
178 /*_______________________VectorNormalize___________________________*/
179     pD3DRMVectorNormalize(&u);
180     U1(e).x=2.0f/3.0f; U2(e).y=2.0f/3.0f; U3(e).z=1.0f/3.0f;
181     expect_vec(e,u);
182
183 /* 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. */
184
185     U1(casnul).x=0.0f; U2(casnul).y=0.0f; U3(casnul).z=0.0f;
186     pD3DRMVectorNormalize(&casnul);
187     U1(e).x=1.0f; U2(e).y=0.0f; U3(e).z=0.0f;
188     expect_vec(e,casnul);
189
190 /*____________________VectorReflect_________________________________*/
191     U1(ray).x=3.0f; U2(ray).y=-4.0f; U3(ray).z=5.0f;
192     U1(norm).x=1.0f; U2(norm).y=-2.0f; U3(norm).z=6.0f;
193     U1(e).x=79.0f; U2(e).y=-160.0f; U3(e).z=487.0f;
194     pD3DRMVectorReflect(&r,&ray,&norm);
195     expect_vec(e,r);
196
197 /*_______________________VectorRotate_______________________________*/
198     U1(w).x=3.0f; U2(w).y=4.0f; U3(w).z=0.0f;
199     U1(axis).x=0.0f; U2(axis).y=0.0f; U3(axis).z=1.0f;
200     theta=2.0f*PI/3.0f;
201     pD3DRMVectorRotate(&r,&w,&axis,theta);
202     U1(e).x=-0.3f-0.4f*sqrtf(3.0f); U2(e).y=0.3f*sqrtf(3.0f)-0.4f; U3(e).z=0.0f;
203     expect_vec(e,r);
204
205 /* The same formula gives D3DRMVectorRotate, for theta in [-PI/2;+PI/2] or not. The following test proves this fact.*/
206     theta=-PI/4.0f;
207     pD3DRMVectorRotate(&r,&w,&axis,theta);
208     U1(e).x=1.4f/sqrtf(2.0f); U2(e).y=0.2f/sqrtf(2.0f); U3(e).z=0.0f;
209     expect_vec(e,r);
210
211     theta=PI/8.0f;
212     pD3DRMVectorRotate(&self,&self,&axis,theta);
213     U1(e).x=0.989950; U2(e).y=0.141421f; U3(e).z=0.0f;
214     expect_vec(e,r);
215
216 /*_______________________VectorScale__________________________*/
217     par=2.5f;
218     pD3DRMVectorScale(&r,&v,par);
219     U1(e).x=10.0f; U2(e).y=10.0f; U3(e).z=0.0f;
220     expect_vec(e,r);
221
222     U1(self).x=9.0f; U2(self).y=18.0f; U3(self).z=27.0f;
223     pD3DRMVectorScale(&self,&self,2);
224     U1(e).x=18.0f; U2(e).y=36.0f; U3(e).z=54.0f;
225     expect_vec(e,self);
226 }
227
228 static void MatrixTest(void)
229 {
230     D3DRMQUATERNION q;
231     D3DRMMATRIX4D exp,mat;
232
233     exp[0][0]=-49.0f; exp[0][1]=4.0f;   exp[0][2]=22.0f;  exp[0][3]=0.0f;
234     exp[1][0]=20.0f;  exp[1][1]=-39.0f; exp[1][2]=20.0f;  exp[1][3]=0.0f;
235     exp[2][0]=10.0f;  exp[2][1]=28.0f;  exp[2][2]=-25.0f; exp[2][3]=0.0f;
236     exp[3][0]=0.0f;   exp[3][1]=0.0f;   exp[3][2]=0.0f;   exp[3][3]=1.0f;
237     q.s=1.0f; U1(q.v).x=2.0f; U2(q.v).y=3.0f; U3(q.v).z=4.0f;
238
239    pD3DRMMatrixFromQuaternion(mat,&q);
240    expect_mat(exp,mat);
241 }
242
243 static void QuaternionTest(void)
244 {
245     D3DVECTOR axis;
246     D3DVALUE par,theta;
247     D3DRMQUATERNION q,q1,q1final,q2,q2final,r;
248
249 /*_________________QuaternionFromRotation___________________*/
250     U1(axis).x=1.0f; U2(axis).y=1.0f; U3(axis).z=1.0f;
251     theta=2.0f*PI/3.0f;
252     pD3DRMQuaternionFromRotation(&r,&axis,theta);
253     q.s=0.5f; U1(q.v).x=0.5f; U2(q.v).y=0.5f; U3(q.v).z=0.5f;
254     expect_quat(q,r);
255
256 /*_________________QuaternionSlerp_________________________*/
257 /* If the angle of the two quaternions is in ]PI/2;3PI/2[, QuaternionSlerp
258  * interpolates between the first quaternion and the opposite of the second one.
259  * The test proves this fact. */
260     par=0.31f;
261     q1.s=1.0f; U1(q1.v).x=2.0f; U2(q1.v).y=3.0f; U3(q1.v).z=50.0f;
262     q2.s=-4.0f; U1(q2.v).x=6.0f; U2(q2.v).y=7.0f; U3(q2.v).z=8.0f;
263 /* The angle between q1 and q2 is in [-PI/2,PI/2]. So, one interpolates between q1 and q2. */
264     q.s = -0.55f; U1(q.v).x=3.24f; U2(q.v).y=4.24f; U3(q.v).z=36.98f;
265     pD3DRMQuaternionSlerp(&r,&q1,&q2,par);
266     expect_quat(q,r);
267
268     q1.s=1.0f; U1(q1.v).x=2.0f; U2(q1.v).y=3.0f; U3(q1.v).z=50.0f;
269     q2.s=-94.0f; U1(q2.v).x=6.0f; U2(q2.v).y=7.0f; U3(q2.v).z=-8.0f;
270 /* The angle between q1 and q2 is not in [-PI/2,PI/2]. So, one interpolates between q1 and -q2. */
271     q.s=29.83f; U1(q.v).x=-0.48f; U2(q.v).y=-0.10f; U3(q.v).z=36.98f;
272     pD3DRMQuaternionSlerp(&r,&q1,&q2,par);
273     expect_quat(q,r);
274
275 /* Test the spherical interpolation part */
276     q1.s=0.1f; U1(q1.v).x=0.2f; U2(q1.v).y=0.3f; U3(q1.v).z=0.4f;
277     q2.s=0.5f; U1(q2.v).x=0.6f; U2(q2.v).y=0.7f; U3(q2.v).z=0.8f;
278     q.s = 0.243943f; U1(q.v).x = 0.351172f; U2(q.v).y = 0.458401f; U3(q.v).z = 0.565629f;
279
280     q1final=q1;
281     q2final=q2;
282     pD3DRMQuaternionSlerp(&r,&q1,&q2,par);
283     expect_quat(q,r);
284
285 /* Test to show that the input quaternions are not changed */
286     expect_quat(q1,q1final);
287     expect_quat(q2,q2final);
288 }
289
290 static void ColorTest(void)
291 {
292     D3DCOLOR color, expected_color, got_color;
293     D3DVALUE expected, got, red, green, blue, alpha;
294
295 /*___________D3DRMCreateColorRGB_________________________*/
296     red=0.8f;
297     green=0.3f;
298     blue=0.55f;
299     expected_color=0xffcc4c8c;
300     got_color=pD3DRMCreateColorRGB(red,green,blue);
301     ok((expected_color==got_color),"Expected color=%x, Got color=%x\n",expected_color,got_color);
302
303 /*___________D3DRMCreateColorRGBA________________________*/
304     red=0.1f;
305     green=0.4f;
306     blue=0.7f;
307     alpha=0.58f;
308     expected_color=0x931966b2;
309     got_color=pD3DRMCreateColorRGBA(red,green,blue,alpha);
310     ok((expected_color==got_color),"Expected color=%x, Got color=%x\n",expected_color,got_color);
311
312 /* if a component is <0 then, then one considers this component as 0. The following test proves this fact (test only with the red component). */
313     red=-0.88f;
314     green=0.4f;
315     blue=0.6f;
316     alpha=0.41f;
317     expected_color=0x68006699;
318     got_color=pD3DRMCreateColorRGBA(red,green,blue,alpha);
319     ok((expected_color==got_color),"Expected color=%x, Got color=%x\n",expected_color,got_color);
320
321 /* if a component is >1 then, then one considers this component as 1. The following test proves this fact (test only with the red component). */
322     red=2.37f;
323     green=0.4f;
324     blue=0.6f;
325     alpha=0.41f;
326     expected_color=0x68ff6699;
327     got_color=pD3DRMCreateColorRGBA(red,green,blue,alpha);
328     ok((expected_color==got_color),"Expected color=%x, Got color=%x\n",expected_color,got_color);
329
330 /*___________D3DRMColorGetAlpha_________________________*/
331     color=0x0e4921bf;
332     expected=14.0f/255.0f;
333     got=pD3DRMColorGetAlpha(color);
334     ok((fabs(expected-got)<admit_error),"Expected=%f, Got=%f\n",expected,got);
335
336 /*___________D3DRMColorGetBlue__________________________*/
337     color=0xc82a1455;
338     expected=1.0f/3.0f;
339     got=pD3DRMColorGetBlue(color);
340     ok((fabs(expected-got)<admit_error),"Expected=%f, Got=%f\n",expected,got);
341
342 /*___________D3DRMColorGetGreen_________________________*/
343     color=0xad971203;
344     expected=6.0f/85.0f;
345     got=pD3DRMColorGetGreen(color);
346     ok((fabs(expected-got)<admit_error),"Expected=%f, Got=%f\n",expected,got);
347
348 /*___________D3DRMColorGetRed__________________________*/
349     color=0xb62d7a1c;
350     expected=3.0f/17.0f;
351     got=pD3DRMColorGetRed(color);
352     ok((fabs(expected-got)<admit_error),"Expected=%f, Got=%f\n",expected,got);
353 }
354
355 START_TEST(vector)
356 {
357     if(!InitFunctionPtrs())
358         return;
359
360     VectorTest();
361     MatrixTest();
362     QuaternionTest();
363     ColorTest();
364
365     FreeLibrary(d3drm_handle);
366 }