Update README; code style changes

This commit is contained in:
Samrat Man Singh 2017-09-03 23:40:30 +05:45
parent dbb66fe05d
commit e3218f9741
3 changed files with 10 additions and 9 deletions

View file

@ -1,5 +1,7 @@
# rayml # rayml
![Example rendered scene](examples/demo.jpg)
A raytracer written in OCaml, following Peter Shirley's mini-book A raytracer written in OCaml, following Peter Shirley's mini-book
series [Raytracing in One Weekend][weekend]. series [Raytracing in One Weekend][weekend].

BIN
examples/demo.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 73 KiB

View file

@ -3,7 +3,7 @@ open Printf
open Vec3 open Vec3
open Ray open Ray
type material = Lambertian of Vec3.vec3 type material = Lambertian of Vec3.vec3 (* matte surface *)
| Metal of Vec3.vec3 | Metal of Vec3.vec3
| DummyNone | DummyNone
@ -46,20 +46,19 @@ let reflect v n =
let hit_scatter rin hit_rec = let hit_scatter rin hit_rec =
match hit_rec.mat with match hit_rec.mat with
Lambertian albedo -> Lambertian(albedo) ->
let target = (Vec3.add (Vec3.add hit_rec.p hit_rec.normal) (random_in_unit_sphere ())) in let target = (Vec3.add (Vec3.add hit_rec.p hit_rec.normal) (random_in_unit_sphere ())) in
let scatter = { ray = Ray.create hit_rec.p (Vec3.sub target hit_rec.p); let scatter = { ray = Ray.create hit_rec.p (Vec3.sub target hit_rec.p);
color = albedo; color = albedo;
scatter = true;} scatter = true;}
in scatter in scatter
| Metal albedo -> | Metal(albedo) ->
let reflected = reflect (Vec3.unit_vector rin.dir) hit_rec.normal in let reflected = reflect (Vec3.unit_vector rin.dir) hit_rec.normal in
let scattered_ray = Ray.create hit_rec.p reflected in let scattered_ray = Ray.create hit_rec.p reflected in
let scattered = { ray = scattered_ray; let scattered = { ray = scattered_ray;
color = albedo; color = albedo;
scatter = (Vec3.dot scattered_ray.dir hit_rec.normal) > 0.0;} in scatter = (Vec3.dot scattered_ray.dir hit_rec.normal) > 0.0;} in
scattered scattered
;;
let hit_sphere sphere ray (tmin, tmax) = let hit_sphere sphere ray (tmin, tmax) =
let oc = sub ray.origin sphere.center in let oc = sub ray.origin sphere.center in
@ -102,15 +101,15 @@ let rec hit_world world ray (tmin, tmax) =
p = Vec3.of_floats (-1., -1., -1.); p = Vec3.of_floats (-1., -1., -1.);
normal = Vec3.of_floats (-1., -1., -1.); normal = Vec3.of_floats (-1., -1., -1.);
mat = DummyNone} mat = DummyNone}
| Some r -> r in | Some(r) -> r in
match (hit h ray (tmin, prev_rec.t)) with match (hit h ray (tmin, prev_rec.t)) with
Some r -> Some r Some(r) -> Some r
| None -> acc) | None -> acc)
and hit h ray (tmin, tmax) = and hit h ray (tmin, tmax) =
match h with match h with
Sphere s -> hit_sphere s ray (tmin, tmax) Sphere(s) -> hit_sphere s ray (tmin, tmax)
| World w -> hit_world w ray (tmin, tmax) | World(w) -> hit_world w ray (tmin, tmax)
let rec get_color world ray depth = let rec get_color world ray depth =
@ -188,5 +187,5 @@ let write_to_file filename =
;; ;;
let () = let () =
write_to_file "matte.ppm" write_to_file "out.ppm"
;; ;;