diff --git a/README.md b/README.md index 8a99c40..1821b39 100644 --- a/README.md +++ b/README.md @@ -52,4 +52,6 @@ From Chapter 12 - Cubes: From Chapter 13 - Cylinders: -![Chapter 13 rendering test](output/ch13_test.png) \ No newline at end of file +![Chapter 13 rendering test](output/ch13_test.png) +Bonus: +![Chapter 13 cone test](output/ch13_cone.png) \ No newline at end of file diff --git a/output/ch13_cone.png b/output/ch13_cone.png new file mode 100644 index 0000000..5a0eb7a Binary files /dev/null and b/output/ch13_cone.png differ diff --git a/source/include/cone.h b/source/include/cone.h new file mode 100644 index 0000000..891ce48 --- /dev/null +++ b/source/include/cone.h @@ -0,0 +1,33 @@ +/* + * DoRayMe - a quick and dirty Raytracer + * Cone header + * + * Created by Manoël Trapier + * Copyright (c) 2020 986-Studio. + * + */ +#ifndef DORAYME_CONE_H +#define DORAYME_CONE_H + +#include +#include +#include + +class Cone : public Shape { +protected: + Intersect localIntersect(Ray r); + + Tuple localNormalAt(Tuple point); + + bool checkCap(Ray r, double t, double y); + void intersectCaps(Ray r, Intersect &xs); + +public: + bool isClosed; + double minCap; + double maxCap; + + Cone() : minCap(-INFINITY), maxCap(INFINITY), isClosed(false), Shape(SHAPE_CONE) {}; +}; + +#endif /* DORAYME_CONE_H */ diff --git a/source/shapes/cone.cpp b/source/shapes/cone.cpp new file mode 100644 index 0000000..de41cca --- /dev/null +++ b/source/shapes/cone.cpp @@ -0,0 +1,124 @@ +/* + * DoRayMe - a quick and dirty Raytracer + * Cone implementation + * + * Created by Manoël Trapier + * Copyright (c) 2020 986-Studio. + * + */ +#include +#include +#include +#include +#include + +bool Cone::checkCap(Ray r, double t, double y) +{ + /* Helping function to reduce duplication. + * Checks to see if the intersection ot t is within a radius + * of 1 (the radius of our Cone from the y axis + */ + double x = r.origin.x + t * r.direction.x; + double z = r.origin.z + t * r.direction.z; + return (x * x + z * z) <= fabs(y); +} + +void Cone::intersectCaps(Ray r, Intersect &xs) +{ + /* Caps only mattter is the Cone is closed, and might possibly be + * intersected by the ray + */ + if ((this->isClosed) && (fabs(r.direction.y) > getEpsilon())) + { + double t; + /* Check for an intersection with the lower end cap by intersecting + * the ray with the plan at y = this->minCap + */ + t = (this->minCap - r.origin.y) / r.direction.y; + if (this->checkCap(r, t, this->minCap)) + { + xs.add(Intersection(t, this)); + } + + /* Check for an intersection with the upper end cap by intersecting + * the ray with the plan at y = this->maxCap + */ + t = (this->maxCap - r.origin.y) / r.direction.y; + if (this->checkCap(r, t, this->maxCap)) + { + xs.add(Intersection(t, this)); + } + } +} + +Intersect Cone::localIntersect(Ray r) +{ + Intersect ret; + + double A = pow(r.direction.x, 2) - + pow(r.direction.y, 2) + + pow(r.direction.z, 2); + double B = (2 * r.origin.x * r.direction.x) - + (2 * r.origin.y * r.direction.y) + + (2 * r.origin.z * r.direction.z); + + double C = pow(r.origin.x, 2) - + pow(r.origin.y, 2) + + pow(r.origin.z, 2); + + if ((fabs(A) <= getEpsilon()) && (fabs(B) >= getEpsilon())) + { + double t = -C / (2*B); + ret.add(Intersection(t, this)); + } + else if (fabs(A) >= getEpsilon()) + { + + double disc = pow(B, 2) - 4 * A * C; + + if (disc >= 0) + { + double t0 = (-B - sqrt(disc)) / (2 * A); + double t1 = (-B + sqrt(disc)) / (2 * A); + + double y0 = r.origin.y + t0 * r.direction.y; + if ((this->minCap < y0) && (y0 < this->maxCap)) + { + ret.add(Intersection(t0, this)); + } + + double y1 = r.origin.y + t1 * r.direction.y; + if ((this->minCap < y1) && (y1 < this->maxCap)) + { + ret.add(Intersection(t1, this)); + } + } + } + + this->intersectCaps(r, ret); + + return ret; +} + +Tuple Cone::localNormalAt(Tuple point) +{ + /* Compute the square of the distance from the Y axis */ + double dist = point.x * point.x + point.z * point.z; + + if ((dist < 1) && (point.y >= (this->maxCap - getEpsilon()))) + { + return Vector(0, 1, 0); + } + + if ((dist < 1) && (point.y <= this->minCap + getEpsilon())) + { + return Vector(0, -1, 0); + } + + double y = sqrt(point.x * point.x + point.z * point.z); + if (point.y > 0) + { + y = -y; + } + return Vector(point.x, y, point.z); +} diff --git a/source/tuple.cpp b/source/tuple.cpp index 42a2c27..fb5aba2 100644 --- a/source/tuple.cpp +++ b/source/tuple.cpp @@ -19,6 +19,11 @@ double Tuple::magnitude() Tuple Tuple::normalise() { double mag = this->magnitude(); + if (mag == 0) + { + return Tuple(0, 0, 0, 0); + } + return Tuple(this->x / mag, this->y / mag, this->z / mag, this->w / mag); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index fdca6f2..b74eb30 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,7 +5,7 @@ find_package(Threads REQUIRED) set(TESTS_SRC math_test.cpp tuple_test.cpp colour_test.cpp canvas_test.cpp matrix_test.cpp transformation_test.cpp ray_test.cpp intersect_test.cpp sphere_test.cpp light_test.cpp material_test.cpp world_test.cpp camera_test.cpp - shape_test.cpp plane_test.cpp pattern_test.cpp cube_test.cpp cylinder_test.cpp) + shape_test.cpp plane_test.cpp pattern_test.cpp cube_test.cpp cylinder_test.cpp cone_test.cpp) add_executable(testMyRays) target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) @@ -74,6 +74,11 @@ target_include_directories(ch13_test PUBLIC ../source/include) target_sources(ch13_test PRIVATE ch13_test.cpp) target_link_libraries(ch13_test rayonnement) +add_executable(ch13_cone) +target_include_directories(ch13_cone PUBLIC ../source/include) +target_sources(ch13_cone PRIVATE ch13_cone.cpp) +target_link_libraries(ch13_cone rayonnement) + add_test(NAME Chapter05_Test COMMAND $) add_test(NAME Chapter06_Test COMMAND $) add_test(NAME Chapter07_Test COMMAND $) @@ -84,4 +89,5 @@ add_test(NAME Chapter11_Refraction COMMAND $) add_test(NAME Chapter11_Test COMMAND $) add_test(NAME Chapter12_Test COMMAND $) add_test(NAME Chapter13_Test COMMAND $) +add_test(NAME Chapter13_ConeBonus COMMAND $) add_test(NAME Hw3Render COMMAND $ ${CMAKE_CURRENT_SOURCE_DIR}/test.hw3scene) \ No newline at end of file diff --git a/tests/ch12_test.cpp b/tests/ch12_test.cpp index eecb4fa..b39ef28 100644 --- a/tests/ch12_test.cpp +++ b/tests/ch12_test.cpp @@ -199,7 +199,7 @@ int main() /* ----------------------------- */ /* Set the camera */ - Camera camera = Camera(1000, 500, 0.785); + Camera camera = Camera(400, 200, 0.785); camera.setTransform(viewTransform(Point(8, 6, -8), Point(0, 3, 0), Vector(0, 1, 0))); diff --git a/tests/ch13_cone.cpp b/tests/ch13_cone.cpp new file mode 100644 index 0000000..b51b857 --- /dev/null +++ b/tests/ch13_cone.cpp @@ -0,0 +1,204 @@ +/* + * DoRayMe - a quick and dirty Raytracer + * Render test for reflection in chapter 13. + * + * Created by Manoël Trapier + * Copyright (c) 2020 986-Studio. + * + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +int main() +{ + World w = World(); + + /* Add light */ + Light light = Light(POINT_LIGHT, Point(1, 6.9, -4.9), Colour(1, 1, 1)); + w.addLight(&light); + + /* ----------------------------- */ + + /* The floor */ + Plane floor = Plane(); + floor.material.pattern = new CheckersPattern(Colour(0.5, 0.5, 0.5), Colour(0.75, 0.75, 0.75)); + floor.material.pattern->setTransform(rotationY(0.3) * scaling(0.25, 0.25, 0.25)); + w.addObject(&floor); + + Plane ceiling = Plane(); + ceiling.material.pattern = new CheckersPattern(Colour(0.5, 0.2, 0.5), Colour(0.75, 0.4, 0.75)); + ceiling.material.pattern->setTransform(rotationY(0.3)); + ceiling.setTransform(translation(0, 8, 0)); + w.addObject(&ceiling); + + /* ----------------------------- */ + + Cone cyl1 = Cone(); + cyl1.minCap = -1; + cyl1.maxCap = 0; + cyl1.isClosed = true; + cyl1.setTransform(translation(-1, 0, 1) * scaling(0.5, 1, 0.5) * translation(0, 1, 0)); + cyl1.material.colour = Colour(0, 1, 0.6); + cyl1.material.diffuse = 0.1; + cyl1.material.specular = 0.9; + cyl1.material.shininess = 300; + cyl1.material.reflective = 0.3; + w.addObject(&cyl1); + + /* ----------------------------- */ + + /* Concentrics */ + Cylinder cons1 = Cylinder(); + cons1.minCap = 0; + cons1.maxCap = 0.2; + cons1.isClosed = false; + cons1.setTransform(translation(1, 0, 0) * scaling(0.8, 1, 0.8)); + cons1.material.colour = Colour(1, 1, 0.3); + cons1.material.ambient = 0.1; + cons1.material.diffuse = 0.8; + cons1.material.specular = 0.9; + cons1.material.shininess = 300; + cons1.material.reflective = 0.2; + w.addObject(&cons1); + + Cylinder cons2 = Cylinder(); + cons2.minCap = 0; + cons2.maxCap = 0.3; + cons2.isClosed = false; + cons2.setTransform(translation(1, 0, 0) * scaling(0.6, 1, 0.6)); + cons2.material.colour = Colour(1, 0.9, 0.4); + cons2.material.ambient = 0.1; + cons2.material.diffuse = 0.8; + cons2.material.specular = 0.9; + cons2.material.shininess = 300; + cons2.material.reflective = 0.2; + w.addObject(&cons2); + + Cylinder cons3 = Cylinder(); + cons3.minCap = 0; + cons3.maxCap = 0.4; + cons3.isClosed = false; + cons3.setTransform(translation(1, 0, 0) * scaling(0.4, 1, 0.4)); + cons3.material.colour = Colour(1, 0.8, 0.5); + cons3.material.ambient = 0.1; + cons3.material.diffuse = 0.8; + cons3.material.specular = 0.9; + cons3.material.shininess = 300; + cons3.material.reflective = 0.2; + w.addObject(&cons3); + + Cylinder cons4 = Cylinder(); + cons4.minCap = 0; + cons4.maxCap = 0.5; + cons4.isClosed = true; + cons4.setTransform(translation(1, 0, 0) * scaling(0.2, 1, 0.2)); + cons4.material.colour = Colour(1, 0.7, 0.6); + cons4.material.ambient = 0.1; + cons4.material.diffuse = 0.8; + cons4.material.specular = 0.9; + cons4.material.shininess = 300; + cons4.material.reflective = 0.2; + w.addObject(&cons4); + + /* ----------------------------- */ + + /* decoratives cylinders */ + Cylinder deco1 = Cylinder(); + deco1.minCap = 0; + deco1.maxCap = 0.3; + deco1.isClosed = true; + deco1.setTransform(translation(0, 0, -0.75) * scaling(0.05, 1, 0.05)); + deco1.material.colour = Colour(1, 0, 0); + deco1.material.ambient = 0.1; + deco1.material.diffuse = 0.9; + deco1.material.specular = 0.9; + deco1.material.shininess = 300; + deco1.material.reflective = 0.5; + w.addObject(&deco1); + + Cylinder deco2 = Cylinder(); + deco2.minCap = 0; + deco2.maxCap = 0.3; + deco2.isClosed = true; + deco2.setTransform(translation(0, 0, -2.25) * rotationY(-0.15) * translation(0, 0, 1.5) * scaling(0.05, 1, 0.05)); + deco2.material.colour = Colour(1, 1, 0); + deco2.material.ambient = 0.1; + deco2.material.diffuse = 0.9; + deco2.material.specular = 0.9; + deco2.material.shininess = 300; + deco2.material.reflective = 0.5; + w.addObject(&deco2); + + Cylinder deco3 = Cylinder(); + deco3.minCap = 0; + deco3.maxCap = 0.3; + deco3.isClosed = true; + deco3.setTransform(translation(0, 0, -2.25) * rotationY(-0.3) * translation(0, 0, 1.5) * scaling(0.05, 1, 0.05)); + deco3.material.colour = Colour(0, 1, 0); + deco3.material.ambient = 0.1; + deco3.material.diffuse = 0.9; + deco3.material.specular = 0.9; + deco3.material.shininess = 300; + deco3.material.reflective = 0.5; + w.addObject(&deco3); + + Cylinder deco4 = Cylinder(); + deco4.minCap = 0; + deco4.maxCap = 0.3; + deco4.isClosed = true; + deco4.setTransform(translation(0, 0, -2.25) * rotationY(-0.45) * translation(0, 0, 1.5) * scaling(0.05, 1, 0.05)); + deco4.material.colour = Colour(0, 1, 1); + deco4.material.ambient = 0.1; + deco4.material.diffuse = 0.9; + deco4.material.specular = 0.9; + deco4.material.shininess = 300; + deco4.material.reflective = 0.5; + w.addObject(&deco4); + + /* ----------------------------- */ + + Cone glassCylinder = Cone(); + glassCylinder.minCap = 0.001; + glassCylinder.maxCap = 1; + glassCylinder.isClosed = true; + glassCylinder.dropShadow = false; + glassCylinder.setTransform(translation(0, 0, -1.5) * scaling(0.33, 1, 0.33)); + glassCylinder.material.colour = Colour(0.2, 0.63, 0.24); + glassCylinder.material.diffuse = 0.1; + glassCylinder.material.specular = 0.9; + glassCylinder.material.shininess = 300; + glassCylinder.material.reflective = 0.3; + w.addObject(&glassCylinder); + + /* ----------------------------- */ + + /* Set the camera */ + Camera camera = Camera(400, 200, 0.314); + camera.setTransform(viewTransform(Point(8, 3.5, -9), + Point(0, 0.3, 0), + Vector(0, 1, 0))); + + /* Now render it */ + Canvas image = camera.render(w, 20); + + image.SaveAsPNG("ch13_cone.png"); + + return 0; +} \ No newline at end of file diff --git a/tests/cone_test.cpp b/tests/cone_test.cpp new file mode 100644 index 0000000..ed37f3c --- /dev/null +++ b/tests/cone_test.cpp @@ -0,0 +1,132 @@ +/* + * DoRayMe - a quick and dirty Raytracer + * Cone unit tests + * + * Created by Manoël Trapier + * Copyright (c) 2020 986-Studio. + * + */ +#include +#include +#include +#include +#include + +class ConeTest : public Cone +{ +public: + Tuple doLocalNormalAt(Tuple point) + { + return localNormalAt(point); + } +}; + +TEST(ConeTest, Intersecting_a_cone_with_a_ray) +{ + Cone cone = Cone(); + + Point Origins[] = { + Point(0, 0, -5), + Point(0, 0, -5), + Point(1, 1, -5), + }; + + Vector Directions[] = { + Vector(0, 0, 1), + Vector(1, 1, 1), + Vector(-0.5, -1, 1), + }; + + double t0s[] = { 5, 8.66025, 4.55006 }; + double t1s[] = { 5, 8.66025, 49.44994 }; + + int i; + for(i = 0; i < 3; i++) + { + Tuple direction = Directions[i].normalise(); + Ray r = Ray(Origins[i], direction); + + Intersect xs = cone.intersect(r); + + /* Temporary lower the precision */ + set_equal_precision(0.00001); + + ASSERT_EQ(xs.count(), 2); + EXPECT_TRUE(double_equal(xs[0].t, t0s[i])); + EXPECT_TRUE(double_equal(xs[1].t, t1s[i])); + + set_equal_precision(FLT_EPSILON); + } +} + +TEST(ConeTest, Intersecting_a_cone_with_a_ray_parall_to_one_of_its_halves) +{ + Cone cone = Cone(); + Tuple direction = Vector(0, 1, 1).normalise(); + Ray r = Ray(Point(0, 0, -1), direction); + Intersect xs = cone.intersect(r); + ASSERT_EQ(xs.count(), 1); + + /* Temporary lower the precision */ + set_equal_precision(0.00001); + + ASSERT_TRUE(double_equal(xs[0].t, 0.35355)); + + set_equal_precision(FLT_EPSILON); + +} +TEST(ConeTest, Intersecting_a_cone_end_cap) +{ + Point Origins[] = { + Point(0, 0, -5), + Point(0, 0, -0.25), + Point(0, 0, -0.25), + }; + + Vector Directions[] = { + Vector(0, 1, 0), + Vector(0, 1, 1), + Vector(0, 1, 0), + }; + + uint32_t Counts[] = { 0, 2, 4 }; + + Cone cone = Cone(); + cone.minCap = -0.5; + cone.maxCap = 0.5; + cone.isClosed = true; + + int i; + for(i = 0; i < 3; i++) + { + Tuple direction = Directions[i].normalise(); + Ray r = Ray(Origins[i], direction); + + Intersect xs = cone.intersect(r); + + ASSERT_EQ(xs.count(), Counts[i]); + } +} + +TEST(ConeTest, Computing_the_normal_vector_on_a_cone) +{ + ConeTest cone = ConeTest(); + + Point HitPointss[] = { + Point(0, 0, 0), + Point(1, 1, 1), + Point(-1, -1, 0), + }; + + Vector Normals[] = { + Vector(0, 0, 0), + Vector(1, -sqrt(2), 1), + Vector(-1, 1, 0), + }; + + int i; + for(i = 0; i < 3; i++) + { + ASSERT_EQ(cone.doLocalNormalAt(HitPointss[i]), Normals[i]); + } +}