Add 3 new type of object: Sphere, Object and Intersect.
Add Intersect object as a way to report where a ray intersect another object and which one it is. Add an Object base class for all object that can be rendered. Add the Sphere object.
This commit is contained in:
@@ -3,7 +3,8 @@ project(DoRayTested)
|
||||
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
set(TESTS_SRC tuple_test.cpp colour_test.cpp canvas_test.cpp matrix_test.cpp transformation_test.cpp ray_test.cpp)
|
||||
set(TESTS_SRC tuple_test.cpp colour_test.cpp canvas_test.cpp matrix_test.cpp transformation_test.cpp ray_test.cpp
|
||||
intersect_test.cpp sphere_test.cpp)
|
||||
|
||||
add_executable(testMyRays)
|
||||
target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
|
||||
|
||||
51
tests/intersect_test.cpp
Normal file
51
tests/intersect_test.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* DoRayMe - a quick and dirty Raytracer
|
||||
* Intersect unit tests
|
||||
*
|
||||
* Created by Manoël Trapier
|
||||
* Copyright (c) 2020 986-Studio.
|
||||
*
|
||||
*/
|
||||
#include <intersect.h>
|
||||
#include <sphere.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
|
||||
TEST(IntersectTest, Creating_an_intersect_and_do_some_check)
|
||||
{
|
||||
Intersect i;
|
||||
|
||||
ASSERT_EQ(i.count(), 0);
|
||||
|
||||
i.add(newIntersection(1.0, nullptr));
|
||||
i.add(newIntersection(4.2, nullptr));
|
||||
|
||||
ASSERT_EQ(i.count(), 2);
|
||||
|
||||
ASSERT_EQ(i[0]->t, 1.0);
|
||||
ASSERT_EQ(i[1]->t, 4.2);
|
||||
}
|
||||
|
||||
TEST(IntersectTest, An_intersection_encapsulate_t_and_object)
|
||||
{
|
||||
Sphere s = Sphere();
|
||||
Intersection *i = newIntersection(3.5, &s);
|
||||
|
||||
ASSERT_EQ(i->t, 3.5);
|
||||
ASSERT_EQ(i->object, (Object *)&s);
|
||||
}
|
||||
|
||||
TEST(IntersectTest, Aggregating_intersections)
|
||||
{
|
||||
Sphere s = Sphere();
|
||||
Intersection *i1 = newIntersection(1, &s);
|
||||
Intersection *i2 = newIntersection(2, &s);
|
||||
|
||||
Intersect xs = Intersect();
|
||||
xs.add(i1);
|
||||
xs.add(i2);
|
||||
|
||||
ASSERT_EQ(xs.count(), 2);
|
||||
ASSERT_EQ(xs[0]->t, 1);
|
||||
ASSERT_EQ(xs[1]->t, 2);
|
||||
}
|
||||
65
tests/sphere_test.cpp
Normal file
65
tests/sphere_test.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* DoRayMe - a quick and dirty Raytracer
|
||||
* Sphere unit tests
|
||||
*
|
||||
* Created by Manoël Trapier
|
||||
* Copyright (c) 2020 986-Studio.
|
||||
*
|
||||
*/
|
||||
#include <ray.h>
|
||||
#include <sphere.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
|
||||
TEST(SphereTest, A_ray_intersect_a_sphere_at_two_points)
|
||||
{
|
||||
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
|
||||
Sphere s = Sphere();
|
||||
Intersect xs = s.intersect(r);
|
||||
|
||||
ASSERT_EQ(xs.count(), 2);
|
||||
ASSERT_EQ(xs[0]->t, 4.0);
|
||||
ASSERT_EQ(xs[1]->t, 6.0);
|
||||
}
|
||||
|
||||
TEST(SphereTest, A_ray_intersect_a_sphere_at_a_tangent)
|
||||
{
|
||||
Ray r = Ray(Point(0, 1, -5), Vector(0, 0, 1));
|
||||
Sphere s = Sphere();
|
||||
Intersect xs = s.intersect(r);
|
||||
|
||||
ASSERT_EQ(xs.count(), 2);
|
||||
ASSERT_EQ(xs[0]->t, 5.0);
|
||||
ASSERT_EQ(xs[1]->t, 5.0);
|
||||
}
|
||||
|
||||
TEST(SphereTest, A_ray_miss_a_sphere)
|
||||
{
|
||||
Ray r = Ray(Point(0, 2, -5), Vector(0, 0, 1));
|
||||
Sphere s = Sphere();
|
||||
Intersect xs = s.intersect(r);
|
||||
|
||||
ASSERT_EQ(xs.count(), 0);
|
||||
}
|
||||
|
||||
TEST(SphereTest, A_ray_originate_inside_a_sphere)
|
||||
{
|
||||
Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
|
||||
Sphere s = Sphere();
|
||||
Intersect xs = s.intersect(r);
|
||||
|
||||
ASSERT_EQ(xs.count(), 2);
|
||||
ASSERT_EQ(xs[0]->t, -1.0);
|
||||
ASSERT_EQ(xs[1]->t, 1.0);
|
||||
}
|
||||
|
||||
TEST(SphereTest, A_sphere_is_behind_a_ray)
|
||||
{
|
||||
Ray r = Ray(Point(0, 0, 5), Vector(0, 0, 1));
|
||||
Sphere s = Sphere();
|
||||
Intersect xs = s.intersect(r);
|
||||
|
||||
ASSERT_EQ(xs.count(), 2);
|
||||
ASSERT_EQ(xs[0]->t, -6.0);
|
||||
ASSERT_EQ(xs[1]->t, -4.0);
|
||||
}
|
||||
Reference in New Issue
Block a user