4 %% a simple raytracer written in Erlang
6 %% Copyright (c) 2008 Michael Ploujnikov
8 %% This program is free software: you can redistribute it and/or modify
9 %% it under the terms of the GNU General Public License as published by
10 %% the Free Software Foundation, either version 2 of the License, or
11 %% (at your option) any later version.
13 %% This program is distributed in the hope that it will be useful,
14 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
15 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 %% GNU General Public License for more details.
18 %% You should have received a copy of the GNU General Public License
19 %% along with this program. If not, see <http://www.gnu.org/licenses/>.
22 %% * three object types:
25 %% * triangles (not done)
27 %% * shadows (not done)
28 %% * lighting based on local illumination models
29 %% * ambient (not done)
32 %% * attenuation (not done)
33 %% * reflections to a fixed depth
34 %% * PPM output file format
35 %% * randomly generated scene (not done)
36 %% * useful test suite (working but not very friendly when fails)
38 %% * distributed (across multiple computers) (not done)
52 raytraced_pixel_list_simple
/4,
53 raytraced_pixel_list_concurrent
/4
56 -record(vector
, {x
, y
, z
}).
57 -record(colour
, {r
, g
, b
}).
58 -record(ray
, {origin
, direction
}).
59 -record(screen
, {width
, height
}). % screen dimensions in the 3D world
60 -record(camera
, {location
, rotation
, fov
, screen
}).
61 -record(material
, {colour
, specular_power
, shininess
, reflectivity
}).
62 -record(sphere
, {radius
, center
, material
}).
63 -record(triangle
, {v1
, v2
, v3
, material
}).
64 -record(plane
, {normal
, distance
, material
}).
65 -record(point_light
, {diffuse_colour
, location
, specular_colour
}).
66 -define(BACKGROUND_COLOUR
, #colour
{r
=0, g
=0, b
=0}).
67 -define(ERROR_COLOUR
, #colour
{r
=1, g
=0, b
=0}).
68 -define(UNKNOWN_COLOUR
, #colour
{r
=0, g
=1, b
=0}).
69 -define(FOG_DISTANCE
, 40).
71 raytraced_pixel_list_simple(0, 0, _
, _
) ->
73 raytraced_pixel_list_simple(Width
, Height
, Scene
, Recursion_depth
)
74 when Width
> 0, Height
> 0 ->
79 % coordinates passed as a percentage
81 trace_ray_through_pixel(
82 {X
/Width
, Y
/Height
}, Scene
, Recursion_depth
))} end,
83 lists:seq(0, Width
- 1)) end,
84 lists:seq(0, Height
- 1)).
86 raytraced_pixel_list_concurrent(0, 0, _
, _
) ->
88 raytraced_pixel_list_concurrent(Width
, Height
, Scene
, Recursion_depth
)
89 when Width
> 0, Height
> 0 ->
90 Master_PID
= spawn(raytracer
, master
, [self(), Width
*Height
]),
95 % coordinates passed as a percentage
96 spawn(raytracer
, worker
,
97 [Master_PID
, X
+Y
*Width
, {X
/Width
, Y
/Height
}, Scene
, Recursion_depth
]) end,
98 lists:seq(0, Width
- 1)) end,
99 lists:seq(0, Height
- 1)),
100 io:format("all workers have been spawned~n", []),
106 master(Program_PID
, Pixel_count
) ->
107 master(Program_PID
, Pixel_count
, []).
108 master(Program_PID
, 0, Pixel_list
) ->
109 io:format("master is done~n", []),
110 Program_PID
! lists:keysort(1, Pixel_list
);
111 % assumes all workers eventually return a good value
112 master(Program_PID
, Pixel_count
, Pixel_list
) ->
115 master(Program_PID
, Pixel_count
-1, [Pixel_tuple
|Pixel_list
])
119 % assumes X and Y are percentages of the screen dimensions
120 worker(Master_PID
, Pixel_num
, {X
, Y
}, Scene
, Recursion_depth
) ->
121 Master_PID
! {Pixel_num
,
122 colour_to_pixel(trace_ray_through_pixel({X
, Y
}, Scene
, Recursion_depth
))}.
124 trace_ray_through_pixel({X
, Y
}, [Camera
|Rest_of_scene
], Recursion_depth
) ->
125 pixel_colour_from_ray(
126 ray_through_pixel(X
, Y
, Camera
),
130 pixel_colour_from_ray(_Ray
, _Scene
, 0) ->
131 #colour
{r
=0, g
=0, b
=0};
132 pixel_colour_from_ray(Ray
, Scene
, Recursion_depth
) ->
133 case nearest_object_intersecting_ray(Ray
, Scene
) of
134 {Nearest_object
, _Distance
, Hit_location
, Hit_normal
} ->
135 %io:format("hit: ~w~n", [{Nearest_object, _Distance}]),
137 vector_to_colour(lighting_function(Ray
,
149 lighting_function(Ray
, Object
, Hit_location
, Hit_normal
, Scene
,
152 fun (#point_light
{diffuse_colour
=Light_colour
,
153 location
=Light_location
,
154 specular_colour
=Specular_colour
},
159 pixel_colour_from_ray(
160 #ray
{origin
=Hit_location
,
161 direction
=vector_bounce_off_plane(
162 Ray#ray
.direction
, Hit_normal
)},
165 object_reflectivity(Object
)),
167 vector_component_mult(
168 colour_to_vector(Light_colour
),
174 specular_term(Ray#ray
.direction
,
178 object_specular_power(Object
),
179 object_shininess(Object
),
182 (_Not_a_point_light
, Final_colour
) ->
185 #vector
{x
=0, y
=0, z
=0},
188 diffuse_term(Object
, Light_location
, Hit_location
, Hit_normal
) ->
190 colour_to_vector(object_diffuse_colour(Object
)),
192 vector_dot_product(Hit_normal
,
194 vector_sub(Light_location
,
197 specular_term(EyeVector
, Light_location
, Hit_location
, Hit_normal
,
198 Specular_power
, Shininess
, Specular_colour
) ->
200 colour_to_vector(Specular_colour
),
207 vector_sub(Light_location
, Hit_location
)),
208 vector_neg(EyeVector
))),
209 Hit_normal
)]), Specular_power
)).
211 nearest_object_intersecting_ray(Ray
, Scene
) ->
212 nearest_object_intersecting_ray(
213 Ray
, none
, hitlocation
, hitnormal
, infinity
, Scene
).
214 nearest_object_intersecting_ray(
215 _Ray
, _NearestObj
, _Hit_location
, _Normal
, infinity
, []) ->
217 nearest_object_intersecting_ray(
218 _Ray
, NearestObj
, Hit_location
, Normal
, Distance
, []) ->
219 % io:format("intersecting ~w at ~w~n", [NearestObj, Distance]),
220 {NearestObj
, Distance
, Hit_location
, Normal
};
221 nearest_object_intersecting_ray(Ray
,
226 [CurrentObject
|Rest_of_scene
]) ->
227 NewDistance
= ray_object_intersect(Ray
, CurrentObject
),
228 %io:format("Distace=~w NewDistace=~w~n", [Distance, NewDistance]),
229 if (NewDistance
/= infinity
)
230 and ((Distance
== infinity
) or (Distance
> NewDistance
)) ->
231 %io:format("another closer object found~n", []),
233 vector_add(Ray#ray
.origin
,
234 vector_scalar_mult(Ray#ray
.direction
, NewDistance
)),
235 New_normal
= object_normal_at_point(
236 CurrentObject
, New_hit_location
),
237 nearest_object_intersecting_ray(
245 %io:format("no closer obj found~n", []),
246 nearest_object_intersecting_ray(Ray
,
254 ray_object_intersect(Ray
, Object
) ->
257 ray_sphere_intersect(Ray
, Object
);
259 ray_triangle_intersect(Ray
, Object
);
261 ray_plane_intersect(Ray
, Object
);
266 object_normal_at_point(#sphere
{center
=Center
}, Point
) ->
268 vector_sub(Point
, Center
));
269 object_normal_at_point(#plane
{normal
=Normal
}, _Point
) ->
272 ray_sphere_intersect(
277 #sphere
{radius
=Radius
, center
=#vector
{
278 x
=Xc
, y
=Yc
, z
=Zc
}}) ->
280 A
= Xd
*Xd
+ Yd
*Yd
+ Zd
*Zd
,
281 B
= 2 * (Xd
*(X0
-Xc
) + Yd
*(Y0
-Yc
) + Zd
*(Z0
-Zc
)),
282 C
= (X0
-Xc
)*(X0
-Xc
) + (Y0
-Yc
)*(Y0
-Yc
) + (Z0
-Zc
)*(Z0
-Zc
) - Radius
*Radius
,
283 Discriminant
= B
*B
- 4*A
*C
,
284 %io:format("A=~w B=~w C=~w discriminant=~w~n",
285 % [A, B, C, Discriminant]),
286 if Discriminant
>= Epsilon
->
287 T0
= (-B
+ math:sqrt(Discriminant
))/2,
288 T1
= (-B
- math:sqrt(Discriminant
))/2,
289 if (T0
>= 0) and (T1
>= 0) ->
290 %io:format("T0=~w T1=~w~n", [T0, T1]),
299 ray_triangle_intersect(_Ray
, _Triangle
) ->
302 ray_plane_intersect(Ray
, Plane
) ->
304 Vd
= vector_dot_product(Plane#plane
.normal
, Ray#ray
.direction
),
306 V0
= -(vector_dot_product(Plane#plane
.normal
, Ray#ray
.origin
)
307 + Plane#plane
.distance
),
309 if Distance
< Epsilon
->
319 focal_length(Angle
, Dimension
) ->
320 Dimension
/(2*math:tan(Angle
*(math:pi()/180)/2)).
322 point_on_screen(X
, Y
, Camera
) ->
323 %TODO: implement rotation (using quaternions)
324 Screen_width
= (Camera#camera
.screen
)#screen
.width
,
325 Screen_height
= (Camera#camera
.screen
)#screen
.height
,
326 lists:foldl(fun(Vect
, Sum
) -> vector_add(Vect
, Sum
) end,
327 Camera#camera
.location
,
329 #vector
{x
=0, y
=0, z
=1},
333 #vector
{x
= (X
-0.5) * Screen_width
,
337 y
= (Y
-0.5) * Screen_height
,
342 shoot_ray(From
, Through
) ->
343 #ray
{origin
=From
, direction
=vector_normalize(vector_sub(Through
, From
))}.
345 % assume that X and Y are percentages of the 3D world screen dimensions
346 ray_through_pixel(X
, Y
, Camera
) ->
347 shoot_ray(Camera#camera
.location
, point_on_screen(X
, Y
, Camera
)).
349 vectors_equal(V1
, V2
) ->
350 vectors_equal(V1
, V2
, 0.0001).
351 vectors_equal(V1
, V2
, Epsilon
) ->
352 (V1#vector
.x
+ Epsilon
>= V2#vector
.x
)
353 and (V1#vector
.x
- Epsilon
=<V2#vector
.x
)
354 and (V1#vector
.y
+ Epsilon
>= V2#vector
.y
)
355 and (V1#vector
.y
- Epsilon
=<V2#vector
.y
)
356 and (V1#vector
.z
+ Epsilon
>= V2#vector
.z
)
357 and (V1#vector
.z
- Epsilon
=<V2#vector
.z
).
360 vector_add(V1
, V2
) ->
361 #vector
{x
= V1#vector
.x
+ V2#vector
.x
,
362 y
= V1#vector
.y
+ V2#vector
.y
,
363 z
= V1#vector
.z
+ V2#vector
.z
}.
365 vector_sub(V1
, V2
) ->
366 #vector
{x
= V1#vector
.x
- V2#vector
.x
,
367 y
= V1#vector
.y
- V2#vector
.y
,
368 z
= V1#vector
.z
- V2#vector
.z
}.
370 vector_square_mag(#vector
{x
=X
, y
=Y
, z
=Z
}) ->
374 math:sqrt(vector_square_mag(V
)).
376 vector_scalar_mult(#vector
{x
=X
, y
=Y
, z
=Z
}, Scalar
) ->
377 #vector
{x
=X
*Scalar
, y
=Y
*Scalar
, z
=Z
*Scalar
}.
379 vector_component_mult(#vector
{x
=X1
, y
=Y1
, z
=Z1
}, #vector
{x
=X2
, y
=Y2
, z
=Z2
}) ->
380 #vector
{x
=X1
*X2
, y
=Y1
*Y2
, z
=Z1
*Z2
}.
382 vector_dot_product(#vector
{x
=A1
, y
=A2
, z
=A3
}, #vector
{x
=B1
, y
=B2
, z
=B3
}) ->
383 A1
*B1
+ A2
*B2
+ A3
*B3
.
385 vector_cross_product(#vector
{x
=A1
, y
=A2
, z
=A3
}, #vector
{x
=B1
, y
=B2
, z
=B3
}) ->
386 #vector
{x
= A2
*B3
- A3
*B2
,
390 vector_normalize(V
) ->
393 #vector
{x
=0, y
=0, z
=0};
395 vector_scalar_mult(V
, 1/vector_mag(V
))
398 vector_neg(#vector
{x
=X
, y
=Y
, z
=Z
}) ->
399 #vector
{x
=-X
, y
=-Y
, z
=-Z
}.
401 vector_bounce_off_plane(Vector
, Normal
) ->
405 2*vector_dot_product(Normal
, vector_neg(Vector
))),
408 object_diffuse_colour(#sphere
{material
=#material
{colour
=C
}}) ->
410 object_diffuse_colour(#plane
{material
=#material
{colour
=C
}}) ->
412 object_specular_power(#sphere
{material
=#material
{specular_power
=SP
}}) ->
414 object_specular_power(#plane
{material
=#material
{specular_power
=SP
}}) ->
417 object_shininess(#sphere
{material
=#material
{shininess
=S
}}) ->
419 object_shininess(#plane
{material
=#material
{shininess
=S
}}) ->
422 object_reflectivity(#sphere
{material
=#material
{reflectivity
=R
}}) ->
424 object_reflectivity(#plane
{material
=#material
{reflectivity
=R
}}) ->
427 point_on_sphere(#sphere
{radius
=Radius
, center
=#vector
{x
=XC
, y
=YC
, z
=ZC
}},
428 #vector
{x
=X
, y
=Y
, z
=Z
}) ->
431 ((X
-XC
)*(X
-XC
) + (Y
-YC
)*(Y
-YC
) + (Z
-ZC
)*(Z
-ZC
)) - Radius
*Radius
).
433 colour_to_vector(#colour
{r
=R
, g
=G
, b
=B
}) ->
434 #vector
{x
=R
, y
=G
, z
=B
}.
435 vector_to_colour(#vector
{x
=X
, y
=Y
, z
=Z
}) ->
436 #colour
{r
=X
, g
=Y
, b
=Z
}.
437 colour_to_pixel(#colour
{r
=R
, g
=G
, b
=B
}) ->
440 % returns a list of objects in the scene
441 % camera is assumed to be the first element in the scene
443 [#camera
{location
=#vector
{x
=0, y
=0, z
=-2},
444 rotation
=#vector
{x
=0, y
=0, z
=0},
446 screen
=#screen
{width
=4, height
=3}},
447 #point_light
{diffuse_colour
=#colour
{r
=1, g
=1, b
=0.5},
448 location
=#vector
{x
=5, y
=-2, z
=0},
449 specular_colour
=#colour
{r
=1, g
=1, b
=1}},
450 #point_light
{diffuse_colour
=#colour
{r
=1, g
=0, b
=0.5},
451 location
=#vector
{x
=-10, y
=0, z
=7},
452 specular_colour
=#colour
{r
=1, g
=0, b
=0.5}},
454 center
=#vector
{x
=4, y
=0, z
=10},
456 colour
=#colour
{r
=0, g
=0.5, b
=1},
461 center
=#vector
{x
=-5, y
=3, z
=9},
463 colour
=#colour
{r
=1, g
=0.5, b
=0},
468 center
=#vector
{x
=-4.5, y
=-2.5, z
=14},
470 colour
=#colour
{r
=0.5, g
=1, b
=0},
474 #triangle
{v1
=#vector
{x
=2, y
=1.5, z
=0},
475 v2
=#vector
{x
=2, y
=1.5, z
=10},
476 v3
=#vector
{x
=-2, y
=1.5, z
=0},
478 colour
=#colour
{r
=0.5, g
=0, b
=1},
482 #plane
{normal
=#vector
{x
=0, y
=-1, z
=0},
485 colour
=#colour
{r
=1, g
=1, b
=1},
492 % assumes Pixels are ordered in a row by row fasion
493 write_pixels_to_ppm(Width
, Height
, MaxValue
, Pixels
, Filename
) ->
494 case file:open(Filename
, write
) of
496 io:format("file opened~n", []),
497 io:format(IoDevice
, "P3~n", []),
498 io:format(IoDevice
, "~p ~p~n", [Width
, Height
]),
499 io:format(IoDevice
, "~p~n", [MaxValue
]),
501 fun({_Num
, {R
, G
, B
}}) ->
502 io:format(IoDevice
, "~p ~p ~p ",
503 [lists:min([trunc(R
*MaxValue
), MaxValue
]),
504 lists:min([trunc(G
*MaxValue
), MaxValue
]),
505 lists:min([trunc(B
*MaxValue
), MaxValue
])]) end,
507 file:close(IoDevice
);
509 io:format("error opening file~n", [])
512 % various invocation style functions
513 standalone([Width
, Height
, Filename
, Recursion_depth
, Strategy
]) ->
514 standalone(list_to_integer(Width
),
515 list_to_integer(Height
),
517 list_to_integer(Recursion_depth
),
518 tracing_function(list_to_atom(Strategy
))).
520 standalone(Width
, Height
, Filename
, Recursion_depth
, Function
) ->
521 {Time
, _Value
} = timer:tc(
529 io:format("Done in ~w seconds~n", [Time
/1000000]),
533 raytrace(tracing_function(Strategy
)).
535 go(Width
, Height
, Filename
, Recursion_depth
, Strategy
) ->
536 raytrace(Width
, Height
, Filename
, Recursion_depth
,
537 tracing_function(Strategy
)).
539 tracing_function(simple
) ->
540 fun raytraced_pixel_list_simple
/4;
541 tracing_function(concurrent
) ->
542 fun raytraced_pixel_list_concurrent
/4.
544 raytrace(Function
) ->
545 raytrace(4, 3, "/tmp/traced.ppm", 5, Function
).
546 raytrace(Width
, Height
, Filename
, Recursion_depth
, Function
) ->
560 Tests
= [fun scene_test
/0,
562 fun vector_equality_test
/0,
563 fun vector_addition_test
/0,
564 fun vector_subtraction_test
/0,
565 fun vector_square_mag_test
/0,
566 fun vector_mag_test
/0,
567 fun vector_scalar_multiplication_test
/0,
568 fun vector_dot_product_test
/0,
569 fun vector_cross_product_test
/0,
570 fun vector_normalization_test
/0,
571 fun vector_negation_test
/0,
572 % fun ray_through_pixel_test/0,
573 fun ray_shooting_test
/0,
574 fun point_on_screen_test
/0,
575 fun nearest_object_intersecting_ray_test
/0,
576 fun focal_length_test
/0,
577 % fun vector_rotation_test/0,
578 fun object_normal_at_point_test
/0,
579 fun vector_bounce_off_plane_test
/0,
580 fun ray_sphere_intersection_test
/0
582 run_tests(Tests
, 1, true
).
585 io:format("testing the scene function", []),
599 {colour
, 1, 0, 0.5}},
603 {material
, {colour
, 0, 0.5, 1}, 20, 1, 0.1}},
607 {material
, {colour
, 1, 0.5, 0}, 4, 0.25, 0.5}},
610 {vector
, -4.5, -2.5, 14},
611 {material
, {colour
, 0.5, 1, 0}, 20, 0.25, 0.7}},
614 {vector
, 2, 1.5, 10},
615 {vector
, -2, 1.5, 0},
616 {material
, {colour
, 0.5, 0, 1}, 40, 1, 1}},
620 {material
, {colour
, 1, 1, 1}, 1, 0, 0.01}}
628 io:format("this test always passes", []),
631 run_tests([], _Num
, Success
) ->
634 io:format("Success!~n", []),
637 io:format("some tests failed~n", []),
641 run_tests([First_test
|Rest_of_tests
], Num
, Success_so_far
) ->
642 io:format("test #~p: ", [Num
]),
643 Current_success
= First_test(),
644 case Current_success
of
646 io:format(" - OK~n", []);
648 io:format(" - FAILED~n", [])
650 run_tests(Rest_of_tests
, Num
+ 1, Current_success and Success_so_far
).
652 vector_equality_test() ->
653 io:format("vector equality"),
654 Vector1
= #vector
{x
=0, y
=0, z
=0},
655 Vector2
= #vector
{x
=1234, y
=-234, z
=0},
656 Vector3
= #vector
{x
=0.0983, y
=0.0214, z
=0.12342},
657 Vector4
= #vector
{x
=0.0984, y
=0.0213, z
=0.12341},
658 Vector5
= #vector
{x
=10/3, y
=-10/6, z
=8/7},
659 Vector6
= #vector
{x
=3.3, y
=-1.6, z
=1.1},
661 Subtest1
= vectors_equal(Vector1
, Vector1
)
662 and
vectors_equal(Vector2
, Vector2
)
663 and
not (vectors_equal(Vector1
, Vector2
))
664 and
not (vectors_equal(Vector2
, Vector1
)),
665 Subtest2
= vectors_equal(Vector3
, Vector4
, 0.0001),
666 Subtest3
= vectors_equal(Vector5
, Vector6
, 0.1),
668 Subtest1 and Subtest2 and Subtest3
.
671 vector_addition_test() ->
672 io:format("vector addition", []),
673 Vector0
= vector_add(
674 #vector
{x
=3, y
=7, z
=-3},
675 #vector
{x
=0, y
=-24, z
=123}),
676 Subtest1
= (Vector0#vector
.x
== 3)
677 and (Vector0#vector
.y
== -17)
678 and (Vector0#vector
.z
== 120),
680 Vector1
= #vector
{x
=5, y
=0, z
=984},
681 Vector2
= vector_add(Vector1
, Vector1
),
682 Subtest2
= (Vector2#vector
.x
== Vector1#vector
.x
*2)
683 and (Vector2#vector
.y
== Vector1#vector
.y
*2)
684 and (Vector2#vector
.z
== Vector1#vector
.z
*2),
686 Vector3
= #vector
{x
=908, y
=-098, z
=234},
687 Vector4
= vector_add(Vector3
, #vector
{x
=0, y
=0, z
=0}),
688 Subtest3
= vectors_equal(Vector3
, Vector4
),
690 Subtest1 and Subtest2 and Subtest3
.
692 vector_subtraction_test() ->
693 io:format("vector subtraction", []),
694 Vector1
= #vector
{x
=0, y
=0, z
=0},
695 Vector2
= #vector
{x
=8390, y
=-2098, z
=939},
696 Vector3
= #vector
{x
=1, y
=1, z
=1},
697 Vector4
= #vector
{x
=-1, y
=-1, z
=-1},
699 Subtest1
= vectors_equal(Vector1
, vector_sub(Vector1
, Vector1
)),
700 Subtest2
= vectors_equal(Vector3
, vector_sub(Vector3
, Vector1
)),
701 Subtest3
= not
vectors_equal(Vector3
, vector_sub(Vector1
, Vector3
)),
702 Subtest4
= vectors_equal(Vector4
, vector_sub(Vector4
, Vector1
)),
703 Subtest5
= not
vectors_equal(Vector4
, vector_sub(Vector1
, Vector4
)),
704 Subtest5
= vectors_equal(vector_add(Vector2
, Vector4
),
705 vector_sub(Vector2
, Vector3
)),
707 Subtest1 and Subtest2 and Subtest3 and Subtest4 and Subtest5
.
709 vector_square_mag_test() ->
710 io:format("vector square magnitude test", []),
711 Vector1
= #vector
{x
=0, y
=0, z
=0},
712 Vector2
= #vector
{x
=1, y
=1, z
=1},
713 Vector3
= #vector
{x
=3, y
=-4, z
=0},
715 Subtest1
= (0 == vector_square_mag(Vector1
)),
716 Subtest2
= (3 == vector_square_mag(Vector2
)),
717 Subtest3
= (25 == vector_square_mag(Vector3
)),
719 Subtest1 and Subtest2 and Subtest3
.
722 io:format("vector magnitude test", []),
723 Vector1
= #vector
{x
=0, y
=0, z
=0},
724 Vector2
= #vector
{x
=1, y
=1, z
=1},
725 Vector3
= #vector
{x
=3, y
=-4, z
=0},
727 Subtest1
= (0 == vector_mag(Vector1
)),
728 Subtest2
= (math:sqrt(3) == vector_mag(Vector2
)),
729 Subtest3
= (5 == vector_mag(Vector3
)),
731 Subtest1 and Subtest2 and Subtest3
.
733 vector_scalar_multiplication_test() ->
734 io:format("scalar multiplication test", []),
735 Vector1
= #vector
{x
=0, y
=0, z
=0},
736 Vector2
= #vector
{x
=1, y
=1, z
=1},
737 Vector3
= #vector
{x
=3, y
=-4, z
=0},
739 Subtest1
= vectors_equal(Vector1
, vector_scalar_mult(Vector1
, 45)),
740 Subtest2
= vectors_equal(Vector1
, vector_scalar_mult(Vector1
, -13)),
741 Subtest3
= vectors_equal(Vector1
, vector_scalar_mult(Vector3
, 0)),
742 Subtest4
= vectors_equal(#vector
{x
=4, y
=4, z
=4},
743 vector_scalar_mult(Vector2
, 4)),
744 Subtest5
= vectors_equal(Vector3
, vector_scalar_mult(Vector3
, 1)),
745 Subtest6
= not
vectors_equal(Vector3
, vector_scalar_mult(Vector3
, -3)),
747 Subtest1 and Subtest2 and Subtest3 and Subtest4 and Subtest5 and Subtest6
.
749 vector_dot_product_test() ->
750 io:format("dot product test", []),
751 Vector1
= #vector
{x
=1, y
=3, z
=-5},
752 Vector2
= #vector
{x
=4, y
=-2, z
=-1},
753 Vector3
= #vector
{x
=0, y
=0, z
=0},
754 Vector4
= #vector
{x
=1, y
=0, z
=0},
755 Vector5
= #vector
{x
=0, y
=1, z
=0},
757 Subtest1
= 3 == vector_dot_product(Vector1
, Vector2
),
758 Subtest2
= vector_dot_product(Vector2
, Vector2
)
759 == vector_square_mag(Vector2
),
760 Subtest3
= 0 == vector_dot_product(Vector3
, Vector1
),
761 Subtest4
= 0 == vector_dot_product(Vector4
, Vector5
),
763 Subtest1 and Subtest2 and Subtest3 and Subtest4
.
765 vector_cross_product_test() ->
766 io:format("cross product test", []),
767 Vector1
= #vector
{x
=0, y
=0, z
=0},
768 Vector2
= #vector
{x
=1, y
=0, z
=0},
769 Vector3
= #vector
{x
=0, y
=1, z
=0},
770 Vector4
= #vector
{x
=0, y
=0, z
=1},
771 Vector5
= #vector
{x
=1, y
=2, z
=3},
772 Vector6
= #vector
{x
=4, y
=5, z
=6},
773 Vector7
= #vector
{x
=-3, y
=6, z
=-3},
774 Vector8
= #vector
{x
=-1, y
=0, z
=0},
775 Vector9
= #vector
{x
=-9, y
=8, z
=433},
777 Subtest1
= vectors_equal(Vector1
, vector_cross_product(Vector2
, Vector2
)),
778 Subtest2
= vectors_equal(Vector1
, vector_cross_product(Vector2
, Vector8
)),
779 Subtest3
= vectors_equal(Vector2
, vector_cross_product(Vector3
, Vector4
)),
780 Subtest4
= vectors_equal(Vector7
, vector_cross_product(Vector5
, Vector6
)),
781 Subtest5
= vectors_equal(
782 vector_cross_product(Vector7
,
783 vector_add(Vector8
, Vector9
)),
785 vector_cross_product(Vector7
, Vector8
),
786 vector_cross_product(Vector7
, Vector9
))),
787 Subtest6
= vectors_equal(Vector1
,
790 vector_cross_product(
792 vector_cross_product(Vector8
, Vector9
)),
793 vector_cross_product(
795 vector_cross_product(Vector9
, Vector7
))),
796 vector_cross_product(
798 vector_cross_product(Vector7
, Vector8
)))),
800 Subtest1 and Subtest2 and Subtest3 and Subtest4 and Subtest5 and Subtest6
.
802 vector_normalization_test() ->
803 io:format("normalization test", []),
804 Vector1
= #vector
{x
=0, y
=0, z
=0},
805 Vector2
= #vector
{x
=1, y
=0, z
=0},
806 Vector3
= #vector
{x
=5, y
=0, z
=0},
808 Subtest1
= vectors_equal(Vector1
, vector_normalize(Vector1
)),
809 Subtest2
= vectors_equal(Vector2
, vector_normalize(Vector2
)),
810 Subtest3
= vectors_equal(Vector2
, vector_normalize(Vector3
)),
811 Subtest4
= vectors_equal(Vector2
, vector_normalize(
812 vector_scalar_mult(Vector2
, 324))),
814 Subtest1 and Subtest2 and Subtest3 and Subtest4
.
816 vector_negation_test() ->
817 io:format("vector negation test", []),
818 Vector1
= #vector
{x
=0, y
=0, z
=0},
819 Vector2
= #vector
{x
=4, y
=-5, z
=6},
821 Subtest1
= vectors_equal(Vector1
, vector_neg(Vector1
)),
822 Subtest2
= vectors_equal(Vector2
, vector_neg(vector_neg(Vector2
))),
824 Subtest1 and Subtest2
.
826 ray_shooting_test() ->
827 io:format("ray shooting test"),
828 Vector1
= #vector
{x
=0, y
=0, z
=0},
829 Vector2
= #vector
{x
=1, y
=0, z
=0},
831 Subtest1
= vectors_equal(
832 (shoot_ray(Vector1
, Vector2
))#ray
.direction
,
837 ray_sphere_intersection_test() ->
838 io:format("ray sphere intersection test", []),
842 center
=#vector
{x
= 0, y
=0, z
=10},
844 colour
=#colour
{r
=0.4, g
=0.4, b
=0.4}}},
846 origin
=#vector
{x
=0, y
=0, z
=0},
847 direction
=#vector
{x
=0, y
=0, z
=1}},
849 origin
=#vector
{x
=3, y
=0, z
=0},
850 direction
=#vector
{x
=0, y
=0, z
=1}},
852 origin
=#vector
{x
=4, y
=0, z
=0},
853 direction
=#vector
{x
=0, y
=0, z
=1}},
854 Subtest1
= ray_sphere_intersect(Ray1
, Sphere
) == 7.0,
855 Subtest2
= ray_sphere_intersect(Ray2
, Sphere
) == infinity
,
856 Subtest3
= ray_sphere_intersect(Ray3
, Sphere
) == infinity
,
857 Subtest1 and Subtest2 and Subtest3
.
859 point_on_screen_test() ->
860 io:format("point on screen test", []),
861 Camera1
= #camera
{location
=#vector
{x
=0, y
=0, z
=0},
862 rotation
=#vector
{x
=0, y
=0, z
=0},
864 screen
=#screen
{width
=1, height
=1}},
865 Camera2
= #camera
{location
=#vector
{x
=0, y
=0, z
=0},
866 rotation
=#vector
{x
=0, y
=0, z
=0},
868 screen
=#screen
{width
=640, height
=480}},
870 Subtest1
= vectors_equal(
871 #vector
{x
=0, y
=0, z
=0.5},
872 point_on_screen(0.5, 0.5, Camera1
)),
873 Subtest2
= vectors_equal(
874 #vector
{x
=-0.5, y
=-0.5, z
=0.5},
875 point_on_screen(0, 0, Camera1
)),
876 Subtest3
= vectors_equal(
877 #vector
{x
=0.5, y
=0.5, z
=0.5},
878 point_on_screen(1, 1, Camera1
)),
879 Subtest4
= vectors_equal(
880 point_on_screen(0, 0, Camera2
),
881 #vector
{x
=-320, y
=-240, z
=320}),
882 Subtest5
= vectors_equal(
883 point_on_screen(1, 1, Camera2
),
884 #vector
{x
=320, y
=240, z
=320}),
885 Subtest6
= vectors_equal(
886 point_on_screen(0.5, 0.5, Camera2
),
887 #vector
{x
=0, y
=0, z
=320}),
889 Subtest1 and Subtest2 and Subtest3 and Subtest4 and Subtest5 and Subtest6
.
891 nearest_object_intersecting_ray_test() ->
892 io:format("nearest object intersecting ray test", []),
893 % test to make sure that we really get the closest object
894 Sphere1
=#sphere
{radius
=5,
895 center
=#vector
{x
=0, y
=0, z
=10},
897 colour
=#colour
{r
=0, g
=0, b
=0.03}}},
898 Sphere2
=#sphere
{radius
=5,
899 center
=#vector
{x
=0, y
=0, z
=20},
901 colour
=#colour
{r
=0, g
=0, b
=0.06}}},
902 Sphere3
=#sphere
{radius
=5,
903 center
=#vector
{x
=0, y
=0, z
=30},
905 colour
=#colour
{r
=0, g
=0, b
=0.09}}},
906 Sphere4
=#sphere
{radius
=5,
907 center
=#vector
{x
=0, y
=0, z
=-10},
909 colour
=#colour
{r
=0, g
=0, b
=-0.4}}},
910 Scene1
=[Sphere1
, Sphere2
, Sphere3
, Sphere4
],
911 Ray1
=#ray
{origin
=#vector
{x
=0, y
=0, z
=0},
912 direction
=#vector
{x
=0, y
=0, z
=1}},
914 {Object1
, Distance1
, Hit_location
, Normal
} = nearest_object_intersecting_ray(
916 Subtest1
= (Object1
== Sphere1
) and (Distance1
== 5)
917 and
vectors_equal(Normal
, vector_neg(Ray1#ray
.direction
))
918 and
point_on_sphere(Sphere1
, Hit_location
),
922 focal_length_test() ->
925 io:format("focal length test", []),
927 fun({Focal_length
, Dimension
}, Matches
) ->
928 %Result = focal_length(Dimension, Size),
929 %io:format("comparing ~w ~w ~w ~w~n", [Focal_length, Dimension, Result, Matches]),
931 and ((Focal_length
+ Epsilon
>= focal_length(
933 and (Focal_length
- Epsilon
=< focal_length(
936 [{13, 108}, {15, 100.4}, {18, 90}, {21, 81.2}]).
938 object_normal_at_point_test() ->
939 io:format("object normal at point test"),
940 Sphere1
= #sphere
{radius
=13.5,
941 center
=#vector
{x
=0, y
=0, z
=0},
943 colour
=#colour
{r
=0, g
=0, b
=0}}},
944 Point1
= #vector
{x
=13.5, y
=0, z
=0},
945 Point2
= #vector
{x
=0, y
=13.5, z
=0},
946 Point3
= #vector
{x
=0, y
=0, z
=13.5},
947 Point4
= vector_neg(Point1
),
948 Point5
= vector_neg(Point2
),
949 Point6
= vector_neg(Point3
),
951 % sphere object tests
952 Subtest1
= vectors_equal(
953 vector_normalize(Point1
),
954 object_normal_at_point(Sphere1
, Point1
)),
955 Subtest2
= vectors_equal(
956 vector_normalize(Point2
),
957 object_normal_at_point(Sphere1
, Point2
)),
958 Subtest3
= vectors_equal(
959 vector_normalize(Point3
),
960 object_normal_at_point(Sphere1
, Point3
)),
961 Subtest4
= vectors_equal(
962 vector_normalize(Point4
),
963 object_normal_at_point(Sphere1
, Point4
)),
964 Subtest5
= vectors_equal(
965 vector_normalize(Point5
),
966 object_normal_at_point(Sphere1
, Point5
)),
967 Subtest6
= vectors_equal(
968 vector_normalize(Point6
),
969 object_normal_at_point(Sphere1
, Point6
)),
970 Subtest7
= not
vectors_equal(
971 vector_normalize(Point1
),
972 object_normal_at_point(Sphere1
, Point4
)),
974 Subtest1 and Subtest2 and Subtest3 and Subtest4 and Subtest5 and Subtest6
977 vector_bounce_off_plane_test() ->
978 io:format("vector reflect about normal", []),
979 Vector1
= #vector
{x
=1, y
=1, z
=0},
980 Vector2
= #vector
{x
=0, y
=-1, z
=0},
981 Vector3
= #vector
{x
=1, y
=-1, z
=0},
982 Vector4
= #vector
{x
=1, y
=0, z
=0},
984 Subtest1
= vectors_equal(vector_bounce_off_plane(
986 vector_normalize(Vector2
)),
989 Subtest2
= vectors_equal(
990 vector_bounce_off_plane(
992 vector_normalize(Vector1
)),
995 Subtest1 and Subtest2
.