mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-04-09 21:44:54 +00:00
Avoid redundant copy for LandData underlying data
This commit is contained in:
parent
955790dc31
commit
53c3f95ac8
@ -2,67 +2,64 @@
|
|||||||
|
|
||||||
#include "esmterrain.hpp"
|
#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)
|
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)
|
, mSize(Constants::CellSizeInUnits)
|
||||||
, mLandSize(ESM::Land::LAND_SIZE)
|
, mLandSize(ESM::Land::LAND_SIZE)
|
||||||
, mPlugin(land.getPlugin())
|
, 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*/)
|
ESM::LandData::LandData(const ESM4::Land& land, int /*loadFlags*/)
|
||||||
: mLoadFlags(land.mDataTypes) // ESM4::Land is always fully loaded. TODO: implement lazy loading
|
: 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)
|
, mSize(Constants::ESM4CellSizeInUnits)
|
||||||
, mLandSize(ESM4::Land::sVertsPerSide)
|
, mLandSize(ESM4::Land::sVertsPerSide)
|
||||||
|
, mNormals(land.mVertNorm)
|
||||||
|
, mColors(land.mVertColr)
|
||||||
|
, mTextures(textures)
|
||||||
{
|
{
|
||||||
|
float rowOffset = land.mHeightMap.heightOffset;
|
||||||
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;
|
|
||||||
for (int y = 0; y < mLandSize; y++)
|
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;
|
const float heightY = rowOffset * ESM4::Land::sHeightScale;
|
||||||
mHeights[y * mLandSize] = heightY;
|
mHeightsData[y * mLandSize] = heightY;
|
||||||
mMinHeight = std::min(mMinHeight, heightY);
|
mMinHeight = std::min(mMinHeight, heightY);
|
||||||
mMaxHeight = std::max(mMaxHeight, heightY);
|
mMaxHeight = std::max(mMaxHeight, heightY);
|
||||||
|
|
||||||
float colOffset = row_offset;
|
float colOffset = rowOffset;
|
||||||
for (int x = 1; x < mLandSize; x++)
|
for (int x = 1; x < mLandSize; x++)
|
||||||
{
|
{
|
||||||
colOffset += land.mHeightMap.gradientData[y * mLandSize + x];
|
colOffset += land.mHeightMap.gradientData[y * mLandSize + x];
|
||||||
const float heightX = colOffset * ESM4::Land::sHeightScale;
|
const float heightX = colOffset * ESM4::Land::sHeightScale;
|
||||||
mMinHeight = std::min(mMinHeight, heightX);
|
mMinHeight = std::min(mMinHeight, heightX);
|
||||||
mMaxHeight = std::max(mMaxHeight, 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);
|
mHeights = mHeightsData;
|
||||||
mNormals = std::vector(normals.begin(), normals.end());
|
|
||||||
|
|
||||||
std::span<const std::uint8_t> colors(land.mVertColr);
|
|
||||||
mColors = std::vector(colors.begin(), colors.end());
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#define COMPONENTS_ESM_ESMTERRAIN
|
#define COMPONENTS_ESM_ESMTERRAIN
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <memory>
|
||||||
#include <span>
|
#include <span>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -15,8 +16,8 @@ namespace ESM
|
|||||||
public:
|
public:
|
||||||
~LandData() = default;
|
~LandData() = default;
|
||||||
LandData() = default;
|
LandData() = default;
|
||||||
LandData(const ESM::Land& Land, int loadFLags);
|
LandData(const ESM::Land& Land, int loadFlags);
|
||||||
LandData(const ESM4::Land& Land, int loadFLags);
|
LandData(const ESM4::Land& Land, int loadFlags);
|
||||||
|
|
||||||
std::span<const float> getHeights() const { return mHeights; }
|
std::span<const float> getHeights() const { return mHeights; }
|
||||||
std::span<const std::int8_t> getNormals() const { return mNormals; }
|
std::span<const std::int8_t> getNormals() const { return mNormals; }
|
||||||
@ -30,16 +31,18 @@ namespace ESM
|
|||||||
int getPlugin() const { return mPlugin; }
|
int getPlugin() const { return mPlugin; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
std::unique_ptr<const ESM::Land::LandData> mData;
|
||||||
int mLoadFlags = 0;
|
int mLoadFlags = 0;
|
||||||
|
std::vector<float> mHeightsData;
|
||||||
float mMinHeight = 0.f;
|
float mMinHeight = 0.f;
|
||||||
float mMaxHeight = 0.f;
|
float mMaxHeight = 0.f;
|
||||||
float mSize = 0.f;
|
float mSize = 0.f;
|
||||||
int mLandSize = 0;
|
int mLandSize = 0;
|
||||||
int mPlugin = 0;
|
int mPlugin = 0;
|
||||||
std::vector<float> mHeights;
|
std::span<const float> mHeights;
|
||||||
std::vector<std::int8_t> mNormals;
|
std::span<const std::int8_t> mNormals;
|
||||||
std::vector<std::uint8_t> mColors;
|
std::span<const std::uint8_t> mColors;
|
||||||
std::vector<std::uint16_t> mTextures;
|
std::span<const std::uint16_t> mTextures;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user