Git Product home page Git Product logo

vkm's Introduction

VKM

VKM is a simple vector and matrix math package written in Go, inspired by glm, and targeting 3D graphics programing.

VKM is specifically targeted at 3D graphics and is NOT inteded to be an all-purpose vector math or linear algebra package. It includes some variant types like Pt2 and Vec3 for convenience, but the primary matrix math functions rely on homogenous 3D vectors and points.

While this module can be used in any Go program wanting to work with 3D transformations, it was built to use with go-vk, a Go language binding for the Vulkan graphics API.

Usage

See the GoDoc for the full API.

VKM defines three primary types: Pt, Vec, and Matrix. All three are stored in column-major order and are fundamentally arrays of [4]float32. (Note: this explicitly means static arrays and not slices.)

We use float32, and not Go's default 64-bit floating point type, because Vulkan (generally) uses 32-bit floats on the GPU. As a consequence, and rather than constantly forcing float32 type casts, VKM uses the chewxy/math32 package for float32 operations and constants.

Examples

Create a basic transformation

You can generate a simple tranformation with the NewMat__ variants:

import "github.com/bbredesen/vkm"
// ...
transVec := vkm.NewVec(-1, -2, -3)
transMat := vkm.NewMatTranslate(transVec)

Rotations are measured in radians by default, but each rotation function has a "Deg" variant to use degrees. Rotation angles will scale with the length of the axis vector, so be sure to normalize the vector first:

import "github.com/chewxy/math32"
// ...
axisVec := vkm.NewVec(1, 1, 1).Normalize()
aRotationMat := vkm.NewMatRotate(axisVec, math32.Pi)

Multiply arbitrary matricies and points or vectors

Matrix multiplication happens in the order written when you directly call MultM, MultV, or MultP. The following code has the apparent effect of rotating the world before translating:

transformMat := myTranslationMat.MultM(myRotationMat)

Create a "readable" transformation

Transformations can be chained, as long as you have a matrix to start from. In this example, we apply a translation of -5 in the X direction, rotate around the Y axis by 45 degrees counterclockwise, and then translate by +5 in the X direction:

transVec := NewVec(-5, 0, 0)

m := Identity().
    Translate(transVec).
    RotateYDeg(-45).
    Translate(transVec.Invert())

When written in this form, we apply transformations in the order they are written. In practice, the code is actually right-multiplying each subsequent transformation, as in the previous example.

Performance Optimization TODO

All math in this library is currently writing in pure Go. Performance could benefit from using SIMD extensions on Intel/AMD CPUs, which will require writing those functions in assembly.

vkm's People

Contributors

bbredesen avatar

Watchers

 avatar

vkm's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.