vkmEngine 1.0.15
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 // To the box's nearest point, not its centre: a field the camera stands on is near, wherever
50 // its middle lies.
51 const glm::vec3 nearest = glm::clamp(context.cameraPosition, bounds.min, bounds.max);
52 const glm::vec3 delta = nearest - context.cameraPosition;
53 return glm::dot(delta, delta) <= context.maxDistanceSquared;
54}
55
66inline bool isLargeEnough(const Math::AABB& bounds, const VisibilityContext& context) {
67 if (context.minPixels <= 0.0f || !context.perspective) return true;
68
69 const glm::vec3 worldHalfExtent = bounds.halfExtent();
70 const float worldRadiusSq = glm::dot(worldHalfExtent, worldHalfExtent);
71
72 const glm::vec3 viewCenter = glm::vec3(context.view * glm::vec4(bounds.center(), 1.0f));
73 const float depth = -viewCenter.z;
74 if (depth <= glm::epsilon<float>()) return true;
75
76 return worldRadiusSq >= context.screenSizeThresholdSq * (depth * depth);
77}
78
79} // namespace Culling
80
81} // 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 nearest point 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:66
An axis-aligned box.
Definition bounds.h:17
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