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

Minor optimisations and tweaks #6087

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/BaseSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class BaseSphere {
virtual void Update() = 0;
virtual void Render(Graphics::Renderer *renderer, const matrix4x4d &modelView, vector3d campos, const float radius, const std::vector<Camera::Shadow> &shadows) = 0;

virtual double GetHeight(const vector3d &p) const { return 0.0; }
virtual double GetTerrainHeight(const vector3d &p) const = 0;

static void Init(Graphics::Renderer *renderer);
static void Uninit();
Expand Down
2 changes: 1 addition & 1 deletion src/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ void Camera::CalcLighting(const Body *b, double &ambient, double &direct) const
vector3d upDir = b->GetInterpPositionRelTo(rotFrame);
const double planetRadius = planet->GetSystemBody()->GetRadius();
const double dist = std::max(planetRadius, upDir.Length());
upDir = upDir.Normalized();
upDir.Normalize();

double pressure, density;
planet->GetAtmosphericState(dist, &pressure, &density);
Expand Down
2 changes: 1 addition & 1 deletion src/GasGiant.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GasGiant : public BaseSphere {
void Update() override;
void Render(Graphics::Renderer *renderer, const matrix4x4d &modelView, vector3d campos, const float radius, const std::vector<Camera::Shadow> &shadows) override;

double GetHeight(const vector3d &p) const final { return 0.0; }
double GetTerrainHeight(const vector3d &p) const final { return 0.0; }

// in sbody radii
double GetMaxFeatureHeight() const override { return 0.0; }
Expand Down
47 changes: 31 additions & 16 deletions src/GeoPatchJobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,32 @@ void SSingleSplitRequest::GenerateMesh() const
{
PROFILE_SCOPED()
const int borderedEdgeLen = edgeLen + (BORDER_SIZE * 2);
#ifndef NDEBUG
const int numBorderedVerts = borderedEdgeLen * borderedEdgeLen;
#endif

// generate heights plus a 1 unit border
double *bhts = borderHeights.get();
vector3d *vrts = borderVertexs.get();
std::vector<vector3d> positions;
positions.reserve(numBorderedVerts);
std::vector<double> heightsOut;
heightsOut.resize(numBorderedVerts);
for (int y = -BORDER_SIZE; y < borderedEdgeLen - BORDER_SIZE; y++) {
const double yfrac = double(y) * fracStep;
for (int x = -BORDER_SIZE; x < borderedEdgeLen - BORDER_SIZE; x++) {
const double xfrac = double(x) * fracStep;
const vector3d p = GetSpherePoint(v0, v1, v2, v3, xfrac, yfrac);
const double height = pTerrain->GetHeight(p);
assert(height >= 0.0f && height <= 1.0f);
*(bhts++) = height;
*(vrts++) = p * (height + 1.0);
positions.emplace_back(p);
}
}
pTerrain->GetHeights(positions, heightsOut);

double *bhts = borderHeights.get();
vector3d *vrts = borderVertexs.get();
for (int iHeights = 0; iHeights < numBorderedVerts; iHeights++)
{
const vector3d &p = positions[iHeights];
const double height = heightsOut[iHeights];
*(bhts++) = height;
*(vrts++) = p * (height + 1.0);
}
assert(bhts == &borderHeights.get()[numBorderedVerts]);

// Generate normals & colors for non-edge vertices since they never change
Expand Down Expand Up @@ -190,24 +198,31 @@ void SQuadSplitRequest::GenerateBorderedData() const
{
PROFILE_SCOPED()
const int borderedEdgeLen = (edgeLen * 2) + (BORDER_SIZE * 2) - 1;
#ifndef NDEBUG
const int numBorderedVerts = borderedEdgeLen * borderedEdgeLen;
#endif

// generate heights plus a N=BORDER_SIZE unit border
double *bhts = borderHeights.get();
vector3d *vrts = borderVertexs.get();
std::vector<vector3d> positions;
positions.reserve(numBorderedVerts);
std::vector<double> heightsOut;
heightsOut.resize(numBorderedVerts);
for (int y = -BORDER_SIZE; y < (borderedEdgeLen - BORDER_SIZE); y++) {
const double yfrac = double(y) * (fracStep * 0.5);
for (int x = -BORDER_SIZE; x < (borderedEdgeLen - BORDER_SIZE); x++) {
const double xfrac = double(x) * (fracStep * 0.5);
const vector3d p = GetSpherePoint(v0, v1, v2, v3, xfrac, yfrac);
const double height = pTerrain->GetHeight(p);
assert(height >= 0.0f && height <= 1.0f);
*(bhts++) = height;
*(vrts++) = p * (height + 1.0);
positions.emplace_back(p);
}
}
pTerrain->GetHeights(positions, heightsOut);

double *bhts = borderHeights.get();
vector3d *vrts = borderVertexs.get();
for (int iHeights = 0; iHeights < numBorderedVerts; iHeights++) {
const vector3d &p = positions[iHeights];
const double height = heightsOut[iHeights];
*(bhts++) = height;
*(vrts++) = p * (height + 1.0);
}
assert(bhts == &borderHeights[numBorderedVerts]);
}

Expand Down
9 changes: 6 additions & 3 deletions src/GeoSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ class GeoSphere : public BaseSphere {
void Update() override;
void Render(Graphics::Renderer *renderer, const matrix4x4d &modelView, vector3d campos, const float radius, const std::vector<Camera::Shadow> &shadows) override;

double GetHeight(const vector3d &p) const final
double GetTerrainHeight(const vector3d &p) const final
{
const double h = m_terrain->GetHeight(p);
std::vector<vector3d> pv{p};
std::vector<double> hv(size_t(1));
m_terrain->GetHeights(pv, hv);
const double h = hv[0];
#ifndef NDEBUG
// XXX don't remove this. Fix your fractals instead
// Fractals absolutely MUST return heights >= 0.0 (one planet radius)
// otherwise atmosphere and other things break.
if (h < 0.0) {
Output("GetHeight({ %f, %f, %f }) returned %f\n", p.x, p.y, p.z, h);
Output("GetTerrainHeight({ %f, %f, %f }) returned %f\n", p.x, p.y, p.z, h);
m_terrain->DebugDump();
assert(h >= 0.0);
}
Expand Down
11 changes: 11 additions & 0 deletions src/ObjectViewerView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,17 @@ void ObjectViewerView::DrawControlsWindow()
bool didChange = false;
uint32_t prevSeed = m_state.seed;

// if we can get the TerrainBody display the height and color fractal names, very useful for dev and debugging
if (const TerrainBody *terrainBody = static_cast<const TerrainBody *>(m_targetBody)) {
const char *heightFractalName = terrainBody->GetHeightFractalName();
const char *colorFractalName = terrainBody->GetColorFractalName();
if (heightFractalName) ImGui::Text("Height fractal: %s", heightFractalName);
if (colorFractalName) ImGui::Text("Color fractal: %s", colorFractalName);
ImGui::Spacing();
ImGui::Separator();
ImGui::Spacing();
}

ImGui::TextUnformatted("Seed");
int *seed = reinterpret_cast<int32_t *>(&m_state.seed);
didChange |= ImGui::DragInt("##seed", seed);
Expand Down
2 changes: 1 addition & 1 deletion src/ShipAICmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ bool AICmdKill::TimeStepUpdate()
// ok, so now pick new direction
vector3d targdir = m_target->GetPositionRelTo(m_ship).Normalized();
vector3d tdir1 = targdir.Cross(vector3d(targdir.z+0.1, targdir.x, targdir.y));
tdir1 = tdir1.Normalized();
tdir1.Normalize();
vector3d tdir2 = targdir.Cross(tdir1);

double d1 = Pi::rng.Double() - 0.5;
Expand Down
19 changes: 18 additions & 1 deletion src/TerrainBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ double TerrainBody::GetTerrainHeight(const vector3d &pos_) const
{
double radius = m_sbody->GetRadius();
if (m_baseSphere) {
return radius * (1.0 + m_baseSphere->GetHeight(pos_));
return radius * (1.0 + m_baseSphere->GetTerrainHeight(pos_));
} else {
assert(0);
return radius;
Expand All @@ -155,3 +155,20 @@ void TerrainBody::OnChangeDetailLevel(Graphics::Renderer *r)
{
BaseSphere::OnChangeDetailLevel(r);
}

const char *TerrainBody::GetHeightFractalName() const
{
if (Terrain* terrain = m_baseSphere->GetTerrain())
{
return terrain->GetHeightFractalName();
}
return nullptr;
}

const char* TerrainBody::GetColorFractalName() const
{
if (Terrain *terrain = m_baseSphere->GetTerrain()) {
return terrain->GetColorFractalName();
}
return nullptr;
}
3 changes: 3 additions & 0 deletions src/TerrainBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class TerrainBody : public Body {
// implements calls to all relevant terrain management sub-systems
static void OnChangeDetailLevel(Graphics::Renderer *r);

const char *GetHeightFractalName() const;
const char *GetColorFractalName() const;

protected:
TerrainBody() = delete;
TerrainBody(SystemBody *);
Expand Down
2 changes: 1 addition & 1 deletion src/matrix4x4.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ class matrix4x4 {
vector3<T> x(cell[0], cell[4], cell[8]);
vector3<T> y(cell[1], cell[5], cell[9]);
vector3<T> z(cell[2], cell[6], cell[10]);
x = x.Normalized();
x.Normalize();
z = x.Cross(y).Normalized();
y = z.Cross(x).Normalized();
cell[0] = x.x;
Expand Down
2 changes: 1 addition & 1 deletion src/ship/Propulsion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ double Propulsion::AIFaceUpdir(const vector3d &updir, double av)
vector3d uphead = updir * m_dBody->GetOrient(); // create desired object-space updir
if (uphead.z > 0.99999) return 0; // bail out if facing updir
uphead.z = 0;
uphead = uphead.Normalized(); // only care about roll axis
uphead.Normalize(); // only care about roll axis

double ang = 0.0, dav = 0.0;
if (uphead.y < 0.99999999) {
Expand Down
28 changes: 5 additions & 23 deletions src/terrain/Terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,8 @@ Terrain::Terrain(const SystemBody *body) :
minHMapScld = std::min(minHMapScld, val);
maxHMapScld = std::max(maxHMapScld, val);
// store then increment pointer
(*pHeightMap) = val;
// pre-multiply and offset the height value
(*pHeightMap) = double(val) * m_heightScaling + m_minh;
++pHeightMap;
}
assert(pHeightMap == &m_heightMap[heightmapPixelArea]);
Expand Down Expand Up @@ -491,9 +492,10 @@ Terrain::Terrain(const SystemBody *body) :
m_invPlanetRadius = 1.0 / rad;
m_planetEarthRadii = rad / EARTH_RADIUS;

// NB: I don't know what this does but only the 1st entry was ever used, and only for Neptune and Jupiter colours
m_entropy = m_rand.Double();

// Pick some colors, mainly reds and greens
for (int i = 0; i < int(COUNTOF(m_entropy)); i++)
m_entropy[i] = m_rand.Double();
for (int i = 0; i < int(COUNTOF(m_rockColor)); i++) {
double r, g, b;
r = m_rand.Double(0.3, 1.0);
Expand All @@ -505,8 +507,6 @@ Terrain::Terrain(const SystemBody *body) :
}

// Pick some darker colours mainly reds and greens
for (int i = 0; i < int(COUNTOF(m_entropy)); i++)
m_entropy[i] = m_rand.Double();
for (int i = 0; i < int(COUNTOF(m_darkrockColor)); i++) {
double r, g, b;
r = m_rand.Double(0.05, 0.3);
Expand All @@ -518,8 +518,6 @@ Terrain::Terrain(const SystemBody *body) :
}

// grey colours, in case you simply must have a grey colour on a world with high metallicity
for (int i = 0; i < int(COUNTOF(m_entropy)); i++)
m_entropy[i] = m_rand.Double();
for (int i = 0; i < int(COUNTOF(m_greyrockColor)); i++) {
double g;
g = m_rand.Double(0.3, 0.9);
Expand All @@ -528,8 +526,6 @@ Terrain::Terrain(const SystemBody *body) :

// Pick some plant colours, mainly greens
// TODO take star class into account
for (int i = 0; i < int(COUNTOF(m_entropy)); i++)
m_entropy[i] = m_rand.Double();
for (int i = 0; i < int(COUNTOF(m_plantColor)); i++) {
double r, g, b;
g = m_rand.Double(0.3, 1.0);
Expand All @@ -542,8 +538,6 @@ Terrain::Terrain(const SystemBody *body) :

// Pick some darker plant colours mainly greens
// TODO take star class into account
for (int i = 0; i < int(COUNTOF(m_entropy)); i++)
m_entropy[i] = m_rand.Double();
for (int i = 0; i < int(COUNTOF(m_darkplantColor)); i++) {
double r, g, b;
g = m_rand.Double(0.05, 0.3);
Expand All @@ -556,8 +550,6 @@ Terrain::Terrain(const SystemBody *body) :

// Pick some sand colours, mainly yellow
// TODO let some planetary value scale this colour
for (int i = 0; i < int(COUNTOF(m_entropy)); i++)
m_entropy[i] = m_rand.Double();
for (int i = 0; i < int(COUNTOF(m_sandColor)); i++) {
double r, g, b;
r = m_rand.Double(0.6, 1.0);
Expand All @@ -569,8 +561,6 @@ Terrain::Terrain(const SystemBody *body) :

// Pick some darker sand colours mainly yellow
// TODO let some planetary value scale this colour
for (int i = 0; i < int(COUNTOF(m_entropy)); i++)
m_entropy[i] = m_rand.Double();
for (int i = 0; i < int(COUNTOF(m_darksandColor)); i++) {
double r, g, b;
r = m_rand.Double(0.05, 0.6);
Expand All @@ -582,8 +572,6 @@ Terrain::Terrain(const SystemBody *body) :

// Pick some dirt colours, mainly red/brown
// TODO let some planetary value scale this colour
for (int i = 0; i < int(COUNTOF(m_entropy)); i++)
m_entropy[i] = m_rand.Double();
for (int i = 0; i < int(COUNTOF(m_dirtColor)); i++) {
double r, g, b;
r = m_rand.Double(0.3, 0.7);
Expand All @@ -594,8 +582,6 @@ Terrain::Terrain(const SystemBody *body) :

// Pick some darker dirt colours mainly red/brown
// TODO let some planetary value scale this colour
for (int i = 0; i < int(COUNTOF(m_entropy)); i++)
m_entropy[i] = m_rand.Double();
for (int i = 0; i < int(COUNTOF(m_darkdirtColor)); i++) {
double r, g, b;
r = m_rand.Double(0.05, 0.3);
Expand All @@ -605,8 +591,6 @@ Terrain::Terrain(const SystemBody *body) :
}

// These are used for gas giant colours, they are more m_random and *should* really use volatileGasses - TODO
for (int i = 0; i < int(COUNTOF(m_entropy)); i++)
m_entropy[i] = m_rand.Double();
for (int i = 0; i < int(COUNTOF(m_gglightColor)); i++) {
double r, g, b;
r = m_rand.Double(0.0, 0.5);
Expand All @@ -615,8 +599,6 @@ Terrain::Terrain(const SystemBody *body) :
m_gglightColor[i] = vector3d(r, g, b);
}
//darker gas giant colours, more reds and greens
for (int i = 0; i < int(COUNTOF(m_entropy)); i++)
m_entropy[i] = m_rand.Double();
for (int i = 0; i < int(COUNTOF(m_ggdarkColor)); i++) {
double r, g, b;
r = m_rand.Double(0.0, 0.3);
Expand Down
16 changes: 6 additions & 10 deletions src/terrain/Terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Terrain : public RefCounted {
return m_fracdef[index];
}

virtual double GetHeight(const vector3d &p) const = 0;
virtual void GetHeights(const std::vector<vector3d> &vP, std::vector<double> &heightsOut) const = 0;
virtual vector3d GetColor(const vector3d &p, double height, const vector3d &norm) const = 0;

virtual const char *GetHeightFractalName() const = 0;
Expand Down Expand Up @@ -91,7 +91,7 @@ class Terrain : public RefCounted {
double m_invPlanetRadius;
double m_planetEarthRadii;

double m_entropy[12];
double m_entropy;

vector3d m_rockColor[8];
vector3d m_darkrockColor[8];
Expand Down Expand Up @@ -124,26 +124,22 @@ template <typename HeightFractal>
class TerrainHeightFractal : virtual public Terrain {
public:
TerrainHeightFractal() = delete;
virtual double GetHeight(const vector3d &p) const;
virtual const char *GetHeightFractalName() const;
void GetHeights(const std::vector<vector3d> &positions, std::vector<double> &heightsOut) const final;
const char *GetHeightFractalName() const final;

protected:
TerrainHeightFractal(const SystemBody *body);

private:
};

template <typename ColorFractal>
class TerrainColorFractal : virtual public Terrain {
public:
TerrainColorFractal() = delete;
virtual vector3d GetColor(const vector3d &p, double height, const vector3d &norm) const;
virtual const char *GetColorFractalName() const;
vector3d GetColor(const vector3d &p, double height, const vector3d &norm) const final;
const char *GetColorFractalName() const final;

protected:
TerrainColorFractal(const SystemBody *body);

private:
};

template <typename HeightFractal, typename ColorFractal>
Expand Down
Loading
Loading