|
|
|
@ -40,15 +40,15 @@ namespace ESM
|
|
|
|
|
|
|
|
|
|
// Loads data and marks it as loaded. Return true if data is actually loaded from reader, false otherwise
|
|
|
|
|
// including the case when data is already loaded.
|
|
|
|
|
bool condLoad(ESMReader& reader, int flags, int& targetFlags, int dataFlag, auto& in)
|
|
|
|
|
bool condLoad(ESMReader& reader, int dataTypes, int& targetDataTypes, int dataFlag, auto& in)
|
|
|
|
|
{
|
|
|
|
|
if ((targetFlags & dataFlag) == 0 && (flags & dataFlag) != 0)
|
|
|
|
|
if ((targetDataTypes & dataFlag) == 0 && (dataTypes & dataFlag) != 0)
|
|
|
|
|
{
|
|
|
|
|
if constexpr (std::is_same_v<std::remove_cvref_t<decltype(in)>, VHGT>)
|
|
|
|
|
reader.getSubComposite(in);
|
|
|
|
|
else
|
|
|
|
|
reader.getHT(in);
|
|
|
|
|
targetFlags |= dataFlag;
|
|
|
|
|
targetDataTypes |= dataFlag;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
reader.skipHSub();
|
|
|
|
@ -150,13 +150,13 @@ namespace ESM
|
|
|
|
|
if (mDataTypes & Land::DATA_VHGT)
|
|
|
|
|
{
|
|
|
|
|
VHGT offsets;
|
|
|
|
|
offsets.mHeightOffset = mLandData->mHeights[0] / HEIGHT_SCALE;
|
|
|
|
|
offsets.mHeightOffset = mLandData->mHeights[0] / sHeightScale;
|
|
|
|
|
|
|
|
|
|
float prevY = mLandData->mHeights[0];
|
|
|
|
|
size_t number = 0; // avoid multiplication
|
|
|
|
|
for (unsigned i = 0; i < LandRecordData::sLandSize; ++i)
|
|
|
|
|
{
|
|
|
|
|
float diff = (mLandData->mHeights[number] - prevY) / HEIGHT_SCALE;
|
|
|
|
|
float diff = (mLandData->mHeights[number] - prevY) / sHeightScale;
|
|
|
|
|
offsets.mHeightData[number]
|
|
|
|
|
= diff >= 0 ? static_cast<std::int8_t>(diff + 0.5) : static_cast<std::int8_t>(diff - 0.5);
|
|
|
|
|
|
|
|
|
@ -165,7 +165,7 @@ namespace ESM
|
|
|
|
|
|
|
|
|
|
for (unsigned j = 1; j < LandRecordData::sLandSize; ++j)
|
|
|
|
|
{
|
|
|
|
|
diff = (mLandData->mHeights[number] - prevX) / HEIGHT_SCALE;
|
|
|
|
|
diff = (mLandData->mHeights[number] - prevX) / sHeightScale;
|
|
|
|
|
offsets.mHeightData[number]
|
|
|
|
|
= diff >= 0 ? static_cast<std::int8_t>(diff + 0.5) : static_cast<std::int8_t>(diff - 0.5);
|
|
|
|
|
|
|
|
|
@ -178,23 +178,8 @@ namespace ESM
|
|
|
|
|
if (mDataTypes & Land::DATA_WNAM)
|
|
|
|
|
{
|
|
|
|
|
// Generate WNAM record
|
|
|
|
|
std::int8_t wnam[LAND_GLOBAL_MAP_LOD_SIZE];
|
|
|
|
|
constexpr float max = std::numeric_limits<std::int8_t>::max();
|
|
|
|
|
constexpr float min = std::numeric_limits<std::int8_t>::min();
|
|
|
|
|
constexpr float vertMult
|
|
|
|
|
= static_cast<float>(LandRecordData::sLandSize - 1) / LAND_GLOBAL_MAP_LOD_SIZE_SQRT;
|
|
|
|
|
for (unsigned row = 0; row < LAND_GLOBAL_MAP_LOD_SIZE_SQRT; ++row)
|
|
|
|
|
{
|
|
|
|
|
for (unsigned col = 0; col < LAND_GLOBAL_MAP_LOD_SIZE_SQRT; ++col)
|
|
|
|
|
{
|
|
|
|
|
float height
|
|
|
|
|
= mLandData->mHeights[static_cast<size_t>(row * vertMult) * LandRecordData::sLandSize
|
|
|
|
|
+ static_cast<size_t>(col * vertMult)];
|
|
|
|
|
height /= height > 0 ? 128.f : 16.f;
|
|
|
|
|
height = std::clamp(height, min, max);
|
|
|
|
|
wnam[row * LAND_GLOBAL_MAP_LOD_SIZE_SQRT + col] = static_cast<std::int8_t>(height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
std::array<std::int8_t, sGlobalMapLodSize> wnam;
|
|
|
|
|
generateWnam(mLandData->mHeights, wnam);
|
|
|
|
|
esm.writeHNT("WNAM", wnam);
|
|
|
|
|
}
|
|
|
|
|
if (mDataTypes & Land::DATA_VCLR)
|
|
|
|
@ -219,7 +204,6 @@ namespace ESM
|
|
|
|
|
if (mLandData == nullptr)
|
|
|
|
|
mLandData = std::make_unique<LandData>();
|
|
|
|
|
|
|
|
|
|
mLandData->mHeightOffset = 0;
|
|
|
|
|
mLandData->mHeights.fill(0);
|
|
|
|
|
mLandData->mMinHeight = 0;
|
|
|
|
|
mLandData->mMaxHeight = 0;
|
|
|
|
@ -239,20 +223,20 @@ namespace ESM
|
|
|
|
|
mContext.filename.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Land::loadData(int flags) const
|
|
|
|
|
void Land::loadData(int dataTypes) const
|
|
|
|
|
{
|
|
|
|
|
if (mLandData == nullptr)
|
|
|
|
|
mLandData = std::make_unique<LandData>();
|
|
|
|
|
|
|
|
|
|
loadData(flags, *mLandData);
|
|
|
|
|
loadData(dataTypes, *mLandData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Land::loadData(int flags, LandData& data) const
|
|
|
|
|
void Land::loadData(int dataTypes, LandData& data) const
|
|
|
|
|
{
|
|
|
|
|
// Try to load only available data
|
|
|
|
|
flags = flags & mDataTypes;
|
|
|
|
|
dataTypes = dataTypes & mDataTypes;
|
|
|
|
|
// Return if all required data is loaded
|
|
|
|
|
if ((data.mDataLoaded & flags) == flags)
|
|
|
|
|
if ((data.mDataLoaded & dataTypes) == dataTypes)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -269,57 +253,7 @@ namespace ESM
|
|
|
|
|
ESMReader reader;
|
|
|
|
|
reader.restoreContext(mContext);
|
|
|
|
|
|
|
|
|
|
if (reader.isNextSub("VNML"))
|
|
|
|
|
{
|
|
|
|
|
condLoad(reader, flags, data.mDataLoaded, DATA_VNML, data.mNormals);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reader.isNextSub("VHGT"))
|
|
|
|
|
{
|
|
|
|
|
VHGT vhgt;
|
|
|
|
|
if (condLoad(reader, flags, data.mDataLoaded, DATA_VHGT, vhgt))
|
|
|
|
|
{
|
|
|
|
|
data.mMinHeight = std::numeric_limits<float>::max();
|
|
|
|
|
data.mMaxHeight = -std::numeric_limits<float>::max();
|
|
|
|
|
float rowOffset = vhgt.mHeightOffset;
|
|
|
|
|
for (unsigned y = 0; y < LandRecordData::sLandSize; y++)
|
|
|
|
|
{
|
|
|
|
|
rowOffset += vhgt.mHeightData[y * LandRecordData::sLandSize];
|
|
|
|
|
|
|
|
|
|
data.mHeights[y * LandRecordData::sLandSize] = rowOffset * HEIGHT_SCALE;
|
|
|
|
|
if (rowOffset * HEIGHT_SCALE > data.mMaxHeight)
|
|
|
|
|
data.mMaxHeight = rowOffset * HEIGHT_SCALE;
|
|
|
|
|
if (rowOffset * HEIGHT_SCALE < data.mMinHeight)
|
|
|
|
|
data.mMinHeight = rowOffset * HEIGHT_SCALE;
|
|
|
|
|
|
|
|
|
|
float colOffset = rowOffset;
|
|
|
|
|
for (unsigned x = 1; x < LandRecordData::sLandSize; x++)
|
|
|
|
|
{
|
|
|
|
|
colOffset += vhgt.mHeightData[y * LandRecordData::sLandSize + x];
|
|
|
|
|
data.mHeights[x + y * LandRecordData::sLandSize] = colOffset * HEIGHT_SCALE;
|
|
|
|
|
|
|
|
|
|
if (colOffset * HEIGHT_SCALE > data.mMaxHeight)
|
|
|
|
|
data.mMaxHeight = colOffset * HEIGHT_SCALE;
|
|
|
|
|
if (colOffset * HEIGHT_SCALE < data.mMinHeight)
|
|
|
|
|
data.mMinHeight = colOffset * HEIGHT_SCALE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reader.isNextSub("WNAM"))
|
|
|
|
|
reader.skipHSub();
|
|
|
|
|
|
|
|
|
|
if (reader.isNextSub("VCLR"))
|
|
|
|
|
condLoad(reader, flags, data.mDataLoaded, DATA_VCLR, data.mColours);
|
|
|
|
|
if (reader.isNextSub("VTEX"))
|
|
|
|
|
{
|
|
|
|
|
uint16_t vtex[LandRecordData::sLandNumTextures];
|
|
|
|
|
if (condLoad(reader, flags, data.mDataLoaded, DATA_VTEX, vtex))
|
|
|
|
|
{
|
|
|
|
|
transposeTextureData(vtex, data.mTextures.data());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
loadLandRecordData(dataTypes, reader, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Land::unloadData()
|
|
|
|
@ -367,4 +301,75 @@ namespace ESM
|
|
|
|
|
mDataTypes |= flags;
|
|
|
|
|
mLandData->mDataLoaded |= flags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loadLandRecordData(int dataTypes, ESMReader& reader, LandRecordData& data)
|
|
|
|
|
{
|
|
|
|
|
if (reader.isNextSub("VNML"))
|
|
|
|
|
condLoad(reader, dataTypes, data.mDataLoaded, Land::DATA_VNML, data.mNormals);
|
|
|
|
|
|
|
|
|
|
if (reader.isNextSub("VHGT"))
|
|
|
|
|
{
|
|
|
|
|
VHGT vhgt;
|
|
|
|
|
if (condLoad(reader, dataTypes, data.mDataLoaded, Land::DATA_VHGT, vhgt))
|
|
|
|
|
{
|
|
|
|
|
data.mMinHeight = std::numeric_limits<float>::max();
|
|
|
|
|
data.mMaxHeight = -std::numeric_limits<float>::max();
|
|
|
|
|
float rowOffset = vhgt.mHeightOffset;
|
|
|
|
|
for (unsigned y = 0; y < LandRecordData::sLandSize; y++)
|
|
|
|
|
{
|
|
|
|
|
rowOffset += vhgt.mHeightData[y * LandRecordData::sLandSize];
|
|
|
|
|
|
|
|
|
|
data.mHeights[y * LandRecordData::sLandSize] = rowOffset * Land::sHeightScale;
|
|
|
|
|
if (rowOffset * Land::sHeightScale > data.mMaxHeight)
|
|
|
|
|
data.mMaxHeight = rowOffset * Land::sHeightScale;
|
|
|
|
|
if (rowOffset * Land::sHeightScale < data.mMinHeight)
|
|
|
|
|
data.mMinHeight = rowOffset * Land::sHeightScale;
|
|
|
|
|
|
|
|
|
|
float colOffset = rowOffset;
|
|
|
|
|
for (unsigned x = 1; x < LandRecordData::sLandSize; x++)
|
|
|
|
|
{
|
|
|
|
|
colOffset += vhgt.mHeightData[y * LandRecordData::sLandSize + x];
|
|
|
|
|
data.mHeights[x + y * LandRecordData::sLandSize] = colOffset * Land::sHeightScale;
|
|
|
|
|
|
|
|
|
|
if (colOffset * Land::sHeightScale > data.mMaxHeight)
|
|
|
|
|
data.mMaxHeight = colOffset * Land::sHeightScale;
|
|
|
|
|
if (colOffset * Land::sHeightScale < data.mMinHeight)
|
|
|
|
|
data.mMinHeight = colOffset * Land::sHeightScale;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reader.isNextSub("WNAM"))
|
|
|
|
|
reader.skipHSub();
|
|
|
|
|
|
|
|
|
|
if (reader.isNextSub("VCLR"))
|
|
|
|
|
condLoad(reader, dataTypes, data.mDataLoaded, Land::DATA_VCLR, data.mColours);
|
|
|
|
|
|
|
|
|
|
if (reader.isNextSub("VTEX"))
|
|
|
|
|
{
|
|
|
|
|
std::uint16_t vtex[LandRecordData::sLandNumTextures];
|
|
|
|
|
if (condLoad(reader, dataTypes, data.mDataLoaded, Land::DATA_VTEX, vtex))
|
|
|
|
|
transposeTextureData(vtex, data.mTextures.data());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void generateWnam(const std::array<float, LandRecordData::sLandNumVerts>& heights,
|
|
|
|
|
std::array<std::int8_t, Land::sGlobalMapLodSize>& wnam)
|
|
|
|
|
{
|
|
|
|
|
constexpr float max = std::numeric_limits<std::int8_t>::max();
|
|
|
|
|
constexpr float min = std::numeric_limits<std::int8_t>::min();
|
|
|
|
|
constexpr float vertMult = static_cast<float>(LandRecordData::sLandSize - 1) / Land::sGlobalMapLodSizeSqrt;
|
|
|
|
|
for (std::size_t row = 0; row < Land::sGlobalMapLodSizeSqrt; ++row)
|
|
|
|
|
{
|
|
|
|
|
for (std::size_t col = 0; col < Land::sGlobalMapLodSizeSqrt; ++col)
|
|
|
|
|
{
|
|
|
|
|
float height = heights[static_cast<std::size_t>(row * vertMult) * LandRecordData::sLandSize
|
|
|
|
|
+ static_cast<std::size_t>(col * vertMult)];
|
|
|
|
|
height /= height > 0 ? 128.f : 16.f;
|
|
|
|
|
height = std::clamp(height, min, max);
|
|
|
|
|
wnam[row * Land::sGlobalMapLodSizeSqrt + col] = static_cast<std::int8_t>(height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|