Cogs.Core
FrustumClassification.cpp
1#include "Foundation/Logging/Logger.h"
2#include "Utilities/FrustumClassification.h"
3
4#include <glm/gtc/packing.hpp>
5
6namespace {
7 Cogs::Logging::Log logger = Cogs::Logging::getLogger("FrustumClassification");
8}
9
10Cogs::Core::FrustumPlanes Cogs::Core::frustumClassifyBoundingBox(const glm::mat4& localToClip,
11 const glm::vec3& min,
12 const glm::vec3& max,
13 const float discardThreshold,
14 const float keepThreshold)
15{
16 FrustumPlanes retval = FrustumPlanes::InsideNone;
17
18 float lx[2], ly[2];
19
20 for (int i = 0; i < 8; i++) {
21 glm::vec3 p((i & 1) ? min.x : max.x,
22 (i & 2) ? min.y : max.y,
23 (i & 4) ? min.z : max.z);
24 glm::vec4 h = localToClip * glm::vec4(p.x, p.y, p.z, 1.f);
25 retval |= (h.x <= h.w ? FrustumPlanes::InsidePosX : FrustumPlanes::InsideNone) |
26 (-h.w <= h.x ? FrustumPlanes::InsideNegX : FrustumPlanes::InsideNone) |
27 (h.y <= h.w ? FrustumPlanes::InsidePosY : FrustumPlanes::InsideNone) |
28 (-h.w <= h.y ? FrustumPlanes::InsideNegY : FrustumPlanes::InsideNone) |
29 (h.z <= h.w ? FrustumPlanes::InsidePosZ : FrustumPlanes::InsideNone) |
30 (-h.w <= h.z ? FrustumPlanes::InsideNegZ : FrustumPlanes::InsideNone);
31 float rcp_w = 1.f / h.w;
32 float px = rcp_w * h.x;
33 float py = rcp_w * h.y;
34 lx[0] = ((i == 0) || (px < lx[0])) ? px : lx[0];
35 lx[1] = ((i == 0) || (lx[1] < px)) ? px : lx[1];
36 ly[0] = ((i == 0) || (py < ly[0])) ? py : ly[0];
37 ly[1] = ((i == 0) || (ly[1] < py)) ? py : ly[1];
38 }
39 bool discard = (lx[1] - lx[0])*(ly[1] - ly[0]) < discardThreshold;
40 if (discard) {
41 auto diff = max - min;
42 if (glm::any(glm::greaterThan(diff, glm::vec3(keepThreshold)))) {
43 discard = false;
44 }
45 }
46 return discard ? FrustumPlanes::InsideNone : retval;
47}
48
49Cogs::Core::FrustumPlanes Cogs::Core::frustumClassifyBoundingBox(const glm::mat4& localToClip,
50 const glm::vec3& min,
51 const glm::vec3& max)
52{
53 FrustumPlanes retval = FrustumPlanes::InsideNone;
54 for (int i = 0; i < 8; i++) {
55 glm::vec3 p((i & 1) ? min.x : max.x,
56 (i & 2) ? min.y : max.y,
57 (i & 4) ? min.z : max.z);
58 glm::vec4 h = localToClip * glm::vec4(p.x, p.y, p.z, 1.f);
59 retval |= (h.x <= h.w ? FrustumPlanes::InsidePosX : FrustumPlanes::InsideNone) |
60 (-h.w <= h.x ? FrustumPlanes::InsideNegX : FrustumPlanes::InsideNone) |
61 (h.y <= h.w ? FrustumPlanes::InsidePosY : FrustumPlanes::InsideNone) |
62 (-h.w <= h.y ? FrustumPlanes::InsideNegY : FrustumPlanes::InsideNone) |
63 (h.z <= h.w ? FrustumPlanes::InsidePosZ : FrustumPlanes::InsideNone) |
64 (-h.w <= h.z ? FrustumPlanes::InsideNegZ : FrustumPlanes::InsideNone);
65 }
66 return retval;
67}
Log implementation class.
Definition: LogManager.h:139
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180