pattern_test.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Pattern unit tests
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <pattern.h>
  10. #include <strippattern.h>
  11. #include <gradientpattern.h>
  12. #include <ringpattern.h>
  13. #include <testpattern.h>
  14. #include <transformation.h>
  15. #include <colour.h>
  16. #include <sphere.h>
  17. #include <gtest/gtest.h>
  18. #include <material.h>
  19. Colour black = Colour(0, 0, 0);
  20. Colour white = Colour(1, 1, 1);
  21. TEST(PatternTest, Creating_a_stripe_pattern)
  22. {
  23. StripPattern p = StripPattern(white, black);
  24. ASSERT_EQ(p.a, white);
  25. ASSERT_EQ(p.b, black);
  26. }
  27. TEST(PatternTest, A_strip_pattern_is_constant_in_y)
  28. {
  29. StripPattern p = StripPattern(white, black);
  30. ASSERT_EQ(p.patternAt(Point(0, 0, 0)), white);
  31. ASSERT_EQ(p.patternAt(Point(0, 1, 0)), white);
  32. ASSERT_EQ(p.patternAt(Point(0, 2, 0)), white);
  33. }
  34. TEST(PatternTest, A_strip_pattern_is_constant_in_z)
  35. {
  36. StripPattern p = StripPattern(white, black);
  37. ASSERT_EQ(p.patternAt(Point(0, 0, 0)), white);
  38. ASSERT_EQ(p.patternAt(Point(0, 0, 1)), white);
  39. ASSERT_EQ(p.patternAt(Point(0, 0, 2)), white);
  40. }
  41. TEST(PatternTest, A_strip_pattern_alternate_in_x)
  42. {
  43. StripPattern p = StripPattern(white, black);
  44. ASSERT_EQ(p.patternAt(Point(0, 0, 0)), white);
  45. ASSERT_EQ(p.patternAt(Point(0.9, 0, 0)), white);
  46. ASSERT_EQ(p.patternAt(Point(1, 0, 0)), black);
  47. ASSERT_EQ(p.patternAt(Point(-0.1, 0, 0)), black);
  48. ASSERT_EQ(p.patternAt(Point(-1, 0, 0)), black);
  49. ASSERT_EQ(p.patternAt(Point(-1.1, 0, 0)), white);
  50. }
  51. TEST(PatternTest, Lightning_with_a_pattern_applied)
  52. {
  53. Sphere s = Sphere();
  54. Material m;
  55. StripPattern p = StripPattern(white, black);
  56. m.pattern = &p;
  57. m.ambient = 1;
  58. m.diffuse = 0;
  59. m.specular = 0;
  60. Tuple eyev = Vector(0, 0, -1);
  61. Tuple normalv = Vector(0, 0, -1);
  62. Light light = Light(POINT_LIGHT, Point(0, 0, -10), Colour(1, 1, 1));
  63. Colour c1 = m.lighting(light, Point(0, 9, 0), eyev, normalv, &s, false);
  64. Colour c2 = m.lighting(light, Point(1, 1, 0), eyev, normalv, &s, false);
  65. ASSERT_EQ(c1, Colour(1, 1, 1));
  66. ASSERT_EQ(c2, Colour(0, 0, 0));
  67. }
  68. TEST(PatternTest, Stripe_with_an_object_transformation)
  69. {
  70. Sphere s = Sphere();
  71. s.setTransform(scaling(2, 2, 2));
  72. StripPattern pattern = StripPattern(white, black);
  73. Colour c = pattern.patternAtObject(&s, Point(1.5, 0, 0));
  74. ASSERT_EQ(c, white);
  75. }
  76. TEST(PatternTest, Stripe_with_a_pattern_transformation)
  77. {
  78. Sphere s = Sphere();
  79. StripPattern pattern = StripPattern(white, black);
  80. pattern.setTransform(scaling(2, 2, 2));
  81. Colour c = pattern.patternAtObject(&s, Point(1.5, 0, 0));
  82. ASSERT_EQ(c, white);
  83. }
  84. TEST(PatternTest, Stripe_with_both_an_object_and_a_pattern_transformation)
  85. {
  86. Sphere s = Sphere();
  87. s.setTransform(scaling(2, 2, 2));
  88. StripPattern pattern = StripPattern(white, black);
  89. pattern.setTransform(translation(0.5, 0, 0));
  90. Colour c = pattern.patternAtObject(&s, Point(2.5, 0, 0));
  91. ASSERT_EQ(c, white);
  92. }
  93. TEST(PatternTest, The_default_pattern_transformation)
  94. {
  95. TestPattern pattern = TestPattern();
  96. ASSERT_EQ(pattern.transformMatrix, Matrix4().identity());
  97. }
  98. TEST(PatternTest, Assigning_a_transformation)
  99. {
  100. TestPattern pattern = TestPattern();
  101. pattern.setTransform(translation(1, 2, 3));
  102. ASSERT_EQ(pattern.transformMatrix, translation(1, 2, 3));
  103. }
  104. TEST(PatternTest, A_pattern_with_an_object_transformation)
  105. {
  106. Sphere s = Sphere();
  107. s.setTransform(scaling(2, 2, 2));
  108. TestPattern pattern = TestPattern();
  109. Colour c = pattern.patternAtObject(&s, Point(2, 3, 4));
  110. ASSERT_EQ(c, Colour(1, 1.5, 2));
  111. }
  112. TEST(PatternTest, A_pattern_with_a_pattern_transformation)
  113. {
  114. Sphere s = Sphere();
  115. TestPattern pattern = TestPattern();
  116. pattern.setTransform(scaling(2, 2, 2));
  117. Colour c = pattern.patternAtObject(&s, Point(2, 3, 4));
  118. ASSERT_EQ(c, Colour(1, 1.5, 2));
  119. }
  120. TEST(PatternTest, A_pattern_with_an_object_and_a_pattern_transformation)
  121. {
  122. Sphere s = Sphere();
  123. s.setTransform(scaling(2, 2, 2));
  124. TestPattern pattern = TestPattern();
  125. pattern.setTransform(translation(0.5, 1, 1.5));
  126. Colour c = pattern.patternAtObject(&s, Point(2.5, 3, 3.5));
  127. ASSERT_EQ(c, Colour(0.75, 0.5, 0.25));
  128. }
  129. TEST(PatternTest, A_gradient_linearly_interpolates_betweens_colors)
  130. {
  131. GradientPattern pattern = GradientPattern(white, black);
  132. ASSERT_EQ(pattern.patternAt(Point(0.25, 0, 0)), Colour(0.75, 0.75, 0.75));
  133. ASSERT_EQ(pattern.patternAt(Point(0.5, 0, 0)), Colour(0.5, 0.5, 0.5));
  134. ASSERT_EQ(pattern.patternAt(Point(0.75, 0, 0)), Colour(0.25, 0.25, 0.25));
  135. }
  136. TEST(PatternTest, A_ring_should_extend_in_both_x_and_z)
  137. {
  138. RingPattern pattern = RingPattern(white, black);
  139. }