Skip to content

Commit

Permalink
Create sampler.ml. Defines a super-sampler signature, which implement…
Browse files Browse the repository at this point in the history
…s super sampling: where color samples are taken at several instances inside the pixel and an average color is calculated. Implements a Stochastic (Random) sampler.
  • Loading branch information
johnyob committed Jul 4, 2020
1 parent e828b9d commit be67686
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/sampler.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
open Base

module type S = sig
type t
val sample : t -> Image.xy -> Color.t
end

module Stochastic (C: Camera.S) (T: Tracer.S) = struct
type t = { camera: C.t; tracer: T.t; n: int}
let make ~camera ~tracer ~n = { camera; tracer; n }

let sample { camera; tracer; n } (x, y) =
let open Color in
let f c = Float.of_int c +. Random.float 1. in
(List.range 1 n
|> List.map ~f:(fun _ -> C.ray_generate camera (f x, f y) |> T.trace tracer)
|> List.fold_left ~init:black ~f:( |+| ))
|/ (Float.of_int n)
end

(* TODO: Add more samplers *)
10 changes: 10 additions & 0 deletions src/sampler.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module type S = sig
type t
val sample : t -> Image.xy -> Color.t
end

module Stochastic (C: Camera.S) (T: Tracer.S) : sig
include S
val make : camera:C.t -> tracer:T.t -> n:int -> t
end

0 comments on commit be67686

Please sign in to comment.