diff options
author | Nat Lasseter <user@4574.co.uk> | 2024-06-24 16:50:36 +0100 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2024-06-24 16:50:36 +0100 |
commit | 2f025ebfc6c9e3146ef1c356f99045edc203c9ee (patch) | |
tree | db279ad3999be6a545d7ba80eef521df63592b66 /camera.go | |
parent | 63cee9aa6eb62afb2e8e726d2de16b76935c5f4c (diff) |
Chapter 8
Diffstat (limited to 'camera.go')
-rw-r--r-- | camera.go | 30 |
1 files changed, 23 insertions, 7 deletions
@@ -12,15 +12,18 @@ type Camera struct { PDU Vec3 PDV Vec3 Pixel00 Vec3 + AntiAliasing uint } -func NewCamera(w uint, ar float64) (cam Camera) { +func NewCamera(w uint, ar float64, aa uint) (cam Camera) { cam.ImageWidth = w; cam.ImageHeight = uint(float64(cam.ImageWidth) / ar); if cam.ImageHeight < 1 { cam.ImageHeight = 1; } + cam.AntiAliasing = aa; + focal_length := 1.0; viewport_height := 2.0; viewport_width := viewport_height * @@ -51,14 +54,27 @@ func (cam Camera) Render(world Hittable) { "[GScanlines remaining: %3d...", (cam.ImageHeight - row)); for col := uint(0); col < cam.ImageWidth; col++ { - pixel_centre := cam.Pixel00. - Add(cam.PDU.Mul(Splat(float64(col)))). - Add(cam.PDV.Mul(Splat(float64(row)))); - ray_direction := pixel_centre.Sub(cam.Centre); - ray := Ray{cam.Centre, ray_direction} - pixel_colour := ray.Colour(world); + pixel_colour := Splat(0); + for i := uint(0); i < cam.AntiAliasing; i++ { + r := cam.GetRay(row, col); + pixel_colour = pixel_colour.Add(r.Colour(world)); + } + pixel_colour = pixel_colour.Div(Splat(float64(cam.AntiAliasing))); fmt.Printf("%s ", pixel_colour.ToPPM()); } fmt.Printf("\n"); } } + +func (cam Camera) GetRay(row uint, col uint) Ray { + offset := unit_square(); + pixel_sample := cam.Pixel00. + Add(cam.PDU.Mul(Splat(float64(col) + offset.X))). + Add(cam.PDV.Mul(Splat(float64(row) + offset.Y))); + return Ray{cam.Centre, pixel_sample.Sub(cam.Centre)}; +} + +func unit_square() Vec3 { + i := Interval{-0.5, 0.5}; + return Vec3{i.Sample(), i.Sample(), 0}; +} |