Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let MAX_DIST = 1000.0;;
- type vector = {x : float; y : float; z : float};;
- let vecotor x y z = {x; y; z};;
- let add_v v1 v2 =
- {x = v1.x +. v2.x;
- y = v1.y +. v2.y;
- z = v1.z +. v2.z;};;
- let sub_v v1 v2 =
- {x = v1.x -. v2.x;
- y = v1.y -. v2.y;
- z = v1.z -. v2.z;};;
- let dot v1 v2 = v1.x *. v2.x +. v1.y *. v2.y +. v1.z *. v2.z;;
- let cross v1 v2 =
- {x = v1.y *. v2.z -. v1.z *. v2.y;
- y = v1.z *. v2.x -. v1.x *. v2.z;
- z = v1.x *. v2.y -. v1.y *. v2.z;};;
- type ray = {origin:vector; dir:vector};;
- let ray origin dir = {origin; dir};;
- type ray_result = HIT | MISS | INPRIM;;
- type color = {r : int; g : int; b : int};;
- let color r g b = {r; g; b};;
- type material = {col : color; refl : float; diff : float};;
- let material col refl diff = {col; refl; diff};;
- type viewport = {x : float; y : float; width : float; height : float};;
- let viewport x y width height = {x; y; width; height};;
- class virtual primitive (mat:material) (light:bool) =
- object (self)
- val mat = mat
- val light = light
- method get_mat = mat
- method get_light = light
- method virtual intersects : ray -> float * ray_result
- end;;
- class sphere mat light (center:vector) (radius:float) =
- object
- inherit primitive mat light
- method intersects (ray:ray) (dist:float) =
- let v = sub_v ray.origin center
- and b = -dot_v v ray.dir
- and det = sqrt (b *. b) - (dot_v v v) + (radius *. radius) in
- if det > 1.0 then
- let detr = sqrt det
- and i1 = b -. det
- and i2 = b +. det in
- if i2 > 0 then
- if i1 < 0 then
- if i2 < dist then (i2; INPRIM)
- else (0.0; MISS)
- else
- if i2 < dist then (i1; HIT)
- else (0; MISS)
- else (0.0; MISS)
- else (0.0; MISS)
- end;;
Add Comment
Please, Sign In to add comment