updated the feature list - one feature added and finished, and one feature just finished
[eraytracer.git] / raytracer.erl
blobd001fde65925c44094cf138c0b661ed69fee41d7
2 %% raytracer.erl
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/>.
21 %% Features:
22 %% * three object types:
23 %% * spheres
24 %% * planes
25 %% * triangles (not done)
26 %% * point lights
27 %% * shadows (not done)
28 %% * lighting based on local illumination models
29 %% * ambient (not done)
30 %% * diffuse
31 %% * specular
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)
37 %% * concurrent
38 %% * distributed (across multiple computers) (not done)
42 -module(raytracer).
43 -export([go/1,
44 go/5,
45 raytrace/1,
46 raytrace/5,
47 run_tests/0,
48 master/2,
49 worker/5,
50 standalone/1,
51 standalone/5,
52 raytraced_pixel_list_simple/4,
53 raytraced_pixel_list_concurrent/4
54 ]).
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, _, _) ->
72 done;
73 raytraced_pixel_list_simple(Width, Height, Scene, Recursion_depth)
74 when Width > 0, Height > 0 ->
75 lists:flatmap(
76 fun(Y) ->
77 lists:map(
78 fun(X) ->
79 % coordinates passed as a percentage
80 {1, colour_to_pixel(
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, _, _) ->
87 done;
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]),
91 lists:flatmap(
92 fun(Y) ->
93 lists:map(
94 fun(X) ->
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", []),
101 receive
102 Final_pixel_list ->
103 Final_pixel_list
104 end.
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) ->
113 receive
114 Pixel_tuple ->
115 master(Program_PID, Pixel_count-1, [Pixel_tuple|Pixel_list])
116 end.
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),
127 Rest_of_scene,
128 Recursion_depth).
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,
138 Nearest_object,
139 Hit_location,
140 Hit_normal,
141 Scene,
142 Recursion_depth));
143 none ->
144 ?BACKGROUND_COLOUR;
145 _Else ->
146 ?ERROR_COLOUR
147 end.
149 lighting_function(Ray, Object, Hit_location, Hit_normal, Scene,
150 Recursion_depth) ->
151 lists:foldl(
152 fun (#point_light{diffuse_colour=Light_colour,
153 location=Light_location,
154 specular_colour=Specular_colour},
155 Final_colour) ->
156 vector_add(
157 vector_scalar_mult(
158 colour_to_vector(
159 pixel_colour_from_ray(
160 #ray{origin=Hit_location,
161 direction=vector_bounce_off_plane(
162 Ray#ray.direction, Hit_normal)},
163 Scene,
164 Recursion_depth-1)),
165 object_reflectivity(Object)),
166 vector_add(
167 vector_component_mult(
168 colour_to_vector(Light_colour),
169 vector_add(
170 diffuse_term(Object,
171 Light_location,
172 Hit_location,
173 Hit_normal),
174 specular_term(Ray#ray.direction,
175 Light_location,
176 Hit_location,
177 Hit_normal,
178 object_specular_power(Object),
179 object_shininess(Object),
180 Specular_colour))),
181 Final_colour));
182 (_Not_a_point_light, Final_colour) ->
183 Final_colour
184 end,
185 #vector{x=0, y=0, z=0},
186 Scene).
188 diffuse_term(Object, Light_location, Hit_location, Hit_normal) ->
189 vector_scalar_mult(
190 colour_to_vector(object_diffuse_colour(Object)),
191 lists:max([0,
192 vector_dot_product(Hit_normal,
193 vector_normalize(
194 vector_sub(Light_location,
195 Hit_location)))])).
197 specular_term(EyeVector, Light_location, Hit_location, Hit_normal,
198 Specular_power, Shininess, Specular_colour) ->
199 vector_scalar_mult(
200 colour_to_vector(Specular_colour),
201 Shininess*math:pow(
202 lists:max([0,
203 vector_dot_product(
204 vector_normalize(
205 vector_add(
206 vector_normalize(
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, []) ->
216 none;
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,
222 NearestObj,
223 Hit_location,
224 Normal,
225 Distance,
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", []),
232 New_hit_location =
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(
238 Ray,
239 CurrentObject,
240 New_hit_location,
241 New_normal,
242 NewDistance,
243 Rest_of_scene);
244 true ->
245 %io:format("no closer obj found~n", []),
246 nearest_object_intersecting_ray(Ray,
247 NearestObj,
248 Hit_location,
249 Normal,
250 Distance,
251 Rest_of_scene)
252 end.
254 ray_object_intersect(Ray, Object) ->
255 case Object of
256 #sphere{} ->
257 ray_sphere_intersect(Ray, Object);
258 #triangle{} ->
259 ray_triangle_intersect(Ray, Object);
260 #plane{} ->
261 ray_plane_intersect(Ray, Object);
262 _Else ->
263 infinity
264 end.
266 object_normal_at_point(#sphere{center=Center}, Point) ->
267 vector_normalize(
268 vector_sub(Point, Center));
269 object_normal_at_point(#plane{normal=Normal}, _Point) ->
270 Normal.
272 ray_sphere_intersect(
273 #ray{origin=#vector{
274 x=X0, y=Y0, z=Z0},
275 direction=#vector{
276 x=Xd, y=Yd, z=Zd}},
277 #sphere{radius=Radius, center=#vector{
278 x=Xc, y=Yc, z=Zc}}) ->
279 Epsilon = 0.001,
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]),
291 lists:min([T0, T1]);
292 true ->
293 infinity
294 end;
295 true ->
296 infinity
297 end.
299 ray_triangle_intersect(_Ray, _Triangle) ->
300 infinity.
302 ray_plane_intersect(Ray, Plane) ->
303 Epsilon = 0.001,
304 Vd = vector_dot_product(Plane#plane.normal, Ray#ray.direction),
305 if Vd < 0 ->
306 V0 = -(vector_dot_product(Plane#plane.normal, Ray#ray.origin)
307 + Plane#plane.distance),
308 Distance = V0 / Vd,
309 if Distance < Epsilon ->
310 infinity;
311 true ->
312 Distance
313 end;
314 true ->
315 infinity
316 end.
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,
328 [vector_scalar_mult(
329 #vector{x=0, y=0, z=1},
330 focal_length(
331 Camera#camera.fov,
332 Screen_width)),
333 #vector{x = (X-0.5) * Screen_width,
334 y=0,
335 z=0},
336 #vector{x=0,
337 y= (Y-0.5) * Screen_height,
338 z=0}
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}) ->
371 X*X + Y*Y + Z*Z.
373 vector_mag(V) ->
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,
387 y = A3*B1 - A1*B3,
388 z = A1*B2 - A2*B1}.
390 vector_normalize(V) ->
391 Mag = vector_mag(V),
392 if Mag == 0 ->
393 #vector{x=0, y=0, z=0};
394 true ->
395 vector_scalar_mult(V, 1/vector_mag(V))
396 end.
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) ->
402 vector_add(
403 vector_scalar_mult(
404 Normal,
405 2*vector_dot_product(Normal, vector_neg(Vector))),
406 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}) ->
429 Epsilon = 0.001,
430 Epsilon > abs(
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}) ->
438 {R, G, B}.
440 % returns a list of objects in the scene
441 % camera is assumed to be the first element in the scene
442 scene() ->
443 [#camera{location=#vector{x=0, y=0, z=-2},
444 rotation=#vector{x=0, y=0, z=0},
445 fov=90,
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}},
453 #sphere{radius=4,
454 center=#vector{x=4, y=0, z=10},
455 material=#material{
456 colour=#colour{r=0, g=0.5, b=1},
457 specular_power=20,
458 shininess=1,
459 reflectivity=0.1}},
460 #sphere{radius=4,
461 center=#vector{x=-5, y=3, z=9},
462 material=#material{
463 colour=#colour{r=1, g=0.5, b=0},
464 specular_power=4,
465 shininess=0.25,
466 reflectivity=0.5}},
467 #sphere{radius=4,
468 center=#vector{x=-4.5, y=-2.5, z=14},
469 material=#material{
470 colour=#colour{r=0.5, g=1, b=0},
471 specular_power=20,
472 shininess=0.25,
473 reflectivity=0.7}},
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},
477 material=#material{
478 colour=#colour{r=0.5, g=0, b=1},
479 specular_power=40,
480 shininess=1,
481 reflectivity=1}},
482 #plane{normal=#vector{x=0, y=-1, z=0},
483 distance=5,
484 material=#material{
485 colour=#colour{r=1, g=1, b=1},
486 specular_power=1,
487 shininess=0,
488 reflectivity=0.01}}
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
495 {ok, IoDevice} ->
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]),
500 lists:foreach(
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,
506 Pixels),
507 file:close(IoDevice);
508 error ->
509 io:format("error opening file~n", [])
510 end.
512 % various invocation style functions
513 standalone([Width, Height, Filename, Recursion_depth, Strategy]) ->
514 standalone(list_to_integer(Width),
515 list_to_integer(Height),
516 Filename,
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(
522 raytracer,
523 raytrace,
524 [Width,
525 Height,
526 Filename,
527 Recursion_depth,
528 Function]),
529 io:format("Done in ~w seconds~n", [Time/1000000]),
530 halt().
532 go(Strategy) ->
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) ->
547 write_pixels_to_ppm(
548 Width,
549 Height,
550 255,
551 Function(
552 Width,
553 Height,
554 scene(),
555 Recursion_depth),
556 Filename).
558 % testing
559 run_tests() ->
560 Tests = [fun scene_test/0,
561 fun passing_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).
584 scene_test() ->
585 io:format("testing the scene function", []),
586 case scene() of
587 [{camera,
588 {vector, 0, 0, -2},
589 {vector, 0, 0, 0},
591 {screen, 4, 3}},
592 {point_light,
593 {colour, 1, 1, 0.5},
594 {vector, 5, -2, 0},
595 {colour, 1, 1, 1}},
596 {point_light,
597 {colour, 1, 0, 0.5},
598 {vector, -10, 0, 7},
599 {colour, 1, 0, 0.5}},
600 {sphere,
602 {vector, 4, 0, 10},
603 {material, {colour, 0, 0.5, 1}, 20, 1, 0.1}},
604 {sphere,
606 {vector, -5, 3, 9},
607 {material, {colour, 1, 0.5, 0}, 4, 0.25, 0.5}},
608 {sphere,
610 {vector, -4.5, -2.5, 14},
611 {material, {colour, 0.5, 1, 0}, 20, 0.25, 0.7}},
612 {triangle,
613 {vector, 2, 1.5, 0},
614 {vector, 2, 1.5, 10},
615 {vector, -2, 1.5, 0},
616 {material, {colour, 0.5, 0, 1}, 40, 1, 1}},
617 {plane,
618 {vector, 0, -1, 0},
620 {material, {colour, 1, 1, 1}, 1, 0, 0.01}}
621 ] ->
622 true;
623 _Else ->
624 false
625 end.
627 passing_test() ->
628 io:format("this test always passes", []),
629 true.
631 run_tests([], _Num, Success) ->
632 case Success of
633 true ->
634 io:format("Success!~n", []),
636 _Else ->
637 io:format("some tests failed~n", []),
638 failed
639 end;
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
645 true ->
646 io:format(" - OK~n", []);
647 _Else ->
648 io:format(" - FAILED~n", [])
649 end,
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.
721 vector_mag_test() ->
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)),
784 vector_add(
785 vector_cross_product(Vector7, Vector8),
786 vector_cross_product(Vector7, Vector9))),
787 Subtest6 = vectors_equal(Vector1,
788 vector_add(
789 vector_add(
790 vector_cross_product(
791 Vector7,
792 vector_cross_product(Vector8, Vector9)),
793 vector_cross_product(
794 Vector8,
795 vector_cross_product(Vector9, Vector7))),
796 vector_cross_product(
797 Vector9,
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,
833 Vector2),
835 Subtest1.
837 ray_sphere_intersection_test() ->
838 io:format("ray sphere intersection test", []),
840 Sphere = #sphere{
841 radius=3,
842 center=#vector{x = 0, y=0, z=10},
843 material=#material{
844 colour=#colour{r=0.4, g=0.4, b=0.4}}},
845 Ray1 = #ray{
846 origin=#vector{x=0, y=0, z=0},
847 direction=#vector{x=0, y=0, z=1}},
848 Ray2 = #ray{
849 origin=#vector{x=3, y=0, z=0},
850 direction=#vector{x=0, y=0, z=1}},
851 Ray3 = #ray{
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},
863 fov=90,
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},
867 fov=90,
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},
896 material=#material{
897 colour=#colour{r=0, g=0, b=0.03}}},
898 Sphere2=#sphere{radius=5,
899 center=#vector{x=0, y=0, z=20},
900 material=#material{
901 colour=#colour{r=0, g=0, b=0.06}}},
902 Sphere3=#sphere{radius=5,
903 center=#vector{x=0, y=0, z=30},
904 material=#material{
905 colour=#colour{r=0, g=0, b=0.09}}},
906 Sphere4=#sphere{radius=5,
907 center=#vector{x=0, y=0, z=-10},
908 material=#material{
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(
915 Ray1, Scene1),
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),
920 Subtest1.
922 focal_length_test() ->
923 Epsilon = 0.1,
924 Size = 36,
925 io:format("focal length test", []),
926 lists:foldl(
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]),
930 Matches
931 and ((Focal_length + Epsilon >= focal_length(
932 Dimension, Size))
933 and (Focal_length - Epsilon =< focal_length(
934 Dimension, Size)))
935 end, true,
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},
942 material=#material{
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
975 and Subtest7.
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(
985 Vector1,
986 vector_normalize(Vector2)),
987 Vector3),
989 Subtest2 = vectors_equal(
990 vector_bounce_off_plane(
991 Vector2,
992 vector_normalize(Vector1)),
993 Vector4),
995 Subtest1 and Subtest2.