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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
#include "UnityPrefix.h"
#include "BoundingUtils.h"
#include "Plane.h"
#include "AABB.h"
#include "Intersection.h"
// --------------------------------------------------------------------------
void GetFrustumPoints( const Matrix4x4f& clipToWorld, Vector3f* frustum )
{
clipToWorld.PerspectiveMultiplyPoint3( Vector3f(-1,-1,-1), frustum[0] );
clipToWorld.PerspectiveMultiplyPoint3( Vector3f( 1,-1,-1), frustum[1] );
clipToWorld.PerspectiveMultiplyPoint3( Vector3f( 1, 1,-1), frustum[2] );
clipToWorld.PerspectiveMultiplyPoint3( Vector3f(-1, 1,-1), frustum[3] );
clipToWorld.PerspectiveMultiplyPoint3( Vector3f(-1,-1, 1), frustum[4] );
clipToWorld.PerspectiveMultiplyPoint3( Vector3f( 1,-1, 1), frustum[5] );
clipToWorld.PerspectiveMultiplyPoint3( Vector3f( 1, 1, 1), frustum[6] );
clipToWorld.PerspectiveMultiplyPoint3( Vector3f(-1, 1, 1), frustum[7] );
}
void GetFrustumPortion( const Vector3f* frustum, float nearSplit, float farSplit, Vector3f* outPortion )
{
outPortion[0] = Lerp( frustum[0], frustum[0+4], nearSplit );
outPortion[1] = Lerp( frustum[1], frustum[1+4], nearSplit );
outPortion[2] = Lerp( frustum[2], frustum[2+4], nearSplit );
outPortion[3] = Lerp( frustum[3], frustum[3+4], nearSplit );
outPortion[4] = Lerp( frustum[0], frustum[0+4], farSplit );
outPortion[5] = Lerp( frustum[1], frustum[1+4], farSplit );
outPortion[6] = Lerp( frustum[2], frustum[2+4], farSplit );
outPortion[7] = Lerp( frustum[3], frustum[3+4], farSplit );
}
static bool ClipTest( const float p, const float q, float& u1, float& u2 )
{
// Return value is 'true' if line segment intersects the current test
// plane. Otherwise 'false' is returned in which case the line segment
// is entirely clipped.
const float EPS = 1.0e-10f;
if( p < -EPS ) {
float r = q/p;
if( r > u2 )
return false;
else {
if( r > u1 )
u1 = r;
return true;
}
}
else if( p > EPS )
{
float r = q/p;
if( r < u1 )
return false;
else {
if( r < u2 )
u2 = r;
return true;
}
}
else
{
return q >= 0.0f;
}
}
static bool IntersectLineAABB( Vector3f& v, const Vector3f& p, const Vector3f& dir, const MinMaxAABB& b )
{
float t1 = 0.0f;
float t2 = 1.0e30f;
bool intersect = false;
if (ClipTest(-dir.z,p.z-b.GetMin().z,t1,t2) && ClipTest(dir.z,b.GetMax().z-p.z,t1,t2) &&
ClipTest(-dir.y,p.y-b.GetMin().y,t1,t2) && ClipTest(dir.y,b.GetMax().y-p.y,t1,t2) &&
ClipTest(-dir.x,p.x-b.GetMin().x,t1,t2) && ClipTest(dir.x,b.GetMax().x-p.x,t1,t2))
{
if( 0 <= t1 ) {
v = p + dir * t1;
intersect = true;
}
if( 0 <= t2 ) {
v = p + dir * t2;
intersect = true;
}
}
return intersect;
}
inline bool ClipPolysByPlane( UInt8 numPoints, const Vector3f* __restrict input, const Plane& A, UInt8* __restrict pNumOutPoints, Vector3f* __restrict output, UInt8* __restrict pNumIntermPoints, Vector3f* __restrict interm )
{
int i;
if( numPoints < 3 )
{
*pNumOutPoints = 0;
return false;
}
UInt8 numOutPoints = 0;
UInt8& numIntermPoints = *pNumIntermPoints;
Vector3f temp;
bool * outside = (bool*)alloca(numPoints*sizeof(bool));
for( i = 0; i < numPoints; ++i )
outside[i] = A.GetDistanceToPoint( input[i] ) < 0.0f;
for( i = 0; i < numPoints; ++i )
{
int idNext = (i+1) % numPoints;
// both outside -> save none
if(outside[i] && outside[idNext])
continue;
// outside-inside -> save intersection and i+1
if(outside[i])
{
if( IntersectSegmentPlane( input[i], input[idNext], A, &temp ) )
{
output[numOutPoints++] = temp;
interm[numIntermPoints++] = temp;
}
output[numOutPoints++] = input[idNext];
continue;
}
// inside-outside -> save intersection
if(outside[idNext])
{
if( IntersectSegmentPlane( input[i], input[idNext], A, &temp ) )
{
output[numOutPoints++] = temp;
interm[numIntermPoints++] = temp;
}
continue;
}
output[numOutPoints++] = input[idNext];
}
*pNumOutPoints = numOutPoints;
return numOutPoints ? true : false;
}
void CalcHullBounds(const Vector3f* __restrict hullPoints, const UInt8* __restrict hullCounts, UInt8 hullFaces, const Plane& nearPlane, const Matrix4x4f& cameraWorldToClip, MinMaxAABB& aabb)
{
Vector3f outputData[128];
Vector3f intermData[128];
UInt8 outputDataCounts[64];
UInt8 intermDataCounts[64];
const Vector3f* __restrict inputPoints = hullPoints;
const UInt8* __restrict inputCounts = hullCounts;
Vector3f* __restrict outputPoints = outputData;
UInt8* __restrict outputCounts = outputDataCounts;
intermDataCounts[0] = 0;
for( UInt8 i = 0; i != hullFaces; ++i )
{
const UInt8 inputCount = *inputCounts++;
ClipPolysByPlane( inputCount, inputPoints, nearPlane, outputCounts, outputPoints, intermDataCounts, intermData);
inputPoints += inputCount;
outputPoints += *outputCounts;
outputCounts++;
}
// Project hull's points onto the screen and compute bounding rectangle of them.
Vector3f projectedPoint;
outputPoints = outputData;
outputCounts = outputDataCounts;
for( int f = 0; f < hullFaces; ++f )
{
const UInt8 numPoints = *outputCounts++;
for( int i = 0; i < numPoints; ++i )
{
cameraWorldToClip.PerspectiveMultiplyPoint3( outputPoints[i], projectedPoint );
aabb.Encapsulate( projectedPoint );
}
outputPoints += numPoints;
}
if( aabb.m_Min.x < -1.0f ) aabb.m_Min.x = -1.0f;
if( aabb.m_Min.y < -1.0f ) aabb.m_Min.y = -1.0f;
if( aabb.m_Max.x > 1.0f ) aabb.m_Max.x = 1.0f;
if( aabb.m_Max.y > 1.0f ) aabb.m_Max.y = 1.0f;
}
// Returns our "focused region of interest": frustum, clipped by scene bounds,
// extruded towards light and clipped by scene bounds again.
//
// Frustum is
// { -1, -1, -1 }
// { 1, -1, -1 }
// { 1, 1, -1 }
// { -1, 1, -1 }
// { -1, -1, 1 }
// { 1, -1, 1 }
// { 1, 1, 1 }
// { -1, 1, 1 }
void CalculateFocusedLightHull( const Vector3f* frustum, const Vector3f& lightDir, const MinMaxAABB& sceneAABB, std::vector<Vector3f>& points )
{
int i;
Vector3f tempPoints[3][256];
UInt8 tempCounts[3][128];
Plane planes[6];
planes[0].SetABCD( 0, 1, 0, -sceneAABB.GetMin().y );
planes[1].SetABCD( 0, -1, 0, sceneAABB.GetMax().y );
planes[2].SetABCD( 1, 0, 0, -sceneAABB.GetMin().x );
planes[3].SetABCD( -1, 0, 0, sceneAABB.GetMax().x );
planes[4].SetABCD( 0, 0, 1, -sceneAABB.GetMin().z );
planes[5].SetABCD( 0, 0, -1, sceneAABB.GetMax().z );
Vector3f* __restrict pData[2] = { (Vector3f*)&tempPoints[0][0], (Vector3f*)&tempPoints[1][0] };
UInt8* __restrict pDataCounts[2] = { (UInt8*)&tempCounts[0][0], (UInt8*)&tempCounts[1][0] };
Vector3f* v = *pData;
UInt8* c = *pDataCounts;
UInt32 numFaces = 6;
c[0] = c[1] = c[2] = c[3] = c[4] = c[5] = 4;
v[ 0] = frustum[0]; v[ 1] = frustum[1]; v[ 2] = frustum[2]; v[ 3] = frustum[3];
v[ 4] = frustum[7]; v[ 5] = frustum[6]; v[ 6] = frustum[5]; v[ 7] = frustum[4];
v[ 8] = frustum[0]; v[ 9] = frustum[3]; v[10] = frustum[7]; v[11] = frustum[4];
v[12] = frustum[1]; v[13] = frustum[5]; v[14] = frustum[6]; v[15] = frustum[2];
v[16] = frustum[4]; v[17] = frustum[5]; v[18] = frustum[1]; v[19] = frustum[0];
v[20] = frustum[6]; v[21] = frustum[7]; v[22] = frustum[3]; v[23] = frustum[2];
UInt32 numTotalPoints;
UInt32 vIn = 0;
Vector3f* intermPoints = &tempPoints[2][0];
UInt8* intermCounts = &tempCounts[2][0];
for( int p = 0; p < 6; ++p )
{
const Vector3f* __restrict inputPoints=pData[vIn];
Vector3f* __restrict outputPoints = pData[1-vIn];
UInt8* __restrict inputCounts=pDataCounts[vIn];
UInt8* __restrict outputCounts = pDataCounts[1-vIn];
numTotalPoints = 0;
*intermCounts = 0;
UInt32 faceCount = numFaces;
for( i = 0; i < faceCount; ++i )
{
const UInt8 numInputPoints = *inputCounts;
if(ClipPolysByPlane(numInputPoints, inputPoints, planes[p], outputCounts, outputPoints, intermCounts, intermPoints))
{
const UInt8 outputCount = *outputCounts++;
numTotalPoints+=outputCount;
outputPoints += outputCount;
}
else
{
if(0 == (--numFaces))
break;
}
inputCounts++;
inputPoints += numInputPoints;
}
vIn = 1-vIn; // anyone for ping-pong ?
// add an extra face built from all the intersection points so it catches all the edges.
const UInt8 numIntermPoints = *intermCounts;
if(numIntermPoints && (p<5))
{
numFaces++;
*outputCounts = numIntermPoints;
memcpy(outputPoints, intermPoints, numIntermPoints * sizeof(Vector3f));
}
}
if(numFaces)
{ // output the clipped points
Vector3f pt;
Vector3f ld = -lightDir;
points.reserve(numTotalPoints << 1); // worst case scenario
Vector3f* __restrict inputPoints=pData[vIn];
UInt8* __restrict inputCounts=pDataCounts[vIn];
for( i = 0; i < numFaces; ++i )
{
const UInt8 numPoints = *inputCounts++;
for(UInt8 k=0;k!=numPoints;k++)
{
const Vector3f& v = inputPoints[k];
points.push_back(v);
if( IntersectLineAABB( pt, v, ld, sceneAABB ) )
points.push_back( pt );
}
inputPoints += numPoints;
}
}
}
void CalculateBoundingSphereFromFrustumPoints(const Vector3f points[8], Vector3f& outCenter, float& outRadius)
{
Vector3f spherePoints[4];
spherePoints[0] = points[0];
spherePoints[1] = points[3];
spherePoints[2] = points[5];
spherePoints[3] = points[7];
// Is bounding sphere at the far or near plane?
for( int plane = 1; plane >= 0; --plane )
{
Vector3f pointA = spherePoints[plane*2];
Vector3f pointB = spherePoints[plane*2 + 1];
Vector3f center = (pointA + pointB) * 0.5f;
float radius2 = SqrMagnitude(pointA - center);
Vector3f pointC = spherePoints[(1-plane)*2];
Vector3f pointD = spherePoints[(1-plane)*2 + 1];
// Check if all points are inside sphere
if( SqrMagnitude(pointC - center) <= radius2 &&
SqrMagnitude(pointD - center) <= radius2 )
{
outCenter = center;
outRadius = sqrt(radius2);
return;
}
}
// Sphere touches all four frustum points
CalculateSphereFrom4Points(spherePoints, outCenter, outRadius);
}
void CalculateSphereFrom4Points(const Vector3f points[4], Vector3f& outCenter, float& outRadius)
{
Matrix4x4f mat;
for( int i = 0; i < 4; ++i )
{
mat.Get(i, 0) = points[i].x;
mat.Get(i, 1) = points[i].y;
mat.Get(i, 2) = points[i].z;
mat.Get(i, 3) = 1;
}
float m11 = mat.GetDeterminant();
for( int i = 0; i < 4; ++i )
{
mat.Get(i, 0) = points[i].x*points[i].x + points[i].y*points[i].y + points[i].z*points[i].z;
mat.Get(i, 1) = points[i].y;
mat.Get(i, 2) = points[i].z;
mat.Get(i, 3) = 1;
}
float m12 = mat.GetDeterminant();
for( int i = 0; i < 4; ++i )
{
mat.Get(i, 0) = points[i].x;
mat.Get(i, 1) = points[i].x*points[i].x + points[i].y*points[i].y + points[i].z*points[i].z;
mat.Get(i, 2) = points[i].z;
mat.Get(i, 3) = 1;
}
float m13 = mat.GetDeterminant();
for( int i = 0; i < 4; ++i )
{
mat.Get(i, 0) = points[i].x;
mat.Get(i, 1) = points[i].y;
mat.Get(i, 2) = points[i].x*points[i].x + points[i].y*points[i].y + points[i].z*points[i].z;
mat.Get(i, 3) = 1;
}
float m14 = mat.GetDeterminant();
for( int i = 0; i < 4; ++i )
{
mat.Get(i, 0) = points[i].x*points[i].x + points[i].y*points[i].y + points[i].z*points[i].z;
mat.Get(i, 1) = points[i].x;
mat.Get(i, 2) = points[i].y;
mat.Get(i, 3) = points[i].z;
}
float m15 = mat.GetDeterminant();
Vector3f c;
c.x = 0.5 * m12 / m11;
c.y = 0.5 * m13 / m11;
c.z = 0.5 * m14 / m11;
outRadius = sqrt(c.x*c.x + c.y*c.y + c.z*c.z - m15/m11);
outCenter = c;
}
void CalculateSpotLightBounds (const float range, const float cotanHalfSpotAngle, const Matrix4x4f& lightMatrix, SpotLightBounds& outBounds)
{
float sideLength = range / cotanHalfSpotAngle;
outBounds.points[0] = lightMatrix.GetPosition();
outBounds.points[1] = lightMatrix.MultiplyPoint3( Vector3f(-sideLength,-sideLength, range) );
outBounds.points[2] = lightMatrix.MultiplyPoint3( Vector3f( sideLength,-sideLength, range) );
outBounds.points[3] = lightMatrix.MultiplyPoint3( Vector3f( sideLength, sideLength, range) );
outBounds.points[4] = lightMatrix.MultiplyPoint3( Vector3f(-sideLength, sideLength, range) );
}
#if ENABLE_UNIT_TESTS
#include "External/UnitTest++/src/UnitTest++.h"
#include "Runtime/Math/Random/rand.h"
SUITE (BoundingUtilsTest)
{
TEST(BoundingUtilsTest_CalculateSphereFrom4Points)
{
Rand rnd(123);
for( int i = 0; i < 10; ++i )
{
Vector3f points[4];
for( int j = 0; j < 4; ++j )
{
points[j].x = rnd.GetSignedFloat()*100.f;
points[j].y = rnd.GetSignedFloat()*100.f;
points[j].z = rnd.GetSignedFloat()*100.f;
}
Vector3f center;
float radius;
CalculateSphereFrom4Points(points, center, radius);
for( int j = 0; j < 4; ++j )
{
float dist = Magnitude(points[j] - center);
float ratio = dist / radius;
// Radius may be large compared to input range, avoid comparing absolute distances
bool correct = (ratio > 0.999f) && (ratio < 1.001f);
CHECK(correct);
}
}
}
}
#endif
|