16 Commits

Author SHA1 Message Date
Godzil
656ff52204 Add Chapter 5 "putting it together" example. 2020-02-17 14:30:21 +00:00
Godzil
00b283053e Add transformation to objects. 2020-02-17 14:15:55 +00:00
Godzil
b799e5f819 Update a comment. 2020-02-17 13:57:07 +00:00
Godzil
cabe7ff147 Change Ray to use the generic Tuple instead of Point/Vector (but you still should use Point/Vector for initialisation) 2020-02-17 13:54:07 +00:00
Godzil
17aebe6538 Move the cross product to Tuple instead of just vector (to ease some stuff later, but this is invalid on Points) 2020-02-17 13:53:30 +00:00
Godzil
a1087a9871 Add default size to matrix (4 as it is the most common) 2020-02-17 13:52:28 +00:00
Godzil
c4418683c6 Add the hit function to get the closest non negative hit, and add some mecanisme to test that properly and report when no valid hit occur. 2020-02-17 12:24:15 +00:00
Godzil
1900d1f45d Change the Intersection to a class, and stop using memory allocation for it (and pointer)
A bit more clean (on the code side)
2020-02-17 11:48:29 +00:00
Godzil
513cd9d7eb Update glfw, googletest and lodepng submodules to their latest version 2020-02-17 11:19:53 +00:00
Godzil
01a0de09ab 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.
2020-02-17 11:16:20 +00:00
Godzil
8faf1db3be More Ray work. 2020-02-15 23:45:19 +00:00
Godzil
79af9fbc97 Also rename the test file. 2020-02-15 23:20:37 +00:00
Godzil
1e2588441f Rename tuple file to remove the plural.
Also add empty shell ray.
2020-02-15 23:18:04 +00:00
Godzil
1f4b14c896 Add a function to convert degree to radian. 2020-02-14 23:29:09 +00:00
Godzil
dee4b9ae91 Now we can transform! 2020-02-14 23:28:56 +00:00
Godzil
514bd649c1 Correct how google test is used.
I think it was working before because the other computer had gtest system installed.

Bonus: it is now also integrated with CTest
2020-02-14 22:19:39 +00:00
31 changed files with 977 additions and 35 deletions

View File

@@ -6,17 +6,6 @@ project(DoRayMe)
set(CMAKE_CXX_STANDARD 11)
#Add external projects that directly need to be builded
ExternalProject_Add(googletest
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/external/googletest"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/external/googletest"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
# LodePNG don't make a .a or .so, so let's build a library here
add_library(LodePNG STATIC)
set(LODEPNG_INCLUDE_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/external/lodepng)
@@ -25,5 +14,11 @@ target_sources(LodePNG PRIVATE external/lodepng/lodepng.cpp external/lodepng/lod
# Main app
add_subdirectory(source)
# Unit Tests
add_subdirectory(tests)
option(PACKAGE_TESTS "Build the tests" ON)
if(PACKAGE_TESTS)
enable_testing()
include(GoogleTest)
add_subdirectory("${PROJECT_SOURCE_DIR}/external/googletest" "external/googletest")
add_subdirectory(tests)
endif()

2
external/glfw vendored

View File

@@ -3,8 +3,11 @@
# First most is build as a library
add_library(rayonnement STATIC)
set(RAY_HEADERS include/tuples.h include/math_helper.h include/colour.h include/canvas.h include/matrix.h)
set(RAY_SOURCES tuples.cpp math_helper.cpp colour.cpp canvas.cpp matrix.cpp)
set(RAY_HEADERS include/tuple.h include/math_helper.h include/colour.h include/canvas.h
include/matrix.h include/transformation.h include/intersect.h include/intersection.h
include/object.h include/ray.h include/sphere.h)
set(RAY_SOURCES tuple.cpp math_helper.cpp colour.cpp canvas.cpp matrix.cpp transformation.cpp intersect.cpp
objects/object.cpp objects/ray.cpp objects/sphere.cpp)
target_include_directories(rayonnement PUBLIC include)
target_sources(rayonnement PRIVATE ${RAY_HEADERS} ${RAY_SOURCES})

View File

@@ -9,7 +9,7 @@
#ifndef DORAYME_COLOUR_H
#define DORAYME_COLOUR_H
#include <tuples.h>
#include <tuple.h>
class Colour : public Tuple
{

View File

@@ -0,0 +1,29 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Intersect header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_INTERSECT_H
#define DORAYME_INTERSECT_H
#include <stdint.h>
#include <intersection.h>
class Intersect
{
private:
Intersection *list;
uint32_t num;
uint32_t allocated;
public:
Intersect();
void add(Intersection i);
int count() { return this->num; };
Intersection operator[](const int p) { return this->list[p]; }
Intersection hit();
};
#endif //DORAYME_INTERSECT_H

View File

@@ -0,0 +1,29 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Intersection header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_INTERSECTION_H
#define DORAYME_INTERSECTION_H
#include <stdlib.h>
class Object;
class Intersection
{
public:
double t;
Object *object;
public:
Intersection(double t, Object *object) : t(t), object(object) { };
bool nothing() { return (this->object == nullptr); };
bool operator==(const Intersection &b) const { return ((this->t == b.t) && (this->object == b.object)); };
};
#endif //DORAYME_INTERSECTION_H

View File

@@ -10,7 +10,11 @@
#ifndef DORAYME_MATH_HELPER_H
#define DORAYME_MATH_HELPER_H
#include <math.h>
void set_equal_precision(double v);
bool double_equal(double a, double b);
double deg_to_rad(double deg);
#endif //DORAYME_MATH_HELPER_H

View File

@@ -9,7 +9,7 @@
#ifndef DORAYME_MATRIX_H
#define DORAYME_MATRIX_H
#include <tuples.h>
#include <tuple.h>
class Matrix
{
@@ -19,7 +19,7 @@ protected:
int size;
public:
Matrix(int size);
Matrix(int size = 4);
Matrix(double values[], int size);
double get(int x, int y) const { return this->data[this->size * x + y]; };

35
source/include/object.h Normal file
View File

@@ -0,0 +1,35 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Object header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_OBJECT_H
#define DORAYME_OBJECT_H
class Object;
#include <ray.h>
#include <tuple.h>
#include <matrix.h>
#include <intersect.h>
/* Base class for all object that can be presented in the world */
class Object
{
public:
Matrix transformMatrix;
Matrix inverseTransform;
public:
Object();
virtual Intersect intersect(Ray r);
void setTransform(Matrix transform);
Ray transform(Ray r) { return Ray(this->transformMatrix * r.origin, this->transformMatrix * r.direction); };
Ray invTransform(Ray r) { return Ray(this->inverseTransform * r.origin, this->inverseTransform * r.direction); };
};
#endif //DORAYME_OBJECT_H

25
source/include/ray.h Normal file
View File

@@ -0,0 +1,25 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Ray header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_RAY_H
#define DORAYME_RAY_H
#include <tuple.h>
class Ray
{
public:
Tuple direction;
Tuple origin;
Ray(Tuple origin, Tuple direction) : origin(origin), direction(direction) { };
Tuple position(double t) { return this->origin + this->direction * t; };
};
#endif //DORAYME_RAY_H

23
source/include/sphere.h Normal file
View File

@@ -0,0 +1,23 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Sphere header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_SPHERE_H
#define DORAYME_SPHERE_H
#include <object.h>
#include <ray.h>
#include <intersect.h>
class Sphere : public Object
{
public:
/* All sphere are at (0, 0, 0) and radius 1 in the object space */
virtual Intersect intersect(Ray r);
};
#endif //DORAYME_SPHERE_H

View File

@@ -0,0 +1,24 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Transformation header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_TRANSFORMATION_H
#define DORAYME_TRANSFORMATION_H
#include <matrix.h>
Matrix translation(double x, double y, double z);
Matrix scaling(double x, double y, double z);
Matrix rotation_x(double angle);
Matrix rotation_y(double angle);
Matrix rotation_z(double angle);
Matrix shearing(double Xy, double Xx, double Yx, double Yz, double Zx, double Zy);
#endif /* DORAYME_TRANSFORMATION_H */

View File

@@ -6,8 +6,8 @@
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_TUPLES_H
#define DORAYME_TUPLES_H
#ifndef DORAYME_TUPLE_H
#define DORAYME_TUPLE_H
#include <math_helper.h>
@@ -41,6 +41,8 @@ public:
double magnitude();
Tuple normalise();
double dot(const Tuple &b);
Tuple cross(const Tuple &b) const;
};
class Point: public Tuple
@@ -53,7 +55,6 @@ class Vector: public Tuple
{
public:
Vector(double x, double y, double z) : Tuple(x, y, z, 0.0) {};
Vector cross(const Vector &b) const;
};
#endif /* DORAYME_TUPLES_H */
#endif /*DORAYME_TUPLE_H*/

54
source/intersect.cpp Normal file
View File

@@ -0,0 +1,54 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Intersect implementation
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <stdlib.h>
#include <math_helper.h>
#include <intersect.h>
#include <float.h>
#define MIN_ALLOC (2)
Intersect::Intersect()
{
this->allocated = MIN_ALLOC;
this->list = (Intersection *)calloc(sizeof(Object *), MIN_ALLOC);
this->num = 0;
}
void Intersect::add(Intersection i)
{
if ((this->num + 1) < this->allocated)
{
this->allocated *= 2;
this->list = (Intersection *)realloc(this->list, sizeof(Object *) * this->allocated);
}
this->list[this->num++] = i;
}
Intersection Intersect::hit()
{
int i;
double minHit = DBL_MAX;
uint32_t curHit = -1;
for(i = 0; i < this->num; i++)
{
if ((this->list[i].t >= 0) && (this->list[i].t < minHit))
{
curHit = i;
minHit = this->list[i].t;
}
}
if (curHit == -1)
{
return Intersection(0, nullptr);
}
return this->list[curHit];
}

View File

@@ -22,3 +22,8 @@ bool double_equal(double a, double b)
{
return fabs(a - b) < current_precision;
}
double deg_to_rad(double deg)
{
return deg * M_PI / 180.;
}

View File

@@ -9,7 +9,7 @@
#include <stdio.h>
#include <matrix.h>
#include <tuples.h>
#include <tuple.h>
#include <math_helper.h>
Matrix::Matrix(int width)

31
source/objects/object.cpp Normal file
View File

@@ -0,0 +1,31 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Object implementation
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <ray.h>
#include <object.h>
#include <matrix.h>
#include <tuple.h>
#include <intersect.h>
Object::Object()
{
this->transformMatrix = Matrix4().identity();
this->inverseTransform = this->transformMatrix.inverse();
}
Intersect Object::intersect(Ray r)
{
return Intersect();
};
void Object::setTransform(Matrix transform)
{
this->transformMatrix = transform;
this->inverseTransform = transform.inverse();
}

8
source/objects/ray.cpp Normal file
View File

@@ -0,0 +1,8 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Ray implementation
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/

38
source/objects/sphere.cpp Normal file
View File

@@ -0,0 +1,38 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Sphere implementation
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <math.h>
#include <sphere.h>
#include <ray.h>
#include <tuple.h>
#include <intersect.h>
Intersect Sphere::intersect(Ray r)
{
Intersect ret;
double a, b, c, discriminant;
Ray transRay = this->invTransform(r);
Tuple sphere_to_ray = transRay.origin - Point(0, 0, 0);
a = transRay.direction.dot(transRay.direction);
b = 2 * transRay.direction.dot(sphere_to_ray);
c = sphere_to_ray.dot(sphere_to_ray) - 1;
discriminant = b * b - 4 * a * c;
if (discriminant >= 0)
{
ret.add(Intersection((-b - sqrt(discriminant)) / (2 * a), this));
ret.add(Intersection((-b + sqrt(discriminant)) / (2 * a), this));
}
return ret;
}

84
source/transformation.cpp Normal file
View File

@@ -0,0 +1,84 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Transformation implementation
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <math.h>
#include <transformation.h>
Matrix translation(double x, double y, double z)
{
Matrix ret = Matrix4().identity();
ret.set(0, 3, x);
ret.set(1, 3, y);
ret.set(2, 3, z);
return ret;
}
Matrix scaling(double x, double y, double z)
{
Matrix ret = Matrix4();
ret.set(0, 0, x);
ret.set(1, 1, y);
ret.set(2, 2, z);
ret.set(3, 3, 1);
return ret;
}
Matrix rotation_x(double angle)
{
Matrix ret = Matrix4().identity();
ret.set(1, 1, cos(angle));
ret.set(1, 2, -sin(angle));
ret.set(2, 1, sin(angle));
ret.set(2, 2, cos(angle));
return ret;
}
Matrix rotation_y(double angle)
{
Matrix ret = Matrix4().identity();
ret.set(0, 0, cos(angle));
ret.set(0, 2, sin(angle));
ret.set(2, 0, -sin(angle));
ret.set(2, 2, cos(angle));
return ret;
}
Matrix rotation_z(double angle)
{
Matrix ret = Matrix4().identity();
ret.set(0, 0, cos(angle));
ret.set(0, 1, -sin(angle));
ret.set(1, 0, sin(angle));
ret.set(1, 1, cos(angle));
return ret;
}
Matrix shearing(double Xy, double Xz, double Yx, double Yz, double Zx, double Zy)
{
Matrix ret = Matrix4().identity();
ret.set(0, 1, Xy);
ret.set(0, 2, Xz);
ret.set(1, 0, Yx);
ret.set(1, 2, Yz);
ret.set(2, 0, Zx);
ret.set(2, 1, Zy);
return ret;
}

View File

@@ -6,7 +6,7 @@
* Copyright (c) 2020 986-Studio.
*
*/
#include <tuples.h>
#include <tuple.h>
#include <math.h>
@@ -27,9 +27,10 @@ double Tuple::dot(const Tuple &b)
return this->x * b.x + this->y * b.y + this->z * b.z + this->w * b.w;
}
Vector Vector::cross(const Vector &b) const
Tuple Tuple::cross(const Tuple &b) const
{
return Vector(this->y * b.z - this->z * b.y,
this->z * b.x - this->x * b.z,
this->x * b.y - this->y * b.x);
return Tuple(this->y * b.z - this->z * b.y,
this->z * b.x - this->x * b.z,
this->x * b.y - this->y * b.x,
0);
}

View File

@@ -3,10 +3,22 @@ project(DoRayTested)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
set(TESTS_SRC tuples_test.cpp colour_test.cpp canvas_test.cpp matrix_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})
target_include_directories(testMyRays PUBLIC ../source/include)
target_sources(testMyRays PRIVATE ${TESTS_SRC})
target_link_libraries(testMyRays gtest gtest_main rayonnement Threads::Threads)
target_link_libraries(testMyRays gtest gtest_main rayonnement Threads::Threads)
gtest_discover_tests(testMyRays
WORKING_DIRECTORY ${PROJECT_DIR}
PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_DIR}"
)
add_executable(ch5_test)
target_include_directories(ch5_test PUBLIC ../source/include)
target_sources(ch5_test PRIVATE ch5_test.cpp)
target_link_libraries(ch5_test rayonnement)

46
tests/ch5_test.cpp Normal file
View File

@@ -0,0 +1,46 @@
/*
* 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 <ray.h>
#include <sphere.h>
#include <colour.h>
#include <canvas.h>
#include <transformation.h>
int main()
{
int x, y;
Canvas c = Canvas(100, 100);
Sphere s = Sphere();
Colour red = Colour(1, 0, 0);
Point cameraOrigin = Point(0, 0, -5);
double wallDistance = 10;
double wallSize = 7;
double pixelSize = wallSize / c.width;
for(y = 0; y < c.height; y++)
{
double worldY = (wallSize / 2) - pixelSize * y;
for(x = 0; x < c.width; x++)
{
double worldX = (wallSize / 2) - pixelSize * x;
Point position = Point(worldX, worldY, wallDistance);
Ray r = Ray(cameraOrigin, (position - cameraOrigin).normalise());
Intersect xs = s.intersect(r);
if (!xs.hit().nothing())
{
c.put_pixel(x, y, red);
}
}
}
c.SaveAsPNG("ch5_test.png");
return 0;
}

132
tests/intersect_test.cpp Normal file
View File

@@ -0,0 +1,132 @@
/*
* 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(Intersection(1.0, nullptr));
i.add(Intersection(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 = Intersection(3.5, &s);
ASSERT_EQ(i.t, 3.5);
ASSERT_EQ(i.object, (Object *)&s);
}
TEST(IntersectTest, Aggregating_intersections)
{
Sphere s = Sphere();
Intersection i1 = Intersection(1, &s);
Intersection i2 = Intersection(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);
}
TEST(IntersectTest, Intersect_sets_the_object_on_the_intersection)
{
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].object, (Object *)&s);
ASSERT_EQ(xs[1].object, (Object *)&s);
}
TEST(IntersectTest, The_hit_when_all_intersection_have_positive_t)
{
Sphere s = Sphere();
Intersect xs = Intersect();
Intersection i1 = Intersection(1, &s);
Intersection i2 = Intersection(2, &s);
xs.add(i1);
xs.add(i2);
Intersection i = xs.hit();
ASSERT_EQ(i, i1);
}
TEST(IntersectTest, The_hit_when_some_intersection_have_negative_t)
{
Sphere s = Sphere();
Intersect xs = Intersect();
Intersection i1 = Intersection(-1, &s);
Intersection i2 = Intersection(2, &s);
Intersection i3 = Intersection(12, &s);
xs.add(i1);
xs.add(i2);
xs.add(i3);
Intersection i = xs.hit();
ASSERT_EQ(i, i2);
}
TEST(IntersectTest, The_hit_when_all_intersection_have_negative_t)
{
Sphere s = Sphere();
Intersect xs = Intersect();
Intersection i1 = Intersection(-2, &s);
Intersection i2 = Intersection(-1, &s);
xs.add(i1);
xs.add(i2);
Intersection i = xs.hit();
ASSERT_TRUE(i.nothing());
}
TEST(IntersectTest, The_hit_is_always_the_lowest_nonnegative_intersection)
{
Sphere s = Sphere();
Intersect xs = Intersect();
Intersection i1 = Intersection(5, &s);
Intersection i2 = Intersection(7, &s);
Intersection i3 = Intersection(-3, &s);
Intersection i4 = Intersection(2, &s);
xs.add(i1);
xs.add(i2);
xs.add(i3);
xs.add(i4);
Intersection i = xs.hit();
ASSERT_EQ(i, i4);
}

View File

@@ -7,7 +7,7 @@
*
*/
#include <matrix.h>
#include <tuples.h>
#include <tuple.h>
#include <math.h>
#include <gtest/gtest.h>

64
tests/ray_test.cpp Normal file
View File

@@ -0,0 +1,64 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Ray unit tests
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <ray.h>
#include <transformation.h>
#include <object.h>
#include <gtest/gtest.h>
TEST(RayTest, Creating_a_ray_and_querying_it)
{
Point origin = Point(1, 2, 3);
Vector direction = Vector(4, 5, 6);
Ray r = Ray(origin, direction);
ASSERT_EQ(r.origin, origin);
ASSERT_EQ(r.direction, direction);
}
TEST(RayTest, Computing_a_point_from_a_distance)
{
Ray r = Ray(Point(2, 3, 4), Vector(1, 0, 0));
ASSERT_EQ(r.position(0), Point(2, 3, 4));
ASSERT_EQ(r.position(1), Point(3, 3, 4));
ASSERT_EQ(r.position(-1), Point(1, 3, 4));
ASSERT_EQ(r.position(2.5), Point(4.5, 3, 4));
}
TEST(RayTest, Translating_a_ray)
{
Ray r = Ray(Point(1, 2, 3), Vector(0, 1, 0));
Matrix m = translation(3, 4, 5);
Object o = Object();
o.setTransform(m);
Ray r2 = o.transform(r);
ASSERT_EQ(r2.origin, Point(4, 6, 8));
ASSERT_EQ(r2.direction, Vector(0, 1, 0));
}
TEST(RayTest, Scaling_a_ray)
{
Ray r = Ray(Point(1, 2, 3), Vector(0, 1, 0));
Matrix m = scaling(2, 3, 4);
Object o = Object();
o.setTransform(m);
Ray r2 = o.transform(r);
ASSERT_EQ(r2.origin, Point(2, 6, 12));
ASSERT_EQ(r2.direction, Vector(0, 3, 0));
}

108
tests/sphere_test.cpp Normal file
View File

@@ -0,0 +1,108 @@
/*
* 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 <transformation.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);
}
TEST(SphereTest, A_sphere_default_transformation)
{
Sphere s = Sphere();
ASSERT_EQ(s.transformMatrix, Matrix4().identity());
}
TEST(SphereTest, Changing_a_sphere_transformation)
{
Sphere s = Sphere();
Matrix t = translation(2, 3, 4);
s.setTransform(t);
ASSERT_EQ(s.transformMatrix, t);
}
TEST(SphereTest, Intersecting_a_scaled_sphere_with_a_ray)
{
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
Sphere s = Sphere();
s.setTransform(scaling(2, 2, 2));
Intersect xs = s.intersect(r);
ASSERT_EQ(xs.count(), 2);
ASSERT_EQ(xs[0].t, 3.0);
ASSERT_EQ(xs[1].t, 7.0);
}
TEST(SphereTest, Intersecting_a_translated_sphere_with_a_ray)
{
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
Sphere s = Sphere();
s.setTransform(translation(5, 0, 0));
Intersect xs = s.intersect(r);
ASSERT_EQ(xs.count(), 0);
}

View File

@@ -0,0 +1,191 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Transformations unit tests
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <transformation.h>
#include <tuple.h>
#include <math.h>
#include <gtest/gtest.h>
TEST(TransformationTest, Multiplying_by_a_translation_matrix)
{
Matrix transform = translation(5, -3, 2);
Point p = Point(-3, 4, 5);
ASSERT_EQ(transform * p, Point(2, 1, 7));
}
TEST(TransformationTest, Multiplying_by_the_inverse_of_a_translation_matrix)
{
Matrix transform = translation(5, -3, 2);
Matrix inv = transform.inverse();
Point p = Point(-3, 4, 5);
ASSERT_EQ(inv * p, Point(-8, 7, 3));
}
TEST(TransformationTest, Translation_does_not_affect_vectors)
{
Matrix transform = translation(5, -3, 2);
Vector v = Vector(-3, 4, 5);
ASSERT_EQ(transform * v, Vector(-3, 4, 5));
}
TEST(TransformationTest, A_scaling_matrix_applied_to_a_point)
{
Matrix transform = scaling(2, 3, 4);
Point p = Point(-4, 6, 8);
ASSERT_EQ(transform * p, Point(-8, 18, 32));
}
TEST(TransformationTest, A_scaling_matrix_applied_to_a_vector)
{
Matrix transform = scaling(2, 3, 4);
Vector v = Vector(-4, 6, 8);
ASSERT_EQ(transform * v, Vector(-8, 18, 32));
}
TEST(TransformationTest, Multiplaying_by_the_inverse_of_a_scaling_matrix)
{
Matrix transform = scaling(2, 3, 4);
Matrix inv = transform.inverse();
Vector v = Vector(-4, 6, 8);
ASSERT_EQ(inv * v, Vector(-2, 2, 2));
}
TEST(TransformationTest, Reflexion_is_scaling_by_a_negative_value)
{
Matrix transform = scaling(-1, 1, 1);
Point p = Point(2, 3, 4);
ASSERT_EQ(transform * p, Point(-2, 3, 4));
}
TEST(TransformationTest, Rotating_a_point_around_the_X_axis)
{
Point p = Point(0, 1, 0);
Matrix half_quarter = rotation_x(M_PI / 4.);
Matrix full_quarter = rotation_x(M_PI / 2.);
ASSERT_EQ(half_quarter * p, Point(0, sqrt(2)/2, sqrt(2)/2));
ASSERT_EQ(full_quarter * p, Point(0, 0, 1));
}
TEST(TransformationTest, The_inverse_of_an_x_rotation_rotates_in_the_opposite_direction)
{
Point p = Point(0, 1, 0);
Matrix half_quarter = rotation_x(M_PI / 4.);
Matrix inv = half_quarter.inverse();
ASSERT_EQ(inv * p, Point(0, sqrt(2)/2, -sqrt(2)/2));
}
TEST(TransformationTest, Rotating_a_point_around_the_Y_axis)
{
Point p = Point(0, 0, 1);
Matrix half_quarter = rotation_y(M_PI / 4.);
Matrix full_quarter = rotation_y(M_PI / 2.);
ASSERT_EQ(half_quarter * p, Point(sqrt(2)/2, 0, sqrt(2)/2));
ASSERT_EQ(full_quarter * p, Point(1, 0, 0));
}
TEST(TransformationTest, Rotating_a_point_around_the_Z_axis)
{
Point p = Point(0, 1, 0);
Matrix half_quarter = rotation_z(M_PI / 4.);
Matrix full_quarter = rotation_z(M_PI / 2.);
ASSERT_EQ(half_quarter * p, Point(-sqrt(2)/2, sqrt(2)/2, 0));
ASSERT_EQ(full_quarter * p, Point(-1, 0, 0));
}
TEST(TransformationTest, A_shearing_transformation_moves_x_in_proportion_to_y)
{
Matrix transform = shearing(1, 0, 0, 0, 0, 0);
Point p = Point(2, 3, 4);
ASSERT_EQ(transform * p, Point(5, 3, 4));
}
TEST(TransformationTest, A_shearing_transformation_moves_x_in_proportion_to_z)
{
Matrix transform = shearing(0, 1, 0, 0, 0, 0);
Point p = Point(2, 3, 4);
ASSERT_EQ(transform * p, Point(6, 3, 4));
}
TEST(TransformationTest, A_shearing_transformation_moves_y_in_proportion_to_x)
{
Matrix transform = shearing(0, 0, 1, 0, 0, 0);
Point p = Point(2, 3, 4);
ASSERT_EQ(transform * p, Point(2, 5, 4));
}
TEST(TransformationTest, A_shearing_transformation_moves_y_in_proportion_to_z)
{
Matrix transform = shearing(0, 0, 0, 1, 0, 0);
Point p = Point(2, 3, 4);
ASSERT_EQ(transform * p, Point(2, 7, 4));
}
TEST(TransformationTest, A_shearing_transformation_moves_z_in_proportion_to_x)
{
Matrix transform = shearing(0, 0, 0, 0, 1, 0);
Point p = Point(2, 3, 4);
ASSERT_EQ(transform * p, Point(2, 3, 6));
}
TEST(TransformationTest, A_shearing_transformation_moves_z_in_proportion_to_y)
{
Matrix transform = shearing(0, 0, 0, 0, 0, 1);
Point p = Point(2, 3, 4);
ASSERT_EQ(transform * p, Point(2, 3, 7));
}
TEST(TransformationTest, Individual_trnasformations_are_applied_in_sequence)
{
Point p = Point(1, 0, 1);
Matrix A = rotation_x(M_PI / 2.);
Matrix B = scaling(5, 5, 5);
Matrix C = translation(10, 5, 7);
Tuple p2 = A * p;
ASSERT_EQ(p2, Point(1, -1, 0));
Tuple p3 = B * p2;
ASSERT_EQ(p3, Point(5, -5, 0));
Tuple p4 = C * p3;
ASSERT_EQ(p4, Point(15, 0, 7));
}
TEST(TransformationTest, Chained_transformation_must_be_applied_in_reverse_order)
{
Point p = Point(1, 0, 1);
Matrix A = rotation_x(M_PI / 2.);
Matrix B = scaling(5, 5, 5);
Matrix C = translation(10, 5, 7);
Matrix T = C * B * A;
ASSERT_EQ(T * p, Point(15, 0, 7));
}

View File

@@ -6,7 +6,7 @@
* Copyright (c) 2020 986-Studio.
*
*/
#include <tuples.h>
#include <tuple.h>
#include <math.h>
#include <gtest/gtest.h>