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