Smooth triangles! And support for them in the OBJ File parser.

Also add an interesting tea party scene!
This commit is contained in:
Godzil
2020-03-06 15:07:26 +00:00
parent 73012b6dd1
commit e57b5715e8
38 changed files with 705 additions and 75 deletions

View File

@@ -10,7 +10,7 @@ link_libraries(rayonnement)
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 cone_test.cpp group_test.cpp
boundingbox_test.cpp triangle_test.cpp sequence_test.cpp objfile_test.cpp)
boundingbox_test.cpp triangle_test.cpp sequence_test.cpp objfile_test.cpp smoothtriangle_test.cpp)
add_executable(testMyRays)
target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
@@ -147,6 +147,7 @@ add_test(NAME Chapter12_Test COMMAND $<TARGET_FILE:ch12_test>)
add_test(NAME Chapter13_Test COMMAND $<TARGET_FILE:ch13_test>)
add_test(NAME Chapter13_ConeBonus COMMAND $<TARGET_FILE:ch13_cone>)
add_test(NAME Chapter14_Test COMMAND $<TARGET_FILE:ch14_test>)
add_test(NAME Chapter15_Teapots COMMAND $<TARGET_FILE:ch15_teapot_objfile>)
add_test(NAME AreaLight_Test COMMAND $<TARGET_FILE:arealight_test>)
add_test(NAME UVMap_CheckeredSphere COMMAND $<TARGET_FILE:uvmap_checkeredsphere>)
add_test(NAME UVMap_CheckeredPlane COMMAND $<TARGET_FILE:uvmap_checkeredplane>)

View File

@@ -80,7 +80,7 @@ int main()
/* ----------------------------- */
/* Set the camera */
Camera camera = Camera(400, 160, 0.7854);
Camera camera = Camera(40, 16, 0.7854);
camera.setTransform(viewTransform(Point(-3, 1, 2.5),
Point(0, 0.5, 0),
Vector(0, 1, 0)));

View File

@@ -104,7 +104,7 @@ int main()
bg4.material.shininess = 50;
w.addObject(&bg4);
/* Forground balls */
/* Foreground balls */
/* Red sphere */
Sphere redBall = Sphere();

View File

@@ -28,9 +28,9 @@ int main()
World w = World();
/* Add lights */
Light light1 = Light(POINT_LIGHT, Point(0, 20, 2), Colour(1, 1, 1));
Light light1 = Light(POINT_LIGHT, Point(50, 100, 20), Colour(.5, .5, .5));
w.addLight(&light1);
Light light2 = Light(POINT_LIGHT, Point(0, 2, 20), Colour(1, 1, 1));
Light light2 = Light(POINT_LIGHT, Point(2, 50, 100), Colour(.5, .5, .5));
w.addLight(&light2);
/* ----------------------------- */
@@ -42,6 +42,7 @@ int main()
p.material.ambient = 1;
p.material.diffuse = 0;
p.material.specular = 0;
p.material.reflective = 0.1;
w.addObject(&p);
Plane p2 = Plane();
@@ -53,26 +54,31 @@ int main()
w.addObject(&p2);
OBJFile teapot = OBJFile("teapot-low.obj");
teapot.setTransform(rotationY(M_PI) * rotationX(-M_PI/2) * scaling(0.4, 0.4, 0.4));
teapot.material.colour = Colour(1, 0.2, 0.1);
teapot.material.ambient = 0.2;
teapot.material.specular = 0.2;
teapot.material.diffuse = 20;
teapot.setTransform(translation(7, 0, 3) * rotationY(M_PI*23/22) * rotationX(-M_PI/2) * scaling(0.3, 0.3, 0.3));
teapot.material.colour = Colour(1, 0.3, 0.2);
teapot.material.shininess = 5;
teapot.material.specular = 0.4;
w.addObject(&teapot);
/* ----------------------------- */
OBJFile teapot2 = OBJFile("teapot-lowtri.obj");
teapot2.setTransform(translation(-7, 0, 3) * rotationY(-M_PI*46/22) * rotationX(-M_PI/2) * scaling(0.3, 0.3, 0.3));
teapot2.material.colour = Colour(1, 0.3, 0.2);
teapot2.material.shininess = 5;
teapot2.material.specular = 0.4;
w.addObject(&teapot2);
FILE *fpOut = fopen("teapot_worlddump.json", "wt");
if (fpOut)
{
w.dumpMe(fpOut);
fclose(fpOut);
}
OBJFile teapot3= OBJFile("teapot.obj");
teapot3.setTransform(translation(0, 0, -5) * rotationY(-M_PI) * rotationX(-M_PI/2) * scaling(0.4, 0.4, 0.4));
teapot3.material.colour = Colour(0.3, 1, 0.2);
teapot3.material.shininess = 5;
teapot3.material.specular = 0.4;
teapot3.material.reflective = 0.5;
w.addObject(&teapot3);
/* ----------------------------- */
/* Set the camera */
Camera camera = Camera(800, 400, M_PI/2);
Camera camera = Camera(80, 40, M_PI/2);
camera.setTransform(viewTransform(Point(0, 7, 13),
Point(0, 1, 0),
Vector(0, 1, 0)));

View File

@@ -10,6 +10,7 @@
#include <intersection.h>
#include <sphere.h>
#include <plane.h>
#include <triangle.h>
#include <transformation.h>
#include <gtest/gtest.h>
@@ -302,4 +303,13 @@ TEST(IntersectTest, The_Schlick_approximation_with_small_angle_and_n2_gt_n1)
ASSERT_TRUE(double_equal(reflectance, 0.48873));
set_equal_precision(FLT_EPSILON);
}
TEST(IntersectTest, An_intersection_can_encapsulage_u_and_v)
{
Triangle s = Triangle(Point(0, 1, 0), Point(-1, 0, 0), Point(1, 0, 0));
Intersection i = Intersection(3.5, &s, 0.2, 0.4);
ASSERT_EQ(i.u, 0.2);
ASSERT_EQ(i.v, 0.4);
}

View File

@@ -10,6 +10,7 @@
#include <math.h>
#include <gtest/gtest.h>
#include <triangle.h>
#include <smoothtriangle.h>
TEST(OBJFileTest, Ignoring_unrecognised_lines)
{
@@ -118,4 +119,58 @@ TEST(OBJFileTest, Triangle_in_groups)
ASSERT_EQ(t2->p1, parser.vertices(1));
ASSERT_EQ(t2->p2, parser.vertices(3));
ASSERT_EQ(t2->p3, parser.vertices(4));
}
TEST(OBJFileTest, Vertex_normal_record)
{
const char file[] = "vn 0 0 1\n"
"vn 0.707 0 -0.707\n"
"vn 1 2 3\n";
OBJFile parser = OBJFile();
parser.parseOBJFile(file);
ASSERT_EQ(parser.verticesNormal(1), Vector(0, 0, 1));
ASSERT_EQ(parser.verticesNormal(2), Vector(0.707, 0, -0.707));
ASSERT_EQ(parser.verticesNormal(3), Vector(1, 2, 3));
}
TEST(OBJFileTest, Faces_with_normal)
{
const char file[] = "v 0 1 0\n"
"v -1 0 0\n"
"v 1 0 0\n"
"\n"
"vn -1 0 0\n"
"vn 1 0 0\n"
"vn 0 1 0\n"
"\n"
"f 1//3 2//1 3//2\n"
"f 1/0/3 2/102/1 3/14/2\n";
OBJFile parser = OBJFile();
parser.parseOBJFile(file);
Group *g0 = parser.groups(0);
SmoothTriangle *t1 = (SmoothTriangle *)(*g0)[0];
SmoothTriangle *t2 = (SmoothTriangle *)(*g0)[1];
ASSERT_EQ(t1->p1, parser.vertices(1));
ASSERT_EQ(t1->p2, parser.vertices(2));
ASSERT_EQ(t1->p3, parser.vertices(3));
ASSERT_EQ(t1->n1, parser.verticesNormal(3));
ASSERT_EQ(t1->n2, parser.verticesNormal(1));
ASSERT_EQ(t1->n3, parser.verticesNormal(2));
ASSERT_EQ(t2->p1, parser.vertices(1));
ASSERT_EQ(t2->p2, parser.vertices(2));
ASSERT_EQ(t2->p3, parser.vertices(3));
ASSERT_EQ(t2->n1, parser.verticesNormal(3));
ASSERT_EQ(t2->n2, parser.verticesNormal(1));
ASSERT_EQ(t2->n3, parser.verticesNormal(2));
}

View File

@@ -0,0 +1,83 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Smooth Triangle unit tests
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <smoothtriangle.h>
#include <math.h>
#include <gtest/gtest.h>
class SmoothTriTest : public SmoothTriangle
{
public:
SmoothTriTest(Point p1, Point p2, Point p3, Vector n1, Vector n2, Vector n3) : SmoothTriangle(p1, p2, p3, n1, n2, n3) {};
Intersect doLocalIntersect(Ray ray)
{
return this->localIntersect(ray);
};
Tuple doLocalNormalAt(Tuple point, Intersection *hit)
{
return this->localNormalAt(point, hit);
};
};
Point p1 = Point(0, 1, 0);
Point p2 = Point(-1, 0, 0);
Point p3 = Point(1, 0, 0);
Vector n1 = Vector(0, 1, 0);
Vector n2 = Vector(-1, 0, 0);
Vector n3 = Vector(1, 0, 0);
SmoothTriTest tri = SmoothTriTest(p1, p2, p3, n1, n2, n3);
TEST(SmoothTriangleTest, Construcring_a_smooth_triangle)
{
ASSERT_EQ(tri.p1, p1);
ASSERT_EQ(tri.p2, p2);
ASSERT_EQ(tri.p3, p3);
ASSERT_EQ(tri.n1, n1);
ASSERT_EQ(tri.n2, n2);
ASSERT_EQ(tri.n3, n3);
}
TEST(SmoothTriangleTest, An_intersection_with_a_smooth_triangle_stores_u_v)
{
Ray r = Ray(Point(-0.2, 0.3, -2), Vector(0, 0, 1));
Intersect xs = tri.doLocalIntersect(r);
ASSERT_TRUE(double_equal(xs[0].u, 0.45));
ASSERT_TRUE(double_equal(xs[0].v, 0.25));
}
TEST(SmoothTriangleTest, A_smooth_triangle_uses_u_v_to_interpolate_the_normal)
{
Intersection i = Intersection(1, &tri, 0.45, 0.25);
Tuple n = tri.doLocalNormalAt(Point(0, 0, 0), &i);
/* Temporary lower the precision */
set_equal_precision(0.00001);
ASSERT_EQ(n, Vector(-0.5547, 0.83205, 0));
set_equal_precision(FLT_EPSILON);
}
TEST(SmoothTriangleTest, Preparing_a_normal_on_a_smooth_triangle)
{
Intersection i = Intersection(1, &tri, 0.45, 0.25);
Tuple n = tri.normalAt(Point(0, 0, 0), &i);
/* Temporary lower the precision */
set_equal_precision(0.00001);
ASSERT_EQ(n, Vector(-0.5547, 0.83205, 0));
set_equal_precision(FLT_EPSILON);
}

View File

@@ -119,7 +119,7 @@ int main()
/* ----------------------------- */
/* Set the camera */
Camera camera = Camera(1280, 900, deg_to_rad(90));
Camera camera = Camera(40, 10, deg_to_rad(90));
camera.setTransform(viewTransform(Point(-2, 2.5, -3.5),
Point(2, 0, 3),
Vector(0, 1, 0)));