123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- /*
- * DoRayMe - a quick and dirty Raytracer
- * Light unit tests
- *
- * Created by Manoël Trapier
- * Copyright (c) 2020 986-Studio.
- *
- */
- #include <light.h>
- #include <math.h>
- #include <math_helper.h>
- #include <colour.h>
- #include <tuple.h>
- #include <gtest/gtest.h>
- #include <world.h>
- #include <worldbuilder.h>
- TEST(LightTest, A_point_lighthas_a_position_and_intensity)
- {
- Colour intensity = Colour(1, 1, 1);
- Point position = Point(0, 0, 0);
- Light light = Light(POINT_LIGHT, position, intensity);
- ASSERT_EQ(light.position, position);
- ASSERT_EQ(light.intensity, intensity);
- }
- TEST(LightTest, Point_lights_evaluate_the_lights_intensity_at_a_given_point)
- {
- World w = DefaultWorld();
- Light *light = w.getLight(0);
- Tuple testList[] = {
- Point(0, 1.0001, 0),
- Point(-1.0001, 0, 0),
- Point(0, 0, -1.0001),
- Point(0, 0, 1.0001),
- Point(1.0001, 0, 0),
- Point(0, -1.0001, 0),
- Point(0, 0, 0),
- };
- double testResult[] = {
- 1.0,
- 1.0,
- 1.0,
- 0.0,
- 0.0,
- 0.0,
- 0.0,
- };
- int testCount = sizeof(testList)/sizeof((testList)[0]);
- int i;
- for(i = 0; i < testCount; i++)
- {
- ASSERT_TRUE(double_equal(light->intensityAt(w, testList[i]), testResult[i]));
- }
- }
- TEST(LightTest, Creating_an_area_light)
- {
- Tuple corner = Point(0, 0, 0);
- Tuple v1 = Vector(2, 0, 0);
- Tuple v2 = Vector(0, 0, 1);
- Light light = Light(AREA_LIGHT, corner, v1, 4, v2, 2, Colour(1, 1, 1));
- /* Position is used to store the corner in area lights */
- ASSERT_EQ(light.corner, corner);
- ASSERT_EQ(light.uVec, Vector(0.5, 0, 0));
- ASSERT_EQ(light.uSteps, 4);
- ASSERT_EQ(light.vVec, Vector(0, 0, 0.5));
- ASSERT_EQ(light.vSteps, 2);
- ASSERT_EQ(light.samples, 8);
- ASSERT_EQ(light.position, Point(1, 0, 0.5));
- }
- TEST(LightTest, Finding_a_single_point_on_an_area_light)
- {
- Tuple corner = Point(0, 0, 0);
- Tuple v1 = Vector(2, 0, 0);
- Tuple v2 = Vector(0, 0, 1);
- Light light = Light(AREA_LIGHT, corner, v1, 4, v2, 2, Colour(1, 1, 1));
- uint32_t testList[][2] = {
- {0, 0},
- {1, 0},
- {0, 1},
- {2, 0},
- {3, 1},
- };
- Point testResults[] {
- Point(0.25, 0, 0.25),
- Point(0.75, 0, 0.25),
- Point(0.25, 0, 0.75),
- Point(1.25, 0, 0.25),
- Point(1.75, 0, 0.75),
- };
- int testCount = sizeof(testList)/sizeof((testList)[0]);
- int i;
- for(i = 0; i < testCount; i++)
- {
- Tuple tp = light.pointOnLight(testList[i][0], testList[i][1]);
- ASSERT_EQ(tp, testResults[i]);
- }
- }
- TEST(LightTest, The_area_light_intensity_function)
- {
- World w = DefaultWorld();
- Tuple corner = Point(-0.5, -0.5, -5);
- Tuple v1 = Vector(1, 0, 0);
- Tuple v2 = Vector(0, 1, 0);
- Light light = Light(AREA_LIGHT, corner, v1, 2, v2, 2, Colour(1, 1, 1));
- Point testList[] = {
- Point(0, 0, 2),
- Point(1, -1, 2),
- Point(1.5, 0, 2),
- Point(1.25, 1.25, 3),
- Point(0, 0, -2),
- };
- double testResults[] {
- 0.0,
- 0.25,
- 0.5,
- 0.75,
- 1,
- };
- int testCount = sizeof(testList)/sizeof((testList)[0]);
- int i;
- for(i = 0; i < testCount; i++)
- {
- double intensity = light.intensityAt(w, testList[i]);
- ASSERT_TRUE(double_equal(intensity, testResults[i]));
- }
- }
- #if 0
- /* This test is not reliable */
- TEST(LightTest, Finding_a_single_point_on_a_jittered_area_light)
- {
- Tuple corner = Point(0, 0, 0);
- Tuple v1 = Vector(2, 0, 0);
- Tuple v2 = Vector(0, 0, 1);
- Light light = Light(AREA_LIGHT, corner, v1, 4, v2, 2, Colour(1, 1, 1), true);
- double seqList[] = { 0.3, 0.7 };
- light.jitterBy = Sequence(seqList, 2);
- uint32_t testList[][2] = {
- {0, 0},
- {1, 0},
- {0, 1},
- {2, 0},
- {3, 1},
- };
- Point testResults[] {
- Point(0.15, 0, 0.35),
- Point(0.65, 0, 0.35),
- Point(0.15, 0, 0.85),
- Point(1.15, 0, 0.35),
- Point(1.65, 0, 0.85),
- };
- int testCount = sizeof(testList)/sizeof((testList)[0]);
- int i;
- for(i = 0; i < testCount; i++)
- {
- Tuple tp = light.pointOnLight(testList[i][0], testList[i][1]);
- ASSERT_EQ(tp, testResults[i]);
- }
- }
- #endif
- TEST(LightTest, The_area_light_with_jittered_samples)
- {
- World w = DefaultWorld();
- Tuple corner = Point(-0.5, -0.5, -5);
- Tuple v1 = Vector(1, 0, 0);
- Tuple v2 = Vector(0, 1, 0);
- Light light = Light(AREA_LIGHT, corner, v1, 2, v2, 2, Colour(1, 1, 1), true);
- double seqList[] = { 0.7, 0.3, 0.9, 0.1, 0.5 };
- light.jitterBy = Sequence(seqList, 5);
- Point testList[] = {
- Point(0, 0, 2),
- Point(1, -1, 2),
- Point(1.5, 0, 2),
- Point(1.25, 1.25, 3),
- Point(0, 0, -2),
- };
- double testResults[] {
- 0.0,
- 0.25, /* Chapter say 0.5 but it's not what I get here ?! */
- 0.75,
- 0.75,
- 1,
- };
- int testCount = sizeof(testList)/sizeof((testList)[0]);
- int i;
- for(i = 0; i < testCount; i++)
- {
- double intensity = light.intensityAt(w, testList[i]);
- EXPECT_TRUE(double_equal(intensity, testResults[i]));
- }
- }
|