From 8730659596dbfdb9625c0fc413625b261f7d6d70 Mon Sep 17 00:00:00 2001 From: Martin Lehoux Date: Sat, 4 Apr 2020 19:55:48 -0700 Subject: feat - Add Metal material --- main.go | 13 +++++++++---- material.go | 11 +++++++++++ vec3d.go | 4 ++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index b8f6335..316ebff 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,4 @@ +// TODO Use pointers when needed to reduce memory package main import ( @@ -22,11 +23,15 @@ func main() { MAT_RED := Lambertian{Color{0.7, 0.3, 0.3}} MAT_GREEN := Lambertian{Color{0.8, 0.8, 0.0}} + METAL_GREY := Metal{Color{0.8, 0.8, 0.8}, 0.3} + METAL_BROWN := Metal{Color{0.8, 0.6, 0.2}, 1.0} - SPHERE := Sphere{Vec3D{0.0, 0.0, -1.0}, 0.5, MAT_RED} - EARTH := Sphere{Vec3D{0.0, -100.5, -1.0}, 100.0, MAT_GREEN} - - WORLD := HitableList{EARTH, SPHERE} + WORLD := HitableList{ + Sphere{Vec3D{0.0, 0.0, -1.0}, 0.5, MAT_RED}, + Sphere{Vec3D{-1.0, 0.0, -1.0}, 0.5, METAL_GREY}, + Sphere{Vec3D{1.0, 0.0, -1.0}, 0.5, METAL_BROWN}, + Sphere{Vec3D{0.0, -100.5, -1.0}, 100.0, MAT_GREEN}, + } frame := Frame{} bar := progressbar.New(HEIGHT) for x := 0; x < HEIGHT; x++ { diff --git a/material.go b/material.go index db1e4d6..a36fb52 100644 --- a/material.go +++ b/material.go @@ -14,3 +14,14 @@ func (lambertian Lambertian) Scatter(ray Ray, hit HitRecord) (Ray, Color) { scattered := Ray{hit.point, direction} return scattered, lambertian.albedo } + +type Metal struct { + albedo Color + fuzz float64 +} + +func (metal Metal) Scatter(ray Ray, hit HitRecord) (Ray, Color) { + reflected := AddVec3D(ray.direction.Unit().Reflect(hit.normal), ScalarProduct(metal.fuzz, RandomInUnitSphere())) + scattered := Ray{hit.point, reflected} + return scattered, metal.albedo +} diff --git a/vec3d.go b/vec3d.go index fcf5b1c..9e4ced0 100644 --- a/vec3d.go +++ b/vec3d.go @@ -25,6 +25,10 @@ func (vec Vec3D) Unit() Vec3D { return ScalarProduct(1.0/length, vec) } +func (vec Vec3D) Reflect(normal Vec3D) Vec3D { + return SubVec3D(vec, ScalarProduct(2*DotProduct(vec, normal), normal)) +} + func AddVec3D(vec1 Vec3D, vec2 Vec3D) Vec3D { return Vec3D{vec1.x + vec2.x, vec1.y + vec2.y, vec1.z + vec2.z} } -- cgit v1.3