-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_camera.c
139 lines (115 loc) · 4.72 KB
/
test_camera.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Authored by paulo-santana <[email protected]>
Copied and modified by vcwild <[email protected]> without strict licensing permission.
*/
#include "munit/munit.h"
#include "minirt.h"
#include <math.h>
// the pixel size for a horizontal canvas
MunitResult camera_test1(const MunitParameter params[], void *fixture)
{
t_camera *camera = new_camera(200, 125, M_PI_2);
munit_assert_float(camera->pixel_size, ==, 0.01);
destroy_camera(camera);
return (MUNIT_OK);
}
// the pixel size for a vertical canvas
MunitResult camera_test2(const MunitParameter params[], void *fixture)
{
t_camera *camera = new_camera(125, 200, M_PI_2);
munit_assert_float(camera->pixel_size, ==, 0.01);
destroy_camera(camera);
return (MUNIT_OK);
}
// constructing a ray through the center of the canvas
MunitResult camera_test3(const MunitParameter params[], void *fixture)
{
t_camera *camera = new_camera(201, 101, M_PI_2);
t_ray *ray = ray_for_pixel(camera, 100, 50);
t_tuple *expected_origin = new_point(0, 0, 0);
t_tuple *expected_direction = new_vector(0, 0, -1);
munit_assert_float(round_to(ray->origin->x), ==, expected_origin->x);
munit_assert_float(round_to(ray->origin->y), ==, expected_origin->y);
munit_assert_float(round_to(ray->origin->z), ==, expected_origin->z);
munit_assert_float(round_to(ray->origin->w), ==, expected_origin->w);
munit_assert_float(round_to(ray->direction->x), ==, expected_direction->x);
munit_assert_float(round_to(ray->direction->y), ==, expected_direction->y);
munit_assert_float(round_to(ray->direction->z), ==, expected_direction->z);
munit_assert_float(round_to(ray->direction->w), ==, expected_direction->w);
destroy_camera(camera);
destroy_ray(ray);
free(expected_direction);
free(expected_origin);
return (MUNIT_OK);
}
// constructing a ray through the corner of the canvas
MunitResult camera_test4(const MunitParameter params[], void *fixture)
{
t_camera *camera = new_camera(201, 101, M_PI_2);
t_ray *ray = ray_for_pixel(camera, 0, 0);
t_tuple *expected_origin = new_point(0, 0, 0);
t_tuple *expected_direction = new_vector(0.66519, 0.33259, -0.66851);
munit_assert_float(round_to(ray->origin->x), ==, expected_origin->x);
munit_assert_float(round_to(ray->origin->y), ==, expected_origin->y);
munit_assert_float(round_to(ray->origin->z), ==, expected_origin->z);
munit_assert_float(round_to(ray->origin->w), ==, expected_origin->w);
munit_assert_float(round_to(ray->direction->x), ==, expected_direction->x);
munit_assert_float(round_to(ray->direction->y), ==, expected_direction->y);
munit_assert_float(round_to(ray->direction->z), ==, expected_direction->z);
munit_assert_float(round_to(ray->direction->w), ==, expected_direction->w);
destroy_camera(camera);
destroy_ray(ray);
free(expected_direction);
free(expected_origin);
return (MUNIT_OK);
}
// constructing a ray when the camera is transformed
MunitResult camera_test5(const MunitParameter params[], void *fixture)
{
t_camera *camera = new_camera(201, 101, M_PI_2);
t_matrix *rot = rotation_y(M_PI_4);
t_matrix *trans = translation(0, -2, 5);
set_camera_transform(camera, matrix_multiply(rot, trans));
t_ray *ray = ray_for_pixel(camera, 100, 50);
t_tuple *expected_origin = new_point(0, 2, -5);
t_tuple *expected_direction = new_vector(M_SQRT2 / 2, 0, -M_SQRT2 / 2);
munit_assert_float(ray->origin->x, ==, expected_origin->x);
munit_assert_float(ray->origin->y, ==, expected_origin->y);
munit_assert_float(ray->origin->z, ==, expected_origin->z);
munit_assert_float(ray->origin->w, ==, expected_origin->w);
munit_assert_float(ray->direction->x, ==, expected_direction->x);
munit_assert_float(ray->direction->y, ==, expected_direction->y);
munit_assert_float(ray->direction->z, ==, expected_direction->z);
munit_assert_float(ray->direction->w, ==, expected_direction->w);
destroy_camera(camera);
destroy_ray(ray);
free(expected_direction);
free(expected_origin);
free(rot);
free(trans);
return (MUNIT_OK);
}
// rendering a world with a camera
MunitResult camera_test6(const MunitParameter params[], void *fixture)
{
t_world *world = default_world();
t_camera *camera = new_camera(11, 11, M_PI_2);
t_tuple *from = new_point(0, 0, -5);
t_tuple *to = new_point(0, 0, 0);
t_tuple *up = new_vector(0, 1, 0);
set_camera_transform(camera, view_transform(from, to, up));
t_canvas *image = render(camera, world);
t_color *color = get_pixel(image, 5, 5);
t_color *expected_color = new_color(0.38066, 0.47583, 0.2855);
munit_assert_float(round_to(color->r), ==, expected_color->r);
munit_assert_float(round_to(color->g), ==, expected_color->g);
munit_assert_float(round_to(color->b), ==, expected_color->b);
free(expected_color);
free(from);
free(to);
free(up);
destroy_camera(camera);
destroy_world(world);
destroy_canvas(image);
return (MUNIT_OK);
}