Formatting

This commit is contained in:
bloeys
2024-05-01 01:25:10 +04:00
parent 4eb59e3386
commit da81ee79d9
12 changed files with 78 additions and 78 deletions

View File

@ -1,6 +1,6 @@
# gglm # 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: gglm currently has the following:
@ -26,7 +26,7 @@ import "github.com/bloeys/gglm/gglm"
func main() { func main() {
//Vec2 // Vec2
v1 := &gglm.Vec2{Data: [2]float32{1, 2}} v1 := &gglm.Vec2{Data: [2]float32{1, 2}}
v2 := &gglm.Vec2{Data: [2]float32{3, 4}} v2 := &gglm.Vec2{Data: [2]float32{3, 4}}
println(gglm.DistVec2(v1, v2)) println(gglm.DistVec2(v1, v2))
@ -35,8 +35,8 @@ func main() {
v2.Set(1, 2) v2.Set(1, 2)
println(v1.Eq(v2)) println(v1.Eq(v2))
//This performs: v1 += v2 // This performs: v1 += v2
//v1 is returned from the function, so we can chain calls that operate on v1 // v1 is returned from the function, so we can chain calls that operate on v1
newX := v1.Add(v2).X() newX := v1.Add(v2).X()
println("newX:", newX) println("newX:", newX)

View File

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

View File

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

View File

@ -24,7 +24,7 @@ func (m *Mat2) Size() MatSize {
} }
func (m *Mat2) String() string { 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]) 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]} return &Vec2{Data: m.Data[c]}
} }
//Add m += m2 // Add m += m2
func (m *Mat2) Add(m2 *Mat2) *Mat2 { func (m *Mat2) Add(m2 *Mat2) *Mat2 {
m.Data[0][0] += m2.Data[0][0] m.Data[0][0] += m2.Data[0][0]
m.Data[0][1] += m2.Data[0][1] m.Data[0][1] += m2.Data[0][1]
@ -41,7 +41,7 @@ func (m *Mat2) Add(m2 *Mat2) *Mat2 {
return m return m
} }
//Add m -= m2 // Add m -= m2
func (m *Mat2) Sub(m2 *Mat2) *Mat2 { func (m *Mat2) Sub(m2 *Mat2) *Mat2 {
m.Data[0][0] -= m2.Data[0][0] m.Data[0][0] -= m2.Data[0][0]
m.Data[0][1] -= m2.Data[0][1] m.Data[0][1] -= m2.Data[0][1]
@ -50,7 +50,7 @@ func (m *Mat2) Sub(m2 *Mat2) *Mat2 {
return m return m
} }
//Mul m *= m2 // Mul m *= m2
func (m1 *Mat2) Mul(m2 *Mat2) *Mat2 { func (m1 *Mat2) Mul(m2 *Mat2) *Mat2 {
m1.Data = [2][2]float32{ m1.Data = [2][2]float32{
{ {
@ -66,7 +66,7 @@ func (m1 *Mat2) Mul(m2 *Mat2) *Mat2 {
return m1 return m1
} }
//Scale m *= x (element wise multiplication) // Scale m *= x (element wise multiplication)
func (m *Mat2) Scale(x float32) *Mat2 { func (m *Mat2) Scale(x float32) *Mat2 {
m.Data[0][0] *= x m.Data[0][0] *= x
m.Data[0][1] *= x m.Data[0][1] *= x
@ -83,7 +83,7 @@ func (m *Mat2) Eq(m2 *Mat2) bool {
return m.Data == m2.Data return m.Data == m2.Data
} }
//AddMat2 m3 = m1 + m2 // AddMat2 m3 = m1 + m2
func AddMat2(m1, m2 *Mat2) *Mat2 { func AddMat2(m1, m2 *Mat2) *Mat2 {
return &Mat2{ return &Mat2{
Data: [2][2]float32{ Data: [2][2]float32{
@ -99,7 +99,7 @@ func AddMat2(m1, m2 *Mat2) *Mat2 {
} }
} }
//SubMat2 m3 = m1 - m2 // SubMat2 m3 = m1 - m2
func SubMat2(m1, m2 *Mat2) *Mat2 { func SubMat2(m1, m2 *Mat2) *Mat2 {
return &Mat2{ return &Mat2{
Data: [2][2]float32{ Data: [2][2]float32{
@ -115,7 +115,7 @@ func SubMat2(m1, m2 *Mat2) *Mat2 {
} }
} }
//MulMat2 m3 = m1 * m2 // MulMat2 m3 = m1 * m2
func MulMat2(m1, m2 *Mat2) *Mat2 { func MulMat2(m1, m2 *Mat2) *Mat2 {
return &Mat2{ return &Mat2{
Data: [2][2]float32{ Data: [2][2]float32{
@ -131,7 +131,7 @@ func MulMat2(m1, m2 *Mat2) *Mat2 {
} }
} }
//MulMat2Vec2 v2 = m1 * v1 // MulMat2Vec2 v2 = m1 * v1
func MulMat2Vec2(m1 *Mat2, v1 *Vec2) *Vec2 { func MulMat2Vec2(m1 *Mat2, v1 *Vec2) *Vec2 {
return &Vec2{ return &Vec2{
Data: [2]float32{ Data: [2]float32{
@ -141,7 +141,7 @@ func MulMat2Vec2(m1 *Mat2, v1 *Vec2) *Vec2 {
} }
} }
//NewMat2Id returns the 2x2 identity matrix // NewMat2Id returns the 2x2 identity matrix
func NewMat2Id() *Mat2 { func NewMat2Id() *Mat2 {
return &Mat2{ return &Mat2{
Data: [2][2]float32{ Data: [2][2]float32{

View File

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

View File

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

View File

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

View File

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

View File

@ -8,12 +8,12 @@ import (
var _ Mat = &TrMat{} var _ Mat = &TrMat{}
var _ fmt.Stringer = &TrMat{} var _ fmt.Stringer = &TrMat{}
//TrMat represents a transformation matrix // TrMat represents a transformation matrix
type TrMat struct { type TrMat struct {
Mat4 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 { func (t *TrMat) Translate(v *Vec3) *TrMat {
t.Data[3][0] += v.Data[0] t.Data[3][0] += v.Data[0]
t.Data[3][1] += v.Data[1] t.Data[3][1] += v.Data[1]
@ -21,7 +21,7 @@ func (t *TrMat) Translate(v *Vec3) *TrMat {
return t 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 { func (t *TrMat) Scale(v *Vec3) *TrMat {
t.Data[0][0] *= v.Data[0] t.Data[0][0] *= v.Data[0]
t.Data[1][1] *= v.Data[1] t.Data[1][1] *= v.Data[1]
@ -29,7 +29,7 @@ func (t *TrMat) Scale(v *Vec3) *TrMat {
return t 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 { func (t *TrMat) Rotate(rads float32, axis *Vec3) *TrMat {
s := Sin32(rads) 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 { func Perspective(fov, aspectRatio, nearClip, farClip float32) *Mat4 {
halfFovTan := float32(math.Tan(float64(fov * 0.5))) halfFovTan := float32(math.Tan(float64(fov * 0.5)))
return &Mat4{ 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 { func Ortho(left, right, top, bottom, nearClip, farClip float32) *TrMat {
return &TrMat{ return &TrMat{
Mat4: Mat4{ Mat4: Mat4{

View File

@ -86,33 +86,33 @@ func (v *Vec2) String() string {
return fmt.Sprintf("(%f, %f)", v.X(), v.Y()) 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 { func (v *Vec2) Scale(x float32) *Vec2 {
v.Data[0] *= x v.Data[0] *= x
v.Data[1] *= x v.Data[1] *= x
return v return v
} }
//Add v += v2 // Add v += v2
func (v *Vec2) Add(v2 *Vec2) *Vec2 { func (v *Vec2) Add(v2 *Vec2) *Vec2 {
v.Data[0] += v2.X() v.Data[0] += v2.X()
v.Data[1] += v2.Y() v.Data[1] += v2.Y()
return v return v
} }
//SubVec2 v -= v2 // SubVec2 v -= v2
func (v *Vec2) Sub(v2 *Vec2) *Vec2 { func (v *Vec2) Sub(v2 *Vec2) *Vec2 {
v.Data[0] -= v2.X() v.Data[0] -= v2.X()
v.Data[1] -= v2.Y() v.Data[1] -= v2.Y()
return v return v
} }
//Mag returns the magnitude of the vector // Mag returns the magnitude of the vector
func (v *Vec2) Mag() float32 { func (v *Vec2) Mag() float32 {
return float32(math.Sqrt(float64(v.X()*v.X() + v.Y()*v.Y()))) 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 { func (v *Vec2) SqrMag() float32 {
return v.X()*v.X() + v.Y()*v.Y() return v.X()*v.X() + v.Y()*v.Y()
} }
@ -136,7 +136,7 @@ func (v *Vec2) Clone() *Vec2 {
return &Vec2{Data: v.Data} return &Vec2{Data: v.Data}
} }
//AddVec2 v3 = v1 + v2 // AddVec2 v3 = v1 + v2
func AddVec2(v1, v2 *Vec2) *Vec2 { func AddVec2(v1, v2 *Vec2) *Vec2 {
return &Vec2{ return &Vec2{
Data: [2]float32{ 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 { func SubVec2(v1, v2 *Vec2) *Vec2 {
return &Vec2{ return &Vec2{
Data: [2]float32{ Data: [2]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()) 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 { func (v *Vec4) Scale(x float32) *Vec4 {
v.Data[0] *= x v.Data[0] *= x
v.Data[1] *= x v.Data[1] *= x
@ -201,7 +201,7 @@ func (v *Vec4) Add(v2 *Vec4) *Vec4 {
return v return v
} }
//SubVec4 v -= v2 // SubVec4 v -= v2
func (v *Vec4) Sub(v2 *Vec4) *Vec4 { func (v *Vec4) Sub(v2 *Vec4) *Vec4 {
v.Data[0] -= v2.X() v.Data[0] -= v2.X()
v.Data[1] -= v2.Y() v.Data[1] -= v2.Y()
@ -210,12 +210,12 @@ func (v *Vec4) Sub(v2 *Vec4) *Vec4 {
return v return v
} }
//Mag returns the magnitude of the vector // Mag returns the magnitude of the vector
func (v *Vec4) Mag() float32 { 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()))) 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 { func (v *Vec4) SqrMag() float32 {
return v.X()*v.X() + v.Y()*v.Y() + v.Z()*v.Z() + v.Z()*v.Z() 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} return &Vec4{Data: v.Data}
} }
//AddVec4 v3 = v1 + v2 // AddVec4 v3 = v1 + v2
func AddVec4(v1, v2 *Vec4) *Vec4 { func AddVec4(v1, v2 *Vec4) *Vec4 {
return &Vec4{ return &Vec4{
Data: [4]float32{ 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 { func SubVec4(v1, v2 *Vec4) *Vec4 {
return &Vec4{ return &Vec4{
Data: [4]float32{ Data: [4]float32{

30
main.go
View File

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