-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrustum.cpp
More file actions
196 lines (162 loc) · 5.86 KB
/
Frustum.cpp
File metadata and controls
196 lines (162 loc) · 5.86 KB
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
#include "Defines.h"
#include "Frustum.h"
Frustum::Frustum()
{
}
Frustum::Frustum(const Frustum& other)
{
}
Frustum::~Frustum()
{
}
void Frustum::ConstructFrustum(float screenDepth, XMMATRIX projMatrix, XMMATRIX viewMatrix)
{
float zMinimum, r;
XMMATRIX matrix;
XMFLOAT4X4 tempMatrix;
XMStoreFloat4x4(&tempMatrix, projMatrix);
// Calculate the minimum Z distance in the frustum.
zMinimum = -tempMatrix._43 / tempMatrix._33;
r = screenDepth / (screenDepth - zMinimum);
tempMatrix._33 = r;
tempMatrix._43 = -r * zMinimum;
projMatrix = XMLoadFloat4x4(&tempMatrix);
// Create the frustum matrix from the view matrix and updated projection matrix.
matrix = XMMatrixMultiply(viewMatrix, projMatrix);
XMStoreFloat4x4(&tempMatrix, matrix);
XMVECTOR tempPlane;
// Calculate near plane of frustum.
planes[0].x = tempMatrix._14 + tempMatrix._13; // _xy in matrix
planes[0].y = tempMatrix._24 + tempMatrix._23;
planes[0].z = tempMatrix._34 + tempMatrix._33;
planes[0].w = tempMatrix._44 + tempMatrix._43;
tempPlane = XMLoadFloat4(&planes[0]);
tempPlane = XMPlaneNormalize(tempPlane);
XMStoreFloat4(&planes[0], tempPlane);
// Calculate far plane of frustum.
planes[1].x = tempMatrix._14 - tempMatrix._13;
planes[1].y = tempMatrix._24 - tempMatrix._23;
planes[1].z = tempMatrix._34 - tempMatrix._33;
planes[1].w = tempMatrix._44 - tempMatrix._43;
tempPlane = XMLoadFloat4(&planes[1]);
tempPlane = XMPlaneNormalize(tempPlane);
XMStoreFloat4(&planes[1], tempPlane);
// Calculate left plane of frustum.
planes[2].x = tempMatrix._14 + tempMatrix._11;
planes[2].y = tempMatrix._24 + tempMatrix._21;
planes[2].z = tempMatrix._34 + tempMatrix._31;
planes[2].w = tempMatrix._44 + tempMatrix._41;
tempPlane = XMLoadFloat4(&planes[2]);
tempPlane = XMPlaneNormalize(tempPlane);
XMStoreFloat4(&planes[2], tempPlane);
// Calculate right plane of frustum.
planes[3].x = tempMatrix._14 - tempMatrix._11;
planes[3].y = tempMatrix._24 - tempMatrix._21;
planes[3].z = tempMatrix._34 - tempMatrix._31;
planes[3].w = tempMatrix._44 - tempMatrix._41;
tempPlane = XMLoadFloat4(&planes[3]);
tempPlane = XMPlaneNormalize(tempPlane);
XMStoreFloat4(&planes[3], tempPlane);
// Calculate top plane of frustum.
planes[4].x = tempMatrix._14 - tempMatrix._12;
planes[4].y = tempMatrix._24 - tempMatrix._22;
planes[4].z = tempMatrix._34 - tempMatrix._32;
planes[4].w = tempMatrix._44 - tempMatrix._42;
tempPlane = XMLoadFloat4(&planes[4]);
tempPlane = XMPlaneNormalize(tempPlane);
XMStoreFloat4(&planes[4], tempPlane);
// Calculate bottom plane of frustum.
planes[5].x = tempMatrix._14 + tempMatrix._12;
planes[5].y = tempMatrix._24 + tempMatrix._22;
planes[5].z = tempMatrix._34 + tempMatrix._32;
planes[5].w = tempMatrix._44 + tempMatrix._42;
tempPlane = XMLoadFloat4(&planes[5]);
tempPlane = XMPlaneNormalize(tempPlane);
XMStoreFloat4(&planes[5], tempPlane);
return;
}
bool Frustum::CheckCube(float xCenter, float yCenter, float zCenter, float radius)
{
XMVECTOR tempPlane = XMVectorSet(0, 0, 0, 0);
XMVECTOR tempPlane2 = XMVectorSet(0, 0, 0, 0);
float tempFloat;
XMFLOAT3 tempFloat3 = XMFLOAT3(0.0f, 0.0f, 0.0f);
// Check if any one point of the cube is in the view frustum.
for (int i = 0; i < 6; i++)
{
tempPlane = XMLoadFloat4(&planes[i]);
tempFloat3 = XMFLOAT3((xCenter - radius), (yCenter - radius), (zCenter - radius));
tempPlane2 = XMLoadFloat3(&tempFloat3);
tempPlane = XMPlaneDotCoord(tempPlane, tempPlane2);
XMStoreFloat(&tempFloat, tempPlane);
if(tempFloat >= 0.0f)
{
continue;
}
tempPlane = XMLoadFloat4(&planes[i]);
tempFloat3 = XMFLOAT3((xCenter + radius), (yCenter - radius), (zCenter - radius));
tempPlane2 = XMLoadFloat3(&tempFloat3);
tempPlane = XMPlaneDotCoord(tempPlane, tempPlane2);
XMStoreFloat(&tempFloat, tempPlane);
if(tempFloat >= 0.0f)
{
continue;
}
tempPlane = XMLoadFloat4(&planes[i]);
tempFloat3 = XMFLOAT3((xCenter - radius), (yCenter + radius), (zCenter - radius));
tempPlane2 = XMLoadFloat3(&tempFloat3);
tempPlane = XMPlaneDotCoord(tempPlane, tempPlane2);
XMStoreFloat(&tempFloat, tempPlane);
if(tempFloat >= 0.0f)
{
continue;
}
tempPlane = XMLoadFloat4(&planes[i]);
tempFloat3 = XMFLOAT3((xCenter + radius), (yCenter + radius), (zCenter - radius));
tempPlane2 = XMLoadFloat3(&tempFloat3);
tempPlane = XMPlaneDotCoord(tempPlane, tempPlane2);
XMStoreFloat(&tempFloat, tempPlane);
if(tempFloat >= 0.0f)
{
continue;
}
tempPlane = XMLoadFloat4(&planes[i]);
tempFloat3 = XMFLOAT3((xCenter - radius), (yCenter - radius), (zCenter + radius));
tempPlane2 = XMLoadFloat3(&tempFloat3);
tempPlane = XMPlaneDotCoord(tempPlane, tempPlane2);
XMStoreFloat(&tempFloat, tempPlane);
if(tempFloat >= 0.0f)
{
continue;
}
tempPlane = XMLoadFloat4(&planes[i]);
tempFloat3 = XMFLOAT3((xCenter + radius), (yCenter - radius), (zCenter + radius));
tempPlane2 = XMLoadFloat3(&tempFloat3);
tempPlane = XMPlaneDotCoord(tempPlane, tempPlane2);
XMStoreFloat(&tempFloat, tempPlane);
if(tempFloat >= 0.0f)
{
continue;
}
tempPlane = XMLoadFloat4(&planes[i]);
tempFloat3 = XMFLOAT3((xCenter - radius), (yCenter + radius), (zCenter + radius));
tempPlane2 = XMLoadFloat3(&tempFloat3);
tempPlane = XMPlaneDotCoord(tempPlane, tempPlane2);
XMStoreFloat(&tempFloat, tempPlane);
if(tempFloat >= 0.0f)
{
continue;
}
tempPlane = XMLoadFloat4(&planes[i]);
tempFloat3 = XMFLOAT3((xCenter + radius), (yCenter + radius), (zCenter + radius));
tempPlane2 = XMLoadFloat3(&tempFloat3);
tempPlane = XMPlaneDotCoord(tempPlane, tempPlane2);
XMStoreFloat(&tempFloat, tempPlane);
if(tempFloat >= 0.0f)
{
continue;
}
return false; //not in frustum
}
return true; //in frustum
}