diff options
Diffstat (limited to 'camera.go')
-rw-r--r-- | camera.go | 26 |
1 files changed, 21 insertions, 5 deletions
@@ -15,12 +15,16 @@ type Camera struct { Pixel00 Vec3 AntiAliasing uint MaxDepth uint + DOF float64 + DefDiskU Vec3 + DefDiskV Vec3 } func NewCamera(wi uint, ar float64, aa uint, md uint, vfov float64, lookfrom Vec3, - lookat Vec3, vup Vec3) (cam Camera) { + lookat Vec3, vup Vec3, + dof float64, fd float64) (cam Camera) { cam.ImageWidth = wi; cam.ImageHeight = uint(float64(cam.ImageWidth) / ar); if cam.ImageHeight < 1 { @@ -30,11 +34,11 @@ func NewCamera(wi uint, ar float64, cam.AntiAliasing = aa; cam.MaxDepth = md; cam.Centre = lookfrom; + cam.DOF = dof; - focal_length := lookfrom.Sub(lookat).Mag(); theta := vfov * maths.Pi / 180; h := maths.Tan(theta / 2) - viewport_height := 2.0 * h * focal_length; + viewport_height := 2.0 * h * fd; viewport_width := viewport_height * float64(cam.ImageWidth) / float64(cam.ImageHeight); @@ -50,12 +54,16 @@ func NewCamera(wi uint, ar float64, cam.PDV = viewport_v.Div(SplatVec3(float64(cam.ImageHeight))); viewport_upperleft := cam.Centre. - Sub(SplatVec3(focal_length).Mul(w)). + Sub(SplatVec3(fd).Mul(w)). Sub(viewport_u.Div(SplatVec3(2))). Sub(viewport_v.Div(SplatVec3(2))); cam.Pixel00 = viewport_upperleft. Add(cam.PDU.Add(cam.PDV).Div(SplatVec3(2))); + def_radius := fd * maths.Tan((cam.DOF / 2) * (maths.Pi / 180)); + cam.DefDiskU = SplatVec3(def_radius).Mul(u); + cam.DefDiskV = SplatVec3(def_radius).Mul(v); + return; } @@ -84,7 +92,15 @@ func (cam Camera) GetRay(row uint, col uint) Ray { pixel_sample := cam.Pixel00. Add(cam.PDU.Mul(SplatVec3(float64(col) + offset.X))). Add(cam.PDV.Mul(SplatVec3(float64(row) + offset.Y))); - return Ray{cam.Centre, pixel_sample.Sub(cam.Centre), true}; + origin := cam.Centre; + if cam.DOF > 0 { + p := RandomVec3OnUnitDisk(); + origin = cam.Centre. + Add(SplatVec3(p.X).Mul(cam.DefDiskU)). + Add(SplatVec3(p.Y).Mul(cam.DefDiskV)); + } + direction := pixel_sample.Sub(origin); + return Ray{origin, direction, true}; } func unit_square() Vec3 { |