Skip to content

Commit

Permalink
Reduce copy and pasted code with IsOverHorizon method
Browse files Browse the repository at this point in the history
  • Loading branch information
fluffyfreak committed Apr 25, 2024
1 parent 43270b2 commit 4f75770
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
50 changes: 21 additions & 29 deletions src/GeoPatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,21 +271,9 @@ void GeoPatch::Render(Graphics::Renderer *renderer, const vector3d &campos, cons
return; // nothing below this patch is visible

// only want to horizon cull patches that can actually be over the horizon!
if (IsOverHorizon(campos))
{
const vector3d camDir(campos - m_centroid);
const vector3d camDirNorm(camDir.Normalized());
const vector3d cenDir(m_centroid.Normalized());
const double dotProd = camDirNorm.Dot(cenDir);

if (dotProd < 0.25 && (camDir.LengthSqr() > (m_clipRadius * m_clipRadius))) {
SSphere obj;
obj.m_centre = m_centroid;
obj.m_radius = m_clipRadius;

if (!s_sph.HorizonCulling(campos, obj)) {
return; // nothing below this patch is visible
}
}
return;
}

if (m_kids[0]) {
Expand Down Expand Up @@ -346,21 +334,8 @@ void GeoPatch::LODUpdate(const vector3d &campos, const Graphics::Frustum &frustu
return; // nothing below this patch is visible

// only want to horizon cull patches that can actually be over the horizon!
{
const vector3d camDir(campos - m_centroid);
const vector3d camDirNorm(camDir.Normalized());
const vector3d cenDir(m_centroid.Normalized());
const double dotProd = camDirNorm.Dot(cenDir);

if (dotProd < 0.25 && (camDir.LengthSqr() > (m_clipRadius * m_clipRadius))) {
SSphere obj;
obj.m_centre = m_centroid;
obj.m_radius = m_clipRadius;

if (!s_sph.HorizonCulling(campos, obj)) {
return; // nothing below this patch is visible
}
}
if (IsOverHorizon(campos)) {
return;
}

// we can see this patch so submit the jobs!
Expand Down Expand Up @@ -470,3 +445,20 @@ void GeoPatch::ReceiveJobHandle(Job::Handle job)
assert(!m_job.HasJob());
m_job = static_cast<Job::Handle &&>(job);
}

bool GeoPatch::IsOverHorizon(const vector3d &camPos)
{
const vector3d camDir(camPos - m_centroid);
const vector3d camDirNorm(camDir.Normalized());
const vector3d cenDir(m_centroid.Normalized());
const double dotProd = camDirNorm.Dot(cenDir);

if (dotProd < 0.25 && (camDir.LengthSqr() > (m_clipRadius * m_clipRadius))) {
// return the result of the Horizon Culling test, inverted to match naming semantic
// eg: HC returns true==visible, but this method returns true==hidden
return !s_sph.HorizonCulling(camPos, SSphere(m_centroid, m_clipRadius));
}

// not over the horizon
return false;
}
2 changes: 2 additions & 0 deletions src/GeoPatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class GeoPatch {
private:
static const int NUM_KIDS = 4;

bool IsOverHorizon(const vector3d &camPos);

RefCountedPtr<GeoPatchContext> m_ctx;
const vector3d m_v0, m_v1, m_v2, m_v3;
std::unique_ptr<double[]> m_heights;
Expand Down

0 comments on commit 4f75770

Please sign in to comment.