Browse Source

Add AlignCheck UV Pattern

Godzil 4 years ago
parent
commit
f5685a45e1

+ 4 - 1
README.md

@@ -90,4 +90,7 @@ Planar mapping:
 ![Planar mapping](output/uvmap_checkeredplane.png)
 
 Cylindrical mapping:
-![Cylindrical mapping](output/uvmap_checkeredcylinder.png)
+![Cylindrical mapping](output/uvmap_checkeredcylinder.png)
+
+Aligncheck plane:
+![Aligncheck plane](output/uvmap_aligncheckplane.png)

BIN
output/uvmap_aligncheckplane.png


+ 39 - 0
source/uvpattern/uv_aligncheck.h

@@ -0,0 +1,39 @@
+/*
+ *  DoRayMe - a quick and dirty Raytracer
+ *  UV Align Check test pattern header
+ *
+ *  Created by Manoël Trapier
+ *  Copyright (c) 2020 986-Studio.
+ *
+ */
+#ifndef DORAYME_UV_ALIGNCHECK_H
+#define DORAYME_UV_ALIGNCHECK_H
+
+class UVAlignCheck : public UVPattern
+{
+public:
+    Colour ul, ur, bl, br;
+
+    UVAlignCheck(Colour main, Colour ul, Colour ur, Colour bl, Colour br) : UVPattern(1, 1, main, main),
+                                                                            ul(ul), ur(ur), bl(bl), br(br) {};
+
+    Colour uvPatternAt(double u, double v) {
+        /* Remember that v=0 is  at the bottom, v=1 at the top */
+        if (v > 0.8)
+        {
+            if (u < 0.2) { return this->ul; }
+            if (u > 0.8) { return this->ur; }
+        }
+        else if (v < 0.2)
+        {
+            if (u < 0.2) { return this->bl; }
+            if (u > 0.8) { return this->br; }
+        }
+
+        /* main is stored in A or B */
+        return this->a;
+    };
+};
+
+
+#endif /* DORAYME_UV_ALIGNCHECK_H */

+ 4 - 0
tests/CMakeLists.txt

@@ -83,6 +83,9 @@ target_sources(uvmap_checkeredplane PRIVATE uvmap_checkeredplane.cpp)
 add_executable(uvmap_checkeredcylinder)
 target_sources(uvmap_checkeredcylinder PRIVATE uvmap_checkeredcylinder.cpp)
 
+add_executable(uvmap_aligncheckplane)
+target_sources(uvmap_aligncheckplane PRIVATE uvmap_aligncheckplane.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>)
@@ -99,6 +102,7 @@ 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 UVMap_CheckeredCylinder COMMAND $<TARGET_FILE:uvmap_checkeredcylinder>)
+add_test(NAME UVMap_AlignCheckPlane COMMAND $<TARGET_FILE:uvmap_aligncheckplane>)
 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>)

+ 37 - 0
tests/pattern_test.cpp

@@ -20,6 +20,7 @@
 #include <material.h>
 #include <uv_pattern.h>
 #include <uv_checkers.h>
+#include <uv_aligncheck.h>
 #include <texturemap.h>
 
 #ifdef ENABLE_LUA_SUPPORT
@@ -429,4 +430,40 @@ TEST(PatternTest, Using_a_cylindrical_mapping_on_a_3d_point)
         ASSERT_TRUE(double_equal(u, testResults[i][0]));
         ASSERT_TRUE(double_equal(v, testResults[i][1]));
     }
+}
+
+TEST(PatternTest, Layout_of_the_align_check_pattern)
+{
+    Colour main = Colour(1, 1, 1);
+    Colour ul = Colour(1, 0, 0);
+    Colour ur = Colour(1, 1, 0);
+    Colour bl = Colour(0, 1, 0);
+    Colour br = Colour(0, 1, 1);
+
+    double testList[][2] = {
+            { 0.5, 0.5 },
+            { 0.1, 0.9 },
+            { 0.9, 0.9 },
+            { 0.1, 0.1 },
+            { 0.9, 0.1 },
+    };
+
+    Colour testResults[] {
+          main,
+          ul,
+          ur,
+          bl,
+          br,
+    };
+
+    int testCount = sizeof(testList)/sizeof((testList)[0]);
+    int i;
+
+    UVAlignCheck uvac = UVAlignCheck(main, ul, ur, bl, br);
+
+    for(i = 0; i < testCount; i++)
+    {
+        Colour ret = uvac.uvPatternAt(testList[i][0], testList[i][1]);
+        ASSERT_EQ(ret, testResults[i]);
+    }
 }

+ 58 - 0
tests/uvmap_aligncheckplane.cpp

@@ -0,0 +1,58 @@
+/*
+ *  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 <uv_aligncheck.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();
+    UVAlignCheck checkers = UVAlignCheck(Colour(1, 1, 1),
+                                         Colour(1, 0, 0),
+                                         Colour(1, 1, 0),
+                                         Colour(0, 1, 0),
+                                         Colour(0, 1, 1)
+                                         );
+    TextureMap tm = TextureMap(PLANAR_MAP, &checkers);
+    sp.material.pattern = &tm;
+    sp.material.ambient = 0.1;
+    sp.material.diffuse = 0.8;
+
+    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_aligncheckplane.png");
+
+    return 0;
+}