mirror of
https://github.com/bloeys/gglm.git
synced 2025-12-29 05:28:20 +00:00
b9faa2e59ed7e1308c8e2c2ed0a482ff2ee10dc1
gglm
Fast OpenGL/Graphics focused Mathematics library in Go inspired by the c++ library glm.
gglm currently has the following:
- Matrices are stored column major
Vec2,Vec3andVec4structs that implement vector (x,y,z,w) operationsMat2,Mat3,Mat4structs that implement square matrix operationsQuatstruct that implements quaternion operationsTrMatstruct that implements 3D transformation matrix operations- Many useful geometric functions (e.g. dot product, cross product, vector reflection etc)
- 32-bit scalar operations (e.g. sin32, cos32, equality using epsilon, sqrt32 etc)
- Useful 32-bit constants (e.g. pi, Deg2Rad, Rad2Deg, float32 epsilon etc)
- Simple 'swizzle' interfaces that allow you to do things like
.X()or.R()etc. - Very easy to use with graphics/native APIs as everything is implemented using arrays
.String()functions on all types for pretty printing
Installation
go get github.com/bloeys/gglm
Usage
import "github.com/bloeys/gglm/gglm"
func main() {
// Vec2
v1 := &gglm.Vec2{Data: [2]float32{1, 2}}
v2 := &gglm.Vec2{Data: [2]float32{3, 4}}
println(gglm.DistVec2(v1, v2))
println(gglm.SqrDistVec2(v2, v1))
println(v1.Eq(v2))
v2.Set(1, 2)
println(v1.Eq(v2))
// This performs: v1 += v2
// v1 is returned from the function, so we can chain calls that operate on v1
newX := v1.Add(v2).X()
println("newX:", newX)
// LookAt for a right-handed coordinate system
camPos := gglm.NewVec3(0, 0, 3)
worldUp := gglm.NewVec3(0, 1, 0)
targetPos := gglm.NewVec3(0, 0, 0)
viewMat := gglm.LookAtRH(camPos, targetPos, worldUp)
println(viewMat.String())
}
Notes
You can check compiler inlining decisions using go run -gcflags "-m" .. Some functions look a bit weird
because we are trying to reduce function complexity so the compiler inlines.
Description
Fast Go OpenGL/Graphics focused Mathematics library inspired by the c++ library glm (https://github.com/g-truc/glm)
Languages
Go
100%