Documentation ¶
Overview ¶
Package goglmath is a lightweight pure Go 3D math package providing essential matrix/vector operations for GL graphics applications.
Index ¶
- func Cross3(x1, y1, z1, x2, y2, z2 float64) (float64, float64, float64)
- func Distance3(x1, y1, z1, x2, y2, z2 float64) float64
- func DistanceSquared3(x1, y1, z1, x2, y2, z2 float64) float64
- func Dot3(x1, y1, z1, x2, y2, z2 float64) float64
- func Length3(x, y, z float64) float64
- func LengthSquared3(x, y, z float64) float64
- func Matrix4Equal(m1, m2 *Matrix4) bool
- func Normalize3(x, y, z float64) (float64, float64, float64)
- func Ortho3(x1, y1, z1, x2, y2, z2 float64) bool
- func PickRay(camera *Matrix4, ...) (nearX, nearY, nearZ, farX, farY, farZ float64, err error)
- func SetModelMatrix(modelMatrix *Matrix4, ...)
- func SetOrthoMatrix(orthoMatrix *Matrix4, left, right, bottom, top, near, far float64)
- func SetPerspectiveMatrix(perspectiveMatrix *Matrix4, ...)
- func SetRotationMatrix(rotationMatrix *Matrix4, forwardX, forwardY, forwardZ, upX, upY, upZ float64)
- func SetViewMatrix(viewMatrix *Matrix4, ...)
- func ViewportTransform(viewportX, viewportWidth, viewportY, viewportHeight int, ...) (int, int, float64)
- type Matrix4
- func (m *Matrix4) CopyFrom(src *Matrix4)
- func (m *Matrix4) CopyInverseFrom(src *Matrix4) error
- func (m *Matrix4) Data() []float32
- func (m *Matrix4) Identity() bool
- func (m *Matrix4) Invert() error
- func (m *Matrix4) Multiply(n *Matrix4)
- func (m *Matrix4) Null() bool
- func (m *Matrix4) Rotate(forwardX, forwardY, forwardZ, upX, upY, upZ float64)
- func (m *Matrix4) Scale(x, y, z, w float64)
- func (m *Matrix4) SetIdentity()
- func (m *Matrix4) SetNull()
- func (m *Matrix4) Transform(x, y, z, w float64) (tx, ty, tz, tw float64)
- func (m *Matrix4) Translate(tx, ty, tz, tw float64)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DistanceSquared3 ¶
DistanceSquared3 calculates the squared of the distance between two points.
func LengthSquared3 ¶
LengthSquared3 calculates the squared length of a vector.
func Matrix4Equal ¶
Matrix4Equal tests matrices for equality.
func Normalize3 ¶
Normalize3 calculates a normalized copy of a vector.
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 ¶
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 (*Matrix4) CopyInverseFrom ¶
CopyInverseFrom sets the matrix as inverse of another source matrix.
func (*Matrix4) Rotate ¶
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