Browse Source

Added planar mapping

Godzil 4 years ago
parent
commit
107b612130

+ 4 - 1
README.md

@@ -84,4 +84,7 @@ With jitter:
 **Bonus chapter - Texture mapping**
 
 Spherical mapping:
-![Spherical mapping](output/uvmap_checkeredsphere.png)
+![Spherical mapping](output/uvmap_checkeredsphere.png)
+
+Planar mapping:
+![Planar mapping](output/uvmap_checkeredplane.png)

BIN
output/uvmap_checkeredplane.png


+ 8 - 0
source/pattern/texturemap.h

@@ -66,6 +66,11 @@ public:
         v = 1 - phi / M_PI;
     }
 
+    static void planarMap(Tuple point, double &u, double &v) {
+        u = fmod(point.x, 1);
+        v = fmod(point.z, 1);
+    }
+
     Colour patternAt(Tuple point)
     {
         double u,v;
@@ -75,6 +80,9 @@ public:
         case SPHERICAL_MAP:
             this->sphericalMap(point, u, v);
             break;
+        case PLANAR_MAP:
+            this->planarMap(point, u, v);
+            break;
         }
 
         return this->pattern->uvPatternAt(u, v);

+ 4 - 0
tests/CMakeLists.txt

@@ -77,6 +77,9 @@ target_sources(christmasball_render PRIVATE christmasball_render.cpp)
 add_executable(uvmap_checkeredsphere)
 target_sources(uvmap_checkeredsphere PRIVATE uvmap_checkeredsphere.cpp)
 
+add_executable(uvmap_checkeredplane)
+target_sources(uvmap_checkeredplane PRIVATE uvmap_checkeredplane.cpp)
+
 add_test(NAME Chapter05_Test COMMAND $<TARGET_FILE:ch5_test>)
 add_test(NAME Chapter06_Test COMMAND $<TARGET_FILE:ch6_test>)
 add_test(NAME Chapter07_Test COMMAND $<TARGET_FILE:ch7_test>)
@@ -91,6 +94,7 @@ add_test(NAME Chapter13_ConeBonus COMMAND $<TARGET_FILE:ch13_cone>)
 add_test(NAME Chapter14_Test COMMAND $<TARGET_FILE:ch14_test>)
 add_test(NAME AreaLight_Test COMMAND $<TARGET_FILE:arealight_test>)
 add_test(NAME UVMap_CheckeredSphere COMMAND $<TARGET_FILE:uvmap_checkeredsphere>)
+add_test(NAME UVMap_CheckeredPlane COMMAND $<TARGET_FILE:uvmap_checkeredplane>)
 add_test(NAME Test_Rendering COMMAND $<TARGET_FILE:test_render>)
 add_test(NAME Triangle_RenderTest COMMAND $<TARGET_FILE:triangle_rendertest>)
 add_test(NAME ChristmasBall_Rendering COMMAND $<TARGET_FILE:christmasball_render>)

+ 36 - 0
tests/pattern_test.cpp

@@ -351,4 +351,40 @@ TEST(PatternTest, Using_a_texture_map_with_a_spherical_map)
         Colour ret = tm.patternAt(testList[i]);
         EXPECT_EQ(ret, testResults[i]);
     }
+}
+
+TEST(PatternTest, Using_a_planar_mapping_on_a_3d_point)
+{
+    Point testList[] = {
+            Point(0.25, 0.0, 0.5),
+            Point(0.25, 0.0, -0.25),
+            Point(0.25, 0.5, -0.25),
+            Point(1.25, 0.0,  0.5),
+            Point(0.25, 0.0, -1.75),
+            Point(1.00, 0.0, -1.0),
+            Point(0.00, 0.0, 0.0),
+    };
+
+    double testResults[][2] {
+            {0.25, 0.50},
+            {0.25, 0.75},
+            {0.25, 0.75},
+            {0.25, 0.50},
+            {0.25, 0.25},
+            {0.00, 0.00},
+            {0.00, 0.00},
+    };
+
+    int testCount = sizeof(testList)/sizeof((testList)[0]);
+    int i;
+
+    TextureMap tm = TextureMap(PLANAR_MAP, nullptr);
+
+    for(i = 0; i < testCount; i++)
+    {
+        double u, v;
+        tm.planarMap(testList[i], u, v);
+        ASSERT_TRUE(double_equal(u, testResults[i][0]));
+        ASSERT_TRUE(double_equal(v, testResults[i][1]));
+    }
 }

+ 53 - 0
tests/uvmap_checkeredplane.cpp

@@ -0,0 +1,53 @@
+/*
+ *  DoRayMe - a quick and dirty Raytracer
+ *  Render test for chapter 10
+ *
+ *  Created by Manoël Trapier
+ *  Copyright (c) 2020 986-Studio.
+ *
+ */
+#include <world.h>
+#include <light.h>
+#include <sphere.h>
+#include <plane.h>
+#include <material.h>
+#include <colour.h>
+#include <canvas.h>
+#include <camera.h>
+
+#include <pattern.h>
+#include <texturemap.h>
+#include <uv_checkers.h>
+
+#include <transformation.h>
+
+int main()
+{
+    World w = World();
+
+    Light light = Light(POINT_LIGHT, Point(-10, 10, -10), Colour(1, 1, 1));
+    w.addLight(&light);
+
+    Plane sp = Plane();
+    UVCheckers checkers = UVCheckers(2, 2, Colour(0, 0.5, 0), Colour(1, 1, 1));
+    TextureMap tm = TextureMap(PLANAR_MAP, &checkers);
+    sp.material.pattern = &tm;
+    sp.material.ambient = 0.1;
+    sp.material.specular = 0;
+    sp.material.diffuse = 0.9;
+
+    w.addObject(&sp);
+
+    /* Set the camera */
+    Camera camera = Camera(400, 400, 0.5);
+    camera.setTransform(viewTransform(Point(1, 2, -5),
+                                      Point(0, 0, 0),
+                                      Vector(0, 1, 0)));
+
+    /* Now render it */
+    Canvas image = camera.render(w);
+
+    image.SaveAsPNG("uvmap_checkeredplane.png");
+
+    return 0;
+}