goglmath

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 28, 2023 License: MIT Imports: 3 Imported by: 2

README

license GoDoc Go Report Card

goglmath

goglmath - Lightweight pure Go 3D math package providing essential matrix/vector operations for GL graphics applications.

Full Transformation Stack

obj.coord. -> P*V*T*R*U*S*o -> clip coord  -> divide by w -> NDC coord -> viewport transform      -> win.coord+depth
                ---------      -----------                   ---------    -----------------------    ---------------
                "MV"           gl_Position                   vec3         Viewport()+DepthRange()    x,y,depth
                               vec4                          -1..1

o           = obj.coord
P*V*T*R*U*S = full transformation matrix
P           = Perspective
V           = View (inverse of camera) built by setViewMatrix
T*R         = Model transformation built by setModelMatrix
T           = Translation
R           = Rotation
U           = Undo Model Local Rotation
S           = Scaling

Typical vertex shader: gl_Position = u_P * u_MV * vec4(a_Position, 1.0);
a_Position  = obj.coord
u_MV        = V*T*R*U*S
u_P         = P
gl_Position = clip coord

Viewport transform:
- After clipping and division by w, NDC coordinates range from -1 to 1
- programmed with: Viewport()+DepthRange()
- input: NDC coord (vec3)
- output: win.coord (x,y) + depth

--xx--

Documentation

Overview

Package goglmath is a lightweight pure Go 3D math package providing essential matrix/vector operations for GL graphics applications.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cross3

func Cross3(x1, y1, z1, x2, y2, z2 float64) (float64, float64, float64)

Cross3 calculates the cross (vector) product of two vectors.

func Distance3

func Distance3(x1, y1, z1, x2, y2, z2 float64) float64

Distance3 calculates the distance between two points.

func DistanceSquared3

func DistanceSquared3(x1, y1, z1, x2, y2, z2 float64) float64

DistanceSquared3 calculates the squared of the distance between two points.

func Dot3

func Dot3(x1, y1, z1, x2, y2, z2 float64) float64

Dot3 calculates the dot (scalar) product of two vectors.

func Length3

func Length3(x, y, z float64) float64

Length3 calculates the length of a vector.

func LengthSquared3

func LengthSquared3(x, y, z float64) float64

LengthSquared3 calculates the squared length of a vector.

func Matrix4Equal

func Matrix4Equal(m1, m2 *Matrix4) bool

Matrix4Equal tests matrices for equality.

func Normalize3

func Normalize3(x, y, z float64) (float64, float64, float64)

Normalize3 calculates a normalized copy of a vector.

func Ortho3

func Ortho3(x1, y1, z1, x2, y2, z2 float64) bool

Ortho3 reports if two vectors are orthogonal.

func PickRay

func PickRay(camera *Matrix4, viewportX, viewportWidth, viewportY, viewportHeight, pickX, pickY int) (nearX, nearY, nearZ, farX, farY, farZ float64, err error)

PickRay calculates points where pickX,pickY intersects near and far planes.

camera = includes both the perspective and view transforms (camera: the func parameter)

obj.coord. -> P*V*T*R*U*S -> clip coord -> divide by w -> NDC coord -> viewport transform -> window coord P*V*T*R*U*S = full transformation P = Perspective V = View (inverse of camera) built by setViewMatrix T*R = model transformation built by setModelMatrix T = Translation R = Rotation U = Undo Model Local Rotation S = Scaling

func SetModelMatrix

func SetModelMatrix(modelMatrix *Matrix4, forwardX, forwardY, forwardZ, upX, upY, upZ, tX, tY, tZ float64)

SetModelMatrix builds the model matrix. Model transformation represents objection location/orientation in world space. Model transformation is also known as "camera" transformation. Model transformation is the inverse of the view transformation. Common use is to compute object location/orientation into full transformation matrix.

obj.coord. -> P*V*T*R*U*S -> clip coord -> divide by w -> NDC coord -> viewport transform -> window coord P*V*T*R*U*S = full transformation P = Perspective V = View (inverse of camera) built by setViewMatrix T*R = model transformation built by THIS setModelMatrix T = Translation R = Rotation U = Undo Model Local Rotation S = Scaling

null model: forward = 0 0 -1 // looking towards -Z up = 0 1 0 // up direction is +Y translation = 0 0 0 // position at origin setModelMatrix(&rotation, 0, 0, -1, 0, 1, 0, 0, 0, 0)

func SetOrthoMatrix

func SetOrthoMatrix(orthoMatrix *Matrix4, left, right, bottom, top, near, far float64)

SetOrthoMatrix builds matrix for orthographic projection.

near=-1 far=1 -> flip Z (this is the usual ortho projection) near=1 far=-1 -> keep Z

SetOrthoMatrix(m,-1,1,-1,1,-1,1): flip Z (this is the usual ortho projection) SetOrthoMatrix(m,-1,1,-1,1,1,-1): identity

func SetPerspectiveMatrix

func SetPerspectiveMatrix(perspectiveMatrix *Matrix4, fieldOfViewYRadians, aspectRatio, zNear, zFar float64)

SetPerspectiveMatrix builds the perspective projection matrix.

func SetRotationMatrix

func SetRotationMatrix(rotationMatrix *Matrix4, forwardX, forwardY, forwardZ, upX, upY, upZ float64)

SetRotationMatrix builds the rotation matrix. The rotation matrix will rotate a point from the null rotation direction to the direction specified by the given forward and up vectors.

Null rotation vectors are: forward = 0 0 -1 // looking towards -Z up = 0 1 0 // up direction is +Y SetRotationMatrix(&rotation, 0, 0, -1, 0, 1, 0)

func SetViewMatrix

func SetViewMatrix(viewMatrix *Matrix4, focusX, focusY, focusZ, upX, upY, upZ, posX, posY, posZ float64)

SetViewMatrix builds the view matrix. View transformation represents camera inverted location/orientation in world space. View transformation moves all world objects in order to simulate a camera. View transformation is also known as "lookAt" transformation. View transformation is the inverse of the model transformation. Common use is to compute camera location/orientation into full transformation matrix.

obj.coord. -> P*V*T*R*U*S -> clip coord -> divide by w -> NDC coord -> viewport transform -> window coord P*V*T*R*U*S = full transformation P = Perspective V = View (inverse of camera) built by THIS setViewMatrix T*R = model transformation built by setModelMatrix T = Translation R = Rotation U = Undo Model Local Rotation S = Scaling

null view matrix: focus = 0 0 -1 up = 0 1 0 pos = 0 0 0 setViewMatrix(&V, 0, 0, -1, 0, 1, 0, 0, 0, 0)

func ViewportTransform

func ViewportTransform(viewportX, viewportWidth, viewportY, viewportHeight int, depthNear, depthFar, ndcX, ndcY, ndcZ float64) (int, int, float64)

ViewportTransform simulates the viewport transform. ViewportTransform maps NDC coordinates to viewport coordinates. Input: viewport (viewportX, viewportWidth, viewportY, viewportHeight) Input: depthRange (depthNear, depthFar) Output: x,y,depth (x,y = viewport coord)

Types

type Matrix4

type Matrix4 struct {
	// contains filtered or unexported fields
}

Matrix4 is a 4x4 matrix.

func NewMatrix4Identity

func NewMatrix4Identity() Matrix4

NewMatrix4Identity creates an identity matrix.

func NewMatrix4Null

func NewMatrix4Null() Matrix4

NewMatrix4Null creates a null matrix.

func (*Matrix4) CopyFrom

func (m *Matrix4) CopyFrom(src *Matrix4)

CopyFrom copy matrix data from another source matrix.

func (*Matrix4) CopyInverseFrom

func (m *Matrix4) CopyInverseFrom(src *Matrix4) error

CopyInverseFrom sets the matrix as inverse of another source matrix.

func (*Matrix4) Data

func (m *Matrix4) Data() []float32

Data returns matrix data as slice ready to GPU upload.

func (*Matrix4) Identity

func (m *Matrix4) Identity() bool

Identity reports if matrix is identity.

func (*Matrix4) Invert

func (m *Matrix4) Invert() error

Invert inverts the matrix.

func (*Matrix4) Multiply

func (m *Matrix4) Multiply(n *Matrix4)

Multiply multiplies the matrix by another matrix.

func (*Matrix4) Null

func (m *Matrix4) Null() bool

Null reports if matrix is null.

func (*Matrix4) Rotate

func (m *Matrix4) Rotate(forwardX, forwardY, forwardZ, upX, upY, upZ float64)

Rotate multiplies the matrix m by a rotation matrix built from specified forward and up vectors. This rotation will rotate a point from the "null rotation" direction to the direction specified by forward and up vectors. null rotation: forward = 0 0 -1 // looking towards -Z up = 0 1 0 // up direction is +Y

func (*Matrix4) Scale

func (m *Matrix4) Scale(x, y, z, w float64)

Scale multiplies the matrix by a scaling matrix. usually set w to 1.0

func (*Matrix4) SetIdentity

func (m *Matrix4) SetIdentity()

SetIdentity sets matrix to identity.

func (*Matrix4) SetNull

func (m *Matrix4) SetNull()

SetNull sets matrix to null.

func (*Matrix4) Transform

func (m *Matrix4) Transform(x, y, z, w float64) (tx, ty, tz, tw float64)

Transform multiples this matrix [m] by vector [x,y,z,w]

func (*Matrix4) Translate

func (m *Matrix4) Translate(tx, ty, tz, tw float64)

Translate multiples the matrix by a translation matrix. usually set w to 1.0

Jump to

Keyboard shortcuts

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