Commits

Anonymous committed de70b87

working reflection

Comments (0)

Files changed (1)

 
 let surf_shiny_green = {surface_color=rgb_green; reflectance=0.5; albedo=1.0}
 let surf_shiny_red = {surface_color=rgb_red; reflectance=0.5; albedo=0.5}
-let surf_shiny_blue = {surface_color=rgb_blue; reflectance=0.5; albedo=0.5}
-let surf_shiny_yellow = {surface_color=rgb_yellow;reflectance=0.5; albedo=0.5}
-let surf_shiny_cyan = {surface_color=rgb_cyan;reflectance=0.5; albedo=0.5}
+let surf_shiny_blue = {surface_color=rgb_blue; reflectance=0.1; albedo=0.5}
+let surf_shiny_yellow = {surface_color=rgb_yellow;reflectance=0.9; albedo=0.5}
+let surf_shiny_cyan = {surface_color=rgb_cyan;reflectance=0.9; albedo=0.5}
 
 (* vector3 functions *)
 
 	  let lightd = v3_l2dist light.light_center isect.location in
 	    isectd > lightd)
 
-let handle_illuminated (isect:intersection) (ray:normal) (light:light)
-    : color =
-  let strip_norm = function Normal n -> n in
-  let light_dir = v3_norm (v3_sub light.light_center isect.location) in
-  let light_norm = strip_norm light_dir in
-  let isect_norm = strip_norm isect.normal in
-  let ray_norm = strip_norm ray in
-  let lambertian_coeff = max (v3_dot light_norm isect_norm) 0.0 in
-  let light_in = rgb_coeff light.light_color lambertian_coeff in
-  let surf = shape_surface isect.obj in
-  let lambert_color = rgb_mix light_in surf.surface_color in
-  let light_normal_comp = -.2.0*.(v3_dot light_norm isect_norm) in
-  let light_reflect_dir = 
-    v3_add light_norm (v3_mul isect_norm light_normal_comp)
-  in
-  let specular_coeff = v3_dot light_reflect_dir ray_norm in
-  let specular_color = rgb_coeff light.light_color specular_coeff in
-    rgb_chop (rgb_sum lambert_color (rgb_coeff specular_color surf.albedo))
-
 let handle_ambient (isect:intersection) (ambience:float) : color =
   rgb_coeff (shape_surface isect.obj).surface_color ambience
 
-let render_ray (src:vector3) (shapes:shape list)
-    (lights:light list) (bg:color) (ambience:float) (ray:normal) : color =
+let rec render_ray (depth:int) (max_depth:int) (src:vector3)
+    (shapes:shape list) (lights:light list) (bg:color) (ambience:float)
+    (ray:normal) : color =
+  let handle_illuminated (isect:intersection) (ray:normal) (light:light)
+      : color =
+    let strip_norm = function Normal n -> n in
+    let reflect_dir v3 norm =
+      let normal_comp = v3_dot v3 norm in
+	v3_add v3 (v3_mul norm (-.2.0*.normal_comp))
+    in
+    let light_dir = v3_norm (v3_sub light.light_center isect.location) in
+    let light_norm = strip_norm light_dir in
+    let isect_norm = strip_norm isect.normal in
+    let ray_norm = strip_norm ray in
+    let lambertian_coeff = max (v3_dot light_norm isect_norm) 0.0 in
+    let light_in = rgb_coeff light.light_color lambertian_coeff in
+    let surf = shape_surface isect.obj in
+    let lambert_color = rgb_mix light_in surf.surface_color in
+    let light_reflect_dir = reflect_dir light_norm isect_norm in
+    let specular_coeff = v3_dot light_reflect_dir ray_norm in
+    let specular_color = rgb_coeff light.light_color specular_coeff in
+    let own_color = 
+      rgb_chop (rgb_sum lambert_color (rgb_coeff specular_color surf.albedo))
+    in
+    let ray_reflect_dir = v3_norm (reflect_dir ray_norm isect_norm) in
+    let reflect_color =
+      if surf.reflectance > 0.01 && depth < max_depth then
+	let rrd = strip_norm ray_reflect_dir in
+	let next_src = v3_add isect.location (v3_mul rrd 0.000001) in
+	render_ray (depth+1) max_depth next_src shapes
+	  lights bg ambience ray_reflect_dir
+      else rgb_black
+    in
+      if depth < max_depth then
+	rgb_arith_mix reflect_color own_color surf.reflectance
+      else own_color
+  in
   let closest = closest_intersection shapes src ray in
     match closest with
 	None -> bg
 let render_scene (camera:camera) (shapes:shape list) (lights:light list)
     (bg:color) (ambience:float) : color list =
   let rays = build_rays camera in
-  let f = render_ray camera.origin shapes lights bg ambience in
+  let f = render_ray 0 1 camera.origin shapes lights bg ambience in
   let rec loop ret rem =
     match rem with
       | [] -> List.rev ret