mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-06 03:05:30 -05:00
Initial commit
This commit is contained in:
commit
f9217e45ea
3 changed files with 112 additions and 0 deletions
63
src/raytracer.ml
Normal file
63
src/raytracer.ml
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
open Core.Std
|
||||
open Printf
|
||||
open Vec3
|
||||
|
||||
type sphere = { center: Vec3.vec3;
|
||||
radius: float }
|
||||
type ray = { origin: Vec3.vec3;
|
||||
dir: Vec3.vec3 }
|
||||
|
||||
let hit_sphere sphere ray =
|
||||
let oc = sub ray.dir sphere.center in
|
||||
let a = dot ray.dir ray.dir in
|
||||
let b = dot oc ray.dir in
|
||||
let c = (dot oc oc) -. (sphere.radius *. sphere.radius) in
|
||||
let discriminant = b*.b -. a*.c in
|
||||
|
||||
( discriminant > 0.0)
|
||||
|
||||
let get_color ray =
|
||||
if (hit_sphere {center = Vec3.of_floats (0., 0., -1.);
|
||||
radius = 0.5}
|
||||
ray)
|
||||
then Vec3.of_floats (1., 0., 0.)
|
||||
else let unit_direction = unit_vector ray.dir in
|
||||
let t = 0.5 *. (unit_direction.y +. 1.0) in
|
||||
add (mul (1.0 -. t) {x= 1.0; y=1.0; z= 1.0}) (mul t {x= 0.5; y= 0.7; z= 1.0})
|
||||
|
||||
|
||||
let main() =
|
||||
let nx = 200 in
|
||||
let ny = 100 in
|
||||
print_string "P3\n";
|
||||
printf "%d\n" nx;
|
||||
printf "%d\n" ny;
|
||||
print_string "\n255\n";
|
||||
|
||||
let lower_left_corner = Vec3.of_floats (-2., -1., -1.) in
|
||||
let horizontal = Vec3.of_floats (4., 0., 0.) in
|
||||
let vertical = Vec3.of_floats (0., 2., 0.) in
|
||||
let origin = Vec3.of_floats (0., 0., 0.) in
|
||||
let color = ref {x=0.; y=0.; z=0.} in
|
||||
for j = ny downto 1 do
|
||||
for i = 0 to nx-1 do
|
||||
let u = (Float.of_int i) /. (Float.of_int nx) in
|
||||
let v = (Float.of_int j) /. (Float.of_int ny) in
|
||||
|
||||
let r = { origin = origin;
|
||||
dir = Vec3.add lower_left_corner (Vec3.add (Vec3.mul u horizontal) (Vec3.mul v vertical)) } in
|
||||
color := get_color r;
|
||||
|
||||
let {x=r; y=g; z=b} = !color in
|
||||
let (ir, ig, ib) = (Int.of_float (r*.255.99),
|
||||
Int.of_float (g*.255.99),
|
||||
Int.of_float (b*.255.99)) in
|
||||
printf "%d " ir;
|
||||
printf "%d " ig;
|
||||
printf "%d " ib;
|
||||
print_newline();
|
||||
done
|
||||
done
|
||||
;;
|
||||
|
||||
(* main() *)
|
||||
38
src/vec3.ml
Normal file
38
src/vec3.ml
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
type vec3 = { x: float;
|
||||
y: float;
|
||||
z: float }
|
||||
|
||||
let of_floats (e1, e2, e3) =
|
||||
{x = e1; y = e2; z = e3}
|
||||
|
||||
let add v w =
|
||||
{x = v.x +. w.x;
|
||||
y = v.y +. w.y;
|
||||
z = v.z +. w.z}
|
||||
|
||||
let sub v w =
|
||||
{x = v.x -. w.x;
|
||||
y = v.y -. w.y;
|
||||
z = v.z -. w.z}
|
||||
|
||||
let dot v w =
|
||||
v.x*.w.x +. v.y*.w.y +.v.z*.w.z
|
||||
|
||||
let cross v w =
|
||||
{x = v.y*.w.z -. v.z*.w.y;
|
||||
y = v.z*.w.x -. v.x*.w.z;
|
||||
z = v.x*.w.y -. v.y*.w.x}
|
||||
|
||||
let length v =
|
||||
sqrt (dot v v)
|
||||
|
||||
let unit_vector v =
|
||||
let l = length v in
|
||||
{ x = v.x /. l;
|
||||
y = v.y /. l;
|
||||
z = v.z /. l }
|
||||
|
||||
let mul t v =
|
||||
{ x = t *. v.x;
|
||||
y = t *. v.y;
|
||||
z = t *. v.z }
|
||||
11
src/vec3.mli
Normal file
11
src/vec3.mli
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
type vec3 = { x: float;
|
||||
y: float;
|
||||
z: float }
|
||||
|
||||
val of_floats : float * float * float -> vec3
|
||||
val add : vec3 -> vec3 -> vec3
|
||||
val sub : vec3 -> vec3 -> vec3
|
||||
val dot : vec3 -> vec3 -> float
|
||||
val cross: vec3 -> vec3 -> vec3
|
||||
val mul : float -> vec3 -> vec3
|
||||
val unit_vector : vec3 -> vec3
|
||||
Loading…
Add table
Reference in a new issue