forked from VDrift/vdrift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
frustumcull.h
169 lines (140 loc) · 5.01 KB
/
frustumcull.h
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
/************************************************************************/
/* */
/* This file is part of VDrift. */
/* */
/* VDrift is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* VDrift is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with VDrift. If not, see <http://www.gnu.org/licenses/>. */
/* */
/************************************************************************/
#ifndef _FRUSTUM_CULL_H
#define _FRUSTUM_CULL_H
#include <cmath>
// Cull sphere against frustum planes
// plane normals are pointing into frustum
template <typename T4, typename T3, typename T>
static inline bool FrustumCull(T4 frustum[6], T3 center, T radius)
{
for (int i = 0; i < 6; i++)
{
auto plane = frustum[i];
auto distance = plane[0] * center[0] + plane[1] * center[1] + plane[2] * center[2] + plane[3];
if (radius < -distance)
return true;
}
return false;
}
// Alterantive variant using squared radius
template <typename T4, typename T3, typename T>
static inline bool FrustumCull2(T4 frustum[6], T3 center, T radius2)
{
for (int i = 0; i < 6; i++)
{
auto plane = frustum[i];
auto distance = plane[0] * center[0] + plane[1] * center[1] + plane[2] * center[2] + plane[3];
if (radius2 > -std::abs(distance) * distance)
return true;
}
return false;
}
// Cull aabb against frustum planes
// center = (min + max) / 2
// extent = (max - min) / 2
template <typename T4, typename T3>
static inline bool FrustumCull(T4 frustum[6], T3 center, T3 extent)
{
for (int i = 0; i < 6; i++)
{
auto plane = frustum[i];
T3 vertex(
std::copysign(extent[0], plane[0]),
std::copysign(extent[1], plane[1]),
std::copysign(extent[2], plane[2]));
T3 normal(plane[0], plane[1], plane[2]);
if ((center + vertex).dot(normal) < -plane[3])
return true;
}
return false;
}
// Cull sphere smaller than a minimum size in pixels
// angular_size = 2 * radius / distance
// pixel_size = screen_height * angular_size / fov
// cull_threshold = (min_angular_size / 2)^2
template <typename T>
static inline T ContributionCullThreshold(T resy, T fovy = M_PI_2, T min_pixel_size = 2)
{
T min_angular_size = min_pixel_size * fovy / resy;
return min_angular_size * min_angular_size * T(0.25);
}
template <typename T3, typename T>
static inline bool ContributionCull(T3 campos, T cull_threshold, T3 center, T radius)
{
T3 d = center - campos;
return radius * radius < d.dot(d) * cull_threshold;
}
// Alterantive variant using squared radius
template <typename T3, typename T>
static inline bool ContributionCull2(T3 campos, T cull_threshold, T3 center, T radius2)
{
T3 d = center - campos;
return radius2 < d.dot(d) * cull_threshold;
}
// Frustum cull functors
template <typename T4>
struct FrustumCuller
{
T4 (&frustum)[6];
FrustumCuller(T4 (&nfrustum)[6]) :
frustum(nfrustum)
{}
template <typename T3, typename T>
inline bool operator()(const T3 & center, T radius) const
{
return FrustumCull(frustum, center, radius);
}
template <typename T3, typename T>
inline bool operator()(const T3 & center, const T3 & extent, T /*radius*/) const
{
return FrustumCull(frustum, center, extent);
}
};
template <typename T4>
static inline FrustumCuller<T4> MakeFrustumCuller(T4 (&frustum)[6])
{
return FrustumCuller<T4>(frustum);
}
template <typename T4, typename T3, typename T>
struct FrustumCullerPersp
{
T4 (&frustum)[6];
T3 & campos;
T cull_threshold;
FrustumCullerPersp(T4 (&nfrustum)[6], T3 & ncampos, T ncull_threshold) :
frustum(nfrustum),
campos(ncampos),
cull_threshold(ncull_threshold)
{}
inline bool operator()(const T3 & center, T radius) const
{
return FrustumCull(frustum, center, radius) || ContributionCull(campos, cull_threshold, center, radius);
}
inline bool operator()(const T3 & center, const T3 & extent, T radius) const
{
return FrustumCull(frustum, center, extent) || ContributionCull(campos, cull_threshold, center, radius);
}
};
template <typename T4, typename T3, typename T>
static inline FrustumCullerPersp<T4, T3, T> MakeFrustumCullerPersp(T4 (&frustum)[6], T3 & campos, T cull_threshold)
{
return FrustumCullerPersp<T4, T3, T>(frustum, campos, cull_threshold);
}
#endif