ray

package module
v0.0.0-...-ffd7058 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2020 License: BSD-2-Clause Imports: 15 Imported by: 0

README

ray

software raytracer

Documentation

Index

Constants

View Source
const (
	X int = 0
	Y     = 1
	Z     = 2
)

Variables

View Source
var BigEpsilon = 1e-3
View Source
var Blue = FloatColor{B: 1}

Blue ...

View Source
var Building = Surface{
	Ka:     0.5,
	Kd:     0.9,
	Ks:     0.2,
	Color:  FloatColor{R: .5, G: .5, B: .5},
	Nphong: 10,
}
View Source
var DefaultSurface = Surface{
	Ka:     0.5,
	Kd:     0.5,
	Ks:     0.5,
	Color:  FloatColor{R: 0.5, G: 0.5, B: 0.5},
	Nphong: 30,
}
View Source
var Diffuse = Surface{
	Ka:     0.5,
	Kd:     0.8,
	Ks:     0.2,
	Color:  FloatColor{R: 0.6, G: 0.4, B: 0.3},
	Nphong: 30,
}
View Source
var Epsilon = 1e-8

var Epsilon = math.Nextafter(1.0, 2.0) - 1.0

View Source
var Green = FloatColor{G: 1}

Green ...

View Source
var IDTransform = Transform{/* contains filtered or unexported fields */}

IDTransform ...

View Source
var MaxDepth = 7

MaxDepth is the max tracing recursion level

View Source
var Mirror = Surface{
	Ka:     0,
	Kd:     0.1,
	Ks:     0.9,
	Color:  FloatColor{R: 0, G: 0, B: 0},
	Nphong: 1000,
}
View Source
var Ocher2 = Surface{
	Ka:     0.7,
	Kd:     0.5,
	Ks:     0.4,
	Color:  FloatColor{R: 1, G: 1 / 1.5, B: 1 / 2},
	Nphong: 50,
}
View Source
var Origin = Point3{0, 0, 0}
View Source
var Red = FloatColor{R: 1}

Red ...

View Source
var White = FloatColor{R: 1, G: 1, B: 1}

White ...

View Source
var White1 = Surface{
	Ka:     0.5,
	Kd:     0.9,
	Ks:     0.5,
	Color:  FloatColor{R: 1, G: 1, B: 1},
	Nphong: 100,
}

Functions

This section is empty.

Types

type BoundingBox

type BoundingBox struct {
	Transform
	// contains filtered or unexported fields
}

BoundingBox ...

func NewBoundingBox

func NewBoundingBox() *BoundingBox

NewBoundingBox ...

func (*BoundingBox) AddObjects

func (bb *BoundingBox) AddObjects(obj ...Object)

AddObjects ...

func (*BoundingBox) Intersect

func (bb *BoundingBox) Intersect(r Ray) *Hit

Intersect ...

func (*BoundingBox) MinMax

func (bb *BoundingBox) MinMax() (Point3, Point3)

MinMax ...

func (*BoundingBox) Name

func (bb *BoundingBox) Name() string

Name returns the object's name

func (*BoundingBox) SetName

func (bb *BoundingBox) SetName(name string)

SetName ...

type Camera

type Camera struct {
	Transform

	Width  int
	Height int
	Image  *image.RGBA
	// contains filtered or unexported fields
}

Camera is the canonical camera object, tracing rays and writing to an image

func NewCamera

func NewCamera(f, dx, dy float64, w int) *Camera

NewCamera creates a camera with a focal length, camera width, camera height and image width. Located at origin and points towards negative z in an orthonormal basis

func (*Camera) BuildRay

func (c *Camera) BuildRay(x, y int) Ray

BuildRay creates a Ray in global space given an image pixel position

func (*Camera) Project

func (c *Camera) Project(p Point3) (int, int)

Project ...

func (*Camera) RotateX

func (c *Camera) RotateX(x float64) *Camera

RotateX applies a rotation around x-axis

func (*Camera) RotateY

func (c *Camera) RotateY(y float64) *Camera

RotateY applies a rotation around y-axis

func (*Camera) RotateZ

func (c *Camera) RotateZ(z float64) *Camera

RotateZ applies a rotation around z-axis

func (*Camera) Scale

func (c *Camera) Scale(x, y, z float64) *Camera

Scale applies a scaling transform

func (*Camera) Translate

func (c *Camera) Translate(x, y, z float64) *Camera

Translate applies a translation

type Cube

type Cube struct {
	Transform
	Surface
	// contains filtered or unexported fields
}

Cube is a canonical cube, centered of origin, of side 2 (-1 to +1)

func NewCube

func NewCube() *Cube

NewCube instantiate a new cube

func (*Cube) Intersect

func (c *Cube) Intersect(r Ray) *Hit

Intersect ...

func (*Cube) MinMax

func (c *Cube) MinMax() (Point3, Point3)

func (*Cube) Name

func (c *Cube) Name() string

Name returns the Cube's name

func (*Cube) RotateX

func (c *Cube) RotateX(x float64) *Cube

RotateX applies a rotation around x-axis to the Cube

func (*Cube) RotateY

func (c *Cube) RotateY(y float64) *Cube

RotateY applies a rotation around y-axis to the Cube

func (*Cube) RotateZ

func (c *Cube) RotateZ(z float64) *Cube

RotateZ applies a rotation around z-axis to the Cube

func (*Cube) Scale

func (c *Cube) Scale(x, y, z float64) *Cube

Scale applies a scaling transform to the Cube

func (*Cube) SetName

func (c *Cube) SetName(name string)

SetName ...

func (*Cube) Surf

func (c *Cube) Surf() *Surface

func (*Cube) Translate

func (c *Cube) Translate(x, y, z float64) *Cube

Translate applies a translation to the cube

type FloatColor

type FloatColor struct {
	R float64
	G float64
	B float64
}

FloatColor implements the color.Color interface by having a RGBA() method

func (*FloatColor) Add

func (fc *FloatColor) Add(b FloatColor) FloatColor

Add adds each RGB component of a color with another color component

func (FloatColor) Color

func (fc FloatColor) Color() color.RGBA

Color ...

func (FloatColor) MulC

func (fc FloatColor) MulC(b FloatColor) FloatColor

MulC multiplies each RGB component of a color by another color component

func (FloatColor) MulF

func (fc FloatColor) MulF(b float64) FloatColor

MulF multiplies each RGB component by a scalar

func (FloatColor) RGBA

func (fc FloatColor) RGBA() (uint32, uint32, uint32, uint32)

RGBA return the color as RGBA

type Hit

type Hit struct {
	*Surface // material properties
	// contains filtered or unexported fields
}

Hit contains intersection informations: surface where hit, and incident and normal ray (both global and local)

type Light

type Light interface {
	RayToLight(Point3) Ray
	Color(Ray) FloatColor
	Sun() bool
}

type Matrix4

type Matrix4 [4][4]float64

func ID

func ID() Matrix4

ID returns a homogeneous, 4x4, row-major, identity matrix.

func RotationX

func RotationX(a float64) Matrix4

RotationX returns a matrix describing a rotation in radians around X-axis in right-hand coordinates.

func RotationY

func RotationY(a float64) Matrix4

RotationY returns a matrix describing a rotation in radians around Y-axis in right-hand coordinates.

func RotationZ

func RotationZ(a float64) Matrix4

RotationZ returns a matrix describing a rotation in radians around Z-axis in right-hand coordinates.

func Scaling

func Scaling(x, y, z float64) Matrix4

Scaling returns a matrix describing a scaling along all 3 axis in space.

func Translation

func Translation(x, y, z float64) Matrix4

Translation returns a matrix describing a translation along all 3 axis in space.

func Transpose

func Transpose(m Matrix4) Matrix4

Transpose returns a new matrix which is the transposition of the given matrix.

func (Matrix4) MulM

func (a Matrix4) MulM(b Matrix4) Matrix4

MulM multiply a with b, return resulting matrix

func (Matrix4) MulP

func (a Matrix4) MulP(b Point3) Point3

MulP returns a point resulting from the application of the transformation matrix to the given point.

func (Matrix4) MulV

func (a Matrix4) MulV(b Vector3) Vector3

MulV returns a vector resulting from the application of the transformation matrix to the given vector.

type Object

type Object interface {
	Transformer
	Intersect(Ray) *Hit
	Name() string
	MinMax() (Point3, Point3) // in local coords
}

Object is the interface for all raytraced objects

type Plane

type Plane struct {
	Transform
	Surface
	// contains filtered or unexported fields
}

Plane is a plane of all points with y = 0

func NewPlane

func NewPlane() *Plane

NewPlane creates a xz plane of 2 units sides centered on origin

func (*Plane) Intersect

func (p *Plane) Intersect(r Ray) *Hit

Intersect returns intersected object and normal ray

func (*Plane) MinMax

func (*Plane) MinMax() (Point3, Point3)

MinMax returns the min max points of the bounding box in local coords

func (*Plane) Name

func (p *Plane) Name() string

Name returns the object name

func (*Plane) RotateX

func (p *Plane) RotateX(x float64) *Plane

RotateX applies a rotation around x-axis

func (*Plane) RotateY

func (p *Plane) RotateY(y float64) *Plane

RotateY applies a rotation around y-axis

func (*Plane) RotateZ

func (p *Plane) RotateZ(z float64) *Plane

RotateZ applies a rotation around z-axis

func (*Plane) Scale

func (p *Plane) Scale(x, y, z float64) *Plane

Scale applies a scaling transform

func (*Plane) SetName

func (p *Plane) SetName(name string)

SetName ...

func (*Plane) Surf

func (p *Plane) Surf() *Surface

Surf ...

func (*Plane) Translate

func (p *Plane) Translate(x, y, z float64) *Plane

Translate applies a translation

type Point3

type Point3 [3]float64

func (Point3) Dist

func (a Point3) Dist(b Point3) float64

Dist returns the distance between 2 points a and b.

func (Point3) SquareDist

func (a Point3) SquareDist(b Point3) float64

SquareDist returns the squared distance between points a and b.

type PointLight

type PointLight struct {
	Transform
	// contains filtered or unexported fields
}

func NewPointLight

func NewPointLight(c FloatColor) *PointLight

func (*PointLight) Color

func (p *PointLight) Color(r Ray) FloatColor

func (*PointLight) RayToLight

func (p *PointLight) RayToLight(pt Point3) Ray

func (*PointLight) SetSun

func (p *PointLight) SetSun(sun bool)

func (*PointLight) Sun

func (p *PointLight) Sun() bool

func (*PointLight) Translate

func (p *PointLight) Translate(x, y, z float64) *PointLight

Translate applies a translation

type Preview

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

Preview ...

func (*Preview) Draw

func (pv *Preview) Draw(screen *ebiten.Image)

Draw ...

func (*Preview) Layout

func (pv *Preview) Layout(outsideWidth, outsideHeight int) (int, int)

Layout ...

func (*Preview) Update

func (pv *Preview) Update() error

Update ...

type Ray

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

Ray contains a point (the origin) and a vector (the direction).

func NewRay

func NewRay(src, dst Point3) Ray

NewRay creates a Ray going from the starting point to the destination point.

func (*Ray) Normalize

func (r *Ray) Normalize()

Normalize modifies the ray by normalizing its direction.

type Scene

type Scene struct {
	MaxDepth int

	Ambiant FloatColor

	Preview bool
	// contains filtered or unexported fields
}

Scene contains the objects, lights, camera and method to render them

func NewScene

func NewScene(cam *Camera) *Scene

NewScene instantiates a scene with a Camera

func (*Scene) AddLights

func (s *Scene) AddLights(list ...Light)

AddLights adds the lights to the scene

func (*Scene) AddObjects

func (s *Scene) AddObjects(list ...Object)

AddObjects adds the objects to the scene

func (*Scene) Background

func (s *Scene) Background() FloatColor

Background ...

func (*Scene) Raytrace

func (s *Scene) Raytrace()

Raytrace ...

func (*Scene) WriteJPG

func (s *Scene) WriteJPG(name string) error

WriteJPG ...

func (*Scene) WritePNG

func (s *Scene) WritePNG(name string) error

WritePNG ...

type Sphere

type Sphere struct {
	Transform
	Surface
	// contains filtered or unexported fields
}

Sphere is a canonical sphere, centered of origin, of radius 1

func NewSphere

func NewSphere() *Sphere

NewSphere instantiate a new sphere

func (*Sphere) Intersect

func (s *Sphere) Intersect(r Ray) *Hit

Intersect finds the intersection point (if any) between a global ray and this sphere, and the normal at intersection point

func (*Sphere) MinMax

func (s *Sphere) MinMax() (Point3, Point3)

MinMax ...

func (*Sphere) Name

func (s *Sphere) Name() string

Name returns the sphere's name

func (*Sphere) RotateX

func (s *Sphere) RotateX(x float64) *Sphere

RotateX applies a rotation around x-axis to the sphere

func (*Sphere) RotateY

func (s *Sphere) RotateY(y float64) *Sphere

RotateY applies a rotation around y-axis to the sphere

func (*Sphere) RotateZ

func (s *Sphere) RotateZ(z float64) *Sphere

RotateZ applies a rotation around z-axis to the sphere

func (*Sphere) Scale

func (s *Sphere) Scale(x, y, z float64) *Sphere

Scale applies a scaling transform to the sphere

func (*Sphere) SetName

func (s *Sphere) SetName(name string)

SetName ...

func (*Sphere) Surf

func (s *Sphere) Surf() *Surface

Surf ...

func (*Sphere) Translate

func (s *Sphere) Translate(x, y, z float64) *Sphere

Translate applies a translation to the sphere

type Surface

type Surface struct {
	Ka     float64
	Kd     float64
	Ks     float64
	Color  FloatColor
	Nphong float64
}

func (*Surface) ColorAt

func (s *Surface) ColorAt(h *Hit) FloatColor

type Transform

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

Transform ...

func (*Transform) Check

func (t *Transform) Check() Matrix4

Check ...

func (*Transform) PointToGlobal

func (t *Transform) PointToGlobal(p Point3) Point3

PointToGlobal ...

func (*Transform) PointToLocal

func (t *Transform) PointToLocal(p Point3) Point3

PointToLocal ...

func (*Transform) RayToGlobal

func (t *Transform) RayToGlobal(r Ray) Ray

RayToGlobal ...

func (*Transform) RayToLocal

func (t *Transform) RayToLocal(r Ray) Ray

RayToLocal ...

func (*Transform) RotateX

func (t *Transform) RotateX(a float64)

RotateX ...

func (*Transform) RotateY

func (t *Transform) RotateY(a float64)

RotateY ...

func (*Transform) RotateZ

func (t *Transform) RotateZ(a float64)

RotateZ ...

func (*Transform) Scale

func (t *Transform) Scale(x, y, z float64)

Scale ...

func (*Transform) Translate

func (t *Transform) Translate(x, y, z float64)

Translate ...

type Transformer

type Transformer interface {
	PointToLocal(Point3) Point3
	PointToGlobal(Point3) Point3
	RayToLocal(Ray) Ray
	RayToGlobal(Ray) Ray
}

Transformer ...

type Vector3

type Vector3 [3]float64

func NewVec

func NewVec(a, b Point3) Vector3

NewVec creates a new vector from point a to point b.

func (Vector3) Add

func (v Vector3) Add(b Vector3) Vector3

Add adds 2 vectors and returns a new vector.

func (Vector3) Cross

func (a Vector3) Cross(b Vector3) Vector3

Cross returns a new vector which is the cross product of the vector parameters. The result is a vector orthogonal to the plane formed by the 2 input vectors, and such that uvw is a direct ...

func (Vector3) Dot

func (a Vector3) Dot(b Vector3) float64

Dot calculates the dot product of 2 vectors (equals to |u|*|v|*cos(uv))

func (Vector3) Mult

func (v Vector3) Mult(f float64) Vector3

Mult returns a new vector where all components have been multiplied by the given factor.

func (Vector3) Norm

func (v Vector3) Norm() float64

Norm returns the norm of the vector.

func (*Vector3) Normalize

func (v *Vector3) Normalize()

Normalize the vector, which is modified (has a norm of 1).

func (*Vector3) Reverse

func (v *Vector3) Reverse()

Reverse modifies the vector to reverse its direction.

func (Vector3) Sub

func (v Vector3) Sub(b Vector3) Vector3

Sub substracts the second vector from the first and returns a new vector.

Directories

Path Synopsis
tests
001 command
002 command
003 command
004 command
005 command
006 command
007 command
008 command
009 command
010 command
011 command
012 command
013 command
014 command
015 command
016 command

Jump to

Keyboard shortcuts

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