-
Notifications
You must be signed in to change notification settings - Fork 18
/
sortingTriangles.go
156 lines (127 loc) · 3.77 KB
/
sortingTriangles.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package tetra3d
// sortingTriangle is used specifically for sorting triangles when rendering. Less data means more data fits in cache,
// which means sorting is faster.
type sortingTriangle struct {
// Triangle *Triangle
TriangleID int
depth float32
vertexIndices []int
}
type sortingTriangleBin struct {
triangles []sortingTriangle
triangleIndex int
}
type sortingTriangleBucket struct {
bins []sortingTriangleBin
unsetTris []sortingTriangle
unsetTriIndex int
sortMode int
}
func newSortingTriangleBucket() *sortingTriangleBucket {
bucket := &sortingTriangleBucket{}
return bucket
}
func (s *sortingTriangleBucket) AddTriangle(triID int, depth float32, vertexIndices []int) {
s.unsetTris[s.unsetTriIndex].TriangleID = triID
s.unsetTris[s.unsetTriIndex].depth = depth
s.unsetTris[s.unsetTriIndex].vertexIndices = vertexIndices
s.unsetTriIndex++
}
func (s *sortingTriangleBucket) Sort(minRange, maxRange float32) {
binCount := len(s.bins)
rangeDiff := maxRange - minRange
if rangeDiff == 0 {
maxRange += 0.001
rangeDiff += 0.001
}
for i := 0; i < s.unsetTriIndex; i++ {
targetBin := 0
if s.sortMode != TriangleSortModeNone && binCount > 1 {
depth := (s.unsetTris[i].depth - minRange) / rangeDiff * float32(binCount)
t := clamp(depth, 0, float32(binCount-1))
targetBin = int(t)
}
s.bins[targetBin].triangles[s.bins[targetBin].triangleIndex].depth = s.unsetTris[i].depth
s.bins[targetBin].triangles[s.bins[targetBin].triangleIndex].TriangleID = s.unsetTris[i].TriangleID
s.bins[targetBin].triangles[s.bins[targetBin].triangleIndex].vertexIndices = s.unsetTris[i].vertexIndices
s.bins[targetBin].triangleIndex++
}
}
func (s *sortingTriangleBucket) Initialize(binCount int) {
s.bins = make([]sortingTriangleBin, binCount)
for i := range s.bins {
s.bins[i].triangles = make([]sortingTriangle, MaxTriangleCount)
}
s.unsetTris = make([]sortingTriangle, MaxTriangleCount)
s.Clear()
}
func (s *sortingTriangleBucket) Clear() {
for i := 0; i < len(s.bins); i++ {
s.bins[i].triangleIndex = 0
}
s.unsetTriIndex = 0
}
func (s *sortingTriangleBucket) ForEach(forEach func(triIndex, triID int, vertexIndices []int)) {
if s.IsEmpty() {
return
}
triIndex := 0
if s.sortMode == TriangleSortModeBackToFront {
for binIndex := len(s.bins) - 1; binIndex >= 0; binIndex-- {
bin := s.bins[binIndex]
for ti := range bin.triangles {
if ti >= bin.triangleIndex {
break
}
forEach(triIndex, bin.triangles[ti].TriangleID, bin.triangles[ti].vertexIndices)
triIndex++
}
}
} else {
for binIndex := 0; binIndex < len(s.bins); binIndex++ {
bin := s.bins[binIndex]
for ti := range bin.triangles {
if ti >= bin.triangleIndex {
break
}
forEach(triIndex, bin.triangles[ti].TriangleID, bin.triangles[ti].vertexIndices)
triIndex++
}
}
}
}
// func (s *sortingTriangleBucket) ForEach(forEach func(triIndex, triID int)) {
// if s.IsEmpty() {
// return
// }
// triIndex := 0
// if s.sortMode == TriangleSortModeBackToFront {
// for binIndex := len(s.bins) - 1; binIndex >= 0; binIndex-- {
// for ti, tri := range s.bins[binIndex].triangles {
// // fmt.Println("bin index:", i, len(s.bins))
// if ti >= s.bins[binIndex].triangleIndex {
// break
// }
// forEach(triIndex, tri.TriangleID)
// triIndex++
// }
// }
// } else {
// for i := 0; i < len(s.bins); i++ {
// for ti, tri := range s.bins[i].triangles {
// if ti >= s.bins[i].triangleIndex {
// break
// }
// forEach(triIndex, tri.TriangleID)
// triIndex++
// }
// }
// }
// }
func (s *sortingTriangleBucket) IsEmpty() bool {
return s.unsetTriIndex == 0
}
var globalSortingTriangleBucket = newSortingTriangleBucket()
func init() {
globalSortingTriangleBucket.Initialize(512)
}