// Raytrace returns the colour obtained by tracing the given ray func (r *Raytracer) Raytrace(incoming *ray.Ray) *hdrcolour.Colour { if incoming.Depth > r.Scene.MaxDepth { return hdrcolour.New(0, 0, 0) } intersectionInfo := r.Scene.Mesh.Intersect(incoming) if intersectionInfo == nil { return hdrcolour.New(0, 0, 0) } return r.Scene.Materials[intersectionInfo.Material].Shade(intersectionInfo, r) }
// New will set the screen to the given width and height func New(width, height int) *Image { pixels := make([][]hdrcolour.Colour, width) for i := range pixels { pixels[i] = make([]hdrcolour.Colour, height) for j := range pixels[i] { pixels[i][j] = *hdrcolour.New(0, 0, 0) } } return &Image{Pixels: pixels, Width: width, Height: height, Divisor: 1} }
// UnmarshalJSON implements the json.Unmarshaler interface func (v *Vec3Sampler) UnmarshalJSON(data []byte) error { err := json.Unmarshal(data, &v.Vector) if err != nil { return err } v.Colour = hdrcolour.New( float32(v.Vector.X), float32(v.Vector.Y), float32(v.Vector.Z), ) v.Fac = (v.Vector.X + v.Vector.Y + v.Vector.Z) / 3 // fixme: this must be smarter return nil }
// UnmarshalJSON implements the json.Unmarshaler interface func (n *NumberSampler) UnmarshalJSON(data []byte) error { err := json.Unmarshal(data, &n.Fac) if err != nil { return err } n.Colour = hdrcolour.New( float32(n.Fac), float32(n.Fac), float32(n.Fac), ) n.Vector = maths.NewVec3(n.Fac, n.Fac, n.Fac) return nil }
// AtHDR returns the Colour of the pixel at [x][y] (scaled by the divisor) func (im *Image) AtHDR(x, y int) *hdrcolour.Colour { if im.Divisor == 0 { return hdrcolour.New(1, 1, 1) } return im.Pixels[x][y].Scaled(1 / float32(im.Divisor)) }