Free Go Library for Vector Operations & Matrix Transformations
Open Source Go 3D Processing Library for Vector Operations That Provides a Wide Range of Vector, Matrix, and Quaternion Types and Operations Essential for 3D Graphics and Scientific Computations
What is MathGL Library?
When creating high-performance 3D graphics, physics simulations, or game engines in Go, mathematical precision and speed are essential. MathGL is an open source Go 3D API that delivers a complete toolkit for vectors, matrices, and quaternions, making it perfect for 3D matrix transformations, 3D vector transformations, and scientific computations. This free Go API supports arithmetic operations, vector handling (2D, 3D, 4D), matrix creation (2x2 to 4x4), and camera projection utilities for 3D rendering math. Developers can also perform vector operations via Go, create matrices via Go API, and manipulate 3D wireframes with ease—making MathGL a powerful foundation for graphics, physics, and real-time rendering workflows.
Part of the go-gl project, MathGL offers a clean, well-documented API that simplifies complex math for Go developers. With both float32 (mgl32) and float64 (mgl64) versions, it balances performance and precision needs. The library even includes utilities to convert quaternion to rotation matrix and easing functions for smooth animations. Its active maintenance and strong community support make it a go-to choice for anyone working in 3D wireframe drawing, game development, or simulation modeling. Whether you’re building immersive 3D worlds or optimizing vector-based computations, MathGL equips you with the mathematical backbone to turn ambitious Go projects into reality.
Getting Started with MathGL
The recommend way to install MathGL is using GitHub. Please use the following command for a smooth installation.
Install MathGL API via Get command
$ go get -u github.com/go-gl/mathgl.git
You can download the compiled shared library from Github repository.
Vector Operations via Go Library
The open source MathGL library provides extensive support for vector and matrix mathematics, including operations for 2D, 3D, and 4D vectors and matrices. These are essential for handling positions, directions, velocities, and other spatial data. It provides support for Add, Sub, Dot, Cross, Normalize, etc. and multiple precision types (mgl32 for float32, mgl64 for float64). Here is a simple example that demonstrates how software developers can perform different vector operations inside their Go applications.
How to Perform Vector Operations via Go Library?
package main
import (
"fmt"
"github.com/go-gl/mathgl/mgl32"
)
func main() {
// Create two 3D vectors
v1 := mgl32.Vec3{1, 2, 3}
v2 := mgl32.Vec3{4, 5, 6}
// Vector addition
sum := v1.Add(v2)
fmt.Println("Vector sum:", sum) // [5 7 9]
// Dot product
dot := v1.Dot(v2)
fmt.Println("Dot product:", dot) // 32
// Cross product
cross := v1.Cross(v2)
fmt.Println("Cross product:", cross) // [-3 6 -3]
}
Matrix Transformations via Go Library
The open source MathGL library provide various matrix types including 2x2, 3x3, and 4x4 matrices. The library has included support for matrix multiplication and vector transformation.These are particularly useful for transformations in 3D space. Moreover, the library includes comprehensive matrix transformation functions for translation, rotation, scaling, and projection. Here is a simple example that shows how software developers can apply different types of transformation inside Go applications.
How to Create Various Types of Matrixes & Apply Transformation to It via Go Library?
package main
import (
"fmt"
"github.com/go-gl/mathgl/mgl32"
"math"
)
func main() {
// Create an identity matrix
identity := mgl32.Ident4()
fmt.Println("Identity matrix:\n", identity)
// Create a translation matrix
translation := mgl32.Translate3D(2, 3, 4)
fmt.Println("Translation matrix:\n", translation)
// Create a rotation matrix (45 degrees around Y axis)
rotation := mgl32.HomogRotate3DY(mgl32.DegToRad(45))
fmt.Println("Rotation matrix:\n", rotation)
// Create a scaling matrix
scale := mgl32.Scale3D(2, 2, 2)
fmt.Println("Scaling matrix:\n", scale)
// Combine transformations
transform := translation.Mul4(rotation).Mul4(scale)
fmt.Println("Combined transformation:\n", transform)
}
Quaternion Operations Support
The MathGL library has included a wide range of primitive shapes such as cubes, spheres, and cones, making it easy to create common 3D objects. Additionally, developers can define custom objects by specifying vertices, edges, and faces, allowing for the creation of complex and unique wireframe scenes. Engineers and designers can use the library to quickly prototype and visualize 3D models, aiding in the evaluation of designs and identification of potential issues before committing to full-scale development.
How to Convert Quaternion to Rotation Matrix via Go Library?
package main
import (
"fmt"
"github.com/go-gl/mathgl/mgl32"
)
func main() {
// Create a quaternion representing 90 degree rotation around X axis
q := mgl32.QuatRotate(mgl32.DegToRad(90), mgl32.Vec3{1, 0, 0})
// Convert quaternion to rotation matrix
rotMat := q.Mat4()
fmt.Println("Rotation matrix from quaternion:\n", rotMat)
// Spherical linear interpolation between two quaternions
q1 := mgl32.QuatRotate(mgl32.DegToRad(0), mgl32.Vec3{0, 1, 0})
q2 := mgl32.QuatRotate(mgl32.DegToRad(90), mgl32.Vec3{0, 1, 0})
interpolated := mgl32.QuatSlerp(q1, q2, 0.5) // Halfway between
fmt.Println("Interpolated quaternion:", interpolated)
}
Geometric Utilities & Easing Functions
The open source MathGL library is very easy to use and included a very clear API and versatile functionality make it an essential tool in any Go developer’s kit, especially in 3D environments. The library includes various geometric utilities for common operations like line-plane intersections, point containment tests, and more. Moreover, the library also included various easing functions useful for animations and smooth transitions.