Gamma correction

This commit is contained in:
Samrat Man Singh 2016-03-12 19:23:13 +05:45
parent 612e9d8af0
commit c092de81ce

View file

@ -16,14 +16,15 @@ type hit_rec = { t : float;
type hit = hit_rec option type hit = hit_rec option
let random_in_unit_sphere () = let random_in_unit_sphere () =
let p = ref (Vec3.of_floats ((Random.float 1.0), let p = ref (Vec3.sub (Vec3.mul 2.0 (Vec3.of_floats ((Random.float 1.0),
(Random.float 1.0), (Random.float 1.0),
(Random.float 1.0))) in (Random.float 1.0))))
(Vec3.of_floats (1., 1., 1.))) in
while ((Vec3.dot !p !p) >= 1.0) do while ((Vec3.dot !p !p) >= 1.0) do
p := Vec3.mul 2.0 (Vec3.sub (Vec3.of_floats ((Random.float 1.0), p := Vec3.sub (Vec3.mul 2.0 (Vec3.of_floats ((Random.float 1.0),
(Random.float 1.0), (Random.float 1.0),
(Random.float 1.0))) (Random.float 1.0))))
(Vec3.of_floats (1., 1., 1.))) (Vec3.of_floats (1., 1., 1.))
done; done;
!p !p
;; ;;
@ -38,22 +39,22 @@ let hit_sphere sphere ray (tmin, tmax) =
if (discriminant > 0.0) if (discriminant > 0.0)
then then
let t = (-.b +. (sqrt discriminant)) /. a in let t = (-.b -. (sqrt discriminant)) /. a in
if (t < tmax && t > tmin) if (t < tmax && t > tmin)
then then
let p = Ray.point_at_parameter ray t in let p = Ray.point_at_parameter ray t in
Some { t = t; Some { t = t;
p = p; p = p;
normal = unit_vector (sub p sphere.center); } normal = mul (1. /. sphere.radius) (sub p sphere.center); }
else else
let t = (-.b -. (sqrt discriminant)) /. a in let t = (-.b +. (sqrt discriminant)) /. a in
if (t < tmax && t > tmin) if (t < tmax && t > tmin)
then then
let p = Ray.point_at_parameter ray t in let p = Ray.point_at_parameter ray t in
Some { t = t; Some { t = t;
p = p; p = p;
normal = unit_vector (sub p sphere.center); } normal = mul (1. /. sphere.radius) (sub p sphere.center); }
else None else None
else None else None
@ -78,7 +79,7 @@ and hit h ray (tmin, tmax) =
let rec get_color world ray = let rec get_color world ray =
match (hit world ray (0., Float.infinity)) with match (hit world ray (0., Float.infinity)) with
Some hit_result -> Some hit_result ->
let target = Vec3.add (Vec3.add hit_result.p hit_result.normal) let target = Vec3.add (Vec3.add hit_result.p hit_result.normal)
(random_in_unit_sphere ()) in (random_in_unit_sphere ()) in
Vec3.mul 0.5 (get_color world {origin = hit_result.p; Vec3.mul 0.5 (get_color world {origin = hit_result.p;
@ -128,6 +129,10 @@ let write_to_file filename =
done; done;
color := Vec3.mul (1. /. (Float.of_int ns)) !color ; color := Vec3.mul (1. /. (Float.of_int ns)) !color ;
(* gamma correction *)
color := Vec3.of_floats (sqrt(!color.x),
sqrt(!color.y),
sqrt(!color.z));
let {x=r; y=g; z=b} = !color in let {x=r; y=g; z=b} = !color in
let (ir, ig, ib) = (Int.of_float (r*.255.99), let (ir, ig, ib) = (Int.of_float (r*.255.99),
Int.of_float (g*.255.99), Int.of_float (g*.255.99),
@ -139,3 +144,7 @@ let write_to_file filename =
done; done;
Out_channel.close oc; Out_channel.close oc;
;; ;;
let () =
write_to_file "matte.ppm"
;;