-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrustum.h
More file actions
175 lines (137 loc) · 3.59 KB
/
Frustum.h
File metadata and controls
175 lines (137 loc) · 3.59 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
#pragma once
#ifndef FRUSTUM_H
#define FRUSTUM_H
#include <vector>
#ifndef SUBSPACES_H
#include "Subspaces.h"
#endif
#ifndef MATRIX_H
#include "Matrix.hpp"
#endif
#ifndef OPERATIONS_H
#include "Operations.hpp"
#endif
namespace cliqCity
{
namespace graphicsMath
{
struct Frustum
{
union
{
struct
{
Plane<Vector3> front, back, left, right, bottom, top;
};
Plane<Vector3> planes[6];
};
};
inline void ExtractNormalizedFrustumLH(Frustum* frustum, Matrix4& viewProjectionMatrix)
{
float distance, mag;
// Near
Vector3 front =
{
viewProjectionMatrix[0][2],
viewProjectionMatrix[1][2],
viewProjectionMatrix[2][2]
};
distance = viewProjectionMatrix[3][2];
mag = magnitude(front);
frustum->front.normal = front / mag;
frustum->front.distance = -distance / mag;
// Far
Vector3 back =
{
viewProjectionMatrix[0][3] - viewProjectionMatrix[0][2],
viewProjectionMatrix[1][3] - viewProjectionMatrix[1][2],
viewProjectionMatrix[2][3] - viewProjectionMatrix[2][2]
};
distance = viewProjectionMatrix[3][3] - viewProjectionMatrix[3][2];
mag = magnitude(back);
frustum->back.normal = back / mag;
frustum->back.distance = -distance / mag;
// Left / Right
Vector3 left =
{
viewProjectionMatrix[0][3] + viewProjectionMatrix[0][0],
viewProjectionMatrix[1][3] + viewProjectionMatrix[1][0],
viewProjectionMatrix[2][3] + viewProjectionMatrix[2][0]
};
distance = viewProjectionMatrix[3][3] + viewProjectionMatrix[3][0];
mag = magnitude(left);
left /= mag;
distance /= mag;
frustum->left.normal = left;
frustum->left.distance = -distance;
Vector3 right =
{
viewProjectionMatrix[0][3] - viewProjectionMatrix[0][0],
viewProjectionMatrix[1][3] - viewProjectionMatrix[1][0],
viewProjectionMatrix[2][3] - viewProjectionMatrix[2][0]
};
distance = viewProjectionMatrix[3][3] - viewProjectionMatrix[3][0];
mag = magnitude(right);
right /= mag;
distance /= mag;
frustum->right.normal = right;
frustum->right.distance = -distance;
// Top / Bottom
Vector3 bottom =
{
viewProjectionMatrix[0][3] + viewProjectionMatrix[0][1],
viewProjectionMatrix[1][3] + viewProjectionMatrix[1][1],
viewProjectionMatrix[2][3] + viewProjectionMatrix[2][1]
};
distance = viewProjectionMatrix[3][3] + viewProjectionMatrix[3][1];
mag = magnitude(bottom);
bottom /= mag;
distance /= mag;
frustum->bottom.normal = bottom;
frustum->bottom.distance = -distance;
Vector3 top =
{
viewProjectionMatrix[0][3] - viewProjectionMatrix[0][1],
viewProjectionMatrix[1][3] - viewProjectionMatrix[1][1],
viewProjectionMatrix[2][3] - viewProjectionMatrix[2][1]
};
distance = viewProjectionMatrix[3][3] - viewProjectionMatrix[3][1];
mag = magnitude(top);
top /= mag;
distance /= mag;
frustum->top.normal = top;
frustum->top.distance = -distance;
}
inline void Cull(const Frustum& frustum, Sphere<Vector3>* spheres, std::vector<uint32_t>& indices, const uint32_t& count)
{
float distance;
const Plane<Vector3>* planes[6] =
{
&frustum.front,
&frustum.back,
&frustum.left,
&frustum.right,
&frustum.bottom,
&frustum.top,
};
for (uint32_t i = 0; i < count; i++)
{
bool shouldCull = false;
for (uint32_t p = 0; p < 6; p++)
{
distance = dot(planes[p]->normal, spheres[i].origin) - (planes[p]->distance - spheres[i].radius);
if (distance < 0)
{
shouldCull = true;
break;
}
}
if (!shouldCull)
{
indices.push_back(i);
}
}
}
}
}
#endif