mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 09:29:44 +00:00
cleanup FrustumCulling.hpp
This commit is contained in:
parent
35e20c7dd4
commit
3895ef020c
1 changed files with 52 additions and 73 deletions
|
|
@ -28,102 +28,81 @@ private:
|
|||
template <Planes a, Planes b, Planes c>
|
||||
glm::vec3 intersection(const glm::vec3* crosses) const;
|
||||
|
||||
glm::vec4 m_planes[Count];
|
||||
glm::vec3 m_points[8];
|
||||
glm::vec4 planes[Count];
|
||||
glm::vec3 points[8];
|
||||
glm::vec3 minPoint;
|
||||
glm::vec3 maxPoint;
|
||||
};
|
||||
|
||||
inline void Frustum::update(glm::mat4 m) {
|
||||
m = glm::transpose(m);
|
||||
m_planes[Left] = m[3] + m[0];
|
||||
m_planes[Right] = m[3] - m[0];
|
||||
m_planes[Bottom] = m[3] + m[1];
|
||||
m_planes[Top] = m[3] - m[1];
|
||||
m_planes[Near] = m[3] + m[2];
|
||||
m_planes[Far] = m[3] - m[2];
|
||||
planes[Left] = m[3] + m[0];
|
||||
planes[Right] = m[3] - m[0];
|
||||
planes[Bottom] = m[3] + m[1];
|
||||
planes[Top] = m[3] - m[1];
|
||||
planes[Near] = m[3] + m[2];
|
||||
planes[Far] = m[3] - m[2];
|
||||
|
||||
glm::vec3 crosses[Combinations] = {
|
||||
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Right])),
|
||||
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Bottom])),
|
||||
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Top])),
|
||||
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Near])),
|
||||
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Far])),
|
||||
glm::cross(glm::vec3(m_planes[Right]), glm::vec3(m_planes[Bottom])),
|
||||
glm::cross(glm::vec3(m_planes[Right]), glm::vec3(m_planes[Top])),
|
||||
glm::cross(glm::vec3(m_planes[Right]), glm::vec3(m_planes[Near])),
|
||||
glm::cross(glm::vec3(m_planes[Right]), glm::vec3(m_planes[Far])),
|
||||
glm::cross(glm::vec3(m_planes[Bottom]), glm::vec3(m_planes[Top])),
|
||||
glm::cross(glm::vec3(m_planes[Bottom]), glm::vec3(m_planes[Near])),
|
||||
glm::cross(glm::vec3(m_planes[Bottom]), glm::vec3(m_planes[Far])),
|
||||
glm::cross(glm::vec3(m_planes[Top]), glm::vec3(m_planes[Near])),
|
||||
glm::cross(glm::vec3(m_planes[Top]), glm::vec3(m_planes[Far])),
|
||||
glm::cross(glm::vec3(m_planes[Near]), glm::vec3(m_planes[Far]))};
|
||||
glm::cross(glm::vec3(planes[Left]), glm::vec3(planes[Right])),
|
||||
glm::cross(glm::vec3(planes[Left]), glm::vec3(planes[Bottom])),
|
||||
glm::cross(glm::vec3(planes[Left]), glm::vec3(planes[Top])),
|
||||
glm::cross(glm::vec3(planes[Left]), glm::vec3(planes[Near])),
|
||||
glm::cross(glm::vec3(planes[Left]), glm::vec3(planes[Far])),
|
||||
glm::cross(glm::vec3(planes[Right]), glm::vec3(planes[Bottom])),
|
||||
glm::cross(glm::vec3(planes[Right]), glm::vec3(planes[Top])),
|
||||
glm::cross(glm::vec3(planes[Right]), glm::vec3(planes[Near])),
|
||||
glm::cross(glm::vec3(planes[Right]), glm::vec3(planes[Far])),
|
||||
glm::cross(glm::vec3(planes[Bottom]), glm::vec3(planes[Top])),
|
||||
glm::cross(glm::vec3(planes[Bottom]), glm::vec3(planes[Near])),
|
||||
glm::cross(glm::vec3(planes[Bottom]), glm::vec3(planes[Far])),
|
||||
glm::cross(glm::vec3(planes[Top]), glm::vec3(planes[Near])),
|
||||
glm::cross(glm::vec3(planes[Top]), glm::vec3(planes[Far])),
|
||||
glm::cross(glm::vec3(planes[Near]), glm::vec3(planes[Far]))};
|
||||
|
||||
m_points[0] = intersection<Left, Bottom, Near>(crosses);
|
||||
m_points[1] = intersection<Left, Top, Near>(crosses);
|
||||
m_points[2] = intersection<Right, Bottom, Near>(crosses);
|
||||
m_points[3] = intersection<Right, Top, Near>(crosses);
|
||||
m_points[4] = intersection<Left, Bottom, Far>(crosses);
|
||||
m_points[5] = intersection<Left, Top, Far>(crosses);
|
||||
m_points[6] = intersection<Right, Bottom, Far>(crosses);
|
||||
m_points[7] = intersection<Right, Top, Far>(crosses);
|
||||
points[0] = intersection<Left, Bottom, Near>(crosses);
|
||||
points[1] = intersection<Left, Top, Near>(crosses);
|
||||
points[2] = intersection<Right, Bottom, Near>(crosses);
|
||||
points[3] = intersection<Right, Top, Near>(crosses);
|
||||
points[4] = intersection<Left, Bottom, Far>(crosses);
|
||||
points[5] = intersection<Left, Top, Far>(crosses);
|
||||
points[6] = intersection<Right, Bottom, Far>(crosses);
|
||||
points[7] = intersection<Right, Top, Far>(crosses);
|
||||
|
||||
minPoint = maxPoint = points[0];
|
||||
for (int i = 1; i < 8; i++) {
|
||||
minPoint = glm::min(minPoint, points[i]);
|
||||
maxPoint = glm::max(maxPoint, points[i]);
|
||||
}
|
||||
}
|
||||
|
||||
inline bool Frustum::isBoxVisible(const glm::vec3& minp, const glm::vec3& maxp)
|
||||
const {
|
||||
// check box outside/inside of frustum
|
||||
for (int i = 0; i < Count; i++) {
|
||||
if ((glm::dot(m_planes[i], glm::vec4(minp.x, minp.y, minp.z, 1.0f)) <
|
||||
0.0) &&
|
||||
(glm::dot(m_planes[i], glm::vec4(maxp.x, minp.y, minp.z, 1.0f)) <
|
||||
0.0) &&
|
||||
(glm::dot(m_planes[i], glm::vec4(minp.x, maxp.y, minp.z, 1.0f)) <
|
||||
0.0) &&
|
||||
(glm::dot(m_planes[i], glm::vec4(maxp.x, maxp.y, minp.z, 1.0f)) <
|
||||
0.0) &&
|
||||
(glm::dot(m_planes[i], glm::vec4(minp.x, minp.y, maxp.z, 1.0f)) <
|
||||
0.0) &&
|
||||
(glm::dot(m_planes[i], glm::vec4(maxp.x, minp.y, maxp.z, 1.0f)) <
|
||||
0.0) &&
|
||||
(glm::dot(m_planes[i], glm::vec4(minp.x, maxp.y, maxp.z, 1.0f)) <
|
||||
0.0) &&
|
||||
(glm::dot(m_planes[i], glm::vec4(maxp.x, maxp.y, maxp.z, 1.0f)) <
|
||||
0.0)) {
|
||||
const glm::vec4& p = planes[i];
|
||||
glm::vec3 v(
|
||||
p.x > 0.0f ? maxp.x : minp.x,
|
||||
p.y > 0.0f ? maxp.y : minp.y,
|
||||
p.z > 0.0f ? maxp.z : minp.z
|
||||
);
|
||||
if (glm::dot(glm::vec3(p), v) + p.w < 0.0f) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// check frustum outside/inside box
|
||||
int out;
|
||||
out = 0;
|
||||
for (int i = 0; i < 8; i++) out += ((m_points[i].x > maxp.x) ? 1 : 0);
|
||||
if (out == 8) return false;
|
||||
out = 0;
|
||||
for (int i = 0; i < 8; i++) out += ((m_points[i].x < minp.x) ? 1 : 0);
|
||||
if (out == 8) return false;
|
||||
out = 0;
|
||||
for (int i = 0; i < 8; i++) out += ((m_points[i].y > maxp.y) ? 1 : 0);
|
||||
if (out == 8) return false;
|
||||
out = 0;
|
||||
for (int i = 0; i < 8; i++) out += ((m_points[i].y < minp.y) ? 1 : 0);
|
||||
if (out == 8) return false;
|
||||
out = 0;
|
||||
for (int i = 0; i < 8; i++) out += ((m_points[i].z > maxp.z) ? 1 : 0);
|
||||
if (out == 8) return false;
|
||||
out = 0;
|
||||
for (int i = 0; i < 8; i++) out += ((m_points[i].z < minp.z) ? 1 : 0);
|
||||
if (out == 8) return false;
|
||||
|
||||
return true;
|
||||
return !(minPoint.x > maxp.x || maxPoint.x < minp.x ||
|
||||
minPoint.y > maxp.y || maxPoint.y < minp.y ||
|
||||
minPoint.z > maxp.z || maxPoint.z < minp.z);
|
||||
}
|
||||
|
||||
template <Frustum::Planes a, Frustum::Planes b, Frustum::Planes c>
|
||||
inline glm::vec3 Frustum::intersection(const glm::vec3* crosses) const {
|
||||
float D = glm::dot(glm::vec3(m_planes[a]), crosses[ij2k<b, c>::k]);
|
||||
float d = glm::dot(glm::vec3(planes[a]), crosses[ij2k<b, c>::k]);
|
||||
glm::vec3 res = glm::mat3(
|
||||
crosses[ij2k<b, c>::k],
|
||||
-crosses[ij2k<a, c>::k],
|
||||
crosses[ij2k<a, b>::k]
|
||||
) *
|
||||
glm::vec3(m_planes[a].w, m_planes[b].w, m_planes[c].w);
|
||||
return res * (-1.0f / D);
|
||||
glm::vec3(planes[a].w, planes[b].w, planes[c].w);
|
||||
return res * (-1.0f / d);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue