yagrt

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

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

Go to latest
Published: Jun 24, 2020 License: GPL-3.0 Imports: 10 Imported by: 0

README

Yet Another Go Ray Tracer (yagrt)

This is a CPU ray tracer written purely in Golang.

✅ Sphere and Triangle Intersections
✅ Point Lights
✅ Ambient, Diffuse and Specular Shading
✅ Shadows

TODO:

  • Acceleration structures (in progress)
  • Reflection and Refraction (in progress)
  • Many other Light types
  • Depth of Field
  • Obj and Stl support

This is a WIP

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EPS float64 = 1e-3

EPS represents a generic epsilon value

View Source
var HitEpsilon float64 = 1e-3

HitEpsilon is used to check ray intersections

View Source
var INF float64 = math.Inf(1)

INF represents infinity

View Source
var ShadowEpsilon float64 = 1e-3

ShadowEpsilon is used to shift intersection points to prevent surface acne

Functions

func Determinant

func Determinant(v1, v2, v3 *Vector) float64

Determinant is used to take 3x3 matrix determinant

func ParseFloat

func ParseFloat(str string) (float64, error)

ParseFloat parses floats from a given string by extending the functionality of strconv.ParseFloat to include scientific notation

func Render

func Render(sceneFile string)

Render parses the scene file and then spawns goroutines for each line to quickly a scene, in the end it encodes the image into a png with the same name as the scene file.

func SubRender

func SubRender(xStart int, yStart int, xEnd int, yEnd int, scene *Scene, camera *Camera, image *image.NRGBA, wg *sync.WaitGroup)

SubRender is a Goroutine which renders the given part of an image

Types

type Camera

type Camera struct {
	Position  Vector
	U         Vector
	V         Vector
	W         Vector
	NearPlane struct {
		Left   float64
		Right  float64
		Bottom float64
		Top    float64
	}
	Distance   float64
	Resolution struct {
		Width  int
		Height int
	}
	ImageName string
	// contains filtered or unexported fields
}

Camera is the point where we look into the scene it holds a position, and the u, v, w vectors.

func (*Camera) CastRay

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

CastRay creates a Ray from the position of the camera to the (x,y) on image plane

func (*Camera) LookAt

func (c *Camera) LookAt(p, gaze, up Vector)

LookAt recomputes the coordinate system of the camera from gaze and up

type Color

type Color struct {
	R, G, B float64
}

Color holds three float64s representing RGB values of a pixel

func (Color) Add

func (c Color) Add(o Color) Color

Add sums two colors channel-wise

func (Color) Div

func (c Color) Div(d float64) Color

Div divides a color by a scalar value

func (Color) Mul

func (c Color) Mul(m float64) Color

Mul multiplies a color with a scalar value

func (Color) MulColor

func (c Color) MulColor(o Color) Color

MulColor multiples a color with another color channel-wise

func (Color) Sub

func (c Color) Sub(o Color) Color

Sub subtracts two colors channel-wise

type Hit

type Hit struct {
	T      float64
	Shape  Shape
	Normal Vector
}

Hit contains information about an intersection point

type Material

type Material struct {
	AmbientReflectance  Color
	DiffuseReflectance  Color
	SpecularReflectance Color
	PhongExponent       float64
}

Material contains different values used in shading

type Mesh

type Mesh struct {
	Triangles []Triangle
	Mat       Material
}

Mesh defies a set of Triangles and a material for them

func (*Mesh) Intersect

func (m *Mesh) Intersect(r Ray) *Hit

Intersect returns Hit data for a given ray

func (*Mesh) Material

func (m *Mesh) Material() *Material

Material returns the material of a mesh

type PointLight

type PointLight struct {
	Position  Vector
	Intensity Color
}

PointLight represents a point light with a defined position and intensity

type Ray

type Ray struct {
	Origin Vector
	Dir    Vector
}

Ray represents a ray sent from camera into the scene it has an origin and a direction

type Scene

type Scene struct {
	BackgroundColor Color
	Cameras         []Camera
	AmbientLight    Color
	PointLights     []PointLight
	Materials       []Material
	VertexData      []Vector
	Shapes          []Shape
}

Scene describes a scene with cameras, lights, shapes and their materials

func ParseScene

func ParseScene(filename string) *Scene

ParseScene parses the given xml scene and fill in a Scene struct TODO: Fix this crappy code

func (*Scene) Intersect

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

Intersect tries to intersect a given ray with each object in the scene and returns the closest intersection as a Hit struct

func (*Scene) Sample

func (s *Scene) Sample(r Ray) Color

Sample is used to sample a scene to get a color for that sample

type Shape

type Shape interface {
	// Incersects a ray with a shape and returns the Hit data.
	Intersect(r Ray) *Hit
	// Material returns the material of a shape.
	Material() *Material
}

Shape is an object a ray can intersect with

type Sphere

type Sphere struct {
	Origin Vector
	Radius float64
	Mat    Material
}

Sphere represents a sphere in 3D, it has an origin and a radius

func (*Sphere) Intersect

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

Intersect calculates the intersection point of a ray with the sphere It returns the distance of the intersection from the ray's origin if ray intersects the sphere, INF if it doesn't.

func (*Sphere) Material

func (s *Sphere) Material() *Material

Material returns the material of a sphere

func (*Sphere) Normal

func (s *Sphere) Normal(p Vector) Vector

Normal returns the sufrace normal of a sphere

type Triangle

type Triangle struct {
	V0, V1, V2 Vector
	Mat        Material
}

Triangle consists of three points and a material

func (*Triangle) Intersect

func (t *Triangle) Intersect(r Ray) *Hit

Intersect returns the Hit data for a given ray

func (*Triangle) Material

func (t *Triangle) Material() *Material

Material returns the material of a triangle

func (*Triangle) Normal

func (t *Triangle) Normal() Vector

Normal calculates the normal vector for a triangle

type Vector

type Vector struct {
	X, Y, Z float64
}

Vector is a 3D vector containing 3 float64s for each dimension

func (Vector) Add

func (v Vector) Add(o Vector) Vector

Add adds two vectors together

func (Vector) AddScalar

func (v Vector) AddScalar(s float64) Vector

AddScalar adds a scalar value to all of the components of a vector

func (Vector) Cross

func (v Vector) Cross(o Vector) Vector

Cross calculates the cross product of two vectors (v.Cross(o) = v X o)

func (Vector) DivScalar

func (v Vector) DivScalar(d float64) Vector

DivScalar divies the vector by the given divisor.

func (Vector) Dot

func (v Vector) Dot(o Vector) float64

Dot calculates the dot product of two vectors

func (Vector) Length

func (v Vector) Length() float64

Length returns the length of a vector

func (Vector) Mul

func (v Vector) Mul(m float64) Vector

Mul multiplies the vector with the given multiplier.

func (Vector) Negate

func (v Vector) Negate() Vector

Negate flips the sign of a vector, same as Vector.Mul(-1)

func (Vector) Normalize

func (v Vector) Normalize() Vector

Normalize normalizes the vector by dividing it by its length

func (Vector) Sub

func (v Vector) Sub(o Vector) Vector

Sub subtracts two vectors

func (Vector) SubScalar

func (v Vector) SubScalar(s float64) Vector

SubScalar subtracts a scalar value from all the components of a vector.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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