package module
Version: v0.0.0-...-27cddc1 Latest Latest

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

Go to latest
Published: Aug 18, 2020 License: MIT Imports: 18 Imported by: 169



3D software rendering in pure Go. No OpenGL, no C extensions, no nothin'.



It's like OpenGL, but it's not. It's FauxGL.

It doesn't use your graphics card, only your CPU. So it's slow and unsuitable for realtime rendering. But it's still pretty fast. It works the same way OpenGL works - rasterizing.

  • STL, OBJ, PLY, 3DS file formats
  • triangle rasterization
  • vertex and fragment "shaders"
  • view volume clipping
  • face culling
  • alpha blending
  • textures
  • triangle & line meshes
  • depth biasing
  • wireframe rendering
  • built-in shapes (plane, sphere, cube, cylinder, cone)
  • anti-aliasing (via supersampling)
  • voxel rendering
  • parallel processing

FauxGL uses all of your CPU cores. But none of your GPU.

Rendering the Stanford Dragon shown above (871306 triangles) at 1920x1080px takes about 150 milliseconds on my machine. With 4x4=16x supersampling, it takes about 950 milliseconds. This is the time to render a frame and does not include loading the mesh from disk.

Go Get
go get -u github.com/fogleman/fauxgl
Go Run
cd go/src/github.com/fogleman/fauxgl
go run examples/hello.go
Go Doc


Complete Example
package main

import (
	. "github.com/fogleman/fauxgl"

const (
	scale  = 1    // optional supersampling
	width  = 1920 // output width in pixels
	height = 1080 // output height in pixels
	fovy   = 30   // vertical field of view in degrees
	near   = 1    // near clipping plane
	far    = 10   // far clipping plane

var (
	eye    = V(-3, 1, -0.75)               // camera position
	center = V(0, -0.07, 0)                // view center position
	up     = V(0, 1, 0)                    // up vector
	light  = V(-0.75, 1, 0.25).Normalize() // light direction
	color  = HexColor("#468966")           // object color

func main() {
	// load a mesh
	mesh, err := LoadOBJ("examples/dragon.obj")
	if err != nil {

	// fit mesh in a bi-unit cube centered at the origin

	// smooth the normals

	// create a rendering context
	context := NewContext(width*scale, height*scale)

	// create transformation matrix and light direction
	aspect := float64(width) / float64(height)
	matrix := LookAt(eye, center, up).Perspective(fovy, aspect, near, far)

	// use builtin phong shader
	shader := NewPhongShader(matrix, light, eye)
	shader.ObjectColor = color
	context.Shader = shader

	// render

	// downsample image for antialiasing
	image := context.Image()
	image = resize.Resize(width, height, image, resize.Bilinear)

	// save image
	SavePNG("out.png", image)





This section is empty.


View Source
var (
	Discard     = Color{}
	Transparent = Color{}
	Black       = Color{0, 0, 0, 1}
	White       = Color{1, 1, 1, 1}
View Source
var EmptyBox = Box{}


func AbsInt

func AbsInt(x int) int

func Clamp

func Clamp(x, lo, hi float64) float64

func ClampInt

func ClampInt(x, lo, hi int) int

func Degrees

func Degrees(radians float64) float64

func InterpolateFloats

func InterpolateFloats(v1, v2, v3 float64, b VectorW) float64

func LoadImage

func LoadImage(path string) (image.Image, error)

func ParseFloats

func ParseFloats(items []string) []float64

func Radians

func Radians(degrees float64) float64

func Round

func Round(a float64) int

func RoundPlaces

func RoundPlaces(a float64, places int) float64

func SavePNG

func SavePNG(path string, im image.Image) error

func SaveSTL

func SaveSTL(path string, mesh *Mesh) error


type Box

type Box struct {
	Min, Max Vector

func BoxForBoxes

func BoxForBoxes(boxes []Box) Box

func (Box) Anchor

func (a Box) Anchor(anchor Vector) Vector

func (Box) Center

func (a Box) Center() Vector

func (Box) Contains

func (a Box) Contains(b Vector) bool

func (Box) ContainsBox

func (a Box) ContainsBox(b Box) bool

func (Box) Extend

func (a Box) Extend(b Box) Box

func (Box) Intersection

func (a Box) Intersection(b Box) Box

func (Box) Intersects

func (a Box) Intersects(b Box) bool

func (Box) Offset

func (a Box) Offset(x float64) Box

func (Box) Size

func (a Box) Size() Vector

func (Box) Transform

func (a Box) Transform(m Matrix) Box

func (Box) Translate

func (a Box) Translate(v Vector) Box

func (Box) Volume

func (a Box) Volume() float64

type Color

type Color struct {
	R, G, B, A float64

func Gray

func Gray(x float64) Color

func HexColor

func HexColor(x string) Color

func InterpolateColors

func InterpolateColors(v1, v2, v3 Color, b VectorW) Color

func MakeColor

func MakeColor(c color.Color) Color

func (Color) Add

func (a Color) Add(b Color) Color

func (Color) AddScalar

func (a Color) AddScalar(b float64) Color

func (Color) Alpha

func (a Color) Alpha(alpha float64) Color

func (Color) Div

func (a Color) Div(b Color) Color

func (Color) DivScalar

func (a Color) DivScalar(b float64) Color

func (Color) Lerp

func (a Color) Lerp(b Color, t float64) Color

func (Color) Max

func (a Color) Max(b Color) Color

func (Color) Min

func (a Color) Min(b Color) Color

func (Color) Mul

func (a Color) Mul(b Color) Color

func (Color) MulScalar

func (a Color) MulScalar(b float64) Color

func (Color) NRGBA

func (c Color) NRGBA() color.NRGBA

func (Color) Opaque

func (a Color) Opaque() Color

func (Color) Pow

func (a Color) Pow(b float64) Color

func (Color) Sub

func (a Color) Sub(b Color) Color

func (Color) SubScalar

func (a Color) SubScalar(b float64) Color

type Context

type Context struct {
	Width       int
	Height      int
	ColorBuffer *image.NRGBA
	DepthBuffer []float64
	ClearColor  Color
	Shader      Shader
	ReadDepth   bool
	WriteDepth  bool
	WriteColor  bool
	AlphaBlend  bool
	Wireframe   bool
	FrontFace   Face
	Cull        Cull
	LineWidth   float64
	DepthBias   float64
	// contains filtered or unexported fields

func NewContext

func NewContext(width, height int) *Context

func (*Context) ClearColorBuffer

func (dc *Context) ClearColorBuffer()

func (*Context) ClearColorBufferWith

func (dc *Context) ClearColorBufferWith(color Color)

func (*Context) ClearDepthBuffer

func (dc *Context) ClearDepthBuffer()

func (*Context) ClearDepthBufferWith

func (dc *Context) ClearDepthBufferWith(value float64)

func (*Context) DepthImage

func (dc *Context) DepthImage() image.Image

func (*Context) DrawLine

func (dc *Context) DrawLine(t *Line) RasterizeInfo

func (*Context) DrawLines

func (dc *Context) DrawLines(lines []*Line) RasterizeInfo

func (*Context) DrawMesh

func (dc *Context) DrawMesh(mesh *Mesh) RasterizeInfo

func (*Context) DrawTriangle

func (dc *Context) DrawTriangle(t *Triangle) RasterizeInfo

func (*Context) DrawTriangles

func (dc *Context) DrawTriangles(triangles []*Triangle) RasterizeInfo

func (*Context) Image

func (dc *Context) Image() image.Image

type Cull

type Cull int
const (
	CullNone Cull

type Face

type Face int
const (
	FaceCW Face

type ImageTexture

type ImageTexture struct {
	Width  int
	Height int
	Image  image.Image

func (*ImageTexture) BilinearSample

func (t *ImageTexture) BilinearSample(u, v float64) Color

func (*ImageTexture) Sample

func (t *ImageTexture) Sample(u, v float64) Color

type Line

type Line struct {
	V1, V2 Vertex

func ClipLine

func ClipLine(l *Line) *Line

func NewLine

func NewLine(v1, v2 Vertex) *Line

func NewLineForPoints

func NewLineForPoints(p1, p2 Vector) *Line

func (*Line) BoundingBox

func (l *Line) BoundingBox() Box

func (*Line) Transform

func (l *Line) Transform(matrix Matrix)

type Matrix

type Matrix struct {
	X00, X01, X02, X03 float64
	X10, X11, X12, X13 float64
	X20, X21, X22, X23 float64
	X30, X31, X32, X33 float64

func Frustum

func Frustum(l, r, b, t, n, f float64) Matrix

func Identity

func Identity() Matrix

func LookAt

func LookAt(eye, center, up Vector) Matrix

func LookAtDirection

func LookAtDirection(forward, up Vector) Matrix

func Orient

func Orient(position, size, up Vector, rotation float64) Matrix

func Orthographic

func Orthographic(l, r, b, t, n, f float64) Matrix

func Perspective

func Perspective(fovy, aspect, near, far float64) Matrix

func Rotate

func Rotate(v Vector, a float64) Matrix

func RotateTo

func RotateTo(a, b Vector) Matrix

func Scale

func Scale(v Vector) Matrix

func Screen

func Screen(w, h int) Matrix

func Translate

func Translate(v Vector) Matrix

func Viewport

func Viewport(x, y, w, h float64) Matrix

func (Matrix) Determinant

func (a Matrix) Determinant() float64

func (Matrix) Frustum

func (m Matrix) Frustum(l, r, b, t, n, f float64) Matrix

func (Matrix) Inverse

func (a Matrix) Inverse() Matrix

func (Matrix) LookAt

func (m Matrix) LookAt(eye, center, up Vector) Matrix

func (Matrix) Mul

func (a Matrix) Mul(b Matrix) Matrix

func (Matrix) MulBox

func (a Matrix) MulBox(box Box) Box

func (Matrix) MulDirection

func (a Matrix) MulDirection(b Vector) Vector

func (Matrix) MulPosition

func (a Matrix) MulPosition(b Vector) Vector

func (Matrix) MulPositionW

func (a Matrix) MulPositionW(b Vector) VectorW

func (Matrix) MulScalar

func (a Matrix) MulScalar(b float64) Matrix

func (Matrix) Orthographic

func (m Matrix) Orthographic(l, r, b, t, n, f float64) Matrix

func (Matrix) Perspective

func (m Matrix) Perspective(fovy, aspect, near, far float64) Matrix

func (Matrix) Rotate

func (m Matrix) Rotate(v Vector, a float64) Matrix

func (Matrix) RotateTo

func (m Matrix) RotateTo(a, b Vector) Matrix

func (Matrix) Scale

func (m Matrix) Scale(v Vector) Matrix

func (Matrix) Translate

func (m Matrix) Translate(v Vector) Matrix

func (Matrix) Transpose

func (a Matrix) Transpose() Matrix

func (Matrix) Viewport

func (m Matrix) Viewport(x, y, w, h float64) Matrix

type Mesh

type Mesh struct {
	Triangles []*Triangle
	Lines     []*Line
	// contains filtered or unexported fields

func Load3DS

func Load3DS(filename string) (*Mesh, error)

func LoadMesh

func LoadMesh(path string) (*Mesh, error)

func LoadOBJ

func LoadOBJ(path string) (*Mesh, error)

func LoadPLY

func LoadPLY(path string) (*Mesh, error)

func LoadSTL

func LoadSTL(path string) (*Mesh, error)

func NewCone

func NewCone(step int, capped bool) *Mesh

func NewCube

func NewCube() *Mesh

func NewCubeForBox

func NewCubeForBox(box Box) *Mesh

func NewCubeOutlineForBox

func NewCubeOutlineForBox(box Box) *Mesh

func NewCylinder

func NewCylinder(step int, capped bool) *Mesh

func NewEmptyMesh

func NewEmptyMesh() *Mesh

func NewIcosahedron

func NewIcosahedron() *Mesh

func NewLatLngSphere

func NewLatLngSphere(latStep, lngStep int) *Mesh

func NewLineMesh

func NewLineMesh(lines []*Line) *Mesh

func NewMesh

func NewMesh(triangles []*Triangle, lines []*Line) *Mesh

func NewPlane

func NewPlane() *Mesh

func NewSphere

func NewSphere(detail int) *Mesh

func NewTriangleMesh

func NewTriangleMesh(triangles []*Triangle) *Mesh

func NewVoxelMesh

func NewVoxelMesh(voxels []Voxel) *Mesh

func (*Mesh) Add

func (a *Mesh) Add(b *Mesh)

func (*Mesh) BiUnitCube

func (m *Mesh) BiUnitCube() Matrix

func (*Mesh) BoundingBox

func (m *Mesh) BoundingBox() Box

func (*Mesh) Center

func (m *Mesh) Center() Matrix

func (*Mesh) Copy

func (m *Mesh) Copy() *Mesh

func (*Mesh) FitInside

func (m *Mesh) FitInside(box Box, anchor Vector) Matrix

func (*Mesh) MoveTo

func (m *Mesh) MoveTo(position, anchor Vector) Matrix

func (*Mesh) ReverseWinding

func (m *Mesh) ReverseWinding()

func (*Mesh) SaveSTL

func (m *Mesh) SaveSTL(path string) error

func (*Mesh) SetColor

func (m *Mesh) SetColor(c Color)

func (*Mesh) SharpEdges

func (m *Mesh) SharpEdges(angleThreshold float64) *Mesh

func (*Mesh) Silhouette

func (m *Mesh) Silhouette(eye Vector, offset float64) *Mesh

func (*Mesh) Simplify

func (m *Mesh) Simplify(factor float64)

func (*Mesh) SmoothNormals

func (m *Mesh) SmoothNormals()

func (*Mesh) SmoothNormalsThreshold

func (m *Mesh) SmoothNormalsThreshold(radians float64)

func (*Mesh) SplitTriangles

func (m *Mesh) SplitTriangles(maxEdgeLength float64)

func (*Mesh) SurfaceArea

func (m *Mesh) SurfaceArea() float64

func (*Mesh) Transform

func (m *Mesh) Transform(matrix Matrix)

func (*Mesh) UnitCube

func (m *Mesh) UnitCube() Matrix

func (*Mesh) Volume

func (m *Mesh) Volume() float64

type PhongShader

type PhongShader struct {
	Matrix         Matrix
	LightDirection Vector
	CameraPosition Vector
	ObjectColor    Color
	AmbientColor   Color
	DiffuseColor   Color
	SpecularColor  Color
	Texture        Texture
	SpecularPower  float64

PhongShader implements Phong shading with an optional texture.

func NewPhongShader

func NewPhongShader(matrix Matrix, lightDirection, cameraPosition Vector) *PhongShader

func (*PhongShader) Fragment

func (shader *PhongShader) Fragment(v Vertex) Color

func (*PhongShader) Vertex

func (shader *PhongShader) Vertex(v Vertex) Vertex

type RasterizeInfo

type RasterizeInfo struct {
	TotalPixels   uint64
	UpdatedPixels uint64

func (RasterizeInfo) Add

func (info RasterizeInfo) Add(other RasterizeInfo) RasterizeInfo

type STLHeader

type STLHeader struct {
	Count uint32
	// contains filtered or unexported fields

type STLTriangle

type STLTriangle struct {
	N, V1, V2, V3 [3]float32
	// contains filtered or unexported fields

type Shader

type Shader interface {
	Vertex(Vertex) Vertex
	Fragment(Vertex) Color

type SolidColorShader

type SolidColorShader struct {
	Matrix Matrix
	Color  Color

SolidColorShader renders with a single, solid color.

func NewSolidColorShader

func NewSolidColorShader(matrix Matrix, color Color) *SolidColorShader

func (*SolidColorShader) Fragment

func (shader *SolidColorShader) Fragment(v Vertex) Color

func (*SolidColorShader) Vertex

func (shader *SolidColorShader) Vertex(v Vertex) Vertex

type Texture

type Texture interface {
	Sample(u, v float64) Color
	BilinearSample(u, v float64) Color

func LoadTexture

func LoadTexture(path string) (Texture, error)

func NewImageTexture

func NewImageTexture(im image.Image) Texture

type TextureShader

type TextureShader struct {
	Matrix  Matrix
	Texture Texture

TextureShader renders with a texture and no lighting.

func NewTextureShader

func NewTextureShader(matrix Matrix, texture Texture) *TextureShader

func (*TextureShader) Fragment

func (shader *TextureShader) Fragment(v Vertex) Color

func (*TextureShader) Vertex

func (shader *TextureShader) Vertex(v Vertex) Vertex

type Triangle

type Triangle struct {
	V1, V2, V3 Vertex

func ClipTriangle

func ClipTriangle(t *Triangle) []*Triangle

func NewTriangle

func NewTriangle(v1, v2, v3 Vertex) *Triangle

func NewTriangleForPoints

func NewTriangleForPoints(p1, p2, p3 Vector) *Triangle

func (*Triangle) Area

func (t *Triangle) Area() float64

func (*Triangle) BoundingBox

func (t *Triangle) BoundingBox() Box

func (*Triangle) FixNormals

func (t *Triangle) FixNormals()

func (*Triangle) IsDegenerate

func (t *Triangle) IsDegenerate() bool

func (*Triangle) Normal

func (t *Triangle) Normal() Vector

func (*Triangle) ReverseWinding

func (t *Triangle) ReverseWinding()

func (*Triangle) SetColor

func (t *Triangle) SetColor(c Color)

func (*Triangle) Transform

func (t *Triangle) Transform(matrix Matrix)

type VOXChunk

type VOXChunk struct {
	ID            [4]byte
	ContentBytes  int32
	ChildrenBytes int32

type VOXHeader

type VOXHeader struct {
	Magic   [4]byte
	Version int32

type VOXVoxel

type VOXVoxel struct {
	X, Y, Z, I uint8

type Vector

type Vector struct {
	X, Y, Z float64

func InterpolateVectors

func InterpolateVectors(v1, v2, v3 Vector, b VectorW) Vector

func LatLngToXYZ

func LatLngToXYZ(lat, lng float64) Vector

func RandomUnitVector

func RandomUnitVector() Vector

func V

func V(x, y, z float64) Vector

func (Vector) Abs

func (a Vector) Abs() Vector

func (Vector) Add

func (a Vector) Add(b Vector) Vector

func (Vector) AddScalar

func (a Vector) AddScalar(b float64) Vector

func (Vector) Ceil

func (a Vector) Ceil() Vector

func (Vector) Cross

func (a Vector) Cross(b Vector) Vector

func (Vector) Distance

func (a Vector) Distance(b Vector) float64

func (Vector) DistanceSquared

func (a Vector) DistanceSquared(b Vector) float64

func (Vector) Div

func (a Vector) Div(b Vector) Vector

func (Vector) DivScalar

func (a Vector) DivScalar(b float64) Vector

func (Vector) Dot

func (a Vector) Dot(b Vector) float64

func (Vector) Floor

func (a Vector) Floor() Vector

func (Vector) IsDegenerate

func (a Vector) IsDegenerate() bool

func (Vector) Length

func (a Vector) Length() float64

func (Vector) LengthSquared

func (a Vector) LengthSquared() float64

func (Vector) Lerp

func (a Vector) Lerp(b Vector, t float64) Vector

func (Vector) LerpDistance

func (a Vector) LerpDistance(b Vector, d float64) Vector

func (Vector) Less

func (a Vector) Less(b Vector) bool

func (Vector) Max

func (a Vector) Max(b Vector) Vector

func (Vector) MaxComponent

func (a Vector) MaxComponent() float64

func (Vector) Min

func (a Vector) Min(b Vector) Vector

func (Vector) MinComponent

func (a Vector) MinComponent() float64

func (Vector) Mod

func (a Vector) Mod(b Vector) Vector

func (Vector) Mul

func (a Vector) Mul(b Vector) Vector

func (Vector) MulScalar

func (a Vector) MulScalar(b float64) Vector

func (Vector) Negate

func (a Vector) Negate() Vector

func (Vector) Normalize

func (a Vector) Normalize() Vector

func (Vector) Perpendicular

func (a Vector) Perpendicular() Vector

func (Vector) Reflect

func (i Vector) Reflect(n Vector) Vector

func (Vector) Round

func (a Vector) Round() Vector

func (Vector) RoundPlaces

func (a Vector) RoundPlaces(n int) Vector

func (Vector) SegmentDistance

func (p Vector) SegmentDistance(v Vector, w Vector) float64

func (Vector) Sub

func (a Vector) Sub(b Vector) Vector

func (Vector) SubScalar

func (a Vector) SubScalar(b float64) Vector

func (Vector) VectorW

func (a Vector) VectorW() VectorW

type VectorW

type VectorW struct {
	X, Y, Z, W float64

func Barycentric

func Barycentric(p1, p2, p3, p Vector) VectorW

func InterpolateVectorWs

func InterpolateVectorWs(v1, v2, v3, b VectorW) VectorW

func (VectorW) Add

func (a VectorW) Add(b VectorW) VectorW

func (VectorW) DivScalar

func (a VectorW) DivScalar(b float64) VectorW

func (VectorW) Dot

func (a VectorW) Dot(b VectorW) float64

func (VectorW) MulScalar

func (a VectorW) MulScalar(b float64) VectorW

func (VectorW) Outside

func (a VectorW) Outside() bool

func (VectorW) Sub

func (a VectorW) Sub(b VectorW) VectorW

func (VectorW) Vector

func (a VectorW) Vector() Vector

type Vertex

type Vertex struct {
	Position Vector
	Normal   Vector
	Texture  Vector
	Color    Color
	Output   VectorW

func InterpolateVertexes

func InterpolateVertexes(v1, v2, v3 Vertex, b VectorW) Vertex

func (Vertex) Outside

func (a Vertex) Outside() bool

type Voxel

type Voxel struct {
	X, Y, Z int
	Color   Color

func LoadVOX

func LoadVOX(path string) ([]Voxel, error)


Path Synopsis

Jump to

Keyboard shortcuts

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