Browse Source

Finishing touch for patterns!

Godzil 4 years ago
parent
commit
7687581e83

+ 5 - 1
README.md

@@ -32,4 +32,8 @@ From Chapter 08:
 
 From Chapter 09:
 
-![Chapter 9 rendering test](output/ch9_test.png)
+![Chapter 9 rendering test](output/ch9_test.png)
+
+From Chapter 10:
+
+![Chapter 10 rendering test](output/ch10_test.png)

BIN
output/ch10_test.png


+ 2 - 2
source/CMakeLists.txt

@@ -3,12 +3,12 @@
 # First most is build as a library
 add_library(rayonnement STATIC)
 
-file(GLOB RAY_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)
+file(GLOB RAY_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h ${CMAKE_CURRENT_SOURCE_DIR}/pattern/*.h)
 
 file(GLOB RAY_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/shapes/*.cpp
                       ${CMAKE_CURRENT_SOURCE_DIR}/worldbuilder/*.cpp)
 
-target_include_directories(rayonnement PUBLIC include)
+target_include_directories(rayonnement PUBLIC include pattern)
 target_sources(rayonnement PRIVATE ${RAY_HEADERS} ${RAY_SOURCES})
 target_link_libraries(rayonnement LodePNG)
 

+ 25 - 0
source/pattern/checkerspattern.h

@@ -0,0 +1,25 @@
+/*
+ *  DoRayMe - a quick and dirty Raytracer
+ *  Checkers Pattern header
+ *
+ *  Created by Manoël Trapier
+ *  Copyright (c) 2020 986-Studio.
+ *
+ */
+#ifndef DORAYME_CHECKERSPATTERN_H
+#define DORAYME_CHECKERSPATTERN_H
+
+class CheckersPattern : public Pattern
+{
+public:
+    CheckersPattern(Colour a, Colour b) : Pattern(a, b) { };
+
+    Colour patternAt(Tuple point)
+    {
+        double value = floor(point.x) + floor(point.y) + floor(point.z);
+
+        return (fmod(value, 2) == 0)?this->a:this->b;
+    }
+};
+
+#endif /* DORAYME_CHECKERSPATTERN_H */

+ 1 - 1
source/include/gradientpattern.h → source/pattern/gradientpattern.h

@@ -27,4 +27,4 @@ public:
     }
 };
 
-#endif //DORAYME_GRADIENTPATTERN_H
+#endif /* DORAYME_GRADIENTPATTERN_H */

+ 4 - 5
source/include/ringpattern.h → source/pattern/ringpattern.h

@@ -18,14 +18,13 @@ public:
 
     Colour patternAt(Tuple point)
     {
-        Tuple distance = this->b - this->a;
-        double fraction = point.x - floor(point.x);
+        double squared = (point.x * point.x) + (point.z * point.z);
 
-        Tuple ret = this->a + distance * fraction;
+        double value = floor(sqrt(squared));
 
-        return Colour(ret.x, ret.y, ret.z);
+        return (fmod(value, 2) == 0)?this->a:this->b;
     }
 };
 
 
-#endif //DORAYME_RINGSUPPORT_H
+#endif /* DORAYME_RINGSUPPORT_H */

+ 0 - 0
source/include/strippattern.h → source/pattern/strippattern.h


+ 1 - 1
source/include/testpattern.h → source/pattern/testpattern.h

@@ -24,4 +24,4 @@ public:
     }
 };
 
-#endif //DORAYME_TESTPATTERN_H
+#endif /* DORAYME_TESTPATTERN_H */

+ 13 - 3
tests/ch10_test.cpp

@@ -18,6 +18,8 @@
 #include <pattern.h>
 #include <strippattern.h>
 #include <gradientpattern.h>
+#include <checkerspattern.h>
+#include <ringpattern.h>
 
 #include <transformation.h>
 
@@ -26,7 +28,7 @@ int main()
     /* First we need to construct the world */
     Plane floor = Plane();
     floor.material.specular = 0;
-    floor.material.pattern = new StripPattern(Colour(1, 0.9, 0.9), Colour(1, 0.2, 0.2));
+    floor.material.pattern = new RingPattern(Colour(1, 0.9, 0.9), Colour(1, 0.2, 0.2));
 
     Plane wall = Plane();
     wall.material.specular = 0;
@@ -35,7 +37,7 @@ int main()
     wall.setTransform(translation(0, 0, 5) * rotationX(M_PI/2));
 
     Sphere middle = Sphere();
-    middle.setTransform(translation(-0.5, 1, 0.5));
+    middle.setTransform(translation(-0.7, 1, 0.6));
     middle.material.diffuse = 0.7;
     middle.material.specular = 0.3;
     middle.material.pattern = new StripPattern(Colour(0.1, 1, 0.5), Colour(0, 0.2, 0.2));
@@ -55,6 +57,13 @@ int main()
     left.material.pattern = new GradientPattern(Colour(1, 0.8, 0.1), Colour(0.1, 0.1, 1));
     left.material.pattern->setTransform(translation(1.5, 0, 0) * scaling(2.1, 2, 2) * rotationY(-M_PI/4));
 
+    Sphere fourth = Sphere();
+    fourth.setTransform(translation(.5, 0.25, 0.4) * scaling(0.3, 0.3, 0.3));
+    fourth.material.diffuse = 0.7;
+    fourth.material.specular = 0.3;
+    fourth.material.pattern = new CheckersPattern(Colour(0.1, 0.8, 0.1), Colour(0.8, 1, 0.8));
+    fourth.material.pattern->setTransform( scaling(0.2, 0.2, 0.2));
+
     World w = World();
 
     w.addObject(&floor);
@@ -62,6 +71,7 @@ int main()
     w.addObject(&middle);
     w.addObject(&left);
     w.addObject(&right);
+    w.addObject(&fourth);
 
     /* Add light */
     Light light = Light(POINT_LIGHT, Point(-10, 10, -10), Colour(1, 1, 1));
@@ -69,7 +79,7 @@ int main()
     w.addLight(&light);
 
     /* Set the camera */
-    Camera camera = Camera(1920, 1080, M_PI / 3);
+    Camera camera = Camera(100, 50, M_PI / 3);
     camera.setTransform(viewTransform(Point(0, 1.5, -5),
                                       Point(0, 1, 0),
                                       Vector(0, 1, 0)));

+ 1 - 1
tests/ch9_test.cpp

@@ -54,7 +54,7 @@ int main()
     w.addLight(&light);
 
     /* Set the camera */
-    Camera camera = Camera(1000, 500, M_PI / 3);
+    Camera camera = Camera(100, 50, M_PI / 3);
     camera.setTransform(viewTransform(Point(0, 1.5, -5),
                                       Point(0, 1, 0),
                                       Vector(0, 1, 0)));

+ 34 - 0
tests/pattern_test.cpp

@@ -11,6 +11,7 @@
 #include <strippattern.h>
 #include <gradientpattern.h>
 #include <ringpattern.h>
+#include <checkerspattern.h>
 #include <testpattern.h>
 #include <transformation.h>
 #include <colour.h>
@@ -174,4 +175,37 @@ TEST(PatternTest, A_ring_should_extend_in_both_x_and_z)
 {
     RingPattern pattern = RingPattern(white, black);
 
+    ASSERT_EQ(pattern.patternAt(Point(0, 0, 0)), white);
+    ASSERT_EQ(pattern.patternAt(Point(1, 0, 0)), black);
+    ASSERT_EQ(pattern.patternAt(Point(0, 0, 1)), black);
+
+    /* 0.708 is just bit more than sqrt(2)/2 */
+    ASSERT_EQ(pattern.patternAt(Point(0.708, 0, 0.708)), black);
+}
+
+TEST(PatternTest, Checkers_should_repeat_in_x)
+{
+    CheckersPattern pattern = CheckersPattern(white, black);
+
+    ASSERT_EQ(pattern.patternAt(Point(0, 0, 0)), white);
+    ASSERT_EQ(pattern.patternAt(Point(0.99, 0, 0)), white);
+    ASSERT_EQ(pattern.patternAt(Point(1.01, 0, 0)), black);
+}
+
+TEST(PatternTest, Checkers_should_repeat_in_y)
+{
+    CheckersPattern pattern = CheckersPattern(white, black);
+
+    ASSERT_EQ(pattern.patternAt(Point(0, 0, 0)), white);
+    ASSERT_EQ(pattern.patternAt(Point(0, 0.99, 0)), white);
+    ASSERT_EQ(pattern.patternAt(Point(0, 1.01, 0)), black);
 }
+
+TEST(PatternTest, Checkers_should_repeat_in_z)
+{
+    CheckersPattern pattern = CheckersPattern(white, black);
+
+    ASSERT_EQ(pattern.patternAt(Point(0, 0, 0)), white);
+    ASSERT_EQ(pattern.patternAt(Point(0, 0, 0.99)), white);
+    ASSERT_EQ(pattern.patternAt(Point(0, 0, 1.01)), black);
+}