This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package primitives | |
| type Lambertian struct { | |
| C Vector | |
| } | |
| func (l Lambertian) Bounce(input Ray, hit Hit) (bool, Ray) { | |
| direction := hit.Normal.Add(VectorInUnitSphere()) | |
| return true, Ray{hit.Point, direction} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var UnitVector = Vector{1, 1, 1} | |
| // rejection method for finding random point on unit sphere | |
| func VectorInUnitSphere() Vector { | |
| for { | |
| r := Vector{rand.Float64(), rand.Float64(), rand.Float64()} | |
| p := r.MultiplyScalar(2.0).Subtract(UnitVector) | |
| if p.SquaredLength() >= 1.0 { | |
| return p | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package primitives | |
| type Material interface { | |
| Bounce(input Ray, hit Hit) (bool, Ray) | |
| Color() Vector | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sphere = p.Sphere{p.Vector{0, 0, -1}, 0.5} | |
| floor = p.Sphere{p.Vector{0, -100.5, -1}, 100} | |
| world = p.World{[]p.Hitable{&sphere, &floor}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type World struct { | |
| Elements []Hitable | |
| } | |
| func (w *World) Hit(r *Ray, tMin float64, tMax float64) (bool, HitRecord) { | |
| hitAnything := false | |
| closest := tMax | |
| record := HitRecord{} | |
| for _, element := range w.Elements { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func (s *Sphere) Hit(r *Ray, tMin float64, tMax float64) (bool, HitRecord) { | |
| ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Hitable interface { | |
| Hit(r *Ray, tMin float64, tMax float64) (bool, HitRecord) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ( | |
| "fmt" | |
| p "github.com/markphelps/go-trace/primitives" | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| for j := ny - 1; j >= 0; j-- { | |
| for i := 0; i < nx; i++ { | |
| rgb := p.Vector{} | |
| // sample rays for anti-aliasing | |
| for s := 0; s < 100; s++ { | |
| u := (float64(i) + rand.Float64()) / float64(nx) | |
| v := (float64(j) + rand.Float64()) / float64(ny) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type HitRecord struct { | |
| T float64 | |
| P, Normal Vector | |
| } |