Documentation ¶
Overview ¶
Package geom provides vector and matrix types suitable for OpenGL programming: Vec2, Vec3 and Mat4.
Using vectors:
v := geom.V3(2, 1.5, 0.5) w := geom.V3(-1, 0, 1) fmt.Println("3*(v+w) =", v.Add(w).Mul(3))
Using matrices:
// A 4x4 zero matrix var a geom.Mat4 // Another 4x4 matrix b := geom.Mat4{ {0, 1, 2.3, 3}, {4, 0.5, 6, 7}, {8, -9, 10, 11}, {12, 13, 14, 15}, } // Copy elements from b to a a = b // Set a matrix element a[2][3] = 5 // Multiply a and b, store the result in a. a.Mul(&a, &b)
Index ¶
- Variables
- func Deg(rad float32) float32
- func Rad(deg float32) float32
- type Mat4
- func (m *Mat4) Det() float32
- func (m *Mat4) Floats() *[16]float32
- func (m *Mat4) Frustum(left, right, bottom, top, near, far float32) *Mat4
- func (m *Mat4) ID() *Mat4
- func (m *Mat4) LookAt(eye, center, up Vec3) *Mat4
- func (m *Mat4) Mul(a *Mat4, b *Mat4) *Mat4
- func (m *Mat4) Ortho(left, right, bottom, top, near, far float32) *Mat4
- func (m *Mat4) Perspective(fovy, aspect, near, far float32) *Mat4
- func (m *Mat4) Rot(a *Mat4, angle float32, axis Vec3) *Mat4
- func (m *Mat4) Scale(a *Mat4, v Vec3) *Mat4
- func (m *Mat4) T(a *Mat4) *Mat4
- func (m *Mat4) Translate(a *Mat4, v Vec3) *Mat4
- func (m *Mat4) Zero() *Mat4
- type Size
- type Vec2
- func (v Vec2) Add(w Vec2) Vec2
- func (v Vec2) CompDiv(w Vec2) Vec2
- func (v Vec2) CompMul(w Vec2) Vec2
- func (v Vec2) CrossLen(w Vec2) float32
- func (v Vec2) Dist(w Vec2) float32
- func (v Vec2) Div(s float32) Vec2
- func (v Vec2) Dot(w Vec2) float32
- func (v Vec2) Len() float32
- func (v Vec2) Lerp(w Vec2, t float32) Vec2
- func (v Vec2) Max(w Vec2) Vec2
- func (v Vec2) Min(w Vec2) Vec2
- func (v Vec2) Mul(s float32) Vec2
- func (v Vec2) NearEq(w Vec2) bool
- func (v Vec2) Neg() Vec2
- func (v Vec2) Norm() Vec2
- func (v Vec2) Reflect(n Vec2) Vec2
- func (v Vec2) SqDist(w Vec2) float32
- func (v Vec2) SqLen() float32
- func (v Vec2) String() string
- func (v Vec2) Sub(w Vec2) Vec2
- func (v Vec2) Transform(m *Mat4) Vec2
- func (v Vec2) Z(z float32) Vec3
- type Vec3
- func (v Vec3) Add(w Vec3) Vec3
- func (v Vec3) CompDiv(w Vec3) Vec3
- func (v Vec3) CompMul(w Vec3) Vec3
- func (v Vec3) Cross(w Vec3) Vec3
- func (v Vec3) Dist(w Vec3) float32
- func (v Vec3) Div(s float32) Vec3
- func (v Vec3) Dot(w Vec3) float32
- func (v Vec3) Len() float32
- func (v Vec3) Lerp(w Vec3, t float32) Vec3
- func (v Vec3) Max(w Vec3) Vec3
- func (v Vec3) Min(w Vec3) Vec3
- func (v Vec3) Mul(s float32) Vec3
- func (v Vec3) NearEq(w Vec3) bool
- func (v Vec3) Neg() Vec3
- func (v Vec3) Norm() Vec3
- func (v Vec3) Reflect(n Vec3) Vec3
- func (v Vec3) SqDist(w Vec3) float32
- func (v Vec3) SqLen() float32
- func (v Vec3) String() string
- func (v Vec3) Sub(w Vec3) Vec3
- func (v Vec3) Transform(m *Mat4) Vec3
Constants ¶
This section is empty.
Variables ¶
var ( // V2Zero is the zero vector (0,0). V2Zero = Vec2{0, 0} // V2Unit is the unit vector (1,1). V2Unit = Vec2{1, 1} // V2UnitX is the x-axis unit vector (1,0). V2UnitX = Vec2{1, 0} // V2UnitY is the y-axis unit vector (0,1). V2UnitY = Vec2{0, 1} )
var ( // V3Zero is the zero vector (0,0,0). V3Zero = Vec3{0, 0, 0} // V3Unit is the unit vector (1,1,1). V3Unit = Vec3{1, 1, 1} // V3UnitX is the x-axis unit vector (1,0,0). V3UnitX = Vec3{1, 0, 0} // V3UnitY is the y-axis unit vector (0,1,0). V3UnitY = Vec3{0, 1, 0} // V3UnitZ is the z-axis unit vector (0,0,1). V3UnitZ = Vec3{0, 0, 1} )
Functions ¶
Types ¶
type Mat4 ¶
type Mat4 [4][4]float32
A Mat4 represents a 4x4 matrix. The indices are [row][column].
func (*Mat4) Floats ¶
Floats returns a pointer to the matrix elements represented as a flat array of float32 numbers in row-major order. Changing an element value of this array will affect m and vice versa.
func (*Mat4) Frustum ¶
Frustum sets m to a frustum matrix with the given clipping planes and returns m.
func (*Mat4) LookAt ¶
LookAt sets m to a viewing matrix given an eye point, a reference point indicating the center of the scene and an up vector, and returns m.
func (*Mat4) Ortho ¶
Ortho sets m to an orthographic projection matrix with the given clipping planes and returns m.
func (*Mat4) Perspective ¶
Perspective sets m to a perspective matrix with the given vertical field of view angle (in radians), aspect ratio, near and far bounds of the frustum, and returns m.
func (*Mat4) Rot ¶
Rot sets m to the rotation of matrix a by the given angle in radians around the given axis, and returns m.
func (*Mat4) Scale ¶
Scale sets m to the scaling of matrix a by the scale factors of v and returns m.
type Size ¶
type Size struct {
// Width and height
W, H float32
}
A Size represents the dimensions of a rectangle.
type Vec2 ¶
type Vec2 struct {
X, Y float32
}
A Vec2 represents a vector with coordinates X and Y in 2-dimensional euclidian space.
func (Vec2) CrossLen ¶
CrossLen returns the length that the cross product of v and w would have in 3-dimensional euclidian space. This is effectively the Z component of the 3D cross product vector.
func (Vec2) Lerp ¶
Lerp returns the linear interpolation between v and w by amount t. The amount t is usually a value between 0 and 1. If t=0 v will be returned; if t=1 w will be returned.
func (Vec2) Max ¶
Max returns a vector with each component set to the greater value of the corresponding component pair of v and w.
func (Vec2) Min ¶
Min returns a vector with each component set to the lesser value of the corresponding component pair of v and w.
func (Vec2) NearEq ¶
NearEq returns whether v and w are approximately equal. This relation is not transitive in general. The tolerance for the floating-point components is ±1e-5.
type Vec3 ¶
type Vec3 struct {
X, Y, Z float32
}
A Vec3 represents a vector with coordinates X, Y and Z in 3-dimensional euclidian space.
func (Vec3) Lerp ¶
Lerp returns the linear interpolation between v and w by amount t. The amount t is usually a value between 0 and 1. If t=0 v will be returned; if t=1 w will be returned.
func (Vec3) Max ¶
Max returns a vector with each component set to the greater value of the corresponding component pair of v and w.
func (Vec3) Min ¶
Min returns a vector with each component set to the lesser value of the corresponding component pair of v and w.
func (Vec3) NearEq ¶
NearEq returns whether v and w are approximately equal. This relation is not transitive in general. The tolerance for the floating-point components is ±1e-5.