material_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Material unit tests
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <material.h>
  10. #include <math.h>
  11. #include <colour.h>
  12. #include <testshape.h>
  13. #include <gtest/gtest.h>
  14. #include <world.h>
  15. #include <worldbuilder.h>
  16. TEST(MaterialTest, The_default_material)
  17. {
  18. Material m = Material();
  19. ASSERT_EQ(m.colour, Colour(1, 1, 1));
  20. ASSERT_EQ(m.ambient, 0.1);
  21. ASSERT_EQ(m.diffuse, 0.9);
  22. ASSERT_EQ(m.specular, 0.9);
  23. ASSERT_EQ(m.shininess, 200.0);
  24. }
  25. // This is used by the next tests
  26. static Material m = Material();
  27. static Point position = Point(0, 0, 0);
  28. TEST(MaterialTest, Lighting_with_the_eye_between_the_light_and_the_surface)
  29. {
  30. TestShape t = TestShape();
  31. Vector eyev = Vector(0, 0, -1);
  32. Vector normalv = Vector(0, 0, -1);
  33. Light light = Light(POINT_LIGHT, Point(0, 0, -10), Colour(1, 1, 1));
  34. Colour result = m.lighting(light, position, eyev, normalv, &t);
  35. ASSERT_EQ(result, Colour(1.9, 1.9, 1.9));
  36. }
  37. TEST(MaterialTest, Lighting_with_the_eye_between_the_light_and_the_surface_eye_offset_by_45)
  38. {
  39. TestShape t = TestShape();
  40. Vector eyev = Vector(0, -sqrt(2)/2, -sqrt(2)/2);
  41. Vector normalv = Vector(0, 0, -1);
  42. Light light = Light(POINT_LIGHT, Point(0, 0, -10), Colour(1, 1, 1));
  43. Colour result = m.lighting(light, position, eyev, normalv, &t);
  44. ASSERT_EQ(result, Colour(1.0, 1.0, 1.0));
  45. }
  46. TEST(MaterialTest, Lighting_with_the_eye_opposite_surface_light_offset_45)
  47. {
  48. TestShape t = TestShape();
  49. Vector eyev = Vector(0, 0, -1);
  50. Vector normalv = Vector(0, 0, -1);
  51. Light light = Light(POINT_LIGHT, Point(0, 10, -10), Colour(1, 1, 1));
  52. Colour result = m.lighting(light, position, eyev, normalv, &t);
  53. set_equal_precision(0.0001);
  54. ASSERT_EQ(result, Colour(0.7364, 0.7364, 0.7364));
  55. set_equal_precision(FLT_EPSILON);
  56. }
  57. TEST(MaterialTest, Lighting_with_the_eye_in_the_path_of_the_reflection_vector)
  58. {
  59. TestShape t = TestShape();
  60. Vector eyev = Vector(0, -sqrt(2)/2, -sqrt(2)/2);
  61. Vector normalv = Vector(0, 0, -1);
  62. Light light = Light(POINT_LIGHT, Point(0, 10, -10), Colour(1, 1, 1));
  63. Colour result = m.lighting(light, position, eyev, normalv, &t);
  64. set_equal_precision(0.0001);
  65. ASSERT_EQ(result, Colour(1.6364, 1.6364, 1.6364));
  66. set_equal_precision(FLT_EPSILON);
  67. }
  68. TEST(MaterialTest, Lighting_with_the_light_behind_the_surface)
  69. {
  70. TestShape t = TestShape();
  71. Vector eyev = Vector(0, 0, -1);
  72. Vector normalv = Vector(0, 0, -1);
  73. Light light = Light(POINT_LIGHT, Point(0, 0, 10), Colour(1, 1, 1));
  74. Colour result = m.lighting(light, position, eyev, normalv, &t);
  75. ASSERT_EQ(result, Colour(0.1, 0.1, 0.1));
  76. }
  77. TEST(MaterialTest, Lighting_with_the_surface_in_shadow)
  78. {
  79. TestShape t = TestShape();
  80. Vector eyev = Vector(0, 0, -1);
  81. Vector normalv = Vector(0, 0, -1);
  82. Light light = Light(POINT_LIGHT, Point(0, 0, -10), Colour(1, 1, 1));
  83. double lightLevel = 0.0;
  84. Colour result = m.lighting(light, position, eyev, normalv, &t, lightLevel);
  85. ASSERT_EQ(result, Colour(0.1, 0.1, 0.1));
  86. }
  87. TEST(MaterialTest, Reflectivity_for_the_default_material)
  88. {
  89. Material m = Material();
  90. ASSERT_EQ(m.reflective, 0);
  91. }
  92. TEST(MaterialTest, Transparency_and_refractive_index_for_the_default_material)
  93. {
  94. Material m = Material();
  95. ASSERT_EQ(m.transparency, 0.0);
  96. ASSERT_EQ(m.refractiveIndex, 1.0);
  97. }
  98. TEST(MaterialTest, Equality_tests)
  99. {
  100. Material m = Material();
  101. Material m2 = Material();
  102. ASSERT_EQ(m, m2);
  103. m.ambient = 42;
  104. ASSERT_NE(m, m2);
  105. m.ambient = m2.ambient;
  106. m.diffuse = 42;
  107. ASSERT_NE(m, m2);
  108. m.diffuse = m2.diffuse;
  109. m.specular = 42;
  110. ASSERT_NE(m, m2);
  111. m.specular = m2.specular;
  112. m.shininess = 42;
  113. ASSERT_NE(m, m2);
  114. m.shininess = m2.shininess;
  115. m.reflective = 42;
  116. ASSERT_NE(m, m2);
  117. m.reflective = m2.reflective;
  118. m.transparency = 42;
  119. ASSERT_NE(m, m2);
  120. m.transparency = m2.transparency;
  121. m.emissive = 42;
  122. ASSERT_NE(m, m2);
  123. m.emissive = m2.emissive;
  124. m.refractiveIndex = 42;
  125. ASSERT_NE(m, m2);
  126. m.refractiveIndex = m2.refractiveIndex;
  127. m.colour = Colour(32, 32, 32);
  128. ASSERT_NE(m, m2);
  129. }
  130. TEST(MaterialTest, lighting_uses_light_intensity_to_attenuate_colour)
  131. {
  132. World w = DefaultWorld();
  133. Light *light = w.getLight(0);
  134. Shape *shape = w.getObject(0);
  135. light->position = Point(0, 0, -10);
  136. light->intensity = Colour(1, 1, 1);
  137. shape->material.ambient = 0.1;
  138. shape->material.diffuse = 0.9;
  139. shape->material.specular = 0;
  140. shape->material.colour = Colour(1, 1, 1);
  141. Tuple pt = Point(0, 0, -1);
  142. Tuple eyev = Vector(0, 0, -1);
  143. Tuple normalv = Vector(0, 0, -1);
  144. double testList[] = {
  145. 1.0,
  146. 0.5,
  147. 0.0,
  148. };
  149. Colour testResult[] = {
  150. Colour(1, 1, 1),
  151. Colour(0.55, 0.55, 0.55),
  152. Colour(0.1, 0.1, 0.1),
  153. };
  154. int testCount = sizeof(testList)/sizeof((testList)[0]);
  155. int i;
  156. for(i = 0; i < testCount; i++)
  157. {
  158. ASSERT_EQ(shape->material.lighting(*light, pt, eyev, normalv, shape, testList[i]), testResult[i]);
  159. }
  160. }