diff --git a/README.md b/README.md index 758ec2c..6923803 100644 --- a/README.md +++ b/README.md @@ -84,4 +84,7 @@ With jitter: **Bonus chapter - Texture mapping** Spherical mapping: -![Spherical mapping](output/uvmap_checkeredsphere.png) \ No newline at end of file +![Spherical mapping](output/uvmap_checkeredsphere.png) + +Planar mapping: +![Planar mapping](output/uvmap_checkeredplane.png) \ No newline at end of file diff --git a/output/uvmap_checkeredplane.png b/output/uvmap_checkeredplane.png new file mode 100644 index 0000000..2df5284 Binary files /dev/null and b/output/uvmap_checkeredplane.png differ diff --git a/source/pattern/texturemap.h b/source/pattern/texturemap.h index 832d1ca..0daf3ae 100644 --- a/source/pattern/texturemap.h +++ b/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); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5a92534..e080d50 100644 --- a/tests/CMakeLists.txt +++ b/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 $) add_test(NAME Chapter06_Test COMMAND $) add_test(NAME Chapter07_Test COMMAND $) @@ -91,6 +94,7 @@ add_test(NAME Chapter13_ConeBonus COMMAND $) add_test(NAME Chapter14_Test COMMAND $) add_test(NAME AreaLight_Test COMMAND $) add_test(NAME UVMap_CheckeredSphere COMMAND $) +add_test(NAME UVMap_CheckeredPlane COMMAND $) add_test(NAME Test_Rendering COMMAND $) add_test(NAME Triangle_RenderTest COMMAND $) add_test(NAME ChristmasBall_Rendering COMMAND $) diff --git a/tests/pattern_test.cpp b/tests/pattern_test.cpp index 9f9d529..3878ca2 100644 --- a/tests/pattern_test.cpp +++ b/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])); + } } \ No newline at end of file diff --git a/tests/uvmap_checkeredplane.cpp b/tests/uvmap_checkeredplane.cpp new file mode 100644 index 0000000..ee2b356 --- /dev/null +++ b/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 +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +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; +} \ No newline at end of file