light_test.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Light unit tests
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <light.h>
  10. #include <math.h>
  11. #include <math_helper.h>
  12. #include <colour.h>
  13. #include <tuple.h>
  14. #include <gtest/gtest.h>
  15. #include <world.h>
  16. #include <worldbuilder.h>
  17. TEST(LightTest, A_point_lighthas_a_position_and_intensity)
  18. {
  19. Colour intensity = Colour(1, 1, 1);
  20. Point position = Point(0, 0, 0);
  21. Light light = Light(POINT_LIGHT, position, intensity);
  22. ASSERT_EQ(light.position, position);
  23. ASSERT_EQ(light.intensity, intensity);
  24. }
  25. TEST(LightTest, Point_lights_evaluate_the_lights_intensity_at_a_given_point)
  26. {
  27. World w = DefaultWorld();
  28. Light *light = w.getLight(0);
  29. Tuple testList[] = {
  30. Point(0, 1.0001, 0),
  31. Point(-1.0001, 0, 0),
  32. Point(0, 0, -1.0001),
  33. Point(0, 0, 1.0001),
  34. Point(1.0001, 0, 0),
  35. Point(0, -1.0001, 0),
  36. Point(0, 0, 0),
  37. };
  38. double testResult[] = {
  39. 1.0,
  40. 1.0,
  41. 1.0,
  42. 0.0,
  43. 0.0,
  44. 0.0,
  45. 0.0,
  46. };
  47. int testCount = sizeof(testList)/sizeof((testList)[0]);
  48. int i;
  49. for(i = 0; i < testCount; i++)
  50. {
  51. ASSERT_TRUE(double_equal(light->intensityAt(w, testList[i]), testResult[i]));
  52. }
  53. }
  54. TEST(LightTest, Creating_an_area_light)
  55. {
  56. Tuple corner = Point(0, 0, 0);
  57. Tuple v1 = Vector(2, 0, 0);
  58. Tuple v2 = Vector(0, 0, 1);
  59. Light light = Light(AREA_LIGHT, corner, v1, 4, v2, 2, Colour(1, 1, 1));
  60. /* Position is used to store the corner in area lights */
  61. ASSERT_EQ(light.corner, corner);
  62. ASSERT_EQ(light.uVec, Vector(0.5, 0, 0));
  63. ASSERT_EQ(light.uSteps, 4);
  64. ASSERT_EQ(light.vVec, Vector(0, 0, 0.5));
  65. ASSERT_EQ(light.vSteps, 2);
  66. ASSERT_EQ(light.samples, 8);
  67. ASSERT_EQ(light.position, Point(1, 0, 0.5));
  68. }
  69. TEST(LightTest, Finding_a_single_point_on_an_area_light)
  70. {
  71. Tuple corner = Point(0, 0, 0);
  72. Tuple v1 = Vector(2, 0, 0);
  73. Tuple v2 = Vector(0, 0, 1);
  74. Light light = Light(AREA_LIGHT, corner, v1, 4, v2, 2, Colour(1, 1, 1));
  75. uint32_t testList[][2] = {
  76. {0, 0},
  77. {1, 0},
  78. {0, 1},
  79. {2, 0},
  80. {3, 1},
  81. };
  82. Point testResults[] {
  83. Point(0.25, 0, 0.25),
  84. Point(0.75, 0, 0.25),
  85. Point(0.25, 0, 0.75),
  86. Point(1.25, 0, 0.25),
  87. Point(1.75, 0, 0.75),
  88. };
  89. int testCount = sizeof(testList)/sizeof((testList)[0]);
  90. int i;
  91. for(i = 0; i < testCount; i++)
  92. {
  93. Tuple tp = light.pointOnLight(testList[i][0], testList[i][1]);
  94. ASSERT_EQ(tp, testResults[i]);
  95. }
  96. }
  97. TEST(LightTest, The_area_light_intensity_function)
  98. {
  99. World w = DefaultWorld();
  100. Tuple corner = Point(-0.5, -0.5, -5);
  101. Tuple v1 = Vector(1, 0, 0);
  102. Tuple v2 = Vector(0, 1, 0);
  103. Light light = Light(AREA_LIGHT, corner, v1, 2, v2, 2, Colour(1, 1, 1));
  104. Point testList[] = {
  105. Point(0, 0, 2),
  106. Point(1, -1, 2),
  107. Point(1.5, 0, 2),
  108. Point(1.25, 1.25, 3),
  109. Point(0, 0, -2),
  110. };
  111. double testResults[] {
  112. 0.0,
  113. 0.25,
  114. 0.5,
  115. 0.75,
  116. 1,
  117. };
  118. int testCount = sizeof(testList)/sizeof((testList)[0]);
  119. int i;
  120. for(i = 0; i < testCount; i++)
  121. {
  122. double intensity = light.intensityAt(w, testList[i]);
  123. ASSERT_TRUE(double_equal(intensity, testResults[i]));
  124. }
  125. }
  126. TEST(LightTest, Finding_a_single_point_on_a_jittered_area_light)
  127. {
  128. Tuple corner = Point(0, 0, 0);
  129. Tuple v1 = Vector(2, 0, 0);
  130. Tuple v2 = Vector(0, 0, 1);
  131. Light light = Light(AREA_LIGHT, corner, v1, 4, v2, 2, Colour(1, 1, 1), true);
  132. double seqList[] = { 0.3, 0.7 };
  133. light.jitterBy = Sequence(seqList, 2);
  134. uint32_t testList[][2] = {
  135. {0, 0},
  136. {1, 0},
  137. {0, 1},
  138. {2, 0},
  139. {3, 1},
  140. };
  141. Point testResults[] {
  142. Point(0.15, 0, 0.35),
  143. Point(0.65, 0, 0.35),
  144. Point(0.15, 0, 0.85),
  145. Point(1.15, 0, 0.35),
  146. Point(1.65, 0, 0.85),
  147. };
  148. int testCount = sizeof(testList)/sizeof((testList)[0]);
  149. int i;
  150. for(i = 0; i < testCount; i++)
  151. {
  152. Tuple tp = light.pointOnLight(testList[i][0], testList[i][1]);
  153. ASSERT_EQ(tp, testResults[i]);
  154. }
  155. }
  156. TEST(LightTest, The_area_light_with_jittered_samples)
  157. {
  158. World w = DefaultWorld();
  159. Tuple corner = Point(-0.5, -0.5, -5);
  160. Tuple v1 = Vector(1, 0, 0);
  161. Tuple v2 = Vector(0, 1, 0);
  162. Light light = Light(AREA_LIGHT, corner, v1, 2, v2, 2, Colour(1, 1, 1), true);
  163. double seqList[] = { 0.7, 0.3, 0.9, 0.1, 0.5 };
  164. light.jitterBy = Sequence(seqList, 5);
  165. Point testList[] = {
  166. Point(0, 0, 2),
  167. Point(1, -1, 2),
  168. Point(1.5, 0, 2),
  169. Point(1.25, 1.25, 3),
  170. Point(0, 0, -2),
  171. };
  172. double testResults[] {
  173. 0.0,
  174. 0.25, /* Chapter say 0.5 but it's not what I get here ?! */
  175. 0.75,
  176. 0.75,
  177. 1,
  178. };
  179. int testCount = sizeof(testList)/sizeof((testList)[0]);
  180. int i;
  181. for(i = 0; i < testCount; i++)
  182. {
  183. double intensity = light.intensityAt(w, testList[i]);
  184. EXPECT_TRUE(double_equal(intensity, testResults[i]));
  185. }
  186. }