world_test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * World unit tests
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <world.h>
  10. #include <light.h>
  11. #include <sphere.h>
  12. #include <material.h>
  13. #include <transformation.h>
  14. #include <worldbuilder.h>
  15. #include <testpattern.h>
  16. #include <math.h>
  17. #include <gtest/gtest.h>
  18. #include <plane.h>
  19. TEST(WorldTest, Creating_a_world)
  20. {
  21. World w;
  22. ASSERT_EQ(w.getLightCount(), 0);
  23. ASSERT_EQ(w.getObjectCount(), 0);
  24. }
  25. TEST(WorldTest, The_default_world)
  26. {
  27. World w = DefaultWorld();
  28. Light l = Light(POINT_LIGHT, Point(-10, 10, -10), Colour(1, 1, 1));
  29. Sphere s1 = Sphere();
  30. Sphere s2 = Sphere();
  31. Material s1Mat = Material();
  32. s1Mat.colour = Colour(0.8, 1.0, 0.6);
  33. s1Mat.diffuse = 0.7;
  34. s1Mat.specular = 0.2;
  35. s1.setMaterial(s1Mat);
  36. s2.setTransform(scaling(0.5, 0.5,0.5));
  37. Shape *obj0 = w.getObject(0);
  38. Shape *obj1 = w.getObject(1);
  39. ASSERT_TRUE(w.lightIsIn(l));
  40. ASSERT_EQ(*obj0, s1);
  41. ASSERT_EQ(*obj1, s2);
  42. }
  43. TEST(WorldTest, Intersect_a_world_with_a_ray)
  44. {
  45. World w = DefaultWorld();
  46. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  47. Intersect xs = w.intersect(r);
  48. ASSERT_EQ(xs.count(), 4);
  49. ASSERT_EQ(xs[0].t, 4);
  50. ASSERT_EQ(xs[1].t, 4.5);
  51. ASSERT_EQ(xs[2].t, 5.5);
  52. ASSERT_EQ(xs[3].t, 6);
  53. }
  54. TEST(WorldTest, Shading_an_intersection)
  55. {
  56. World w = DefaultWorld();
  57. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  58. Shape *s = w.getObject(0);
  59. Intersection i = Intersection(4, s);
  60. Computation comps = i.prepareComputation(r);
  61. Tuple c = w.shadeHit(comps);
  62. /* Temporary lower the precision */
  63. set_equal_precision(0.00001);
  64. ASSERT_EQ(c, Colour(0.38066, 0.47583, 0.2855));
  65. set_equal_precision(FLT_EPSILON);
  66. }
  67. TEST(WorldTest, The_when_ray_miss)
  68. {
  69. World w = DefaultWorld();
  70. Ray r = Ray(Point(0, 0, -5), Vector(0, 1, 0));
  71. Tuple c = w.colourAt(r);
  72. ASSERT_EQ(c, Colour(0, 0, 0));
  73. }
  74. TEST(WorldTest, The_when_ray_hit)
  75. {
  76. World w = DefaultWorld();
  77. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  78. Tuple c = w.colourAt(r);
  79. /* Temporary lower the precision */
  80. set_equal_precision(0.00001);
  81. ASSERT_EQ(c, Colour(0.38066, 0.47583, 0.2855));
  82. set_equal_precision(FLT_EPSILON);
  83. }
  84. TEST(WorldTest, The_colour_with_an_intersection_behind_the_ray)
  85. {
  86. World w = DefaultWorld();
  87. Shape *outer = w.getObject(0);
  88. outer->material.ambient = 1;
  89. Shape *inner = w.getObject(1);
  90. inner->material.ambient = 1;
  91. Ray r = Ray(Point(0, 0, 0.75), Vector(0, 0, -1));
  92. Tuple c = w.colourAt(r);
  93. ASSERT_EQ(c, inner->material.colour);
  94. }
  95. TEST(WorldTest, There_is_no_shadow_when_nothing_is_collinear_with_point_and_light)
  96. {
  97. World w = DefaultWorld();
  98. Tuple p = Point(0, 10, 0);
  99. ASSERT_FALSE(w.isShadowed(p, w.getLight(0)->position));
  100. }
  101. TEST(WorldTest, The_shadow_when_an_object_is_between_the_point_and_the_light)
  102. {
  103. World w = DefaultWorld();
  104. Tuple p = Point(10, -10, 10);
  105. ASSERT_TRUE(w.isShadowed(p, w.getLight(0)->position));
  106. }
  107. TEST(WorldTest, There_is_no_shadow_whne_an_object_is_behing_the_light)
  108. {
  109. World w = DefaultWorld();
  110. Tuple p = Point(-20, 20, -20);
  111. ASSERT_FALSE(w.isShadowed(p, w.getLight(0)->position));
  112. }
  113. TEST(WorldTest, There_is_no_shadow_when_an_object_is_behing_the_point)
  114. {
  115. World w = DefaultWorld();
  116. Tuple p = Point(-2, 2, -2);
  117. ASSERT_FALSE(w.isShadowed(p, w.getLight(0)->position));
  118. }
  119. TEST(WorldTest, Shade_hit_is_given_an_intersection_in_shadow)
  120. {
  121. World w = World();
  122. Light l = Light(POINT_LIGHT, Point(0, 0, -10), Colour(1, 1, 1));
  123. w.addLight(&l);
  124. Sphere s1 = Sphere();
  125. w.addObject(&s1);
  126. Sphere s2 = Sphere();
  127. s2.setTransform(translation(0, 0, 10));
  128. w.addObject(&s2);
  129. Ray r = Ray(Point(0, 0, 5), Vector(0, 0, 1));
  130. Intersection i = Intersection(4, &s2);
  131. Computation comps = i.prepareComputation(r);
  132. Tuple c = w.shadeHit(comps);
  133. ASSERT_EQ(c, Colour(0.1, 0.1, 0.1));
  134. }
  135. TEST(WorldTest, The_reflected_colour_for_a_non_reflective_material)
  136. {
  137. World w = DefaultWorld();
  138. Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
  139. Shape *shape = w.getObject(1); /* The second object */
  140. shape->material.ambient = 1; /* We use this to get a predictable colour */
  141. Intersection i = Intersection(1, shape);
  142. Computation comps = i.prepareComputation(r);
  143. Colour colour = w.reflectColour(comps);
  144. ASSERT_EQ(colour, Colour(0, 0, 0));
  145. }
  146. TEST(WorldTest, The_reflected_colour_for_a_reflective_material)
  147. {
  148. World w = DefaultWorld();
  149. Plane shape = Plane();
  150. shape.material.reflective = 0.5;
  151. shape.setTransform(translation(0, -1, 0));
  152. w.addObject(&shape);
  153. Ray r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2)/2, sqrt(2)/2));
  154. Intersection i = Intersection(sqrt(2), &shape);
  155. Computation comps = i.prepareComputation(r);
  156. Colour colour = w.reflectColour(comps);
  157. /* Temporary lower the precision */
  158. set_equal_precision(0.00002);
  159. ASSERT_EQ(colour, Colour(0.19032, 0.2379, 0.14274));
  160. set_equal_precision(FLT_EPSILON);
  161. }
  162. TEST(WorldTest, Shade_hit_with_a_reflective_material)
  163. {
  164. World w = DefaultWorld();
  165. Plane shape = Plane();
  166. shape.material.reflective = 0.5;
  167. shape.setTransform(translation(0, -1, 0));
  168. w.addObject(&shape);
  169. Ray r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2)/2, sqrt(2)/2));
  170. Intersection i = Intersection(sqrt(2), &shape);
  171. Computation comps = i.prepareComputation(r);
  172. Tuple colour = w.shadeHit(comps);
  173. /* Temporary lower the precision */
  174. set_equal_precision(0.00005);
  175. ASSERT_EQ(colour, Colour(0.87677, 0.92436, 0.82918));
  176. set_equal_precision(FLT_EPSILON);
  177. }
  178. TEST(WorldTest, Colour_at_with_mutually_reflective_surfaces)
  179. {
  180. World w = World();
  181. Light l = Light(POINT_LIGHT, Point(0, 0, 0), Colour(1, 1, 1));
  182. w.addLight(&l);
  183. Plane lower = Plane();
  184. lower.material.reflective = 1;
  185. lower.setTransform(translation(0, -1, 0));
  186. Plane higher = Plane();
  187. higher.material.reflective = 1;
  188. higher.setTransform(translation(0, 1, 0));
  189. w.addObject(&lower);
  190. w.addObject(&higher);
  191. Ray r = Ray(Point(0, 0, 0), Vector(0, 1, 0));
  192. /* It should just exit, we don't care about the actual colour */
  193. w.colourAt(r);
  194. SUCCEED();
  195. }
  196. TEST(WorldTest, The_reflected_colour_at_the_maximum_recursion_depth)
  197. {
  198. World w = DefaultWorld();
  199. Plane shape = Plane();
  200. shape.material.reflective = 0.5;
  201. shape.setTransform(translation(0, -1, 0));
  202. w.addObject(&shape);
  203. Ray r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2)/2, sqrt(2)/2));
  204. Intersection i = Intersection(sqrt(2), &shape);
  205. Computation comps = i.prepareComputation(r);
  206. Tuple colour = w.reflectColour(comps, 0);
  207. /* Temporary lower the precision */
  208. ASSERT_EQ(colour, Colour(0, 0, 0));
  209. }
  210. TEST(WorldTest, The_refracted_colour_with_an_opaque_surface)
  211. {
  212. World w = DefaultWorld();
  213. Shape *shape = w.getObject(0);
  214. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  215. Intersect xs = Intersect();
  216. xs.add(Intersection(4, shape));
  217. xs.add(Intersection(6, shape));
  218. Computation comps = xs[0].prepareComputation(r, &xs);
  219. Colour c = w.refractedColour(comps, 5);
  220. ASSERT_EQ(c, Colour(0, 0, 0));
  221. }
  222. TEST(WorldTest, The_refracted_colour_at_the_maximum_recursive_depth)
  223. {
  224. World w = DefaultWorld();
  225. Shape *shape = w.getObject(0);
  226. shape->material.transparency = 1.0;
  227. shape->material.refractiveIndex = 1.5;
  228. Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
  229. Intersect xs = Intersect();
  230. xs.add(Intersection(4, shape));
  231. xs.add(Intersection(6, shape));
  232. Computation comps = xs[0].prepareComputation(r, &xs);
  233. Colour c = w.refractedColour(comps, 0);
  234. ASSERT_EQ(c, Colour(0, 0, 0));
  235. }
  236. TEST(WorldTest, The_refracted_colour_under_total_internal_reflection)
  237. {
  238. World w = DefaultWorld();
  239. Shape *shape = w.getObject(0);
  240. shape->material.transparency = 1.0;
  241. shape->material.refractiveIndex = 1.5;
  242. Ray r = Ray(Point(0, 0, sqrt(2)/2), Vector(0, 1, 0));
  243. Intersect xs = Intersect();
  244. xs.add(Intersection(-sqrt(2)/2, shape));
  245. xs.add(Intersection(sqrt(2)/2, shape));
  246. Computation comps = xs[1].prepareComputation(r, &xs);
  247. Colour c = w.refractedColour(comps, 5);
  248. ASSERT_EQ(c, Colour(0, 0, 0));
  249. }
  250. TEST(WorldTest, The_refracted_coloud_with_a_refracted_ray)
  251. {
  252. World w = DefaultWorld();
  253. Shape *A = w.getObject(0);
  254. A->material.ambient = 1.0;
  255. A->material.pattern = new TestPattern();
  256. Shape *B = w.getObject(1);
  257. B->material.transparency = 1.0;
  258. B->material.refractiveIndex = 1.5;
  259. Ray r = Ray(Point(0, 0, 0.1), Vector(0, 1, 0));
  260. Intersect xs = Intersect();
  261. xs.add(Intersection(-0.9899, A));
  262. xs.add(Intersection(-0.4899, B));
  263. xs.add(Intersection(0.4899, B));
  264. xs.add(Intersection(0.9899, A));
  265. Computation comps = xs[2].prepareComputation(r, &xs);
  266. Colour c = w.refractedColour(comps, 5);
  267. /* Temporary lower the precision */
  268. set_equal_precision(0.00005);
  269. ASSERT_EQ(c, Colour(0, 0.99888, 0.04725));
  270. set_equal_precision(FLT_EPSILON);
  271. }
  272. TEST(WorldTest, Shade_hit_with_a_transparent_material)
  273. {
  274. World w = DefaultWorld();
  275. Plane floor = Plane();
  276. floor.setTransform(translation(0, -1, 0));
  277. floor.material.transparency = 0.5;
  278. floor.material.refractiveIndex = 1.5;
  279. w.addObject(&floor);
  280. Sphere ball = Sphere();
  281. ball.material.colour = Colour(1, 0, 0);
  282. ball.material.ambient = 0.5;
  283. ball.setTransform(translation(0, -3.5, -0.5));
  284. w.addObject(&ball);
  285. Ray r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2)/2, sqrt(2)/2));
  286. Intersect xs = Intersect();
  287. xs.add(Intersection(sqrt(2), &floor));
  288. Computation comps = xs[0].prepareComputation(r, &xs);
  289. Tuple c = w.shadeHit(comps, 5);
  290. /* Temporary lower the precision */
  291. set_equal_precision(0.00001);
  292. ASSERT_EQ(c, Colour(0.93642, 0.68642, 0.68642));
  293. set_equal_precision(FLT_EPSILON);
  294. }
  295. TEST(WorldTest, Shade_hit_with_a_reflective_transparent_material)
  296. {
  297. World w = DefaultWorld();
  298. Ray r = Ray(Point(0, 0, -3), Vector(0, -sqrt(2)/2, sqrt(2)/2));
  299. Plane floor = Plane();
  300. floor.setTransform(translation(0, -1, 0));
  301. floor.material.transparency = 0.5;
  302. floor.material.reflective = 0.5;
  303. floor.material.refractiveIndex = 1.5;
  304. w.addObject(&floor);
  305. Sphere ball = Sphere();
  306. ball.material.colour = Colour(1, 0, 0);
  307. ball.material.ambient = 0.5;
  308. ball.setTransform(translation(0, -3.5, -0.5));
  309. w.addObject(&ball);
  310. Intersect xs = Intersect();
  311. xs.add(Intersection(sqrt(2), &floor));
  312. Computation comps = xs[0].prepareComputation(r, &xs);
  313. Tuple c = w.shadeHit(comps, 5);
  314. /* Temporary lower the precision */
  315. set_equal_precision(0.00001);
  316. ASSERT_EQ(c, Colour(0.93391, 0.69643, 0.69243));
  317. set_equal_precision(FLT_EPSILON);
  318. }
  319. TEST(WorldTest, Is_shadow_test_for_occlusion_between_two_points)
  320. {
  321. World w = DefaultWorld();
  322. Tuple lightPosition = Point(-10, -10, -10);
  323. Point testList[] = {
  324. Point(-10, -10, 10),
  325. Point(10, 10, 10),
  326. Point(-20, -20, 20),
  327. Point(-5, -5, 5),
  328. };
  329. bool testResult[] = {
  330. false,
  331. true,
  332. false,
  333. false,
  334. };
  335. int testCount = sizeof(testList)/sizeof((testList)[0]);
  336. int i;
  337. for(i = 0; i < testCount; i++)
  338. {
  339. ASSERT_EQ(w.isShadowed(lightPosition, testList[i]), testResult[i]);
  340. }
  341. }