diff options
author | Nat Lasseter <user@4574.co.uk> | 2024-06-24 16:01:18 +0100 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2024-06-24 16:01:18 +0100 |
commit | b7814d6822d03517fc846b95865965618be7d406 (patch) | |
tree | 2afc2a5de6d3ca82f465f51686d47dc032d2efb4 /rtiaw.go |
Initial Commit, finished Chapter 6
Diffstat (limited to 'rtiaw.go')
-rw-r--r-- | rtiaw.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/rtiaw.go b/rtiaw.go new file mode 100644 index 0000000..9f900b6 --- /dev/null +++ b/rtiaw.go @@ -0,0 +1,64 @@ +package main + +import ( + "fmt" + "os" + "time" +) + +func main() { + start := time.Now(); + + image_width := 400; + aspect := 16.0 / 9.0; + + var world Hittables; + world.Add(Sphere{Vec3{0, 0 , -1}, 0.5}); + world.Add(Sphere{Vec3{0, -100.5, -1}, 100 }); + + image_height := int(float64(image_width) / aspect); + if image_height < 1 { + image_height = 1; + } + + focal_length := 1.0; + viewport_height := 2.0; + viewport_width := viewport_height * + (float64(image_width) / float64(image_height)); + camera_centre := Splat(0); + + viewport_u := Vec3{viewport_width, 0, 0}; + viewport_v := Vec3{0, -viewport_height, 0}; + + pd_u := viewport_u.Div(Splat(float64(image_width))); + pd_v := viewport_v.Div(Splat(float64(image_height))); + + viewport_upperleft := camera_centre. + Sub(Vec3{0, 0, focal_length}). + Sub(viewport_u.Div(Splat(2))). + Sub(viewport_v.Div(Splat(2))); + pixel00_loc := viewport_upperleft. + Add(pd_u.Add(pd_v).Div(Splat(2))); + + fmt.Printf("P3\n%d %d\n255\n", image_width, image_height); + for row := 0; row < image_height; row++ { + fmt.Fprintf(os.Stderr, + "[GScanlines remaining: %3d...", + (image_height - row)); + for col := 0; col < image_width; col++ { + pixel_centre := pixel00_loc. + Add(pd_u.Mul(Splat(float64(col)))). + Add(pd_v.Mul(Splat(float64(row)))); + ray_direction := pixel_centre.Sub(camera_centre); + ray := Ray{camera_centre, ray_direction} + pixel_colour := ray.Colour(world); + fmt.Printf("%s ", pixel_colour.ToPPM()); + } + fmt.Printf("\n"); + } + + dur := time.Since(start); + fmt.Fprintf(os.Stderr, + "[GDone, in %5.2f seconds! \n", + dur.Seconds()); +} |