Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/rocky/Horizon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* MIT License
*/
#include "Horizon.h"
#include <cmath>

using namespace ROCKY_NAMESPACE;

Expand Down Expand Up @@ -123,9 +124,21 @@ Horizon::isVisible(double x, double y, double z, double radius) const
VT = target - _eye;
double a = glm::dot(VT, -_eyeUnit);
double b = a * _coneTan;
double c = sqrt(glm::dot(VT, VT) - a * a);
double VTdotVT = glm::dot(VT, VT);
double cSquared = VTdotVT - a * a;

// Guard against numerical precision issues that could produce negative value
if (cSquared < 0.0)
cSquared = 0.0;

double c = sqrt(cSquared);
double d = c - b;
double e = d * _coneCos;

// Check for NaN in the calculation (indicates numerical instability)
if (std::isnan(e))
return true; // Default to visible if we can't determine

if (e > -radius)
return true;

Expand Down
7 changes: 5 additions & 2 deletions src/rocky/vsg/terrain/SurfaceNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,19 @@ namespace ROCKY_NAMESPACE
auto viewID = rv.getCommandBuffer()->viewID;
auto& horizon = (*_horizon)[viewID];

// Use the bounding sphere radius as margin for more conservative culling
double cullRadius = worldBoundingSphere.radius;

if (_horizonCullingPoint_valid)
{
return horizon.isVisible(_horizonCullingPoint);
return horizon.isVisible(_horizonCullingPoint, cullRadius);
}
else
{
for (int p = 0; p < 4; ++p)
{
auto& wp = _worldPoints[p];
if (horizon.isVisible(wp.x, wp.y, wp.z))
if (horizon.isVisible(wp.x, wp.y, wp.z, cullRadius))
return true;
}
return false;
Expand Down