ray-tracer-challenge/test/test_shapes.c
2024-08-11 19:36:48 -05:00

122 lines
3.0 KiB
C

#include <stdbool.h>
#include <stdio.h>
#include "shapes.h"
#include "intersections_shapes.h"
#include "transformations.h"
#include "runner.h"
#include "rays.h"
#include "spheres.h"
static bool shapes_test_0(const char ** scenario)
{
*scenario = "A shape's default transformation";
struct shape s = shape();
struct mat4x4 identity = mat4x4_identity();
return mat4x4_equal(s.transform, identity);
}
static bool shapes_test_1(const char ** scenario)
{
*scenario = "Changing a shape's transformation";
struct shape s = shape();
struct mat4x4 t = translation(2.0f, 3.0f, 4.0f);
s.transform = t;
return mat4x4_equal(s.transform, t);
}
static bool shapes_test_2(const char ** scenario)
{
*scenario = "A shape has a default material";
struct shape s = shape();
struct material m = s.material;
return material_equal(m, material());
}
static bool shapes_test_3(const char ** scenario)
{
*scenario = "A shape may be assigned a material";
struct shape s = shape();
struct material m = material();
m.ambient = 1.0f;
s.material = m;
return material_equal(s.material, m);
}
static bool shapes_test_4(const char ** scenario)
{
*scenario = "Intersecting a scaled shape with a ray";
struct ray r = ray(point(0.0f, 0.0f, -5.0f), vector(0.0f, 0.0f, 1.0f));
struct shape s = shape();
s.transform = scaling(2.0f, 2.0f, 2.0f);
struct intersections xs;
xs.count = 0;
intersect(&s, r, &xs);
return
tuple_equal(intersections_saved_ray.origin, point(0.0f, 0.0f, -2.5f)) &&
tuple_equal(intersections_saved_ray.direction, vector(0.0f, 0.0f, 0.5f));
}
static bool shapes_test_5(const char ** scenario)
{
*scenario = "Intersecting a translated shape with a ray";
struct ray r = ray(point(0.0f, 0.0f, -5.0f), vector(0.0f, 0.0f, 1.0f));
struct shape s = shape();
s.transform = translation(5.0f, 0.0f, 0.0f);
struct intersections xs;
xs.count = 0;
intersect(&s, r, &xs);
return
tuple_equal(intersections_saved_ray.origin, point(-5.0f, 0.0f, -5.0f)) &&
tuple_equal(intersections_saved_ray.direction, vector(0.0f, 0.0f, 1.0f));
}
static bool shapes_test_6(const char ** scenario)
{
*scenario = "Computing the normal on a translated shape";
struct shape s = shape();
s.transform = translation(0.0f, 1.0f, 0.0f);
struct tuple n = normal_at(&s, point(0.0f, 1.70711f, -0.70711));
return tuple_equal(n, vector(0.0f, 0.70711f, -0.70711f));
}
static bool shapes_test_7(const char ** scenario)
{
*scenario = "Computing the normal on a transformed shape";
struct shape s = shape();
struct mat4x4 scale = scaling(1.0f, 0.5f, 1.0f);
struct mat4x4 rotate = rotation_z(pi / 5.0f);
struct mat4x4 m = mat4x4_mul_m(scale, rotate);
s.transform = m;
struct tuple n = normal_at(&s, point(0.0f, 0.7071067811865476, -0.7071067811865476));
return tuple_equal(n, vector(0.0f, 0.97014f, -0.24254f));
}
test_t shape_tests[] = {
shapes_test_0,
shapes_test_1,
shapes_test_2,
shapes_test_3,
shapes_test_4,
shapes_test_5,
shapes_test_6,
shapes_test_7,
};
RUNNER(shape_tests)