177 lines
5.4 KiB
C
177 lines
5.4 KiB
C
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
|
|
#include "patterns.h"
|
|
#include "spheres.h"
|
|
#include "transformations.h"
|
|
#include "runner.h"
|
|
|
|
#define black color(0.0f, 0.0f, 0.0f)
|
|
#define white color(1.0f, 1.0f, 1.0f)
|
|
|
|
static bool patterns_test_0(const char ** scenario)
|
|
{
|
|
*scenario = "Creating a stripe pattern";
|
|
|
|
struct pattern pattern = stripe_pattern(white, black);
|
|
|
|
return
|
|
tuple_equal(pattern.a, white) &&
|
|
tuple_equal(pattern.b, black);
|
|
}
|
|
|
|
static bool patterns_test_1(const char ** scenario)
|
|
{
|
|
*scenario = "A stripe pattern is constant in y";
|
|
|
|
struct pattern pattern = stripe_pattern(white, black);
|
|
|
|
return
|
|
tuple_equal(stripe_at(pattern, point(0.0f, 0.0f, 0.0f)), white) &&
|
|
tuple_equal(stripe_at(pattern, point(0.0f, 1.0f, 0.0f)), white) &&
|
|
tuple_equal(stripe_at(pattern, point(0.0f, 2.0f, 0.0f)), white);
|
|
}
|
|
|
|
static bool patterns_test_2(const char ** scenario)
|
|
{
|
|
*scenario = "A stripe pattern is constant in z";
|
|
|
|
struct pattern pattern = stripe_pattern(white, black);
|
|
|
|
return
|
|
tuple_equal(stripe_at(pattern, point(0.0f, 0.0f, 0.0f)), white) &&
|
|
tuple_equal(stripe_at(pattern, point(0.0f, 0.0f, 1.0f)), white) &&
|
|
tuple_equal(stripe_at(pattern, point(0.0f, 0.0f, 2.0f)), white);
|
|
}
|
|
|
|
static bool patterns_test_3(const char ** scenario)
|
|
{
|
|
*scenario = "A stripe pattern alternates in x";
|
|
|
|
struct pattern pattern = stripe_pattern(white, black);
|
|
|
|
return
|
|
tuple_equal(stripe_at(pattern, point(0.0f, 0.0f, 0.0f)), white) &&
|
|
tuple_equal(stripe_at(pattern, point(0.9f, 0.0f, 0.0f)), white) &&
|
|
tuple_equal(stripe_at(pattern, point(1.0f, 0.0f, 0.0f)), black) &&
|
|
tuple_equal(stripe_at(pattern, point(-0.1f, 0.0f, 0.0f)), black) &&
|
|
tuple_equal(stripe_at(pattern, point(-1.0f, 0.0f, 0.0f)), black) &&
|
|
tuple_equal(stripe_at(pattern, point(-1.1f, 0.0f, 0.0f)), white);
|
|
}
|
|
|
|
static bool patterns_test_4(const char ** scenario)
|
|
{
|
|
*scenario = "Stripes with an object transformation";
|
|
|
|
struct shape object = sphere();
|
|
object.transform = scaling(2.0f, 2.0f, 2.0f);
|
|
struct pattern pattern = stripe_pattern(white, black);
|
|
struct tuple c = pattern_at_object(pattern, object.transform, point(1.5f, 0.0f, 0.0f));
|
|
|
|
return tuple_equal(c, white);
|
|
}
|
|
|
|
static bool patterns_test_5(const char ** scenario)
|
|
{
|
|
*scenario = "Stripes with a pattern transformation";
|
|
|
|
struct shape object = sphere();
|
|
struct pattern pattern = stripe_pattern(white, black);
|
|
pattern.transform = scaling(2.0f, 2.0f, 2.0f);
|
|
struct tuple c = pattern_at_object(pattern, object.transform, point(1.5f, 0.0f, 0.0f));
|
|
|
|
return tuple_equal(c, white);
|
|
}
|
|
|
|
static bool patterns_test_6(const char ** scenario)
|
|
{
|
|
*scenario = "Stripes with both an object and a pattern transformation";
|
|
|
|
struct shape object = sphere();
|
|
object.transform = scaling(2.0f, 2.0f, 2.0f);
|
|
struct pattern pattern = stripe_pattern(white, black);
|
|
pattern.transform = translation(0.5f, 0.0f, 0.0f);
|
|
struct tuple c = pattern_at_object(pattern, object.transform, point(2.5f, 0.0f, 0.0f));
|
|
|
|
return tuple_equal(c, white);
|
|
}
|
|
|
|
static bool patterns_test_7(const char ** scenario)
|
|
{
|
|
*scenario = "A gradient linearly interpolates between colors";
|
|
|
|
struct pattern pattern = gradient_pattern(white, black);
|
|
|
|
return
|
|
tuple_equal(gradient_at(pattern, point(0.0f, 0.0f, 0.0f)), white) &&
|
|
tuple_equal(gradient_at(pattern, point(0.25f, 0.0f, 0.0f)), color(0.75f, 0.75f, 0.75f)) &&
|
|
tuple_equal(gradient_at(pattern, point(0.5f, 0.0f, 0.0f)), color(0.5f, 0.5f, 0.5f)) &&
|
|
tuple_equal(gradient_at(pattern, point(0.75f, 0.0f, 0.0f)), color(0.25f, 0.25f, 0.25f));
|
|
}
|
|
|
|
static bool patterns_test_8(const char ** scenario)
|
|
{
|
|
*scenario = "A ring should extend in both x and z";
|
|
|
|
struct pattern pattern = ring_pattern(white, black);
|
|
|
|
return
|
|
tuple_equal(ring_at(pattern, point(0.0f, 0.0f, 0.0f)), white) &&
|
|
tuple_equal(ring_at(pattern, point(1.0f, 0.0f, 0.0f)), black) &&
|
|
tuple_equal(ring_at(pattern, point(0.0f, 0.0f, 1.0f)), black) &&
|
|
tuple_equal(ring_at(pattern, point(0.708f, 0.0f, 0.708f)), black);
|
|
}
|
|
|
|
static bool patterns_test_9(const char ** scenario)
|
|
{
|
|
*scenario = "Checkers should repeat in x";
|
|
|
|
struct pattern pattern = checkers_pattern(white, black);
|
|
|
|
return
|
|
tuple_equal(checkers_at(pattern, point(0.0f, 0.0f, 0.0f)), white) &&
|
|
tuple_equal(checkers_at(pattern, point(0.99f, 0.0f, 0.0f)), white) &&
|
|
tuple_equal(checkers_at(pattern, point(1.01f, 0.0f, 0.0f)), black);
|
|
}
|
|
|
|
static bool patterns_test_10(const char ** scenario)
|
|
{
|
|
*scenario = "Checkers should repeat in y";
|
|
|
|
struct pattern pattern = checkers_pattern(white, black);
|
|
|
|
return
|
|
tuple_equal(checkers_at(pattern, point(0.0f, 0.0f, 0.0f)), white) &&
|
|
tuple_equal(checkers_at(pattern, point(0.0f, 0.99f, 0.0f)), white) &&
|
|
tuple_equal(checkers_at(pattern, point(0.0f, 1.01f, 0.0f)), black);
|
|
}
|
|
|
|
static bool patterns_test_11(const char ** scenario)
|
|
{
|
|
*scenario = "Checkers should repeat in z";
|
|
|
|
struct pattern pattern = checkers_pattern(white, black);
|
|
|
|
return
|
|
tuple_equal(checkers_at(pattern, point(0.0f, 0.0f, 0.0f)), white) &&
|
|
tuple_equal(checkers_at(pattern, point(0.0f, 0.0f, 0.99f)), white) &&
|
|
tuple_equal(checkers_at(pattern, point(0.0f, 0.0f, 1.01f)), black);
|
|
}
|
|
|
|
test_t patterns_tests[] = {
|
|
patterns_test_0,
|
|
patterns_test_1,
|
|
patterns_test_2,
|
|
patterns_test_3,
|
|
patterns_test_4,
|
|
patterns_test_5,
|
|
patterns_test_6,
|
|
patterns_test_7,
|
|
patterns_test_8,
|
|
patterns_test_9,
|
|
patterns_test_10,
|
|
patterns_test_11
|
|
};
|
|
|
|
RUNNER(patterns_tests);
|