Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Terrain Disappearing #5829

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 4 additions & 0 deletions src/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const Color4f Color4f::YELLOW = Color4f(1.0f, 1.0f, 0.0f, 1.0f);
const Color4f Color4f::GRAY = Color4f(0.5f, 0.5f, 0.5f, 1.f);
const Color4f Color4f::STEELBLUE = Color4f(0.27f, 0.51f, 0.71f, 1.f);
const Color4f Color4f::BLANK = Color4f(0.0f, 0.0f, 0.0f, 0.0f);
const Color4f Color4f::CYAN = Color4f(0.0f, 1.0f, 1.0f, 1.0f);

const Color4ub Color::BLACK = Color(0, 0, 0, 255);
const Color4ub Color::WHITE = Color(255, 255, 255, 255);
Expand All @@ -26,6 +27,7 @@ const Color4ub Color::GRAY = Color(128, 128, 128, 255);
const Color4ub Color::STEELBLUE = Color(68, 130, 181, 255);
const Color4ub Color::BLANK = Color(0, 0, 0, 0);
const Color4ub Color::PINK = Color(252, 15, 192, 255); // debug pink
const Color4ub Color::CYAN = Color(0, 255, 255, 255);

const Color3ub Color3ub::BLACK = Color3ub(0, 0, 0);
const Color3ub Color3ub::WHITE = Color3ub(255, 255, 255);
Expand All @@ -35,6 +37,8 @@ const Color3ub Color3ub::BLUE = Color3ub(0, 0, 255);
const Color3ub Color3ub::YELLOW = Color3ub(255, 255, 0);
const Color3ub Color3ub::STEELBLUE = Color3ub(68, 130, 181);
const Color3ub Color3ub::BLANK = Color3ub(0, 0, 0);
const Color3ub Color3ub::PINK = Color3ub(252, 15, 192); // debug pink
const Color3ub Color3ub::CYAN = Color3ub(0, 255, 255);

float Color4f::GetLuminance() const
{
Expand Down
4 changes: 4 additions & 0 deletions src/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ struct Color4f {
static const Color4f GRAY;
static const Color4f STEELBLUE;
static const Color4f BLANK;
static const Color4f CYAN;
};

namespace {
Expand Down Expand Up @@ -162,6 +163,7 @@ struct Color4ub {
static const Color4ub STEELBLUE;
static const Color4ub BLANK;
static const Color4ub PINK;
static const Color4ub CYAN;
};

struct Color3ub {
Expand Down Expand Up @@ -207,6 +209,8 @@ struct Color3ub {
static const Color3ub YELLOW;
static const Color3ub STEELBLUE;
static const Color3ub BLANK;
static const Color3ub PINK;
static const Color3ub CYAN;
};

typedef Color4ub Color;
Expand Down
66 changes: 31 additions & 35 deletions src/GeoPatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,15 @@ void GeoPatch::UpdateVBOs(Graphics::Renderer *renderer)
m_colors.reset();

#ifdef DEBUG_BOUNDING_SPHERES
RefCountedPtr<Graphics::Material> mat(Pi::renderer->CreateMaterial("unlit", Graphics::MaterialDescriptor(), Graphics::RenderStateDesc()));
m_matboundsphere.Reset(Pi::renderer->CreateMaterial("unlit", Graphics::MaterialDescriptor(), Graphics::RenderStateDesc()));
switch (m_depth) {
case 0: mat->diffuse = Color::WHITE; break;
case 1: mat->diffuse = Color::RED; break;
case 2: mat->diffuse = Color::GREEN; break;
case 3: mat->diffuse = Color::BLUE; break;
default: mat->diffuse = Color::BLACK; break;
case 0: m_matboundsphere->diffuse = Color::WHITE; break;
case 1: m_matboundsphere->diffuse = Color::RED; break;
case 2: m_matboundsphere->diffuse = Color::GREEN; break;
case 3: m_matboundsphere->diffuse = Color::BLUE; break;
default: m_matboundsphere->diffuse = Color::PINK; break;
}
m_boundsphere.reset(new Graphics::Drawables::Sphere3D(Pi::renderer, mat, 1, m_clipRadius));
m_boundsphere.reset(new Graphics::Drawables::Sphere3D(Pi::renderer, 1, m_clipRadius));
#endif
}
}
Expand All @@ -266,24 +266,14 @@ void GeoPatch::Render(Graphics::Renderer *renderer, const vector3d &campos, cons
PROFILE_SCOPED()
// must update the VBOs to calculate the clipRadius...
UpdateVBOs(renderer);
// ...before doing the furstum culling that relies on it.
// ...before doing the frustum culling that relies on it.
if (!frustum.TestPoint(m_clipCentroid, m_clipRadius))
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_clipCentroid);
const vector3d camDirNorm(camDir.Normalized());
const vector3d cenDir(m_clipCentroid.Normalized());
const double dotProd = camDirNorm.Dot(cenDir);

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

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

if (m_kids[0]) {
Expand All @@ -310,7 +300,7 @@ void GeoPatch::Render(Graphics::Renderer *renderer, const vector3d &campos, cons
#ifdef DEBUG_BOUNDING_SPHERES
if (m_boundsphere.get()) {
renderer->SetWireFrameMode(true);
m_boundsphere->Draw(renderer);
m_boundsphere->Draw(renderer, m_matboundsphere.Get());
renderer->SetWireFrameMode(false);
}
#endif
Expand Down Expand Up @@ -344,19 +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_clipCentroid);
const vector3d camDirNorm(camDir.Normalized());
const vector3d cenDir(m_clipCentroid.Normalized());
const double dotProd = camDirNorm.Dot(cenDir);

if (dotProd < 0.25 && (camDir.LengthSqr() > (m_clipRadius * m_clipRadius))) {
SSphere obj;
obj.m_centre = m_clipCentroid;
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 @@ -466,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;
}
6 changes: 5 additions & 1 deletion 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 All @@ -104,7 +106,8 @@ class GeoPatch {
GeoPatch *m_parent;
GeoSphere *m_geosphere;
double m_roughLength;
vector3d m_clipCentroid, m_centroid;
vector3d m_clipCentroid; // rendering relative position centroid used for frustum clipping & camera relative rendering calculation
vector3d m_centroid; // geometry centroid used for horizon culling, split request, camera distance test
double m_clipRadius;
Sint32 m_depth;
bool m_needUpdateVBOs;
Expand All @@ -114,6 +117,7 @@ class GeoPatch {
bool m_HasJobRequest;
#ifdef DEBUG_BOUNDING_SPHERES
std::unique_ptr<Graphics::Drawables::Sphere3D> m_boundsphere;
RefCountedPtr<Graphics::Material> m_matboundsphere;
#endif
};

Expand Down
1 change: 0 additions & 1 deletion src/GeoSphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ void GeoSphere::Render(Graphics::Renderer *renderer, const matrix4x4d &modelView
emission = StarSystem::starRealColors[GetSystemBody()->GetType()];
emission.a = 255;
}

else {
// give planet some ambient lighting if the viewer is close to it
double camdist = 0.1 / campos.LengthSqr();
Expand Down
4 changes: 4 additions & 0 deletions src/Sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ struct SSphere {
SSphere(const double rad) :
m_centre(vector3d(0.0)),
m_radius(rad) {}
SSphere(const vector3d &centre, const double rad) :
m_centre(centre),
m_radius(rad) {}

vector3d m_centre;
double m_radius;

Expand Down