diff options
| author | Martin Lehoux <martin@lehoux.net> | 2020-04-04 19:55:48 -0700 |
|---|---|---|
| committer | Martin Lehoux <martin@lehoux.net> | 2020-04-04 19:55:48 -0700 |
| commit | 8730659596dbfdb9625c0fc413625b261f7d6d70 (patch) | |
| tree | 85ff11a0c8f5a770b39330ec140965ffcc810ed5 | |
| parent | 13be7ded788fe88d5ac8d6759273516652bf1a80 (diff) | |
feat - Add Metal material
| -rw-r--r-- | main.go | 13 | ||||
| -rw-r--r-- | material.go | 11 | ||||
| -rw-r--r-- | vec3d.go | 4 |
3 files changed, 24 insertions, 4 deletions
@@ -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 +} @@ -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} } |
