geom

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2018 License: BSD-3-Clause Imports: 3 Imported by: 4

README

Package geom provides vector and matrix types suitable for OpenGL
programming in Go: Vec2, Vec3 and Mat4. 

Documentation:
  http://godoc.org/github.com/fzipp/geom

Documentation

Overview

Package geom provides vector and matrix types suitable for OpenGL programming: Vec2, Vec3 and Mat4.

Using vectors:

v := geom.V3(2, 1.5, 0.5)
w := geom.V3(-1, 0, 1)
fmt.Println("3*(v+w) =", v.Add(w).Mul(3))

Using matrices:

// A 4x4 zero matrix
var a geom.Mat4

// Another 4x4 matrix
b := geom.Mat4{
	{0, 1, 2.3, 3},
	{4, 0.5, 6, 7},
	{8, -9, 10, 11},
	{12, 13, 14, 15},
}

// Copy elements from b to a
a = b

// Set a matrix element
a[2][3] = 5

// Multiply a and b, store the result in a.
a.Mul(&a, &b)

Index

Constants

This section is empty.

Variables

View Source
var (
	// V2Zero is the zero vector (0,0).
	V2Zero = Vec2{0, 0}
	// V2Unit is the unit vector (1,1).
	V2Unit = Vec2{1, 1}
	// V2UnitX is the x-axis unit vector (1,0).
	V2UnitX = Vec2{1, 0}
	// V2UnitY is the y-axis unit vector (0,1).
	V2UnitY = Vec2{0, 1}
)
View Source
var (
	// V3Zero is the zero vector (0,0,0).
	V3Zero = Vec3{0, 0, 0}
	// V3Unit is the unit vector (1,1,1).
	V3Unit = Vec3{1, 1, 1}
	// V3UnitX is the x-axis unit vector (1,0,0).
	V3UnitX = Vec3{1, 0, 0}
	// V3UnitY is the y-axis unit vector (0,1,0).
	V3UnitY = Vec3{0, 1, 0}
	// V3UnitZ is the z-axis unit vector (0,0,1).
	V3UnitZ = Vec3{0, 0, 1}
)

Functions

func Deg

func Deg(rad float32) float32

Deg converts the measurement of an angle from radians to degrees.

func Rad

func Rad(deg float32) float32

Rad converts the measurement of an angle from degrees to radians.

Types

type Mat4

type Mat4 [4][4]float32

A Mat4 represents a 4x4 matrix. The indices are [row][column].

func (*Mat4) Det

func (m *Mat4) Det() float32

Det calculates the determinant of 4x4 matrix m.

func (*Mat4) Floats

func (m *Mat4) Floats() *[16]float32

Floats returns a pointer to the matrix elements represented as a flat array of float32 numbers in row-major order. Changing an element value of this array will affect m and vice versa.

func (*Mat4) Frustum

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

Frustum sets m to a frustum matrix with the given clipping planes and returns m.

func (*Mat4) ID

func (m *Mat4) ID() *Mat4

ID sets m to the identity matrix and returns m.

func (*Mat4) LookAt

func (m *Mat4) LookAt(eye, center, up Vec3) *Mat4

LookAt sets m to a viewing matrix given an eye point, a reference point indicating the center of the scene and an up vector, and returns m.

func (*Mat4) Mul

func (m *Mat4) Mul(a *Mat4, b *Mat4) *Mat4

Mul sets m to the matrix product a*b and returns m.

func (*Mat4) Ortho

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

Ortho sets m to an orthographic projection matrix with the given clipping planes and returns m.

func (*Mat4) Perspective

func (m *Mat4) Perspective(fovy, aspect, near, far float32) *Mat4

Perspective sets m to a perspective matrix with the given vertical field of view angle (in radians), aspect ratio, near and far bounds of the frustum, and returns m.

func (*Mat4) Rot

func (m *Mat4) Rot(a *Mat4, angle float32, axis Vec3) *Mat4

Rot sets m to the rotation of matrix a by the given angle in radians around the given axis, and returns m.

func (*Mat4) Scale

func (m *Mat4) Scale(a *Mat4, v Vec3) *Mat4

Scale sets m to the scaling of matrix a by the scale factors of v and returns m.

func (*Mat4) T

func (m *Mat4) T(a *Mat4) *Mat4

T sets m to the transpose of matrix a and returns m.

func (*Mat4) Translate

func (m *Mat4) Translate(a *Mat4, v Vec3) *Mat4

Translate sets m to the translation of matrix a by the vector v and returns m.

func (*Mat4) Zero

func (m *Mat4) Zero() *Mat4

Zero sets all elements of m to 0 (zero matrix) and returns m.

type Size

type Size struct {
	// Width and height
	W, H float32
}

A Size represents the dimensions of a rectangle.

type Vec2

type Vec2 struct {
	X, Y float32
}

A Vec2 represents a vector with coordinates X and Y in 2-dimensional euclidian space.

func V2

func V2(x, y float32) Vec2

V2 is shorthand for Vec2{X: x, Y: y}.

func (Vec2) Add

func (v Vec2) Add(w Vec2) Vec2

Add returns the vector v+w.

func (Vec2) CompDiv

func (v Vec2) CompDiv(w Vec2) Vec2

CompDiv returns the component-wise division of two vectors.

func (Vec2) CompMul

func (v Vec2) CompMul(w Vec2) Vec2

CompMul returns the component-wise multiplication of two vectors.

func (Vec2) CrossLen

func (v Vec2) CrossLen(w Vec2) float32

CrossLen returns the length that the cross product of v and w would have in 3-dimensional euclidian space. This is effectively the Z component of the 3D cross product vector.

func (Vec2) Dist

func (v Vec2) Dist(w Vec2) float32

Dist returns the euclidian distance between two vectors.

func (Vec2) Div

func (v Vec2) Div(s float32) Vec2

Div returns the vector v/s.

func (Vec2) Dot

func (v Vec2) Dot(w Vec2) float32

Dot returns the dot (a.k.a. scalar) product of v and w.

func (Vec2) Len

func (v Vec2) Len() float32

Len returns the length (euclidian norm) of a vector.

func (Vec2) Lerp

func (v Vec2) Lerp(w Vec2, t float32) Vec2

Lerp returns the linear interpolation between v and w by amount t. The amount t is usually a value between 0 and 1. If t=0 v will be returned; if t=1 w will be returned.

func (Vec2) Max

func (v Vec2) Max(w Vec2) Vec2

Max returns a vector with each component set to the greater value of the corresponding component pair of v and w.

func (Vec2) Min

func (v Vec2) Min(w Vec2) Vec2

Min returns a vector with each component set to the lesser value of the corresponding component pair of v and w.

func (Vec2) Mul

func (v Vec2) Mul(s float32) Vec2

Mul returns the vector v*s.

func (Vec2) NearEq

func (v Vec2) NearEq(w Vec2) bool

NearEq returns whether v and w are approximately equal. This relation is not transitive in general. The tolerance for the floating-point components is ±1e-5.

func (Vec2) Neg

func (v Vec2) Neg() Vec2

Neg returns the negated vector of v.

func (Vec2) Norm

func (v Vec2) Norm() Vec2

Norm returns the normalized vector of a vector.

func (Vec2) Reflect

func (v Vec2) Reflect(n Vec2) Vec2

Reflect returns the reflection vector of v given a normal n.

func (Vec2) SqDist

func (v Vec2) SqDist(w Vec2) float32

SqDist returns the square of the euclidian distance between two vectors.

func (Vec2) SqLen

func (v Vec2) SqLen() float32

SqLen returns the square of the length (euclidian norm) of a vector.

func (Vec2) String

func (v Vec2) String() string

String returns a string representation of v like "(3.25, -1.5)".

func (Vec2) Sub

func (v Vec2) Sub(w Vec2) Vec2

Sub returns the vector v-w.

func (Vec2) Transform

func (v Vec2) Transform(m *Mat4) Vec2

Transform transforms vector v with 4x4 matrix m.

func (Vec2) Z

func (v Vec2) Z(z float32) Vec3

Z returns a Vec3 based on v with the additional coordinate z.

type Vec3

type Vec3 struct {
	X, Y, Z float32
}

A Vec3 represents a vector with coordinates X, Y and Z in 3-dimensional euclidian space.

func V3

func V3(x, y, z float32) Vec3

V3 is shorthand for Vec3{X: x, Y: y, Z: z}.

func (Vec3) Add

func (v Vec3) Add(w Vec3) Vec3

Add returns the vector v+w.

func (Vec3) CompDiv

func (v Vec3) CompDiv(w Vec3) Vec3

CompDiv returns the component-wise division of two vectors.

func (Vec3) CompMul

func (v Vec3) CompMul(w Vec3) Vec3

CompMul returns the component-wise multiplication of two vectors.

func (Vec3) Cross

func (v Vec3) Cross(w Vec3) Vec3

Cross returns the cross product of v and w.

func (Vec3) Dist

func (v Vec3) Dist(w Vec3) float32

Dist returns the euclidian distance between two vectors.

func (Vec3) Div

func (v Vec3) Div(s float32) Vec3

Div returns the vector v/s.

func (Vec3) Dot

func (v Vec3) Dot(w Vec3) float32

Dot returns the dot (a.k.a. scalar) product of v and w.

func (Vec3) Len

func (v Vec3) Len() float32

Len returns the length (euclidian norm) of a vector.

func (Vec3) Lerp

func (v Vec3) Lerp(w Vec3, t float32) Vec3

Lerp returns the linear interpolation between v and w by amount t. The amount t is usually a value between 0 and 1. If t=0 v will be returned; if t=1 w will be returned.

func (Vec3) Max

func (v Vec3) Max(w Vec3) Vec3

Max returns a vector with each component set to the greater value of the corresponding component pair of v and w.

func (Vec3) Min

func (v Vec3) Min(w Vec3) Vec3

Min returns a vector with each component set to the lesser value of the corresponding component pair of v and w.

func (Vec3) Mul

func (v Vec3) Mul(s float32) Vec3

Mul returns the vector v*s.

func (Vec3) NearEq

func (v Vec3) NearEq(w Vec3) bool

NearEq returns whether v and w are approximately equal. This relation is not transitive in general. The tolerance for the floating-point components is ±1e-5.

func (Vec3) Neg

func (v Vec3) Neg() Vec3

Neg returns the negated vector of v.

func (Vec3) Norm

func (v Vec3) Norm() Vec3

Norm returns the normalized vector of a vector.

func (Vec3) Reflect

func (v Vec3) Reflect(n Vec3) Vec3

Reflect returns the reflection vector of v given a normal n.

func (Vec3) SqDist

func (v Vec3) SqDist(w Vec3) float32

SqDist returns the square of the euclidian distance between two vectors.

func (Vec3) SqLen

func (v Vec3) SqLen() float32

SqLen returns the square of the length (euclidian norm) of a vector.

func (Vec3) String

func (v Vec3) String() string

String returns a string representation of v like "(3.25, -1.5, 1.2)".

func (Vec3) Sub

func (v Vec3) Sub(w Vec3) Vec3

Sub returns the vector v-w.

func (Vec3) Transform

func (v Vec3) Transform(m *Mat4) Vec3

Transform transforms vector v with 4x4 matrix m.

Jump to

Keyboard shortcuts

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