Added geo and gl packages from gogeline and improved documentation.

This commit is contained in:
Marvin Blum
2016-05-03 21:23:50 +02:00
parent 05013370fd
commit 19731dc0e5
15 changed files with 1388 additions and 3 deletions

171
geo/mat3.go Normal file
View File

@@ -0,0 +1,171 @@
package geo
import (
"math"
)
// 3x3 column major matrix.
type Mat3 struct {
Values [9]float64
}
// Creates a new 3x3 matrix with initial values.
func NewMat3(m00, m10, m20, m01, m11, m21, m02, m12, m22 float64) *Mat3 {
return &Mat3{[9]float64{m00, m10, m20, m01, m11, m21, m02, m12, m22}}
}
// Creates a copy of actual matrix.
func (m *Mat3) Copy() *Mat3 {
return &Mat3{m.Values}
}
// Sets the matrix to zeros.
func (m *Mat3) Clear() {
for i := range m.Values {
m.Values[i] = 0
}
}
// Sets the matrix to identity matrix.
func (m *Mat3) Identity() {
m.Clear()
m.Values[0] = 1
m.Values[4] = 1
m.Values[8] = 1
}
// Multiplies actual matrix with given matrix and saves result.
func (m *Mat3) Mult(mat *Mat3) {
this := m.Copy()
m.Values[0] = mat.Values[0]*this.Values[0] + mat.Values[1]*this.Values[3] + mat.Values[2]*this.Values[6]
m.Values[1] = mat.Values[0]*this.Values[1] + mat.Values[1]*this.Values[4] + mat.Values[2]*this.Values[7]
m.Values[2] = mat.Values[0]*this.Values[2] + mat.Values[1]*this.Values[5] + mat.Values[2]*this.Values[8]
m.Values[3] = mat.Values[3]*this.Values[0] + mat.Values[4]*this.Values[3] + mat.Values[5]*this.Values[6]
m.Values[4] = mat.Values[3]*this.Values[1] + mat.Values[4]*this.Values[4] + mat.Values[5]*this.Values[7]
m.Values[5] = mat.Values[3]*this.Values[2] + mat.Values[4]*this.Values[5] + mat.Values[5]*this.Values[8]
m.Values[6] = mat.Values[6]*this.Values[0] + mat.Values[7]*this.Values[3] + mat.Values[8]*this.Values[6]
m.Values[7] = mat.Values[6]*this.Values[1] + mat.Values[7]*this.Values[4] + mat.Values[8]*this.Values[7]
m.Values[8] = mat.Values[6]*this.Values[2] + mat.Values[7]*this.Values[5] + mat.Values[8]*this.Values[8]
}
// Multiplies given vector with actual matrix and returns result.
func (m *Mat3) MultVec(v Vec3) Vec3 {
vec := Vec3{}
vec.X = m.Values[0]*v.X + m.Values[3]*v.X + m.Values[6]*v.X
vec.Y = m.Values[1]*v.Y + m.Values[4]*v.Y + m.Values[7]*v.Y
vec.Z = m.Values[2]*v.Z + m.Values[5]*v.Z + m.Values[8]*v.Z
return vec
}
// Returns the determinate of actual matrix.
func (m *Mat3) Determinate() float64 {
var d float64
d = m.Values[0]*m.Values[4]*m.Values[8] + m.Values[3]*m.Values[7]*m.Values[2] + m.Values[6]*m.Values[1]*m.Values[5]
d -= m.Values[2]*m.Values[4]*m.Values[6] - m.Values[5]*m.Values[7]*m.Values[0] - m.Values[8]*m.Values[1]*m.Values[3]
return d
}
// Sets the inverse of actual matrix.
func (m *Mat3) Inverse() {
d := 1 / m.Determinate()
mat := m.Copy()
m.Values[0] = (mat.Values[4]*mat.Values[8] - mat.Values[7]*mat.Values[5]) * d
m.Values[1] = (mat.Values[7]*mat.Values[2] - mat.Values[1]*mat.Values[8]) * d
m.Values[2] = (mat.Values[1]*mat.Values[5] - mat.Values[4]*mat.Values[2]) * d
m.Values[3] = (mat.Values[6]*mat.Values[5] - mat.Values[3]*mat.Values[8]) * d
m.Values[4] = (mat.Values[0]*mat.Values[8] - mat.Values[6]*mat.Values[2]) * d
m.Values[5] = (mat.Values[3]*mat.Values[2] - mat.Values[0]*mat.Values[5]) * d
m.Values[6] = (mat.Values[3]*mat.Values[7] - mat.Values[6]*mat.Values[4]) * d
m.Values[7] = (mat.Values[6]*mat.Values[1] - mat.Values[0]*mat.Values[7]) * d
m.Values[8] = (mat.Values[0]*mat.Values[4] - mat.Values[3]*mat.Values[1]) * d
}
// Calculates and saves the transpose of actual matrix.
func (m *Mat3) Transpose() {
mat := m.Copy()
m.Values[1] = mat.Values[3]
m.Values[2] = mat.Values[6]
m.Values[3] = mat.Values[1]
m.Values[5] = mat.Values[7]
m.Values[6] = mat.Values[2]
m.Values[7] = mat.Values[5]
}
// Translates and saves actual matrix by given vector.
func (m *Mat3) Translate(v Vec2) {
mat := &Mat3{}
mat.Identity()
mat.Values[6] = v.X
mat.Values[7] = v.Y
m.Mult(mat)
}
// Scales and saves actual matrix by given vector.
func (m *Mat3) Scale(v Vec2) {
mat := &Mat3{}
mat.Identity()
mat.Values[0] = v.X
mat.Values[4] = v.Y
m.Mult(mat)
}
// Rotates and saves actual matrix by given vector.
func (m *Mat3) Rotate(angle float64) {
mat := &Mat3{}
var co, si float64
angle = angle * (math.Pi / 180)
si = math.Sin(angle)
co = math.Cos(angle)
mat.Values[0] = co
mat.Values[1] = si
mat.Values[2] = 0
mat.Values[3] = -si
mat.Values[4] = co
mat.Values[5] = 0
mat.Values[6] = 0
mat.Values[7] = 0
mat.Values[8] = 1
m.Mult(mat)
}
// Sets actual matrix to orthogonal projection with given viewport.
func (m *Mat3) Ortho(viewport Vec4) {
if viewport.X != viewport.Z && viewport.Y != viewport.W {
m.Identity()
m.Values[0] = 2 / (viewport.Z - viewport.X)
m.Values[4] = 2 / (viewport.W - viewport.Y)
m.Values[6] = -(viewport.Z + viewport.X) / (viewport.Z - viewport.X)
m.Values[7] = -(viewport.W + viewport.Y) / (viewport.W - viewport.Y)
m.Values[8] = 1
}
}
// Multiplies both matrices and returns a new Mat3.
func MultMat3(a, b *Mat3) *Mat3 {
c := a.Copy()
c.Mult(b)
return c
}

255
geo/mat4.go Normal file
View File

@@ -0,0 +1,255 @@
package geo
import (
"math"
)
// 4x4 column major matrix.
type Mat4 struct {
Values [16]float64
}
// Creates a new 4x4 matrix with initial values.
func NewMat4(m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32, m03, m13, m23, m33 float64) *Mat4 {
return &Mat4{[16]float64{m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32, m03, m13, m23, m33}}
}
// Creates a copy of actual matrix.
func (m *Mat4) Copy() *Mat4 {
return &Mat4{m.Values}
}
// Sets the matrix to zeros.
func (m *Mat4) Clear() {
for i := range m.Values {
m.Values[i] = 0
}
}
// Sets the matrix to identity matrix.
func (m *Mat4) Identity() {
m.Clear()
m.Values[0] = 1
m.Values[5] = 1
m.Values[10] = 1
m.Values[15] = 1
}
// Multiplies actual matrix with given matrix and saves result.
func (m *Mat4) Mult(mat *Mat4) {
this := m.Copy()
m.Values[0] = mat.Values[0]*this.Values[0] + mat.Values[1]*this.Values[4] + mat.Values[2]*this.Values[8] + mat.Values[3]*this.Values[12]
m.Values[1] = mat.Values[0]*this.Values[1] + mat.Values[1]*this.Values[5] + mat.Values[2]*this.Values[9] + mat.Values[3]*this.Values[13]
m.Values[2] = mat.Values[0]*this.Values[2] + mat.Values[1]*this.Values[6] + mat.Values[2]*this.Values[10] + mat.Values[3]*this.Values[14]
m.Values[3] = mat.Values[0]*this.Values[3] + mat.Values[1]*this.Values[7] + mat.Values[2]*this.Values[11] + mat.Values[3]*this.Values[15]
m.Values[4] = mat.Values[4]*this.Values[0] + mat.Values[5]*this.Values[4] + mat.Values[6]*this.Values[8] + mat.Values[7]*this.Values[12]
m.Values[5] = mat.Values[4]*this.Values[1] + mat.Values[5]*this.Values[5] + mat.Values[6]*this.Values[9] + mat.Values[7]*this.Values[13]
m.Values[6] = mat.Values[4]*this.Values[2] + mat.Values[5]*this.Values[6] + mat.Values[6]*this.Values[10] + mat.Values[7]*this.Values[14]
m.Values[7] = mat.Values[4]*this.Values[3] + mat.Values[5]*this.Values[7] + mat.Values[6]*this.Values[11] + mat.Values[7]*this.Values[15]
m.Values[8] = mat.Values[8]*this.Values[0] + mat.Values[9]*this.Values[4] + mat.Values[10]*this.Values[8] + mat.Values[11]*this.Values[12]
m.Values[9] = mat.Values[8]*this.Values[1] + mat.Values[9]*this.Values[5] + mat.Values[10]*this.Values[9] + mat.Values[11]*this.Values[13]
m.Values[10] = mat.Values[8]*this.Values[2] + mat.Values[9]*this.Values[6] + mat.Values[10]*this.Values[10] + mat.Values[11]*this.Values[14]
m.Values[11] = mat.Values[8]*this.Values[3] + mat.Values[9]*this.Values[7] + mat.Values[10]*this.Values[11] + mat.Values[11]*this.Values[15]
m.Values[12] = mat.Values[12]*this.Values[0] + mat.Values[13]*this.Values[4] + mat.Values[14]*this.Values[8] + mat.Values[15]*this.Values[12]
m.Values[13] = mat.Values[12]*this.Values[1] + mat.Values[13]*this.Values[5] + mat.Values[14]*this.Values[9] + mat.Values[15]*this.Values[13]
m.Values[14] = mat.Values[12]*this.Values[2] + mat.Values[13]*this.Values[6] + mat.Values[14]*this.Values[10] + mat.Values[15]*this.Values[14]
m.Values[15] = mat.Values[12]*this.Values[3] + mat.Values[13]*this.Values[7] + mat.Values[14]*this.Values[11] + mat.Values[15]*this.Values[15]
}
// Multiplies given vector with actual matrix and returns result.
func (m *Mat4) MultVec(v Vec3) Vec3 {
vec := Vec3{}
vec.X = m.Values[0]*v.X + m.Values[4]*v.X + m.Values[8]*v.X + m.Values[12]*v.X
vec.Y = m.Values[1]*v.Y + m.Values[5]*v.Y + m.Values[9]*v.Y + m.Values[13]*v.Y
vec.Z = m.Values[2]*v.Z + m.Values[6]*v.Z + m.Values[10]*v.Z + m.Values[14]*v.Z
return vec
}
// Returns the determinate of actual matrix.
func (m *Mat4) Determinate() float64 {
var d float64
d = m.Values[0]*m.Values[4]*m.Values[8] + m.Values[1]*m.Values[5]*m.Values[6] + m.Values[2]*m.Values[3]*m.Values[7]
d -= m.Values[2]*m.Values[4]*m.Values[6] + m.Values[0]*m.Values[5]*m.Values[7] + m.Values[1]*m.Values[3]*m.Values[8]
return d
}
// Sets the inverse of actual matrix.
func (m *Mat4) Inverse() {
mat := m.Copy()
m.Values[0] = mat.Values[0]
m.Values[1] = mat.Values[4]
m.Values[2] = mat.Values[8]
m.Values[4] = mat.Values[1]
m.Values[6] = mat.Values[9]
m.Values[8] = mat.Values[2]
m.Values[9] = mat.Values[6]
m.Values[12] = m.Values[0]*-mat.Values[12] + m.Values[4]*-mat.Values[13] + m.Values[8]*-mat.Values[14]
m.Values[13] = m.Values[1]*-mat.Values[12] + m.Values[5]*-mat.Values[13] + m.Values[9]*-mat.Values[14]
m.Values[14] = m.Values[2]*-mat.Values[12] + m.Values[6]*-mat.Values[13] + m.Values[10]*-mat.Values[14]
m.Values[3] = 0
m.Values[7] = 0
m.Values[11] = 0
m.Values[15] = 1
}
// Calculates and saves the transpose of actual matrix.
func (m *Mat4) Transpose() {
mat := m.Copy()
m.Values[1] = mat.Values[4]
m.Values[2] = mat.Values[8]
m.Values[3] = mat.Values[12]
m.Values[4] = mat.Values[1]
m.Values[6] = mat.Values[9]
m.Values[7] = mat.Values[13]
m.Values[8] = mat.Values[2]
m.Values[9] = mat.Values[6]
m.Values[11] = mat.Values[14]
m.Values[12] = mat.Values[3]
m.Values[13] = mat.Values[2]
m.Values[14] = mat.Values[11]
}
// Translates and saves actual matrix by given vector.
func (m *Mat4) Translate(v Vec3) {
mat := Mat4{}
mat.Identity()
mat.Values[12] = v.X
mat.Values[13] = v.Y
mat.Values[14] = v.Z
m.Mult(&mat)
}
// Scales and saves actual matrix by given vector.
func (m *Mat4) Scale(v Vec3) {
mat := Mat4{}
mat.Identity()
mat.Values[0] = v.X
mat.Values[5] = v.Y
mat.Values[10] = v.Z
m.Mult(&mat)
}
// Rotates and saves actual matrix by given vector.
func (m *Mat4) Rotate(angle float64, axis Vec3) {
mat := Mat4{}
var co, si float64
axis.Normalize()
angle = angle * (math.Pi / 180)
si = float64(math.Sin(float64(angle)))
co = float64(math.Cos(float64(angle)))
mat.Values[0] = axis.X*axis.X*(1-co) + co
mat.Values[1] = axis.Y*axis.X*(1-co) + axis.Z*si
mat.Values[2] = axis.X*axis.Z*(1-co) - axis.Y*si
mat.Values[3] = 0
mat.Values[4] = axis.X*axis.Y*(1-co) - axis.Z*si
mat.Values[5] = axis.Y*axis.Y*(1-co) + co
mat.Values[6] = axis.Y*axis.Z*(1-co) + axis.X*si
mat.Values[7] = 0
mat.Values[8] = axis.X*axis.Z*(1-co) + axis.Y*si
mat.Values[9] = axis.Y*axis.Z*(1-co) - axis.X*si
mat.Values[10] = axis.Z*axis.Z*(1-co) + co
mat.Values[11] = 0
mat.Values[12] = 0
mat.Values[13] = 0
mat.Values[14] = 0
mat.Values[15] = 1
m.Mult(&mat)
}
// Sets actual matrix to orthogonal projection with given viewport.
func (m *Mat4) Ortho(viewport Vec4, znear, zfar float64) {
if viewport.X != viewport.Z && viewport.Y != viewport.W && znear != zfar {
m.Identity()
m.Values[0] = 2 / (viewport.Z - viewport.X)
m.Values[5] = 2 / (viewport.W - viewport.Y)
m.Values[10] = -2 / (zfar - znear)
m.Values[12] = -(viewport.Z + viewport.X) / (viewport.Z - viewport.X)
m.Values[13] = -(viewport.W + viewport.Y) / (viewport.W - viewport.Y)
m.Values[14] = -(zfar + znear) / (zfar - znear)
m.Values[15] = 1
}
}
// Sets actual matrix to project to specified locations with given upper axis.
func (m *Mat4) LookAt(pos, lookAt, up Vec3) {
dir := lookAt.Copy()
dir.Sub(pos)
dir.Normalize()
right := CrossVec3(dir, up)
right.Normalize()
up = CrossVec3(right, dir)
up.Normalize()
mat := Mat4{}
mat.Values[0] = right.X
mat.Values[4] = right.Y
mat.Values[8] = right.Z
mat.Values[12] = -right.DotVec(pos)
mat.Values[1] = up.X
mat.Values[5] = up.Y
mat.Values[9] = up.Z
mat.Values[13] = -up.DotVec(pos)
mat.Values[2] = -dir.X
mat.Values[6] = -dir.Y
mat.Values[10] = -dir.Z
mat.Values[14] = dir.DotVec(pos)
mat.Values[3] = 0
mat.Values[7] = 0
mat.Values[11] = 0
mat.Values[15] = 1
m.Mult(&mat)
}
// Sets actual matrix to perspective projection with given viewport.
func (m *Mat4) Perspective(fov, ratio, znear, zfar float64) {
f := 1 / math.Tan(float64(fov*(math.Pi/360)))
m.Identity()
m.Values[0] = f / ratio
m.Values[5] = f
m.Values[10] = (zfar + znear) / (znear - zfar)
m.Values[11] = -1
m.Values[14] = (2 * zfar * znear) / (znear - zfar)
m.Values[15] = 0
}
// Multiplies both matrices and returns a new Mat4.
func MultMat4(a, b *Mat4) *Mat4 {
c := a.Copy()
c.Mult(b)
return c
}

20
geo/util.go Normal file
View File

@@ -0,0 +1,20 @@
package geo
import (
"math"
)
// Returns the distance between two 2D vectors.
func DistanceVec2(a, b Vec2) float64 {
return math.Sqrt(float64((a.X-b.X)*(a.X-b.X) + (a.Y-b.Y)*(a.Y-b.Y)))
}
// Returns the distance between two 3D vectors.
func DistanceVec3(a, b Vec3) float64 {
return math.Sqrt(float64((a.X-b.X)*(a.X-b.X) + (a.Y-b.Y)*(a.Y-b.Y) + (a.Z-b.Z)*(a.Z-b.Z)))
}
// Returns the distance between two 4D vectors.
func DistanceVec4(a, b Vec4) float64 {
return math.Sqrt(float64((a.X-b.X)*(a.X-b.X) + (a.Y-b.Y)*(a.Y-b.Y) + (a.Z-b.Z)*(a.Z-b.Z) + (a.W-b.W)*(a.W-b.W)))
}

62
geo/vec2.go Normal file
View File

@@ -0,0 +1,62 @@
package geo
import (
"math"
)
// A 2D vector.
type Vec2 struct {
X, Y float64
}
// Creates a copy of actual vector.
func (v *Vec2) Copy() Vec2 {
return Vec2{v.X, v.Y}
}
// Adds and saves the given vector to actual vector.
func (v *Vec2) Add(vec Vec2) {
v.X += vec.X
v.Y += vec.Y
}
// Subtracts and saves the given vector to actual vector.
func (v *Vec2) Sub(vec Vec2) {
v.X -= vec.X
v.Y -= vec.Y
}
// Multiplies and saves the given vector to actual vector.
func (v *Vec2) Mult(vec Vec2) {
v.X *= vec.X
v.Y *= vec.Y
}
// Divides and saves the given vector to actual vector.
func (v *Vec2) Div(vec Vec2) {
v.X /= vec.X
v.Y /= vec.Y
}
// Calculates and returns the dot product of actual vector.
func (v *Vec2) Dot() float64 {
return v.X*v.X + v.Y*v.Y
}
// Calculates and returns the dot product of combination of given vector and actual vector.
func (v *Vec2) DotVec(vec Vec2) float64 {
return v.X*vec.X + v.Y*vec.Y
}
// Returns the length of actual vector.
func (v *Vec2) Length() float64 {
return float64(math.Sqrt(float64(v.Dot())))
}
// Normalizes actual vector to length 1.
func (v *Vec2) Normalize() {
l := v.Length()
v.X /= l
v.Y /= l
}

87
geo/vec3.go Normal file
View File

@@ -0,0 +1,87 @@
package geo
import (
"math"
)
// A 3D vector.
type Vec3 struct {
X, Y, Z float64
}
// Creates a copy of actual vector.
func (v *Vec3) Copy() Vec3 {
return Vec3{v.X, v.Y, v.Z}
}
// Adds and saves the given vector to actual vector.
func (v *Vec3) Add(vec Vec3) {
v.X += vec.X
v.Y += vec.Y
v.Z += vec.Z
}
// Subtracts and saves the given vector to actual vector.
func (v *Vec3) Sub(vec Vec3) {
v.X -= vec.X
v.Y -= vec.Y
v.Z -= vec.Z
}
// Multiplies and saves the given vector to actual vector.
func (v *Vec3) Mult(vec Vec3) {
v.X *= vec.X
v.Y *= vec.Y
v.Z *= vec.Z
}
// Divides and saves the given vector to actual vector.
func (v *Vec3) Div(vec Vec3) {
v.X /= vec.X
v.Y /= vec.Y
v.Z /= vec.Z
}
// Calculates and returns the dot product of actual vector.
func (v *Vec3) Dot() float64 {
return v.X*v.X + v.Y*v.Y + v.Z*v.Z
}
// Calculates and returns the dot product of combination of given vector and actual vector.
func (v *Vec3) DotVec(vec Vec3) float64 {
return v.X*vec.X + v.Y*vec.Y + v.Z*vec.Z
}
// Returns the length of actual vector.
func (v *Vec3) Length() float64 {
return math.Sqrt(v.Dot())
}
// Normalizes actual vector to length 1.
func (v *Vec3) Normalize() {
l := v.Length()
v.X /= l
v.Y /= l
v.Z /= l
}
// Calculates and saves cross product of given and actual vector.
func (v *Vec3) Cross(vec Vec3) {
this := Vec3{v.X, v.Y, v.Z}
v.X = this.Y*vec.Z - this.Z*vec.Y
v.Y = this.Z*vec.X - this.X*vec.Z
v.Z = this.X*vec.Y - this.Y*vec.X
}
// Calulates the cross product of given vectors and returns result as a new vector.
func CrossVec3(a, b Vec3) Vec3 {
vec := Vec3{}
vec.X = (a.Y * b.Z) - (a.Z * b.Y)
vec.Y = (a.Z * b.X) - (a.X * b.Z)
vec.Z = (a.X * b.Y) - (a.Y * b.X)
return vec
}

72
geo/vec4.go Normal file
View File

@@ -0,0 +1,72 @@
package geo
import (
"math"
)
// A 4D vector.
type Vec4 struct {
X, Y, Z, W float64
}
// Creates a copy of actual vector.
func (v *Vec4) Copy() Vec4 {
return Vec4{v.X, v.Y, v.Z, v.W}
}
// Adds and saves the given vector to actual vector.
func (v *Vec4) Add(vec Vec4) {
v.X += vec.X
v.Y += vec.Y
v.Z += vec.Z
v.W += vec.W
}
// Subracts and saves the given vector to actual vector.
func (v *Vec4) Sub(vec Vec4) {
v.X -= vec.X
v.Y -= vec.Y
v.Z -= vec.Z
v.W -= vec.W
}
// Multiplies and saves the given vector to actual vector.
func (v *Vec4) Mult(vec Vec4) {
v.X *= vec.X
v.Y *= vec.Y
v.Z *= vec.Z
v.W *= vec.W
}
// Divides and saves the given vector to actual vector.
func (v *Vec4) Div(vec Vec4) {
v.X /= vec.X
v.Y /= vec.Y
v.Z /= vec.Z
v.W /= vec.W
}
// Calculates and returns the dot product of actual vector.
func (v *Vec4) Dot() float64 {
return v.X*v.X + v.Y*v.Y + v.Z*v.Z + v.W*v.W
}
// Calculates and returns the dot product of combination of given vector and actual vector.
func (v *Vec4) DotVec(vec Vec4) float64 {
return v.X*vec.X + v.Y*vec.Y + v.Z*vec.Z + v.W*vec.W
}
// Returns the length of actual vector.
func (v *Vec4) Length() float64 {
return math.Sqrt(v.Dot())
}
// Normalizes actual vector to length 1.
func (v *Vec4) Normalize() {
l := v.Length()
v.X /= l
v.Y /= l
v.Z /= l
v.W /= l
}