light_test.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <colour.h>
  12. #include <tuple.h>
  13. #include <gtest/gtest.h>
  14. #include <world.h>
  15. #include <worldbuilder.h>
  16. TEST(LightTest, A_point_lighthas_a_position_and_intensity)
  17. {
  18. Colour intensity = Colour(1, 1, 1);
  19. Point position = Point(0, 0, 0);
  20. Light light = Light(POINT_LIGHT, position, intensity);
  21. ASSERT_EQ(light.position, position);
  22. ASSERT_EQ(light.intensity, intensity);
  23. }
  24. TEST(LightTest, Point_lights_evaluate_the_lights_intensity_at_a_given_point)
  25. {
  26. World w = DefaultWorld();
  27. Light *light = w.getLight(0);
  28. Tuple testList[] = {
  29. Point(0, 1.0001, 0),
  30. Point(-1.0001, 0, 0),
  31. Point(0, 0, -1.0001),
  32. Point(0, 0, 1.0001),
  33. Point(1.0001, 0, 0),
  34. Point(0, -1.0001, 0),
  35. Point(0, 0, 0),
  36. };
  37. double testResult[] = {
  38. 1.0,
  39. 1.0,
  40. 1.0,
  41. 0.0,
  42. 0.0,
  43. 0.0,
  44. 0.0,
  45. };
  46. int testCount = sizeof(testList)/sizeof((testList)[0]);
  47. int i;
  48. for(i = 0; i < testCount; i++)
  49. {
  50. ASSERT_TRUE(double_equal(light->intensityAt(w, testList[i]), testResult[i]));
  51. }
  52. }
  53. TEST(LightTest, Creating_an_area_light)
  54. {
  55. Tuple corner = Point(0, 0, 0);
  56. Tuple v1 = Vector(2, 0, 0);
  57. Tuple v2 = Vector(0, 0, 1);
  58. Light light = Light(AREA_LIGHT, corner, v1, 4, v2, 2, Colour(1, 1, 1));
  59. /* Position is used to store the corner in area lights */
  60. ASSERT_EQ(light.corner, corner);
  61. ASSERT_EQ(light.uVec, Vector(0.5, 0, 0));
  62. ASSERT_EQ(light.uSteps, 4);
  63. ASSERT_EQ(light.vVec, Vector(0, 0, 0.5));
  64. ASSERT_EQ(light.vSteps, 2);
  65. ASSERT_EQ(light.samples, 8);
  66. ASSERT_EQ(light.position, Point(1, 0, 0.5));
  67. }
  68. TEST(LightTest, Finding_a_single_point_on_an_area_light)
  69. {
  70. Tuple corner = Point(0, 0, 0);
  71. Tuple v1 = Vector(2, 0, 0);
  72. Tuple v2 = Vector(0, 0, 1);
  73. Light light = Light(AREA_LIGHT, corner, v1, 4, v2, 2, Colour(1, 1, 1));
  74. uint32_t testList[][2] = {
  75. {0, 0},
  76. {1, 0},
  77. {0, 1},
  78. {2, 0},
  79. {3, 1},
  80. };
  81. Point testResults[] {
  82. Point(0.25, 0, 0.25),
  83. Point(0.75, 0, 0.25),
  84. Point(0.25, 0, 0.75),
  85. Point(1.25, 0, 0.25),
  86. Point(1.75, 0, 0.75),
  87. };
  88. int testCount = sizeof(testList)/sizeof((testList)[0]);
  89. int i;
  90. for(i = 0; i < testCount; i++)
  91. {
  92. Tuple tp = light.pointOnLight(testList[i][0], testList[i][1]);
  93. ASSERT_EQ(tp, testResults[i]);
  94. }
  95. }
  96. TEST(LightTest, The_area_light_intensity_function)
  97. {
  98. World w = DefaultWorld();
  99. Tuple corner = Point(-0.5, -0.5, -5);
  100. Tuple v1 = Vector(1, 0, 0);
  101. Tuple v2 = Vector(0, 1, 0);
  102. Light light = Light(AREA_LIGHT, corner, v1, 2, v2, 2, Colour(1, 1, 1));
  103. Point testList[] = {
  104. Point(0, 0, 2),
  105. Point(1, -1, 2),
  106. Point(1.5, 0, 2),
  107. Point(1.25, 1.25, 3),
  108. Point(0, 0, -2),
  109. };
  110. double testResults[] {
  111. 0.0,
  112. 0.25,
  113. 0.5,
  114. 0.75,
  115. 1,
  116. };
  117. int testCount = sizeof(testList)/sizeof((testList)[0]);
  118. int i;
  119. for(i = 0; i < testCount; i++)
  120. {
  121. double intensity = light.intensityAt(w, testList[i]);
  122. ASSERT_TRUE(double_equal(intensity, testResults[i]));
  123. }
  124. }