rtc

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2021 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package rtc implements the code for the Ray Tracer Challenge book.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasePattern

type BasePattern struct {
	// contains filtered or unexported fields
}

BasePattern represents the common functionality for all patterns.

func (*BasePattern) SetTransform

func (s *BasePattern) SetTransform(m M4)

SetTransform sets the object's transform 4x4 matrix.

func (*BasePattern) Transform

func (s *BasePattern) Transform() M4

Transform returns the object's transform 4x4 matrix.

type BoundsT

type BoundsT struct {
	Min Tuple
	Max Tuple
}

BoundsT represents a minimum bounding box of an object.

func Bounds

func Bounds() *BoundsT

Bounds returns an empty bounding box.

func UpdateTransformedBounds

func UpdateTransformedBounds(object Object, boundingBox *BoundsT) *BoundsT

UpdateTransformedBounds returns the updated bounding box of an object, taking into account its own transformation. If a starting bounding box is supplied, it is updated (expanded), otherwise a new one is returned.

func (*BoundsT) LocalIntersect

func (b *BoundsT) LocalIntersect(ray RayT, object Object) []IntersectionT

LocalIntersect returns a slice of IntersectionT values where the transformed (object space) ray intersects the object.

func (*BoundsT) String

func (b *BoundsT) String() string

func (*BoundsT) UpdateBounds

func (b *BoundsT) UpdateBounds(p Tuple)

UpdateBounds updates the bounding box with the provided point.

type CSGOperation

type CSGOperation int

CSGOperation represents a CSG operation.

const (
	CSGUnion CSGOperation = iota
	CSGIntersection
	CSGDifference
)

type CSGT

type CSGT struct {
	Shape
	Operation CSGOperation
	Left      Object
	Right     Object
	// contains filtered or unexported fields
}

CSGT represents a CSG object.

func CSG

func CSG(operation CSGOperation, left, right Object) *CSGT

CSG represents a constructive solid geometry object.

func (*CSGT) Bounds

func (c *CSGT) Bounds() *BoundsT

Bounds returns the minimum bounding box of the object in object (untransformed) space.

func (*CSGT) FilterIntersections

func (c *CSGT) FilterIntersections(xs []IntersectionT) []IntersectionT

FilterIntersections filters allowed CSG intersections from all possible intersections.

func (*CSGT) Includes

func (c *CSGT) Includes(other Object) bool

Includes returns whether this object includes (or actually is) the other object.

func (*CSGT) LocalIntersect

func (c *CSGT) LocalIntersect(ray RayT) []IntersectionT

LocalIntersect returns a slice of IntersectionT values where the transformed (object space) ray intersects the object.

func (*CSGT) LocalNormalAt

func (c *CSGT) LocalNormalAt(objectPoint Tuple, hit *IntersectionT) Tuple

LocalNormalAt returns the normal vector at the given point of intersection (transformed to object space) with the object.

func (*CSGT) SetMaterial

func (c *CSGT) SetMaterial(material MaterialT) Object

SetMaterial sets the object's material.

func (*CSGT) SetParent

func (c *CSGT) SetParent(parent Object) Object

SetParent sets the object's parent object.

func (*CSGT) SetTransform

func (c *CSGT) SetTransform(m M4) Object

SetTransform sets the object's transform 4x4 matrix.

type CameraT

type CameraT struct {
	HSize       int
	VSize       int
	FieldOfView float64
	Transform   M4
	PixelSize   float64
	HalfWidth   float64
	HalfHeight  float64
	NumWorkers  int
	// contains filtered or unexported fields
}

CameraT represents a camera.

func Camera

func Camera(hsize, vsize int, fov float64) *CameraT

Camera creates a new CameraT with the provided canvas size and field of view (in radians).

func (*CameraT) RayForPixel

func (c *CameraT) RayForPixel(px, py int) RayT

RayForPixel returns a ray for the camera at the given pixel.

func (*CameraT) Render

func (c *CameraT) Render(world *WorldT) *Canvas

Render renders the world with the camera and returns an image.

type Canvas

type Canvas struct {
	// contains filtered or unexported fields
}

Canvas represents an image canvas and implements the image.Image interface.

func NewCanvas

func NewCanvas(width, height int) *Canvas

NewCanvas returns a new canvas with the given dimensions.

func (*Canvas) At

func (c *Canvas) At(x, y int) color.Color

At returns the color at the provides location in the canvas.

func (*Canvas) Bounds

func (c *Canvas) Bounds() image.Rectangle

Bounds returns the bounding box of the canvas.

func (*Canvas) ColorModel

func (c *Canvas) ColorModel() color.Model

ColorModel returns the Image's color model.

func (*Canvas) PixelAt

func (c *Canvas) PixelAt(x, y int) Tuple

PixelAt returns the color (Tuple) at the given pixel.

func (*Canvas) ToPPM

func (c *Canvas) ToPPM() string

ToPPM returns a string PPM representation of the canvas.

func (*Canvas) WritePNGFile

func (c *Canvas) WritePNGFile(filename string) error

WritePNGFile writes a PNG file to the provided filename.

func (*Canvas) WritePPMFile

func (c *Canvas) WritePPMFile(filename string) error

WritePPMFile writes a PPM file to the provided filename.

func (*Canvas) WritePixel

func (c *Canvas) WritePixel(x, y int, color Tuple)

WritePixel writes a pixel to the canvas.

type CheckersPatternT

type CheckersPatternT struct {
	BasePattern
	// contains filtered or unexported fields
}

CheckersPatternT is a pattern that draws checkerss. It implements the Pattern interface.

func CheckersPattern

func CheckersPattern(a, b Tuple) *CheckersPatternT

CheckersPattern returns a CheckersPatternT.

func (*CheckersPatternT) LocalPatternAt

func (s *CheckersPatternT) LocalPatternAt(localPoint Tuple) Tuple

LocalPatternAt returns a color at a local point.

type Comps

type Comps struct {
	T             float64
	Object        Object
	Point         Tuple
	EyeVector     Tuple
	NormalVector  Tuple
	ReflectVector Tuple
	Inside        bool
	OverPoint     Tuple   // For shadow testing - slightly above surface of object.
	UnderPoint    Tuple   // For transparency and index of refraction calculations.
	N1            float64 // Refractive index of material being exited.
	N2            float64 // Refractive index of material being entered.
}

Comps contains precomputed information about an intersection.

func (*Comps) Schlick

func (c *Comps) Schlick() float64

Schlick returns the reflectance of the intersection as an approximation the Fresnel law, as developed by Christophe Shlick.

type ConeT

type ConeT struct {
	Shape
	Minimum float64
	Maximum float64
	Closed  bool
}

ConeT represents a Cone.

func Cone

func Cone() *ConeT

Cone creates a cone at the origin with its axis on the Y axis. It implements the Object interface.

func (*ConeT) Bounds

func (c *ConeT) Bounds() *BoundsT

Bounds returns the minimum bounding box of the object in object (untransformed) space.

func (*ConeT) Includes

func (c *ConeT) Includes(other Object) bool

Includes returns whether this object includes (or actually is) the other object.

func (*ConeT) LocalIntersect

func (c *ConeT) LocalIntersect(ray RayT) []IntersectionT

LocalIntersect returns a slice of IntersectionT values where the transformed (object space) ray intersects the object.

func (*ConeT) LocalNormalAt

func (c *ConeT) LocalNormalAt(objectPoint Tuple, hit *IntersectionT) Tuple

LocalNormalAt returns the normal vector at the given point of intersection (transformed to object space) with the object.

func (*ConeT) SetMaterial

func (c *ConeT) SetMaterial(material MaterialT) Object

SetMaterial sets the object's material.

func (*ConeT) SetParent

func (c *ConeT) SetParent(parent Object) Object

SetParent sets the object's parent object.

func (*ConeT) SetTransform

func (c *ConeT) SetTransform(m M4) Object

SetTransform sets the object's transform 4x4 matrix.

type CubeT

type CubeT struct {
	Shape
}

CubeT represents a Cube.

func Cube

func Cube() *CubeT

Cube creates a cube at the origin ranging from -1 to 1 on each axis. It implements the Object interface.

func (*CubeT) Bounds

func (c *CubeT) Bounds() *BoundsT

Bounds returns the minimum bounding box of the object in object (untransformed) space.

func (*CubeT) Includes

func (c *CubeT) Includes(other Object) bool

Includes returns whether this object includes (or actually is) the other object.

func (*CubeT) LocalIntersect

func (c *CubeT) LocalIntersect(ray RayT) []IntersectionT

LocalIntersect returns a slice of IntersectionT values where the transformed (object space) ray intersects the object.

func (*CubeT) LocalNormalAt

func (c *CubeT) LocalNormalAt(objectPoint Tuple, hit *IntersectionT) Tuple

LocalNormalAt returns the normal vector at the given point of intersection (transformed to object space) with the object.

func (*CubeT) SetMaterial

func (c *CubeT) SetMaterial(material MaterialT) Object

SetMaterial sets the object's material.

func (*CubeT) SetParent

func (c *CubeT) SetParent(parent Object) Object

SetParent sets the object's parent object.

func (*CubeT) SetTransform

func (c *CubeT) SetTransform(m M4) Object

SetTransform sets the object's transform 4x4 matrix.

type CylinderT

type CylinderT struct {
	Shape
	Minimum float64
	Maximum float64
	Closed  bool
}

CylinderT represents a Cylinder.

func Cylinder

func Cylinder() *CylinderT

Cylinder creates a cylinder at the origin with its axis on the Y axis. It implements the Object interface.

func (*CylinderT) Bounds

func (c *CylinderT) Bounds() *BoundsT

Bounds returns the minimum bounding box of the object in object (untransformed) space.

func (*CylinderT) Includes

func (c *CylinderT) Includes(other Object) bool

Includes returns whether this object includes (or actually is) the other object.

func (*CylinderT) LocalIntersect

func (c *CylinderT) LocalIntersect(ray RayT) []IntersectionT

LocalIntersect returns a slice of IntersectionT values where the transformed (object space) ray intersects the object.

func (*CylinderT) LocalNormalAt

func (c *CylinderT) LocalNormalAt(objectPoint Tuple, hit *IntersectionT) Tuple

LocalNormalAt returns the normal vector at the given point of intersection (transformed to object space) with the object.

func (*CylinderT) SetMaterial

func (c *CylinderT) SetMaterial(material MaterialT) Object

SetMaterial sets the object's material.

func (*CylinderT) SetParent

func (c *CylinderT) SetParent(parent Object) Object

SetParent sets the object's parent object.

func (*CylinderT) SetTransform

func (c *CylinderT) SetTransform(m M4) Object

SetTransform sets the object's transform 4x4 matrix.

type GradientPatternT

type GradientPatternT struct {
	BasePattern
	// contains filtered or unexported fields
}

GradientPatternT is a pattern that draws gradients. It implements the Pattern interface.

func GradientPattern

func GradientPattern(a, b Tuple) *GradientPatternT

GradientPattern returns a GradientPatternT.

func (*GradientPatternT) LocalPatternAt

func (s *GradientPatternT) LocalPatternAt(localPoint Tuple) Tuple

LocalPatternAt returns a color at a local point.

type GroupT

type GroupT struct {
	Shape
	Children []Object
	// contains filtered or unexported fields
}

GroupT represents a group of objects with its own transformation matrix. It implements the Object interface.

func Group

func Group(shapes ...Object) *GroupT

Group creates a group of objects at the origin. It implements the Object interface.

func (*GroupT) AddChild

func (g *GroupT) AddChild(shapes ...Object)

AddChild adds shape(s) to a group.

func (*GroupT) Bounds

func (g *GroupT) Bounds() *BoundsT

Bounds returns the minimum bounding box of the object in object (untransformed) space.

func (*GroupT) Includes

func (g *GroupT) Includes(other Object) bool

Includes returns whether this object includes (or actually is) the other object.

func (*GroupT) LocalIntersect

func (g *GroupT) LocalIntersect(ray RayT) []IntersectionT

LocalIntersect returns a slice of IntersectionT values where the transformed (object space) ray intersects the object.

func (*GroupT) LocalNormalAt

func (g *GroupT) LocalNormalAt(objectPoint Tuple, hit *IntersectionT) Tuple

LocalNormalAt returns the normal vector at the given point of intersection (transformed to object space) with the object.

func (*GroupT) SetMaterial

func (g *GroupT) SetMaterial(material MaterialT) Object

SetMaterial sets the object's material.

func (*GroupT) SetParent

func (g *GroupT) SetParent(parent Object) Object

SetParent sets the object's parent object.

func (*GroupT) SetTransform

func (g *GroupT) SetTransform(m M4) Object

SetTransform sets the object's transform 4x4 matrix.

type IntersectionT

type IntersectionT struct {
	T      float64
	Object Object

	U float64
	V float64
}

IntersectionT represents an intersection with an object.

func Hit

func Hit(xs []IntersectionT) *IntersectionT

Hit returns the first non-negative intersection. It assumes that the intersections have already been sorted by Intersections above.

func Intersect

func Intersect(object Object, ray RayT) []IntersectionT

Intersect returns a slice of IntersectionT values where the ray intersects the object.

func Intersection

func Intersection(t float64, object Object) IntersectionT

Intersection returns an IntersectionT.

func IntersectionWithUV

func IntersectionWithUV(t float64, object Object, u, v float64) IntersectionT

IntersectionWithUV returns an IntersectionT with U and V values for triangles.

func Intersections

func Intersections(args ...IntersectionT) []IntersectionT

Intersections returns a slice of IntersectionT after sorting by intersection T values.

func (*IntersectionT) NormalAt

func (hit *IntersectionT) NormalAt(worldPoint Tuple) Tuple

NormalAt returns the normal vector at the given point of intersection with the object.

func (*IntersectionT) PrepareComputations

func (i *IntersectionT) PrepareComputations(ray RayT, xs []IntersectionT) *Comps

PrepareComputations returns a new data structure encapsulating information about the intersection.

type M2

type M2 [2]Tuple

M2 is a 2x2 matrix.

func (M2) Determinant

func (m M2) Determinant() float64

Determinant finds the determinant of a 2x2 matrix.

func (M2) Equal

func (m M2) Equal(other M2) bool

Equal tests if two matrices are equal.

func (M2) Get

func (m M2) Get(row, col int) float64

Get returns a value within the matrix.

type M3

type M3 [3]Tuple

M3 is a 3x3 matrix.

func (M3) Cofactor

func (m M3) Cofactor(row, col int) float64

Cofactor returns the cofactor of a submatrix of a 3x3 matrix.

func (M3) Determinant

func (m M3) Determinant() float64

Determinant returns the determinant of the 3x3 matrix.

func (M3) Equal

func (m M3) Equal(other M3) bool

Equal tests if two matrices are equal.

func (M3) Get

func (m M3) Get(row, col int) float64

Get returns a value within the matrix.

func (M3) Minor

func (m M3) Minor(row, col int) float64

Minor returns the determinant of a submatrix of a 3x3 matrix.

func (M3) Submatrix

func (m M3) Submatrix(row, col int) M2

Submatrix returns a 2x2 submatrix with a row and column removed from a 3x3 matrix.

type M4

type M4 [4]Tuple

M4 is a 4x4 matrix.

func M4Identity

func M4Identity() M4

M4Identity returns a 4x4 identity matrix.

func RotationX

func RotationX(radians float64) M4

RotationX returns a 4x4 rotation matrix clockwise about the X axis using the left-hand rule.

func RotationY

func RotationY(radians float64) M4

RotationY returns a 4x4 rotation matrix clockwise about the Y axis using the left-hand rule.

func RotationZ

func RotationZ(radians float64) M4

RotationZ returns a 4x4 rotation matrix clockwise about the Z axis using the left-hand rule.

func Scaling

func Scaling(x, y, z float64) M4

Scaling returns a 4x4 scaling matrix.

func Shearing

func Shearing(xy, xz, yx, yz, zx, zy float64) M4

Shearing returns a 4x4 shearing matrix.

func Translation

func Translation(x, y, z float64) M4

Translation returns a 4x4 translation matrix.

func ViewTransform

func ViewTransform(from, to, up Tuple) M4

ViewTransform creates a camera transformation matrix. from and to are Points, and up is a Vector.

func (M4) Cofactor

func (m M4) Cofactor(row, col int) float64

Cofactor returns the cofactor of a submatrix of a 4x4 matrix.

func (M4) Column

func (m M4) Column(col int) Tuple

Column returns a column of the matrix as a Tuple.

func (M4) Determinant

func (m M4) Determinant() float64

Determinant returns the determinant of the 4x4 matrix.

func (M4) Equal

func (m M4) Equal(other M4) bool

Equal tests if two matrices are equal.

func (M4) Get

func (m M4) Get(row, col int) float64

Get returns a value within the matrix.

func (M4) Inverse

func (m M4) Inverse() M4

Inverse calculates the inverse of the 4x4 matrix.

func (M4) Invertible

func (m M4) Invertible() bool

Invertible returns the invertibility of the 4x4 matrix.

func (M4) Minor

func (m M4) Minor(row, col int) float64

Minor returns the determinant of a submatrix of a 4x4 matrix.

func (M4) Mult

func (m M4) Mult(other M4) M4

Mult multiplies two M4 matrices. Order is important.

func (M4) MultTuple

func (m M4) MultTuple(other Tuple) Tuple

MultTuple multiples a M4 matrix by a tuple.

func (M4) RotateX

func (m M4) RotateX(radians float64) M4

RotateX rotates a 4x4 matrix clockwise about the X axis using the left-hand rule and returns a new 4x4 matrix.

func (M4) RotateY

func (m M4) RotateY(radians float64) M4

RotateX rotates a 4x4 matrix clockwise about the Y axis using the left-hand rule and returns a new 4x4 matrix.

func (M4) RotateZ

func (m M4) RotateZ(radians float64) M4

RotateX rotates a 4x4 matrix clockwise about the Z axis using the left-hand rule and returns a new 4x4 matrix.

func (M4) Scale

func (m M4) Scale(x, y, z float64) M4

Scale scales a 4x4 matrix and returns a new one.

func (M4) Shear

func (m M4) Shear(xy, xz, yx, yz, zx, zy float64) M4

Shear shears a 4x4 matrix and returns a new one.

func (M4) Submatrix

func (m M4) Submatrix(row, col int) M3

Submatrix returns a 3x3 submatrix with a row and column removed from a 4x4 matrix.

func (M4) Translate

func (m M4) Translate(x, y, z float64) M4

Translate translates a 4x4 matrix and returns a new one.

func (M4) Transpose

func (m M4) Transpose() M4

Transpose transposes a 4x4 matrix.

type MaterialT

type MaterialT struct {
	Color           Tuple
	Ambient         float64
	Diffuse         float64
	Specular        float64
	Shininess       float64
	Reflective      float64
	Transparency    float64
	RefractiveIndex float64
	Pattern         Pattern
}

MaterialT represents a material.

func Material

func Material() MaterialT

Material returns a default material.

type Object

type Object interface {
	// LocalIntersect returns a slice of IntersectionT values where the
	// transformed (object space) ray intersects the object.
	LocalIntersect(ray RayT) []IntersectionT

	// Parent returns the object's parent object.
	Parent() Object
	// SetParent sets the object's parent object.
	SetParent(parent Object) Object

	// Transform returns the object's transform 4x4 matrix.
	Transform() M4
	// SetTransform sets the object's transform 4x4 matrix.
	SetTransform(m M4) Object

	// Material returns the object's material.
	Material() *MaterialT
	// SetMaterial sets the object's material.
	SetMaterial(material MaterialT) Object

	// LocalNormalAt returns the normal vector at the given point of intersection
	// (transformed to object space) with the object.
	LocalNormalAt(localPoint Tuple, hit *IntersectionT) Tuple

	// Bounds returns the minimum bounding box of the object in object
	// (untransformed) space.
	Bounds() *BoundsT

	// Includes returns whether this object includes (or actually is) the
	// other object.
	Includes(other Object) bool
}

Object is an interface that represents an object in the scene.

type Pattern

type Pattern interface {
	// LocalPatternAt returns a color at a local point.
	LocalPatternAt(localPoint Tuple) Tuple

	// Transform returns the object's transform 4x4 matrix.
	Transform() M4
	// SetTransform sets the object's transform 4x4 matrix.
	SetTransform(m M4)
}

Pattern represents a color pattern.

type PlaneT

type PlaneT struct {
	Shape
}

PlaneT represents a Plane.

func Plane

func Plane() *PlaneT

Plane creates a plane at the origin on the X-Z axes and +Y is up. It implements the Object interface.

func (*PlaneT) Bounds

func (p *PlaneT) Bounds() *BoundsT

Bounds returns the minimum bounding box of the object in object (untransformed) space.

func (*PlaneT) Includes

func (p *PlaneT) Includes(other Object) bool

Includes returns whether this object includes (or actually is) the other object.

func (*PlaneT) LocalIntersect

func (p *PlaneT) LocalIntersect(ray RayT) []IntersectionT

LocalIntersect returns a slice of IntersectionT values where the transformed (object space) ray intersects the object.

func (*PlaneT) LocalNormalAt

func (p *PlaneT) LocalNormalAt(objectPoint Tuple, hit *IntersectionT) Tuple

LocalNormalAt returns the normal vector at the given point of intersection (transformed to object space) with the object.

func (*PlaneT) SetMaterial

func (p *PlaneT) SetMaterial(material MaterialT) Object

SetMaterial sets the object's material.

func (*PlaneT) SetParent

func (p *PlaneT) SetParent(parent Object) Object

SetParent sets the object's parent object.

func (*PlaneT) SetTransform

func (p *PlaneT) SetTransform(m M4) Object

SetTransform sets the object's transform 4x4 matrix.

type PointLightT

type PointLightT struct {
	// contains filtered or unexported fields
}

PointLightT represents a point light.

func PointLight

func PointLight(position Tuple, intensity Tuple) *PointLightT

PointLight returns a point light at the given position (a point Tuple) with the provided intensity (a color Tuple).

type RayT

type RayT struct {
	Origin    Tuple
	Direction Tuple
}

RayT represents an origin and a direction in 3D space.

func Ray

func Ray(origin, direction Tuple) RayT

Ray returns a new RayT.

func (RayT) Position

func (r RayT) Position(t float64) Tuple

Position computes a Point a distance t along a given Ray.

func (RayT) Transform

func (r RayT) Transform(m M4) RayT

Transform returns a new RayT that is transformed by the provided 4x4 matrix.

type RingPatternT

type RingPatternT struct {
	BasePattern
	// contains filtered or unexported fields
}

RingPatternT is a pattern that draws rings. It implements the Pattern interface.

func RingPattern

func RingPattern(a, b Tuple) *RingPatternT

RingPattern returns a RingPatternT.

func (*RingPatternT) LocalPatternAt

func (s *RingPatternT) LocalPatternAt(localPoint Tuple) Tuple

LocalPatternAt returns a color at a local point.

type Shape

type Shape struct {
	// contains filtered or unexported fields
}

Shape represents the common functionality for all shapes.

func (*Shape) Material

func (s *Shape) Material() *MaterialT

Material returns the object's material.

func (*Shape) Parent

func (s *Shape) Parent() Object

Parent returns the object's parent object.

func (*Shape) Transform

func (s *Shape) Transform() M4

Transform returns the object's transform 4x4 matrix.

type SmoothTriangleT

type SmoothTriangleT struct {
	TriangleT

	N1 Tuple
	N2 Tuple
	N3 Tuple
}

SmoothTriangleT represents a smooth triangle object.

func SmoothTriangle

func SmoothTriangle(p1, p2, p3, n1, n2, n3 Tuple) *SmoothTriangleT

SmoothTriangle returns a new smooth SmoothTriangleT.

func (*SmoothTriangleT) Bounds

func (s *SmoothTriangleT) Bounds() *BoundsT

Bounds returns the minimum bounding box of the object in object (untransformed) space.

func (*SmoothTriangleT) Includes

func (s *SmoothTriangleT) Includes(other Object) bool

Includes returns whether this object includes (or actually is) the other object.

func (*SmoothTriangleT) LocalIntersect

func (s *SmoothTriangleT) LocalIntersect(ray RayT) []IntersectionT

LocalIntersect returns a slice of IntersectionT values where the transformed (object space) ray intersects the object.

func (*SmoothTriangleT) LocalNormalAt

func (s *SmoothTriangleT) LocalNormalAt(objectPoint Tuple, hit *IntersectionT) Tuple

LocalNormalAt returns the normal vector at the given point of intersection (transformed to object space) with the object.

func (*SmoothTriangleT) SetMaterial

func (s *SmoothTriangleT) SetMaterial(material MaterialT) Object

SetMaterial sets the object's material.

func (*SmoothTriangleT) SetParent

func (s *SmoothTriangleT) SetParent(parent Object) Object

SetParent sets the object's parent object.

func (*SmoothTriangleT) SetTransform

func (s *SmoothTriangleT) SetTransform(m M4) Object

SetTransform sets the object's transform 4x4 matrix.

type SphereT

type SphereT struct {
	Shape
}

SphereT represents a sphere.

func GlassSphere

func GlassSphere() *SphereT

GlassSphere creates a unit glass sphere at the origin. It implements the Object interface.

func Sphere

func Sphere() *SphereT

Sphere creates a unit sphere at the origin. It implements the Object interface.

func (*SphereT) Bounds

func (s *SphereT) Bounds() *BoundsT

Bounds returns the minimum bounding box of the object in object (untransformed) space.

func (*SphereT) Includes

func (s *SphereT) Includes(other Object) bool

Includes returns whether this object includes (or actually is) the other object.

func (*SphereT) LocalIntersect

func (s *SphereT) LocalIntersect(ray RayT) []IntersectionT

LocalIntersect returns a slice of IntersectionT values where the transformed (object space) ray intersects the object.

func (*SphereT) LocalNormalAt

func (s *SphereT) LocalNormalAt(objectPoint Tuple, hit *IntersectionT) Tuple

LocalNormalAt returns the normal vector at the given point of intersection (transformed to object space) with the object.

func (*SphereT) SetMaterial

func (s *SphereT) SetMaterial(material MaterialT) Object

SetMaterial sets the object's material.

func (*SphereT) SetParent

func (s *SphereT) SetParent(parent Object) Object

SetParent sets the object's parent object.

func (*SphereT) SetTransform

func (s *SphereT) SetTransform(m M4) Object

SetTransform sets the object's transform 4x4 matrix.

type StripePatternT

type StripePatternT struct {
	BasePattern
	// contains filtered or unexported fields
}

StripePatternT is a pattern that draws stripes. It implements the Pattern interface.

func StripePattern

func StripePattern(a, b Tuple) *StripePatternT

StripePattern returns a StripePatternT.

func (*StripePatternT) LocalPatternAt

func (s *StripePatternT) LocalPatternAt(localPoint Tuple) Tuple

LocalPatternAt returns a color at a local point.

type TriangleT

type TriangleT struct {
	Shape
	P1 Tuple
	P2 Tuple
	P3 Tuple

	N1 Tuple
	N2 Tuple
	N3 Tuple

	E1     Tuple
	E2     Tuple
	Normal Tuple
	// contains filtered or unexported fields
}

TriangleT represents a triangle object.

func Triangle

func Triangle(p1, p2, p3 Tuple) *TriangleT

Triangle returns a new TriangleT.

func (*TriangleT) Bounds

func (t *TriangleT) Bounds() *BoundsT

Bounds returns the minimum bounding box of the object in object (untransformed) space.

func (*TriangleT) Includes

func (t *TriangleT) Includes(other Object) bool

Includes returns whether this object includes (or actually is) the other object.

func (*TriangleT) LocalIntersect

func (t *TriangleT) LocalIntersect(ray RayT) []IntersectionT

LocalIntersect returns a slice of IntersectionT values where the transformed (object space) ray intersects the object.

func (*TriangleT) LocalNormalAt

func (t *TriangleT) LocalNormalAt(objectPoint Tuple, hit *IntersectionT) Tuple

LocalNormalAt returns the normal vector at the given point of intersection (transformed to object space) with the object.

func (*TriangleT) SetMaterial

func (t *TriangleT) SetMaterial(material MaterialT) Object

SetMaterial sets the object's material.

func (*TriangleT) SetParent

func (t *TriangleT) SetParent(parent Object) Object

SetParent sets the object's parent object.

func (*TriangleT) SetTransform

func (t *TriangleT) SetTransform(m M4) Object

SetTransform sets the object's transform 4x4 matrix.

type Tuple

type Tuple [4]float64

Tuple is a 4-float vector.

func Color

func Color(red, green, blue float64) Tuple

Color returns a new Tuple as a color.

func Lighting

func Lighting(material *MaterialT, object Object, light *PointLightT, point Tuple, eyeVector Tuple, normalVector Tuple, inShadow bool) Tuple

Lighting calculates the lighting on an object and returns the color as a Tuple.

func NormalToWorld

func NormalToWorld(object Object, normal Tuple) Tuple

NormalToWorld converts an object-space normal to world space, taking into account all the parents of the object.

func PatternAt

func PatternAt(pattern Pattern, object Object, worldPoint Tuple) Tuple

PatternAt returns the pattern at the given point of intersection with the object.

func Point

func Point(x, y, z float64) Tuple

Point returns a new Tuple as a Point.

func Vector

func Vector(x, y, z float64) Tuple

Vector returns a new Tuple as a Vector.

func WorldToObject

func WorldToObject(object Object, point Tuple) Tuple

WorldToObject converts a world-space point to object space, taking into account all the parents of the object.

func (Tuple) Add

func (t Tuple) Add(other Tuple) Tuple

Add adds two Tuples and returns a new one.

func (Tuple) Alpha

func (t Tuple) Alpha() float64

Alpha returns the alpha component of a color (Tuple).

func (Tuple) Blue

func (t Tuple) Blue() float64

Blue returns the blue component of a color (Tuple).

func (Tuple) Cross

func (t Tuple) Cross(other Tuple) Tuple

Cross computes the cross product of two vectors (order matters and this implements t cross other).

func (Tuple) DivScalar

func (t Tuple) DivScalar(f float64) Tuple

DivScalar divides a tuple by a scalar.

func (Tuple) Dot

func (t Tuple) Dot(other Tuple) float64

Dot computes the dot product (aka "scalar product" or "inner product") of two vectors (Tuples). The dot product is the cosine of the angle between two unit vectors.

func (Tuple) Equal

func (t Tuple) Equal(other Tuple) bool

Equal tests if two Tuples are equal.

func (Tuple) Green

func (t Tuple) Green() float64

Green returns the green component of a color (Tuple).

func (Tuple) HadamardProduct

func (t Tuple) HadamardProduct(other Tuple) Tuple

HadamardProduct computes the product of two colors.

func (Tuple) IsPoint

func (t Tuple) IsPoint() bool

IsPoint identifies the Tuple as a Point.

func (Tuple) IsVector

func (t Tuple) IsVector() bool

IsVector identifies the Tuple as a Vector.

func (Tuple) Magnitude

func (t Tuple) Magnitude() float64

Magnitude computes the magnitude or length of a vector (Tuple).

func (Tuple) MultScalar

func (t Tuple) MultScalar(f float64) Tuple

MultScalar multiplies a tuple by a scalar.

func (Tuple) Negate

func (t Tuple) Negate() Tuple

Negate negates a Tuple.

func (Tuple) Normalize

func (t Tuple) Normalize() Tuple

Normalize normalizes a vector to a unit vector (of length 1).

func (Tuple) Red

func (t Tuple) Red() float64

Red returns the red component of a color (Tuple).

func (Tuple) Reflect

func (t Tuple) Reflect(normal Tuple) Tuple

Reflect reflects a vector around a prodived normal vector.

func (Tuple) Sub

func (t Tuple) Sub(other Tuple) Tuple

Sub subtracts two Tuples and returns a new one.

func (Tuple) W

func (t Tuple) W() float64

W returns the W value of the Tuple.

func (Tuple) X

func (t Tuple) X() float64

X returns the X value of the Tuple.

func (Tuple) Y

func (t Tuple) Y() float64

Y returns the Y value of the Tuple.

func (Tuple) Z

func (t Tuple) Z() float64

Z returns the Z value of the Tuple.

type WorldT

type WorldT struct {
	Objects []Object
	Lights  []*PointLightT // TODO: Replace with light interfaces.
}

WorldT represents the world to be rendered.

func DefaultWorld

func DefaultWorld() *WorldT

DefaultWorld returns a default test world.

func World

func World() *WorldT

World creates an empty world.

func (*WorldT) ColorAt

func (w *WorldT) ColorAt(ray RayT, remaining int) Tuple

ColorAt returns the color (as a Tuple) when casting the given ray.

func (*WorldT) IntersectWorld

func (w *WorldT) IntersectWorld(ray RayT) []IntersectionT

IntersectWorld intersects a world with a ray.

func (*WorldT) IsShadowed

func (w *WorldT) IsShadowed(point Tuple, light *PointLightT) bool

IsShadowed determines if the provided point is in a shadow for the given light.

func (*WorldT) ReflectedColor

func (w *WorldT) ReflectedColor(comps *Comps, remaining int) Tuple

ReflectedColor returns the reflected color for the precomputed intersection.

func (*WorldT) RefractedColor

func (w *WorldT) RefractedColor(comps *Comps, remaining int) Tuple

RefractedColor returns the refracted color for the precomputed intersection.

func (*WorldT) ShadeHit

func (w *WorldT) ShadeHit(comps *Comps, remaining int) Tuple

ShadeHit returns the color (as a Tuple) for the precomputed intersection.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL