pattern_test.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Pattern unit tests
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <pattern.h>
  10. #include <strippattern.h>
  11. #include <gradientpattern.h>
  12. #include <ringpattern.h>
  13. #include <checkerspattern.h>
  14. #include <testpattern.h>
  15. #include <transformation.h>
  16. #include <colour.h>
  17. #include <sphere.h>
  18. #include <gtest/gtest.h>
  19. #include <material.h>
  20. #include <uv_pattern.h>
  21. #include <uv_checkers.h>
  22. #include <uv_aligncheck.h>
  23. #include <texturemap.h>
  24. #ifdef ENABLE_LUA_SUPPORT
  25. extern "C" {
  26. #include <lua.h>
  27. #include <lauxlib.h>
  28. #include <lualib.h>
  29. }
  30. #include <luapattern.h>
  31. #endif
  32. Colour black = Colour(0, 0, 0);
  33. Colour white = Colour(1, 1, 1);
  34. TEST(PatternTest, Creating_a_stripe_pattern)
  35. {
  36. StripPattern p = StripPattern(white, black);
  37. ASSERT_EQ(p.a, white);
  38. ASSERT_EQ(p.b, black);
  39. }
  40. TEST(PatternTest, A_strip_pattern_is_constant_in_y)
  41. {
  42. StripPattern p = StripPattern(white, black);
  43. ASSERT_EQ(p.patternAt(Point(0, 0, 0)), white);
  44. ASSERT_EQ(p.patternAt(Point(0, 1, 0)), white);
  45. ASSERT_EQ(p.patternAt(Point(0, 2, 0)), white);
  46. }
  47. TEST(PatternTest, A_strip_pattern_is_constant_in_z)
  48. {
  49. StripPattern p = StripPattern(white, black);
  50. ASSERT_EQ(p.patternAt(Point(0, 0, 0)), white);
  51. ASSERT_EQ(p.patternAt(Point(0, 0, 1)), white);
  52. ASSERT_EQ(p.patternAt(Point(0, 0, 2)), white);
  53. }
  54. TEST(PatternTest, A_strip_pattern_alternate_in_x)
  55. {
  56. StripPattern p = StripPattern(white, black);
  57. ASSERT_EQ(p.patternAt(Point(0, 0, 0)), white);
  58. ASSERT_EQ(p.patternAt(Point(0.9, 0, 0)), white);
  59. ASSERT_EQ(p.patternAt(Point(1, 0, 0)), black);
  60. ASSERT_EQ(p.patternAt(Point(-0.1, 0, 0)), black);
  61. ASSERT_EQ(p.patternAt(Point(-1, 0, 0)), black);
  62. ASSERT_EQ(p.patternAt(Point(-1.1, 0, 0)), white);
  63. }
  64. TEST(PatternTest, Lightning_with_a_pattern_applied)
  65. {
  66. Sphere s = Sphere();
  67. Material m;
  68. StripPattern p = StripPattern(white, black);
  69. m.pattern = &p;
  70. m.ambient = 1;
  71. m.diffuse = 0;
  72. m.specular = 0;
  73. Tuple eyev = Vector(0, 0, -1);
  74. Tuple normalv = Vector(0, 0, -1);
  75. Light light = Light(POINT_LIGHT, Point(0, 0, -10), Colour(1, 1, 1));
  76. Colour c1 = m.lighting(light, Point(0, 9, 0), eyev, normalv, &s, false);
  77. Colour c2 = m.lighting(light, Point(1, 1, 0), eyev, normalv, &s, false);
  78. ASSERT_EQ(c1, Colour(1, 1, 1));
  79. ASSERT_EQ(c2, Colour(0, 0, 0));
  80. }
  81. TEST(PatternTest, Stripe_with_an_object_transformation)
  82. {
  83. Sphere s = Sphere();
  84. s.setTransform(scaling(2, 2, 2));
  85. StripPattern pattern = StripPattern(white, black);
  86. Colour c = pattern.patternAtObject(&s, Point(1.5, 0, 0));
  87. ASSERT_EQ(c, white);
  88. }
  89. TEST(PatternTest, Stripe_with_a_pattern_transformation)
  90. {
  91. Sphere s = Sphere();
  92. StripPattern pattern = StripPattern(white, black);
  93. pattern.setTransform(scaling(2, 2, 2));
  94. Colour c = pattern.patternAtObject(&s, Point(1.5, 0, 0));
  95. ASSERT_EQ(c, white);
  96. }
  97. TEST(PatternTest, Stripe_with_both_an_object_and_a_pattern_transformation)
  98. {
  99. Sphere s = Sphere();
  100. s.setTransform(scaling(2, 2, 2));
  101. StripPattern pattern = StripPattern(white, black);
  102. pattern.setTransform(translation(0.5, 0, 0));
  103. Colour c = pattern.patternAtObject(&s, Point(2.5, 0, 0));
  104. ASSERT_EQ(c, white);
  105. }
  106. TEST(PatternTest, The_default_pattern_transformation)
  107. {
  108. TestPattern pattern = TestPattern();
  109. ASSERT_EQ(pattern.transformMatrix, Matrix4().identity());
  110. }
  111. TEST(PatternTest, Assigning_a_transformation)
  112. {
  113. TestPattern pattern = TestPattern();
  114. pattern.setTransform(translation(1, 2, 3));
  115. ASSERT_EQ(pattern.transformMatrix, translation(1, 2, 3));
  116. }
  117. TEST(PatternTest, A_pattern_with_an_object_transformation)
  118. {
  119. Sphere s = Sphere();
  120. s.setTransform(scaling(2, 2, 2));
  121. TestPattern pattern = TestPattern();
  122. Colour c = pattern.patternAtObject(&s, Point(2, 3, 4));
  123. ASSERT_EQ(c, Colour(1, 1.5, 2));
  124. }
  125. TEST(PatternTest, A_pattern_with_a_pattern_transformation)
  126. {
  127. Sphere s = Sphere();
  128. TestPattern pattern = TestPattern();
  129. pattern.setTransform(scaling(2, 2, 2));
  130. Colour c = pattern.patternAtObject(&s, Point(2, 3, 4));
  131. ASSERT_EQ(c, Colour(1, 1.5, 2));
  132. }
  133. TEST(PatternTest, A_pattern_with_an_object_and_a_pattern_transformation)
  134. {
  135. Sphere s = Sphere();
  136. s.setTransform(scaling(2, 2, 2));
  137. TestPattern pattern = TestPattern();
  138. pattern.setTransform(translation(0.5, 1, 1.5));
  139. Colour c = pattern.patternAtObject(&s, Point(2.5, 3, 3.5));
  140. ASSERT_EQ(c, Colour(0.75, 0.5, 0.25));
  141. }
  142. TEST(PatternTest, A_gradient_linearly_interpolates_betweens_colors)
  143. {
  144. GradientPattern pattern = GradientPattern(white, black);
  145. ASSERT_EQ(pattern.patternAt(Point(0.25, 0, 0)), Colour(0.75, 0.75, 0.75));
  146. ASSERT_EQ(pattern.patternAt(Point(0.5, 0, 0)), Colour(0.5, 0.5, 0.5));
  147. ASSERT_EQ(pattern.patternAt(Point(0.75, 0, 0)), Colour(0.25, 0.25, 0.25));
  148. }
  149. TEST(PatternTest, A_ring_should_extend_in_both_x_and_z)
  150. {
  151. RingPattern pattern = RingPattern(white, black);
  152. ASSERT_EQ(pattern.patternAt(Point(0, 0, 0)), white);
  153. ASSERT_EQ(pattern.patternAt(Point(1, 0, 0)), black);
  154. ASSERT_EQ(pattern.patternAt(Point(0, 0, 1)), black);
  155. /* 0.708 is just bit more than sqrt(2)/2 */
  156. ASSERT_EQ(pattern.patternAt(Point(0.708, 0, 0.708)), black);
  157. }
  158. TEST(PatternTest, Checkers_should_repeat_in_x)
  159. {
  160. CheckersPattern pattern = CheckersPattern(white, black);
  161. ASSERT_EQ(pattern.patternAt(Point(0, 0, 0)), white);
  162. ASSERT_EQ(pattern.patternAt(Point(0.99, 0, 0)), white);
  163. ASSERT_EQ(pattern.patternAt(Point(1.01, 0, 0)), black);
  164. }
  165. TEST(PatternTest, Checkers_should_repeat_in_y)
  166. {
  167. CheckersPattern pattern = CheckersPattern(white, black);
  168. ASSERT_EQ(pattern.patternAt(Point(0, 0, 0)), white);
  169. ASSERT_EQ(pattern.patternAt(Point(0, 0.99, 0)), white);
  170. ASSERT_EQ(pattern.patternAt(Point(0, 1.01, 0)), black);
  171. }
  172. TEST(PatternTest, Checkers_should_repeat_in_z)
  173. {
  174. CheckersPattern pattern = CheckersPattern(white, black);
  175. ASSERT_EQ(pattern.patternAt(Point(0, 0, 0)), white);
  176. ASSERT_EQ(pattern.patternAt(Point(0, 0, 0.99)), white);
  177. ASSERT_EQ(pattern.patternAt(Point(0, 0, 1.01)), black);
  178. }
  179. #ifdef ENABLE_LUA_SUPPORT
  180. TEST(PatternTest, Simple_test_of_a_lua_pattern)
  181. {
  182. int error;
  183. LuaPattern pattern = LuaPattern(white, black);
  184. lua_State *L = luaL_newstate();
  185. luaL_openlibs(L);
  186. pattern.setLua(L);
  187. pattern.setLuaFunctionName("pat");
  188. error = luaL_loadstring(L, "function pat(x, y, z, a, b)\n"
  189. " local v = math.floor(x) + math.floor(y) + math.floor(z)\n"
  190. " if (v % 2) == 0 then\n"
  191. " return a.r, a.g, a.b\n"
  192. " end\n"
  193. " return b.r, b.g, b.b\n"
  194. "end");
  195. if (error)
  196. {
  197. fprintf(stderr, "%s\n", lua_tostring(L, -1));
  198. lua_pop(L, 1); /* pop error message from the stack */
  199. }
  200. ASSERT_EQ(error, 0);
  201. error = lua_pcall(L, 0, 0, 0);
  202. if (error)
  203. {
  204. fprintf(stderr, "%s\n", lua_tostring(L, -1));
  205. lua_pop(L, 1); /* pop error message from the stack */
  206. }
  207. ASSERT_EQ(error, 0);
  208. ASSERT_EQ(pattern.patternAt(Point(0, 0, 0)), white);
  209. ASSERT_EQ(pattern.patternAt(Point(0, 0.99, 0)), white);
  210. ASSERT_EQ(pattern.patternAt(Point(0, 1.01, 0)), black);
  211. lua_close(L);
  212. }
  213. #endif
  214. TEST(PatternTest, Checkers_pattern_in_2D)
  215. {
  216. UVCheckers checkers = UVCheckers(2, 2, black, white);
  217. ASSERT_EQ(checkers.uvPatternAt(0.0, 0.0), black);
  218. ASSERT_EQ(checkers.uvPatternAt(0.5, 0.0), white);
  219. ASSERT_EQ(checkers.uvPatternAt(0.0, 0.5), white);
  220. ASSERT_EQ(checkers.uvPatternAt(0.5, 0.5), black);
  221. ASSERT_EQ(checkers.uvPatternAt(1.0, 1.0), black);
  222. }
  223. TEST(PatternTest, Using_a_spherical_mapping_on_a_3d_point)
  224. {
  225. Point testList[] = {
  226. Point( 0, 0, -1),
  227. Point( 1, 0, 0),
  228. Point( 0, 0, 1),
  229. Point(-1, 0, 0),
  230. Point( 0, 1, 0),
  231. Point( 0, -1, 0),
  232. Point(sqrt(2)/2, sqrt(2)/2, 0),
  233. };
  234. double testResults[][2] {
  235. {0.0, 0.5},
  236. {0.25, 0.5},
  237. {0.5, 0.5},
  238. {0.75, 0.5},
  239. {0.5, 1.0},
  240. {0.5, 0.0},
  241. {0.25, 0.75},
  242. };
  243. int testCount = sizeof(testList)/sizeof((testList)[0]);
  244. int i;
  245. TextureMap tm = TextureMap(SPHERICAL_MAP, nullptr);
  246. for(i = 0; i < testCount; i++)
  247. {
  248. double u, v;
  249. tm.sphericalMap(testList[i], u, v);
  250. ASSERT_TRUE(double_equal(u, testResults[i][0]));
  251. ASSERT_TRUE(double_equal(v, testResults[i][1]));
  252. }
  253. }
  254. TEST(PatternTest, Using_a_texture_map_with_a_spherical_map)
  255. {
  256. Point testList[] = {
  257. Point( 0.4315, 0.4670, 0.7719),
  258. Point( -0.9654, 0.2252, -0.0534),
  259. Point( 0.1039, 0.7090, 0.6975),
  260. Point( -0.4986, -0.7856, -0.3663),
  261. Point( -0.0317, -0.9395, 0.3411),
  262. Point( 0.4809, -0.7721, 0.4154),
  263. Point( 0.0285, -0.9612, -0.2745),
  264. Point( -0.5734, -0.2162, -0.7903),
  265. Point( 0.7688, -0.1470, 0.6223),
  266. Point( -0.7652, 0.2175, 0.6060),
  267. };
  268. Colour testResults[] {
  269. white,
  270. black,
  271. white,
  272. black,
  273. black,
  274. black,
  275. black,
  276. white,
  277. black,
  278. black,
  279. };
  280. int testCount = sizeof(testList)/sizeof((testList)[0]);
  281. int i;
  282. UVCheckers uvpat = UVCheckers(16, 8, black, white);
  283. TextureMap tm = TextureMap(SPHERICAL_MAP, &uvpat);
  284. for(i = 0; i < testCount; i++)
  285. {
  286. Colour ret = tm.patternAt(testList[i]);
  287. EXPECT_EQ(ret, testResults[i]);
  288. }
  289. }
  290. TEST(PatternTest, Using_a_planar_mapping_on_a_3d_point)
  291. {
  292. Point testList[] = {
  293. Point(0.25, 0.0, 0.5),
  294. Point(0.25, 0.0, -0.25),
  295. Point(0.25, 0.5, -0.25),
  296. Point(1.25, 0.0, 0.5),
  297. Point(0.25, 0.0, -1.75),
  298. Point(1.00, 0.0, -1.0),
  299. Point(0.00, 0.0, 0.0),
  300. };
  301. double testResults[][2] {
  302. {0.25, 0.50},
  303. {0.25, 0.75},
  304. {0.25, 0.75},
  305. {0.25, 0.50},
  306. {0.25, 0.25},
  307. {0.00, 0.00},
  308. {0.00, 0.00},
  309. };
  310. int testCount = sizeof(testList)/sizeof((testList)[0]);
  311. int i;
  312. TextureMap tm = TextureMap(PLANAR_MAP, nullptr);
  313. for(i = 0; i < testCount; i++)
  314. {
  315. double u, v;
  316. tm.planarMap(testList[i], u, v);
  317. ASSERT_TRUE(double_equal(u, testResults[i][0]));
  318. ASSERT_TRUE(double_equal(v, testResults[i][1]));
  319. }
  320. }
  321. TEST(PatternTest, Using_a_cylindrical_mapping_on_a_3d_point)
  322. {
  323. Point testList[] = {
  324. Point( 0.00000, 0.00, -1.00000),
  325. Point( 0.00000, 0.50, -1.00000),
  326. Point( 0.00000, 1.00, -1.00000),
  327. Point( 0.70711, 0.50, -0.70711),
  328. Point( 1.00000, 0.50, -0.00000),
  329. Point( 0.70711, 0.50, 0.70711),
  330. Point( 0.00000, -0.25, 1.00000),
  331. Point(-0.70711, 0.50, 0.70711),
  332. Point(-1.00000, 1.25, 0.00000),
  333. Point(-0.70711, 0.50, -0.70711),
  334. };
  335. double testResults[][2] {
  336. {0.000, 0.00},
  337. {0.000, 0.50},
  338. {0.000, 0.00},
  339. {0.125, 0.50},
  340. {0.250, 0.50},
  341. {0.375, 0.50},
  342. {0.500, 0.75},
  343. {0.625, 0.50},
  344. {0.750, 0.25},
  345. {0.875, 0.50},
  346. };
  347. int testCount = sizeof(testList)/sizeof((testList)[0]);
  348. int i;
  349. TextureMap tm = TextureMap(CYLINDRICAL_MAP, nullptr);
  350. for(i = 0; i < testCount; i++)
  351. {
  352. double u, v;
  353. tm.cylindricalMap(testList[i], u, v);
  354. ASSERT_TRUE(double_equal(u, testResults[i][0]));
  355. ASSERT_TRUE(double_equal(v, testResults[i][1]));
  356. }
  357. }
  358. TEST(PatternTest, Layout_of_the_align_check_pattern)
  359. {
  360. Colour main = Colour(1, 1, 1);
  361. Colour ul = Colour(1, 0, 0);
  362. Colour ur = Colour(1, 1, 0);
  363. Colour bl = Colour(0, 1, 0);
  364. Colour br = Colour(0, 1, 1);
  365. double testList[][2] = {
  366. { 0.5, 0.5 },
  367. { 0.1, 0.9 },
  368. { 0.9, 0.9 },
  369. { 0.1, 0.1 },
  370. { 0.9, 0.1 },
  371. };
  372. Colour testResults[] {
  373. main,
  374. ul,
  375. ur,
  376. bl,
  377. br,
  378. };
  379. int testCount = sizeof(testList)/sizeof((testList)[0]);
  380. int i;
  381. UVAlignCheck uvac = UVAlignCheck(main, ul, ur, bl, br);
  382. for(i = 0; i < testCount; i++)
  383. {
  384. Colour ret = uvac.uvPatternAt(testList[i][0], testList[i][1]);
  385. ASSERT_EQ(ret, testResults[i]);
  386. }
  387. }
  388. TEST(PatternTest, Identifying_the_face_of_a_cube_from_a_point)
  389. {
  390. Point testList[] = {
  391. Point(-1, 0.5, -0.25),
  392. Point(1.1, -0.75, 0.8),
  393. Point(0.1, 0.6, 0.9),
  394. Point(-0.7, 0, -2),
  395. Point(0.5, 1, 0.9),
  396. Point(-0.2, -1.3, 1.1),
  397. };
  398. TextureMap::CubeFaces testResults[] {
  399. TextureMap::CUBE_LEFT,
  400. TextureMap::CUBE_RIGHT,
  401. TextureMap::CUBE_FRONT,
  402. TextureMap::CUBE_BACK,
  403. TextureMap::CUBE_UP,
  404. TextureMap::CUBE_DOWN,
  405. };
  406. int testCount = sizeof(testList)/sizeof((testList)[0]);
  407. int i;
  408. TextureMap tm = TextureMap(CUBIC_MAP, nullptr);
  409. for(i = 0; i < testCount; i++)
  410. {
  411. TextureMap::CubeFaces face = tm.faceFromPoint(testList[i]);
  412. ASSERT_EQ(face, testResults[i]);
  413. }
  414. }
  415. TEST(PatternTest, UV_mapping_the_front_face_of_a_cube)
  416. {
  417. Point testList[] = {
  418. Point(-0.5, 0.5, 1),
  419. Point(0.5, -0.5, 1),
  420. };
  421. double testResults[][2] {
  422. {0.25, 0.75},
  423. {0.75, 0.25},
  424. };
  425. int testCount = sizeof(testList)/sizeof((testList)[0]);
  426. int i;
  427. TextureMap tm = TextureMap(CUBIC_MAP, nullptr);
  428. for(i = 0; i < testCount; i++)
  429. {
  430. double u, v;
  431. tm.cubeUBFront(testList[i], u, v);
  432. ASSERT_TRUE(double_equal(u, testResults[i][0]));
  433. ASSERT_TRUE(double_equal(v, testResults[i][1]));
  434. }
  435. }
  436. TEST(PatternTest, UV_mapping_the_back_face_of_a_cube)
  437. {
  438. Point testList[] = {
  439. Point(0.5, 0.5, -1),
  440. Point(-0.5, -0.5, -1),
  441. };
  442. double testResults[][2] {
  443. {0.25, 0.75},
  444. {0.75, 0.25},
  445. };
  446. int testCount = sizeof(testList)/sizeof((testList)[0]);
  447. int i;
  448. TextureMap tm = TextureMap(CUBIC_MAP, nullptr);
  449. for(i = 0; i < testCount; i++)
  450. {
  451. double u, v;
  452. tm.cubeUBBack(testList[i], u, v);
  453. ASSERT_TRUE(double_equal(u, testResults[i][0]));
  454. ASSERT_TRUE(double_equal(v, testResults[i][1]));
  455. }
  456. }
  457. TEST(PatternTest, UV_mapping_the_left_face_of_a_cube)
  458. {
  459. Point testList[] = {
  460. Point(-1, 0.5, -0.5),
  461. Point(-1, -0.5, 0.5),
  462. };
  463. double testResults[][2] {
  464. {0.25, 0.75},
  465. {0.75, 0.25},
  466. };
  467. int testCount = sizeof(testList)/sizeof((testList)[0]);
  468. int i;
  469. TextureMap tm = TextureMap(CUBIC_MAP, nullptr);
  470. for(i = 0; i < testCount; i++)
  471. {
  472. double u, v;
  473. tm.cubeUBLeft(testList[i], u, v);
  474. ASSERT_TRUE(double_equal(u, testResults[i][0]));
  475. ASSERT_TRUE(double_equal(v, testResults[i][1]));
  476. }
  477. }
  478. TEST(PatternTest, UV_mapping_the_right_face_of_a_cube)
  479. {
  480. Point testList[] = {
  481. Point(1, 0.5, 0.5),
  482. Point(1, -0.5, -0.5),
  483. };
  484. double testResults[][2] {
  485. {0.25, 0.75},
  486. {0.75, 0.25},
  487. };
  488. int testCount = sizeof(testList)/sizeof((testList)[0]);
  489. int i;
  490. TextureMap tm = TextureMap(CUBIC_MAP, nullptr);
  491. for(i = 0; i < testCount; i++)
  492. {
  493. double u, v;
  494. tm.cubeUBRight(testList[i], u, v);
  495. ASSERT_TRUE(double_equal(u, testResults[i][0]));
  496. ASSERT_TRUE(double_equal(v, testResults[i][1]));
  497. }
  498. }
  499. TEST(PatternTest, UV_mapping_the_up_face_of_a_cube)
  500. {
  501. Point testList[] = {
  502. Point(-0.5, 1, -0.5),
  503. Point(0.5, 1, 0.5),
  504. };
  505. double testResults[][2] {
  506. {0.25, 0.75},
  507. {0.75, 0.25},
  508. };
  509. int testCount = sizeof(testList)/sizeof((testList)[0]);
  510. int i;
  511. TextureMap tm = TextureMap(CUBIC_MAP, nullptr);
  512. for(i = 0; i < testCount; i++)
  513. {
  514. double u, v;
  515. tm.cubeUBUp(testList[i], u, v);
  516. ASSERT_TRUE(double_equal(u, testResults[i][0]));
  517. ASSERT_TRUE(double_equal(v, testResults[i][1]));
  518. }
  519. }
  520. TEST(PatternTest, UV_mapping_the_down_face_of_a_cube)
  521. {
  522. Point testList[] = {
  523. Point(-0.5, -1, 0.5),
  524. Point(0.5, -1, -0.5),
  525. };
  526. double testResults[][2] {
  527. {0.25, 0.75},
  528. {0.75, 0.25},
  529. };
  530. int testCount = sizeof(testList)/sizeof((testList)[0]);
  531. int i;
  532. TextureMap tm = TextureMap(CUBIC_MAP, nullptr);
  533. for(i = 0; i < testCount; i++)
  534. {
  535. double u, v;
  536. tm.cubeUBDown(testList[i], u, v);
  537. ASSERT_TRUE(double_equal(u, testResults[i][0]));
  538. ASSERT_TRUE(double_equal(v, testResults[i][1]));
  539. }
  540. }
  541. TEST(PatternTest, Finding_the_colours_on_a_mapped_cube)
  542. {
  543. Colour red = Colour(1, 0, 0);
  544. Colour yellow = Colour(1, 1, 0);
  545. Colour brown = Colour(1, 0.5, 0);
  546. Colour green = Colour(0, 1, 0);
  547. Colour cyan = Colour(0, 1, 1);
  548. Colour blue = Colour(0, 0, 1);
  549. Colour purple = Colour(1, 0, 1);
  550. Colour white = Colour(1, 1, 1);
  551. UVAlignCheck left = UVAlignCheck(yellow, cyan, red, blue, brown);
  552. UVAlignCheck front = UVAlignCheck(cyan, red, yellow, brown, green);
  553. UVAlignCheck right = UVAlignCheck(red, yellow, purple, green, white);
  554. UVAlignCheck back = UVAlignCheck(green, purple, cyan, white, blue);
  555. UVAlignCheck up = UVAlignCheck(brown, cyan, purple, red, yellow);
  556. UVAlignCheck down = UVAlignCheck(purple, brown, green, blue, white);
  557. TextureMap tm = TextureMap(CUBIC_MAP, nullptr);
  558. tm.setCubePattern(&front, &left, &right, &back, &up, &down);
  559. Point testList[] = {
  560. Point(-1, 0, 0), Point(-1, 0.9, -0.9), Point(-1, 0.9, 0.9), Point(-1, -0.9, -0.9), Point(-1, -0.9, 0.9), /* left */
  561. Point(0, 0, 1), Point(-0.9, 0.9, 1), Point(0.9, 0.9, 1), Point(-0.9, -0.9, 1), Point(0.9, -0.9, 1), /* front */
  562. Point(1, 0, 0), Point(1, 0.9, 0.9), Point(1, 0.9, -0.9), Point(1, -0.9, 0.9), Point(1, -0.9, -0.9), /* right */
  563. Point(0, 0, -1), Point(0.9, 0.9, -1), Point(-0.9, 0.9, -1), Point(0.9, -0.9, -1), Point(-0.9, -0.9, -1), /* back */
  564. Point(0, 1, 0), Point(-0.9, 1, -0.9), Point(0.9, 1, -0.9), Point(-0.9, 1, 0.9), Point(0.9, 1, 0.9), /* up */
  565. Point(0, -1, 0), Point(-0.9, -1, 0.9), Point(0.9, -1, 0.9), Point(-0.9, -1, -0.9), Point(0.9, -1, -0.9), /* down */
  566. };
  567. Colour testResults[] {
  568. yellow, cyan, red, blue, brown, /* left */
  569. cyan, red, yellow, brown, green, /* front */
  570. red, yellow, purple, green, white, /* right */
  571. green, purple, cyan, white, blue, /* back */
  572. brown, cyan, purple, red, yellow, /* up */
  573. purple, brown, green, blue, white, /* down */
  574. };
  575. int testCount = sizeof(testList)/sizeof((testList)[0]);
  576. int i;
  577. for(i = 0; i < testCount; i++)
  578. {
  579. Colour ret = tm.patternAt(testList[i]);
  580. ASSERT_EQ(ret, testResults[i]);
  581. }
  582. }