-
Notifications
You must be signed in to change notification settings - Fork 4
/
clipping.go
236 lines (201 loc) · 5.32 KB
/
clipping.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"sync"
)
const (
// maxClipPoints is the maximum number of vertices a polygon can have.
// 9 is the worst case scenario for a triangle clipped against all 6 planes.
maxClipPoints = 9
)
const (
PlaneLeft = iota
PlaneRight
PlaneTop
PlaneBottom
PlaneNear
PlaneFar
)
const (
BoxVisibilityOutside = iota
BoxVisibilityIntersect
BoxVisibilityInside
)
type Polygon struct {
Intensity [maxClipPoints]float32
Points [maxClipPoints]Vec4
UVs [maxClipPoints]UV
Count int
}
func (p *Polygon) AddVertex(v Vec4, uv UV, intensity float32) {
p.Intensity[p.Count] = intensity
p.Points[p.Count] = v
p.UVs[p.Count] = uv
p.Count++
}
func (p *Polygon) Triangulate(
points *[maxClipPoints][3]Vec4,
uvs *[maxClipPoints][3]UV,
intensity *[maxClipPoints][3]float32,
) (numOut int) {
if p.Count < 3 {
return 0
}
// The resulting polygon is convex, so we can always triangulate it by connecting
// the first vertex with the second and the third, then the first vertex with the
// third and the fourth, and so on (fan triangulation).
for i := 0; i < p.Count-2; i++ {
intensity[numOut] = [3]float32{p.Intensity[0], p.Intensity[i+1], p.Intensity[i+2]}
points[numOut] = [3]Vec4{p.Points[0], p.Points[i+1], p.Points[i+2]}
uvs[numOut] = [3]UV{p.UVs[0], p.UVs[i+1], p.UVs[i+2]}
numOut++
}
return numOut
}
type Plane struct {
Point Vec4
Normal Vec4
}
func (p *Plane) DistanceToVertex(v Vec4) float32 {
return p.Normal.DotProduct(v) - p.Normal.DotProduct(p.Point)
}
// IsVertexInside tells if a point is inside or outside the plane.
func (p *Plane) IsVertexInside(q Vec4) bool {
return q.Sub(p.Point).DotProduct(p.Normal) <= 0
}
// Intersect returns a point between q0 and q1 intersect with the plane.
func (p *Plane) Intersect(q0, q1 Vec4) (Vec4, float32) {
u := q1.Sub(q0)
w := q0.Sub(p.Point)
d := p.Normal.DotProduct(u)
n := -p.Normal.DotProduct(w)
factor := n / d // interpolation factor
return q0.Add(u.Multiply(factor)), factor
}
type Frustum struct {
Planes [6]Plane
polygonPool *sync.Pool // *Polygon
}
func NewFrustum(zNear, zFar float32) *Frustum {
polygonPool := &sync.Pool{
New: func() any {
return &Polygon{}
},
}
return &Frustum{
Planes: [6]Plane{
PlaneLeft: {
Point: Vec4{-1, 0, 0, 1},
Normal: Vec4{1, 0, 0, 1},
},
PlaneRight: {
Point: Vec4{1, 0, 0, 1},
Normal: Vec4{-1, 0, 0, 1},
},
PlaneTop: {
Point: Vec4{0, -1, 0, 1},
Normal: Vec4{0, 1, 0, 1},
},
PlaneBottom: {
Point: Vec4{0, 1, 0, 1},
Normal: Vec4{0, -1, 0, 1},
},
PlaneNear: {
Point: Vec4{0, 0, zNear, 1},
Normal: Vec4{0, 0, -1, 0},
},
PlaneFar: {
Point: Vec4{0, 0, zFar, 1},
Normal: Vec4{0, 0, 1, 0},
},
},
polygonPool: polygonPool,
}
}
func (f *Frustum) BoxVisibility(bbox *[8]Vec4) int {
for i := range f.Planes {
outside := 0
for _, point := range bbox {
if f.Planes[i].DistanceToVertex(point) > 0 {
outside++
}
}
// All points are outside, the box is not visible
if outside == len(bbox) {
return BoxVisibilityOutside
}
// Some points are outside, clipping is needed
if outside > 0 {
return BoxVisibilityIntersect
}
}
return BoxVisibilityInside
}
func lerpUV(a, b UV, factor float32) UV {
return UV{
U: a.U + (b.U-a.U)*factor,
V: a.V + (b.V-a.V)*factor,
}
}
func lerp32(a, b float32, factor float32) float32 {
return a + (b-a)*factor
}
func (f *Frustum) ClipTriangle(
pointsIn *[3]Vec4,
uvsIn *[3]UV,
intensityIn *[3]float32,
pointsOut *[maxClipPoints][3]Vec4,
uvsOut *[maxClipPoints][3]UV,
intensityOut *[maxClipPoints][3]float32,
) (numOut int) {
polygon := f.polygonPool.Get().(*Polygon)
defer f.polygonPool.Put(polygon)
polygon.Count = 0
polygon2 := f.polygonPool.Get().(*Polygon)
defer f.polygonPool.Put(polygon2)
polygon2.Count = 0
polygon.AddVertex(pointsIn[0], uvsIn[0], intensityIn[0])
polygon.AddVertex(pointsIn[1], uvsIn[1], intensityIn[1])
polygon.AddVertex(pointsIn[2], uvsIn[2], intensityIn[2])
planes := []int{
PlaneLeft,
PlaneRight,
PlaneTop,
PlaneBottom,
PlaneNear,
PlaneFar,
}
// Iterate over each plane of the frustum and build a polygon from the input
// triangle, containing only the vertices that are inside the frustum.
for _, pi := range planes {
plane := &f.Planes[pi]
polygon2.Count = 0
// Iterate over each edge of the polygon
for b := 0; b < polygon.Count; b++ {
a := (b + 1) % polygon.Count
uvA, uvB := polygon.UVs[a], polygon.UVs[b]
vertA, vertB := polygon.Points[a], polygon.Points[b]
intensityA, intensityB := polygon.Intensity[a], polygon.Intensity[b]
if plane.IsVertexInside(vertA) {
if !plane.IsVertexInside(vertB) {
intersect, factor := plane.Intersect(vertA, vertB)
intensity := lerp32(intensityA, intensityB, factor)
uv := lerpUV(uvA, uvB, factor)
polygon2.AddVertex(intersect, uv, intensity)
}
polygon2.AddVertex(vertA, uvA, intensityA)
} else if plane.IsVertexInside(vertB) {
intersect, factor := plane.Intersect(vertA, vertB)
intensity := lerp32(intensityA, intensityB, factor)
uv := lerpUV(uvA, uvB, factor)
polygon2.AddVertex(intersect, uv, intensity)
}
}
// All vertices are outside
if polygon2.Count == 0 {
return 0
}
polygon, polygon2 = polygon2, polygon
}
// Convert the polygon back to triangles
return polygon.Triangulate(pointsOut, uvsOut, intensityOut)
}