Skip to content

Commit f896962

Browse files
committed
Frustum class created with box checking method for AABB
1 parent 7f15c4b commit f896962

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#pragma once
2+
#include <iostream>
3+
#include <DXMath/DirectXMath.h>
4+
5+
using namespace DirectX;
6+
7+
class Frustum {
8+
private:
9+
XMVECTOR m_planes[6];
10+
public:
11+
Frustum() = default;
12+
13+
void Update(const XMMATRIX& View, const XMMATRIX& Projection) {
14+
XMMATRIX viewProj = XMMatrixMultiply(View, Projection);
15+
XMFLOAT4X4 vp;
16+
XMStoreFloat4x4(&vp, viewProj);
17+
18+
// Left plane
19+
m_planes[0] = DirectX::XMVectorSet(
20+
vp._14 + vp._11,
21+
vp._24 + vp._21,
22+
vp._34 + vp._31,
23+
vp._44 + vp._41
24+
);
25+
26+
// Right plane
27+
m_planes[1] = DirectX::XMVectorSet(
28+
vp._14 - vp._11,
29+
vp._24 - vp._21,
30+
vp._34 - vp._31,
31+
vp._44 - vp._41
32+
);
33+
34+
// Top plane
35+
m_planes[2] = DirectX::XMVectorSet(
36+
vp._14 - vp._12,
37+
vp._24 - vp._22,
38+
vp._34 - vp._32,
39+
vp._44 - vp._42
40+
);
41+
42+
// Bottom plane
43+
m_planes[3] = DirectX::XMVectorSet(
44+
vp._14 + vp._12,
45+
vp._24 + vp._22,
46+
vp._34 + vp._32,
47+
vp._44 + vp._42
48+
);
49+
50+
// Near plane
51+
m_planes[4] = DirectX::XMVectorSet(
52+
vp._13,
53+
vp._23,
54+
vp._33,
55+
vp._43
56+
);
57+
58+
// Far plane
59+
m_planes[5] = DirectX::XMVectorSet(
60+
vp._14 - vp._13,
61+
vp._24 - vp._23,
62+
vp._34 - vp._33,
63+
vp._44 - vp._43
64+
);
65+
66+
for (int i = 0; i < 6; ++i) {
67+
m_planes[i] = DirectX::XMVector4Normalize(m_planes[i]);
68+
}
69+
}
70+
71+
bool CheckBox(const XMFLOAT3& min, const XMFLOAT& max) {
72+
for (UINT i = 0; i < 6; i++) {
73+
XMVECTOR plane = this->m_planes[i];
74+
75+
XMFLOAT3 normal(
76+
XMVectorGetX(plane),
77+
XMVectorGetY(plane),
78+
XMVectorGetZ(plane)
79+
);
80+
81+
float planeD = XMVectorGetW(plane);
82+
83+
/* Find near point (P) and far point (N) */
84+
XMFLOAT3 p, n;
85+
86+
if (normal.x >= 0.f) {
87+
p.x = min,x;
88+
n.x = max.x;
89+
} else {
90+
p.x = max.x;
91+
n.x = min.x;
92+
}
93+
94+
if (normal.y >= 0.f) {
95+
p.y = min.y;
96+
n.y = max.y;
97+
} else {
98+
p.y = max.y;
99+
n.y = min.y;
100+
}
101+
102+
if (normal.z >= 0.f) {
103+
p.z = min.z;
104+
n.z = max.z;
105+
} else {
106+
p.z = max.z;
107+
n.z = min.z;
108+
}
109+
110+
float d_near = normal.x * n.x + normal.y * n.y + normal.z * n.z + planeD;
111+
112+
if (d_near < 0.f) {
113+
float d_far = normal.x * p.x + normal.y * p.y + normal.z * p.z + planeD;
114+
115+
if (d_far < 0.f) {
116+
return false;
117+
}
118+
}
119+
}
120+
121+
return true;
122+
}
123+
};

0 commit comments

Comments
 (0)