d3dmath

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2025 License: MIT Imports: 2 Imported by: 1

README

Package d3dmath provides vector and matrix functions similar to those in D3DX, a helper library for doing 3D graphics maths.

It is designed to work well with d3d9.

Matrices are layed out in row-major order, meaning a matrix defined as:

Mat4 {
	 1,  2,  3,  4,
	 5,  6,  7,  8,
	 9, 10, 11, 12,
	13, 14, 15, 16,
}

represents the matrix you would expect by reading the source code, i.e.:

 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15 16

Vectors are row-vectors, so the vector:

Vec3{1, 2, 3}

is the vector:

1 2 3

and not the vector:

1
2    <- not this
3

Functions that work with view transformations, like LookAt or Perspective assume a left-handed coordinate system.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mat2

type Mat2 [4]float32

Mat2 is a 2 by 2 matrix of float32s in row-major order.

func Identity2

func Identity2() Mat2

Identity2 returns the 2 by 2 identity matrix.

func Mul2

func Mul2(m0 Mat2, m ...Mat2) Mat2

Mul2 returns the product of the given matrices.

func (Mat2) Add

func (m Mat2) Add(n Mat2) Mat2

Add returns the sum of m + n.

func (Mat2) Homogeneous

func (m Mat2) Homogeneous() Mat3

Homogeneous returns the homogeneous 3-dimensional equivalent of the 2-dimensional matrix.

func (Mat2) Mul

func (m Mat2) Mul(n Mat2) Mat2

Mul returns the product of m * n.

func (Mat2) String

func (m Mat2) String() string

func (Mat2) Sub

func (m Mat2) Sub(n Mat2) Mat2

Sub returns the difference of m - n.

func (Mat2) Transposed added in v1.1.0

func (m Mat2) Transposed() Mat2

Transposed returns a transposed copy of m.

type Mat2x3

type Mat2x3 [6]float32

Mat2x3 is a 2x3 matrix of float32s in row-major order. It represents a homogeneous 3x3 matrix where the last line is 0,0,1 implicitly.

func Identity2x3

func Identity2x3() Mat2x3

Identity2x3 returns the 2 by 3 homogeneous identity matrix.

func Mul2x3

func Mul2x3(m0 Mat2x3, m ...Mat2x3) Mat2x3

Mul2x3 returns the product of the given matrices.

func (Mat2x3) Add

func (m Mat2x3) Add(n Mat2x3) (sum Mat2x3)

Add returns the sum of m + n.

func (Mat2x3) Mul

func (m Mat2x3) Mul(n Mat2x3) Mat2x3

Mul returns the product of m * n.

func (Mat2x3) String

func (m Mat2x3) String() string

func (Mat2x3) Sub

func (m Mat2x3) Sub(n Mat2x3) (diff Mat2x3)

Sub returns the difference of m - n.

func (Mat2x3) ToMat3

func (m Mat2x3) ToMat3() Mat3

ToMat3 returns the 3 by 3 representation of m.

type Mat3

type Mat3 [9]float32

Mat3 is a 3 by 3 matrix of float32s in row-major order.

func Identity3

func Identity3() Mat3

Identity3 returns the 3 by 3 identity matrix.

func Mul3

func Mul3(m0 Mat3, m ...Mat3) Mat3

Mul3 returns the product of the given matrices.

func (Mat3) Add

func (m Mat3) Add(n Mat3) (sum Mat3)

Add returns the sum of m + n.

func (Mat3) Homogeneous

func (m Mat3) Homogeneous() Mat4

Homogeneous returns the homogeneous 4-dimensional equivalent of the 3-dimensional matrix.

func (Mat3) Mul

func (m Mat3) Mul(n Mat3) Mat3

Mul returns the product of m * n.

func (Mat3) String

func (m Mat3) String() string

func (Mat3) Sub

func (m Mat3) Sub(n Mat3) (diff Mat3)

Sub returns the difference of m - n.

func (Mat3) Transposed added in v1.1.0

func (m Mat3) Transposed() Mat3

Transposed returns a transposed copy of m.

type Mat4

type Mat4 [16]float32

Mat4 is a 4 by 4 matrix of float32s in row-major order.

func DecomposeAffineTransform

func DecomposeAffineTransform(m Mat4) (scale, rotation, translation Mat4)

DecomposeAffineTransform decomposes the given matrix into scale, rotation and translation matrices that, when multiplied in that order, produce the original matrix. See this forum post for reference: https://math.stackexchange.com/questions/237369/given-this-transformation-matrix-how-do-i-decompose-it-into-translation-rotati

func Identity4

func Identity4() Mat4

Identity4 returns the 4 by 4 identity matrix.

func LookAt

func LookAt(pos, target, up Vec3) Mat4

LookAt returns a matrix that, when used for the camera, looks at target from position pos. Since you can tilt your head in infinite ways looking from one point at another, the up vector is used to specify which direction is up.

func Mul4

func Mul4(m0 Mat4, m ...Mat4) Mat4

Mul4 returns the product of the given matrices.

func Ortho

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

Ortho returns an orthographic projection matrix.

func Perspective

func Perspective(fovRadians, aspect, near, far float32) Mat4

Perspective returns an perspective projection matrix.

func RotateAbout

func RotateAbout(v Vec3, radians float32) Mat4

RotateAbout reutrns 4 by 4 matrix that, when multiplied with a homogeneous 4-element 3D vector, rotates the vector about the given vector v by the given angle in radians.

func RotateX

func RotateX(radians float32) Mat4

RotateX reutrns 4 by 4 matrix that, when multiplied with a homogeneous 4-element 3D vector, rotates the vector about the x-axis by the given angle in radians.

func RotateY

func RotateY(radians float32) Mat4

RotateY reutrns 4 by 4 matrix that, when multiplied with a homogeneous 4-element 3D vector, rotates the vector about the y-axis by the given angle in radians.

func RotateZ

func RotateZ(radians float32) Mat4

RotateZ reutrns 4 by 4 matrix that, when multiplied with a homogeneous 4-element 3D vector, rotates the vector about the z-axis by the given angle in radians.

func Scale

func Scale(dx, dy, dz float32) Mat4

Scale reutrns 4 by 4 matrix that, when multiplied with a homogeneous 4-element 3D vector, scales the vector by the given factors in x, y and z.

func ScaleV

func ScaleV(v Vec3) Mat4

ScaleV is the same as Scale, but it takes a Vec3 as its argument instead of single x, y, z parameters.

func Translate

func Translate(dx, dy, dz float32) Mat4

Translate reutrns 4 by 4 matrix that, when multiplied with a homogeneous 4-element 3D vector, moves the vector by the given amounts in x, y and z.

func TranslateV

func TranslateV(v Vec3) Mat4

TranslateV is the same as Translate, but it takes a Vec3 as its argument instead of single x, y, z parameters.

func (Mat4) Add

func (m Mat4) Add(n Mat4) (sum Mat4)

Add returns the sum of m + n.

func (Mat4) Mul

func (m Mat4) Mul(n Mat4) Mat4

Mul returns the product of m * n.

func (Mat4) String

func (m Mat4) String() string

func (Mat4) Sub

func (m Mat4) Sub(n Mat4) (diff Mat4)

Sub returns the difference of m - n.

func (Mat4) Transposed

func (m Mat4) Transposed() Mat4

Transposed returns a transposed copy of m.

type Vec2

type Vec2 [2]float32

Vec2 is a 2-element row vector. Elements are called x, y in the docs.

func AddVec2 added in v1.1.0

func AddVec2(v0 Vec2, v ...Vec2) Vec2

AddVec2 returns the sum of all given vectors.

func (Vec2) Add

func (v Vec2) Add(w Vec2) Vec2

Add returns the sum of v + w.

func (Vec2) Dot

func (v Vec2) Dot(w Vec2) float32

Dot returns the dot-product of v and w.

func (Vec2) Homogeneous

func (v Vec2) Homogeneous() Vec3

Homogeneous returns a 3-element vector where x and y are the same as in v and z is 1.

func (Vec2) MulMat

func (v Vec2) MulMat(m Mat2) Vec2

MulMat returns the product of row vector v and matrix m.

func (Vec2) MulScalar

func (v Vec2) MulScalar(s float32) Vec2

MulScalar returns a vector with all elements of v scaled by s.

func (Vec2) Negate

func (v Vec2) Negate() Vec2

Negate returns a vector with all elements of v negated.

func (Vec2) Norm

func (v Vec2) Norm() float32

Norm returns the length of v.

func (Vec2) Normalized

func (v Vec2) Normalized() Vec2

Normalized returns a copy of v with elements normalized so the returned vector has length 1.

func (Vec2) SquareNorm

func (v Vec2) SquareNorm() float32

SquareNorm returns the square of the length of v.

func (Vec2) String

func (v Vec2) String() string

func (Vec2) Sub

func (v Vec2) Sub(w Vec2) Vec2

Sub returns the difference of v - w.

type Vec3

type Vec3 [3]float32

Vec3 is a 3-element row vector. Elements are called x, y, z in the docs.

func AddVec3

func AddVec3(v0 Vec3, v ...Vec3) Vec3

AddVec3 returns the sum of all given vectors.

func (Vec3) Add

func (v Vec3) Add(w Vec3) Vec3

Add returns the sum of v + w.

func (Vec3) ByZ added in v1.1.0

func (v Vec3) ByZ() Vec2

ByW returns a 2-element vector where x and y are the same as in v but divided by z. This can be useful when going back from a homogeneous 3-element vector with z != 1, down one dimension to a 2-element vector.

func (Vec3) Cross

func (v Vec3) Cross(w Vec3) Vec3

Cross returns the cross-product of v and w.

func (Vec3) Dot

func (v Vec3) Dot(w Vec3) float32

Dot returns the dot-product of v and w.

func (Vec3) DropZ added in v1.1.0

func (v Vec3) DropZ() Vec2

DropZ returns a 2-element vector where x and y are the same as in v. This can be useful when going back from a homogeneous 3-element vector with z == 1, down one dimension to a 2-element vector. If z != 1 then use ByZ() to divide by z instead.

func (Vec3) Homogeneous

func (v Vec3) Homogeneous() Vec4

Homogeneous returns a 4-element vector where x, y and z are the same as in v and w is 1.

func (Vec3) MulMat

func (v Vec3) MulMat(m Mat3) Vec3

MulMat returns the product of row vector v and matrix m.

func (Vec3) MulScalar

func (v Vec3) MulScalar(s float32) Vec3

MulScalar returns a vector with all elements of v scaled by s.

func (Vec3) Negate

func (v Vec3) Negate() Vec3

Negate returns a vector with all elements of v negated.

func (Vec3) Norm

func (v Vec3) Norm() float32

Norm returns the length of v.

func (Vec3) Normalized

func (v Vec3) Normalized() Vec3

Normalized returns a copy of v with elements normalized so the returned vector has length 1.

func (Vec3) SquareNorm

func (v Vec3) SquareNorm() float32

SquareNorm returns the square of the length of v.

func (Vec3) String

func (v Vec3) String() string

func (Vec3) Sub

func (v Vec3) Sub(w Vec3) Vec3

Sub returns the difference of v - w.

type Vec4

type Vec4 [4]float32

Vec4 is a 4-element row vector. Elements are called x, y, z, w in the docs.

func AddVec4 added in v1.1.0

func AddVec4(v0 Vec4, v ...Vec4) Vec4

AddVec4 returns the sum of all given vectors.

func (Vec4) Add

func (v Vec4) Add(w Vec4) Vec4

Add returns the sum of v + w.

func (Vec4) ByW added in v1.1.0

func (v Vec4) ByW() Vec3

ByW returns a 3-element vector where x, y and z are the same as in v but divided by w. This can be useful when going back from a homogeneous 4-element vector with w != 1, down one dimension to a 3-element vector.

func (Vec4) Dot

func (v Vec4) Dot(w Vec4) float32

Dot returns the dot-product of v and w.

func (Vec4) DropW

func (v Vec4) DropW() Vec3

DropW returns a 3-element vector where x, y and z are the same as in v. This can be useful when going back from a homogeneous 4-element vector with w == 1, down one dimension to a 3-element vector. If w != 1 then use ByW() to divide by w instead.

func (Vec4) MulMat

func (v Vec4) MulMat(m Mat4) Vec4

MulMat returns the product of row vector v and matrix m.

func (Vec4) MulScalar

func (v Vec4) MulScalar(s float32) Vec4

MulScalar returns a vector with all elements of v scaled by s.

func (Vec4) Negate

func (v Vec4) Negate() Vec4

Negate returns a vector with all elements of v negated.

func (Vec4) Norm added in v1.1.0

func (v Vec4) Norm() float32

Norm returns the length of v.

func (Vec4) Normalized added in v1.1.0

func (v Vec4) Normalized() Vec4

Normalized returns a copy of v with elements normalized so the returned vector has length 1.

func (Vec4) SquareNorm added in v1.1.0

func (v Vec4) SquareNorm() float32

SquareNorm returns the square of the length of v.

func (Vec4) String

func (v Vec4) String() string

func (Vec4) Sub

func (v Vec4) Sub(w Vec4) Vec4

Sub returns the difference of v - w.

Directories

Path Synopsis
column_major
d3dmath
Package d3dmath provices vector and matrix functions for Direct3D. Vectors are row vectors and matrices are stored in column-major order.
Package d3dmath provices vector and matrix functions for Direct3D. Vectors are row vectors and matrices are stored in column-major order.
row_major
d3dmath
Package d3dmath provices vector and matrix functions for Direct3D. Vectors are row vectors and matrices are stored in row-major order.
Package d3dmath provices vector and matrix functions for Direct3D. Vectors are row vectors and matrices are stored in row-major order.

Jump to

Keyboard shortcuts

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