10 Commits

Author SHA1 Message Date
Godzil
9d0db6a635 Added planes! 2020-02-21 00:26:48 +00:00
Godzil
66c1582a5f Shape is now an abstract class and can't be instanciated.
Change derived shape to only deal with local calculation they don't need anymore to deal with how they've been transformed.
2020-02-21 00:02:30 +00:00
Godzil
2a8fe61388 Working on adding test for the shape object. 2020-02-20 18:06:29 +00:00
Godzil
f8c60da05e Updating readme 2020-02-20 17:48:09 +00:00
Godzil
de315d06f9 Just to be sure. 2020-02-20 17:47:26 +00:00
Godzil
cf5597ad6d Adding shadows! 2020-02-20 17:46:03 +00:00
Manoël Trapier
5198888df6 Doh 2020-02-20 16:49:02 +00:00
Godzil
10ae695f01 Trying to fixing some weird things about coverall, also add some real more coverage. 2020-02-20 16:47:00 +00:00
Godzil
d4fae2dbe2 Revert the canvas size to the one from the chapter 2020-02-20 16:20:20 +00:00
Godzil
daef0c078f Revert the canvas size to the one from the chapter 2020-02-20 16:13:58 +00:00
33 changed files with 584 additions and 52 deletions

View File

@@ -3,4 +3,33 @@
DoRayMe
=======
A Quick and dirty raytracer.
A Quick and dirty raytracer.
This raytracer is made following the book "[The Ray Tracer Challenge](https://pragprog.com/book/jbtracer/the-ray-tracer-challenge)" by Jamis Buck.
It is writen in C++ with no STL and use [LodePNG](https://github.com/lvandeve/lodepng) to output PNG file.
Examples outputs
----------------
From chapter 05:
![Chapter 5 rendering test](output/ch5_test.png)
From Chapter 06:
![Chapter 6 rendering test](output/ch6_test.png)
From Chapter 07:
![Chapter 7 rendering test](output/ch7_test.png)
From Chapter 08:
![Chapter 8 rendering test](output/ch8_test.png)
From Chapter 09:
![Chapter 9 rendering test](output/ch9_test.png)

BIN
output/ch5_test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 273 B

BIN
output/ch6_test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

BIN
output/ch7_test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

BIN
output/ch8_test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 80 KiB

BIN
output/ch9_test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

View File

@@ -16,11 +16,13 @@ class Shape;
struct Computation
{
Computation(Shape *object, double t, Tuple point, Tuple eyev, Tuple normalv, bool inside) :
object(object), t(t), hitPoint(point), eyeVector(eyev), normalVector(normalv), inside(inside) { };
Computation(Shape *object, double t, Tuple point, Tuple eyev, Tuple normalv, Tuple overHitP, bool inside) :
object(object), t(t), hitPoint(point), eyeVector(eyev), normalVector(normalv), inside(inside), overHitPoint(overHitP) { };
Shape *object;
double t;
Tuple hitPoint;
Tuple overHitPoint;
Tuple eyeVector;
Tuple normalVector;

View File

@@ -25,7 +25,7 @@ public:
public:
Material() : colour(Colour(1, 1, 1)), ambient(0.1), diffuse(0.9), specular(0.9), shininess(200) {};
Colour lighting(Light light, Tuple point, Tuple eyeVector, Tuple normalVector);
Colour lighting(Light light, Tuple point, Tuple eyeVector, Tuple normalVector, bool inShadow = false);
bool operator==(const Material &b) const { return double_equal(this->ambient, b.ambient) &&
double_equal(this->diffuse, b.diffuse) &&

View File

@@ -13,6 +13,7 @@
#include <math.h>
void set_equal_precision(double v);
double getEpsilon();
bool double_equal(double a, double b);
double deg_to_rad(double deg);

22
source/include/plane.h Normal file
View File

@@ -0,0 +1,22 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Plane header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_PLANE_H
#define DORAYME_PLANE_H
class Plane : public Shape
{
private:
Intersect localIntersect(Ray r);
Tuple localNormalAt(Tuple point);
public:
Plane() : Shape(SHAPE_PLANE) { };
};
#endif //DORAYME_PLANE_H

View File

@@ -21,6 +21,7 @@ enum ShapeType
{
SHAPE_NONE,
SHAPE_SPHERE,
SHAPE_PLANE,
};
/* Base class for all object that can be presented in the world */
@@ -29,6 +30,10 @@ class Shape
private:
ShapeType type;
private:
virtual Intersect localIntersect(Ray r) = 0;
virtual Tuple localNormalAt(Tuple point) = 0;
public:
Matrix transformMatrix;
Matrix inverseTransform;
@@ -37,8 +42,8 @@ public:
public:
Shape(ShapeType = SHAPE_NONE);
virtual Intersect intersect(Ray r);
virtual Tuple normalAt(Tuple point);
Intersect intersect(Ray r);
Tuple normalAt(Tuple point);
void setTransform(Matrix transform);
void setMaterial(Material material) { this->material = material; };

View File

@@ -15,11 +15,13 @@
class Sphere : public Shape
{
private:
Intersect localIntersect(Ray r);
Tuple localNormalAt(Tuple point);
public:
Sphere() : Shape(SHAPE_SPHERE) { };
/* All sphere are at (0, 0, 0) and radius 1 in the object space */
virtual Intersect intersect(Ray r);
virtual Tuple normalAt(Tuple point);
};
#endif /* DORAYME_SPHERE_H */

View File

@@ -0,0 +1,28 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Test shape header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_TESTSHAPE_H
#define DORAYME_TESTSHAPE_H
#include <shape.h>
#include <ray.h>
#include <tuple.h>
class TestShape : public Shape
{
private:
Intersect localIntersect(Ray r);
Tuple localNormalAt(Tuple point);
public:
Ray localRay;
TestShape();
};
#endif //DORAYME_TESTSHAPE_H

View File

@@ -43,6 +43,7 @@ public:
Tuple shadeHit(Computation comps);;
Tuple colourAt(Ray r);
bool isShadowed(Tuple point);
Intersect intersect(Ray r);

View File

@@ -22,11 +22,13 @@ Computation Intersection::prepareComputation(Ray r)
normalV = -normalV;
}
Tuple overHitP = hitP + normalV * getEpsilon();
return Computation(this->object,
this->t,
hitP,
eyeV,
normalV,
overHitP,
inside);
}

View File

@@ -18,6 +18,11 @@ void set_equal_precision(double v)
current_precision = v;
}
double getEpsilon()
{
return current_precision;
}
bool double_equal(double a, double b)
{
return fabs(a - b) < current_precision;

View File

@@ -10,7 +10,7 @@
#include <material.h>
#include <colour.h>
Colour Material::lighting(Light light, Tuple point, Tuple eyeVector, Tuple normalVector)
Colour Material::lighting(Light light, Tuple point, Tuple eyeVector, Tuple normalVector, bool inShadow)
{
Tuple lightVector = (light.position - point).normalise();
Tuple reflectVector = Tuple(0, 0, 0, 0);
@@ -25,31 +25,33 @@ Colour Material::lighting(Light light, Tuple point, Tuple eyeVector, Tuple norma
ambientColour = effectiveColour * this->ambient;
lightDotNormal = lightVector.dot(normalVector);
if (lightDotNormal < 0)
if (!inShadow)
{
diffuseColour = Colour(0, 0, 0);
specularColour = Colour(0, 0, 0);
}
else
{
diffuseColour = effectiveColour * this->diffuse * lightDotNormal;
reflectVector = -lightVector.reflect(normalVector);
lightDotNormal = lightVector.dot(normalVector);
reflectDotEye = reflectVector.dot(eyeVector);
if (reflectDotEye < 0)
if (lightDotNormal < 0)
{
diffuseColour = Colour(0, 0, 0);
specularColour = Colour(0, 0, 0);
}
else
{
double factor = pow(reflectDotEye, this->shininess);
specularColour = light.intensity * this->specular * factor;
diffuseColour = effectiveColour * this->diffuse * lightDotNormal;
reflectVector = -lightVector.reflect(normalVector);
reflectDotEye = reflectVector.dot(eyeVector);
if (reflectDotEye < 0)
{
specularColour = Colour(0, 0, 0);
}
else
{
double factor = pow(reflectDotEye, this->shininess);
specularColour = light.intensity * this->specular * factor;
}
}
}
finalColour = ambientColour + diffuseColour + specularColour;
return Colour(finalColour.x, finalColour.y, finalColour.z);

36
source/shapes/plane.cpp Normal file
View File

@@ -0,0 +1,36 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Plane implementation
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <tuple.h>
#include <ray.h>
#include <shape.h>
#include <plane.h>
#include <math_helper.h>
Intersect Plane::localIntersect(Ray r)
{
double t;
Intersect ret = Intersect();
if (fabs(r.direction.y) < getEpsilon())
{
/* With a direction == 0, the ray can't intersect the plane */
return ret;
}
t = -r.origin.y / r.direction.y;
ret.add(Intersection(t, this));
return ret;
}
Tuple Plane::localNormalAt(Tuple point)
{
return Vector(0, 1, 0);
}

View File

@@ -22,12 +22,21 @@ Shape::Shape(ShapeType type)
Intersect Shape::intersect(Ray r)
{
return Intersect();
return this->localIntersect(this->invTransform(r));
};
Tuple Shape::normalAt(Tuple point)
{
return Vector(0, 0, 0);
Tuple local_point = this->inverseTransform * point;
Tuple local_normal = this->localNormalAt(local_point);
Tuple world_normal = this->inverseTransform.transpose() * local_normal;
/* W may get wrong, so hack it. This is perfectly normal as we are using a 4x4 matrix instead of a 3x3 */
world_normal.w = 0;
return world_normal.normalise();
}
void Shape::setTransform(Matrix transform)

View File

@@ -13,17 +13,15 @@
#include <tuple.h>
#include <intersect.h>
Intersect Sphere::intersect(Ray r)
Intersect Sphere::localIntersect(Ray r)
{
Intersect ret;
double a, b, c, discriminant;
Ray transRay = this->invTransform(r);
Tuple sphere_to_ray = r.origin - Point(0, 0, 0);
Tuple sphere_to_ray = transRay.origin - Point(0, 0, 0);
a = transRay.direction.dot(transRay.direction);
b = 2 * transRay.direction.dot(sphere_to_ray);
a = r.direction.dot(r.direction);
b = 2 * r.direction.dot(sphere_to_ray);
c = sphere_to_ray.dot(sphere_to_ray) - 1;
discriminant = b * b - 4 * a * c;
@@ -37,14 +35,7 @@ Intersect Sphere::intersect(Ray r)
return ret;
}
Tuple Sphere::normalAt(Tuple point)
Tuple Sphere::localNormalAt(Tuple point)
{
Tuple object_point = this->inverseTransform * point;
Tuple object_normal = (object_point - Point(0, 0, 0)).normalise();
Tuple world_normal = this->inverseTransform.transpose() * object_normal;
/* W may get wrong, so hack it. This is perfectly normal as we are using a 4x4 matrix instead of a 3x3 */
world_normal.w = 0;
return world_normal.normalise();
return (point - Point(0, 0, 0)).normalise();
}

View File

@@ -0,0 +1,25 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Test shape implementation
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <shape.h>
#include <testshape.h>
TestShape::TestShape() : localRay(Point(0, 0, 0), Vector(0, 0, 0))
{
}
Intersect TestShape::localIntersect(Ray r)
{
this->localRay = r;
return Intersect();
}
Tuple TestShape::localNormalAt(Tuple point)
{
return Vector(point.x, point.y, point.z);
}

View File

@@ -94,7 +94,12 @@ Intersect World::intersect(Ray r)
Tuple World::shadeHit(Computation comps)
{
return comps.object->material.lighting(*this->lightList[0], comps.hitPoint, comps.eyeVector, comps.normalVector);
/* TODO: Add support for more than one light */
bool isThereAnObstacle = this->isShadowed(comps.overHitPoint);
return comps.object->material.lighting(*this->lightList[0], comps.overHitPoint, comps.eyeVector,
comps.normalVector, isThereAnObstacle);
}
Tuple World::colourAt(Ray r)
@@ -109,4 +114,23 @@ Tuple World::colourAt(Ray r)
{
return this->shadeHit(hit.prepareComputation(r));
}
}
}
bool World::isShadowed(Tuple point)
{
/* TODO: Add support for more than one light */
Tuple v = this->lightList[0]->position - point;
double distance = v.magnitude();
Tuple direction = v.normalise();
Ray r = Ray(point, direction);
Intersection h = this->intersect(r).hit();
if (!h.nothing() && (h.t < distance))
{
return true;
}
return false;
}

View File

@@ -4,7 +4,8 @@ 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
intersect_test.cpp sphere_test.cpp light_test.cpp material_test.cpp world_test.cpp camera_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)
add_executable(testMyRays)
target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
@@ -33,6 +34,12 @@ target_include_directories(ch6_test PUBLIC ../source/include)
target_sources(ch7_test PRIVATE ch7_test.cpp)
target_link_libraries(ch7_test rayonnement)
add_executable(ch9_test)
target_include_directories(ch6_test PUBLIC ../source/include)
target_sources(ch9_test PRIVATE ch9_test.cpp)
target_link_libraries(ch9_test rayonnement)
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>)
add_test(NAME Chapter07_Test COMMAND $<TARGET_FILE:ch7_test>)
add_test(NAME Chapter09_Test COMMAND $<TARGET_FILE:ch9_test>)

View File

@@ -50,4 +50,22 @@ TEST(CanvasTest, Save_a_PNG_file)
ASSERT_TRUE(c.SaveAsPNG("Save_a_PNG_file.png"));
}
TEST(CanvasTest, Create_a_canvas_from_another_using_reference)
{
Canvas c = Canvas(100, 100);
Canvas copy = Canvas(c);
ASSERT_EQ(c.width, copy.width);
}
TEST(CanvasTest, Create_a_canvas_from_another_using_pointer)
{
Canvas c = Canvas(100, 100);
Canvas copy = Canvas(&c);
ASSERT_EQ(c.width, copy.width);
}

View File

@@ -68,7 +68,7 @@ int main()
w.addLight(&light);
/* Set the camera */
Camera camera = Camera(1000, 500, M_PI / 3);
Camera camera = Camera(100, 50, M_PI / 3);
camera.setTransform(viewTransform(Point(0, 1.5, -5),
Point(0, 1, 0),
Vector(0, 1, 0)));

69
tests/ch9_test.cpp Normal file
View File

@@ -0,0 +1,69 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Render test for chapter 5 "Put it together".
*
* 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 <transformation.h>
int main()
{
/* First we need to construct the world */
Plane floor = Plane();
floor.material.colour = Colour(1, 0.9, 0.9);
floor.material.specular = 0;
Sphere middle = Sphere();
middle.setTransform(translation(-0.5, 1, 0.5));
middle.material.colour = Colour(0.1, 1, 0.5);
middle.material.diffuse = 0.7;
middle.material.specular = 0.3;
Sphere right = Sphere();
right.setTransform(translation(1.5, 0.5, -0.5) * scaling(0.5, 0.5, 0.5));
right.material.colour = Colour(0.5, 1, 0.1);
right.material.diffuse = 0.7;
right.material.specular = 0.3;
Sphere left = Sphere();
left.setTransform(translation(-1.5, 0.33, -0.75) * scaling(0.33, 0.33, 0.33));
left.material.colour = Colour(1, 0.8, 0.1);
left.material.diffuse = 0.7;
left.material.specular = 0.3;
World w = World();
w.addObject(&floor);
w.addObject(&middle);
w.addObject(&left);
w.addObject(&right);
/* Add light */
Light light = Light(POINT_LIGHT, Point(-10, 10, -10), Colour(1, 1, 1));
w.addLight(&light);
/* Set the camera */
Camera camera = Camera(1000, 500, M_PI / 3);
camera.setTransform(viewTransform(Point(0, 1.5, -5),
Point(0, 1, 0),
Vector(0, 1, 0)));
/* Now render it */
Canvas image = camera.render(w);
image.SaveAsPNG("ch9_test.png");
return 0;
}

View File

@@ -9,9 +9,9 @@
#include <intersect.h>
#include <intersection.h>
#include <sphere.h>
#include <transformation.h>
#include <gtest/gtest.h>
TEST(IntersectTest, Creating_an_intersect_and_do_some_check)
{
Intersect i;
@@ -173,4 +173,20 @@ TEST(IntersectTest, The_hit_when_an_intersection_occurs_on_the_inside)
/* Normal vector would have been (0, 0, 1); but is inverted ! */
ASSERT_EQ(comps.normalVector, Vector(0, 0, -1));
}
TEST(IntersectTest, The_hit_should_offset_the_point)
{
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
Sphere shape = Sphere();
shape.setTransform(translation(0, 0, 1));
Intersection i = Intersection(5, &shape);
Computation comps = i.prepareComputation(r);
/* Normal vector would have been (0, 0, 1); but is inverted ! */
ASSERT_LT(comps.overHitPoint.z, -getEpsilon() / 2);
ASSERT_GT(comps.hitPoint.z, comps.overHitPoint.z);
}

View File

@@ -86,5 +86,17 @@ TEST(MaterialTest, Lighting_with_the_light_behind_the_surface)
Colour result = m.lighting(light, position, eyev, normalv);
ASSERT_EQ(result, Colour(0.1, 0.1, 0.1));
}
TEST(MaterialTest, Lighting_with_the_surface_in_shadow)
{
Vector eyev = Vector(0, 0, -1);
Vector normalv = Vector(0, 0, -1);
Light light = Light(POINT_LIGHT, Point(0, 0, -10), Colour(1, 1, 1));
bool inShadow = true;
Colour result = m.lighting(light, position, eyev, normalv, inShadow);
ASSERT_EQ(result, Colour(0.1, 0.1, 0.1));
}

71
tests/plane_test.cpp Normal file
View File

@@ -0,0 +1,71 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Plane unit tests
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <ray.h>
#include <shape.h>
#include <plane.h>
#include <material.h>
#include <transformation.h>
#include <gtest/gtest.h>
TEST(PlaneTest, The_normal_of_a_plane_is_constant_everywhere)
{
Plane p = Plane();
Tuple n1 = p.normalAt(Point(0, 0, 0));
Tuple n2 = p.normalAt(Point(10, 0, -10));
Tuple n3 = p.normalAt(Point(-5, 0, 0150));
ASSERT_EQ(n1, Vector(0, 1, 0));
ASSERT_EQ(n2, Vector(0, 1, 0));
ASSERT_EQ(n3, Vector(0, 1, 0));
}
TEST(PlaneTest, Intersect_with_a_ray_parallel_to_the_plane)
{
Plane p = Plane();
Ray r = Ray(Point(0, 10, 0), Vector(0, 0, 1));
Intersect xs = p.intersect(r);
ASSERT_EQ(xs.count(), 0);
}
TEST(PlaneTest, Intersect_with_a_coplanar_ray)
{
Plane p = Plane();
Ray r = Ray(Point(0, 0, 0), Vector(0, 0, 1));
Intersect xs = p.intersect(r);
ASSERT_EQ(xs.count(), 0);
}
TEST(PlaneTest, A_ray_intersecting_a_plane_from_above)
{
Plane p = Plane();
Ray r = Ray(Point(0, 1, 0), Vector(0, -1, 0));
Intersect xs = p.intersect(r);
ASSERT_EQ(xs.count(), 1);
ASSERT_EQ(xs[0].t, 1);
ASSERT_EQ(xs[0].object, &p);
}
TEST(PlaneTest, A_ray_intersecting_a_plane_from_below)
{
Plane p = Plane();
Ray r = Ray(Point(0, -1, 0), Vector(0, 1, 0));
Intersect xs = p.intersect(r);
ASSERT_EQ(xs.count(), 1);
ASSERT_EQ(xs[0].t, 1);
ASSERT_EQ(xs[0].object, &p);
}

View File

@@ -9,6 +9,7 @@
#include <ray.h>
#include <transformation.h>
#include <shape.h>
#include <testshape.h>
#include <gtest/gtest.h>
@@ -38,7 +39,7 @@ TEST(RayTest, Translating_a_ray)
Ray r = Ray(Point(1, 2, 3), Vector(0, 1, 0));
Matrix m = translation(3, 4, 5);
Shape o = Shape();
TestShape o = TestShape();
o.setTransform(m);
@@ -53,7 +54,7 @@ TEST(RayTest, Scaling_a_ray)
Ray r = Ray(Point(1, 2, 3), Vector(0, 1, 0));
Matrix m = scaling(2, 3, 4);
Shape o = Shape();
TestShape o = TestShape();
o.setTransform(m);

98
tests/shape_test.cpp Normal file
View File

@@ -0,0 +1,98 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Shape unit tests
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <shape.h>
#include <testshape.h>
#include <matrix.h>
#include <transformation.h>
#include <gtest/gtest.h>
TEST(ShapeTest, The_default_transformation)
{
TestShape s = TestShape();
ASSERT_EQ(s.transformMatrix, Matrix4().identity());
}
TEST(ShapeTest, Assigning_a_transformation)
{
TestShape s = TestShape();
s.setTransform(translation(2, 3, 4));
ASSERT_EQ(s.transformMatrix, translation(2, 3, 4));
}
TEST(ShapeTest, The_default_material)
{
TestShape s = TestShape();
ASSERT_EQ(s.material, Material());
}
TEST(ShapeTest, Assigning_a_material)
{
TestShape s = TestShape();
Material m = Material();
m.ambient = 1;
s.material = m;
ASSERT_EQ(s.material, m);
}
TEST(ShapeTest, Intersecting_a_scaled_shape_with_a_ray)
{
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
TestShape s = TestShape();
s.setTransform(scaling(2, 2, 2));
Intersect xs = s.intersect(r);
ASSERT_EQ(s.localRay.origin, Point(0, 0, -2.5));
ASSERT_EQ(s.localRay.direction, Vector(0, 0, 0.5));
}
TEST(ShapeTest, Intersecting_a_translated_shape_with_a_ray)
{
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
TestShape s = TestShape();
s.setTransform(translation(5, 0, 0));
Intersect xs = s.intersect(r);
ASSERT_EQ(s.localRay.origin, Point(-5, 0, -5));
ASSERT_EQ(s.localRay.direction, Vector(0, 0, 1));
}
TEST(ShapeTest, Computing_the_normal_on_a_translated_shape)
{
TestShape s = TestShape();
s.setTransform(translation(0, 1, 0));
Tuple n = s.normalAt(Point(0, 1.70711, -0.70711));
/* Temporary lower the precision */
set_equal_precision(0.00001);
ASSERT_EQ(n, Vector(0, 0.70711, -0.70711));
set_equal_precision(FLT_EPSILON);
}
TEST(ShapeTest, Computing_the_normal_on_a_tranformed_shape)
{
TestShape s = TestShape();
s.setTransform(scaling(1, 0.5, 1) * rotationZ(M_PI / 5));
Tuple n = s.normalAt(Point(0, sqrt(2)/2, -sqrt(2)/2));
/* Temporary lower the precision */
set_equal_precision(0.00001);
ASSERT_EQ(n, Vector(0, 0.97014, -0.24254));
set_equal_precision(FLT_EPSILON);
}

View File

@@ -111,4 +111,60 @@ TEST(WorldTest, The_colour_with_an_intersection_behind_the_ray)
Tuple c = w.colourAt(r);
ASSERT_EQ(c, inner->material.colour);
}
}
TEST(WorldTest, There_is_no_shadow_when_nothing_is_collinear_with_point_and_light)
{
World w = DefaultWorld();
Tuple p = Point(0, 10, 0);
ASSERT_FALSE(w.isShadowed(p));
}
TEST(WorldTest, The_shadow_when_an_object_is_between_the_point_and_the_light)
{
World w = DefaultWorld();
Tuple p = Point(10, -10, 10);
ASSERT_TRUE(w.isShadowed(p));
}
TEST(WorldTest, There_is_no_shadow_whne_an_object_is_behing_the_light)
{
World w = DefaultWorld();
Tuple p = Point(-20, 20, -20);
ASSERT_FALSE(w.isShadowed(p));
}
TEST(WorldTest, There_is_no_shadow_when_an_object_is_behing_the_point)
{
World w = DefaultWorld();
Tuple p = Point(-2, 2, -2);
ASSERT_FALSE(w.isShadowed(p));
}
TEST(WorldTest, Shade_hit_is_given_an_intersection_in_shadow)
{
World w = World();
Light l = Light(POINT_LIGHT, Point(0, 0, -10), Colour(1, 1, 1));
w.addLight(&l);
Sphere s1 = Sphere();
w.addObject(&s1);
Sphere s2 = Sphere();
s2.setTransform(translation(0, 0, 10));
w.addObject(&s2);
Ray r = Ray(Point(0, 0, 5), Vector(0, 0, 1));
Intersection i = Intersection(4, &s2);
Computation comps = i.prepareComputation(r);
Tuple c = w.shadeHit(comps);
ASSERT_EQ(c, Colour(0.1, 0.1, 0.1));
};