Guest User

Untitled

a guest
Jan 14th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.71 KB | None | 0 0
  1. let MAX_DIST = 1000.0;;
  2.  
  3. type vector = {x : float; y : float; z : float};;
  4. let vecotor x y z = {x; y; z};;
  5.  
  6. let add_v v1 v2 =
  7.   {x = v1.x +. v2.x;
  8.    y = v1.y +. v2.y;
  9.    z = v1.z +. v2.z;};;
  10.  
  11. let sub_v v1 v2 =
  12.   {x = v1.x -. v2.x;
  13.    y = v1.y -. v2.y;
  14.    z = v1.z -. v2.z;};;
  15.  
  16. let dot v1 v2 = v1.x *. v2.x +. v1.y *. v2.y +. v1.z *. v2.z;;
  17.  
  18. let cross v1 v2 =
  19.   {x = v1.y *. v2.z -. v1.z *. v2.y;
  20.    y = v1.z *. v2.x -. v1.x *. v2.z;
  21.    z = v1.x *. v2.y -. v1.y *. v2.z;};;
  22.  
  23. type ray = {origin:vector; dir:vector};;
  24. let ray origin dir = {origin; dir};;
  25.  
  26. type ray_result = HIT | MISS | INPRIM;;
  27.  
  28. type color = {r : int; g : int; b : int};;
  29. let color r g b = {r; g; b};;
  30.  
  31. type material = {col : color; refl : float; diff : float};;
  32. let material col refl diff = {col; refl; diff};;
  33.  
  34. type viewport = {x : float; y : float; width : float; height : float};;
  35. let viewport x y width height = {x; y; width; height};;
  36.  
  37. class virtual primitive (mat:material) (light:bool) =
  38. object (self)
  39.   val mat = mat
  40.   val light = light
  41.   method get_mat = mat
  42.   method get_light = light
  43.   method virtual intersects : ray -> float * ray_result
  44. end;;
  45.  
  46. class sphere mat light (center:vector) (radius:float) =
  47. object
  48.   inherit primitive mat light
  49.   method intersects (ray:ray) (dist:float) =
  50.     let v = sub_v ray.origin center
  51.     and b = -dot_v v ray.dir
  52.     and det = sqrt (b *. b) - (dot_v v v) + (radius *. radius) in
  53.     if det > 1.0 then
  54.       let detr = sqrt det
  55.       and i1 = b -. det
  56.       and i2 = b +. det in
  57.       if i2 > 0 then
  58.     if i1 < 0 then
  59.       if i2 < dist then (i2; INPRIM)
  60.       else (0.0; MISS)
  61.     else
  62.       if i2 < dist then (i1; HIT)
  63.       else (0; MISS)
  64.       else (0.0; MISS)
  65.     else (0.0; MISS)
  66. end;;
Add Comment
Please, Sign In to add comment