forked from GameTechDev/CloudsGPUPro6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisibility.h
116 lines (97 loc) · 4.46 KB
/
Visibility.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
/////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or imlied.
// See the License for the specific language governing permissions and
// limitations under the License.
/////////////////////////////////////////////////////////////////////////////////////////////
struct SBoundingBox
{
float fMinX, fMaxX, fMinY, fMaxY, fMinZ, fMaxZ;
};
// Structure describing a plane
struct SPlane3D
{
D3DXVECTOR3 Normal;
float Distance; //Distance from the coordinate system origin to the plane along normal direction
};
#pragma pack(1)
struct SViewFrustum
{
SPlane3D LeftPlane, RightPlane, BottomPlane, TopPlane, NearPlane, FarPlane;
};
#pragma pack()
static UINT TEST_LEFT_PLANE = (1 << 0);
static UINT TEST_RIGHT_PLANE = (1 << 1);
static UINT TEST_BOTTOM_PLANE = (1 << 2);
static UINT TEST_TOP_PLANE = (1 << 3);
static UINT TEST_NEAR_PLANE = (1 << 4);
static UINT TEST_FAR_PLANE = (1 << 5);
static UINT TEST_ALL_PLANES = (TEST_LEFT_PLANE | TEST_RIGHT_PLANE | TEST_BOTTOM_PLANE | TEST_TOP_PLANE | TEST_NEAR_PLANE | TEST_FAR_PLANE);
// Tests if bounding box is visible by the camera
static inline bool IsBoxVisible(const SViewFrustum &ViewFrustum, const SBoundingBox &Box, UINT uiPlaneFlags = TEST_ALL_PLANES)
{
SPlane3D *pPlanes = (SPlane3D *)&ViewFrustum;
// If bounding box is "behind" some plane, then it is invisible
// Otherwise it is treated as visible
for(int iViewFrustumPlane = 0; iViewFrustumPlane < 6; iViewFrustumPlane++)
{
if( !(uiPlaneFlags & (1<<iViewFrustumPlane)) )
continue;
SPlane3D *pCurrPlane = pPlanes + iViewFrustumPlane;
D3DXVECTOR3 *pCurrNormal = &pCurrPlane->Normal;
D3DXVECTOR3 MaxPoint;
MaxPoint.x = (pCurrNormal->x > 0) ? Box.fMaxX : Box.fMinX;
MaxPoint.y = (pCurrNormal->y > 0) ? Box.fMaxY : Box.fMinY;
MaxPoint.z = (pCurrNormal->z > 0) ? Box.fMaxZ : Box.fMinZ;
float DMax = D3DXVec3Dot( &MaxPoint, pCurrNormal ) + pCurrPlane->Distance;
if( DMax < 0 )
return false;
}
return true;
}
// Extract view frustum planes from the world-view-projection matrix
static inline void ExtractViewFrustumPlanesFromMatrix(const D3DXMATRIX &Matrix, SViewFrustum &ViewFrustum)
{
// For more details, see Gribb G., Hartmann K., "Fast Extraction of Viewing Frustum Planes from the
// World-View-Projection Matrix" (the paper is available at
// http://www2.ravensoft.com/users/ggribb/plane%20extraction.pdf)
// Left clipping plane
ViewFrustum.LeftPlane.Normal.x = Matrix._14 + Matrix._11;
ViewFrustum.LeftPlane.Normal.y = Matrix._24 + Matrix._21;
ViewFrustum.LeftPlane.Normal.z = Matrix._34 + Matrix._31;
ViewFrustum.LeftPlane.Distance = Matrix._44 + Matrix._41;
// Right clipping plane
ViewFrustum.RightPlane.Normal.x = Matrix._14 - Matrix._11;
ViewFrustum.RightPlane.Normal.y = Matrix._24 - Matrix._21;
ViewFrustum.RightPlane.Normal.z = Matrix._34 - Matrix._31;
ViewFrustum.RightPlane.Distance = Matrix._44 - Matrix._41;
// Top clipping plane
ViewFrustum.TopPlane.Normal.x = Matrix._14 - Matrix._12;
ViewFrustum.TopPlane.Normal.y = Matrix._24 - Matrix._22;
ViewFrustum.TopPlane.Normal.z = Matrix._34 - Matrix._32;
ViewFrustum.TopPlane.Distance = Matrix._44 - Matrix._42;
// Bottom clipping plane
ViewFrustum.BottomPlane.Normal.x = Matrix._14 + Matrix._12;
ViewFrustum.BottomPlane.Normal.y = Matrix._24 + Matrix._22;
ViewFrustum.BottomPlane.Normal.z = Matrix._34 + Matrix._32;
ViewFrustum.BottomPlane.Distance = Matrix._44 + Matrix._42;
// Near clipping plane
ViewFrustum.NearPlane.Normal.x = Matrix._13;
ViewFrustum.NearPlane.Normal.y = Matrix._23;
ViewFrustum.NearPlane.Normal.z = Matrix._33;
ViewFrustum.NearPlane.Distance = Matrix._43;
// Far clipping plane
ViewFrustum.FarPlane.Normal.x = Matrix._14 - Matrix._13;
ViewFrustum.FarPlane.Normal.y = Matrix._24 - Matrix._23;
ViewFrustum.FarPlane.Normal.z = Matrix._34 - Matrix._33;
ViewFrustum.FarPlane.Distance = Matrix._44 - Matrix._43;
}