glam

package module
v0.0.0-...-4cea4d0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2014 License: BSD-2-Clause Imports: 1 Imported by: 1

README

GLaM: OpenGL Mathematics for Go

Features

GLaM is a Go package providing mathematical types and operations for use with OpenGL. It is written with game development in mind, so the focus is on speed over portability or accuracy.

NOTE: this is a work in progress.

  • Vectors and matrices.
  • Efficient single-precision math.
  • Some noise functions (Perlin, Simplex...).

Package GLaM

import "github.com/Ferguzz/glam"

This package provides vectors and matrices, and their associated operations.

  • The names mirrors the GLSL types: Vec2, Vec3, Vec4, Mat3, Mat4, IVec3...
  • All types are pure values: there's no heap allocation, and no hidden data.
  • All types have the same memory layout than their corresponding GLSL types.
  • Most methods are inlined by the compiler.

Package GLaM Math

import "github.com/Ferguzz/glam/math"

This package aims to provide fast float32 math functions, using assembly when appropriate.

Package GLaM Noise

import "github.com/Ferguzz/glam/noise"

The Perlin and Simplex noise functions are adapted from "Simplex Noise Demystified" by Stefan Gustavson (code in the public domain).

Author

Laurent Moussault [email protected]

Documentation

Overview

Package GLaM provides mathematical types and operations for use with OpenGL.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IVec2

type IVec2 struct {
	X int32
	Y int32
}

`IVec2` is an integer vector with 2 components.

type IVec3

type IVec3 struct {
	X int32
	Y int32
	Z int32
}

`IVec3` is an integer vector with 3 components.

type IVec4

type IVec4 struct {
	X int32
	Y int32
	Z int32
	W int32
}

`IVec4` is an integer vector with 4 components.

type Mat4

type Mat4 [16]float32

`Mat4` is a single-precision matrix with 4 columns and 4 rows.

Note: matrices are stored in column-major order, so when writing literals remember to use the transpose.

func Identity

func Identity() Mat4

`Identity` returns a 4x4 Identity matrix.

func LookAt

func LookAt(eye, center, up Vec3) Mat4

`LookAt` returns a transform from world space into the specific eye space that the projective matrix functions (Perspective, OrthographicFrustum, ...) are designed to expect.

See also `Perspective` and `OrthographicFrustum`.

func MakeMat4

func MakeMat4(
	a, e, i, m,
	b, f, j, n,
	c, g, k, o,
	d, h, l, p float32,
) Mat4

`MakeMat4` returns a matrix. The elements are stored in alphabetical order (column-major order).

See also `NewMat4` and `SetTo`.

func NewMat4

func NewMat4(
	a, e, i, m,
	b, f, j, n,
	c, g, k, o,
	d, h, l, p float32,
) *Mat4

`NewMat4` allocates and returns a new matrix. The elements are stored in alphabetical order (column-major order).

See also `MakeMat4` and `SetTo`.

func Orthographic

func Orthographic(zoom, aspectRatio, near, far float32) Mat4

`Orthographic` returns an orthographic (parallel) projection matrix. `zoom` is the height of the projection plane.

See also `SetToOrthographic`, `OrthographicFrustum` and `SetToOrthographicFrustum`.

func OrthographicFrustum

func OrthographicFrustum(left, right, bottom, top, near, far float32) Mat4

`OrthographicFrustum` returns an orthographic (parallel) projection matrix.

See also `SetToOrthographicFrustum`, `Orthographic` and `SetToOrthographic`.

func Perspective

func Perspective(fieldOfView, aspectRatio, near, far float32) Mat4

`Perspective` returns a perspective projection matrix.

See also `SetToPerspective`, `PerspectiveFrustum` and `SetToPerspectiveFrustum`.

func PerspectiveFrustum

func PerspectiveFrustum(left, right, bottom, top, near, far float32) Mat4

`PerspectiveFrustum` returns a perspective projection matrix.

See also `SetToPerspectiveFrustum`, `Perspective` and `SetToPerspective`.

func Rotation

func Rotation(angle float32, axis Vec3) Mat4

`Rotation` returns a rotation matrix.

See also `SetToRotation`.

func Scale

func Scale(t Vec3) Mat4

func Translation

func Translation(t Vec3) Mat4

`Translation` returns a translation matrix.

See also `SetToTranslation`.

func Zeros

func Zeros() Mat4

`Zeros` returns a 4x4 zeroed matrix.

func (*Mat4) Multiply

func (r *Mat4) Multiply(m, o *Mat4)

`Multiply` sets `r` to the matrix product of `m` and `o`.

`r` must not be `m` or `o`.

See also `Multiply`, `TimesVec` and `MultiplyVec`.

func (*Mat4) Rotation

func (m *Mat4) Rotation(angle float32, axis Vec3)

`SetToRotation` sets `m` to a rotation matrix.

See also `Rotation`.

func (*Mat4) SetTo

func (matrix *Mat4) SetTo(
	a, e, i, m,
	b, f, j, n,
	c, g, k, o,
	d, h, l, p float32,
)

`SetTo` initializes `matrix`. The elements are stored in alphabetical order (column-major order).

See also `NewMat4` and `SetTo`.

func (*Mat4) SetToOrthographic

func (m *Mat4) SetToOrthographic(zoom, aspectRatio, near, far float32)

`SetToOrthographic` sets `m` to an orthographic (parallel) projection matrix. `zoom` is the height of the projection plane.

See also `Orthographic`, `OrthographicFrustum` and `SetToOrthographicFrustum`.

func (*Mat4) SetToOrthographicFrustum

func (m *Mat4) SetToOrthographicFrustum(left, right, bottom, top, near, far float32)

`SetToOrthographicFrustum` returns an orthographic (parallel) projection matrix.

See also `OrthographicFrustum`, `Orthographic` and `SetToOrthographic`.

func (*Mat4) SetToPerspective

func (m *Mat4) SetToPerspective(fieldOfView, aspectRatio, near, far float32)

`SetToPerspective` sets `m` to a perspective projection matrix.

See also `Perspective`, `PerspectiveFrustum` and `SetToPerspectiveFrustum`.

func (*Mat4) SetToPerspectiveFrustum

func (m *Mat4) SetToPerspectiveFrustum(left, right, bottom, top, near, far float32)

`SetToPerspectiveFrustum` sets `m` to a perspective projection matrix.

See also `PerspectiveFrustum`, `Perspective` and `SetToPerspective`.

func (*Mat4) SetToTranslation

func (m *Mat4) SetToTranslation(t Vec3)

`SetToTranslation` sets `m` to a translation matrix.

See also `Translation`.

func (*Mat4) Times

func (m *Mat4) Times(o *Mat4) Mat4

`Times` returns the matrix product of `m` and `o`.

See also `Multiply`, `TimesVec` and `MultiplyVec`.

type Vec2

type Vec2 struct {
	X float32
	Y float32
}

`Vec2` is a single-precision vector with 2 components..

func (*Vec2) Add

func (a *Vec2) Add(b Vec2)

`Add` sets `a` to the sum `a + b`.

More efficient than `Plus`.

func (*Vec2) Divide

func (a *Vec2) Divide(s float32)

`Divide` sets `a` to the division of `a` by the scalar `s`. `s` must be non-zero.

More efficient than `Slash`.

func (Vec2) Dot

func (a Vec2) Dot(b Vec2) float32

`Dot` returns the dot product of `a` and `b`.

func (Vec2) Homogenized

func (a Vec2) Homogenized() Vec3

`Homogenized` returns the homogeneous coordinates of `a`.

func (Vec2) HomogenizedAsDirection

func (a Vec2) HomogenizedAsDirection() Vec3

`HomogenizedAsDirection` returns the homogeneous coordinates of a point at infinity in the direction of `a`.

func (Vec2) Inverse

func (a Vec2) Inverse() Vec2

`Inverse` return the inverse of `a`.

See also `Invert`.

func (*Vec2) Invert

func (a *Vec2) Invert()

`Invert` sets `a` to its inverse.

More efficient than `Inverse`.

func (Vec2) Length

func (a Vec2) Length() float32

`Length` returns `|a|` (the euclidian length of `a`).

func (Vec2) Minus

func (a Vec2) Minus(b Vec2) Vec2

`Minus` returns the difference `a - b`.

See also `Subtract`.

func (*Vec2) Multiply

func (a *Vec2) Multiply(s float32)

`Multiply` sets `a` to the product of `a` with the scalar `s`.

More efficient than `Times`.

func (*Vec2) Normalize

func (a *Vec2) Normalize()

`Normalize` sets `a` to `a/|a|` (i.e. normalizes `a`). `a` must be non-zero.

More efficitent than `Normalized`.

func (Vec2) Normalized

func (a Vec2) Normalized() Vec2

`Normalized` return `a/|a|` (i.e. the normalization of `a`). `a` must be non-zero.

See also `Normalize`.

func (Vec2) Plus

func (a Vec2) Plus(b Vec2) Vec2

`Plus` returns the sum `a + b`.

See also `Add`.

func (Vec2) Slash

func (a Vec2) Slash(s float32) Vec2

`Slash` returns the division of `a` by the scalar `s`. `s` must be non-zero.

See also `Divide`.

func (*Vec2) Subtract

func (a *Vec2) Subtract(b Vec2)

`Subtract` sets `a` to the difference `a - b`.

More efficient than `Minus`.

func (Vec2) Times

func (a Vec2) Times(s float32) Vec2

`Times` returns the product of `a` with the scalar `s`.

See also `Multiply`.

type Vec3

type Vec3 struct {
	X float32
	Y float32
	Z float32
}

`Vec3` is a single-precision vector with 3 components.

Example
var a Vec3
b := Vec3{1.1, 2.2, 3.3}
c := b.Plus(Vec3{4.4, 5.5, 6.6})
d := b
d.Add(Vec3{4.4, 5.5, 6.6})
e := b.Slash(2.2)
f := e.Homogenized()
g := b
g.Normalize()

fmt.Printf("a == %#v\n", a)
fmt.Printf("b == %#v\n", b)
fmt.Printf("c == %#v\n", c)
fmt.Printf("d == %#v\n", d)
fmt.Printf("e == %#v\n", e)
fmt.Printf("f == %#v\n", f)
fmt.Printf("g == %#v\n", g)
Output:

a == glam.Vec3{X:0, Y:0, Z:0}
b == glam.Vec3{X:1.1, Y:2.2, Z:3.3}
c == glam.Vec3{X:5.5, Y:7.7, Z:9.9}
d == glam.Vec3{X:5.5, Y:7.7, Z:9.9}
e == glam.Vec3{X:0.5, Y:1, Z:1.5}
f == glam.Vec4{X:0.5, Y:1, Z:1.5, W:1}
g == glam.Vec3{X:0.26726127, Y:0.53452253, Z:0.8017838}

func (*Vec3) Add

func (a *Vec3) Add(b Vec3)

`Add` sets `a` to the sum `a + b`.

More efficient than `Plus`.

func (Vec3) Cross

func (a Vec3) Cross(b Vec3) Vec3

`Cross` returns the cross product of `a` and `b`.

func (Vec3) Dehomogenized

func (a Vec3) Dehomogenized() Vec2

`Returns` the dehomogenization of `a` (perspective divide). `a.Z` must be non-zero.

func (*Vec3) Divide

func (a *Vec3) Divide(s float32)

`Divide` sets `a` to the division of `a` by the scalar `s`. `s` must be non-zero.

More efficient than `Slash`.

func (Vec3) Dot

func (a Vec3) Dot(b Vec3) float32

`Dot` returns the dot product of `a` and `b`.

func (Vec3) Homogenized

func (a Vec3) Homogenized() Vec4

`Homogenized` returns the homogeneous coordinates of `a`.

func (Vec3) HomogenizedAsDirection

func (a Vec3) HomogenizedAsDirection() Vec4

`HomogenizedAsDirection` returns the homogeneous coordinates of a point at infinity in the direction of `a`.

func (Vec3) Inverse

func (a Vec3) Inverse() Vec3

`Inverse` return the inverse of `a`.

See also `Invert`.

func (*Vec3) Invert

func (a *Vec3) Invert()

`Invert` sets `a` to its inverse.

More efficient than `Inverse`.

func (Vec3) Length

func (a Vec3) Length() float32

`Length` returns `|a|` (the euclidian length of `a`).

func (Vec3) Minus

func (a Vec3) Minus(b Vec3) Vec3

`Minus` returns the difference `a - b`.

See also `Subtract`.

func (*Vec3) Multiply

func (a *Vec3) Multiply(s float32)

`Multiply` sets `a` to the product of `a` with the scalar `s`. More efficient than `Times`.

func (*Vec3) Normalize

func (a *Vec3) Normalize()

`Normalize` sets `a` to `a/|a|` (i.e. normalizes `a`). `a` must be non-zero.

More efficitent than `Normalized`.

func (Vec3) Normalized

func (a Vec3) Normalized() Vec3

`Normalized` return `a/|a|` (i.e. the normalization of `a`). `a` must be non-zero.

See also `Normalize`.

func (Vec3) Plus

func (a Vec3) Plus(b Vec3) Vec3

`Plus` returns the sum `a + b`.

See also `Add`.

func (Vec3) Slash

func (a Vec3) Slash(s float32) Vec3

`Slash` returns the division of `a` by the scalar `s`. `s` must be non-zero.

See also `Divide`.

func (*Vec3) Subtract

func (a *Vec3) Subtract(b Vec3)

`Subtract` sets `a` to the difference `a - b`. More efficient than `Minus`.

func (Vec3) Times

func (a Vec3) Times(s float32) Vec3

`Times` returns the product of `a` with the scalar `s`.

See also `Multiply`.

type Vec4

type Vec4 struct {
	X float32
	Y float32
	Z float32
	W float32
}

`Vec4` is single-precision vector with 4 components.

Example
var a Vec4
b := Vec4{1.1, 2.2, 3.3, 4.4}
c := b.Plus(Vec4{5.5, 6.6, 7.7, 8.8})
d := b
d.Add(Vec4{5.5, 6.6, 7.7, 8.8})
e := b.Slash(2.2)
f := e.Dehomogenized()
g := b
g.Normalize()

fmt.Printf("a == %#v\n", a)
fmt.Printf("b == %#v\n", b)
fmt.Printf("c == %#v\n", c)
fmt.Printf("d == %#v\n", d)
fmt.Printf("e == %#v\n", e)
fmt.Printf("f == %#v\n", f)
fmt.Printf("g == %#v\n", g)
Output:

a == glam.Vec4{X:0, Y:0, Z:0, W:0}
b == glam.Vec4{X:1.1, Y:2.2, Z:3.3, W:4.4}
c == glam.Vec4{X:6.6, Y:8.8, Z:11, W:13.200001}
d == glam.Vec4{X:6.6, Y:8.8, Z:11, W:13.200001}
e == glam.Vec4{X:0.5, Y:1, Z:1.5, W:2}
f == glam.Vec3{X:0.25, Y:0.5, Z:0.75}
g == glam.Vec4{X:0.18257418, Y:0.36514837, Z:0.5477226, W:0.73029673}

func (*Vec4) Add

func (a *Vec4) Add(b Vec4)

`Add` sets `a` to the sum `a + b`.

More efficient than `Plus`.

func (Vec4) Dehomogenized

func (a Vec4) Dehomogenized() Vec3

`Dehomogenized` returns the dehomogenization of `a` (perspective divide). `a.W` must be non-zero.

func (*Vec4) Divide

func (a *Vec4) Divide(s float32)

`Divide` sets `a` to the division of `a` by the scalar `s`. `s` must be non-zero.

More efficient than `Slash`.

func (Vec4) Dot

func (a Vec4) Dot(b Vec4) float32

`Dot` returns the dot product of `a` and `b`.

func (Vec4) Inverse

func (a Vec4) Inverse() Vec4

`Inverse` return the inverse of `a`.

See also `Invert`.

func (*Vec4) Invert

func (a *Vec4) Invert()

`Invert` sets `a` to its inverse. More efficient than `Inverse`.

func (Vec4) Length

func (a Vec4) Length() float32

`Length` returns `|a|` (the euclidian length of `a`).

func (Vec4) Minus

func (a Vec4) Minus(b Vec4) Vec4

`Minus` returns the difference `a - b`.

See also `Subtract`.

func (*Vec4) Multiply

func (a *Vec4) Multiply(s float32)

`Multiply` sets `a` to the product of `a` with the scalar `s`. More efficient than `Times`.

func (*Vec4) Normalize

func (a *Vec4) Normalize()

`Normalize` sets `a` to `a/|a|` (i.e. normalizes `a`). `a` must be non-zero.

More efficitent than `Normalized`.

func (Vec4) Normalized

func (a Vec4) Normalized() Vec4

`Normalized` return `a/|a|` (i.e. the normalization of `a`). `a` must be non-zero.

See also `Normalize`.

func (Vec4) Plus

func (a Vec4) Plus(b Vec4) Vec4

`Plus` returns the sum `a + b`.

See also `Add`.

func (Vec4) Slash

func (a Vec4) Slash(s float32) Vec4

`Slash` returns the division of `a` by the scalar `s`. `s` must be non-zero.

See also `Divide`.

func (*Vec4) Subtract

func (a *Vec4) Subtract(b Vec4)

`Subtract` sets `a` to the difference `a - b`. More efficient than `Minus`.

func (Vec4) Times

func (a Vec4) Times(s float32) Vec4

`Times` returns the product of `a` with the scalar `s`.

See also `Multiply`.

Directories

Path Synopsis
Package GLaM Math provides fast float32 math functions.
Package GLaM Math provides fast float32 math functions.
Package GLaM Noise provides pseudo-random and noise functions.
Package GLaM Noise provides pseudo-random and noise functions.

Jump to

Keyboard shortcuts

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