From af587dc1d7c30e8ad6af8afda09b78b584d416dc Mon Sep 17 00:00:00 2001 From: Martin Lehoux Date: Sat, 4 Apr 2020 18:23:43 -0700 Subject: feat - Antialliasing and camera --- camera.go | 17 +++++++++++++++++ main.go | 15 ++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 camera.go diff --git a/camera.go b/camera.go new file mode 100644 index 0000000..6575390 --- /dev/null +++ b/camera.go @@ -0,0 +1,17 @@ +package main + +type Camera struct { + lower_left_corner Vec3D + origin, horizontal, vertical Vec3D +} + +func (camera Camera) GetRay(u float64, v float64) Ray { + return Ray{ + camera.origin, + AddVec3D(AddVec3D( + camera.lower_left_corner, + ScalarProduct(u, camera.horizontal)), + ScalarProduct(v, camera.vertical), + ), + } +} diff --git a/main.go b/main.go index daf2718..f85ad8a 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,19 @@ package main +import "math/rand" + const HEIGHT = 100 const WIDTH = 200 const T_MIN = 0.001 const T_MAX = 1000 +const RAY_PER_PIXEL = 100 func main() { LOWER_LEFT_CORNER := Vec3D{-2.0, -1.0, -1.0} ORIGIN := Vec3D{0.0, 0.0, 0.0} HORIZONTAL := Vec3D{4.0, 0.0, 0.0} VERTICAL := Vec3D{0.0, 2.0, 0.0} + CAMERA := Camera{LOWER_LEFT_CORNER, ORIGIN, HORIZONTAL, VERTICAL} SPHERE := Sphere{Vec3D{0.0, 0.0, -1.0}, 0.5} EARTH := Sphere{Vec3D{0.0, -100.5, -1.0}, 100.0} @@ -18,9 +22,14 @@ func main() { frame := Frame{} for x := 0; x < HEIGHT; x++ { for y := 0; y < WIDTH; y++ { - u, v := float64(y)/float64(WIDTH), float64(x)/float64(HEIGHT) - ray := Ray{ORIGIN, AddVec3D(AddVec3D(LOWER_LEFT_CORNER, ScalarProduct(u, HORIZONTAL)), ScalarProduct(v, VERTICAL))} - color := RayColor(ray, WORLD) + var color_list [RAY_PER_PIXEL]Color + for it := 0; it < RAY_PER_PIXEL; it++ { + u := (float64(y) + rand.Float64()) / float64(WIDTH) + v := (float64(x) + rand.Float64()) / float64(HEIGHT) + ray := CAMERA.GetRay(u, v) + color_list[it] = RayColor(ray, WORLD) + } + color := ColorMeanSquare(color_list) frame[HEIGHT-1-x][y] = color.ToPixel() } } -- cgit v1.3