forked from flywave/ofbx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.go
128 lines (109 loc) · 2.37 KB
/
math.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package ofbx
import (
"fmt"
"math"
"github.com/oakmound/oak/v2/alg/floatgeom"
)
// UpVector specifies which canonical axis represents up in the system (typically Y or Z).
type UpVector int
// UpVector Options
const (
UpVectorX UpVector = 1
UpVectorY UpVector = 2
UpVectorZ UpVector = 3
)
// FrontVector is a vector with origin at the screen pointing toward the camera.
type FrontVector int
// FrontVector Parity Options
const (
FrontVectorParityEven FrontVector = 1
FrontVectorParityOdd FrontVector = 2
)
// CoordSystem specifies the third vector of the system.
type CoordSystem int
// CoordSystem options
const (
CoordSystemRight CoordSystem = iota
CoordSystemLeft CoordSystem = iota
)
// Matrix is a 16 sized slice that we operate on as if it was actually a matrix
type Matrix struct {
m [16]float64 // last 4 are translation
}
func (mat *Matrix) ToArray() [16]float64 {
return mat.m
}
func matrixFromSlice(fs []float64) (Matrix, error) {
if len(fs) != 16 {
return Matrix{}, fmt.Errorf("Expected 16 values, got %d", len(fs))
}
var a [16]float64
copy(a[:], fs)
return Matrix{a}, nil
}
// Quat probably can bve removed
type Quat struct {
X, Y, Z, w float64
}
// Mul multiplies the values of two matricies together and returns the output
func (m1 Matrix) Mul(m2 Matrix) Matrix {
res := [16]float64{}
for j := 0; j < 4; j++ {
for i := 0; i < 4; i++ {
tmp := 0.0
for k := 0; k < 4; k++ {
tmp += m1.m[i+k*4] * m2.m[k+j*4]
}
res[i+j*4] = tmp
}
}
return Matrix{res}
}
func setTranslation(v floatgeom.Point3, m *Matrix) {
m.m[12] = v.X()
m.m[13] = v.Y()
m.m[14] = v.Z()
}
func makeIdentity() Matrix {
return Matrix{[16]float64{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}}
}
func rotationX(angle float64) Matrix {
m2 := makeIdentity()
//radian
c := math.Cos(angle)
s := math.Sin(angle)
m2.m[5] = c
m2.m[10] = c
m2.m[9] = -s
m2.m[6] = s
return m2
}
func rotationY(angle float64) Matrix {
m2 := makeIdentity()
//radian
c := math.Cos(angle)
s := math.Sin(angle)
m2.m[0] = c
m2.m[10] = c
m2.m[8] = s
m2.m[2] = -s
return m2
}
func rotationZ(angle float64) Matrix {
m2 := makeIdentity()
//radian
c := math.Cos(angle)
s := math.Sin(angle)
m2.m[0] = c
m2.m[5] = c
m2.m[4] = -s
m2.m[1] = s
return m2
}
func getTriCountFromPoly(indices []int, idx int) (int, int) {
count := 1
for indices[idx+1+count] >= 0 {
count++
}
return count, idx
}