material_test.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. TEST(MaterialTest, The_default_material)
  15. {
  16. Material m = Material();
  17. ASSERT_EQ(m.colour, Colour(1, 1, 1));
  18. ASSERT_EQ(m.ambient, 0.1);
  19. ASSERT_EQ(m.diffuse, 0.9);
  20. ASSERT_EQ(m.specular, 0.9);
  21. ASSERT_EQ(m.shininess, 200.0);
  22. }
  23. // This is used by the next tests
  24. static Material m = Material();
  25. static Point position = Point(0, 0, 0);
  26. TEST(MaterialTest, Lighting_with_the_eye_between_the_light_and_the_surface)
  27. {
  28. TestShape t = TestShape();
  29. Vector eyev = Vector(0, 0, -1);
  30. Vector normalv = Vector(0, 0, -1);
  31. Light light = Light(POINT_LIGHT, Point(0, 0, -10), Colour(1, 1, 1));
  32. Colour result = m.lighting(light, position, eyev, normalv, &t);
  33. ASSERT_EQ(result, Colour(1.9, 1.9, 1.9));
  34. }
  35. TEST(MaterialTest, Lighting_with_the_eye_between_the_light_and_the_surface_eye_offset_by_45)
  36. {
  37. TestShape t = TestShape();
  38. Vector eyev = Vector(0, -sqrt(2)/2, -sqrt(2)/2);
  39. Vector normalv = Vector(0, 0, -1);
  40. Light light = Light(POINT_LIGHT, Point(0, 0, -10), Colour(1, 1, 1));
  41. Colour result = m.lighting(light, position, eyev, normalv, &t);
  42. ASSERT_EQ(result, Colour(1.0, 1.0, 1.0));
  43. }
  44. TEST(MaterialTest, Lighting_with_the_eye_opposite_surface_light_offset_45)
  45. {
  46. TestShape t = TestShape();
  47. Vector eyev = Vector(0, 0, -1);
  48. Vector normalv = Vector(0, 0, -1);
  49. Light light = Light(POINT_LIGHT, Point(0, 10, -10), Colour(1, 1, 1));
  50. Colour result = m.lighting(light, position, eyev, normalv, &t);
  51. set_equal_precision(0.0001);
  52. ASSERT_EQ(result, Colour(0.7364, 0.7364, 0.7364));
  53. set_equal_precision(FLT_EPSILON);
  54. }
  55. TEST(MaterialTest, Lighting_with_the_eye_in_the_path_of_the_reflection_vector)
  56. {
  57. TestShape t = TestShape();
  58. Vector eyev = Vector(0, -sqrt(2)/2, -sqrt(2)/2);
  59. Vector normalv = Vector(0, 0, -1);
  60. Light light = Light(POINT_LIGHT, Point(0, 10, -10), Colour(1, 1, 1));
  61. Colour result = m.lighting(light, position, eyev, normalv, &t);
  62. set_equal_precision(0.0001);
  63. ASSERT_EQ(result, Colour(1.6364, 1.6364, 1.6364));
  64. set_equal_precision(FLT_EPSILON);
  65. }
  66. TEST(MaterialTest, Lighting_with_the_light_behind_the_surface)
  67. {
  68. TestShape t = TestShape();
  69. Vector eyev = Vector(0, 0, -1);
  70. Vector normalv = Vector(0, 0, -1);
  71. Light light = Light(POINT_LIGHT, Point(0, 0, 10), Colour(1, 1, 1));
  72. Colour result = m.lighting(light, position, eyev, normalv, &t);
  73. ASSERT_EQ(result, Colour(0.1, 0.1, 0.1));
  74. }
  75. TEST(MaterialTest, Lighting_with_the_surface_in_shadow)
  76. {
  77. TestShape t = TestShape();
  78. Vector eyev = Vector(0, 0, -1);
  79. Vector normalv = Vector(0, 0, -1);
  80. Light light = Light(POINT_LIGHT, Point(0, 0, -10), Colour(1, 1, 1));
  81. bool inShadow = true;
  82. Colour result = m.lighting(light, position, eyev, normalv, &t, inShadow);
  83. ASSERT_EQ(result, Colour(0.1, 0.1, 0.1));
  84. }