Skip to content

Instantly share code, notes, and snippets.

View markphelps's full-sized avatar

Mark Phelps markphelps

View GitHub Profile
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}
}
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
}
package primitives
type Material interface {
Bounce(input Ray, hit Hit) (bool, Ray)
Color() Vector
}
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}}
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 {
func (s *Sphere) Hit(r *Ray, tMin float64, tMax float64) (bool, HitRecord) {
...
}
type Hitable interface {
Hit(r *Ray, tMin float64, tMax float64) (bool, HitRecord)
}
import (
"fmt"
p "github.com/markphelps/go-trace/primitives"
)
...
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)
type HitRecord struct {
T float64
P, Normal Vector
}