-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfrustum.go
176 lines (160 loc) · 4.5 KB
/
frustum.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
package math
var (
clipSpacePlanePoints = []Vector3{
Vec3(-1, -1, -1),
Vec3(1, -1, -1),
Vec3(1, 1, -1),
Vec3(-1, 1, -1),
Vec3(-1, -1, 1),
Vec3(1, -1, 1),
Vec3(1, 1, 1),
Vec3(-1, 1, 1)}
)
// A truncated rectangular pyramid.
// Used to define the viewable region and it's projection onto the screen.
type Frustum struct {
// The six clipping planes, near, far, left, right, top, bottom
Left, Right *Plane
Top, Bottom *Plane
Near, Far *Plane
planePoints []Vector3
}
func NewFrustum() *Frustum {
zeroPlane := NewPlane(Vec3(0, 0, 0), 0)
return &Frustum{Left: zeroPlane, Right: zeroPlane.Cpy(),
Top: zeroPlane.Cpy(), Bottom: zeroPlane.Cpy(),
Near: zeroPlane.Cpy(), Far: zeroPlane.Cpy(),
planePoints: make([]Vector3, len(clipSpacePlanePoints))}
}
func (f *Frustum) Update(invProjectionView *Matrix4) {
/* TODO
for i := range clipSpacePlanePoints {
f.planePoints[i] = invProjectionView.Project(clipSpacePlanePoints[i])
}
*/
f.Near.Set(f.planePoints[1], f.planePoints[0], f.planePoints[2])
f.Far.Set(f.planePoints[4], f.planePoints[5], f.planePoints[7])
f.Left.Set(f.planePoints[0], f.planePoints[4], f.planePoints[3])
f.Right.Set(f.planePoints[5], f.planePoints[1], f.planePoints[6])
f.Top.Set(f.planePoints[2], f.planePoints[3], f.planePoints[6])
f.Bottom.Set(f.planePoints[4], f.planePoints[0], f.planePoints[1])
}
// Returns whether the point is in the frustum.
func (f *Frustum) PointInFrustum(point Vector3) bool {
if f.Left.PlaneSide(point) == PlaneSide_Back {
return false
}
if f.Right.PlaneSide(point) == PlaneSide_Back {
return false
}
if f.Top.PlaneSide(point) == PlaneSide_Back {
return false
}
if f.Bottom.PlaneSide(point) == PlaneSide_Back {
return false
}
if f.Near.PlaneSide(point) == PlaneSide_Back {
return false
}
if f.Far.PlaneSide(point) == PlaneSide_Back {
return false
}
return true
}
// Returns whether the given sphere is in the frustum.
func (f *Frustum) SphereInFrustum(center Vector3, radius float32) bool {
if (f.Left.Normal.X*center.X + f.Left.Normal.Y*center.Y + f.Left.Normal.Z*center.Z) < (-radius - f.Left.D) {
return false
}
if (f.Right.Normal.X*center.X + f.Right.Normal.Y*center.Y + f.Right.Normal.Z*center.Z) < (-radius - f.Right.D) {
return false
}
if (f.Top.Normal.X*center.X + f.Top.Normal.Y*center.Y + f.Top.Normal.Z*center.Z) < (-radius - f.Top.D) {
return false
}
if (f.Bottom.Normal.X*center.X + f.Bottom.Normal.Y*center.Y + f.Bottom.Normal.Z*center.Z) < (-radius - f.Bottom.D) {
return false
}
if (f.Near.Normal.X*center.X + f.Near.Normal.Y*center.Y + f.Near.Normal.Z*center.Z) < (-radius - f.Near.D) {
return false
}
if (f.Far.Normal.X*center.X + f.Far.Normal.Y*center.Y + f.Far.Normal.Z*center.Z) < (-radius - f.Far.D) {
return false
}
return true
}
// Returns whether the given sphere is in the frustum not checking whether it is behind the near and far clipping plane.
func (f *Frustum) SphereInFrustumWithoutNearFar(center Vector3, radius float32) bool {
if (f.Left.Normal.X*center.X + f.Left.Normal.Y*center.Y + f.Left.Normal.Z*center.Z) < (-radius - f.Left.D) {
return false
}
if (f.Right.Normal.X*center.X + f.Right.Normal.Y*center.Y + f.Right.Normal.Z*center.Z) < (-radius - f.Right.D) {
return false
}
if (f.Top.Normal.X*center.X + f.Top.Normal.Y*center.Y + f.Top.Normal.Z*center.Z) < (-radius - f.Top.D) {
return false
}
if (f.Bottom.Normal.X*center.X + f.Bottom.Normal.Y*center.Y + f.Bottom.Normal.Z*center.Z) < (-radius - f.Bottom.D) {
return false
}
return true
}
// Returns whether the given {@link BoundingBox} is in the frustum.
func (f *Frustum) BoundsInFrustum(bounds *BoundingBox) bool {
corners := bounds.Corners()
out := 0
for i := 0; i < len(corners); i++ {
if f.Left.PlaneSide(corners[i]) == PlaneSide_Back {
out++
}
}
if out == 8 {
return false
}
out = 0
for i := 0; i < len(corners); i++ {
if f.Right.PlaneSide(corners[i]) == PlaneSide_Back {
out++
}
}
if out == 8 {
return false
}
out = 0
for i := 0; i < len(corners); i++ {
if f.Top.PlaneSide(corners[i]) == PlaneSide_Back {
out++
}
}
if out == 8 {
return false
}
out = 0
for i := 0; i < len(corners); i++ {
if f.Bottom.PlaneSide(corners[i]) == PlaneSide_Back {
out++
}
}
if out == 8 {
return false
}
out = 0
for i := 0; i < len(corners); i++ {
if f.Near.PlaneSide(corners[i]) == PlaneSide_Back {
out++
}
}
if out == 8 {
return false
}
out = 0
for i := 0; i < len(corners); i++ {
if f.Far.PlaneSide(corners[i]) == PlaneSide_Back {
out++
}
}
if out == 8 {
return false
}
return true
}