1. Products
  2.   3D
  3.   GO
  4.   MathGL
 
  

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 building high-performance applications involving 3D graphics, physics simulations, or game development in Go programming language, the mathematical precision and efficiency are paramount. This is where MathGL shines. It is an open-source mathematics library for the Go programming language that provides a wide range of vector, matrix, and quaternion types and operations essential for 3D graphics and scientific computations. The library supports various features such as Arithmetic operations, Mathematical primitives like vectors (2D, 3D, 4D), matrices (2x2 to 4x4). Moreover, it also supports transformations and utilities for camera manipulation, projection, and 3D rendering math.

Developed as part of the go-gl project, MathGL offers a clean, well-documented API that makes complex mathematical operations accessible to Go developers. The library is actively maintained and has become a popular choice among developers working with 3D graphics and linear algebra in Go. MathGL provides both float32 (mgl32) and float64 (mgl64) versions of all operations, allowing developers to choose based on their precision and performance requirements. The library also includes various easing functions useful for animations and smooth transitions. If you're diving into 3D graphics with Go, give MathGL a try—you might find it’s exactly the tool you need to bring your ideas to life.

Previous Next

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.