123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- /*
- * DoRayMe - a quick and dirty Raytracer
- * Pattern unit tests
- *
- * Created by Manoël Trapier
- * Copyright (c) 2020 986-Studio.
- *
- */
- #include <pattern.h>
- #include <strippattern.h>
- #include <gradientpattern.h>
- #include <ringpattern.h>
- #include <checkerspattern.h>
- #include <testpattern.h>
- #include <transformation.h>
- #include <colour.h>
- #include <sphere.h>
- #include <gtest/gtest.h>
- #include <material.h>
- #include <uv_pattern.h>
- #include <uv_checkers.h>
- #include <texturemap.h>
- #ifdef ENABLE_LUA_SUPPORT
- extern "C" {
- #include <lua.h>
- #include <lauxlib.h>
- #include <lualib.h>
- }
- #include <luapattern.h>
- #endif
- Colour black = Colour(0, 0, 0);
- Colour white = Colour(1, 1, 1);
- TEST(PatternTest, Creating_a_stripe_pattern)
- {
- StripPattern p = StripPattern(white, black);
- ASSERT_EQ(p.a, white);
- ASSERT_EQ(p.b, black);
- }
- TEST(PatternTest, A_strip_pattern_is_constant_in_y)
- {
- StripPattern p = StripPattern(white, black);
- ASSERT_EQ(p.patternAt(Point(0, 0, 0)), white);
- ASSERT_EQ(p.patternAt(Point(0, 1, 0)), white);
- ASSERT_EQ(p.patternAt(Point(0, 2, 0)), white);
- }
- TEST(PatternTest, A_strip_pattern_is_constant_in_z)
- {
- StripPattern p = StripPattern(white, black);
- ASSERT_EQ(p.patternAt(Point(0, 0, 0)), white);
- ASSERT_EQ(p.patternAt(Point(0, 0, 1)), white);
- ASSERT_EQ(p.patternAt(Point(0, 0, 2)), white);
- }
- TEST(PatternTest, A_strip_pattern_alternate_in_x)
- {
- StripPattern p = StripPattern(white, black);
- ASSERT_EQ(p.patternAt(Point(0, 0, 0)), white);
- ASSERT_EQ(p.patternAt(Point(0.9, 0, 0)), white);
- ASSERT_EQ(p.patternAt(Point(1, 0, 0)), black);
- ASSERT_EQ(p.patternAt(Point(-0.1, 0, 0)), black);
- ASSERT_EQ(p.patternAt(Point(-1, 0, 0)), black);
- ASSERT_EQ(p.patternAt(Point(-1.1, 0, 0)), white);
- }
- TEST(PatternTest, Lightning_with_a_pattern_applied)
- {
- Sphere s = Sphere();
- Material m;
- StripPattern p = StripPattern(white, black);
- m.pattern = &p;
- m.ambient = 1;
- m.diffuse = 0;
- m.specular = 0;
- Tuple eyev = Vector(0, 0, -1);
- Tuple normalv = Vector(0, 0, -1);
- Light light = Light(POINT_LIGHT, Point(0, 0, -10), Colour(1, 1, 1));
- Colour c1 = m.lighting(light, Point(0, 9, 0), eyev, normalv, &s, false);
- Colour c2 = m.lighting(light, Point(1, 1, 0), eyev, normalv, &s, false);
- ASSERT_EQ(c1, Colour(1, 1, 1));
- ASSERT_EQ(c2, Colour(0, 0, 0));
- }
- TEST(PatternTest, Stripe_with_an_object_transformation)
- {
- Sphere s = Sphere();
- s.setTransform(scaling(2, 2, 2));
- StripPattern pattern = StripPattern(white, black);
- Colour c = pattern.patternAtObject(&s, Point(1.5, 0, 0));
- ASSERT_EQ(c, white);
- }
- TEST(PatternTest, Stripe_with_a_pattern_transformation)
- {
- Sphere s = Sphere();
- StripPattern pattern = StripPattern(white, black);
- pattern.setTransform(scaling(2, 2, 2));
- Colour c = pattern.patternAtObject(&s, Point(1.5, 0, 0));
- ASSERT_EQ(c, white);
- }
- TEST(PatternTest, Stripe_with_both_an_object_and_a_pattern_transformation)
- {
- Sphere s = Sphere();
- s.setTransform(scaling(2, 2, 2));
- StripPattern pattern = StripPattern(white, black);
- pattern.setTransform(translation(0.5, 0, 0));
- Colour c = pattern.patternAtObject(&s, Point(2.5, 0, 0));
- ASSERT_EQ(c, white);
- }
- TEST(PatternTest, The_default_pattern_transformation)
- {
- TestPattern pattern = TestPattern();
- ASSERT_EQ(pattern.transformMatrix, Matrix4().identity());
- }
- TEST(PatternTest, Assigning_a_transformation)
- {
- TestPattern pattern = TestPattern();
- pattern.setTransform(translation(1, 2, 3));
- ASSERT_EQ(pattern.transformMatrix, translation(1, 2, 3));
- }
- TEST(PatternTest, A_pattern_with_an_object_transformation)
- {
- Sphere s = Sphere();
- s.setTransform(scaling(2, 2, 2));
- TestPattern pattern = TestPattern();
- Colour c = pattern.patternAtObject(&s, Point(2, 3, 4));
- ASSERT_EQ(c, Colour(1, 1.5, 2));
- }
- TEST(PatternTest, A_pattern_with_a_pattern_transformation)
- {
- Sphere s = Sphere();
- TestPattern pattern = TestPattern();
- pattern.setTransform(scaling(2, 2, 2));
- Colour c = pattern.patternAtObject(&s, Point(2, 3, 4));
- ASSERT_EQ(c, Colour(1, 1.5, 2));
- }
- TEST(PatternTest, A_pattern_with_an_object_and_a_pattern_transformation)
- {
- Sphere s = Sphere();
- s.setTransform(scaling(2, 2, 2));
- TestPattern pattern = TestPattern();
- pattern.setTransform(translation(0.5, 1, 1.5));
- Colour c = pattern.patternAtObject(&s, Point(2.5, 3, 3.5));
- ASSERT_EQ(c, Colour(0.75, 0.5, 0.25));
- }
- TEST(PatternTest, A_gradient_linearly_interpolates_betweens_colors)
- {
- GradientPattern pattern = GradientPattern(white, black);
- ASSERT_EQ(pattern.patternAt(Point(0.25, 0, 0)), Colour(0.75, 0.75, 0.75));
- ASSERT_EQ(pattern.patternAt(Point(0.5, 0, 0)), Colour(0.5, 0.5, 0.5));
- ASSERT_EQ(pattern.patternAt(Point(0.75, 0, 0)), Colour(0.25, 0.25, 0.25));
- }
- 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);
- }
- #ifdef ENABLE_LUA_SUPPORT
- TEST(PatternTest, Simple_test_of_a_lua_pattern)
- {
- int error;
- LuaPattern pattern = LuaPattern(white, black);
- lua_State *L = luaL_newstate();
- luaL_openlibs(L);
- pattern.setLua(L);
- pattern.setLuaFunctionName("pat");
- error = luaL_loadstring(L, "function pat(x, y, z, a, b)\n"
- " local v = math.floor(x) + math.floor(y) + math.floor(z)\n"
- " if (v % 2) == 0 then\n"
- " return a.r, a.g, a.b\n"
- " end\n"
- " return b.r, b.g, b.b\n"
- "end");
- if (error)
- {
- fprintf(stderr, "%s\n", lua_tostring(L, -1));
- lua_pop(L, 1); /* pop error message from the stack */
- }
- ASSERT_EQ(error, 0);
- error = lua_pcall(L, 0, 0, 0);
- if (error)
- {
- fprintf(stderr, "%s\n", lua_tostring(L, -1));
- lua_pop(L, 1); /* pop error message from the stack */
- }
- ASSERT_EQ(error, 0);
- 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);
- lua_close(L);
- }
- #endif
- TEST(PatternTest, Checkers_pattern_in_2D)
- {
- UVCheckers checkers = UVCheckers(2, 2, black, white);
- ASSERT_EQ(checkers.uvPatternAt(0.0, 0.0), black);
- ASSERT_EQ(checkers.uvPatternAt(0.5, 0.0), white);
- ASSERT_EQ(checkers.uvPatternAt(0.0, 0.5), white);
- ASSERT_EQ(checkers.uvPatternAt(0.5, 0.5), black);
- ASSERT_EQ(checkers.uvPatternAt(1.0, 1.0), black);
- }
- TEST(PatternTest, Using_a_spherical_mapping_on_a_3d_point)
- {
- Point testList[] = {
- Point( 0, 0, -1),
- Point( 1, 0, 0),
- Point( 0, 0, 1),
- Point(-1, 0, 0),
- Point( 0, 1, 0),
- Point( 0, -1, 0),
- Point(sqrt(2)/2, sqrt(2)/2, 0),
- };
- double testResults[][2] {
- {0.0, 0.5},
- {0.25, 0.5},
- {0.5, 0.5},
- {0.75, 0.5},
- {0.5, 1.0},
- {0.5, 0.0},
- {0.25, 0.75},
- };
- int testCount = sizeof(testList)/sizeof((testList)[0]);
- int i;
- TextureMap tm = TextureMap(SPHERICAL_MAP, nullptr);
- for(i = 0; i < testCount; i++)
- {
- double u, v;
- tm.sphericalMap(testList[i], u, v);
- ASSERT_TRUE(double_equal(u, testResults[i][0]));
- ASSERT_TRUE(double_equal(v, testResults[i][1]));
- }
- }
- TEST(PatternTest, Using_a_texture_map_with_a_spherical_map)
- {
- Point testList[] = {
- Point( 0.4315, 0.4670, 0.7719),
- Point( -0.9654, 0.2252, -0.0534),
- Point( 0.1039, 0.7090, 0.6975),
- Point( -0.4986, -0.7856, -0.3663),
- Point( -0.0317, -0.9395, 0.3411),
- Point( 0.4809, -0.7721, 0.4154),
- Point( 0.0285, -0.9612, -0.2745),
- Point( -0.5734, -0.2162, -0.7903),
- Point( 0.7688, -0.1470, 0.6223),
- Point( -0.7652, 0.2175, 0.6060),
- };
- Colour testResults[] {
- white,
- black,
- white,
- black,
- black,
- black,
- black,
- white,
- black,
- black,
- };
- int testCount = sizeof(testList)/sizeof((testList)[0]);
- int i;
- UVCheckers uvpat = UVCheckers(16, 8, black, white);
- TextureMap tm = TextureMap(SPHERICAL_MAP, &uvpat);
- for(i = 0; i < testCount; i++)
- {
- 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]));
- }
- }
- TEST(PatternTest, Using_a_cylindrical_mapping_on_a_3d_point)
- {
- Point testList[] = {
- Point( 0.00000, 0.00, -1.00000),
- Point( 0.00000, 0.50, -1.00000),
- Point( 0.00000, 1.00, -1.00000),
- Point( 0.70711, 0.50, -0.70711),
- Point( 1.00000, 0.50, -0.00000),
- Point( 0.70711, 0.50, 0.70711),
- Point( 0.00000, -0.25, 1.00000),
- Point(-0.70711, 0.50, 0.70711),
- Point(-1.00000, 1.25, 0.00000),
- Point(-0.70711, 0.50, -0.70711),
- };
- double testResults[][2] {
- {0.000, 0.00},
- {0.000, 0.50},
- {0.000, 0.00},
- {0.125, 0.50},
- {0.250, 0.50},
- {0.375, 0.50},
- {0.500, 0.75},
- {0.625, 0.50},
- {0.750, 0.25},
- {0.875, 0.50},
- };
- int testCount = sizeof(testList)/sizeof((testList)[0]);
- int i;
- TextureMap tm = TextureMap(CYLINDRICAL_MAP, nullptr);
- for(i = 0; i < testCount; i++)
- {
- double u, v;
- tm.cylindricalMap(testList[i], u, v);
- ASSERT_TRUE(double_equal(u, testResults[i][0]));
- ASSERT_TRUE(double_equal(v, testResults[i][1]));
- }
- }
|