Vec2 struct

Vec2 represents a 2D vector.

Fields:

  • X (float32)
  • Y (float32)

Methods:

Add

Add adds two vectors component-wise.


Parameters:
  • o Vec2

Returns:
  • Vec2

References:


Show/Hide Method Body
{ return Vec2{v.X + o.X, v.Y + o.Y} }

Normalize

Normalize returns the unit vector in the same direction.


Returns:
  • Vec2

References:


Show/Hide Method Body
{
	l := float32(stdmath.Sqrt(float64(v.X*v.X + v.Y*v.Y)))
	if l == 0 {
		return Vec2{}
	}
	return Vec2{v.X / l, v.Y / l}
}

Vec3 struct

Vec3 represents a 3D vector.

Fields:

  • X (float32)
  • Y (float32)
  • Z (float32)

Methods:

Add

Add adds two Vec3 values component-wise.


Parameters:
  • o Vec3

Returns:
  • Vec3

References:


Show/Hide Method Body
{ return Vec3{v.X + o.X, v.Y + o.Y, v.Z + o.Z} }

Normalize

Normalize returns the unit vector of v.


Returns:
  • Vec3

References:


Show/Hide Method Body
{
	l := float32(stdmath.Sqrt(float64(v.X*v.X + v.Y*v.Y + v.Z*v.Z)))
	if l == 0 {
		return Vec3{}
	}
	return Vec3{v.X / l, v.Y / l, v.Z / l}
}

Mat4 type

Mat4 represents a 4x4 matrix in column-major order.

Type Definition:

[16]float32

Identity function

Identity returns an identity matrix.

Returns:

  • Mat4

References:

Show/Hide Function Body
{
	return Mat4{
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		0, 0, 0, 1,
	}
}

Translation function

Translation returns a matrix translating by v.

Parameters:

  • v Vec3

Returns:

  • Mat4

References:

Show/Hide Function Body
{
	m := Identity()
	m[12], m[13], m[14] = v.X, v.Y, v.Z
	return m
}

Scale function

Scale returns a scaling matrix for v.

Parameters:

  • v Vec3

Returns:

  • Mat4

References:

Show/Hide Function Body
{
	m := Identity()
	m[0], m[5], m[10] = v.X, v.Y, v.Z
	return m
}

Perspective function

Perspective returns a perspective projection matrix.

Parameters:

  • fovY float32
  • aspect float32
  • near float32
  • far float32

Returns:

  • Mat4

References:

Show/Hide Function Body
{
	f := float32(1 / stdmath.Tan(float64(fovY)/2))
	var m Mat4
	m[0] = f / aspect
	m[5] = f
	m[10] = (far + near) / (near - far)
	m[11] = -1
	m[14] = (2 * far * near) / (near - far)
	return m
}

Orthographic function

Orthographic returns an orthographic projection matrix.

Parameters:

  • left float32
  • right float32
  • bottom float32
  • top float32
  • near float32
  • far float32

Returns:

  • Mat4

References:

Show/Hide Function Body
{
	var m Mat4
	m[0] = 2 / (right - left)
	m[5] = 2 / (top - bottom)
	m[10] = -2 / (far - near)
	m[12] = -(right + left) / (right - left)
	m[13] = -(top + bottom) / (top - bottom)
	m[14] = -(far + near) / (far - near)
	m[15] = 1
	return m
}

almostEqual function

Parameters:

  • a float32
  • b float32

Returns:

  • bool
Show/Hide Function Body
{ return stdmath.Abs(float64(a-b)) < 1e-5 }

TestVecOperations function

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	v := Vec2{1, 2}.Add(Vec2{3, 4})
	if v.X != 4 || v.Y != 6 {
		t.Fatalf("unexpected sum: %+v", v)
	}
	n := Vec3{3, 4, 0}.Normalize()
	if !almostEqual(n.X, 0.6) || !almostEqual(n.Y, 0.8) || !almostEqual(n.Z, 0) {
		t.Fatalf("unexpected normalization: %+v", n)
	}
}

TestMat4Multiply function

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	a := Translation(Vec3{1, 2, 3})
	b := Scale(Vec3{2, 2, 2})
	r := a.Mul(b)
	exp := Mat4{
		2, 0, 0, 0,
		0, 2, 0, 0,
		0, 0, 2, 0,
		1, 2, 3, 1,
	}
	for i := 0; i < 16; i++ {
		if !almostEqual(r[i], exp[i]) {
			t.Fatalf("matrix mismatch at %d: got %v want %v", i, r[i], exp[i])
		}
	}
}

math import

Import example:

import "math"

Imported as:

stdmath

math import

Import example:

import "math"

Imported as:

stdmath

testing import

Import example:

import "testing"