4 Commits

Author SHA1 Message Date
41307a8c5b Matrix transpose 2024-05-01 02:25:52 +04:00
da81ee79d9 Formatting 2024-05-01 01:25:10 +04:00
4eb59e3386 Implement RotByQuat and AngleVec3 and their tests 2022-12-06 04:26:10 +04:00
ed6806f23b Update readme 2022-10-01 00:15:51 +04:00
18 changed files with 293 additions and 111 deletions

View File

@ -1,9 +1,10 @@
# gglm
Fast OpenGL/Graphics focused Mathematics library in Go inspired by the c++ library [glm](https://github.com/g-truc/glm).
Fast OpenGL/Graphics focused Mathematics library in Go inspired by the c++ library [glm](https://github.com/g-truc/glm).
gglm currently has the following:
- Matrices are stored column major
- `Vec2`, `Vec3` and `Vec4` structs that implement vector (x,y,z,w) operations
- `Mat2`, `Mat3`, `Mat4` structs that implement square matrix operations
- `Quat` struct that implements quaternion operations
@ -11,9 +12,9 @@ gglm currently has the following:
- 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 'siwzzle' interfaces that allow you to do things like `.X()` or `.R()` 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 pritning
- `.String()` functions on all types for pretty printing
## Installation
@ -24,17 +25,9 @@ gglm currently has the following:
```go
import "github.com/bloeys/gglm/gglm"
func main() {
//LookAt
camPos := gglm.NewVec3(0, 0, 3)
worldUp := gglm.NewVec3(0, 1, 0)
targetPos := gglm.NewVec3(0, 0, 0)
viewMat := gglm.LookAt(camPos, targetPos, worldUp)
println(viewMat.String())
//Vec2
// Vec2
v1 := &gglm.Vec2{Data: [2]float32{1, 2}}
v2 := &gglm.Vec2{Data: [2]float32{3, 4}}
println(gglm.DistVec2(v1, v2))
@ -43,10 +36,17 @@ func main() {
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
// 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())
}
```

View File

@ -6,6 +6,6 @@ const (
Rad2Deg float32 = 180 / Pi
F32Epsilon float32 = 1e-6
//CosHalf is Cos32(0.5)
// CosHalf is Cos32(0.5)
CosHalf float32 = 0.87758256189
)

View File

@ -28,14 +28,14 @@ func Cross(v1, v2 *Vec3) *Vec3 {
}
}
//DistVec2 returns euclidean distance between v1 and v2
// DistVec2 returns euclidean distance between v1 and v2
func DistVec2(v1, v2 *Vec2) float32 {
x := v1.X() - v2.X()
y := v1.Y() - v2.Y()
return float32(math.Sqrt(float64(x*x + y*y)))
}
//DistVec3 returns euclidean distance between v1 and v2
// DistVec3 returns euclidean distance between v1 and v2
func DistVec3(v1, v2 *Vec3) float32 {
x := v1.X() - v2.X()
y := v1.Y() - v2.Y()
@ -43,7 +43,7 @@ func DistVec3(v1, v2 *Vec3) float32 {
return float32(math.Sqrt(float64(x*x + y*y + z*z)))
}
//DistVec4 returns euclidean distance between v1 and v2
// DistVec4 returns euclidean distance between v1 and v2
func DistVec4(v1, v2 *Vec4) float32 {
//Using X() etc won't let the function inline
@ -54,14 +54,14 @@ func DistVec4(v1, v2 *Vec4) float32 {
return float32(math.Sqrt(float64(x*x + y*y + z*z + w*w)))
}
//DistVec2 returns the squared euclidean distance between v1 and v2 (avoids a sqrt)
// DistVec2 returns the squared euclidean distance between v1 and v2 (avoids a sqrt)
func SqrDistVec2(v1, v2 *Vec2) float32 {
x := v1.X() - v2.X()
y := v1.Y() - v2.Y()
return x*x + y*y
}
//DistVec3 returns the squared euclidean distance between v1 and v2 (avoids a sqrt)
// DistVec3 returns the squared euclidean distance between v1 and v2 (avoids a sqrt)
func SqrDistVec3(v1, v2 *Vec3) float32 {
x := v1.X() - v2.X()
y := v1.Y() - v2.Y()
@ -69,7 +69,7 @@ func SqrDistVec3(v1, v2 *Vec3) float32 {
return x*x + y*y + z*z
}
//DistVec4 returns the squared euclidean distance between v1 and v2 (avoids a sqrt)
// DistVec4 returns the squared euclidean distance between v1 and v2 (avoids a sqrt)
func SqrDistVec4(v1, v2 *Vec4) float32 {
x := v1.Data[0] - v2.Data[0]
y := v1.Data[1] - v2.Data[1]
@ -78,9 +78,9 @@ func SqrDistVec4(v1, v2 *Vec4) float32 {
return x*x + y*y + z*z + w*w
}
//ReflectVec2 returns the reflected vector of the incoming vector 'v', and the surface normal 'n'.
// ReflectVec2 returns the reflected vector of the incoming vector 'v', and the surface normal 'n'.
//
//Note: n must be normalized or you will get wrong results
// Note: n must be normalized or you will get wrong results
func ReflectVec2(v, n *Vec2) *Vec2 {
//reflectedVec = v 2*dot(v, norm)*norm
@ -94,9 +94,9 @@ func ReflectVec2(v, n *Vec2) *Vec2 {
}
}
//ReflectVec3 returns the reflected vector of the incoming vector 'v', and the surface normal 'n'.
// ReflectVec3 returns the reflected vector of the incoming vector 'v', and the surface normal 'n'.
//
//Note: n must be normalized or you will get wrong results
// Note: n must be normalized or you will get wrong results
func ReflectVec3(v, n *Vec3) *Vec3 {
//reflectedVec = v 2*dot(v, norm)*norm
@ -111,7 +111,12 @@ func ReflectVec3(v, n *Vec3) *Vec3 {
}
}
//AngleQuat returns the angle between the two quaternions in radians
// AngleVec3 returns the angle between the two vectors in radians
func AngleVec3(v1, v2 *Vec3) float32 {
return Acos32(DotVec3(v1, v2) / (v1.Mag() * v2.Mag()))
}
// AngleQuat returns the angle between the two quaternions in radians
func AngleQuat(q1, q2 *Quat) float32 {
return Acos32(DotQuat(q1, q2))
}

View File

@ -13,7 +13,7 @@ const (
MatSize4x4
)
//String panics if the MatSize is not known
// String panics if the MatSize is not known
func (ms MatSize) String() string {
switch ms {

View File

@ -24,7 +24,7 @@ func (m *Mat2) Size() MatSize {
}
func (m *Mat2) String() string {
//+ always shows +/- sign; - means pad to the right; 9 means total of 9 digits (or padding if less); .3 means 3 decimals
// + always shows +/- sign; - means pad to the right; 9 means total of 9 digits (or padding if less); .3 means 3 decimals
return fmt.Sprintf("\n| %+-9.3f %+-9.3f |\n| %+-9.3f %+-9.3f |\n", m.Data[0][0], m.Data[0][1], m.Data[1][0], m.Data[1][1])
}
@ -32,7 +32,7 @@ func (m *Mat2) Col(c int) *Vec2 {
return &Vec2{Data: m.Data[c]}
}
//Add m += m2
// Add m += m2
func (m *Mat2) Add(m2 *Mat2) *Mat2 {
m.Data[0][0] += m2.Data[0][0]
m.Data[0][1] += m2.Data[0][1]
@ -41,7 +41,7 @@ func (m *Mat2) Add(m2 *Mat2) *Mat2 {
return m
}
//Add m -= m2
// Add m -= m2
func (m *Mat2) Sub(m2 *Mat2) *Mat2 {
m.Data[0][0] -= m2.Data[0][0]
m.Data[0][1] -= m2.Data[0][1]
@ -50,7 +50,7 @@ func (m *Mat2) Sub(m2 *Mat2) *Mat2 {
return m
}
//Mul m *= m2
// Mul m *= m2
func (m1 *Mat2) Mul(m2 *Mat2) *Mat2 {
m1.Data = [2][2]float32{
{
@ -66,7 +66,7 @@ func (m1 *Mat2) Mul(m2 *Mat2) *Mat2 {
return m1
}
//Scale m *= x (element wise multiplication)
// Scale m *= x (element wise multiplication)
func (m *Mat2) Scale(x float32) *Mat2 {
m.Data[0][0] *= x
m.Data[0][1] *= x
@ -75,15 +75,25 @@ func (m *Mat2) Scale(x float32) *Mat2 {
return m
}
func (v *Mat2) Clone() *Mat2 {
return &Mat2{Data: v.Data}
func (m *Mat2) Clone() *Mat2 {
return &Mat2{Data: m.Data}
}
func (m *Mat2) Eq(m2 *Mat2) bool {
return m.Data == m2.Data
}
//AddMat2 m3 = m1 + m2
func (m *Mat2) Transpose() *Mat2 {
m.Data = [2][2]float32{
{m.Data[0][0], m.Data[1][0]},
{m.Data[0][1], m.Data[1][1]},
}
return m
}
// AddMat2 m3 = m1 + m2
func AddMat2(m1, m2 *Mat2) *Mat2 {
return &Mat2{
Data: [2][2]float32{
@ -99,7 +109,7 @@ func AddMat2(m1, m2 *Mat2) *Mat2 {
}
}
//SubMat2 m3 = m1 - m2
// SubMat2 m3 = m1 - m2
func SubMat2(m1, m2 *Mat2) *Mat2 {
return &Mat2{
Data: [2][2]float32{
@ -115,7 +125,7 @@ func SubMat2(m1, m2 *Mat2) *Mat2 {
}
}
//MulMat2 m3 = m1 * m2
// MulMat2 m3 = m1 * m2
func MulMat2(m1, m2 *Mat2) *Mat2 {
return &Mat2{
Data: [2][2]float32{
@ -131,7 +141,7 @@ func MulMat2(m1, m2 *Mat2) *Mat2 {
}
}
//MulMat2Vec2 v2 = m1 * v1
// MulMat2Vec2 v2 = m1 * v1
func MulMat2Vec2(m1 *Mat2, v1 *Vec2) *Vec2 {
return &Vec2{
Data: [2]float32{
@ -141,7 +151,7 @@ func MulMat2Vec2(m1 *Mat2, v1 *Vec2) *Vec2 {
}
}
//NewMat2Id returns the 2x2 identity matrix
// NewMat2Id returns the 2x2 identity matrix
func NewMat2Id() *Mat2 {
return &Mat2{
Data: [2][2]float32{

View File

@ -147,6 +147,38 @@ func TestMulMat2Vec2(t *testing.T) {
}
}
func TestTransposeMat2(t *testing.T) {
m := gglm.NewMat2Id()
ans := gglm.NewMat2Id()
if !m.Transpose().Transpose().Eq(ans) {
t.Errorf("Got: %v; Expected: %v", m.String(), ans.String())
}
if !m.Transpose().Eq(ans) {
t.Errorf("Got: %v; Expected: %v", m.String(), ans.String())
}
m = &gglm.Mat2{
Data: [2][2]float32{
{00, 01},
{10, 11},
},
}
ans = &gglm.Mat2{
Data: [2][2]float32{
{00, 10},
{01, 11},
},
}
if !m.Transpose().Eq(ans) {
t.Errorf("Got: %v; Expected: %v", m.String(), ans.String())
}
}
func BenchmarkMulMat2(b *testing.B) {
m1 := gglm.NewMat2Id()

View File

@ -35,7 +35,7 @@ func (m *Mat3) Col(c int) *Vec3 {
return &Vec3{Data: m.Data[c]}
}
//Add m += m2
// Add m += m2
func (m *Mat3) Add(m2 *Mat3) *Mat3 {
m.Data[0][0] += m2.Data[0][0]
@ -52,7 +52,7 @@ func (m *Mat3) Add(m2 *Mat3) *Mat3 {
return m
}
//Add m -= m2
// Add m -= m2
func (m *Mat3) Sub(m2 *Mat3) *Mat3 {
m.Data[0][0] -= m2.Data[0][0]
@ -69,7 +69,7 @@ func (m *Mat3) Sub(m2 *Mat3) *Mat3 {
return m
}
//Mul m *= m2
// Mul m *= m2
func (m *Mat3) Mul(m2 *Mat3) *Mat3 {
//Array indices:
@ -109,7 +109,7 @@ func (m *Mat3) Mul(m2 *Mat3) *Mat3 {
return m
}
//Scale m *= x (element wise multiplication)
// Scale m *= x (element wise multiplication)
func (m *Mat3) Scale(x float32) *Mat3 {
m.Data[0][0] *= x
@ -126,15 +126,26 @@ func (m *Mat3) Scale(x float32) *Mat3 {
return m
}
func (v *Mat3) Clone() *Mat3 {
return &Mat3{Data: v.Data}
func (m *Mat3) Clone() *Mat3 {
return &Mat3{Data: m.Data}
}
func (m *Mat3) Eq(m2 *Mat3) bool {
return m.Data == m2.Data
}
//AddMat3 m3 = m1 + m2
func (m *Mat3) Transpose() *Mat3 {
m.Data = [3][3]float32{
{m.Data[0][0], m.Data[1][0], m.Data[2][0]},
{m.Data[0][1], m.Data[1][1], m.Data[2][1]},
{m.Data[0][2], m.Data[1][2], m.Data[2][2]},
}
return m
}
// AddMat3 m3 = m1 + m2
func AddMat3(m1, m2 *Mat3) *Mat3 {
return &Mat3{
Data: [3][3]float32{
@ -157,7 +168,7 @@ func AddMat3(m1, m2 *Mat3) *Mat3 {
}
}
//SubMat3 m3 = m1 - m2
// SubMat3 m3 = m1 - m2
func SubMat3(m1, m2 *Mat3) *Mat3 {
return &Mat3{
Data: [3][3]float32{
@ -180,7 +191,7 @@ func SubMat3(m1, m2 *Mat3) *Mat3 {
}
}
//MulMat3 m3 = m1 * m2
// MulMat3 m3 = m1 * m2
func MulMat3(m1, m2 *Mat3) *Mat3 {
m00 := m1.Data[0][0]
@ -216,7 +227,7 @@ func MulMat3(m1, m2 *Mat3) *Mat3 {
}
}
//MulMat3Vec3 v2 = m1 * v1
// MulMat3Vec3 v2 = m1 * v1
func MulMat3Vec3(m1 *Mat3, v1 *Vec3) *Vec3 {
return &Vec3{
Data: [3]float32{
@ -227,7 +238,7 @@ func MulMat3Vec3(m1 *Mat3, v1 *Vec3) *Vec3 {
}
}
//NewMat3Id returns the 3x3 identity matrix
// NewMat3Id returns the 3x3 identity matrix
func NewMat3Id() *Mat3 {
return &Mat3{
Data: [3][3]float32{

View File

@ -162,6 +162,40 @@ func TestMulMat3Vec3(t *testing.T) {
}
}
func TestTransposeMat3(t *testing.T) {
m := gglm.NewMat3Id()
ans := gglm.NewMat3Id()
if !m.Transpose().Transpose().Eq(ans) {
t.Errorf("Got: %v; Expected: %v", m.String(), ans.String())
}
if !m.Transpose().Eq(ans) {
t.Errorf("Got: %v; Expected: %v", m.String(), ans.String())
}
m = &gglm.Mat3{
Data: [3][3]float32{
{00, 01, 02},
{10, 11, 12},
{20, 21, 22},
},
}
ans = &gglm.Mat3{
Data: [3][3]float32{
{00, 10, 20},
{01, 11, 21},
{02, 12, 22},
},
}
if !m.Transpose().Eq(ans) {
t.Errorf("Got: %v; Expected: %v", m.String(), ans.String())
}
}
func BenchmarkMulMat3(b *testing.B) {
m1 := gglm.NewMat3Id()

View File

@ -36,7 +36,7 @@ func (m *Mat4) Col(c int) *Vec4 {
return &Vec4{Data: m.Data[c]}
}
//Add m += m2
// Add m += m2
func (m *Mat4) Add(m2 *Mat4) *Mat4 {
m.Data[0][0] += m2.Data[0][0]
@ -62,7 +62,7 @@ func (m *Mat4) Add(m2 *Mat4) *Mat4 {
return m
}
//Add m -= m2
// Add m -= m2
func (m *Mat4) Sub(m2 *Mat4) *Mat4 {
m.Data[0][0] -= m2.Data[0][0]
@ -87,7 +87,7 @@ func (m *Mat4) Sub(m2 *Mat4) *Mat4 {
return m
}
//Mul m *= m2
// Mul m *= m2
func (m *Mat4) Mul(m2 *Mat4) *Mat4 {
//Array indices:
@ -147,7 +147,7 @@ func (m *Mat4) Mul(m2 *Mat4) *Mat4 {
return m
}
//Scale m *= x (element wise multiplication)
// Scale m *= x (element wise multiplication)
func (m *Mat4) Scale(x float32) *Mat4 {
m.Data[0][0] *= x
@ -180,7 +180,19 @@ func (m *Mat4) Eq(m2 *Mat4) bool {
return m.Data == m2.Data
}
//AddMat4 m3 = m1 + m2
func (m *Mat4) Transpose() *Mat4 {
m.Data = [4][4]float32{
{m.Data[0][0], m.Data[1][0], m.Data[2][0], m.Data[3][0]},
{m.Data[0][1], m.Data[1][1], m.Data[2][1], m.Data[3][1]},
{m.Data[0][2], m.Data[1][2], m.Data[2][2], m.Data[3][2]},
{m.Data[0][3], m.Data[1][3], m.Data[2][3], m.Data[3][3]},
}
return m
}
// AddMat4 m3 = m1 + m2
func AddMat4(m1, m2 *Mat4) *Mat4 {
return &Mat4{
Data: [4][4]float32{
@ -212,7 +224,7 @@ func AddMat4(m1, m2 *Mat4) *Mat4 {
}
}
//SubMat4 m3 = m1 - m2
// SubMat4 m3 = m1 - m2
func SubMat4(m1, m2 *Mat4) *Mat4 {
return &Mat4{
Data: [4][4]float32{
@ -244,7 +256,7 @@ func SubMat4(m1, m2 *Mat4) *Mat4 {
}
}
//MulMat4 m3 = m1 * m2
// MulMat4 m3 = m1 * m2
func MulMat4(m1, m2 *Mat4) *Mat4 {
m00 := m1.Data[0][0]
@ -297,7 +309,7 @@ func MulMat4(m1, m2 *Mat4) *Mat4 {
}
}
//MulMat4Vec4 v2 = m1 * v1
// MulMat4Vec4 v2 = m1 * v1
func MulMat4Vec4(m1 *Mat4, v1 *Vec4) *Vec4 {
return &Vec4{
Data: [4]float32{
@ -309,7 +321,7 @@ func MulMat4Vec4(m1 *Mat4, v1 *Vec4) *Vec4 {
}
}
//NewMat4Id returns the 4x4 identity matrix
// NewMat4Id returns the 4x4 identity matrix
func NewMat4Id() *Mat4 {
return &Mat4{
Data: [4][4]float32{

View File

@ -182,6 +182,42 @@ func TestMulMat4Vec4(t *testing.T) {
}
}
func TestTransposeMat4(t *testing.T) {
m := gglm.NewMat4Id()
ans := gglm.NewMat4Id()
if !m.Transpose().Transpose().Eq(ans) {
t.Errorf("Got: %v; Expected: %v", m.String(), ans.String())
}
if !m.Transpose().Eq(ans) {
t.Errorf("Got: %v; Expected: %v", m.String(), ans.String())
}
m = &gglm.Mat4{
Data: [4][4]float32{
{00, 01, 02, 03},
{10, 11, 12, 13},
{20, 21, 22, 23},
{30, 31, 32, 33},
},
}
ans = &gglm.Mat4{
Data: [4][4]float32{
{00, 10, 20, 30},
{01, 11, 21, 31},
{02, 12, 22, 32},
{03, 13, 23, 33},
},
}
if !m.Transpose().Eq(ans) {
t.Errorf("Got: %v; Expected: %v", m.String(), ans.String())
}
}
func BenchmarkMulMat4(b *testing.B) {
m1 := gglm.NewMat4Id()

View File

@ -11,12 +11,12 @@ type Quat struct {
Vec4
}
//Eq checks for exact equality
// Eq checks for exact equality
func (q *Quat) Eq(q2 *Quat) bool {
return q.Data == q2.Data
}
//Angle returns the angle represented by this quaternion in radians
// Angle returns the angle represented by this quaternion in radians
func (q *Quat) Angle() float32 {
if Abs32(q.Data[3]) > CosHalf {
@ -31,7 +31,7 @@ func (q *Quat) Angle() float32 {
return Acos32(q.Data[3]) * 2
}
//Axis returns the rotation axis represented by this quaternion
// Axis returns the rotation axis represented by this quaternion
func (q *Quat) Axis() *Vec3 {
var t float32 = 1 - q.Data[3]*q.Data[3]
@ -47,8 +47,8 @@ func (q *Quat) Axis() *Vec3 {
}}
}
//Euler takes rotations in radians and produces a rotation that
//rotates around the z-axis, y-axis and lastly x-axis.
// Euler takes rotations in radians and produces a rotation that
// rotates around the z-axis, y-axis and lastly x-axis.
func NewQuatEuler(v *Vec3) *Quat {
//Some other common terminology: x=roll, y=pitch, z=yaw
@ -74,8 +74,8 @@ func NewQuatEuler(v *Vec3) *Quat {
}
}
//Euler takes rotations in radians and produces a rotation that
//rotates around the z-axis, y-axis and lastly x-axis.
// Euler takes rotations in radians and produces a rotation that
// rotates around the z-axis, y-axis and lastly x-axis.
func NewQuatEulerXYZ(x, y, z float32) *Quat {
//Some other common terminology: x=roll, y=pitch, z=yaw
@ -101,7 +101,7 @@ func NewQuatEulerXYZ(x, y, z float32) *Quat {
}
}
//NewQuatAngleAxis produces a quaternion thats rotates rotRad radians around the *normalized* vector rotAxisNorm
// NewQuatAngleAxis produces a quaternion thats rotates rotRad radians around the *normalized* vector rotAxisNorm
func NewQuatAngleAxis(rotRad float32, rotAxisNorm *Vec3) *Quat {
s, c := Sincos32(rotRad * 0.5)

View File

@ -2,12 +2,12 @@ package gglm
import "math"
//EqF32 true if abs(f1-f2) <= F32Epsilon
// EqF32 true if abs(f1-f2) <= F32Epsilon
func EqF32(f1, f2 float32) bool {
return math.Abs(float64(f1-f2)) <= float64(F32Epsilon)
}
//EqF32Epsilon true if abs(f1-f2) <= eps
// EqF32Epsilon true if abs(f1-f2) <= eps
func EqF32Epsilon(f1, f2, eps float32) bool {
return math.Abs(float64(f1-f2)) <= float64(eps)
}

View File

@ -8,12 +8,12 @@ import (
var _ Mat = &TrMat{}
var _ fmt.Stringer = &TrMat{}
//TrMat represents a transformation matrix
// TrMat represents a transformation matrix
type TrMat struct {
Mat4
}
//Translate adds v to the translation components of the transformation matrix
// Translate adds v to the translation components of the transformation matrix
func (t *TrMat) Translate(v *Vec3) *TrMat {
t.Data[3][0] += v.Data[0]
t.Data[3][1] += v.Data[1]
@ -21,7 +21,7 @@ func (t *TrMat) Translate(v *Vec3) *TrMat {
return t
}
//Scale multiplies the scale components of the transformation matrix by v
// Scale multiplies the scale components of the transformation matrix by v
func (t *TrMat) Scale(v *Vec3) *TrMat {
t.Data[0][0] *= v.Data[0]
t.Data[1][1] *= v.Data[1]
@ -29,7 +29,7 @@ func (t *TrMat) Scale(v *Vec3) *TrMat {
return t
}
//Rotate takes a *normalized* axis and angles in radians to rotate around the given axis
// Rotate takes a *normalized* axis and angles in radians to rotate around the given axis
func (t *TrMat) Rotate(rads float32, axis *Vec3) *TrMat {
s := Sin32(rads)
@ -185,7 +185,7 @@ func LookAtLH(pos, targetPos, worldUp *Vec3) *TrMat {
}
}
//Perspective creates a perspective projection matrix
// Perspective creates a perspective projection matrix
func Perspective(fov, aspectRatio, nearClip, farClip float32) *Mat4 {
halfFovTan := float32(math.Tan(float64(fov * 0.5)))
return &Mat4{
@ -198,7 +198,7 @@ func Perspective(fov, aspectRatio, nearClip, farClip float32) *Mat4 {
}
}
//Perspective creates an orthographic projection matrix
// Perspective creates an orthographic projection matrix
func Ortho(left, right, top, bottom, nearClip, farClip float32) *TrMat {
return &TrMat{
Mat4: Mat4{

View File

@ -86,33 +86,33 @@ func (v *Vec2) String() string {
return fmt.Sprintf("(%f, %f)", v.X(), v.Y())
}
//Scale v *= x (element wise multiplication)
// Scale v *= x (element wise multiplication)
func (v *Vec2) Scale(x float32) *Vec2 {
v.Data[0] *= x
v.Data[1] *= x
return v
}
//Add v += v2
// Add v += v2
func (v *Vec2) Add(v2 *Vec2) *Vec2 {
v.Data[0] += v2.X()
v.Data[1] += v2.Y()
return v
}
//SubVec2 v -= v2
// SubVec2 v -= v2
func (v *Vec2) Sub(v2 *Vec2) *Vec2 {
v.Data[0] -= v2.X()
v.Data[1] -= v2.Y()
return v
}
//Mag returns the magnitude of the vector
// Mag returns the magnitude of the vector
func (v *Vec2) Mag() float32 {
return float32(math.Sqrt(float64(v.X()*v.X() + v.Y()*v.Y())))
}
//Mag returns the squared magnitude of the vector
// Mag returns the squared magnitude of the vector
func (v *Vec2) SqrMag() float32 {
return v.X()*v.X() + v.Y()*v.Y()
}
@ -136,7 +136,7 @@ func (v *Vec2) Clone() *Vec2 {
return &Vec2{Data: v.Data}
}
//AddVec2 v3 = v1 + v2
// AddVec2 v3 = v1 + v2
func AddVec2(v1, v2 *Vec2) *Vec2 {
return &Vec2{
Data: [2]float32{
@ -146,7 +146,7 @@ func AddVec2(v1, v2 *Vec2) *Vec2 {
}
}
//SubVec2 v3 = v1 - v2
// SubVec2 v3 = v1 - v2
func SubVec2(v1, v2 *Vec2) *Vec2 {
return &Vec2{
Data: [2]float32{

View File

@ -132,7 +132,7 @@ func (v *Vec3) String() string {
return fmt.Sprintf("(%f, %f, %f)", v.X(), v.Y(), v.Z())
}
//Scale v *= x (element wise multiplication)
// Scale v *= x (element wise multiplication)
func (v *Vec3) Scale(x float32) *Vec3 {
v.Data[0] *= x
v.Data[1] *= x
@ -148,7 +148,7 @@ func (v *Vec3) Add(v2 *Vec3) *Vec3 {
return v
}
//SubVec3 v -= v2
// SubVec3 v -= v2
func (v *Vec3) Sub(v2 *Vec3) *Vec3 {
v.Data[0] -= v2.X()
v.Data[1] -= v2.Y()
@ -156,12 +156,12 @@ func (v *Vec3) Sub(v2 *Vec3) *Vec3 {
return v
}
//Mag returns the magnitude of the vector
// Mag returns the magnitude of the vector
func (v *Vec3) Mag() float32 {
return float32(math.Sqrt(float64(v.X()*v.X() + v.Y()*v.Y() + v.Z()*v.Z())))
}
//Mag returns the squared magnitude of the vector
// Mag returns the squared magnitude of the vector
func (v *Vec3) SqrMag() float32 {
return v.X()*v.X() + v.Y()*v.Y() + v.Z()*v.Z()
}
@ -176,7 +176,7 @@ func (v *Vec3) Set(x, y, z float32) {
v.Data[2] = z
}
//Normalize normalizes this vector and returns it (doesn't copy)
// Normalize normalizes this vector and returns it (doesn't copy)
func (v *Vec3) Normalize() *Vec3 {
mag := float32(math.Sqrt(float64(v.X()*v.X() + v.Y()*v.Y() + v.Z()*v.Z())))
v.Data[0] /= mag
@ -186,11 +186,30 @@ func (v *Vec3) Normalize() *Vec3 {
return v
}
// RotByQuat rotates this vector by the given quaternion
func (v *Vec3) RotByQuat(q *Quat) *Vec3 {
// Reference: https://gamedev.stackexchange.com/questions/28395/rotating-vector3-by-a-quaternion
// u := NewVec3(q.X(), q.Y(), q.Z())
// t1 := 2.0f * dot(u, v) * u
// t2 := (s*s - dot(u, u)) * v
// t3 := 2.0f * s * cross(u, v);
// vprime = t1 + t2 + t3
u := NewVec3(q.X(), q.Y(), q.Z())
t1 := u.Clone().Scale(2 * DotVec3(u, v))
t2 := v.Clone().Scale(q.W()*q.W() - DotVec3(u, u))
t3 := Cross(u, v).Scale(2 * q.W())
v.Data = t1.Add(t2).Add(t3).Data
return v
}
func (v *Vec3) Clone() *Vec3 {
return &Vec3{Data: v.Data}
}
//AsRad returns a new vector with all values converted to Radians (i.e. multiplied by gglm.Deg2Rad)
// AsRad returns a new vector with all values converted to Radians (i.e. multiplied by gglm.Deg2Rad)
func (v *Vec3) AsRad() *Vec3 {
return &Vec3{
Data: [3]float32{
@ -201,7 +220,7 @@ func (v *Vec3) AsRad() *Vec3 {
}
}
//AddVec3 v3 = v1 + v2
// AddVec3 v3 = v1 + v2
func AddVec3(v1, v2 *Vec3) *Vec3 {
return &Vec3{
Data: [3]float32{
@ -212,7 +231,7 @@ func AddVec3(v1, v2 *Vec3) *Vec3 {
}
}
//SubVec3 v3 = v1 - v2
// SubVec3 v3 = v1 - v2
func SubVec3(v1, v2 *Vec3) *Vec3 {
return &Vec3{
Data: [3]float32{

View File

@ -184,7 +184,7 @@ func (v *Vec4) String() string {
return fmt.Sprintf("(%f, %f, %f, %f)", v.X(), v.Y(), v.Z(), v.W())
}
//Scale v *= x (element wise multiplication)
// Scale v *= x (element wise multiplication)
func (v *Vec4) Scale(x float32) *Vec4 {
v.Data[0] *= x
v.Data[1] *= x
@ -201,7 +201,7 @@ func (v *Vec4) Add(v2 *Vec4) *Vec4 {
return v
}
//SubVec4 v -= v2
// SubVec4 v -= v2
func (v *Vec4) Sub(v2 *Vec4) *Vec4 {
v.Data[0] -= v2.X()
v.Data[1] -= v2.Y()
@ -210,12 +210,12 @@ func (v *Vec4) Sub(v2 *Vec4) *Vec4 {
return v
}
//Mag returns the magnitude of the vector
// Mag returns the magnitude of the vector
func (v *Vec4) Mag() float32 {
return float32(math.Sqrt(float64(v.X()*v.X() + v.Y()*v.Y() + v.Z()*v.Z() + v.W()*v.W())))
}
//Mag returns the squared magnitude of the vector
// Mag returns the squared magnitude of the vector
func (v *Vec4) SqrMag() float32 {
return v.X()*v.X() + v.Y()*v.Y() + v.Z()*v.Z() + v.Z()*v.Z()
}
@ -243,7 +243,7 @@ func (v *Vec4) Clone() *Vec4 {
return &Vec4{Data: v.Data}
}
//AddVec4 v3 = v1 + v2
// AddVec4 v3 = v1 + v2
func AddVec4(v1, v2 *Vec4) *Vec4 {
return &Vec4{
Data: [4]float32{
@ -255,7 +255,7 @@ func AddVec4(v1, v2 *Vec4) *Vec4 {
}
}
//SubVec4 v3 = v1 - v2
// SubVec4 v3 = v1 - v2
func SubVec4(v1, v2 *Vec4) *Vec4 {
return &Vec4{
Data: [4]float32{

View File

@ -191,6 +191,29 @@ func TestVecSwizzleSet(t *testing.T) {
t.Errorf("Got: %v; Expected: %v", v3.String(), ans3.String())
}
// Test AngleVec3
v3 = gglm.NewVec3(1, 0, 0)
v32 := gglm.NewVec3(1, 0, 0)
angleV3 := gglm.AngleVec3(v3, v32) * gglm.Rad2Deg
if angleV3 != 0 {
t.Errorf("Got: %v; Expected: %v", v3.String(), 0)
}
v32.SetXY(0, 1)
angleV3 = gglm.AngleVec3(v3, v32) * gglm.Rad2Deg
if angleV3 != 90 {
t.Errorf("Got: %v; Expected: %v", v3.String(), 0)
}
// Test rot by quat
v32.SetXY(1, 0)
v32.RotByQuat(gglm.NewQuatAngleAxis(90*gglm.Deg2Rad, gglm.NewVec3(0, 1, 0)))
angleV3 = gglm.AngleVec3(v3, v32) * gglm.Rad2Deg
if angleV3 != 90 {
t.Errorf("Got: %v; Expected: %v", v3.String(), 0)
}
//Vec4
v4 := gglm.NewVec4(1, 1, 1, 1)
ans4 := gglm.NewVec4(1, 2, 3, 4)

30
main.go
View File

@ -8,7 +8,7 @@ import (
func main() {
//Mat3
// Mat3
m1 := &gglm.Mat3{
Data: [3][3]float32{
{1, 4, 7},
@ -30,7 +30,7 @@ func main() {
println(m1.String())
println(m3.String())
//Mat4
// Mat4
m4 := &gglm.Mat4{
Data: [4][4]float32{
{1, 5, 9, 13},
@ -55,7 +55,7 @@ func main() {
println(m6.String())
println(m4.Eq(m6))
//Vec2
// Vec2
v1 := &gglm.Vec2{Data: [2]float32{1, 2}}
v2 := &gglm.Vec2{Data: [2]float32{3, 4}}
println(gglm.DistVec2(v1, v2))
@ -71,7 +71,7 @@ func main() {
v1.Normalize()
println("V1 Normal: " + v1.String())
//Vec3
// Vec3
v3 := &gglm.Vec3{Data: [3]float32{1, 2, 3}}
v4 := &gglm.Vec3{Data: [3]float32{4, 5, 6}}
println(gglm.DistVec3(v3, v4))
@ -88,7 +88,7 @@ func main() {
v3.Normalize()
println("V3 Normal: " + v3.String())
//Vec4
// Vec4
v5 := &gglm.Vec4{Data: [4]float32{1, 2, 3, 4}}
v6 := &gglm.Vec4{Data: [4]float32{5, 6, 7, 8}}
println(gglm.DistVec4(v5, v6))
@ -108,7 +108,7 @@ func main() {
v6.Normalize()
println("V6 Normal: " + v6.String())
//Mat2Vec2
// Mat2Vec2
mat2A := gglm.Mat2{
Data: [2][2]float32{
{1, 3},
@ -119,7 +119,7 @@ func main() {
vec2A := gglm.Vec2{Data: [2]float32{1, 2}}
println(gglm.MulMat2Vec2(&mat2A, &vec2A).String())
//Mat3Vec3
// Mat3Vec3
mat3A := gglm.Mat3{
Data: [3][3]float32{
{1, 4, 7},
@ -132,13 +132,13 @@ func main() {
mm3v3 := gglm.MulMat3Vec3(&mat3A, &vec3A)
println(mm3v3.String())
//ReflectVec2
// ReflectVec2
vec2B := &gglm.Vec2{Data: [2]float32{4, 5}}
normA := &gglm.Vec2{Data: [2]float32{0, 1}}
rVec2A := gglm.ReflectVec2(vec2B, normA)
println(rVec2A.String())
//Quaternion
// Quaternion
vRot := &gglm.Vec3{Data: [3]float32{60, 30, 20}}
q := gglm.NewQuatEuler(vRot.AsRad())
println("\n" + vRot.AsRad().String())
@ -148,7 +148,7 @@ func main() {
println("\n" + vRot.Normalize().String())
println(q.String())
//Transform
// Transform
translationMat := gglm.NewTranslationMat(&gglm.Vec3{Data: [3]float32{1, 2, 3}})
rotMat := gglm.NewRotMat(gglm.NewQuatEuler(gglm.NewVec3(60, 30, 20).AsRad()))
scaleMat := gglm.NewScaleMat(gglm.NewVec3(1, 1, 1))
@ -158,32 +158,32 @@ func main() {
println("\n\n\n", modelMat.String())
//Clone Vec2
// Clone Vec2
v2Orig := gglm.Vec2{Data: [2]float32{1, 2}}
v2Clone := v2Orig.Clone()
v2Clone.SetX(99)
println("\n\n", v2Orig.String(), "; ", v2Clone.String())
//Clone TrMat
// Clone TrMat
trMatOrig := gglm.NewTranslationMat(gglm.NewVec3(1, 2, 3))
trMatClone := trMatOrig.Clone()
trMatClone.Scale(gglm.NewVec3(2, 2, 2))
trMatClone.Translate(gglm.NewVec3(9, 0, 0))
println("\n\n", trMatOrig.String(), "; ", trMatClone.String())
//Quat geo
// Quat geo
q1 := gglm.NewQuatEuler(gglm.NewVec3(180, 0, 0).AsRad())
q2 := gglm.NewQuatEuler(gglm.NewVec3(0, 180, 0).AsRad())
println(gglm.AngleQuat(q1, q2) * gglm.Rad2Deg)
//LookAt
// LookAt
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())
//Mat2Col
// Mat2Col
mc := gglm.NewMat2Id()
println("===============================")
println(mc.String())