mirror of
https://github.com/OpenMW/openmw.git
synced 2025-04-01 22:06:40 +00:00
Avoid redundant copy for LandData underlying data
This commit is contained in:
parent
955790dc31
commit
53c3f95ac8
2 changed files with 42 additions and 42 deletions
|
@ -2,67 +2,64 @@
|
|||
|
||||
#include "esmterrain.hpp"
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr std::uint16_t textures[ESM::Land::LAND_NUM_TEXTURES]{ 0 };
|
||||
|
||||
std::unique_ptr<const ESM::Land::LandData> loadData(const ESM::Land& land, int loadFlags)
|
||||
{
|
||||
std::unique_ptr<ESM::Land::LandData> result = std::make_unique<ESM::Land::LandData>();
|
||||
land.loadData(loadFlags, *result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
ESM::LandData::LandData(const ESM::Land& land, int loadFlags)
|
||||
: mLoadFlags(loadFlags)
|
||||
: mData(loadData(land, loadFlags))
|
||||
, mLoadFlags(mData->mDataLoaded)
|
||||
, mMinHeight(mData->mMinHeight)
|
||||
, mMaxHeight(mData->mMaxHeight)
|
||||
, mSize(Constants::CellSizeInUnits)
|
||||
, mLandSize(ESM::Land::LAND_SIZE)
|
||||
, mPlugin(land.getPlugin())
|
||||
, mHeights(mData->mHeights)
|
||||
, mNormals(mData->mNormals)
|
||||
, mColors(mData->mColours)
|
||||
, mTextures(mData->mTextures)
|
||||
{
|
||||
ESM::Land::LandData data;
|
||||
land.loadData(loadFlags, data);
|
||||
mLoadFlags = data.mDataLoaded;
|
||||
std::span<const float> heights(data.mHeights);
|
||||
mHeights = std::vector(heights.begin(), heights.end());
|
||||
|
||||
std::span<const std::int8_t> normals(data.mNormals);
|
||||
mNormals = std::vector(normals.begin(), normals.end());
|
||||
|
||||
std::span<const std::uint8_t> colors(data.mColours);
|
||||
mColors = std::vector(colors.begin(), colors.end());
|
||||
|
||||
std::span<const uint16_t> textures(data.mTextures);
|
||||
mTextures = std::vector(textures.begin(), textures.end());
|
||||
|
||||
mMinHeight = data.mMinHeight;
|
||||
mMaxHeight = data.mMaxHeight;
|
||||
}
|
||||
|
||||
ESM::LandData::LandData(const ESM4::Land& land, int /*loadFlags*/)
|
||||
: mLoadFlags(land.mDataTypes) // ESM4::Land is always fully loaded. TODO: implement lazy loading
|
||||
, mHeightsData(ESM4::Land::sLandNumVerts)
|
||||
, mMinHeight(std::numeric_limits<float>::max())
|
||||
, mMaxHeight(std::numeric_limits<float>::lowest())
|
||||
, mSize(Constants::ESM4CellSizeInUnits)
|
||||
, mLandSize(ESM4::Land::sVertsPerSide)
|
||||
, mNormals(land.mVertNorm)
|
||||
, mColors(land.mVertColr)
|
||||
, mTextures(textures)
|
||||
{
|
||||
|
||||
mMinHeight = std::numeric_limits<float>::max();
|
||||
mMaxHeight = std::numeric_limits<float>::lowest();
|
||||
mHeights.resize(ESM4::Land::sLandNumVerts);
|
||||
mTextures.resize(ESM::Land::LAND_NUM_TEXTURES);
|
||||
std::fill(mTextures.begin(), mTextures.end(), 0);
|
||||
|
||||
float row_offset = land.mHeightMap.heightOffset;
|
||||
float rowOffset = land.mHeightMap.heightOffset;
|
||||
for (int y = 0; y < mLandSize; y++)
|
||||
{
|
||||
row_offset += land.mHeightMap.gradientData[y * mLandSize];
|
||||
rowOffset += land.mHeightMap.gradientData[y * mLandSize];
|
||||
|
||||
const float heightY = row_offset * ESM4::Land::sHeightScale;
|
||||
mHeights[y * mLandSize] = heightY;
|
||||
const float heightY = rowOffset * ESM4::Land::sHeightScale;
|
||||
mHeightsData[y * mLandSize] = heightY;
|
||||
mMinHeight = std::min(mMinHeight, heightY);
|
||||
mMaxHeight = std::max(mMaxHeight, heightY);
|
||||
|
||||
float colOffset = row_offset;
|
||||
float colOffset = rowOffset;
|
||||
for (int x = 1; x < mLandSize; x++)
|
||||
{
|
||||
colOffset += land.mHeightMap.gradientData[y * mLandSize + x];
|
||||
const float heightX = colOffset * ESM4::Land::sHeightScale;
|
||||
mMinHeight = std::min(mMinHeight, heightX);
|
||||
mMaxHeight = std::max(mMaxHeight, heightX);
|
||||
mHeights[x + y * mLandSize] = heightX;
|
||||
mHeightsData[x + y * mLandSize] = heightX;
|
||||
}
|
||||
}
|
||||
|
||||
std::span<const std::int8_t> normals(land.mVertNorm);
|
||||
mNormals = std::vector(normals.begin(), normals.end());
|
||||
|
||||
std::span<const std::uint8_t> colors(land.mVertColr);
|
||||
mColors = std::vector(colors.begin(), colors.end());
|
||||
mHeights = mHeightsData;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define COMPONENTS_ESM_ESMTERRAIN
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
|
||||
|
@ -15,8 +16,8 @@ namespace ESM
|
|||
public:
|
||||
~LandData() = default;
|
||||
LandData() = default;
|
||||
LandData(const ESM::Land& Land, int loadFLags);
|
||||
LandData(const ESM4::Land& Land, int loadFLags);
|
||||
LandData(const ESM::Land& Land, int loadFlags);
|
||||
LandData(const ESM4::Land& Land, int loadFlags);
|
||||
|
||||
std::span<const float> getHeights() const { return mHeights; }
|
||||
std::span<const std::int8_t> getNormals() const { return mNormals; }
|
||||
|
@ -30,16 +31,18 @@ namespace ESM
|
|||
int getPlugin() const { return mPlugin; }
|
||||
|
||||
private:
|
||||
std::unique_ptr<const ESM::Land::LandData> mData;
|
||||
int mLoadFlags = 0;
|
||||
std::vector<float> mHeightsData;
|
||||
float mMinHeight = 0.f;
|
||||
float mMaxHeight = 0.f;
|
||||
float mSize = 0.f;
|
||||
int mLandSize = 0;
|
||||
int mPlugin = 0;
|
||||
std::vector<float> mHeights;
|
||||
std::vector<std::int8_t> mNormals;
|
||||
std::vector<std::uint8_t> mColors;
|
||||
std::vector<std::uint16_t> mTextures;
|
||||
std::span<const float> mHeights;
|
||||
std::span<const std::int8_t> mNormals;
|
||||
std::span<const std::uint8_t> mColors;
|
||||
std::span<const std::uint16_t> mTextures;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue