vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
culling.h
1#pragma once
2
3#include <glm/glm.hpp>
4#include <glm/gtc/constants.hpp>
5
6#include "core/math/bounds.h"
7#include "core/math/frustum.h"
8
9namespace Vkm::Engine {
10
15 Math::Frustum frustum;
16 glm::vec3 cameraPosition;
17 glm::mat4 view;
18
19 float minPixels;
21 float maxDistanceSquared;
24 float lodDistanceScale = 1.0f;
25
31 bool perspective = true;
32};
33
37namespace Culling {
38
46inline bool isNearEnough(const Math::AABB& bounds, const VisibilityContext& context) {
47 if (context.maxDistance <= 0.0f) return true;
48
49 const glm::vec3 delta = bounds.center() - context.cameraPosition;
50 return glm::dot(delta, delta) <= context.maxDistanceSquared;
51}
52
63inline bool isLargeEnough(const Math::AABB& bounds, const VisibilityContext& context) {
64 if (context.minPixels <= 0.0f || !context.perspective) return true;
65
66 const glm::vec3 worldHalfExtent = bounds.halfExtent();
67 const float worldRadiusSq = glm::dot(worldHalfExtent, worldHalfExtent);
68
69 const glm::vec3 viewCenter = glm::vec3(context.view * glm::vec4(bounds.center(), 1.0f));
70 const float depth = -viewCenter.z;
71 if (depth <= glm::epsilon<float>()) return true;
72
73 return worldRadiusSq >= context.screenSizeThresholdSq * (depth * depth);
74}
75
76} // namespace Culling
77
78} // namespace Vkm::Engine
The culls past the frustum test, which is Math::frustumIntersectsAABB.
Definition culling.h:37
bool isNearEnough(const Math::AABB &bounds, const VisibilityContext &context)
Distance culling: whether the AABB's centre is within maxDistance of the camera.
Definition culling.h:46
bool isLargeEnough(const Math::AABB &bounds, const VisibilityContext &context)
Screen-size culling: whether the AABB's bounding sphere projects to minPixels or more.
Definition culling.h:63
An axis-aligned box.
Definition bounds.h:14
View frustum for culling: six planes from a view-projection matrix.
Definition frustum.h:16
Per-frame data the culls read.
Definition culling.h:14
float screenSizeThresholdSq
Definition culling.h:22
float lodDistanceScale
LOD::REFERENCE_P11 over the view's projection[1][1]: what a distance counts for in LOD selection.
Definition culling.h:24
float maxDistance
Distance beyond which distance culling rejects; <= 0 disables.
Definition culling.h:20
glm::vec3 cameraPosition
World space.
Definition culling.h:16
float minPixels
Pixels below which screen-size culling rejects; <= 0 disables.
Definition culling.h:19
bool perspective
Whether the view divides by depth.
Definition culling.h:31