Assorted spelling, punctuation and case fixes.
[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 <assert.h>
21 #include "d3drmdef.h"
22 #include <math.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, LPD3DRMQUATERNION);
71 static LPD3DVECTOR (WINAPI* pD3DRMVectorAdd)(LPD3DVECTOR, LPD3DVECTOR, LPD3DVECTOR);
72 static LPD3DVECTOR (WINAPI* pD3DRMVectorCrossProduct)(LPD3DVECTOR, LPD3DVECTOR, LPD3DVECTOR);
73 static D3DVALUE (WINAPI* pD3DRMVectorDotProduct)(LPD3DVECTOR, LPD3DVECTOR);
74 static D3DVALUE (WINAPI* pD3DRMVectorModulus)(LPD3DVECTOR);
75 static LPD3DVECTOR (WINAPI * pD3DRMVectorNormalize)(LPD3DVECTOR);
76 static LPD3DVECTOR (WINAPI * pD3DRMVectorReflect)(LPD3DVECTOR, LPD3DVECTOR, LPD3DVECTOR);
77 static LPD3DVECTOR (WINAPI * pD3DRMVectorRotate)(LPD3DVECTOR, LPD3DVECTOR, LPD3DVECTOR, D3DVALUE);
78 static LPD3DVECTOR (WINAPI * pD3DRMVectorScale)(LPD3DVECTOR, LPD3DVECTOR, D3DVALUE);
79 static LPD3DVECTOR (WINAPI * pD3DRMVectorSubtract)(LPD3DVECTOR, LPD3DVECTOR, LPD3DVECTOR);
80 static LPD3DRMQUATERNION (WINAPI * pD3DRMQuaternionFromRotation)(LPD3DRMQUATERNION, LPD3DVECTOR, D3DVALUE);
81 static LPD3DRMQUATERNION (WINAPI * pD3DRMQuaternionSlerp)(LPD3DRMQUATERNION, LPD3DRMQUATERNION, LPD3DRMQUATERNION, D3DVALUE);
82 static D3DCOLOR (WINAPI * pD3DRMCreateColorRGB)(D3DVALUE, D3DVALUE, D3DVALUE);
83 static D3DCOLOR (WINAPI * pD3DRMCreateColorRGBA)(D3DVALUE, D3DVALUE, D3DVALUE, D3DVALUE);
84 static D3DVALUE (WINAPI * pD3DRMColorGetAlpha)(D3DCOLOR);
85 static D3DVALUE (WINAPI * pD3DRMColorGetBlue)(D3DCOLOR);
86 static D3DVALUE (WINAPI * pD3DRMColorGetGreen)(D3DCOLOR);
87 static D3DVALUE (WINAPI * pD3DRMColorGetRed)(D3DCOLOR);
88
89 #define D3DRM_GET_PROC(func) \
90     p ## func = (void*)GetProcAddress(d3drm_handle, #func); \
91     if(!p ## func) { \
92       trace("GetProcAddress(%s) failed\n", #func); \
93       FreeLibrary(d3drm_handle); \
94       return FALSE; \
95     }
96
97 static BOOL InitFunctionPtrs(void)
98 {
99     d3drm_handle = LoadLibraryA("d3drm.dll");
100
101     if(!d3drm_handle)
102     {
103         skip("Could not load d3drm.dll\n");
104         return FALSE;
105     }
106
107     D3DRM_GET_PROC(D3DRMMatrixFromQuaternion)
108     D3DRM_GET_PROC(D3DRMVectorAdd)
109     D3DRM_GET_PROC(D3DRMVectorCrossProduct)
110     D3DRM_GET_PROC(D3DRMVectorDotProduct)
111     D3DRM_GET_PROC(D3DRMVectorModulus)
112     D3DRM_GET_PROC(D3DRMVectorNormalize)
113     D3DRM_GET_PROC(D3DRMVectorReflect)
114     D3DRM_GET_PROC(D3DRMVectorRotate)
115     D3DRM_GET_PROC(D3DRMVectorScale)
116     D3DRM_GET_PROC(D3DRMVectorSubtract)
117     D3DRM_GET_PROC(D3DRMQuaternionFromRotation)
118     D3DRM_GET_PROC(D3DRMQuaternionSlerp)
119     D3DRM_GET_PROC(D3DRMCreateColorRGB)
120     D3DRM_GET_PROC(D3DRMCreateColorRGBA)
121     D3DRM_GET_PROC(D3DRMColorGetAlpha)
122     D3DRM_GET_PROC(D3DRMColorGetBlue)
123     D3DRM_GET_PROC(D3DRMColorGetGreen)
124     D3DRM_GET_PROC(D3DRMColorGetRed)
125
126     return TRUE;
127 }
128
129
130 static void VectorTest(void)
131 {
132     D3DVALUE mod,par,theta;
133     D3DVECTOR e,r,u,v,w,axis,casnul,norm,ray;
134
135     U1(u).x=2.0f; U2(u).y=2.0f; U3(u).z=1.0f;
136     U1(v).x=4.0f; U2(v).y=4.0f; U3(v).z=0.0f;
137
138 /*______________________VectorAdd_________________________________*/
139     pD3DRMVectorAdd(&r,&u,&v);
140     U1(e).x=6.0f; U2(e).y=6.0f; U3(e).z=1.0f;
141     expect_vec(e,r);
142
143 /*_______________________VectorSubtract__________________________*/
144     pD3DRMVectorSubtract(&r,&u,&v);
145     U1(e).x=-2.0f; U2(e).y=-2.0f; U3(e).z=1.0f;
146     expect_vec(e,r);
147
148 /*_______________________VectorCrossProduct_______________________*/
149     pD3DRMVectorCrossProduct(&r,&u,&v);
150     U1(e).x=-4.0f; U2(e).y=4.0f; U3(e).z=0.0f;
151     expect_vec(e,r);
152
153 /*_______________________VectorDotProduct__________________________*/
154     mod=pD3DRMVectorDotProduct(&u,&v);
155     ok((mod == 16.0f), "Expected 16.0f, Got %f\n", mod);
156
157 /*_______________________VectorModulus_____________________________*/
158     mod=pD3DRMVectorModulus(&u);
159     ok((mod == 3.0f), "Expected 3.0f, Got %f\n", mod);
160
161 /*_______________________VectorNormalize___________________________*/
162     pD3DRMVectorNormalize(&u);
163     U1(e).x=2.0f/3.0f; U2(e).y=2.0f/3.0f; U3(e).z=1.0f/3.0f;
164     expect_vec(e,u);
165
166 /* 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. */
167
168     U1(casnul).x=0.0f; U2(casnul).y=0.0f; U3(casnul).z=0.0f;
169     pD3DRMVectorNormalize(&casnul);
170     U1(e).x=1.0f; U2(e).y=0.0f; U3(e).z=0.0f;
171     expect_vec(e,casnul);
172
173 /*____________________VectorReflect_________________________________*/
174     U1(ray).x=3.0f; U2(ray).y=-4.0f; U3(ray).z=5.0f;
175     U1(norm).x=1.0f; U2(norm).y=-2.0f; U3(norm).z=6.0f;
176     U1(e).x=79.0f; U2(e).y=-160.0f; U3(e).z=487.0f;
177     pD3DRMVectorReflect(&r,&ray,&norm);
178     expect_vec(e,r);
179
180 /*_______________________VectorRotate_______________________________*/
181     U1(w).x=3.0f; U2(w).y=4.0f; U3(w).z=0.0f;
182     U1(axis).x=0.0f; U2(axis).y=0.0f; U3(axis).z=1.0f;
183     theta=2.0f*PI/3.0f;
184     pD3DRMVectorRotate(&r,&w,&axis,theta);
185     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;
186     expect_vec(e,r);
187
188 /* The same formula gives D3DRMVectorRotate, for theta in [-PI/2;+PI/2] or not. The following test proves this fact.*/
189     theta=-PI/4.0f;
190     pD3DRMVectorRotate(&r,&w,&axis,theta);
191     U1(e).x=1.4f/sqrtf(2.0f); U2(e).y=0.2f/sqrtf(2.0f); U3(e).z=0.0f;
192     expect_vec(e,r);
193
194 /*_______________________VectorScale__________________________*/
195     par=2.5f;
196     pD3DRMVectorScale(&r,&v,par);
197     U1(e).x=10.0f; U2(e).y=10.0f; U3(e).z=0.0f;
198     expect_vec(e,r);
199 }
200
201 static void MatrixTest(void)
202 {
203     D3DRMQUATERNION q;
204     D3DRMMATRIX4D exp,mat;
205
206     exp[0][0]=-49.0f; exp[0][1]=4.0f;   exp[0][2]=22.0f;  exp[0][3]=0.0f;
207     exp[1][0]=20.0f;  exp[1][1]=-39.0f; exp[1][2]=20.0f;  exp[1][3]=0.0f;
208     exp[2][0]=10.0f;  exp[2][1]=28.0f;  exp[2][2]=-25.0f; exp[2][3]=0.0f;
209     exp[3][0]=0.0f;   exp[3][1]=0.0f;   exp[3][2]=0.0f;   exp[3][3]=1.0f;
210     q.s=1.0f; U1(q.v).x=2.0f; U2(q.v).y=3.0f; U3(q.v).z=4.0f;
211
212    pD3DRMMatrixFromQuaternion(mat,&q);
213    expect_mat(exp,mat);
214 }
215
216 static void QuaternionTest(void)
217 {
218     D3DVECTOR axis;
219     D3DVALUE g,h,epsilon,par,theta;
220     D3DRMQUATERNION q,q1,q2,r;
221
222 /*_________________QuaternionFromRotation___________________*/
223     U1(axis).x=1.0f; U2(axis).y=1.0f; U3(axis).z=1.0f;
224     theta=2.0f*PI/3.0f;
225     pD3DRMQuaternionFromRotation(&r,&axis,theta);
226     q.s=0.5f; U1(q.v).x=0.5f; U2(q.v).y=0.5f; U3(q.v).z=0.5f;
227     expect_quat(q,r);
228
229 /*_________________QuaternionSlerp_________________________*/
230 /* Interpolation slerp is in fact a linear interpolation, not a spherical linear
231  * interpolation. Moreover, if the angle of the two quaternions is in ]PI/2;3PI/2[, QuaternionSlerp
232  * interpolates between the first quaternion and the opposite of the second one. The test proves
233  * these two facts. */
234     par=0.31f;
235     q1.s=1.0f; U1(q1.v).x=2.0f; U2(q1.v).y=3.0f; U3(q1.v).z=50.0f;
236     q2.s=-4.0f; U1(q2.v).x=6.0f; U2(q2.v).y=7.0f; U3(q2.v).z=8.0f;
237 /* The angle between q1 and q2 is in [-PI/2,PI/2]. So, one interpolates between q1 and q2. */
238     epsilon=1.0f;
239     g=1.0f-par; h=epsilon*par;
240 /* Part of the test proving that the interpolation is linear. */
241     q.s=g*q1.s+h*q2.s;
242     U1(q.v).x=g*U1(q1.v).x+h*U1(q2.v).x;
243     U2(q.v).y=g*U2(q1.v).y+h*U2(q2.v).y;
244     U3(q.v).z=g*U3(q1.v).z+h*U3(q2.v).z;
245     pD3DRMQuaternionSlerp(&r,&q1,&q2,par);
246     expect_quat(q,r);
247
248     q1.s=1.0f; U1(q1.v).x=2.0f; U2(q1.v).y=3.0f; U3(q1.v).z=50.0f;
249     q2.s=-94.0f; U1(q2.v).x=6.0f; U2(q2.v).y=7.0f; U3(q2.v).z=-8.0f;
250 /* The angle between q1 and q2 is not in [-PI/2,PI/2]. So, one interpolates between q1 and -q2. */
251     epsilon=-1.0f;
252     g=1.0f-par; h=epsilon*par;
253     q.s=g*q1.s+h*q2.s;
254     U1(q.v).x=g*U1(q1.v).x+h*U1(q2.v).x;
255     U2(q.v).y=g*U2(q1.v).y+h*U2(q2.v).y;
256     U3(q.v).z=g*U3(q1.v).z+h*U3(q2.v).z;
257     pD3DRMQuaternionSlerp(&r,&q1,&q2,par);
258     expect_quat(q,r);
259 }
260
261 static void ColorTest(void)
262 {
263     D3DCOLOR color, expected_color, got_color;
264     D3DVALUE expected, got, red, green, blue, alpha;
265
266 /*___________D3DRMCreateColorRGB_________________________*/
267     red=0.8f;
268     green=0.3f;
269     blue=0.55f;
270     expected_color=0xffcc4c8c;
271     got_color=pD3DRMCreateColorRGB(red,green,blue);
272     ok((expected_color==got_color),"Expected color=%x, Got color=%x\n",expected_color,got_color);
273
274 /*___________D3DRMCreateColorRGBA________________________*/
275     red=0.1f;
276     green=0.4f;
277     blue=0.7f;
278     alpha=0.58f;
279     expected_color=0x931966b2;
280     got_color=pD3DRMCreateColorRGBA(red,green,blue,alpha);
281     ok((expected_color==got_color),"Expected color=%x, Got color=%x\n",expected_color,got_color);
282
283 /* 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). */
284     red=-0.88f;
285     green=0.4f;
286     blue=0.6f;
287     alpha=0.41f;
288     expected_color=0x68006699;
289     got_color=pD3DRMCreateColorRGBA(red,green,blue,alpha);
290     ok((expected_color==got_color),"Expected color=%x, Got color=%x\n",expected_color,got_color);
291
292 /* 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). */
293     red=2.37f;
294     green=0.4f;
295     blue=0.6f;
296     alpha=0.41f;
297     expected_color=0x68ff6699;
298     got_color=pD3DRMCreateColorRGBA(red,green,blue,alpha);
299     ok((expected_color==got_color),"Expected color=%x, Got color=%x\n",expected_color,got_color);
300
301 /*___________D3DRMColorGetAlpha_________________________*/
302     color=0x0e4921bf;
303     expected=14.0f/255.0f;
304     got=pD3DRMColorGetAlpha(color);
305     ok((fabs(expected-got)<admit_error),"Expected=%f, Got=%f\n",expected,got);
306
307 /*___________D3DRMColorGetBlue__________________________*/
308     color=0xc82a1455;
309     expected=1.0f/3.0f;
310     got=pD3DRMColorGetBlue(color);
311     ok((fabs(expected-got)<admit_error),"Expected=%f, Got=%f\n",expected,got);
312
313 /*___________D3DRMColorGetGreen_________________________*/
314     color=0xad971203;
315     expected=6.0f/85.0f;
316     got=pD3DRMColorGetGreen(color);
317     ok((fabs(expected-got)<admit_error),"Expected=%f, Got=%f\n",expected,got);
318
319 /*___________D3DRMColorGetRed__________________________*/
320     color=0xb62d7a1c;
321     expected=3.0f/17.0f;
322     got=pD3DRMColorGetRed(color);
323     ok((fabs(expected-got)<admit_error),"Expected=%f, Got=%f\n",expected,got);
324 }
325
326 START_TEST(vector)
327 {
328     if(!InitFunctionPtrs())
329         return;
330
331     VectorTest();
332     MatrixTest();
333     QuaternionTest();
334     ColorTest();
335
336     FreeLibrary(d3drm_handle);
337 }